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