DGP - added DensityType parsing for recovery devices
[debian/openrocket] / src / net / sf / openrocket / file / rocksim / ParachuteHandler.java
1 /*
2  * ParachuteHandler.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.BodyTube;
11 import net.sf.openrocket.rocketcomponent.InnerTube;
12 import net.sf.openrocket.rocketcomponent.Parachute;
13 import net.sf.openrocket.rocketcomponent.RocketComponent;
14 import org.xml.sax.SAXException;
15
16 import java.util.HashMap;
17
18 /**
19  * A SAX handler for Rocksim's Parachute XML type.
20  */
21 class ParachuteHandler extends RecoveryDeviceHandler<Parachute> {
22     /**
23      * The OpenRocket Parachute instance
24      */
25     private final Parachute chute;
26     /**
27      * The shroud line density.
28      */
29     private double shroudLineDensity = 0.0d;
30
31     /**
32      * Constructor.
33      *
34      * @param c the parent component
35      * @throws IllegalArgumentException thrown if <code>c</code> is null
36      */
37     public ParachuteHandler(RocketComponent c) throws IllegalArgumentException {
38         if (c == null) {
39             throw new IllegalArgumentException("The parent of a parachute may not be null.");
40         }
41         chute = new Parachute();
42         c.addChild(chute);
43     }
44
45     /**
46      * {@inheritDoc}
47      */
48     @Override
49     public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) {
50         return PlainTextHandler.INSTANCE;
51     }
52
53     /**
54      * {@inheritDoc}
55      */
56     @Override
57     public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
58             throws SAXException {
59         super.closeElement(element, attributes, content, warnings);
60         try {
61             if ("Dia".equals(element)) {
62                 chute.setDiameter(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
63                 /* Rocksim doesn't have a packed parachute radius, so we approximate it. */
64                 double packed;
65                 RocketComponent parent = chute.getParent();
66                 if (parent instanceof BodyTube) {
67                     packed = ((BodyTube) parent).getRadius() * 0.9;
68                 }
69                 else if (parent instanceof InnerTube) {
70                     packed = ((InnerTube) parent).getInnerRadius() * 0.9;
71                 }
72                 else {
73                     packed = chute.getDiameter() * 0.025;
74                 }
75                 chute.setRadius(packed);
76             }
77             if ("ShroudLineCount".equals(element)) {
78                 chute.setLineCount(Math.max(0, Integer.parseInt(content)));
79             }
80             if ("ShroudLineLen".equals(element)) {
81                 chute.setLineLength(Math.max(0, Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH));
82             }
83             if ("SpillHoleDia".equals(element)) {
84                 //Not supported in OpenRocket
85                 double spillHoleRadius = Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS;
86                 warnings.add("Parachute spill holes are not supported. Ignoring.");
87             }
88             if ("ShroudLineMassPerMM".equals(element)) {
89                 shroudLineDensity = Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LINE_DENSITY;
90             }
91             if ("ShroudLineMaterial".equals(element)) {
92                 chute.setLineMaterial(BaseHandler.createCustomMaterial(Material.Type.LINE, content, shroudLineDensity));
93             }
94             if ("DragCoefficient".equals(element)) {
95                 chute.setCD(Double.parseDouble(content));
96             }
97             if ("Material".equals(element)) {
98                 setMaterialName(content);
99             }
100         }
101         catch (NumberFormatException nfe) {
102             warnings.add("Could not convert " + element + " value of " + content + ".  It is expected to be a number.");
103         }
104     }
105
106     /**
107      * Get the component this handler is working upon.
108      *
109      * @return a component
110      */
111     public Parachute getComponent() {
112         return chute;
113     }
114
115 }
116