1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.indi.server;
20
21 import java.util.Queue;
22
23 import org.indi.clientmessages.EnableBlob;
24 import org.indi.clientmessages.GetProperties;
25 import org.indi.objects.Blob;
26 import org.indi.objects.BlobVector;
27 import org.indi.objects.Number;
28 import org.indi.objects.NumberVector;
29 import org.indi.objects.Switch;
30 import org.indi.objects.SwitchVector;
31 import org.indi.objects.Text;
32 import org.indi.objects.TextVector;
33 import org.xml.sax.Attributes;
34 import org.xml.sax.SAXException;
35 import org.xml.sax.helpers.DefaultHandler;
36
37
38
39
40
41
42 public class SaxHandler extends DefaultHandler {
43
44
45
46
47 private SwitchVector sv = null;
48
49
50
51
52 private NumberVector nv = null;
53
54
55
56
57 private TextVector tv = null;
58
59
60
61
62 private BlobVector bv = null;
63
64
65
66 private String name = null;
67
68
69
70 private String value = null;
71
72
73
74 private String blobformat = null;
75
76
77
78 private String device = null;
79
80
81
82 private long blobsize = 0;
83
84
85
86 private final Queue<java.lang.Object> queue;
87
88
89
90 private ClientHandler clientHandler = null;
91
92
93
94
95
96
97
98
99
100 public SaxHandler(Queue<java.lang.Object> queue, ClientHandler clientHandler) {
101 this.queue = queue;
102 this.clientHandler = clientHandler;
103 }
104
105 @Override
106 public void startElement(String uri, String localName, String qName,
107 Attributes attrs) throws SAXException {
108 if (qName.equals("newSwitchVector")) {
109 if ((this.sv != null) || (this.nv != null) || (this.tv != null)
110 || (this.bv != null)) {
111 throw new RuntimeException();
112 }
113 this.sv = new SwitchVector(attrs.getValue("device"), attrs
114 .getValue("name"), attrs.getValue("timestamp"));
115 }
116 if (qName.equals("newNumberVector")) {
117 if ((this.sv != null) || (this.nv != null) || (this.tv != null)
118 || (this.bv != null)) {
119 throw new RuntimeException();
120 }
121 this.nv = new NumberVector(attrs.getValue("device"), attrs
122 .getValue("name"), attrs.getValue("timestamp"));
123 }
124 if (qName.equals("newTextVector")) {
125 if ((this.sv != null) || (this.nv != null) || (this.tv != null)
126 || (this.bv != null)) {
127 throw new RuntimeException();
128 }
129 this.tv = new TextVector(attrs.getValue("device"), attrs
130 .getValue("name"), attrs.getValue("timestamp"));
131 }
132 if (qName.equals("newBLOBVector")) {
133 if ((this.sv != null) || (this.nv != null) || (this.tv != null)
134 || (this.bv != null)) {
135 throw new RuntimeException();
136 }
137 this.bv = new BlobVector(attrs.getValue("device"), attrs
138 .getValue("name"), attrs.getValue("timestamp"));
139 }
140 if (qName.equals("oneText")) {
141 if ((this.sv != null) || (this.nv != null) || (this.tv == null)
142 || (this.bv != null)) {
143 throw new RuntimeException();
144 }
145 this.name = attrs.getValue("name");
146 this.value = new String();
147 }
148 if (qName.equals("oneSwitch")) {
149 if ((this.sv == null) || (this.nv != null) || (this.tv != null)
150 || (this.bv != null)) {
151 throw new RuntimeException();
152 }
153 this.name = attrs.getValue("name");
154 this.value = new String();
155 }
156 if (qName.equals("oneNumber")) {
157 if ((this.sv != null) || (this.nv == null) || (this.tv != null)
158 || (this.bv != null)) {
159 throw new RuntimeException();
160 }
161 this.name = attrs.getValue("name");
162 this.value = new String();
163 }
164 if (qName.equals("oneBLOB")) {
165 if ((this.sv != null) || (this.nv != null) || (this.tv != null)
166 || (this.bv == null)) {
167 throw new RuntimeException();
168 }
169 this.name = attrs.getValue("name");
170 this.blobsize = Long.valueOf(attrs.getValue("size"));
171 this.blobformat = attrs.getValue("format");
172 this.value = new String();
173 }
174 if (qName.equals("enableBLOB")) {
175 if ((this.sv != null) || (this.nv != null) || (this.tv != null)
176 || (this.bv != null)) {
177 throw new RuntimeException();
178 }
179 this.name = attrs.getValue("name");
180 this.device = attrs.getValue("device");
181 this.value = new String();
182 }
183 if (qName.equals("getProperties")) {
184 if ((this.sv != null) || (this.nv != null) || (this.tv != null)
185 || (this.bv != null)) {
186 throw new RuntimeException();
187 }
188 GetProperties g = new GetProperties(attrs.getValue("version"),
189 attrs.getValue("device"));
190 this.clientHandler.onGetProperties(g);
191 this.queue.add(g);
192 }
193 }
194
195 @Override
196 public void endElement(String uri, String localName, String qName)
197 throws SAXException {
198 if (qName.equals("oneText")) {
199 if ((this.sv != null) || (this.nv != null) || (this.tv == null)
200 || (this.bv != null)) {
201 throw new RuntimeException();
202 }
203 this.tv.add(new Text(this.name, this.value));
204 }
205 if (qName.equals("oneSwitch")) {
206 if ((this.sv == null) || (this.nv != null) || (this.tv != null)
207 || (this.bv != null)) {
208 throw new RuntimeException();
209 }
210 this.sv.add(new Switch(this.name, normalize(this.value)));
211 }
212 if (qName.equals("oneNumber")) {
213 if ((this.sv != null) || (this.nv == null) || (this.tv != null)
214 || (this.bv != null)) {
215 throw new RuntimeException();
216 }
217 this.nv.add(new Number(this.name, normalize(this.value)));
218 }
219 if (qName.equals("oneBLOB")) {
220 if ((this.sv != null) || (this.nv != null) || (this.tv != null)
221 || (this.bv == null)) {
222 throw new RuntimeException();
223 }
224 this.bv.add(new Blob(this.name, this.blobformat, this.blobsize,
225 this.value));
226 }
227 if (qName.equals("newTextVector")) {
228 if ((this.sv != null) || (this.nv != null) || (this.tv == null)
229 || (this.bv != null)) {
230 throw new RuntimeException();
231 }
232 this.queue.add(this.tv);
233 this.tv = null;
234 }
235 if (qName.equals("enableBLOB")) {
236 if ((this.sv != null) || (this.nv != null) || (this.tv != null)
237 || (this.bv != null)) {
238 throw new RuntimeException();
239 }
240
241 this.clientHandler.onEnableBlob(new EnableBlob(this.device,
242 this.name, normalize(this.value)));
243 this.value = null;
244 this.device = null;
245 this.name = null;
246 }
247 if (qName.equals("newSwitchVector")) {
248 if ((this.sv == null) || (this.nv != null) || (this.tv != null)
249 || (this.bv != null)) {
250 throw new RuntimeException();
251 }
252 ;
253 this.queue.add(this.sv);
254 this.sv = null;
255 }
256 if (qName.equals("newNumberVector")) {
257 if ((this.sv != null) || (this.nv == null) || (this.tv != null)
258 || (this.bv != null)) {
259 throw new RuntimeException();
260 }
261 this.queue.add(this.nv);
262 this.nv = null;
263 }
264 if (qName.equals("newBLOBVector")) {
265 if ((this.sv != null) || (this.nv != null) || (this.tv != null)
266 || (this.bv == null)) {
267 throw new RuntimeException();
268 }
269 this.queue.add(this.bv);
270 this.bv = null;
271 }
272 }
273
274 @Override
275 public void characters(char ch[], int start, int length)
276 throws SAXException {
277
278
279 String v = this.value + new String(ch, start, length);
280 this.value = this.value + v;
281 }
282
283 private String normalize(String in) {
284 in = in.replaceAll("\n", "");
285 String[] str = in.split(" ");
286 if (str.length == 1) {
287 return in;
288 }
289 String out = new String();
290 out = str[0];
291 for (int i = 1; i < str.length; i++) {
292 if (!out.equals("")) {
293 if (!((str[i].equals("")) && (i == str.length - 1))) {
294 out = out + " " + str[i];
295 }
296 } else {
297 out = str[i];
298 }
299 }
300 return out;
301 }
302 }