View Javadoc
1   /*
2    * Copyright (c) 2013, Dienst Landelijk Gebied - Ministerie van Economische Zaken
3    *
4    * Gepubliceerd onder de BSD 2-clause licentie,
5    * zie https://github.com/MinELenI/CBSviewer/blob/master/LICENSE.md voor de volledige licentie.
6    */
7   package nl.mineleni.openls.parser;
8   
9   import java.io.IOException;
10  import java.io.StringReader;
11  
12  import nl.mineleni.openls.databinding.openls.Address;
13  import nl.mineleni.openls.databinding.openls.Building;
14  import nl.mineleni.openls.databinding.openls.GeocodeRequest;
15  import nl.mineleni.openls.databinding.openls.Place;
16  import nl.mineleni.openls.databinding.openls.PostalCode;
17  import nl.mineleni.openls.databinding.openls.Street;
18  import nl.mineleni.openls.databinding.openls.StreetAddress;
19  
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  import org.xml.sax.Attributes;
23  import org.xml.sax.InputSource;
24  import org.xml.sax.SAXException;
25  
26  /**
27   * The Class OpenLSGeocodeRequestParser.
28   *
29   * @since 1.7
30   */
31  public class OpenLSGeocodeRequestParser extends AbstractOpenLSParser {
32  
33  	/** logger. */
34  	private static final Logger LOGGER = LoggerFactory
35  	        .getLogger(OpenLSGeocodeRequestParser.class);
36  
37  	/*
38  	 * (non-Javadoc)
39  	 *
40  	 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
41  	 * java.lang.String, java.lang.String)
42  	 */
43  	@Override
44  	public void endElement(final String uri, final String localName,
45  	        final String qName) throws SAXException {
46  		final String[] nsName = qName.split(":");
47  		String eName = "";
48  		if (nsName.length > 1) {
49  			eName = nsName[1];
50  		} else {
51  			eName = nsName[0];
52  		}
53  		switch (eName.toLowerCase()) {
54  		case "address":
55  			final Address address = (Address) (this.objStack.pop());
56  			if (this.objStack.peek().getClass() == GeocodeRequest.class) {
57  				((GeocodeRequest) (this.objStack.peek())).addAddress(address);
58  			}
59  			break;
60  		case "streetaddress":
61  			final StreetAddress streetaddress = (StreetAddress) (this.objStack
62  			        .pop());
63  			if (this.objStack.peek().getClass() == Address.class) {
64  				((Address) (this.objStack.peek()))
65  				        .setStreetAddress(streetaddress);
66  			}
67  			break;
68  		case "building":
69  			final Building building = (Building) (this.objStack.pop());
70  			if (this.objStack.peek().getClass() == StreetAddress.class) {
71  				((StreetAddress) (this.objStack.peek())).setBuilding(building);
72  			}
73  			break;
74  		case "street":
75  			final Street street = (Street) (this.objStack.pop());
76  			street.setStreet(this.eValBuf.toString());
77  			if (this.objStack.peek().getClass() == StreetAddress.class) {
78  				((StreetAddress) (this.objStack.peek())).setStreet(street);
79  			}
80  			break;
81  		case "place":
82  			final Place place = (Place) (this.objStack.pop());
83  			place.setPlace(this.eValBuf.toString());
84  			if (this.objStack.peek().getClass() == Address.class) {
85  				((Address) (this.objStack.peek())).addPlace(place);
86  			}
87  			break;
88  		case "postalcode":
89  			final PostalCode pc = (PostalCode) (this.objStack.pop());
90  			pc.setPostalCode(this.eValBuf.toString());
91  			if (this.objStack.peek().getClass() == Address.class) {
92  				((Address) (this.objStack.peek())).setPostalCode(pc);
93  			}
94  			break;
95  		default:
96  			return;
97  		}
98  	}
99  
100 	/**
101 	 * Gets the geocode request.
102 	 *
103 	 * @return the geocode request
104 	 */
105 	public GeocodeRequest getGeocodeRequest() {
106 		GeocodeRequest geocodeRequest = null;
107 		if ((this.objStack.firstElement() != null)
108 		        && (this.objStack.firstElement().getClass() == GeocodeRequest.class)) {
109 			geocodeRequest = (GeocodeRequest) this.objStack.firstElement();
110 		}
111 		return geocodeRequest;
112 	}
113 
114 	/**
115 	 * Parses the open ls request.
116 	 *
117 	 * @param data
118 	 *            the data
119 	 * @return the geocode request
120 	 */
121 	public GeocodeRequest parseOpenLSRequest(final String data) {
122 		this.objStack.clear();
123 		try {
124 			this.parser.parse(new InputSource(new StringReader(data)), this);
125 		} catch (final SAXException | IOException e) {
126 			LOGGER.error("OpenLS response XML verwerken is mislukt: " + data
127 			        + ": ", e);
128 		}
129 		return this.getGeocodeRequest();
130 	}
131 
132 	/*
133 	 * (non-Javadoc)
134 	 *
135 	 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
136 	 * java.lang.String, java.lang.String, org.xml.sax.Attributes)
137 	 */
138 	@Override
139 	public void startElement(final String uri, final String localName,
140 	        final String qName, final Attributes attributes)
141 	        throws SAXException {
142 		this.eValBuf = new StringBuffer();
143 		final String[] nsName = qName.split(":");
144 		String eName = nsName[0];
145 		if (nsName.length > 1) {
146 			eName = nsName[1];
147 		}
148 		switch (eName.toLowerCase()) {
149 		case "geocoderequest":
150 			this.objStack.push(new GeocodeRequest());
151 			break;
152 		case "address":
153 			final Address address = new Address();
154 			this.objStack.push(address);
155 			for (int i = 0; i < attributes.getLength(); i++) {
156 				final String key = attributes.getQName(i);
157 				final String value = attributes.getValue(i);
158 				if (key.equalsIgnoreCase("countryCode")) {
159 					address.setCountryCode(value);
160 				}
161 			}
162 			break;
163 		case "streetaddress":
164 			this.objStack.push(new StreetAddress());
165 			break;
166 		case "building":
167 			final Building building = new Building();
168 			this.objStack.push(building);
169 			for (int i = 0; i < attributes.getLength(); i++) {
170 				final String key = attributes.getQName(i);
171 				final String value = attributes.getValue(i);
172 				if (key.equalsIgnoreCase("number")) {
173 					building.setNumber(value);
174 				}
175 			}
176 			break;
177 		case "street":
178 			this.objStack.push(new Street());
179 			break;
180 		case "place":
181 			final Place place = new Place();
182 			this.objStack.push(place);
183 			for (int i = 0; i < attributes.getLength(); i++) {
184 				final String key = attributes.getQName(i);
185 				final String value = attributes.getValue(i);
186 				if (key.equalsIgnoreCase("type")) {
187 					place.setType(value);
188 				}
189 			}
190 			break;
191 		case "postalcode":
192 			this.objStack.push(new PostalCode());
193 			break;
194 		default:
195 			return;
196 		}
197 
198 	}
199 }