2 * TransitionHandler.java
4 package net.sf.openrocket.file.rocksim;
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;
14 import java.util.HashMap;
17 * The SAX handler for Transition components.
19 class TransitionHandler extends BaseHandler<Transition> {
21 * The OpenRocket Transition.
23 private final Transition transition = new Transition();
26 * The wall thickness. Used for hollow nose cones.
28 private double thickness = 0d;
33 * @param c the parent component
34 * @throws IllegalArgumentException thrown if <code>c</code> is null
36 public TransitionHandler(RocketComponent c) throws IllegalArgumentException {
38 throw new IllegalArgumentException("The parent of a transition may not be null.");
40 c.addChild(transition);
44 public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) {
45 return PlainTextHandler.INSTANCE;
49 public void closeElement(String element, HashMap<String, String> attributes,
50 String content, WarningSet warnings) throws SAXException {
51 super.closeElement(element, attributes, content, warnings);
54 if ("ShapeCode".equals(element)) {
55 transition.setType(RocksimNoseConeCode.fromCode(Integer.parseInt(content)).asOpenRocket());
57 if ("Len".equals(element)) {
58 transition.setLength(Math.max(0, Double.parseDouble(
59 content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH));
61 if ("FrontDia".equals(element)) {
62 transition.setForeRadius(Math.max(0, Double.parseDouble(
63 content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS));
65 if ("RearDia".equals(element)) {
66 transition.setAftRadius(Math.max(0, Double.parseDouble(
67 content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS));
69 if ("WallThickness".equals(element)) {
70 thickness = Math.max(0d, Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
72 if ("FrontShoulderDia".equals(element)) {
73 transition.setForeShoulderRadius(Math.max(0d, Double.parseDouble(
74 content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS));
76 if ("RearShoulderDia".equals(element)) {
77 transition.setAftShoulderRadius(Math.max(0d, Double.parseDouble(
78 content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS));
80 if ("FrontShoulderLen".equals(element)) {
81 transition.setForeShoulderLength(Math.max(0d, Double.parseDouble(
82 content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH));
84 if ("RearShoulderLen".equals(element)) {
85 transition.setAftShoulderLength(Math.max(0d, Double.parseDouble(
86 content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH));
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));
95 if ("ConstructionType".equals(element)) {
96 int typeCode = Integer.parseInt(content);
99 transition.setFilled(true);
101 else if (typeCode == 1) {
103 transition.setFilled(false);
106 if ("FinishCode".equals(element)) {
107 transition.setFinish(RocksimFinishCode.fromCode(Integer.parseInt(content)).asOpenRocket());
109 if ("Material".equals(element)) {
110 setMaterialName(content);
113 catch (NumberFormatException nfe) {
114 warnings.add("Could not convert " + element + " value of " + content + ". It is expected to be a number.");
119 public void endHandler(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
120 throws SAXException {
121 super.endHandler(element, attributes, content, warnings);
123 if (transition.isFilled()) {
124 transition.setAftShoulderThickness(transition.getAftShoulderRadius());
125 transition.setForeShoulderThickness(transition.getForeShoulderRadius());
128 transition.setThickness(thickness);
129 transition.setAftShoulderThickness(thickness);
130 transition.setForeShoulderThickness(thickness);
136 public Transition getComponent() {
141 * Get the required type of material for this component.
145 public Material.Type getMaterialType() {
146 return Material.Type.BULK;