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