Add filename information to file/rocket loading

Useful later for using the file name as the rocket name
This commit is contained in:
SiboVG 2023-02-12 02:38:39 +00:00
parent dad762c31e
commit b5de5347ff
9 changed files with 45 additions and 46 deletions

View File

@ -14,11 +14,11 @@ public abstract class AbstractRocketLoader implements RocketLoader {
* Loads a rocket from the specified InputStream. * Loads a rocket from the specified InputStream.
*/ */
@Override @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(); warnings.clear();
try { try {
loadFromStream(context, source); loadFromStream(context, source, fileName);
} catch (RocketLoadException e) { } catch (RocketLoadException e) {
throw e; throw e;
} catch (IOException e) { } catch (IOException e) {
@ -34,7 +34,7 @@ public abstract class AbstractRocketLoader implements RocketLoader {
* *
* @throws RocketLoadException if an error occurs during loading. * @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;

View File

@ -71,9 +71,9 @@ public class GeneralRocketLoader {
InputStream stream = null; InputStream stream = null;
try { try {
String fileName = baseFile != null && baseFile.getName() != null ? baseFile.getName().replaceFirst("[.][^.]+$", "") : null;
stream = new BufferedInputStream(new FileInputStream(baseFile)); stream = new BufferedInputStream(new FileInputStream(baseFile));
load(stream); load(stream, fileName);
return doc; return doc;
} catch (Exception e) { } 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 { try {
loadStep1(source); loadStep1(source, fileName);
doc.getRocket().enableEvents(); doc.getRocket().enableEvents();
return doc; return doc;
} catch (Exception e) { } catch (Exception e) {
@ -116,7 +116,7 @@ public class GeneralRocketLoader {
* @throws IOException * @throws IOException
* @throws RocketLoadException * @throws RocketLoadException
*/ */
private void loadStep1(InputStream source) throws IOException, RocketLoadException { private void loadStep1(InputStream source, String fileName) throws IOException, RocketLoadException {
// Check for mark() support // Check for mark() support
if (!source.markSupported()) { if (!source.markSupported()) {
@ -141,7 +141,7 @@ public class GeneralRocketLoader {
if (buffer[0] == GZIP_SIGNATURE[0] && buffer[1] == GZIP_SIGNATURE[1]) { if (buffer[0] == GZIP_SIGNATURE[0] && buffer[1] == GZIP_SIGNATURE[1]) {
isContainer = false; isContainer = false;
setAttachmentFactory(); setAttachmentFactory();
loadRocket(new GZIPInputStream(source)); loadRocket(new GZIPInputStream(source), fileName);
return; return;
} }
@ -151,30 +151,27 @@ public class GeneralRocketLoader {
setAttachmentFactory(); setAttachmentFactory();
// Search for entry with name *.ork // Search for entry with name *.ork
ZipInputStream in = new ZipInputStream(source); ZipInputStream in = new ZipInputStream(source);
while (true) { ZipEntry entry = in.getNextEntry();
ZipEntry entry = in.getNextEntry(); if (entry == null) {
if (entry == null) { throw new RocketLoadException("Unsupported or corrupt file.");
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;
} }
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; isContainer = false;
setAttachmentFactory(); setAttachmentFactory();
loadRocket(source); loadRocket(source, fileName);
return;
} }
private void loadRocket(InputStream source) throws IOException, RocketLoadException { private void loadRocket(InputStream source, String fileName) throws IOException, RocketLoadException {
// Check for mark() support // Check for mark() support
if (!source.markSupported()) { if (!source.markSupported()) {
@ -198,17 +195,18 @@ public class GeneralRocketLoader {
if (buffer[i] == OPENROCKET_SIGNATURE[match]) { if (buffer[i] == OPENROCKET_SIGNATURE[match]) {
match++; match++;
if (match == OPENROCKET_SIGNATURE.length) { if (match == OPENROCKET_SIGNATURE.length) {
loadUsing(openRocketLoader, source); loadUsing(openRocketLoader, source, fileName);
return; return;
} }
} else { } else {
match = 0; match = 0;
} }
} }
// Check for RockSim
byte[] typeIdentifier = ArrayUtils.copyOf(buffer, ROCKSIM_SIGNATURE.length); byte[] typeIdentifier = ArrayUtils.copyOf(buffer, ROCKSIM_SIGNATURE.length);
if (Arrays.equals(ROCKSIM_SIGNATURE, typeIdentifier)) { if (Arrays.equals(ROCKSIM_SIGNATURE, typeIdentifier)) {
loadUsing(rocksimLoader, source); loadUsing(rocksimLoader, source, fileName);
return; return;
} }
throw new RocketLoadException("Unsupported or corrupt file."); 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(); warnings.clear();
DocumentLoadingContext context = new DocumentLoadingContext(); DocumentLoadingContext context = new DocumentLoadingContext();
context.setOpenRocketDocument(doc); context.setOpenRocketDocument(doc);
context.setMotorFinder(motorFinder); context.setMotorFinder(motorFinder);
context.setAttachmentFactory(attachmentFactory); context.setAttachmentFactory(attachmentFactory);
loader.load(context, source); loader.load(context, source, fileName);
warnings.addAll(loader.getWarnings()); warnings.addAll(loader.getWarnings());
} }
} }

View File

@ -6,7 +6,7 @@ import net.sf.openrocket.aerodynamics.WarningSet;
public interface RocketLoader { 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(); public WarningSet getWarnings();

View File

@ -39,7 +39,7 @@ public class OpenRocketLoader extends AbstractRocketLoader {
@Override @Override
public void loadFromStream(DocumentLoadingContext context, InputStream source) throws RocketLoadException, public void loadFromStream(DocumentLoadingContext context, InputStream source, String fileName) throws RocketLoadException,
IOException { IOException {
log.info("Loading .ork file"); log.info("Loading .ork file");

View File

@ -39,7 +39,7 @@ public class RockSimLoader extends AbstractRocketLoader {
* if an error occurs during loading. * if an error occurs during loading.
*/ */
@Override @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); InputSource xmlSource = new InputSource(source);

View File

@ -120,7 +120,7 @@ public class RockSimDocumentDTOTest extends RockSimTestBase {
DocumentLoadingContext context = new DocumentLoadingContext(); DocumentLoadingContext context = new DocumentLoadingContext();
context.setOpenRocketDocument(importedDocument); context.setOpenRocketDocument(importedDocument);
context.setMotorFinder(new DatabaseMotorFinder()); context.setMotorFinder(new DatabaseMotorFinder());
loader.loadFromStream(context, new BufferedInputStream(stream)); loader.loadFromStream(context, new BufferedInputStream(stream), null);
Rocket importedRocket = importedDocument.getRocket(); Rocket importedRocket = importedDocument.getRocket();
// Test children counts // Test children counts
@ -187,7 +187,7 @@ public class RockSimDocumentDTOTest extends RockSimTestBase {
DocumentLoadingContext context = new DocumentLoadingContext(); DocumentLoadingContext context = new DocumentLoadingContext();
context.setOpenRocketDocument(importedDocument); context.setOpenRocketDocument(importedDocument);
context.setMotorFinder(new DatabaseMotorFinder()); context.setMotorFinder(new DatabaseMotorFinder());
loader.loadFromStream(context, new BufferedInputStream(stream)); loader.loadFromStream(context, new BufferedInputStream(stream), null);
Rocket importedRocket = importedDocument.getRocket(); Rocket importedRocket = importedDocument.getRocket();
// Test children counts // Test children counts

View File

@ -54,7 +54,7 @@ public class RockSimLoaderTest extends BaseTestCase {
DocumentLoadingContext context = new DocumentLoadingContext(); DocumentLoadingContext context = new DocumentLoadingContext();
context.setOpenRocketDocument(doc); context.setOpenRocketDocument(doc);
context.setMotorFinder(new DatabaseMotorFinder()); context.setMotorFinder(new DatabaseMotorFinder());
loader.loadFromStream(context, new BufferedInputStream(stream)); loader.loadFromStream(context, new BufferedInputStream(stream), null);
Rocket rocket = doc.getRocket(); Rocket rocket = doc.getRocket();
Assert.assertNotNull(rocket); Assert.assertNotNull(rocket);
} }
@ -82,7 +82,7 @@ public class RockSimLoaderTest extends BaseTestCase {
DocumentLoadingContext context = new DocumentLoadingContext(); DocumentLoadingContext context = new DocumentLoadingContext();
context.setOpenRocketDocument(doc); context.setOpenRocketDocument(doc);
context.setMotorFinder(new DatabaseMotorFinder()); context.setMotorFinder(new DatabaseMotorFinder());
loader.loadFromStream(context, new BufferedInputStream(stream)); loader.loadFromStream(context, new BufferedInputStream(stream), null);
Assert.assertNotNull(doc); Assert.assertNotNull(doc);
rocket = doc.getRocket(); rocket = doc.getRocket();
@ -171,7 +171,7 @@ public class RockSimLoaderTest extends BaseTestCase {
DocumentLoadingContext context = new DocumentLoadingContext(); DocumentLoadingContext context = new DocumentLoadingContext();
context.setOpenRocketDocument(doc); context.setOpenRocketDocument(doc);
context.setMotorFinder(new DatabaseMotorFinder()); context.setMotorFinder(new DatabaseMotorFinder());
loader.loadFromStream(context, new BufferedInputStream(stream)); loader.loadFromStream(context, new BufferedInputStream(stream), null);
Assert.assertNotNull(doc); Assert.assertNotNull(doc);
rocket = doc.getRocket(); rocket = doc.getRocket();
@ -199,7 +199,7 @@ public class RockSimLoaderTest extends BaseTestCase {
context = new DocumentLoadingContext(); context = new DocumentLoadingContext();
context.setOpenRocketDocument(doc); context.setOpenRocketDocument(doc);
context.setMotorFinder(new DatabaseMotorFinder()); context.setMotorFinder(new DatabaseMotorFinder());
loader.loadFromStream(context, new BufferedInputStream(stream)); loader.loadFromStream(context, new BufferedInputStream(stream), null);
Assert.assertNotNull(doc); Assert.assertNotNull(doc);
rocket = doc.getRocket(); rocket = doc.getRocket();
@ -295,7 +295,7 @@ public class RockSimLoaderTest extends BaseTestCase {
DocumentLoadingContext context = new DocumentLoadingContext(); DocumentLoadingContext context = new DocumentLoadingContext();
context.setOpenRocketDocument(doc); context.setOpenRocketDocument(doc);
context.setMotorFinder(new DatabaseMotorFinder()); context.setMotorFinder(new DatabaseMotorFinder());
loader.loadFromStream(context, new BufferedInputStream(stream)); loader.loadFromStream(context, new BufferedInputStream(stream), null);
Assert.assertNotNull(doc); Assert.assertNotNull(doc);
rocket = doc.getRocket(); rocket = doc.getRocket();
@ -336,7 +336,7 @@ public class RockSimLoaderTest extends BaseTestCase {
DocumentLoadingContext context = new DocumentLoadingContext(); DocumentLoadingContext context = new DocumentLoadingContext();
context.setOpenRocketDocument(doc); context.setOpenRocketDocument(doc);
context.setMotorFinder(new DatabaseMotorFinder()); context.setMotorFinder(new DatabaseMotorFinder());
loader.loadFromStream(context, new BufferedInputStream(stream)); loader.loadFromStream(context, new BufferedInputStream(stream), null);
Assert.assertNotNull(doc); Assert.assertNotNull(doc);
rocket = doc.getRocket(); rocket = doc.getRocket();
@ -401,7 +401,7 @@ public class RockSimLoaderTest extends BaseTestCase {
DocumentLoadingContext context = new DocumentLoadingContext(); DocumentLoadingContext context = new DocumentLoadingContext();
context.setOpenRocketDocument(doc); context.setOpenRocketDocument(doc);
context.setMotorFinder(new DatabaseMotorFinder()); context.setMotorFinder(new DatabaseMotorFinder());
loader.loadFromStream(context, new BufferedInputStream(stream)); loader.loadFromStream(context, new BufferedInputStream(stream), null);
Assert.assertNotNull(doc); Assert.assertNotNull(doc);
rocket = doc.getRocket(); rocket = doc.getRocket();
@ -481,7 +481,7 @@ public class RockSimLoaderTest extends BaseTestCase {
DocumentLoadingContext context = new DocumentLoadingContext(); DocumentLoadingContext context = new DocumentLoadingContext();
context.setOpenRocketDocument(doc); context.setOpenRocketDocument(doc);
context.setMotorFinder(new DatabaseMotorFinder()); context.setMotorFinder(new DatabaseMotorFinder());
theLoader.loadFromStream(context, new BufferedInputStream(stream)); theLoader.loadFromStream(context, new BufferedInputStream(stream), null);
return doc; return doc;
} }
} }

View File

@ -69,7 +69,8 @@ public class OpenFileWorker extends SwingWorker<OpenRocketDocument, Void> {
is = new ProgressInputStream(is); is = new ProgressInputStream(is);
try { 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 // Set document state
document.setFile(file); document.setFile(file);

View File

@ -358,7 +358,7 @@ public class IntegrationTest {
OpenRocketDocument rocketDoc = null; OpenRocketDocument rocketDoc = null;
try { try {
rocketDoc = loader.load(is); rocketDoc = loader.load(is, fileName);
} catch (RocketLoadException e) { } catch (RocketLoadException e) {
fail("RocketLoadException while loading file " + fileName + " : " + e.getMessage()); fail("RocketLoadException while loading file " + fileName + " : " + e.getMessage());
} }