9db287d2731c0d6e5c975e615c809fc9df7caa08
[debian/openrocket] / src / net / sf / openrocket / file / rocksim / StreamerHandler.java
1 /*
2  * StreamerHandler.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.RocketComponent;
11 import net.sf.openrocket.rocketcomponent.Streamer;
12 import org.xml.sax.SAXException;
13
14 import java.util.HashMap;
15
16 /**
17  * A SAX handler for Streamer components.
18  */
19 class StreamerHandler extends PositionDependentHandler<Streamer> {
20
21     /**
22      * The OpenRocket Streamer.
23      */
24     private final Streamer streamer;
25
26     /**
27      * Constructor.
28      *
29      * @param c the parent component
30      * @throws IllegalArgumentException thrown if <code>c</code> is null
31      */
32     public StreamerHandler(RocketComponent c) throws IllegalArgumentException {
33         if (c == null) {
34             throw new IllegalArgumentException("The parent of a streamer may not be null.");
35         }
36         streamer = new Streamer();
37         c.addChild(streamer);
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 ("Width".equals(element)) {
52                 streamer.setStripWidth(Math.max(0, Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH));
53             }
54             if ("Len".equals(element)) {
55                 streamer.setStripLength(Math.max(0, Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH));
56             }
57             if ("DragCoefficient".equals(element)) {
58                 streamer.setCD(Double.parseDouble(content));
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     @Override
70     public Streamer getComponent() {
71         return streamer;
72     }
73
74     /**
75      * Set the relative position onto the component.  This cannot be done directly because setRelativePosition is not
76      * public in all components.
77      *
78      * @param position the OpenRocket position
79      */
80     @Override
81     public void setRelativePosition(RocketComponent.Position position) {
82         streamer.setRelativePosition(position);
83     }
84
85     /**
86      * Get the required type of material for this component.
87      *
88      * @return BULK
89      */
90     @Override
91     public Material.Type getMaterialType() {
92         return Material.Type.SURFACE;
93     }
94 }
95