committed Doug's Rocksim loader
[debian/openrocket] / src / net / sf / openrocket / file / rocksim / RingHandler.java
1 /*
2  * RingHandler.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.CenteringRing;
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 centering rings and bulkheads.
18  */
19 class RingHandler extends PositionDependentHandler<CenteringRing> {
20
21     /**
22      * The OpenRocket Ring.
23      */
24     private final CenteringRing ring;
25
26     /**
27      * Constructor.
28      *
29      * @param c the parent component
30      * @throws IllegalArgumentException thrown if <code>c</code> is null
31      */
32     public RingHandler(RocketComponent c) throws IllegalArgumentException {
33         if (c == null) {
34             throw new IllegalArgumentException("The parent of a ring may not be null.");
35         }
36         ring = new CenteringRing();
37         c.addChild(ring);
38     }
39
40     @Override
41     public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) {
42         return PlainTextHandler.INSTANCE;
43     }
44
45     @Override
46     public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
47             throws SAXException {
48         super.closeElement(element, attributes, content, warnings);
49
50         try {
51             if ("OD".equals(element)) {
52                 ring.setOuterRadius(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS);
53             }
54             if ("ID".equals(element)) {
55                 ring.setInnerRadius(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS);
56             }
57             if ("Len".equals(element)) {
58                 ring.setLength(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
59             }
60             if ("Material".equals(element)) {
61                 setMaterialName(content);
62             }
63         }
64         catch (NumberFormatException nfe) {
65             warnings.add("Could not convert " + element + " value of " + content + ".  It is expected to be a number.");
66         }
67     }
68
69     /**
70      * Get the ring component this handler is working upon.
71      * 
72      * @return a component
73      */
74     @Override
75     public CenteringRing getComponent() {
76         return ring;
77     }
78
79     /**
80      * Set the relative position onto the component.  This cannot be done directly because setRelativePosition is not 
81      * public in all components.
82      * 
83      * @param position  the OpenRocket position
84      */
85     @Override
86     public void setRelativePosition(RocketComponent.Position position) {
87         ring.setRelativePosition(position);
88     }
89
90     /**
91      * Get the required type of material for this component.
92      *
93      * @return BULK
94      */
95     @Override
96     public Material.Type getMaterialType() {
97         return Material.Type.BULK;
98     }
99 }
100