From 48adc0b7bfdaf4bd0a6b0b200ace59750b43c176 Mon Sep 17 00:00:00 2001 From: kruland2607 Date: Wed, 9 Jan 2013 21:28:53 -0600 Subject: [PATCH] Rework the AttachmentFactory and Attachments. Make the Attachment implementations stand on their own and be able to determine the location of their bytes without needing a reference back to the AttachmentFactory. This required changing the loading semantics so the OpenRocketDocument is created prior to beginning to read the xml file. Added an OpenRocketDocumentFactory to facilitate construction of OpenRocketDocument objects. --- .../sf/openrocket/document/Attachment.java | 2 +- .../document/BaseAttachmentFactory.java | 135 ---- .../sf/openrocket/document/DecalRegistry.java | 17 +- .../document/FileSystemAttachmentFactory.java | 41 ++ .../document/OpenRocketDocument.java | 60 +- .../document/OpenRocketDocumentFactory.java | 43 ++ .../document/ZipFileAttachmentFactory.java | 20 + .../document/attachments/BaseAttachment.java | 40 ++ .../attachments/FileSystemAttachment.java | 25 + .../attachments/ZipFileAttachment.java | 45 ++ .../openrocket/file/AbstractRocketLoader.java | 10 +- core/src/net/sf/openrocket/file/FileInfo.java | 35 - .../openrocket/file/GeneralRocketLoader.java | 93 ++- .../net/sf/openrocket/file/RocketLoader.java | 2 +- .../importt/OpenRocketContentHandler.java | 12 +- .../openrocket/importt/OpenRocketLoader.java | 6 +- .../file/rocksim/importt/RocksimHandler.java | 598 +++++++++--------- .../file/rocksim/importt/RocksimLoader.java | 10 +- .../CustomExpressionPanel.java | 3 +- .../sf/openrocket/gui/main/BasicFrame.java | 18 +- .../openrocket/gui/util/OpenFileWorker.java | 3 +- .../sf/openrocket/utils/RocksimConverter.java | 3 +- .../net/sf/openrocket/IntegrationTest.java | 6 +- .../importt/RocksimContentHandlerTest.java | 26 - .../rocksim/importt/RocksimLoaderTest.java | 20 +- .../rocketcomponent/ComponentCompare.java | 17 +- .../customexpression/TestExpressions.java | 12 +- 27 files changed, 681 insertions(+), 621 deletions(-) delete mode 100644 core/src/net/sf/openrocket/document/BaseAttachmentFactory.java create mode 100644 core/src/net/sf/openrocket/document/FileSystemAttachmentFactory.java create mode 100644 core/src/net/sf/openrocket/document/OpenRocketDocumentFactory.java create mode 100644 core/src/net/sf/openrocket/document/ZipFileAttachmentFactory.java create mode 100644 core/src/net/sf/openrocket/document/attachments/BaseAttachment.java create mode 100644 core/src/net/sf/openrocket/document/attachments/FileSystemAttachment.java create mode 100644 core/src/net/sf/openrocket/document/attachments/ZipFileAttachment.java delete mode 100644 core/src/net/sf/openrocket/file/FileInfo.java delete mode 100644 core/test/net/sf/openrocket/file/rocksim/importt/RocksimContentHandlerTest.java diff --git a/core/src/net/sf/openrocket/document/Attachment.java b/core/src/net/sf/openrocket/document/Attachment.java index 50e96de55..a684f1cf7 100644 --- a/core/src/net/sf/openrocket/document/Attachment.java +++ b/core/src/net/sf/openrocket/document/Attachment.java @@ -4,7 +4,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; -public interface Attachment { +public interface Attachment extends Comparable { public abstract String getName(); diff --git a/core/src/net/sf/openrocket/document/BaseAttachmentFactory.java b/core/src/net/sf/openrocket/document/BaseAttachmentFactory.java deleted file mode 100644 index 077fedbc0..000000000 --- a/core/src/net/sf/openrocket/document/BaseAttachmentFactory.java +++ /dev/null @@ -1,135 +0,0 @@ -package net.sf.openrocket.document; - -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; - -import net.sf.openrocket.file.FileInfo; -import net.sf.openrocket.util.FileUtils; - -public class BaseAttachmentFactory implements AttachmentFactory { - - private FileInfo fileInfo; - private boolean isZipFile = false; - - public void setBaseFile(FileInfo fileInfo) { - this.fileInfo = fileInfo; - } - - public void setIsZipFile(boolean isZipFile) { - this.isZipFile = isZipFile; - } - - public class BaseAttachment implements Attachment, Comparable { - - protected String name; - - BaseAttachment(String name) { - this.name = name; - } - - @Override - public String getName() { - return name; - } - - @Override - public InputStream getBytes() throws FileNotFoundException, IOException { - return BaseAttachmentFactory.this.getBytes(this); - } - - @Override - public int compareTo(Object o) { - if (!(o instanceof BaseAttachment)) { - return -1; - } - return this.name.compareTo(((BaseAttachment) o).name); - } - - @Override - public String toString() { - return getName(); - } - - } - - @Override - public BaseAttachment getAttachment(String name) { - return new BaseAttachment(name); - } - - /** - * This function returns an InputStream backed by a byte[] containing the decal pixels. - * If it reads in the bytes from an actual file, the underlying file is closed. - * - * @param name - * @return - * @throws FileNotFoundException - * @throws IOException - */ - private InputStream getBytes(BaseAttachment attachment) throws FileNotFoundException, IOException { - - // This is the InputStream to be returned. - InputStream rawIs = null; - - - String name = attachment.getName(); - - if (rawIs == null && isZipFile) { - rawIs = findInZipContainer(name); - } - - // Try relative to the model file directory. This is so we can support unzipped container format. - if (rawIs == null) { - if (fileInfo != null && fileInfo.getDirectory() != null) { - File decalFile = new File(fileInfo.getDirectory(), name); - rawIs = new FileInputStream(decalFile); - } - } - - if (rawIs == null) { - throw new FileNotFoundException("Unable to locate decal for name " + name); - } - - try { - byte[] bytes = FileUtils.readBytes(rawIs); - return new ByteArrayInputStream(bytes); - } finally { - rawIs.close(); - } - - } - - private ZipInputStream findInZipContainer(String name) { - ZipInputStream zis = null; - try { - zis = new ZipInputStream(fileInfo.getFileURL().openStream()); - } catch (IOException ex) { - return null; - } - try { - ZipEntry entry = zis.getNextEntry(); - while (entry != null) { - if (entry.getName().equals(name)) { - return zis; - } - entry = zis.getNextEntry(); - } - zis.close(); - return null; - } catch (IOException ioex) { - try { - zis.close(); - } catch (IOException ex) { - // why does close throw? it's maddening - } - return null; - } - } - -} diff --git a/core/src/net/sf/openrocket/document/DecalRegistry.java b/core/src/net/sf/openrocket/document/DecalRegistry.java index b7510caf7..95f64798d 100644 --- a/core/src/net/sf/openrocket/document/DecalRegistry.java +++ b/core/src/net/sf/openrocket/document/DecalRegistry.java @@ -19,7 +19,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import net.sf.openrocket.appearance.DecalImage; -import net.sf.openrocket.document.BaseAttachmentFactory.BaseAttachment; import net.sf.openrocket.logging.LogHelper; import net.sf.openrocket.startup.Application; import net.sf.openrocket.util.BugException; @@ -28,9 +27,9 @@ import net.sf.openrocket.util.FileUtils; public class DecalRegistry implements AttachmentFactory { private static LogHelper log = Application.getLogger(); - private final BaseAttachmentFactory baseFactory; + private final AttachmentFactory baseFactory; - public DecalRegistry(BaseAttachmentFactory baseFactory) { + public DecalRegistry(AttachmentFactory baseFactory) { this.baseFactory = baseFactory; } @@ -39,7 +38,7 @@ public class DecalRegistry implements AttachmentFactory { public DecalImage getAttachment(String decalName) { DecalImageImpl d = registeredDecals.get(decalName); if (d == null) { - BaseAttachment attachment = baseFactory.getAttachment(decalName); + Attachment attachment = baseFactory.getAttachment(decalName); d = new DecalImageImpl(attachment); registeredDecals.put(decalName, d); } @@ -58,7 +57,7 @@ public class DecalRegistry implements AttachmentFactory { // It's a new file, generate a name for it. String decalName = makeUniqueName(file.getName()); - BaseAttachment attachment = baseFactory.getAttachment(decalName); + Attachment attachment = baseFactory.getAttachment(decalName); decal = new DecalImageImpl(attachment); decal.setFileSystemLocation(file); @@ -76,13 +75,13 @@ public class DecalRegistry implements AttachmentFactory { return decals; } - public class DecalImageImpl implements DecalImage, Comparable { + public class DecalImageImpl implements DecalImage { - private final BaseAttachment delegate; + private final Attachment delegate; private File fileSystemLocation; - private DecalImageImpl(BaseAttachment delegate) { + private DecalImageImpl(Attachment delegate) { this.delegate = delegate; } @@ -116,7 +115,7 @@ public class DecalRegistry implements AttachmentFactory { } @Override - public int compareTo(Object o) { + public int compareTo(Attachment o) { if (!(o instanceof DecalImageImpl)) { return -1; } diff --git a/core/src/net/sf/openrocket/document/FileSystemAttachmentFactory.java b/core/src/net/sf/openrocket/document/FileSystemAttachmentFactory.java new file mode 100644 index 000000000..c68c4be26 --- /dev/null +++ b/core/src/net/sf/openrocket/document/FileSystemAttachmentFactory.java @@ -0,0 +1,41 @@ +package net.sf.openrocket.document; + +import java.io.File; + +import net.sf.openrocket.document.attachments.BaseAttachment; +import net.sf.openrocket.document.attachments.FileSystemAttachment; + +public class FileSystemAttachmentFactory implements AttachmentFactory { + + private final File baseDirectory; + + public FileSystemAttachmentFactory() { + super(); + this.baseDirectory = null; + } + + public FileSystemAttachmentFactory(File baseDirectory) { + super(); + if (baseDirectory != null && baseDirectory.isDirectory() == false) { + throw new IllegalArgumentException("Base file for FileSystemAttachmentFactory is not a directory"); + } + this.baseDirectory = baseDirectory; + } + + @Override + public BaseAttachment getAttachment(String name) { + + File file = new File(name); + + if (file.isAbsolute()) { + return new FileSystemAttachment(name, file); + } + + else { + file = new File(baseDirectory, name); + return new FileSystemAttachment(name, file); + } + + } + +} diff --git a/core/src/net/sf/openrocket/document/OpenRocketDocument.java b/core/src/net/sf/openrocket/document/OpenRocketDocument.java index 17309d2d7..e1536d9c3 100644 --- a/core/src/net/sf/openrocket/document/OpenRocketDocument.java +++ b/core/src/net/sf/openrocket/document/OpenRocketDocument.java @@ -1,6 +1,8 @@ package net.sf.openrocket.document; import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Collections; import java.util.LinkedHashSet; import java.util.LinkedList; @@ -10,7 +12,6 @@ import java.util.Set; import net.sf.openrocket.document.events.DocumentChangeEvent; import net.sf.openrocket.document.events.DocumentChangeListener; import net.sf.openrocket.document.events.SimulationChangeEvent; -import net.sf.openrocket.file.FileInfo; import net.sf.openrocket.logging.LogHelper; import net.sf.openrocket.logging.TraceException; import net.sf.openrocket.rocketcomponent.ComponentChangeEvent; @@ -59,8 +60,8 @@ public class OpenRocketDocument implements ComponentChangeListener { private final ArrayList simulations = new ArrayList(); private ArrayList customExpressions = new ArrayList(); - private BaseAttachmentFactory attachmentFactory = new BaseAttachmentFactory(); - private DecalRegistry decalRegistry = new DecalRegistry(attachmentFactory); + private final AttachmentFactory attachmentFactory; + private final DecalRegistry decalRegistry; /* * The undo/redo variables and mechanism are documented in doc/undo-redo-flow.* @@ -95,34 +96,44 @@ public class OpenRocketDocument implements ComponentChangeListener { private final StorageOptions storageOptions = new StorageOptions(); - private final List listeners = - new ArrayList(); + private final List listeners = new ArrayList(); - public OpenRocketDocument(Rocket rocket) { - this(rocket.getDefaultConfiguration()); + OpenRocketDocument(Rocket rocket, File fileName, boolean isContainer) { + this.configuration = rocket.getDefaultConfiguration(); + this.rocket = rocket; + AttachmentFactory attachments; + if (isContainer) { + try { + attachments = new ZipFileAttachmentFactory(fileName.toURI().toURL()); + } catch (MalformedURLException mex) { + attachments = new FileSystemAttachmentFactory(null); + } + } else { + if (fileName == null) { + attachments = new FileSystemAttachmentFactory(null); + } else { + attachments = new FileSystemAttachmentFactory(fileName.getParentFile()); + } + } + this.attachmentFactory = attachments; + this.decalRegistry = new DecalRegistry(this.attachmentFactory); + init(); } + OpenRocketDocument(Rocket rocket, URL jarURL, boolean isContainer) { + this.configuration = rocket.getDefaultConfiguration(); + this.rocket = rocket; + this.attachmentFactory = isContainer ? new ZipFileAttachmentFactory(jarURL) : new FileSystemAttachmentFactory(null); + this.decalRegistry = new DecalRegistry(this.attachmentFactory); + init(); + } - private OpenRocketDocument(Configuration configuration) { - this.configuration = configuration; - this.rocket = configuration.getRocket(); - + private void init() { clearUndo(); rocket.addComponentChangeListener(this); } - - public void setBaseFile(FileInfo fileInfo) { - attachmentFactory.setBaseFile(fileInfo); - } - - - public void setIsZipFile(boolean isZipFile) { - attachmentFactory.setIsZipFile(isZipFile); - } - - public void addCustomExpression(CustomExpression expression) { if (customExpressions.contains(expression)) { log.user("Could not add custom expression " + expression.getName() + " to document as document alerady has a matching expression."); @@ -553,11 +564,14 @@ public class OpenRocketDocument implements ComponentChangeListener { * motor configuration ID is maintained and the simulations are copied to the new rocket. * No undo/redo information or file storage information is maintained. * + * This function is used from the Optimization routine to store alternatives of the same rocket. + * For now we can assume that the copy returned does not have any of the attachment factories in place. + * * @return a copy of this document. */ public OpenRocketDocument copy() { Rocket rocketCopy = rocket.copyWithOriginalID(); - OpenRocketDocument documentCopy = new OpenRocketDocument(rocketCopy); + OpenRocketDocument documentCopy = OpenRocketDocumentFactory.createDocumentFromRocket(rocketCopy); documentCopy.getDefaultConfiguration().setFlightConfigurationID(configuration.getFlightConfigurationID()); for (Simulation s : simulations) { documentCopy.addSimulation(s.duplicateSimulation(rocketCopy)); diff --git a/core/src/net/sf/openrocket/document/OpenRocketDocumentFactory.java b/core/src/net/sf/openrocket/document/OpenRocketDocumentFactory.java new file mode 100644 index 000000000..bb7c1376f --- /dev/null +++ b/core/src/net/sf/openrocket/document/OpenRocketDocumentFactory.java @@ -0,0 +1,43 @@ +package net.sf.openrocket.document; + +import java.io.File; +import java.net.URL; + +import net.sf.openrocket.l10n.Translator; +import net.sf.openrocket.rocketcomponent.Rocket; +import net.sf.openrocket.rocketcomponent.Stage; +import net.sf.openrocket.startup.Application; + +public class OpenRocketDocumentFactory { + + private static final Translator trans = Application.getTranslator(); + + public static OpenRocketDocument createNewRocket() { + Rocket rocket = new Rocket(); + Stage stage = new Stage(); + //// Sustainer + stage.setName(trans.get("BasicFrame.StageName.Sustainer")); + rocket.addChild(stage); + OpenRocketDocument doc = new OpenRocketDocument(rocket, (File) null, false); + doc.setSaved(true); + return doc; + } + + public static OpenRocketDocument createDocumentFromRocket(Rocket r) { + OpenRocketDocument doc = new OpenRocketDocument(r, (File) null, false); + return doc; + } + + public static OpenRocketDocument createDocumentForFile(File filename, boolean isContainer) { + Rocket rocket = new Rocket(); + OpenRocketDocument doc = new OpenRocketDocument(rocket, filename, isContainer); + return doc; + } + + public static OpenRocketDocument createDocumentForUrl(URL filename, boolean isContainer) { + Rocket rocket = new Rocket(); + OpenRocketDocument doc = new OpenRocketDocument(rocket, filename, isContainer); + return doc; + } + +} diff --git a/core/src/net/sf/openrocket/document/ZipFileAttachmentFactory.java b/core/src/net/sf/openrocket/document/ZipFileAttachmentFactory.java new file mode 100644 index 000000000..173412b14 --- /dev/null +++ b/core/src/net/sf/openrocket/document/ZipFileAttachmentFactory.java @@ -0,0 +1,20 @@ +package net.sf.openrocket.document; + +import java.net.URL; + +import net.sf.openrocket.document.attachments.ZipFileAttachment; + +public class ZipFileAttachmentFactory implements AttachmentFactory { + + private final URL zipFile; + + public ZipFileAttachmentFactory(URL zipFile) { + super(); + this.zipFile = zipFile; + } + + @Override + public Attachment getAttachment(String name) { + return new ZipFileAttachment(name, zipFile); + } +} diff --git a/core/src/net/sf/openrocket/document/attachments/BaseAttachment.java b/core/src/net/sf/openrocket/document/attachments/BaseAttachment.java new file mode 100644 index 000000000..549b5800d --- /dev/null +++ b/core/src/net/sf/openrocket/document/attachments/BaseAttachment.java @@ -0,0 +1,40 @@ +package net.sf.openrocket.document.attachments; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; + +import net.sf.openrocket.document.Attachment; + +public abstract class BaseAttachment implements Attachment { + + private final String name; + + public BaseAttachment(String name) { + super(); + this.name = name; + } + + @Override + public final String getName() { + return name; + } + + @Override + public abstract InputStream getBytes() throws FileNotFoundException, IOException; + + @Override + public int compareTo(Attachment o) { + if (!(o instanceof BaseAttachment)) { + return -1; + } + return this.name.compareTo(((BaseAttachment) o).name); + } + + @Override + public String toString() { + return getName(); + } + + +} diff --git a/core/src/net/sf/openrocket/document/attachments/FileSystemAttachment.java b/core/src/net/sf/openrocket/document/attachments/FileSystemAttachment.java new file mode 100644 index 000000000..50fdae583 --- /dev/null +++ b/core/src/net/sf/openrocket/document/attachments/FileSystemAttachment.java @@ -0,0 +1,25 @@ +package net.sf.openrocket.document.attachments; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; + +import net.sf.openrocket.document.Attachment; + +public class FileSystemAttachment extends BaseAttachment implements Attachment { + + private final File location; + + public FileSystemAttachment(String name, File location) { + super(name); + this.location = location; + } + + @Override + public InputStream getBytes() throws FileNotFoundException, IOException { + return new FileInputStream(location); + } + +} diff --git a/core/src/net/sf/openrocket/document/attachments/ZipFileAttachment.java b/core/src/net/sf/openrocket/document/attachments/ZipFileAttachment.java new file mode 100644 index 000000000..a2a5021e2 --- /dev/null +++ b/core/src/net/sf/openrocket/document/attachments/ZipFileAttachment.java @@ -0,0 +1,45 @@ +package net.sf.openrocket.document.attachments; + +import java.io.ByteArrayInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import net.sf.openrocket.document.Attachment; +import net.sf.openrocket.util.FileUtils; + +public class ZipFileAttachment extends BaseAttachment implements Attachment { + + private final URL zipFileLocation; + + public ZipFileAttachment(String name, URL zipFileLocation) { + super(name); + this.zipFileLocation = zipFileLocation; + } + + @Override + public InputStream getBytes() throws FileNotFoundException, IOException { + String name = getName(); + + ZipInputStream zis = new ZipInputStream(zipFileLocation.openStream()); + + try { + ZipEntry entry = zis.getNextEntry(); + while (entry != null) { + if (entry.getName().equals(name)) { + byte[] bytes = FileUtils.readBytes(zis); + return new ByteArrayInputStream(bytes); + } + entry = zis.getNextEntry(); + } + throw new FileNotFoundException("Unable to locate decal for name " + name); + } finally { + zis.close(); + } + + } + +} diff --git a/core/src/net/sf/openrocket/file/AbstractRocketLoader.java b/core/src/net/sf/openrocket/file/AbstractRocketLoader.java index c4804e3bd..599e3f62b 100644 --- a/core/src/net/sf/openrocket/file/AbstractRocketLoader.java +++ b/core/src/net/sf/openrocket/file/AbstractRocketLoader.java @@ -1,9 +1,5 @@ package net.sf.openrocket.file; -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -19,11 +15,11 @@ public abstract class AbstractRocketLoader implements RocketLoader { * Loads a rocket from the specified InputStream. */ @Override - public final OpenRocketDocument load(InputStream source, MotorFinder motorFinder) throws RocketLoadException { + public final void load(OpenRocketDocument doc, InputStream source, MotorFinder motorFinder) throws RocketLoadException { warnings.clear(); try { - return loadFromStream(source, motorFinder); + loadFromStream(doc, source, motorFinder); } catch (RocketLoadException e) { throw e; } catch (IOException e) { @@ -39,7 +35,7 @@ public abstract class AbstractRocketLoader implements RocketLoader { * * @throws RocketLoadException if an error occurs during loading. */ - protected abstract OpenRocketDocument loadFromStream(InputStream source, MotorFinder motorFinder) throws IOException, + protected abstract void loadFromStream(OpenRocketDocument doc, InputStream source, MotorFinder motorFinder) throws IOException, RocketLoadException; diff --git a/core/src/net/sf/openrocket/file/FileInfo.java b/core/src/net/sf/openrocket/file/FileInfo.java deleted file mode 100644 index fd37488db..000000000 --- a/core/src/net/sf/openrocket/file/FileInfo.java +++ /dev/null @@ -1,35 +0,0 @@ -package net.sf.openrocket.file; - -import java.io.File; -import java.net.MalformedURLException; -import java.net.URL; - -public class FileInfo { - - public final URL fileURL; - public final File directory; - - public FileInfo(File sourceFile) { - URL theURL = null; - try { - theURL = sourceFile.toURI().toURL(); - } catch (MalformedURLException mex) { - } - this.fileURL = theURL; - this.directory = sourceFile.getParentFile(); - } - - public FileInfo(URL sourceURL) { - this.fileURL = sourceURL; - this.directory = null; - } - - public URL getFileURL() { - return fileURL; - } - - public File getDirectory() { - return directory; - } - -} diff --git a/core/src/net/sf/openrocket/file/GeneralRocketLoader.java b/core/src/net/sf/openrocket/file/GeneralRocketLoader.java index e1658cd77..d73e0b2ab 100644 --- a/core/src/net/sf/openrocket/file/GeneralRocketLoader.java +++ b/core/src/net/sf/openrocket/file/GeneralRocketLoader.java @@ -13,6 +13,7 @@ import java.util.zip.ZipInputStream; import net.sf.openrocket.aerodynamics.WarningSet; import net.sf.openrocket.document.OpenRocketDocument; +import net.sf.openrocket.document.OpenRocketDocumentFactory; import net.sf.openrocket.file.openrocket.importt.OpenRocketLoader; import net.sf.openrocket.file.rocksim.importt.RocksimLoader; import net.sf.openrocket.util.ArrayUtils; @@ -38,33 +39,41 @@ public class GeneralRocketLoader { private static final byte[] ROCKSIM_SIGNATURE = TextUtil.asciiBytes(" tag. */ class OpenRocketContentHandler extends AbstractElementHandler { private final DocumentLoadingContext context; - private final OpenRocketDocument doc; - private final Rocket rocket; private boolean rocketDefined = false; private boolean simulationsDefined = false; @@ -23,15 +20,12 @@ class OpenRocketContentHandler extends AbstractElementHandler { public OpenRocketContentHandler(DocumentLoadingContext context) { this.context = context; - this.rocket = new Rocket(); - this.doc = new OpenRocketDocument(rocket); - context.setOpenRocketDocument(doc); } public OpenRocketDocument getDocument() { if (!rocketDefined) return null; - return doc; + return context.getOpenRocketDocument(); } @Override @@ -46,7 +40,7 @@ class OpenRocketContentHandler extends AbstractElementHandler { return null; } rocketDefined = true; - return new ComponentParameterHandler(rocket, context); + return new ComponentParameterHandler(getDocument().getRocket(), context); } if (element.equals("datatypes")) { @@ -66,7 +60,7 @@ class OpenRocketContentHandler extends AbstractElementHandler { return null; } simulationsDefined = true; - return new SimulationsHandler(doc, context); + return new SimulationsHandler(getDocument(), context); } warnings.add(Warning.fromString("Unknown element " + element + ", ignoring.")); diff --git a/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java b/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java index 81fb57329..0f634d983 100644 --- a/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java +++ b/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java @@ -35,11 +35,12 @@ public class OpenRocketLoader extends AbstractRocketLoader { @Override - public OpenRocketDocument loadFromStream(InputStream source, MotorFinder motorFinder) throws RocketLoadException, + public void loadFromStream(OpenRocketDocument doc, InputStream source, MotorFinder motorFinder) throws RocketLoadException, IOException { log.info("Loading .ork file"); DocumentLoadingContext context = new DocumentLoadingContext(); context.setMotorFinder(motorFinder); + context.setOpenRocketDocument(doc); InputSource xmlSource = new InputSource(source); OpenRocketHandler handler = new OpenRocketHandler(context); @@ -52,8 +53,6 @@ public class OpenRocketLoader extends AbstractRocketLoader { throw new RocketLoadException("Malformed XML in input.", e); } - - OpenRocketDocument doc = handler.getDocument(); doc.getDefaultConfiguration().setAllStages(); // Deduce suitable time skip @@ -88,7 +87,6 @@ public class OpenRocketLoader extends AbstractRocketLoader { doc.clearUndo(); log.info("Loading done"); - return doc; } } diff --git a/core/src/net/sf/openrocket/file/rocksim/importt/RocksimHandler.java b/core/src/net/sf/openrocket/file/rocksim/importt/RocksimHandler.java index 69c9b061b..cd5229453 100644 --- a/core/src/net/sf/openrocket/file/rocksim/importt/RocksimHandler.java +++ b/core/src/net/sf/openrocket/file/rocksim/importt/RocksimHandler.java @@ -27,118 +27,123 @@ import org.xml.sax.SAXException; * Limitations: Rocksim flight simulations are not imported; tube fins are not supported; Rocksim 'pods' are not supported. */ public class RocksimHandler extends AbstractElementHandler { - - /** - * The main content handler. - */ - private RocksimContentHandler handler = null; - - /** - * Return the OpenRocketDocument read from the file, or null if a document - * has not been read yet. - * - * @return the document read, or null. - */ - public OpenRocketDocument getDocument() { - return handler.getDocument(); - } - - @Override - public ElementHandler openElement(String element, HashMap attributes, - WarningSet warnings) { - - // Check for unknown elements - if (!element.equals("RockSimDocument")) { - warnings.add(Warning.fromString("Unknown element " + element + ", ignoring.")); - return null; - } - - // Check for first call - if (handler != null) { - warnings.add(Warning.fromString("Multiple document elements found, ignoring later " - + "ones.")); - return null; - } - - handler = new RocksimContentHandler(); - return handler; - } - + + /** + * The main content handler. + */ + private RocksimContentHandler handler = null; + + private final OpenRocketDocument document; + + public RocksimHandler(OpenRocketDocument document) { + super(); + this.document = document; + } + + /** + * Return the OpenRocketDocument read from the file, or null if a document + * has not been read yet. + * + * @return the document read, or null. + */ + public OpenRocketDocument getDocument() { + return document; + } + + @Override + public ElementHandler openElement(String element, HashMap attributes, + WarningSet warnings) { + + // Check for unknown elements + if (!element.equals("RockSimDocument")) { + warnings.add(Warning.fromString("Unknown element " + element + ", ignoring.")); + return null; + } + + // Check for first call + if (handler != null) { + warnings.add(Warning.fromString("Multiple document elements found, ignoring later " + + "ones.")); + return null; + } + + handler = new RocksimContentHandler(document); + return handler; + } + } /** * Handles the content of the tag. */ class RocksimContentHandler extends AbstractElementHandler { - /** - * The OpenRocketDocument that is the container for the rocket. - */ - private final OpenRocketDocument doc; - - /** - * The top-level component, from which all child components are added. - */ - private final Rocket rocket; - - /** - * The rocksim file version. - */ - private String version; - - /** - * Constructor. - */ - public RocksimContentHandler() { - this.rocket = new Rocket(); - this.doc = new OpenRocketDocument(rocket); - } - - /** - * Get the OpenRocket document that has been created from parsing the Rocksim design file. - * - * @return the instantiated OpenRocketDocument - */ - public OpenRocketDocument getDocument() { - return doc; - } - - @Override - public ElementHandler openElement(String element, HashMap attributes, - WarningSet warnings) { - if (RocksimCommonConstants.DESIGN_INFORMATION.equals(element)) { - //The next sub-element is "RocketDesign", which is really the only thing that matters. Rather than - //create another handler just for that element, handle it here. - return this; - } - if (RocksimCommonConstants.FILE_VERSION.equals(element)) { - return PlainTextHandler.INSTANCE; - } - if (RocksimCommonConstants.ROCKET_DESIGN.equals(element)) { - return new RocketDesignHandler(doc, rocket); - } - return null; - } - - @Override - public void closeElement(String element, HashMap attributes, - String content, WarningSet warnings) throws SAXException { - /** - * SAX handler for Rocksim file version number. The value is not used currently, but could be used in the future - * for backward/forward compatibility reasons (different lower level handlers could be called via a strategy pattern). - */ - if (RocksimCommonConstants.FILE_VERSION.equals(element)) { - version = content; - } - } - - /** - * Answer the file version. - * - * @return the version of the Rocksim design file - */ - public String getVersion() { - return version; - } + /** + * The OpenRocketDocument that is the container for the rocket. + */ + private final OpenRocketDocument doc; + + /** + * The top-level component, from which all child components are added. + */ + private final Rocket rocket; + + /** + * The rocksim file version. + */ + private String version; + + public RocksimContentHandler(OpenRocketDocument doc) { + super(); + this.doc = doc; + this.rocket = doc.getRocket(); + } + + /** + * Get the OpenRocket document that has been created from parsing the Rocksim design file. + * + * @return the instantiated OpenRocketDocument + */ + public OpenRocketDocument getDocument() { + return doc; + } + + @Override + public ElementHandler openElement(String element, HashMap attributes, + WarningSet warnings) { + if (RocksimCommonConstants.DESIGN_INFORMATION.equals(element)) { + //The next sub-element is "RocketDesign", which is really the only thing that matters. Rather than + //create another handler just for that element, handle it here. + return this; + } + if (RocksimCommonConstants.FILE_VERSION.equals(element)) { + return PlainTextHandler.INSTANCE; + } + if (RocksimCommonConstants.ROCKET_DESIGN.equals(element)) { + return new RocketDesignHandler(doc, rocket); + } + return null; + } + + @Override + public void closeElement(String element, HashMap attributes, + String content, WarningSet warnings) throws SAXException { + /** + * SAX handler for Rocksim file version number. The value is not used currently, but could be used in the future + * for backward/forward compatibility reasons (different lower level handlers could be called via a strategy pattern). + */ + if (RocksimCommonConstants.FILE_VERSION.equals(element)) { + version = content; + } + } + + /** + * Answer the file version. + * + * @return the version of the Rocksim design file + */ + public String getVersion() { + return version; + } } @@ -149,165 +154,164 @@ class RocksimContentHandler extends AbstractElementHandler { */ class RocketDesignHandler extends AbstractElementHandler { private final OpenRocketDocument document; - /** - * The parent component. - */ - private final RocketComponent component; - /** - * The parsed stage count. Defaults to 1. - */ - private int stageCount = 1; - /** - * The overridden stage 1 mass. - */ - private double stage1Mass = 0d; - /** - * The overridden stage 2 mass. - */ - private double stage2Mass = 0d; - /** - * The overridden stage 3 mass. - */ - private double stage3Mass = 0d; - /** - * The overridden stage 1 Cg. - */ - private double stage1CG = 0d; - /** - * The overridden stage 2 Cg. - */ - private double stage2CG = 0d; - /** - * The overridden stage 3 Cg. - */ - private double stage3CG = 0d; - - /** - * Constructor. - * - * @param c the parent component - */ - public RocketDesignHandler(OpenRocketDocument document, RocketComponent c) { - this.document = document; - component = c; - } - - @Override - public ElementHandler openElement(String element, HashMap attributes, WarningSet warnings) { - /** - * In Rocksim stages are from the top down, so a single stage rocket is actually stage '3'. A 2-stage - * rocket defines stage '2' as the initial booster with stage '3' sitting atop it. And so on. - */ - if ("Stage3Parts".equals(element)) { - final Stage stage = new Stage(); - if (stage3Mass > 0.0d) { - stage.setMassOverridden(true); - stage.setOverrideSubcomponents(true); //Rocksim does not support this type of override - stage.setOverrideMass(stage3Mass); - } - if (stage3CG > 0.0d) { - stage.setCGOverridden(true); - stage.setOverrideSubcomponents(true); //Rocksim does not support this type of override - stage.setOverrideCGX(stage3CG); - } - component.addChild(stage); - return new StageHandler(document, stage); - } - if ("Stage2Parts".equals(element)) { - if (stageCount >= 2) { - final Stage stage = new Stage(); - if (stage2Mass > 0.0d) { - stage.setMassOverridden(true); - stage.setOverrideSubcomponents(true); //Rocksim does not support this type of override - stage.setOverrideMass(stage2Mass); - } - if (stage2CG > 0.0d) { - stage.setCGOverridden(true); - stage.setOverrideSubcomponents(true); //Rocksim does not support this type of override - stage.setOverrideCGX(stage2CG); - } - component.addChild(stage); - return new StageHandler(document, stage); - } - } - if ("Stage1Parts".equals(element)) { - if (stageCount == 3) { - final Stage stage = new Stage(); - if (stage1Mass > 0.0d) { - stage.setMassOverridden(true); - stage.setOverrideSubcomponents(true); //Rocksim does not support this type of override - stage.setOverrideMass(stage1Mass); - } - if (stage1CG > 0.0d) { - stage.setCGOverridden(true); - stage.setOverrideSubcomponents(true); //Rocksim does not support this type of override - stage.setOverrideCGX(stage1CG); - } - component.addChild(stage); - return new StageHandler(document, stage); - } - } - if (RocksimCommonConstants.NAME.equals(element)) { - return PlainTextHandler.INSTANCE; - } - if ("StageCount".equals(element)) { - return PlainTextHandler.INSTANCE; - } - if ("Stage3Mass".equals(element)) { - return PlainTextHandler.INSTANCE; - } - if ("Stage2Mass".equals(element)) { - return PlainTextHandler.INSTANCE; - } - if ("Stage1Mass".equals(element)) { - return PlainTextHandler.INSTANCE; - } - if ("Stage3CG".equals(element)) { - return PlainTextHandler.INSTANCE; - } - if ("Stage2CGAlone".equals(element)) { - return PlainTextHandler.INSTANCE; - } - if ("Stage1CGAlone".equals(element)) { - return PlainTextHandler.INSTANCE; - } - return null; - } - - @Override - public void closeElement(String element, HashMap attributes, - String content, WarningSet warnings) throws SAXException { - try { - if (RocksimCommonConstants.NAME.equals(element)) { - component.setName(content); - } - if ("StageCount".equals(element)) { - stageCount = Integer.parseInt(content); - } - if ("Stage3Mass".equals(element)) { - stage3Mass = Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_MASS; - } - if ("Stage2Mass".equals(element)) { - stage2Mass = Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_MASS; - } - if ("Stage1Mass".equals(element)) { - stage1Mass = Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_MASS; - } - if ("Stage3CG".equals(element)) { - stage3CG = Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH; - } - if ("Stage2CGAlone".equals(element)) { - stage2CG = Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH; - } - if ("Stage1CGAlone".equals(element)) { - stage1CG = Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH; - } - } - catch (NumberFormatException nfe) { - warnings.add("Could not convert " + element + " value of " + content + ". It is expected to be a number."); - } - } - + /** + * The parent component. + */ + private final RocketComponent component; + /** + * The parsed stage count. Defaults to 1. + */ + private int stageCount = 1; + /** + * The overridden stage 1 mass. + */ + private double stage1Mass = 0d; + /** + * The overridden stage 2 mass. + */ + private double stage2Mass = 0d; + /** + * The overridden stage 3 mass. + */ + private double stage3Mass = 0d; + /** + * The overridden stage 1 Cg. + */ + private double stage1CG = 0d; + /** + * The overridden stage 2 Cg. + */ + private double stage2CG = 0d; + /** + * The overridden stage 3 Cg. + */ + private double stage3CG = 0d; + + /** + * Constructor. + * + * @param c the parent component + */ + public RocketDesignHandler(OpenRocketDocument document, RocketComponent c) { + this.document = document; + component = c; + } + + @Override + public ElementHandler openElement(String element, HashMap attributes, WarningSet warnings) { + /** + * In Rocksim stages are from the top down, so a single stage rocket is actually stage '3'. A 2-stage + * rocket defines stage '2' as the initial booster with stage '3' sitting atop it. And so on. + */ + if ("Stage3Parts".equals(element)) { + final Stage stage = new Stage(); + if (stage3Mass > 0.0d) { + stage.setMassOverridden(true); + stage.setOverrideSubcomponents(true); //Rocksim does not support this type of override + stage.setOverrideMass(stage3Mass); + } + if (stage3CG > 0.0d) { + stage.setCGOverridden(true); + stage.setOverrideSubcomponents(true); //Rocksim does not support this type of override + stage.setOverrideCGX(stage3CG); + } + component.addChild(stage); + return new StageHandler(document, stage); + } + if ("Stage2Parts".equals(element)) { + if (stageCount >= 2) { + final Stage stage = new Stage(); + if (stage2Mass > 0.0d) { + stage.setMassOverridden(true); + stage.setOverrideSubcomponents(true); //Rocksim does not support this type of override + stage.setOverrideMass(stage2Mass); + } + if (stage2CG > 0.0d) { + stage.setCGOverridden(true); + stage.setOverrideSubcomponents(true); //Rocksim does not support this type of override + stage.setOverrideCGX(stage2CG); + } + component.addChild(stage); + return new StageHandler(document, stage); + } + } + if ("Stage1Parts".equals(element)) { + if (stageCount == 3) { + final Stage stage = new Stage(); + if (stage1Mass > 0.0d) { + stage.setMassOverridden(true); + stage.setOverrideSubcomponents(true); //Rocksim does not support this type of override + stage.setOverrideMass(stage1Mass); + } + if (stage1CG > 0.0d) { + stage.setCGOverridden(true); + stage.setOverrideSubcomponents(true); //Rocksim does not support this type of override + stage.setOverrideCGX(stage1CG); + } + component.addChild(stage); + return new StageHandler(document, stage); + } + } + if (RocksimCommonConstants.NAME.equals(element)) { + return PlainTextHandler.INSTANCE; + } + if ("StageCount".equals(element)) { + return PlainTextHandler.INSTANCE; + } + if ("Stage3Mass".equals(element)) { + return PlainTextHandler.INSTANCE; + } + if ("Stage2Mass".equals(element)) { + return PlainTextHandler.INSTANCE; + } + if ("Stage1Mass".equals(element)) { + return PlainTextHandler.INSTANCE; + } + if ("Stage3CG".equals(element)) { + return PlainTextHandler.INSTANCE; + } + if ("Stage2CGAlone".equals(element)) { + return PlainTextHandler.INSTANCE; + } + if ("Stage1CGAlone".equals(element)) { + return PlainTextHandler.INSTANCE; + } + return null; + } + + @Override + public void closeElement(String element, HashMap attributes, + String content, WarningSet warnings) throws SAXException { + try { + if (RocksimCommonConstants.NAME.equals(element)) { + component.setName(content); + } + if ("StageCount".equals(element)) { + stageCount = Integer.parseInt(content); + } + if ("Stage3Mass".equals(element)) { + stage3Mass = Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_MASS; + } + if ("Stage2Mass".equals(element)) { + stage2Mass = Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_MASS; + } + if ("Stage1Mass".equals(element)) { + stage1Mass = Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_MASS; + } + if ("Stage3CG".equals(element)) { + stage3CG = Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH; + } + if ("Stage2CGAlone".equals(element)) { + stage2CG = Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH; + } + if ("Stage1CGAlone".equals(element)) { + stage1CG = Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH; + } + } catch (NumberFormatException nfe) { + warnings.add("Could not convert " + element + " value of " + content + ". It is expected to be a number."); + } + } + } /** @@ -315,36 +319,36 @@ class RocketDesignHandler extends AbstractElementHandler { */ class StageHandler extends AbstractElementHandler { private final OpenRocketDocument document; - /** - * The parent OpenRocket component. - */ - private final RocketComponent component; - - /** - * Constructor. - * - * @param c the parent component - * @throws IllegalArgumentException thrown if c is null - */ - public StageHandler(OpenRocketDocument document, RocketComponent c) throws IllegalArgumentException { - if (c == null) { - throw new IllegalArgumentException("The stage component may not be null."); - } - this.document = document; - component = c; - } - - @Override - public ElementHandler openElement(String element, HashMap attributes, WarningSet warnings) { - if (RocksimCommonConstants.NOSE_CONE.equals(element)) { - return new NoseConeHandler(document, component, warnings); - } - if (RocksimCommonConstants.BODY_TUBE.equals(element)) { - return new BodyTubeHandler(document, component, warnings); - } - if (RocksimCommonConstants.TRANSITION.equals(element)) { - return new TransitionHandler(document, component, warnings); - } - return null; - } + /** + * The parent OpenRocket component. + */ + private final RocketComponent component; + + /** + * Constructor. + * + * @param c the parent component + * @throws IllegalArgumentException thrown if c is null + */ + public StageHandler(OpenRocketDocument document, RocketComponent c) throws IllegalArgumentException { + if (c == null) { + throw new IllegalArgumentException("The stage component may not be null."); + } + this.document = document; + component = c; + } + + @Override + public ElementHandler openElement(String element, HashMap attributes, WarningSet warnings) { + if (RocksimCommonConstants.NOSE_CONE.equals(element)) { + return new NoseConeHandler(document, component, warnings); + } + if (RocksimCommonConstants.BODY_TUBE.equals(element)) { + return new BodyTubeHandler(document, component, warnings); + } + if (RocksimCommonConstants.TRANSITION.equals(element)) { + return new TransitionHandler(document, component, warnings); + } + return null; + } } diff --git a/core/src/net/sf/openrocket/file/rocksim/importt/RocksimLoader.java b/core/src/net/sf/openrocket/file/rocksim/importt/RocksimLoader.java index 41fdaad4c..46f4cbee4 100644 --- a/core/src/net/sf/openrocket/file/rocksim/importt/RocksimLoader.java +++ b/core/src/net/sf/openrocket/file/rocksim/importt/RocksimLoader.java @@ -39,11 +39,11 @@ public class RocksimLoader extends AbstractRocketLoader { * if an error occurs during loading. */ @Override - protected OpenRocketDocument loadFromStream(InputStream source, MotorFinder motorFinder) throws IOException, RocketLoadException { + protected void loadFromStream(OpenRocketDocument doc, InputStream source, MotorFinder motorFinder) throws IOException, RocketLoadException { InputSource xmlSource = new InputSource(source); - RocksimHandler handler = new RocksimHandler(); + RocksimHandler handler = new RocksimHandler(doc); try { SimpleSAX.readXML(xmlSource, handler, warnings); @@ -51,9 +51,7 @@ public class RocksimLoader extends AbstractRocketLoader { throw new RocketLoadException("Malformed XML in input.", e); } - final OpenRocketDocument document = handler.getDocument(); - document.setFile(null); - document.clearUndo(); - return document; + doc.setFile(null); + doc.clearUndo(); } } diff --git a/core/src/net/sf/openrocket/gui/customexpression/CustomExpressionPanel.java b/core/src/net/sf/openrocket/gui/customexpression/CustomExpressionPanel.java index 2ff0242ce..20856d2c9 100644 --- a/core/src/net/sf/openrocket/gui/customexpression/CustomExpressionPanel.java +++ b/core/src/net/sf/openrocket/gui/customexpression/CustomExpressionPanel.java @@ -20,7 +20,6 @@ import javax.swing.filechooser.FileNameExtensionFilter; import net.miginfocom.swing.MigLayout; import net.sf.openrocket.document.OpenRocketDocument; -import net.sf.openrocket.file.DatabaseMotorFinder; import net.sf.openrocket.file.GeneralRocketLoader; import net.sf.openrocket.file.RocketLoadException; import net.sf.openrocket.gui.components.UnitSelector; @@ -98,7 +97,7 @@ public class CustomExpressionPanel extends JPanel { // Load expressions from selected document GeneralRocketLoader loader = new GeneralRocketLoader(importFile); try { - OpenRocketDocument importedDocument = loader.load(importFile, new DatabaseMotorFinder()); + OpenRocketDocument importedDocument = loader.load(); for (CustomExpression exp : importedDocument.getCustomExpressions()) { doc.addCustomExpression(exp); } diff --git a/core/src/net/sf/openrocket/gui/main/BasicFrame.java b/core/src/net/sf/openrocket/gui/main/BasicFrame.java index ecaee7ede..94d0e2d0c 100644 --- a/core/src/net/sf/openrocket/gui/main/BasicFrame.java +++ b/core/src/net/sf/openrocket/gui/main/BasicFrame.java @@ -60,6 +60,7 @@ import javax.swing.tree.TreeSelectionModel; import net.miginfocom.swing.MigLayout; import net.sf.openrocket.aerodynamics.WarningSet; import net.sf.openrocket.document.OpenRocketDocument; +import net.sf.openrocket.document.OpenRocketDocumentFactory; import net.sf.openrocket.document.StorageOptions; import net.sf.openrocket.file.GeneralRocketSaver; import net.sf.openrocket.file.RocketLoadException; @@ -96,7 +97,6 @@ import net.sf.openrocket.rocketcomponent.ComponentChangeEvent; import net.sf.openrocket.rocketcomponent.ComponentChangeListener; import net.sf.openrocket.rocketcomponent.Rocket; import net.sf.openrocket.rocketcomponent.RocketComponent; -import net.sf.openrocket.rocketcomponent.Stage; import net.sf.openrocket.startup.Application; import net.sf.openrocket.util.BugException; import net.sf.openrocket.util.MemoryManagement; @@ -839,7 +839,7 @@ public class BasicFrame extends JFrame { return; } - OpenRocketDocument doc = new OpenRocketDocument(r); + OpenRocketDocument doc = OpenRocketDocumentFactory.createDocumentFromRocket(r); doc.setSaved(true); BasicFrame frame = new BasicFrame(doc); frame.setVisible(true); @@ -855,7 +855,7 @@ public class BasicFrame extends JFrame { public void actionPerformed(ActionEvent e) { log.user("Create Iso-Haisu selected"); Rocket r = TestRockets.makeIsoHaisu(); - OpenRocketDocument doc = new OpenRocketDocument(r); + OpenRocketDocument doc = OpenRocketDocumentFactory.createDocumentFromRocket(r); doc.setSaved(true); BasicFrame frame = new BasicFrame(doc); frame.setVisible(true); @@ -870,7 +870,7 @@ public class BasicFrame extends JFrame { public void actionPerformed(ActionEvent e) { log.user("Create Big Blue selected"); Rocket r = TestRockets.makeBigBlue(); - OpenRocketDocument doc = new OpenRocketDocument(r); + OpenRocketDocument doc = OpenRocketDocumentFactory.createDocumentFromRocket(r); doc.setSaved(true); BasicFrame frame = new BasicFrame(doc); frame.setVisible(true); @@ -1465,19 +1465,11 @@ public class BasicFrame extends JFrame { public static void newAction() { log.info("New action initiated"); - Rocket rocket = new Rocket(); - Stage stage = new Stage(); - //// Sustainer - stage.setName(trans.get("BasicFrame.StageName.Sustainer")); - rocket.addChild(stage); - OpenRocketDocument doc = new OpenRocketDocument(rocket); - doc.setSaved(true); + OpenRocketDocument doc = OpenRocketDocumentFactory.createNewRocket(); BasicFrame frame = new BasicFrame(doc); frame.replaceable = true; frame.setVisible(true); - // kruland commented this out - I don't like it. - //ComponentConfigDialog.showDialog(frame, doc, rocket); } /** diff --git a/core/src/net/sf/openrocket/gui/util/OpenFileWorker.java b/core/src/net/sf/openrocket/gui/util/OpenFileWorker.java index 57026fb08..e67973516 100644 --- a/core/src/net/sf/openrocket/gui/util/OpenFileWorker.java +++ b/core/src/net/sf/openrocket/gui/util/OpenFileWorker.java @@ -12,7 +12,6 @@ import java.net.URL; import javax.swing.SwingWorker; import net.sf.openrocket.document.OpenRocketDocument; -import net.sf.openrocket.file.DatabaseMotorFinder; import net.sf.openrocket.file.GeneralRocketLoader; import net.sf.openrocket.logging.LogHelper; import net.sf.openrocket.startup.Application; @@ -68,7 +67,7 @@ public class OpenFileWorker extends SwingWorker { is = new ProgressInputStream(is); try { - OpenRocketDocument document = loader.load(is, new DatabaseMotorFinder()); + OpenRocketDocument document = loader.load(is); // Set document state document.setFile(file); diff --git a/core/src/net/sf/openrocket/utils/RocksimConverter.java b/core/src/net/sf/openrocket/utils/RocksimConverter.java index 6a3ed6878..ed46cef5b 100644 --- a/core/src/net/sf/openrocket/utils/RocksimConverter.java +++ b/core/src/net/sf/openrocket/utils/RocksimConverter.java @@ -6,7 +6,6 @@ import java.util.Locale; import net.sf.openrocket.document.OpenRocketDocument; import net.sf.openrocket.document.StorageOptions; -import net.sf.openrocket.file.DatabaseMotorFinder; import net.sf.openrocket.file.GeneralRocketLoader; import net.sf.openrocket.file.GeneralRocketSaver; import net.sf.openrocket.file.RocketLoadException; @@ -62,7 +61,7 @@ public class RocksimConverter { opts.setExplicitlySet(true); GeneralRocketLoader loader = new GeneralRocketLoader(input); - OpenRocketDocument document = loader.load(input, new DatabaseMotorFinder()); + OpenRocketDocument document = loader.load(); saver.save(output, document, opts); } catch (RocketLoadException e) { diff --git a/core/test/net/sf/openrocket/IntegrationTest.java b/core/test/net/sf/openrocket/IntegrationTest.java index 3a4ed8fac..599257017 100644 --- a/core/test/net/sf/openrocket/IntegrationTest.java +++ b/core/test/net/sf/openrocket/IntegrationTest.java @@ -20,8 +20,6 @@ import net.sf.openrocket.database.motor.MotorDatabase; import net.sf.openrocket.database.motor.ThrustCurveMotorSetDatabase; import net.sf.openrocket.document.OpenRocketDocument; import net.sf.openrocket.document.Simulation; -import net.sf.openrocket.file.DatabaseMotorFinder; -import net.sf.openrocket.file.FileInfo; import net.sf.openrocket.file.GeneralRocketLoader; import net.sf.openrocket.file.RocketLoadException; import net.sf.openrocket.file.motor.GeneralMotorLoader; @@ -117,10 +115,10 @@ public class IntegrationTest extends BaseTestCase { System.setProperty("openrocket.unittest", "true"); // Load the rocket - GeneralRocketLoader loader = new GeneralRocketLoader(); + GeneralRocketLoader loader = new GeneralRocketLoader(new File("simplerocket.ork")); InputStream is = this.getClass().getResourceAsStream("simplerocket.ork"); assertNotNull("Problem in unit test, cannot find simplerocket.ork", is); - document = loader.load(is, new FileInfo(new File("simplerocket.ork")), new DatabaseMotorFinder()); + document = loader.load(is); is.close(); undoAction = UndoRedoAction.newUndoAction(document); diff --git a/core/test/net/sf/openrocket/file/rocksim/importt/RocksimContentHandlerTest.java b/core/test/net/sf/openrocket/file/rocksim/importt/RocksimContentHandlerTest.java deleted file mode 100644 index 0eb1d6253..000000000 --- a/core/test/net/sf/openrocket/file/rocksim/importt/RocksimContentHandlerTest.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * RocksimContentHandlerTest.java - */ -package net.sf.openrocket.file.rocksim.importt; - -import org.junit.Assert; - -/** - * RocksimContentHandler Tester. - * - */ -public class RocksimContentHandlerTest { - - /** - * - * Method: getDocument() - * - * @throws Exception thrown if something goes awry - */ - @org.junit.Test - public void testGetDocument() throws Exception { - RocksimContentHandler handler = new RocksimContentHandler(); - Assert.assertNotNull(handler.getDocument()); - } - -} diff --git a/core/test/net/sf/openrocket/file/rocksim/importt/RocksimLoaderTest.java b/core/test/net/sf/openrocket/file/rocksim/importt/RocksimLoaderTest.java index 48ea77aff..eb123c0b5 100644 --- a/core/test/net/sf/openrocket/file/rocksim/importt/RocksimLoaderTest.java +++ b/core/test/net/sf/openrocket/file/rocksim/importt/RocksimLoaderTest.java @@ -9,6 +9,7 @@ import java.io.IOException; import java.io.InputStream; import net.sf.openrocket.document.OpenRocketDocument; +import net.sf.openrocket.document.OpenRocketDocumentFactory; import net.sf.openrocket.file.DatabaseMotorFinder; import net.sf.openrocket.file.RocketLoadException; import net.sf.openrocket.rocketcomponent.BodyTube; @@ -34,7 +35,8 @@ public class RocksimLoaderTest { InputStream stream = this.getClass().getResourceAsStream("PodFins.rkt"); Assert.assertNotNull("Could not open PodFins.rkt", stream); try { - OpenRocketDocument doc = loader.loadFromStream(new BufferedInputStream(stream), new DatabaseMotorFinder()); + OpenRocketDocument doc = OpenRocketDocumentFactory.createDocumentForFile(null, false); + loader.loadFromStream(doc, new BufferedInputStream(stream), new DatabaseMotorFinder()); Assert.assertNotNull(doc); Rocket rocket = doc.getRocket(); Assert.assertNotNull(rocket); @@ -64,7 +66,9 @@ public class RocksimLoaderTest { stream = this.getClass().getResourceAsStream("rocksimTestRocket2.rkt"); Assert.assertNotNull("Could not open rocksimTestRocket2.rkt", stream); - doc = loader.loadFromStream(new BufferedInputStream(stream), new DatabaseMotorFinder()); + + doc = OpenRocketDocumentFactory.createDocumentForFile(null, false); + loader.loadFromStream(doc, new BufferedInputStream(stream), new DatabaseMotorFinder()); Assert.assertNotNull(doc); rocket = doc.getRocket(); @@ -87,7 +91,9 @@ public class RocksimLoaderTest { stream = this.getClass().getResourceAsStream("rocksimTestRocket3.rkt"); Assert.assertNotNull("Could not open rocksimTestRocket3.rkt", stream); - doc = loader.loadFromStream(new BufferedInputStream(stream), new DatabaseMotorFinder()); + + doc = OpenRocketDocumentFactory.createDocumentForFile(null, false); + loader.loadFromStream(doc, new BufferedInputStream(stream), new DatabaseMotorFinder()); Assert.assertNotNull(doc); rocket = doc.getRocket(); @@ -133,7 +139,9 @@ public class RocksimLoaderTest { InputStream stream = RocksimLoaderTest.class.getResourceAsStream("rocksimTestRocket1.rkt"); try { Assert.assertNotNull("Could not open rocksimTestRocket1.rkt", stream); - return theLoader.loadFromStream(new BufferedInputStream(stream), new DatabaseMotorFinder()); + OpenRocketDocument doc = OpenRocketDocumentFactory.createDocumentForFile(null, false); + theLoader.loadFromStream(doc, new BufferedInputStream(stream), new DatabaseMotorFinder()); + return doc; } finally { stream.close(); } @@ -143,7 +151,9 @@ public class RocksimLoaderTest { InputStream stream = RocksimLoaderTest.class.getResourceAsStream("rocksimTestRocket3.rkt"); try { Assert.assertNotNull("Could not open rocksimTestRocket3.rkt", stream); - return theLoader.loadFromStream(new BufferedInputStream(stream), new DatabaseMotorFinder()); + OpenRocketDocument doc = OpenRocketDocumentFactory.createDocumentForFile(null, false); + theLoader.loadFromStream(doc, new BufferedInputStream(stream), new DatabaseMotorFinder()); + return doc; } finally { stream.close(); } diff --git a/core/test/net/sf/openrocket/rocketcomponent/ComponentCompare.java b/core/test/net/sf/openrocket/rocketcomponent/ComponentCompare.java index aa3fc486d..f37d71d6c 100644 --- a/core/test/net/sf/openrocket/rocketcomponent/ComponentCompare.java +++ b/core/test/net/sf/openrocket/rocketcomponent/ComponentCompare.java @@ -1,6 +1,9 @@ package net.sf.openrocket.rocketcomponent; -import static org.junit.Assert.*; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import java.lang.reflect.Method; import java.util.Iterator; @@ -15,10 +18,10 @@ public class ComponentCompare { private static final String[] IGNORED_METHODS = { "getClass", "getChildCount", "getChildren", "getNextComponent", "getID", "getPreviousComponent", "getParent", "getRocket", "getRoot", "getStage", - "getStageNumber", "getComponentName", + "getStageNumber", "getComponentName", "getDefaultFlightConfiguration", // Rocket specific methods: "getModID", "getMassModID", "getAerodynamicModID", "getTreeModID", "getFunctionalModID", - "getMotorConfigurationIDs", "getDefaultConfiguration", "getMotorMounts" + "getFlightConfigurationIDs", "getDefaultConfiguration", "getMotorMounts" }; @@ -38,7 +41,7 @@ public class ComponentCompare { } - + public static void assertDeepEquality(RocketComponent c1, RocketComponent c2) { assertEquality(c1, c2); @@ -54,7 +57,7 @@ public class ComponentCompare { } - + public static void assertDeepSimilarity(RocketComponent c1, RocketComponent c2, boolean allowNameDifference) { assertSimilarity(c1, c2, allowNameDifference); @@ -71,7 +74,7 @@ public class ComponentCompare { } - + /** * Check whether the two components are similar. Two components are similar * if each of the getXXX and isXXX methods that both object types have return @@ -114,7 +117,7 @@ public class ComponentCompare { if (allowNameDifference && name.equals("getName")) continue; - + // Check for method in other class Method m2; try { diff --git a/core/test/net/sf/openrocket/simulation/customexpression/TestExpressions.java b/core/test/net/sf/openrocket/simulation/customexpression/TestExpressions.java index fd7769172..9df240497 100644 --- a/core/test/net/sf/openrocket/simulation/customexpression/TestExpressions.java +++ b/core/test/net/sf/openrocket/simulation/customexpression/TestExpressions.java @@ -1,22 +1,22 @@ package net.sf.openrocket.simulation.customexpression; +import net.sf.openrocket.document.OpenRocketDocument; +import net.sf.openrocket.document.OpenRocketDocumentFactory; + import org.junit.Test; -import net.sf.openrocket.document.OpenRocketDocument; -import net.sf.openrocket.rocketcomponent.Rocket; - public class TestExpressions { - + @Test public void testExpressions() { // TODO Auto-generated constructor stub - OpenRocketDocument doc = new OpenRocketDocument(new Rocket()); + OpenRocketDocument doc = OpenRocketDocumentFactory.createNewRocket(); //CustomExpression exp = new CustomExpression(doc, "Kinetic energy", "Ek", "J", ".5*m*Vt^2"); CustomExpression exp = new CustomExpression(doc, "Average mass", "Mavg", "kg", "mean(m[0:t])"); - System.out.println( exp.getExpressionString() ); + System.out.println(exp.getExpressionString()); } }