1 package net.sf.openrocket.android.thrustcurve;
\r
3 import java.io.InputStream;
\r
5 import org.xml.sax.Attributes;
\r
7 import android.sax.Element;
\r
8 import android.sax.EndElementListener;
\r
9 import android.sax.EndTextElementListener;
\r
10 import android.sax.RootElement;
\r
11 import android.sax.StartElementListener;
\r
12 import android.util.Xml;
\r
14 public class SearchResponseParser {
\r
16 private static final String thrustcurveURI = "http://www.thrustcurve.org/2008/SearchResponse";
\r
18 * XML Tags in SearchResult xsd
\r
20 private static final String root_tag = "search-response";
\r
21 private static final String criteria = "criteria";
\r
22 private static final String criterion = "criterion";
\r
23 private static final String name = "name";
\r
24 private static final String value = "value";
\r
25 private static final String matches = "matches";
\r
26 private static final String results = "results";
\r
27 private static final String result = "result";
\r
29 private static final String motor_id = "motor-id";
\r
30 private static final String manufacturer = "manufacturer";
\r
31 private static final String manufacturer_abbr = "manufacturer-abbrev";
\r
32 private static final String designation = "designation";
\r
33 private static final String brand_name = "brand-name";
\r
34 private static final String common_name = "common-name";
\r
35 private static final String impulse_class = "impulse-class";
\r
36 private static final String diameter = "diameter";
\r
37 private static final String length = "length";
\r
38 private static final String type = "type";
\r
39 private static final String cert_org = "cert-org";
\r
40 private static final String avg_thrust_n = "avg-thrust-n";
\r
41 private static final String max_thrust_n = "max-thrust-n";
\r
42 private static final String tot_impulse_ns = "tot-impulse-ns";
\r
43 private static final String burn_time_s = "burn-time-s";
\r
44 private static final String data_files = "data-files";
\r
45 private static final String info_url = "info-url";
\r
46 private static final String total_weight_g = "total-weight-g";
\r
47 private static final String prop_weight_g = "prop-weight-g";
\r
48 private static final String delays = "delays";
\r
49 private static final String case_info = "case-info";
\r
50 private static final String prop_info = "prop-info";
\r
51 private static final String updated_on = "updated-on";
\r
53 public static SearchResponse parse( InputStream in ) {
\r
55 final SearchResponse ret = new SearchResponse();
\r
56 final TCMotor currentMotor = new TCMotor();
\r
58 RootElement rootEl = new RootElement(thrustcurveURI, root_tag);
\r
59 Element criteriaEl = rootEl.getChild( thrustcurveURI, criteria);
\r
61 criteriaEl.getChild(thrustcurveURI,matches).setEndTextElementListener(
\r
62 new EndTextElementListener() {
\r
64 public void end(String arg0) {
\r
65 ret.setMatches(Integer.parseInt(arg0));
\r
69 Element resultsEl = rootEl.getChild(thrustcurveURI,results);
\r
70 Element resultEl = resultsEl.getChild(thrustcurveURI,result);
\r
72 resultEl.setStartElementListener(
\r
73 new StartElementListener() {
\r
75 public void start(Attributes arg0) {
\r
76 currentMotor.init();
\r
81 resultEl.setEndElementListener(
\r
82 new EndElementListener() {
\r
85 ret.getResults().add((TCMotor)currentMotor.clone());
\r
90 resultEl.getChild(thrustcurveURI,motor_id).setEndTextElementListener(
\r
91 new EndTextElementListener() {
\r
93 public void end(String arg0) {
\r
94 currentMotor.setMotor_id(Integer.parseInt(arg0));
\r
98 resultEl.getChild(thrustcurveURI,manufacturer).setEndTextElementListener(
\r
99 new EndTextElementListener() {
\r
101 public void end(String arg0) {
\r
102 if ( arg0 != null ) {
\r
103 currentMotor.setManufacturer(arg0);
\r
108 resultEl.getChild(thrustcurveURI,manufacturer_abbr).setEndTextElementListener(
\r
109 new EndTextElementListener() {
\r
111 public void end(String arg0) {
\r
112 if ( arg0 != null ) {
\r
113 currentMotor.setManufacturer_abbr(arg0);
\r
118 resultEl.getChild(thrustcurveURI,designation).setEndTextElementListener(
\r
119 new EndTextElementListener() {
\r
121 public void end(String arg0) {
\r
122 currentMotor.setDesignation(arg0);
\r
126 resultEl.getChild(thrustcurveURI,brand_name).setEndTextElementListener(
\r
127 new EndTextElementListener() {
\r
129 public void end(String arg0) {
\r
130 currentMotor.setBrand_name(arg0);
\r
134 resultEl.getChild(thrustcurveURI,common_name).setEndTextElementListener(
\r
135 new EndTextElementListener() {
\r
137 public void end(String arg0) {
\r
138 currentMotor.setCommon_name(arg0);
\r
142 resultEl.getChild(thrustcurveURI,impulse_class).setEndTextElementListener(
\r
143 new EndTextElementListener() {
\r
145 public void end(String arg0) {
\r
146 currentMotor.setImpulse_class(arg0);
\r
150 resultEl.getChild(thrustcurveURI,diameter).setEndTextElementListener(
\r
151 new EndTextElementListener() {
\r
153 public void end(String arg0) {
\r
154 currentMotor.setDiameter(Float.parseFloat(arg0));
\r
158 resultEl.getChild(thrustcurveURI,length).setEndTextElementListener(
\r
159 new EndTextElementListener() {
\r
161 public void end(String arg0) {
\r
162 currentMotor.setLength(Float.parseFloat(arg0));
\r
166 resultEl.getChild(thrustcurveURI,type).setEndTextElementListener(
\r
167 new EndTextElementListener() {
\r
169 public void end(String arg0) {
\r
170 currentMotor.setType(arg0);
\r
174 resultEl.getChild(thrustcurveURI,cert_org).setEndTextElementListener(
\r
175 new EndTextElementListener() {
\r
177 public void end(String arg0) {
\r
178 currentMotor.setCert_org(arg0);
\r
182 resultEl.getChild(thrustcurveURI,avg_thrust_n).setEndTextElementListener(
\r
183 new EndTextElementListener() {
\r
185 public void end(String arg0) {
\r
186 currentMotor.setAvg_thrust_n(Float.parseFloat(arg0));
\r
190 resultEl.getChild(thrustcurveURI,max_thrust_n).setEndTextElementListener(
\r
191 new EndTextElementListener() {
\r
193 public void end(String arg0) {
\r
194 currentMotor.setMax_thrust_n(Float.parseFloat(arg0));
\r
198 resultEl.getChild(thrustcurveURI,tot_impulse_ns).setEndTextElementListener(
\r
199 new EndTextElementListener() {
\r
201 public void end(String arg0) {
\r
202 currentMotor.setTot_impulse_ns(Float.parseFloat(arg0));
\r
206 resultEl.getChild(thrustcurveURI,burn_time_s).setEndTextElementListener(
\r
207 new EndTextElementListener() {
\r
209 public void end(String arg0) {
\r
210 currentMotor.setBurn_time_s(Float.parseFloat(arg0));
\r
214 resultEl.getChild(thrustcurveURI,data_files).setEndTextElementListener(
\r
215 new EndTextElementListener() {
\r
217 public void end(String arg0) {
\r
218 currentMotor.setData_files(Integer.parseInt(arg0));
\r
222 resultEl.getChild(thrustcurveURI,info_url).setEndTextElementListener(
\r
223 new EndTextElementListener() {
\r
225 public void end(String arg0) {
\r
226 currentMotor.setInfo_url(arg0);
\r
230 resultEl.getChild(thrustcurveURI,total_weight_g).setEndTextElementListener(
\r
231 new EndTextElementListener() {
\r
233 public void end(String arg0) {
\r
234 currentMotor.setTot_mass_g(Double.parseDouble(arg0));
\r
238 resultEl.getChild(thrustcurveURI,prop_weight_g).setEndTextElementListener(
\r
239 new EndTextElementListener() {
\r
241 public void end(String arg0) {
\r
242 currentMotor.setProp_mass_g(Double.parseDouble(arg0));
\r
246 resultEl.getChild(thrustcurveURI,delays).setEndTextElementListener(
\r
247 new EndTextElementListener() {
\r
249 public void end(String arg0) {
\r
250 currentMotor.setDelays(arg0);
\r
254 resultEl.getChild(thrustcurveURI,case_info).setEndTextElementListener(
\r
255 new EndTextElementListener() {
\r
257 public void end(String arg0) {
\r
258 currentMotor.setCase_info(arg0);
\r
262 resultEl.getChild(thrustcurveURI,prop_info).setEndTextElementListener(
\r
263 new EndTextElementListener() {
\r
265 public void end(String arg0) {
\r
266 currentMotor.setProp_info(arg0);
\r
273 Xml.parse(in, Xml.Encoding.UTF_8, rootEl.getContentHandler());
\r
274 } catch (Exception e) {
\r
275 throw new RuntimeException(e);
\r