committed Doug's Rocksim loader
[debian/openrocket] / src / net / sf / openrocket / file / rocksim / LaunchLugHandler.java
1 /*
2  * LaunchLugHandler.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.LaunchLug;
11 import net.sf.openrocket.rocketcomponent.RocketComponent;
12 import org.xml.sax.SAXException;
13
14 import java.util.HashMap;
15
16 /**
17  * The SAX handler for Rocksim Launch Lugs.
18  */
19 class LaunchLugHandler extends PositionDependentHandler<LaunchLug> {
20
21     /**
22      * The OpenRocket LaunchLug instance.
23      */
24     private final LaunchLug lug;
25
26     /**
27      * Constructor.
28      *
29      * @param c the parent
30      * @throws IllegalArgumentException thrown if <code>c</code> is null
31      */
32     public LaunchLugHandler(RocketComponent c) throws IllegalArgumentException {
33         if (c == null) {
34             throw new IllegalArgumentException("The parent component of a launch lug may not be null.");
35         }
36         lug = new LaunchLug();
37         c.addChild(lug);
38     }
39
40     @Override
41     public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) {
42         return PlainTextHandler.INSTANCE;
43     }
44
45     @Override
46     public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
47             throws SAXException {
48         super.closeElement(element, attributes, content, warnings);
49
50         try {
51             if ("OD".equals(element)) {
52                 lug.setRadius(Math.max(0, Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS));
53             }
54             if ("ID".equals(element)) {
55                 lug.setInnerRadius(Math.max(0, Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS));
56             }
57             if ("Len".equals(element)) {
58                 lug.setLength(Math.max(0, Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH));
59             }
60             if ("Material".equals(element)) {
61                 setMaterialName(content);
62             }
63             if ("FinishCode".equals(element)) {
64                 lug.setFinish(RocksimFinishCode.fromCode(Integer.parseInt(content)).asOpenRocket());
65             }
66         }
67         catch (NumberFormatException nfe) {
68             warnings.add("Could not convert " + element + " value of " + content + ".  It is expected to be a number.");
69         }
70     }
71
72     /**
73      * Get the LaunchLug component this handler is working upon.
74      * 
75      * @return a LaunchLug component
76      */
77     @Override
78     public LaunchLug getComponent() {
79         return lug;
80     }
81
82     /**
83      * Set the relative position onto the component.  This cannot be done directly because setRelativePosition is not 
84      * public in all components.
85      * 
86      * @param position  the OpenRocket position
87      */
88     @Override
89     public void setRelativePosition(RocketComponent.Position position) {
90         lug.setRelativePosition(position);
91     }
92
93     /**
94      * Get the required type of material for this component.
95      *
96      * @return BULK
97      */
98     @Override
99     public Material.Type getMaterialType() {
100         return Material.Type.BULK;
101     }
102 }
103