1 package net.sf.openrocket.file.motor;
3 import java.io.IOException;
4 import java.io.InputStream;
7 import net.sf.openrocket.file.UnknownFileTypeException;
8 import net.sf.openrocket.motor.Motor;
11 * A motor loader class that detects the file type based on the file name extension.
13 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
15 public class GeneralMotorLoader implements MotorLoader {
17 private final MotorLoader RASP_LOADER = new RASPMotorLoader();
18 private final MotorLoader ROCKSIM_LOADER = new RockSimMotorLoader();
19 private final MotorLoader ZIP_LOADER;
22 public GeneralMotorLoader() {
23 // Must use this loader in order to avoid recursive instantiation
24 ZIP_LOADER = new ZipFileMotorLoader(this);
32 * @throws UnknownFileTypeException if the file format is not supported
35 public List<Motor> load(InputStream stream, String filename) throws IOException {
36 return selectLoader(filename).load(stream, filename);
42 * Return an array containing the supported file extensions.
44 * @return an array of the supported file extensions.
46 public String[] getSupportedExtensions() {
47 return new String[] { "rse", "eng", "zip" };
52 * Return the appropriate motor loader based on the file name.
54 * @param filename the file name (may be <code>null</code>).
55 * @return the appropriate motor loader to use for the file.
56 * @throws UnknownFileTypeException if the file type cannot be detected from the file name.
58 private MotorLoader selectLoader(String filename) throws IOException {
59 if (filename == null) {
60 throw new UnknownFileTypeException("Unknown file type, filename=null");
64 int point = filename.lastIndexOf('.');
67 ext = filename.substring(point + 1);
69 if (ext.equalsIgnoreCase("eng")) {
71 } else if (ext.equalsIgnoreCase("rse")) {
72 return ROCKSIM_LOADER;
73 } else if (ext.equalsIgnoreCase("zip")) {
77 throw new UnknownFileTypeException("Unknown file type, filename=" + filename);