a58a8553fcadfeab53fb43ca9a25778f3a97b3e4
[debian/openrocket] / core / src / net / sf / openrocket / file / rocksim / importt / RocksimLoader.java
1 /*
2  * RocksimLoader.java
3  */
4 package net.sf.openrocket.file.rocksim.importt;
5
6 import net.sf.openrocket.document.OpenRocketDocument;
7 import net.sf.openrocket.file.RocketLoadException;
8 import net.sf.openrocket.file.RocketLoader;
9 import net.sf.openrocket.file.simplesax.SimpleSAX;
10 import org.xml.sax.InputSource;
11 import org.xml.sax.SAXException;
12
13 import java.io.IOException;
14 import java.io.InputStream;
15
16 /**
17  * This class is the main entry point for Rocksim design file imported to OpenRocket.  Currently only Rocksim v9
18  * file formats are supported, although it is possible that v8 formats will work for most components.
19  * 
20  * In the cases of v9 components that exist in Rocksim but have no corollary in OpenRocket a message is added to
21  * a warning set and presented to the user.  In effect, this loading is a 'best-effort' mapping and is not meant to
22  * be an exact representation of any possible Rocksim design in an OpenRocket format.
23  * 
24  * Rocksim simulations are not imported.
25  * 
26  * Wish List:
27  *       Material interface (or at least make them abstract in RocketComponent)
28  *          setMaterial
29  *          getMaterial
30  */
31 public class RocksimLoader extends RocketLoader {
32         /**
33          * This method is called by the default implementations of {@link #load(java.io.File)}
34          * and {@link #load(java.io.InputStream)} to load the rocket.
35          *
36          * @throws net.sf.openrocket.file.RocketLoadException
37          *          if an error occurs during loading.
38          */
39         @Override
40         protected OpenRocketDocument loadFromStream(InputStream source) throws IOException, RocketLoadException {
41                 
42                 InputSource xmlSource = new InputSource(source);
43                 
44                 RocksimHandler handler = new RocksimHandler();
45                 
46                 try {
47                         SimpleSAX.readXML(xmlSource, handler, warnings);
48                 } catch (SAXException e) {
49                         throw new RocketLoadException("Malformed XML in input.", e);
50                 }
51                 
52                 final OpenRocketDocument document = handler.getDocument();
53                 document.setFile(null);
54                 document.clearUndo();
55                 return document;
56         }
57 }