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