DGP - added isCompatible checks for all Rocksim components
[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      * @param warnings  the warning set
31      * @throws IllegalArgumentException thrown if <code>c</code> is null
32      */
33     public RingHandler(RocketComponent c, WarningSet warnings) throws IllegalArgumentException {
34         if (c == null) {
35             throw new IllegalArgumentException("The parent of a ring may not be null.");
36         }
37         ring = new CenteringRing();
38         if (isCompatible(c, CenteringRing.class, warnings)) {
39             c.addChild(ring);
40         }
41     }
42
43     @Override
44     public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) {
45         return PlainTextHandler.INSTANCE;
46     }
47
48     @Override
49     public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
50             throws SAXException {
51         super.closeElement(element, attributes, content, warnings);
52
53         try {
54             if ("OD".equals(element)) {
55                 ring.setOuterRadius(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS);
56             }
57             if ("ID".equals(element)) {
58                 ring.setInnerRadius(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS);
59             }
60             if ("Len".equals(element)) {
61                 ring.setLength(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
62             }
63             if ("Material".equals(element)) {
64                 setMaterialName(content);
65             }
66         }
67         catch (NumberFormatException nfe) {
68             warnings.add("Could not convert " + element + " value of " + content + ".  It is expected to be a number.");
69         }
70     }
71
72     /**
73      * Get the ring component this handler is working upon.
74      * 
75      * @return a component
76      */
77     @Override
78     public CenteringRing getComponent() {
79         return ring;
80     }
81
82     /**
83      * Set the relative position onto the component.  This cannot be done directly because setRelativePosition is not 
84      * public in all components.
85      * 
86      * @param position  the OpenRocket position
87      */
88     @Override
89     public void setRelativePosition(RocketComponent.Position position) {
90         ring.setRelativePosition(position);
91     }
92
93     /**
94      * Get the required type of material for this component.
95      *
96      * @return BULK
97      */
98     @Override
99     public Material.Type getMaterialType() {
100         return Material.Type.BULK;
101     }
102 }
103