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 return PlainTextHandler.INSTANCE;
55 public void closeElement(String element, HashMap<String, String> attributes,
56 String content, WarningSet warnings) throws SAXException {
57 super.closeElement(element, attributes, content, warnings);
60 if ("ShapeCode".equals(element)) {
61 transition.setType(RocksimNoseConeCode.fromCode(Integer.parseInt(content)).asOpenRocket());
63 if ("Len".equals(element)) {
64 transition.setLength(Math.max(0, Double.parseDouble(
65 content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH));
67 if ("FrontDia".equals(element)) {
68 transition.setForeRadius(Math.max(0, Double.parseDouble(
69 content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS));
71 if ("RearDia".equals(element)) {
72 transition.setAftRadius(Math.max(0, Double.parseDouble(
73 content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS));
75 if ("WallThickness".equals(element)) {
76 thickness = Math.max(0d, Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
78 if ("FrontShoulderDia".equals(element)) {
79 transition.setForeShoulderRadius(Math.max(0d, Double.parseDouble(
80 content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS));
82 if ("RearShoulderDia".equals(element)) {
83 transition.setAftShoulderRadius(Math.max(0d, Double.parseDouble(
84 content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS));
86 if ("FrontShoulderLen".equals(element)) {
87 transition.setForeShoulderLength(Math.max(0d, Double.parseDouble(
88 content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH));
90 if ("RearShoulderLen".equals(element)) {
91 transition.setAftShoulderLength(Math.max(0d, Double.parseDouble(
92 content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH));
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));
101 if ("ConstructionType".equals(element)) {
102 int typeCode = Integer.parseInt(content);
105 transition.setFilled(true);
107 else if (typeCode == 1) {
109 transition.setFilled(false);
112 if ("FinishCode".equals(element)) {
113 transition.setFinish(RocksimFinishCode.fromCode(Integer.parseInt(content)).asOpenRocket());
115 if ("Material".equals(element)) {
116 setMaterialName(content);
119 catch (NumberFormatException nfe) {
120 warnings.add("Could not convert " + element + " value of " + content + ". It is expected to be a number.");
125 public void endHandler(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
126 throws SAXException {
127 super.endHandler(element, attributes, content, warnings);
129 if (transition.isFilled()) {
130 transition.setAftShoulderThickness(transition.getAftShoulderRadius());
131 transition.setForeShoulderThickness(transition.getForeShoulderRadius());
134 transition.setThickness(thickness);
135 transition.setAftShoulderThickness(thickness);
136 transition.setForeShoulderThickness(thickness);
142 public Transition getComponent() {
147 * Get the required type of material for this component.
151 public Material.Type getMaterialType() {
152 return Material.Type.BULK;