2 * TransitionHandler.java
4 package net.sf.openrocket.file.rocksim.importt;
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;
17 import java.util.HashMap;
20 * The SAX handler for Transition components.
22 class TransitionHandler extends BaseHandler<Transition> {
24 * The OpenRocket Transition.
26 private final Transition transition = new Transition();
29 * The wall thickness. Used for hollow nose cones.
31 private double thickness = 0d;
36 * @param c the parent component
37 * @param warnings the warning set
38 * @throws IllegalArgumentException thrown if <code>c</code> is null
40 public TransitionHandler(RocketComponent c, WarningSet warnings) throws IllegalArgumentException {
42 throw new IllegalArgumentException("The parent of a transition may not be null.");
44 if (isCompatible(c, Transition.class, warnings)) {
45 c.addChild(transition);
50 public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) {
51 if (RocksimCommonConstants.ATTACHED_PARTS.equals(element)) {
52 return new AttachedPartsHandler(transition);
54 return PlainTextHandler.INSTANCE;
58 public void closeElement(String element, HashMap<String, String> attributes,
59 String content, WarningSet warnings) throws SAXException {
60 super.closeElement(element, attributes, content, warnings);
63 if ("ShapeCode".equals(element)) {
64 transition.setType(RocksimNoseConeCode.fromCode(Integer.parseInt(content)).asOpenRocket());
66 if ("Len".equals(element)) {
67 transition.setLength(Math.max(0, Double.parseDouble(
68 content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH));
70 if ("FrontDia".equals(element)) {
71 transition.setForeRadius(Math.max(0, Double.parseDouble(
72 content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS));
74 if ("RearDia".equals(element)) {
75 transition.setAftRadius(Math.max(0, Double.parseDouble(
76 content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS));
78 if ("WallThickness".equals(element)) {
79 thickness = Math.max(0d, Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
81 if ("FrontShoulderDia".equals(element)) {
82 transition.setForeShoulderRadius(Math.max(0d, Double.parseDouble(
83 content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS));
85 if ("RearShoulderDia".equals(element)) {
86 transition.setAftShoulderRadius(Math.max(0d, Double.parseDouble(
87 content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS));
89 if ("FrontShoulderLen".equals(element)) {
90 transition.setForeShoulderLength(Math.max(0d, Double.parseDouble(
91 content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH));
93 if ("RearShoulderLen".equals(element)) {
94 transition.setAftShoulderLength(Math.max(0d, Double.parseDouble(
95 content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH));
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));
104 if ("ConstructionType".equals(element)) {
105 int typeCode = Integer.parseInt(content);
108 transition.setFilled(true);
110 else if (typeCode == 1) {
112 transition.setFilled(false);
115 if ("FinishCode".equals(element)) {
116 transition.setFinish(RocksimFinishCode.fromCode(Integer.parseInt(content)).asOpenRocket());
118 if ("Material".equals(element)) {
119 setMaterialName(content);
122 catch (NumberFormatException nfe) {
123 warnings.add("Could not convert " + element + " value of " + content + ". It is expected to be a number.");
128 public void endHandler(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
129 throws SAXException {
130 super.endHandler(element, attributes, content, warnings);
132 if (transition.isFilled()) {
133 transition.setAftShoulderThickness(transition.getAftShoulderRadius());
134 transition.setForeShoulderThickness(transition.getForeShoulderRadius());
137 transition.setThickness(thickness);
138 transition.setAftShoulderThickness(thickness);
139 transition.setForeShoulderThickness(thickness);
145 public Transition getComponent() {
150 * Get the required type of material for this component.
154 public Material.Type getMaterialType() {
155 return Material.Type.BULK;