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.Log;
\r
13 import android.util.Xml;
\r
15 public class DownloadResponseParser {
\r
17 private static final String TAG = "DownloadResponseParser";
\r
19 private static final String thrustcurveURI = "http://www.thrustcurve.org/2009/DownloadResponse";
\r
21 private static final String root_tag = "download-response";
\r
22 private static final String results_tag = "results";
\r
23 private static final String result_tag = "result";
\r
24 private static final String motor_id_tag = "motor-id";
\r
25 private static final String simfile_id_tag = "simfile-id";
\r
26 private static final String format_tag = "format";
\r
27 private static final String source_tag = "source";
\r
28 private static final String license_tag = "license";
\r
29 private static final String data_tag = "data";
\r
30 private static final String error_tag = "error";
\r
32 public static DownloadResponse parse( InputStream in ) {
\r
34 final DownloadResponse ret = new DownloadResponse();
\r
35 final MotorBurnFile currentMotor = new MotorBurnFile();
\r
37 // Have a place to put the data string and format.
\r
38 // We hold on to these here, then push them into the currentMotor
\r
39 // only if it a supported filetype
\r
40 final StringHolder current_format = new StringHolder();
\r
41 final StringHolder current_data = new StringHolder();
\r
43 RootElement rootEl = new RootElement(thrustcurveURI, root_tag);
\r
45 rootEl.setStartElementListener(
\r
46 new StartElementListener() {
\r
47 public void start(Attributes arg0) {
\r
48 Log.d(TAG,"Start Element error");
\r
49 ret.setError("IsError");
\r
54 Element resultsEl = rootEl.getChild( thrustcurveURI, results_tag);
\r
55 Element resultEl = resultsEl.getChild( thrustcurveURI, result_tag);
\r
56 resultEl.setStartElementListener(
\r
57 new StartElementListener() {
\r
59 public void start(Attributes arg0) {
\r
60 Log.d(TAG,"Start Element result");
\r
61 currentMotor.init();
\r
66 resultEl.setEndElementListener(
\r
67 new EndElementListener() {
\r
70 if ( SupportedFileTypes.isSupportedFileType(current_format.s) ) {
\r
71 currentMotor.setFiletype(current_format.s);
\r
74 s = Base64Decoder.decodeData(current_data.s);
\r
75 } catch ( Exception ex ) {
\r
76 Log.d(TAG,"base64: " + ex.getMessage());
\r
78 currentMotor.decodeFile( s );
\r
80 ret.add((MotorBurnFile)currentMotor.clone());
\r
85 resultEl.getChild(thrustcurveURI,motor_id_tag).setEndTextElementListener(
\r
86 new EndTextElementListener() {
\r
88 public void end(String arg0) {
\r
89 currentMotor.setMotorId(Integer.parseInt(arg0));
\r
93 resultEl.getChild(thrustcurveURI,format_tag).setEndTextElementListener(
\r
94 new EndTextElementListener() {
\r
96 public void end(String arg0) {
\r
97 current_format.s = arg0;
\r
101 resultEl.getChild(thrustcurveURI,data_tag).setEndTextElementListener(
\r
102 new EndTextElementListener() {
\r
104 public void end(String arg0) {
\r
105 current_data.s = arg0;
\r
110 Xml.parse(in, Xml.Encoding.UTF_8, rootEl.getContentHandler());
\r
111 } catch (Exception e) {
\r
112 throw new RuntimeException(e);
\r
118 private static class StringHolder {
\r