updates for 0.9.3
[debian/openrocket] / src / net / sf / openrocket / file / GeneralMotorLoader.java
1 package net.sf.openrocket.file;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.Reader;
6 import java.nio.charset.Charset;
7 import java.util.List;
8
9 import net.sf.openrocket.rocketcomponent.Motor;
10
11 /**
12  * A motor loader class that detects the file type based on the file name extension.
13  * 
14  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
15  */
16 public class GeneralMotorLoader extends MotorLoader {
17
18         private static final MotorLoader RASP_LOADER = new RASPMotorLoader();
19         private static final MotorLoader ROCKSIM_LOADER = new RockSimMotorLoader();
20         
21         
22         @Override
23         public List<Motor> load(InputStream stream, String filename) throws IOException {
24                 return selectLoader(filename).load(stream, filename);
25         }
26
27         @Override
28         public List<Motor> load(Reader reader, String filename) throws IOException {
29                 return selectLoader(filename).load(reader, filename);
30         }
31         
32
33         @Override
34         protected Charset getDefaultCharset() {
35                 // Not used, may return null
36                 return null;
37         }
38
39         
40         /**
41          * Return the appropriate motor loader based on the file name.
42          * 
43          * @param filename              the file name (may be <code>null</code>).
44          * @return                              the appropriate motor loader to use for the file.
45          * @throws IOException  if the file type cannot be detected from the file name.
46          */
47         public static MotorLoader selectLoader(String filename) throws IOException {
48                 if (filename == null) {
49                         throw new IOException("Unknown file type.");
50                 }
51                 
52                 String ext = "";
53                 int point = filename.lastIndexOf('.');
54                 
55                 if (point > 0)
56                         ext = filename.substring(point+1);
57                 
58                 if (ext.equalsIgnoreCase("eng")) {
59                         return RASP_LOADER;
60                 } else if (ext.equalsIgnoreCase("rse")) {
61                         return ROCKSIM_LOADER;
62                 }
63                 
64                 throw new IOException("Unknown file type.");
65         }
66         
67 }