From b5de5347ff77cce48f526a4b4de5e4d1ac9e5b85 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Sun, 12 Feb 2023 02:38:39 +0000 Subject: [PATCH] Add filename information to file/rocket loading Useful later for using the file name as the rocket name --- .../openrocket/file/AbstractRocketLoader.java | 6 +-- .../openrocket/file/GeneralRocketLoader.java | 54 +++++++++---------- .../net/sf/openrocket/file/RocketLoader.java | 2 +- .../openrocket/importt/OpenRocketLoader.java | 2 +- .../file/rocksim/importt/RockSimLoader.java | 2 +- .../export/RockSimDocumentDTOTest.java | 4 +- .../rocksim/importt/RockSimLoaderTest.java | 16 +++--- .../openrocket/gui/util/OpenFileWorker.java | 3 +- .../net/sf/openrocket/IntegrationTest.java | 2 +- 9 files changed, 45 insertions(+), 46 deletions(-) diff --git a/core/src/net/sf/openrocket/file/AbstractRocketLoader.java b/core/src/net/sf/openrocket/file/AbstractRocketLoader.java index 0c0a0a222..c5d206ccd 100644 --- a/core/src/net/sf/openrocket/file/AbstractRocketLoader.java +++ b/core/src/net/sf/openrocket/file/AbstractRocketLoader.java @@ -14,11 +14,11 @@ public abstract class AbstractRocketLoader implements RocketLoader { * Loads a rocket from the specified InputStream. */ @Override - public final void load(DocumentLoadingContext context, InputStream source) throws RocketLoadException { + public final void load(DocumentLoadingContext context, InputStream source, String fileName) throws RocketLoadException { warnings.clear(); try { - loadFromStream(context, source); + loadFromStream(context, source, fileName); } catch (RocketLoadException e) { throw e; } catch (IOException e) { @@ -34,7 +34,7 @@ public abstract class AbstractRocketLoader implements RocketLoader { * * @throws RocketLoadException if an error occurs during loading. */ - protected abstract void loadFromStream(DocumentLoadingContext context, InputStream source) throws IOException, RocketLoadException; + protected abstract void loadFromStream(DocumentLoadingContext context, InputStream source, String fileName) throws IOException, RocketLoadException; diff --git a/core/src/net/sf/openrocket/file/GeneralRocketLoader.java b/core/src/net/sf/openrocket/file/GeneralRocketLoader.java index 913edcdfa..23faad889 100644 --- a/core/src/net/sf/openrocket/file/GeneralRocketLoader.java +++ b/core/src/net/sf/openrocket/file/GeneralRocketLoader.java @@ -71,9 +71,9 @@ public class GeneralRocketLoader { InputStream stream = null; try { - + String fileName = baseFile != null && baseFile.getName() != null ? baseFile.getName().replaceFirst("[.][^.]+$", "") : null; stream = new BufferedInputStream(new FileInputStream(baseFile)); - load(stream); + load(stream, fileName); return doc; } catch (Exception e) { @@ -89,9 +89,9 @@ public class GeneralRocketLoader { } } - public final OpenRocketDocument load(InputStream source) throws RocketLoadException { + public final OpenRocketDocument load(InputStream source, String fileName) throws RocketLoadException { try { - loadStep1(source); + loadStep1(source, fileName); doc.getRocket().enableEvents(); return doc; } catch (Exception e) { @@ -116,7 +116,7 @@ public class GeneralRocketLoader { * @throws IOException * @throws RocketLoadException */ - private void loadStep1(InputStream source) throws IOException, RocketLoadException { + private void loadStep1(InputStream source, String fileName) throws IOException, RocketLoadException { // Check for mark() support if (!source.markSupported()) { @@ -141,7 +141,7 @@ public class GeneralRocketLoader { if (buffer[0] == GZIP_SIGNATURE[0] && buffer[1] == GZIP_SIGNATURE[1]) { isContainer = false; setAttachmentFactory(); - loadRocket(new GZIPInputStream(source)); + loadRocket(new GZIPInputStream(source), fileName); return; } @@ -151,30 +151,27 @@ public class GeneralRocketLoader { setAttachmentFactory(); // Search for entry with name *.ork ZipInputStream in = new ZipInputStream(source); - while (true) { - ZipEntry entry = in.getNextEntry(); - if (entry == null) { - throw new RocketLoadException("Unsupported or corrupt file."); - } - if (entry.getName().matches(".*\\.[oO][rR][kK]$")) { - loadRocket(in); - } else if (entry.getName().matches(".*\\.[rR][kK][tT]$")) { - loadRocket(in); - } - in.close(); - return; + ZipEntry entry = in.getNextEntry(); + if (entry == null) { + throw new RocketLoadException("Unsupported or corrupt file."); } - + if (entry.getName().matches(".*\\.[oO][rR][kK]$")) { + loadRocket(in, fileName); + } else if (entry.getName().matches(".*\\.[rR][kK][tT]$")) { + loadRocket(in, fileName); + } else if (entry.getName().matches(".*\\.[cC][dD][xX]1$")) { + loadRocket(in, fileName); + } + in.close(); + return; } isContainer = false; setAttachmentFactory(); - loadRocket(source); - return; - + loadRocket(source, fileName); } - private void loadRocket(InputStream source) throws IOException, RocketLoadException { + private void loadRocket(InputStream source, String fileName) throws IOException, RocketLoadException { // Check for mark() support if (!source.markSupported()) { @@ -198,17 +195,18 @@ public class GeneralRocketLoader { if (buffer[i] == OPENROCKET_SIGNATURE[match]) { match++; if (match == OPENROCKET_SIGNATURE.length) { - loadUsing(openRocketLoader, source); + loadUsing(openRocketLoader, source, fileName); return; } } else { match = 0; } } - + + // Check for RockSim byte[] typeIdentifier = ArrayUtils.copyOf(buffer, ROCKSIM_SIGNATURE.length); if (Arrays.equals(ROCKSIM_SIGNATURE, typeIdentifier)) { - loadUsing(rocksimLoader, source); + loadUsing(rocksimLoader, source, fileName); return; } throw new RocketLoadException("Unsupported or corrupt file."); @@ -231,13 +229,13 @@ public class GeneralRocketLoader { } } - private void loadUsing(RocketLoader loader, InputStream source) throws RocketLoadException { + private void loadUsing(RocketLoader loader, InputStream source, String fileName) throws RocketLoadException { warnings.clear(); DocumentLoadingContext context = new DocumentLoadingContext(); context.setOpenRocketDocument(doc); context.setMotorFinder(motorFinder); context.setAttachmentFactory(attachmentFactory); - loader.load(context, source); + loader.load(context, source, fileName); warnings.addAll(loader.getWarnings()); } } diff --git a/core/src/net/sf/openrocket/file/RocketLoader.java b/core/src/net/sf/openrocket/file/RocketLoader.java index 37e73dc51..1bd5c2460 100644 --- a/core/src/net/sf/openrocket/file/RocketLoader.java +++ b/core/src/net/sf/openrocket/file/RocketLoader.java @@ -6,7 +6,7 @@ import net.sf.openrocket.aerodynamics.WarningSet; public interface RocketLoader { - public void load(DocumentLoadingContext context, InputStream source) throws RocketLoadException; + public void load(DocumentLoadingContext context, InputStream source, String fileName) throws RocketLoadException; public WarningSet getWarnings(); 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 e0bf4717b..5138d111c 100644 --- a/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java +++ b/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java @@ -39,7 +39,7 @@ public class OpenRocketLoader extends AbstractRocketLoader { @Override - public void loadFromStream(DocumentLoadingContext context, InputStream source) throws RocketLoadException, + public void loadFromStream(DocumentLoadingContext context, InputStream source, String fileName) throws RocketLoadException, IOException { log.info("Loading .ork file"); 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 d422f784f..1ae2e9808 100644 --- a/core/src/net/sf/openrocket/file/rocksim/importt/RockSimLoader.java +++ b/core/src/net/sf/openrocket/file/rocksim/importt/RockSimLoader.java @@ -39,7 +39,7 @@ public class RockSimLoader extends AbstractRocketLoader { * if an error occurs during loading. */ @Override - public void loadFromStream(DocumentLoadingContext context, InputStream source) throws IOException, RocketLoadException { + public void loadFromStream(DocumentLoadingContext context, InputStream source, String fileName) throws IOException, RocketLoadException { InputSource xmlSource = new InputSource(source); diff --git a/core/test/net/sf/openrocket/file/rocksim/export/RockSimDocumentDTOTest.java b/core/test/net/sf/openrocket/file/rocksim/export/RockSimDocumentDTOTest.java index 242a62c7f..f0b8ad12e 100644 --- a/core/test/net/sf/openrocket/file/rocksim/export/RockSimDocumentDTOTest.java +++ b/core/test/net/sf/openrocket/file/rocksim/export/RockSimDocumentDTOTest.java @@ -120,7 +120,7 @@ public class RockSimDocumentDTOTest extends RockSimTestBase { DocumentLoadingContext context = new DocumentLoadingContext(); context.setOpenRocketDocument(importedDocument); context.setMotorFinder(new DatabaseMotorFinder()); - loader.loadFromStream(context, new BufferedInputStream(stream)); + loader.loadFromStream(context, new BufferedInputStream(stream), null); Rocket importedRocket = importedDocument.getRocket(); // Test children counts @@ -187,7 +187,7 @@ public class RockSimDocumentDTOTest extends RockSimTestBase { DocumentLoadingContext context = new DocumentLoadingContext(); context.setOpenRocketDocument(importedDocument); context.setMotorFinder(new DatabaseMotorFinder()); - loader.loadFromStream(context, new BufferedInputStream(stream)); + loader.loadFromStream(context, new BufferedInputStream(stream), null); Rocket importedRocket = importedDocument.getRocket(); // Test children counts 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 1eac69a8a..baa8dd212 100644 --- a/core/test/net/sf/openrocket/file/rocksim/importt/RockSimLoaderTest.java +++ b/core/test/net/sf/openrocket/file/rocksim/importt/RockSimLoaderTest.java @@ -54,7 +54,7 @@ public class RockSimLoaderTest extends BaseTestCase { DocumentLoadingContext context = new DocumentLoadingContext(); context.setOpenRocketDocument(doc); context.setMotorFinder(new DatabaseMotorFinder()); - loader.loadFromStream(context, new BufferedInputStream(stream)); + loader.loadFromStream(context, new BufferedInputStream(stream), null); Rocket rocket = doc.getRocket(); Assert.assertNotNull(rocket); } @@ -82,7 +82,7 @@ public class RockSimLoaderTest extends BaseTestCase { DocumentLoadingContext context = new DocumentLoadingContext(); context.setOpenRocketDocument(doc); context.setMotorFinder(new DatabaseMotorFinder()); - loader.loadFromStream(context, new BufferedInputStream(stream)); + loader.loadFromStream(context, new BufferedInputStream(stream), null); Assert.assertNotNull(doc); rocket = doc.getRocket(); @@ -171,7 +171,7 @@ public class RockSimLoaderTest extends BaseTestCase { DocumentLoadingContext context = new DocumentLoadingContext(); context.setOpenRocketDocument(doc); context.setMotorFinder(new DatabaseMotorFinder()); - loader.loadFromStream(context, new BufferedInputStream(stream)); + loader.loadFromStream(context, new BufferedInputStream(stream), null); Assert.assertNotNull(doc); rocket = doc.getRocket(); @@ -199,7 +199,7 @@ public class RockSimLoaderTest extends BaseTestCase { context = new DocumentLoadingContext(); context.setOpenRocketDocument(doc); context.setMotorFinder(new DatabaseMotorFinder()); - loader.loadFromStream(context, new BufferedInputStream(stream)); + loader.loadFromStream(context, new BufferedInputStream(stream), null); Assert.assertNotNull(doc); rocket = doc.getRocket(); @@ -295,7 +295,7 @@ public class RockSimLoaderTest extends BaseTestCase { DocumentLoadingContext context = new DocumentLoadingContext(); context.setOpenRocketDocument(doc); context.setMotorFinder(new DatabaseMotorFinder()); - loader.loadFromStream(context, new BufferedInputStream(stream)); + loader.loadFromStream(context, new BufferedInputStream(stream), null); Assert.assertNotNull(doc); rocket = doc.getRocket(); @@ -336,7 +336,7 @@ public class RockSimLoaderTest extends BaseTestCase { DocumentLoadingContext context = new DocumentLoadingContext(); context.setOpenRocketDocument(doc); context.setMotorFinder(new DatabaseMotorFinder()); - loader.loadFromStream(context, new BufferedInputStream(stream)); + loader.loadFromStream(context, new BufferedInputStream(stream), null); Assert.assertNotNull(doc); rocket = doc.getRocket(); @@ -401,7 +401,7 @@ public class RockSimLoaderTest extends BaseTestCase { DocumentLoadingContext context = new DocumentLoadingContext(); context.setOpenRocketDocument(doc); context.setMotorFinder(new DatabaseMotorFinder()); - loader.loadFromStream(context, new BufferedInputStream(stream)); + loader.loadFromStream(context, new BufferedInputStream(stream), null); Assert.assertNotNull(doc); rocket = doc.getRocket(); @@ -481,7 +481,7 @@ public class RockSimLoaderTest extends BaseTestCase { DocumentLoadingContext context = new DocumentLoadingContext(); context.setOpenRocketDocument(doc); context.setMotorFinder(new DatabaseMotorFinder()); - theLoader.loadFromStream(context, new BufferedInputStream(stream)); + theLoader.loadFromStream(context, new BufferedInputStream(stream), null); return doc; } } diff --git a/swing/src/net/sf/openrocket/gui/util/OpenFileWorker.java b/swing/src/net/sf/openrocket/gui/util/OpenFileWorker.java index c1f6ecb92..58267bd66 100644 --- a/swing/src/net/sf/openrocket/gui/util/OpenFileWorker.java +++ b/swing/src/net/sf/openrocket/gui/util/OpenFileWorker.java @@ -69,7 +69,8 @@ public class OpenFileWorker extends SwingWorker { is = new ProgressInputStream(is); try { - OpenRocketDocument document = loader.load(is); + String fileName = file != null && file.getName() != null ? file.getName().replaceFirst("[.][^.]+$", "") : null; + OpenRocketDocument document = loader.load(is, fileName); // Set document state document.setFile(file); diff --git a/swing/test/net/sf/openrocket/IntegrationTest.java b/swing/test/net/sf/openrocket/IntegrationTest.java index a1fd04a58..c558ded70 100644 --- a/swing/test/net/sf/openrocket/IntegrationTest.java +++ b/swing/test/net/sf/openrocket/IntegrationTest.java @@ -358,7 +358,7 @@ public class IntegrationTest { OpenRocketDocument rocketDoc = null; try { - rocketDoc = loader.load(is); + rocketDoc = loader.load(is, fileName); } catch (RocketLoadException e) { fail("RocketLoadException while loading file " + fileName + " : " + e.getMessage()); }