DGP - added isCompatible checks for all Rocksim components
[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      * @param warnings  the warning set
31      * @throws IllegalArgumentException thrown if <code>c</code> is null
32      */
33     public InnerBodyTubeHandler(RocketComponent c, WarningSet warnings) throws IllegalArgumentException {
34         if (c == null) {
35             throw new IllegalArgumentException("The parent component of an inner tube may not be null.");
36         }
37         bodyTube = new InnerTube();
38         if (isCompatible(c, InnerTube.class, warnings)) {
39             c.addChild(bodyTube);
40         }
41     }
42
43     @Override
44     public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) {
45         if ("AttachedParts".equals(element)) {
46             return new AttachedPartsHandler(bodyTube);
47         }
48         return PlainTextHandler.INSTANCE;
49     }
50
51     @Override
52     public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
53             throws SAXException {
54         super.closeElement(element, attributes, content, warnings);
55
56         try {
57             if ("OD".equals(element)) {
58                 bodyTube.setOuterRadius(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS);
59             }
60             if ("ID".equals(element)) {
61                 final double r = Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS;
62                 bodyTube.setInnerRadius(r);
63             }
64             if ("Len".equals(element)) {
65                 bodyTube.setLength(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
66             }
67             if ("IsMotorMount".equals(element)) {
68                 bodyTube.setMotorMount("1".equals(content));
69             }
70             if ("EngineOverhang".equals(element)) {
71                 bodyTube.setMotorOverhang(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
72             }
73             if ("Material".equals(element)) {
74                 setMaterialName(content);
75             }
76         }
77         catch (NumberFormatException nfe) {
78             warnings.add("Could not convert " + element + " value of " + content + ".  It is expected to be a number.");
79         }
80     }
81
82     /**
83      * Get the InnerTube component this handler is working upon.
84      * 
85      * @return an InnerTube component
86      */
87     @Override
88     public InnerTube getComponent() {
89         return bodyTube;
90     }
91
92     /**
93      * Set the relative position onto the component.  This cannot be done directly because setRelativePosition is not 
94      * public in all components.
95      * 
96      * @param position  the OpenRocket position
97      */
98     @Override
99     public void setRelativePosition(RocketComponent.Position position) {
100         bodyTube.setRelativePosition(position);
101     }
102
103     /**
104      * Get the required type of material for this component.
105      *
106      * @return BULK
107      */
108     @Override
109     public Material.Type getMaterialType() {
110         return Material.Type.BULK;
111     }
112
113 }