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