create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / file / motor / GeneralMotorLoader.java
1 package net.sf.openrocket.file.motor;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.List;
6
7 import net.sf.openrocket.file.UnknownFileTypeException;
8 import net.sf.openrocket.motor.Motor;
9
10 /**
11  * A motor loader class that detects the file type based on the file name extension.
12  * 
13  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
14  */
15 public class GeneralMotorLoader implements MotorLoader {
16         
17         private final MotorLoader RASP_LOADER = new RASPMotorLoader();
18         private final MotorLoader ROCKSIM_LOADER = new RockSimMotorLoader();
19         private final MotorLoader ZIP_LOADER;
20         
21         
22         public GeneralMotorLoader() {
23                 // Must use this loader in order to avoid recursive instantiation
24                 ZIP_LOADER = new ZipFileMotorLoader(this);
25         }
26         
27         
28
29         /**
30          * {@inheritDoc}
31          * 
32          * @throws UnknownFileTypeException             if the file format is not supported
33          */
34         @Override
35         public List<Motor> load(InputStream stream, String filename) throws IOException {
36                 return selectLoader(filename).load(stream, filename);
37         }
38         
39         
40
41         /**
42          * Return an array containing the supported file extensions.
43          * 
44          * @return      an array of the supported file extensions.
45          */
46         public String[] getSupportedExtensions() {
47                 return new String[] { "rse", "eng", "zip" };
48         }
49         
50         
51         /**
52          * Return the appropriate motor loader based on the file name.
53          * 
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.
57          */
58         private MotorLoader selectLoader(String filename) throws IOException {
59                 if (filename == null) {
60                         throw new UnknownFileTypeException("Unknown file type, filename=null");
61                 }
62                 
63                 String ext = "";
64                 int point = filename.lastIndexOf('.');
65                 
66                 if (point > 0)
67                         ext = filename.substring(point + 1);
68                 
69                 if (ext.equalsIgnoreCase("eng")) {
70                         return RASP_LOADER;
71                 } else if (ext.equalsIgnoreCase("rse")) {
72                         return ROCKSIM_LOADER;
73                 } else if (ext.equalsIgnoreCase("zip")) {
74                         return ZIP_LOADER;
75                 }
76                 
77                 throw new UnknownFileTypeException("Unknown file type, filename=" + filename);
78         }
79         
80 }