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