committed Doug's Rocksim loader
[debian/openrocket] / src / net / sf / openrocket / file / rocksim / InnerBodyTubeHandler.java
1 /*
2  * InnerBodyTubeHandler.java
3  */
4 package net.sf.openrocket.file.rocksim;
5
6 import net.sf.openrocket.aerodynamics.WarningSet;
7 import net.sf.openrocket.file.simplesax.ElementHandler;
8 import net.sf.openrocket.file.simplesax.PlainTextHandler;
9 import net.sf.openrocket.material.Material;
10 import net.sf.openrocket.rocketcomponent.InnerTube;
11 import net.sf.openrocket.rocketcomponent.RocketComponent;
12 import org.xml.sax.SAXException;
13
14 import java.util.HashMap;
15
16 /**
17  * A SAX handler for Rocksim inside tubes.
18  */
19 class InnerBodyTubeHandler extends PositionDependentHandler<InnerTube> {
20
21     /**
22      * The OpenRocket InnerTube instance.
23      */
24     private final InnerTube bodyTube;
25
26     /**
27      * Constructor.
28      *
29      * @param c the parent component
30      * @throws IllegalArgumentException thrown if <code>c</code> is null
31      */
32     public InnerBodyTubeHandler(RocketComponent c) throws IllegalArgumentException {
33         if (c == null) {
34             throw new IllegalArgumentException("The parent component of an inner tube may not be null.");
35         }
36         bodyTube = new InnerTube();
37         c.addChild(bodyTube);
38     }
39
40     @Override
41     public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) {
42         if ("AttachedParts".equals(element)) {
43             return new AttachedPartsHandler(bodyTube);
44         }
45         return PlainTextHandler.INSTANCE;
46     }
47
48     @Override
49     public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
50             throws SAXException {
51         super.closeElement(element, attributes, content, warnings);
52
53         try {
54             if ("OD".equals(element)) {
55                 bodyTube.setOuterRadius(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS);
56             }
57             if ("ID".equals(element)) {
58                 final double r = Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS;
59                 bodyTube.setInnerRadius(r);
60             }
61             if ("Len".equals(element)) {
62                 bodyTube.setLength(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
63             }
64             if ("IsMotorMount".equals(element)) {
65                 bodyTube.setMotorMount("1".equals(content));
66             }
67             if ("EngineOverhang".equals(element)) {
68                 bodyTube.setMotorOverhang(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
69             }
70             if ("Material".equals(element)) {
71                 setMaterialName(content);
72             }
73         }
74         catch (NumberFormatException nfe) {
75             warnings.add("Could not convert " + element + " value of " + content + ".  It is expected to be a number.");
76         }
77     }
78
79     /**
80      * Get the InnerTube component this handler is working upon.
81      * 
82      * @return an InnerTube component
83      */
84     @Override
85     public InnerTube getComponent() {
86         return bodyTube;
87     }
88
89     /**
90      * Set the relative position onto the component.  This cannot be done directly because setRelativePosition is not 
91      * public in all components.
92      * 
93      * @param position  the OpenRocket position
94      */
95     @Override
96     public void setRelativePosition(RocketComponent.Position position) {
97         bodyTube.setRelativePosition(position);
98     }
99
100     /**
101      * Get the required type of material for this component.
102      *
103      * @return BULK
104      */
105     @Override
106     public Material.Type getMaterialType() {
107         return Material.Type.BULK;
108     }
109
110 }