82997829f3fc3bd0f76781711c8b2f5b34475d9a
[debian/openrocket] / android / src / net / sf / openrocket / android / thrustcurve / DownloadResponseParser.java
1 package net.sf.openrocket.android.thrustcurve;\r
2 \r
3 import java.io.InputStream;\r
4 \r
5 import org.xml.sax.Attributes;\r
6 \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
14 \r
15 public class DownloadResponseParser {\r
16 \r
17         private static final String TAG = "DownloadResponseParser";\r
18 \r
19         private static final String thrustcurveURI = "http://www.thrustcurve.org/2009/DownloadResponse";\r
20 \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
31 \r
32         public static DownloadResponse parse( InputStream in ) {\r
33 \r
34                 final DownloadResponse ret = new DownloadResponse();\r
35                 final MotorBurnFile currentMotor = new MotorBurnFile();\r
36 \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
42 \r
43                 RootElement rootEl = new RootElement(thrustcurveURI, root_tag);\r
44                 /*\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
50                                         }\r
51                                 }\r
52                                 );\r
53                                 */\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
58                                         @Override\r
59                                         public void start(Attributes arg0) {\r
60                                                 Log.d(TAG,"Start Element result");\r
61                                                 currentMotor.init();\r
62                                         }\r
63                                 }\r
64                                 );\r
65 \r
66                 resultEl.setEndElementListener(\r
67                                 new EndElementListener() {\r
68                                         @Override\r
69                                         public void end() {\r
70                                                 if ( SupportedFileTypes.isSupportedFileType(current_format.s) ) {\r
71                                                         currentMotor.setFiletype(current_format.s);\r
72                                                         String s = null;\r
73                                                         try {\r
74                                                                 s = Base64Decoder.decodeData(current_data.s);\r
75                                                         } catch ( Exception ex ) {\r
76                                                                 Log.d(TAG,"base64: " + ex.getMessage());\r
77                                                         }\r
78                                                         currentMotor.decodeFile( s );\r
79                                                 }\r
80                                                 ret.add((MotorBurnFile)currentMotor.clone());\r
81                                         }\r
82                                 }\r
83                                 );\r
84 \r
85                 resultEl.getChild(thrustcurveURI,motor_id_tag).setEndTextElementListener(\r
86                                 new EndTextElementListener() {\r
87                                         @Override\r
88                                         public void end(String arg0) {\r
89                                                 currentMotor.setMotorId(Integer.parseInt(arg0));\r
90                                         }\r
91                                 }\r
92                                 );\r
93                 resultEl.getChild(thrustcurveURI,format_tag).setEndTextElementListener(\r
94                                 new EndTextElementListener() {\r
95                                         @Override\r
96                                         public void end(String arg0) {\r
97                                                 current_format.s = arg0;\r
98                                         }\r
99                                 }\r
100                                 );\r
101                 resultEl.getChild(thrustcurveURI,data_tag).setEndTextElementListener(\r
102                                 new EndTextElementListener() {\r
103                                         @Override\r
104                                         public void end(String arg0) {\r
105                                                 current_data.s = arg0;\r
106                                         }\r
107                                 }\r
108                                 );\r
109                 try {\r
110                         Xml.parse(in, Xml.Encoding.UTF_8,  rootEl.getContentHandler());\r
111                 } catch (Exception e) {\r
112                         throw new RuntimeException(e);\r
113                 }\r
114 \r
115                 return ret;\r
116         }\r
117 \r
118         private static class StringHolder {\r
119                 public String s;\r
120         }\r
121 \r
122 }