2 * InnerBodyTubeHandler.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.simplesax.ElementHandler;
9 import net.sf.openrocket.file.simplesax.PlainTextHandler;
10 import net.sf.openrocket.material.Material;
11 import net.sf.openrocket.rocketcomponent.InnerTube;
12 import net.sf.openrocket.rocketcomponent.RocketComponent;
13 import org.xml.sax.SAXException;
15 import java.util.HashMap;
18 * A SAX handler for Rocksim inside tubes.
20 class InnerBodyTubeHandler extends PositionDependentHandler<InnerTube> {
23 * The OpenRocket InnerTube instance.
25 private final InnerTube bodyTube;
30 * @param c the parent component
31 * @param warnings the warning set
32 * @throws IllegalArgumentException thrown if <code>c</code> is null
34 public InnerBodyTubeHandler(RocketComponent c, WarningSet warnings) throws IllegalArgumentException {
36 throw new IllegalArgumentException("The parent component of an inner tube may not be null.");
38 bodyTube = new InnerTube();
39 if (isCompatible(c, InnerTube.class, warnings)) {
45 public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) {
46 if (RocksimCommonConstants.ATTACHED_PARTS.equals(element)) {
47 return new AttachedPartsHandler(bodyTube);
49 return PlainTextHandler.INSTANCE;
53 public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
55 super.closeElement(element, attributes, content, warnings);
58 if (RocksimCommonConstants.OD.equals(element)) {
59 bodyTube.setOuterRadius(Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS);
61 if (RocksimCommonConstants.ID.equals(element)) {
62 final double r = Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_RADIUS;
63 bodyTube.setInnerRadius(r);
65 if (RocksimCommonConstants.LEN.equals(element)) {
66 bodyTube.setLength(Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
68 if (RocksimCommonConstants.IS_MOTOR_MOUNT.equals(element)) {
69 bodyTube.setMotorMount("1".equals(content));
71 if (RocksimCommonConstants.ENGINE_OVERHANG.equals(element)) {
72 bodyTube.setMotorOverhang(Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
74 if (RocksimCommonConstants.MATERIAL.equals(element)) {
75 setMaterialName(content);
77 if (RocksimCommonConstants.RADIAL_ANGLE.equals(element)) {
78 bodyTube.setRadialDirection(Double.parseDouble(content));
80 if (RocksimCommonConstants.RADIAL_LOC.equals(element)) {
81 bodyTube.setRadialPosition(Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
84 catch (NumberFormatException nfe) {
85 warnings.add("Could not convert " + element + " value of " + content + ". It is expected to be a number.");
90 * Get the InnerTube component this handler is working upon.
92 * @return an InnerTube component
95 public InnerTube getComponent() {
100 * Set the relative position onto the component. This cannot be done directly because setRelativePosition is not
101 * public in all components.
103 * @param position the OpenRocket position
106 public void setRelativePosition(RocketComponent.Position position) {
107 bodyTube.setRelativePosition(position);
111 * Get the required type of material for this component.
116 public Material.Type getMaterialType() {
117 return Material.Type.BULK;