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