create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / file / rocksim / importt / TransitionHandler.java
1 /*
2  * TransitionHandler.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.rocksim.RocksimNoseConeCode;
10 import net.sf.openrocket.file.simplesax.ElementHandler;
11 import net.sf.openrocket.file.simplesax.PlainTextHandler;
12 import net.sf.openrocket.material.Material;
13 import net.sf.openrocket.rocketcomponent.RocketComponent;
14 import net.sf.openrocket.rocketcomponent.Transition;
15 import org.xml.sax.SAXException;
16
17 import java.util.HashMap;
18
19 /**
20  * The SAX handler for Transition components.
21  */
22 class TransitionHandler extends BaseHandler<Transition> {
23     /**
24      * The OpenRocket Transition.
25      */
26     private final Transition transition = new Transition();
27
28     /**
29      * The wall thickness.  Used for hollow nose cones.
30      */
31     private double thickness = 0d;
32
33     /**
34      * Constructor.
35      *
36      * @param c the parent component
37      * @param warnings  the warning set
38      * @throws IllegalArgumentException thrown if <code>c</code> is null
39      */
40     public TransitionHandler(RocketComponent c, WarningSet warnings) throws IllegalArgumentException {
41         if (c == null) {
42             throw new IllegalArgumentException("The parent of a transition may not be null.");
43         }
44         if (isCompatible(c, Transition.class, warnings)) {
45             c.addChild(transition);
46         }
47     }
48
49     @Override
50     public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) {
51         if (RocksimCommonConstants.ATTACHED_PARTS.equals(element)) {
52             return new AttachedPartsHandler(transition);
53         }
54         return PlainTextHandler.INSTANCE;
55     }
56
57     @Override
58     public void closeElement(String element, HashMap<String, String> attributes,
59                              String content, WarningSet warnings) throws SAXException {
60         super.closeElement(element, attributes, content, warnings);
61
62         try {
63             if ("ShapeCode".equals(element)) {
64                 transition.setType(RocksimNoseConeCode.fromCode(Integer.parseInt(content)).asOpenRocket());
65             }
66             if ("Len".equals(element)) {
67                 transition.setLength(Math.max(0, Double.parseDouble(
68                         content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH));
69             }
70             if ("FrontDia".equals(element)) {
71                 transition.setForeRadius(Math.max(0, Double.parseDouble(
72                         content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS));
73             }
74             if ("RearDia".equals(element)) {
75                 transition.setAftRadius(Math.max(0, Double.parseDouble(
76                         content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS));
77             }
78             if ("WallThickness".equals(element)) {
79                 thickness = Math.max(0d, Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
80             }
81             if ("FrontShoulderDia".equals(element)) {
82                 transition.setForeShoulderRadius(Math.max(0d, Double.parseDouble(
83                         content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS));
84             }
85             if ("RearShoulderDia".equals(element)) {
86                 transition.setAftShoulderRadius(Math.max(0d, Double.parseDouble(
87                         content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS));
88             }
89             if ("FrontShoulderLen".equals(element)) {
90                 transition.setForeShoulderLength(Math.max(0d, Double.parseDouble(
91                         content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH));
92             }
93             if ("RearShoulderLen".equals(element)) {
94                 transition.setAftShoulderLength(Math.max(0d, Double.parseDouble(
95                         content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH));
96             }
97             if ("ShapeParameter".equals(element)) {
98                 if (Transition.Shape.POWER.equals(transition.getType()) ||
99                     Transition.Shape.HAACK.equals(transition.getType()) ||
100                     Transition.Shape.PARABOLIC.equals(transition.getType())) {
101                     transition.setShapeParameter(Double.parseDouble(content));
102                 }
103             }
104             if ("ConstructionType".equals(element)) {
105                 int typeCode = Integer.parseInt(content);
106                 if (typeCode == 0) {
107                     //SOLID
108                     transition.setFilled(true);
109                 }
110                 else if (typeCode == 1) {
111                     //HOLLOW
112                     transition.setFilled(false);
113                 }
114             }
115             if ("FinishCode".equals(element)) {
116                 transition.setFinish(RocksimFinishCode.fromCode(Integer.parseInt(content)).asOpenRocket());
117             }
118             if ("Material".equals(element)) {
119                 setMaterialName(content);
120             }
121         }
122         catch (NumberFormatException nfe) {
123             warnings.add("Could not convert " + element + " value of " + content + ".  It is expected to be a number.");
124         }
125     }
126
127     @Override
128     public void endHandler(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
129             throws SAXException {
130         super.endHandler(element, attributes, content, warnings);
131
132         if (transition.isFilled()) {
133             transition.setAftShoulderThickness(transition.getAftShoulderRadius());
134             transition.setForeShoulderThickness(transition.getForeShoulderRadius());
135         }
136         else {
137             transition.setThickness(thickness);
138             transition.setAftShoulderThickness(thickness);
139             transition.setForeShoulderThickness(thickness);
140         }
141     }
142
143
144     @Override
145     public Transition getComponent() {
146         return transition;
147     }
148
149     /**
150      * Get the required type of material for this component.
151      *
152      * @return BULK
153      */
154     public Material.Type getMaterialType() {
155         return Material.Type.BULK;
156     }
157
158
159 }
160