committed Doug's Rocksim loader
[debian/openrocket] / src / net / sf / openrocket / file / rocksim / MassObjectHandler.java
1 /*
2  * MassObjectHandler.java
3  */
4 package net.sf.openrocket.file.rocksim;
5
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.MassComponent;
11 import net.sf.openrocket.rocketcomponent.RocketComponent;
12 import org.xml.sax.SAXException;
13
14 import java.util.HashMap;
15
16 /**
17  * A SAX handler for Rocksim's MassObject XML type.
18  */
19 class MassObjectHandler extends PositionDependentHandler<MassComponent> {
20
21     /** 
22      * The Rocksim Mass length fudge factor.  Rocksim completely exaggerates the length of a mass object to the point
23      * that it looks ridiculous in OpenRocket.  This fudge factor is here merely to get the typical mass object to
24      * render in the OpenRocket UI with it's bounds mostly inside it's parent.  The odd thing about it is that 
25      * Rocksim does not expose the length of a mass object in the UI and actually treats mass objects as point objects -
26      * not 3 or even 2 dimensional.
27      */
28     public static final int MASS_LEN_FUDGE_FACTOR = 100;
29
30     /**
31      * The OpenRocket MassComponent - counterpart to the RS MassObject.
32      */
33     private final MassComponent mass;
34
35     /**
36      * Constructor.
37      *l
38      * @param c the parent component
39      * 
40      * @throws IllegalArgumentException  thrown if <code>c</code> is null
41      */
42     public MassObjectHandler(RocketComponent c) throws IllegalArgumentException {
43         if (c == null) {
44             throw new IllegalArgumentException("The parent component of a mass component may not be null.");
45         }
46         mass = new MassComponent();
47         c.addChild(mass);
48     }
49
50     @Override
51     public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) {
52         return PlainTextHandler.INSTANCE;
53     }
54
55     @Override
56     public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
57             throws SAXException {
58         super.closeElement(element, attributes, content, warnings);
59         try {
60             if ("Len".equals(element)) {
61                 mass.setLength(Double.parseDouble(content) / (RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH * MASS_LEN_FUDGE_FACTOR));
62             }
63             if ("KnownMass".equals(element)) {
64                 mass.setComponentMass(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_MASS);
65             }
66             if ("KnownCG".equals(element)) {
67                 //Setting the CG of the Mass Object to 0 is important because of the different ways that Rocksim and
68                 //OpenRocket treat mass objects.  Rocksim treats them as points (even though the data file contains a
69                 //length) and because Rocksim sets the CG of the mass object to really be relative to the front of
70                 //the parent.  But that value is already assumed in the position and position value for the component.
71                 //Thus it needs to be set to 0 to say that the mass object's CG is at the point of the mass object.
72                 super.setCG(0); 
73             }
74         }
75         catch (NumberFormatException nfe) {
76             warnings.add("Could not convert " + element + " value of " + content + ".  It is expected to be a number.");
77         }
78     }
79
80     /**
81      * Get the component this handler is working upon.
82      *
83      * @return a component
84      */
85     @Override
86     public MassComponent getComponent() {
87         return mass;
88     }
89
90     /**
91      * Set the relative position onto the component.  This cannot be done directly because setRelativePosition is not
92      * public in all components.
93      *
94      * @param position the OpenRocket position
95      */
96     public void setRelativePosition(RocketComponent.Position position) {
97         mass.setRelativePosition(position);
98     }
99
100     /**
101      * Get the required type of material for this component.  Does not apply to MassComponents.
102      *
103      * @return BULK
104      */
105     @Override
106     public Material.Type getMaterialType() {
107         return Material.Type.BULK;
108     }
109
110 }