View Javadoc

1   /*
2   Copyright (C) 2007 Dirk Huenniger
3   
4   This library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Lesser General Public
6   License as published by the Free Software Foundation; either
7   version 2.1 of the License, or (at your option) any later version.
8   
9   This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  Lesser General Public License for more details.
13  
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
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   * A sax handler to parse the information received from the client
39   * 
40   * @author Dirk Hünniger
41   */
42  public class SaxHandler extends DefaultHandler {
43      /**
44       * the SwitchVector the parser is currently passing. if null the parser is
45       * currently not parsing any SwitchVector
46       */
47      private SwitchVector sv = null;
48      /**
49       * the NumberVector the parser is currently passing. if null the parser is
50       * currently not parsing any NumberVector
51       */
52      private NumberVector nv = null;
53      /**
54       * the TextVector the parser is currently passing. if null the parser is
55       * currently not parsing any TextVector
56       */
57      private TextVector tv = null;
58      /**
59       * the BLOBVector the parser is currently passing. if null the parser is
60       * currently not parsing any BLOBVector
61       */
62      private BlobVector bv = null;
63      /**
64       * the name attibute of the XMLelement currently beening parsed
65       */
66      private String name = null;
67      /**
68       * the value of the indiobject.Element currently beeing parsed
69       */
70      private String value = null;
71      /**
72       * the format attribute of the BLOB currently beeing parsed
73       */
74      private String blobformat = null;
75      /**
76       * the name of the device the vector currently beeing parsed belongs to
77       */
78      private String device = null;
79      /**
80       * the size of the blob currently beeing parsed
81       */
82      private long blobsize = 0;
83      /**
84       * the queue to write parsed objects to
85       */
86      private final Queue<java.lang.Object> queue;
87      /**
88       * the clienthandker this sax parser belongs to
89       */
90      private ClientHandler clientHandler = null;
91  
92      /**
93       * class constructor
94       * 
95       * @param queue
96       *                the queue to write the parsed elements to
97       * @param clientHandler
98       *                the clienthandler this saxparser belongs to
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 	// if ((sv==null) && (nv==null) && (tv==null) && (bv==null) &&
278 	// (device==null)) return;
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 }