create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / file / rocksim / importt / BodyTubeHandler.java
1 /*
2  * BodyTubeHandler.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.rocksim.RocksimFinishCode;
9 import net.sf.openrocket.file.simplesax.ElementHandler;
10 import net.sf.openrocket.file.simplesax.PlainTextHandler;
11 import net.sf.openrocket.material.Material;
12 import net.sf.openrocket.rocketcomponent.BodyTube;
13 import net.sf.openrocket.rocketcomponent.RocketComponent;
14 import org.xml.sax.SAXException;
15
16 import java.util.HashMap;
17
18 /**
19  * A SAX handler for Rocksim Body Tubes.
20  */
21 class BodyTubeHandler extends BaseHandler<BodyTube> {
22     /**
23      * The OpenRocket BodyTube.
24      */
25     private final BodyTube bodyTube;
26
27     /**
28      * Constructor.
29      *
30      * @param c parent component
31      * @param warnings  the warning set
32      * @throws IllegalArgumentException thrown if <code>c</code> is null
33      */
34     public BodyTubeHandler(RocketComponent c, WarningSet warnings) throws IllegalArgumentException {
35         if (c == null) {
36             throw new IllegalArgumentException("The parent component of a body tube may not be null.");
37         }
38         bodyTube = new BodyTube();
39         if (isCompatible(c, BodyTube.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.FINISH_CODE.equals(element)) {
69                 bodyTube.setFinish(RocksimFinishCode.fromCode(Integer.parseInt(content)).asOpenRocket());
70             }
71             if (RocksimCommonConstants.IS_MOTOR_MOUNT.equals(element)) {
72                 bodyTube.setMotorMount("1".equals(content));
73             }
74             if (RocksimCommonConstants.ENGINE_OVERHANG.equals(element)) {
75                 bodyTube.setMotorOverhang(Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
76             }
77             if (RocksimCommonConstants.MATERIAL.equals(element)) {
78                 setMaterialName(content);
79             }
80         }
81         catch (NumberFormatException nfe) {
82             warnings.add("Could not convert " + element + " value of " + content + ".  It is expected to be a number.");
83         }
84     }
85
86     /**
87      * Get the component this handler is working upon.
88      * 
89      * @return a component
90      */
91     @Override
92     public BodyTube getComponent() {
93         return bodyTube;
94     }
95
96     /**
97      * Get the required type of material for this component.
98      *
99      * @return BULK
100      */
101     public Material.Type getMaterialType() {
102         return Material.Type.BULK;
103     }
104 }
105