lose embedded source jars from upstream branch
[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         return PlainTextHandler.INSTANCE;
52     }
53
54     @Override
55     public void closeElement(String element, HashMap<String, String> attributes,
56                              String content, WarningSet warnings) throws SAXException {
57         super.closeElement(element, attributes, content, warnings);
58
59         try {
60             if ("ShapeCode".equals(element)) {
61                 transition.setType(RocksimNoseConeCode.fromCode(Integer.parseInt(content)).asOpenRocket());
62             }
63             if ("Len".equals(element)) {
64                 transition.setLength(Math.max(0, Double.parseDouble(
65                         content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH));
66             }
67             if ("FrontDia".equals(element)) {
68                 transition.setForeRadius(Math.max(0, Double.parseDouble(
69                         content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS));
70             }
71             if ("RearDia".equals(element)) {
72                 transition.setAftRadius(Math.max(0, Double.parseDouble(
73                         content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS));
74             }
75             if ("WallThickness".equals(element)) {
76                 thickness = Math.max(0d, Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
77             }
78             if ("FrontShoulderDia".equals(element)) {
79                 transition.setForeShoulderRadius(Math.max(0d, Double.parseDouble(
80                         content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS));
81             }
82             if ("RearShoulderDia".equals(element)) {
83                 transition.setAftShoulderRadius(Math.max(0d, Double.parseDouble(
84                         content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS));
85             }
86             if ("FrontShoulderLen".equals(element)) {
87                 transition.setForeShoulderLength(Math.max(0d, Double.parseDouble(
88                         content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH));
89             }
90             if ("RearShoulderLen".equals(element)) {
91                 transition.setAftShoulderLength(Math.max(0d, Double.parseDouble(
92                         content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH));
93             }
94             if ("ShapeParameter".equals(element)) {
95                 if (Transition.Shape.POWER.equals(transition.getType()) ||
96                     Transition.Shape.HAACK.equals(transition.getType()) ||
97                     Transition.Shape.PARABOLIC.equals(transition.getType())) {
98                     transition.setShapeParameter(Double.parseDouble(content));
99                 }
100             }
101             if ("ConstructionType".equals(element)) {
102                 int typeCode = Integer.parseInt(content);
103                 if (typeCode == 0) {
104                     //SOLID
105                     transition.setFilled(true);
106                 }
107                 else if (typeCode == 1) {
108                     //HOLLOW
109                     transition.setFilled(false);
110                 }
111             }
112             if ("FinishCode".equals(element)) {
113                 transition.setFinish(RocksimFinishCode.fromCode(Integer.parseInt(content)).asOpenRocket());
114             }
115             if ("Material".equals(element)) {
116                 setMaterialName(content);
117             }
118         }
119         catch (NumberFormatException nfe) {
120             warnings.add("Could not convert " + element + " value of " + content + ".  It is expected to be a number.");
121         }
122     }
123
124     @Override
125     public void endHandler(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
126             throws SAXException {
127         super.endHandler(element, attributes, content, warnings);
128
129         if (transition.isFilled()) {
130             transition.setAftShoulderThickness(transition.getAftShoulderRadius());
131             transition.setForeShoulderThickness(transition.getForeShoulderRadius());
132         }
133         else {
134             transition.setThickness(thickness);
135             transition.setAftShoulderThickness(thickness);
136             transition.setForeShoulderThickness(thickness);
137         }
138     }
139
140
141     @Override
142     public Transition getComponent() {
143         return transition;
144     }
145
146     /**
147      * Get the required type of material for this component.
148      *
149      * @return BULK
150      */
151     public Material.Type getMaterialType() {
152         return Material.Type.BULK;
153     }
154
155
156 }
157