Add filename information to file/rocket loading
Useful later for using the file name as the rocket name
This commit is contained in:
parent
dad762c31e
commit
b5de5347ff
@ -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;
|
||||
|
||||
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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");
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -69,7 +69,8 @@ public class OpenFileWorker extends SwingWorker<OpenRocketDocument, Void> {
|
||||
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);
|
||||
|
@ -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());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user