committed Doug's Rocksim loader
[debian/openrocket] / src / net / sf / openrocket / file / rocksim / BodyTubeHandler.java
1 /*
2  * BodyTubeHandler.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.BodyTube;
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 Body Tubes.
18  */
19 class BodyTubeHandler extends BaseHandler<BodyTube> {
20     /**
21      * The OpenRocket BodyTube.
22      */
23     private final BodyTube bodyTube;
24
25     /**
26      * Constructor.
27      *
28      * @param c parent component
29      * @throws IllegalArgumentException thrown if <code>c</code> is null
30      */
31     public BodyTubeHandler(RocketComponent c) throws IllegalArgumentException {
32         if (c == null) {
33             throw new IllegalArgumentException("The parent component of a body tube may not be null.");
34         }
35         bodyTube = new BodyTube();
36         c.addChild(bodyTube);
37     }
38
39     @Override
40     public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) {
41         if ("AttachedParts".equals(element)) {
42             return new AttachedPartsHandler(bodyTube);
43         }
44         return PlainTextHandler.INSTANCE;
45     }
46
47     @Override
48     public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
49             throws SAXException {
50         super.closeElement(element, attributes, content, warnings);
51
52         try {
53             if ("OD".equals(element)) {
54                 bodyTube.setRadius(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS);
55             }
56             if ("ID".equals(element)) {
57                 final double r = Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS;
58                 bodyTube.setInnerRadius(r);
59             }
60             if ("Len".equals(element)) {
61                 bodyTube.setLength(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
62             }
63             if ("FinishCode".equals(element)) {
64                 bodyTube.setFinish(RocksimFinishCode.fromCode(Integer.parseInt(content)).asOpenRocket());
65             }
66             if ("IsMotorMount".equals(element)) {
67                 bodyTube.setMotorMount("1".equals(content));
68             }
69             if ("EngineOverhang".equals(element)) {
70                 bodyTube.setMotorOverhang(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
71             }
72             if ("Material".equals(element)) {
73                 setMaterialName(content);
74             }
75         }
76         catch (NumberFormatException nfe) {
77             warnings.add("Could not convert " + element + " value of " + content + ".  It is expected to be a number.");
78         }
79     }
80
81     /**
82      * Get the component this handler is working upon.
83      * 
84      * @return a component
85      */
86     @Override
87     public BodyTube getComponent() {
88         return bodyTube;
89     }
90
91     /**
92      * Get the required type of material for this component.
93      *
94      * @return BULK
95      */
96     public Material.Type getMaterialType() {
97         return Material.Type.BULK;
98     }
99 }
100