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.BodyTube;
11 import net.sf.openrocket.rocketcomponent.RocketComponent;
12 import org.xml.sax.SAXException;
14 import java.util.HashMap;
17 * A SAX handler for Rocksim Body Tubes.
19 class BodyTubeHandler extends BaseHandler<BodyTube> {
21 * The OpenRocket BodyTube.
23 private final BodyTube bodyTube;
28 * @param c parent component
29 * @throws IllegalArgumentException thrown if <code>c</code> is null
31 public BodyTubeHandler(RocketComponent c) throws IllegalArgumentException {
33 throw new IllegalArgumentException("The parent component of a body tube may not be null.");
35 bodyTube = new BodyTube();
40 public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) {
41 if ("AttachedParts".equals(element)) {
42 return new AttachedPartsHandler(bodyTube);
44 return PlainTextHandler.INSTANCE;
48 public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
50 super.closeElement(element, attributes, content, warnings);
53 if ("OD".equals(element)) {
54 bodyTube.setRadius(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS);
56 if ("ID".equals(element)) {
57 final double r = Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS;
58 bodyTube.setInnerRadius(r);
60 if ("Len".equals(element)) {
61 bodyTube.setLength(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
63 if ("FinishCode".equals(element)) {
64 bodyTube.setFinish(RocksimFinishCode.fromCode(Integer.parseInt(content)).asOpenRocket());
66 if ("IsMotorMount".equals(element)) {
67 bodyTube.setMotorMount("1".equals(content));
69 if ("EngineOverhang".equals(element)) {
70 bodyTube.setMotorOverhang(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
72 if ("Material".equals(element)) {
73 setMaterialName(content);
76 catch (NumberFormatException nfe) {
77 warnings.add("Could not convert " + element + " value of " + content + ". It is expected to be a number.");
82 * Get the component this handler is working upon.
87 public BodyTube getComponent() {
92 * Get the required type of material for this component.
96 public Material.Type getMaterialType() {
97 return Material.Type.BULK;