1 package nl.geozet.openls.parser;
2
3 import java.io.IOException;
4 import java.io.StringReader;
5 import java.util.Stack;
6
7 import javax.xml.parsers.ParserConfigurationException;
8 import javax.xml.parsers.SAXParser;
9 import javax.xml.parsers.SAXParserFactory;
10
11 import nl.geozet.openls.databinding.gml.Point;
12 import nl.geozet.openls.databinding.gml.Pos;
13 import nl.geozet.openls.databinding.openls.Address;
14 import nl.geozet.openls.databinding.openls.Building;
15 import nl.geozet.openls.databinding.openls.GeocodeResponse;
16 import nl.geozet.openls.databinding.openls.GeocodeResponseList;
17 import nl.geozet.openls.databinding.openls.GeocodedAddress;
18 import nl.geozet.openls.databinding.openls.Place;
19 import nl.geozet.openls.databinding.openls.PostalCode;
20 import nl.geozet.openls.databinding.openls.Street;
21 import nl.geozet.openls.databinding.openls.StreetAddress;
22
23 import org.apache.log4j.Logger;
24 import org.xml.sax.Attributes;
25 import org.xml.sax.InputSource;
26 import org.xml.sax.SAXException;
27 import org.xml.sax.helpers.DefaultHandler;
28
29
30
31
32 public class OpenLSResponseParser extends DefaultHandler {
33
34
35 private static final Logger LOGGER = Logger
36 .getLogger(OpenLSResponseParser.class);
37
38
39 private SAXParser parser;
40
41
42 private final Stack objStack = new Stack();
43
44
45 private StringBuffer eValBuf;
46
47
48
49
50 public OpenLSResponseParser() {
51 SAXParserFactory factory = SAXParserFactory.newInstance();
52 factory.setValidating(false);
53 try {
54 parser = factory.newSAXParser();
55 } catch (ParserConfigurationException e) {
56 LOGGER.fatal("Configureren van de saxparser is mislukt: ", e);
57 } catch (SAXException e) {
58 LOGGER.error("Maken van de saxparser is mislukt: ", e);
59 }
60 }
61
62
63
64
65
66
67
68
69
70 public GeocodeResponse parseOpenLSResponse(String data) {
71 objStack.clear();
72 try {
73 parser.parse(new InputSource(new StringReader(data)), this);
74 } catch (SAXException e) {
75 LOGGER.error("OpenLS response XML verwerken is mislukt: " + data
76 + ": ", e);
77 } catch (IOException e) {
78 LOGGER.error("OpenLS response XML lezen is mislukt: ", e);
79 }
80 return getGeocodeResponse();
81 }
82
83
84
85
86
87
88
89 @Override
90 public void startElement(String uri, String localName, String qName,
91 Attributes attributes) {
92 eValBuf = new StringBuffer();
93 String[] nsName = qName.split(":");
94 String eName;
95 if (nsName.length > 1) {
96 eName = nsName[1];
97 } else {
98 eName = nsName[0];
99 }
100 if (eName.equalsIgnoreCase("GeocodeResponse")) {
101 GeocodeResponses/GeocodeResponse.html#GeocodeResponse">GeocodeResponse obj = new GeocodeResponse();
102 objStack.push(obj);
103 } else if (eName.equalsIgnoreCase("GeocodeResponseList")) {
104 GeocodeResponseListocodeResponseList.html#GeocodeResponseList">GeocodeResponseList obj = new GeocodeResponseList();
105 objStack.push(obj);
106 for (int i = 0; i < attributes.getLength(); i++) {
107 String key = attributes.getQName(i);
108 String value = attributes.getValue(i);
109 if (key.equalsIgnoreCase("numberOfGeocodedAddresses")) {
110 int val = Integer.parseInt(value);
111 obj.setNumberOfGeocodedAddresses(val);
112 }
113 }
114 } else if (eName.equalsIgnoreCase("GeocodedAddress")) {
115 GeocodedAddresss/GeocodedAddress.html#GeocodedAddress">GeocodedAddress obj = new GeocodedAddress();
116 objStack.push(obj);
117 } else if (eName.equalsIgnoreCase("Point")) {
118 Pointding/gml/Point.html#Point">Point obj = new Point();
119 objStack.push(obj);
120 for (int i = 0; i < attributes.getLength(); i++) {
121 String key = attributes.getQName(i);
122 String value = attributes.getValue(i);
123 if (key.equalsIgnoreCase("srsName")) {
124 obj.setSrsName(value);
125 }
126 }
127 } else if (eName.equalsIgnoreCase("pos")) {
128 Posinding/gml/Pos.html#Pos">Pos obj = new Pos();
129 objStack.push(obj);
130 for (int i = 0; i < attributes.getLength(); i++) {
131 String key = attributes.getQName(i);
132 String value = attributes.getValue(i);
133 if (key.equalsIgnoreCase("dimension")) {
134 obj.setDimension(Integer.parseInt(value));
135 }
136 }
137 } else if (eName.equalsIgnoreCase("Address")) {
138 Addressng/openls/Address.html#Address">Address obj = new Address();
139 objStack.push(obj);
140 for (int i = 0; i < attributes.getLength(); i++) {
141 String key = attributes.getQName(i);
142 String value = attributes.getValue(i);
143 if (key.equalsIgnoreCase("countryCode")) {
144 obj.setCountryCode(value);
145 }
146 }
147 } else if (eName.equalsIgnoreCase("StreetAddress")) {
148 StreetAddressnls/StreetAddress.html#StreetAddress">StreetAddress obj = new StreetAddress();
149 objStack.push(obj);
150 } else if (eName.equalsIgnoreCase("Building")) {
151 Buildingg/openls/Building.html#Building">Building obj = new Building();
152 objStack.push(obj);
153 for (int i = 0; i < attributes.getLength(); i++) {
154 String key = attributes.getQName(i);
155 String value = attributes.getValue(i);
156 if (key.equalsIgnoreCase("number")) {
157 obj.setNumber(value);
158 }
159 }
160 } else if (eName.equalsIgnoreCase("Street")) {
161 Streeting/openls/Street.html#Street">Street obj = new Street();
162 objStack.push(obj);
163 } else if (eName.equalsIgnoreCase("Place")) {
164 Placeding/openls/Place.html#Place">Place obj = new Place();
165 objStack.push(obj);
166 for (int i = 0; i < attributes.getLength(); i++) {
167 String key = attributes.getQName(i);
168 String value = attributes.getValue(i);
169 if (key.equalsIgnoreCase("type")) {
170 obj.setType(value);
171 }
172 }
173 } else if (eName.equalsIgnoreCase("PostalCode")) {
174 PostalCodeopenls/PostalCode.html#PostalCode">PostalCode obj = new PostalCode();
175 objStack.push(obj);
176 }
177 }
178
179
180
181
182
183
184
185 @Override
186 public void endElement(String uri, String localName, String qName) {
187 String[] nsName = qName.split(":");
188 String eName;
189 if (nsName.length > 1) {
190 eName = nsName[1];
191 } else {
192 eName = nsName[0];
193 }
194 if (eName.equalsIgnoreCase("GeocodeResponseList")) {
195 GeocodeResponseList./nl/geozet/openls/databinding/openls/GeocodeResponseList.html#GeocodeResponseList">GeocodeResponseList obj = (GeocodeResponseList) (objStack.pop());
196 if (objStack.peek().getClass() == GeocodeResponse.class) {
197 ((GeocodeResponse) (objStack.peek()))
198 .addGeocodeResponseList(obj);
199 }
200 } else if (eName.equalsIgnoreCase("GeocodedAddress")) {
201 GeocodedAddress../../nl/geozet/openls/databinding/openls/GeocodedAddress.html#GeocodedAddress">GeocodedAddress obj = (GeocodedAddress) (objStack.pop());
202 if (objStack.peek().getClass() == GeocodeResponseList.class) {
203 ((GeocodeResponseList) (objStack.peek()))
204 .addGeocodedAddress(obj);
205 }
206 } else if (eName.equalsIgnoreCase("Point")) {
207 Pointef="../../../../nl/geozet/openls/databinding/gml/Point.html#Point">Point obj = (Point) (objStack.pop());
208 if (objStack.peek().getClass() == GeocodedAddress.class) {
209 ((GeocodedAddress) (objStack.peek())).setPoint(obj);
210 }
211 } else if (eName.equalsIgnoreCase("pos")) {
212 Poshref="../../../../nl/geozet/openls/databinding/gml/Pos.html#Pos">Pos obj = (Pos) (objStack.pop());
213 obj.setXY(eValBuf.toString());
214 if (objStack.peek().getClass() == Point.class) {
215 ((Point) (objStack.peek())).addPos(obj);
216 }
217 } else if (eName.equalsIgnoreCase("Address")) {
218 Address="../../../../nl/geozet/openls/databinding/openls/Address.html#Address">Address obj = (Address) (objStack.pop());
219 if (objStack.peek().getClass() == GeocodedAddress.class) {
220 ((GeocodedAddress) (objStack.peek())).setAddress(obj);
221 }
222 } else if (eName.equalsIgnoreCase("StreetAddress")) {
223 StreetAddress./../../nl/geozet/openls/databinding/openls/StreetAddress.html#StreetAddress">StreetAddress obj = (StreetAddress) (objStack.pop());
224 if (objStack.peek().getClass() == Address.class) {
225 ((Address) (objStack.peek())).setStreetAddress(obj);
226 }
227 } else if (eName.equalsIgnoreCase("Building")) {
228 Building"../../../../nl/geozet/openls/databinding/openls/Building.html#Building">Building obj = (Building) (objStack.pop());
229 if (objStack.peek().getClass() == StreetAddress.class) {
230 ((StreetAddress) (objStack.peek())).setBuilding(obj);
231 }
232 } else if (eName.equalsIgnoreCase("Street")) {
233 Streetf="../../../../nl/geozet/openls/databinding/openls/Street.html#Street">Street obj = (Street) (objStack.pop());
234 obj.setStreet(eValBuf.toString());
235 if (objStack.peek().getClass() == StreetAddress.class) {
236 ((StreetAddress) (objStack.peek())).setStreet(obj);
237 }
238 } else if (eName.equalsIgnoreCase("Place")) {
239 Placeef="../../../../nl/geozet/openls/databinding/openls/Place.html#Place">Place obj = (Place) (objStack.pop());
240 obj.setPlace(eValBuf.toString());
241 if (objStack.peek().getClass() == Address.class) {
242 ((Address) (objStack.peek())).addPlace(obj);
243 }
244 } else if (eName.equalsIgnoreCase("PostalCode")) {
245 PostalCode./../../../nl/geozet/openls/databinding/openls/PostalCode.html#PostalCode">PostalCode obj = (PostalCode) (objStack.pop());
246 obj.setPostalCode(eValBuf.toString());
247 if (objStack.peek().getClass() == Address.class) {
248 ((Address) (objStack.peek())).setPostalCode(obj);
249 }
250 }
251 }
252
253
254
255
256
257
258 public void characters(char[] ch, int start, int length) {
259 eValBuf.append(ch, start, length);
260 }
261
262
263
264
265
266
267 public GeocodeResponse getGeocodeResponse() {
268 GeocodeResponse geocodeResponse = null;
269 if (objStack.firstElement() != null) {
270 if (objStack.firstElement().getClass() == GeocodeResponse.class) {
271 geocodeResponse = (GeocodeResponse) objStack.firstElement();
272 }
273 }
274 return geocodeResponse;
275 }
276
277 }