View Javadoc
1   /*
2    * Copyright (c) 2013-2014, 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.gml.Point;
13  import nl.mineleni.openls.databinding.gml.Pos;
14  import nl.mineleni.openls.databinding.openls.Position;
15  import nl.mineleni.openls.databinding.openls.ReverseGeocodePreference;
16  import nl.mineleni.openls.databinding.openls.ReverseGeocodeRequest;
17  
18  import org.slf4j.Logger;
19  import org.slf4j.LoggerFactory;
20  import org.xml.sax.Attributes;
21  import org.xml.sax.InputSource;
22  import org.xml.sax.SAXException;
23  
24  /**
25   * Parse reverse geocode requests.
26   *
27   * @author prinsmc
28   *
29   */
30  public class OpenLSReverseGeocodeRequestParser extends AbstractOpenLSParser {
31  	/** logger. */
32  	private static final Logger LOGGER = LoggerFactory
33  	        .getLogger(OpenLSReverseGeocodeRequestParser.class);
34  
35  	/*
36  	 * (non-Javadoc)
37  	 *
38  	 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
39  	 * java.lang.String, java.lang.String)
40  	 */
41  	@Override
42  	public void endElement(final String uri, final String localName,
43  	        final String qName) throws SAXException {
44  		final String[] nsName = qName.split(":");
45  		String eName = "";
46  		if (nsName.length > 1) {
47  			eName = nsName[1];
48  		} else {
49  			eName = nsName[0];
50  		}
51  
52  		switch (eName.toLowerCase()) {
53  		case "reversegeocoderequest":
54  			break;
55  		case "position":
56  			final Position position = (Position) (this.objStack.pop());
57  			if (this.objStack.peek().getClass() == ReverseGeocodeRequest.class) {
58  				((ReverseGeocodeRequest) (this.objStack.peek()))
59  				        .setPosition(position);
60  			}
61  			break;
62  		case "point":
63  			final Point point = (Point) (this.objStack.pop());
64  			if (this.objStack.peek().getClass() == Position.class) {
65  				((Position) (this.objStack.peek())).setPoint(point);
66  			}
67  			break;
68  		case "pos":
69  			final Pos pos = (Pos) (this.objStack.pop());
70  			pos.setXY(this.eValBuf.toString());
71  			if (this.objStack.peek().getClass() == Point.class) {
72  				((Point) (this.objStack.peek())).addPos(pos);
73  			}
74  			break;
75  		case "reversegeocodepreference":
76  			final ReverseGeocodePreference rcp = (ReverseGeocodePreference) (this.objStack
77  			        .pop());
78  			rcp.setPreference(this.eValBuf.toString());
79  			if (this.objStack.peek().getClass() == ReverseGeocodeRequest.class) {
80  				((ReverseGeocodeRequest) (this.objStack.peek()))
81  				        .setReverseGeocodePreference(rcp);
82  			}
83  			break;
84  		default:
85  			break;
86  		}
87  
88  	}
89  
90  	/**
91  	 * Gets the reverse geocode request.
92  	 *
93  	 * @return the reverse geocode request
94  	 */
95  	public ReverseGeocodeRequest getReverseGeocodeRequest() {
96  		ReverseGeocodeRequest geocodeRequest = null;
97  		if ((this.objStack.firstElement() != null)
98  		        && (this.objStack.firstElement().getClass() == ReverseGeocodeRequest.class)) {
99  			geocodeRequest = (ReverseGeocodeRequest) this.objStack
100 			        .firstElement();
101 		}
102 		return geocodeRequest;
103 	}
104 
105 	/**
106 	 * Parses the open ls reverse geocode response.
107 	 *
108 	 * @param data
109 	 *            the data which is an OpenLS response xml document
110 	 * @return the geocode response object, will return null if parsing the data
111 	 *         failed
112 	 */
113 	public ReverseGeocodeRequest parseOpenLSReverseGeocodeRequest(
114 	        final String data) {
115 		this.objStack.clear();
116 		try {
117 			this.parser.parse(new InputSource(new StringReader(data)), this);
118 		} catch (final SAXException | IOException e) {
119 			LOGGER.error("OpenLS request XML verwerken is mislukt: " + data
120 			        + ": ", e);
121 		}
122 		return this.getReverseGeocodeRequest();
123 	}
124 
125 	/*
126 	 * (non-Javadoc)
127 	 *
128 	 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
129 	 * java.lang.String, java.lang.String, org.xml.sax.Attributes)
130 	 */
131 	@Override
132 	public void startElement(final String uri, final String localName,
133 	        final String qName, final Attributes attributes)
134 	        throws SAXException {
135 		this.eValBuf = new StringBuffer();
136 		final String[] nsName = qName.split(":");
137 		String eName = nsName[0];
138 		if (nsName.length > 1) {
139 			eName = nsName[1];
140 		}
141 
142 		switch (eName.toLowerCase()) {
143 		case "reversegeocoderequest":
144 			this.objStack.push(new ReverseGeocodeRequest());
145 			break;
146 		case "position":
147 			this.objStack.push(new Position());
148 			break;
149 		case "point":
150 			final Point point = new Point();
151 			for (int i = 0; i < attributes.getLength(); i++) {
152 				final String key = attributes.getQName(i);
153 				final String value = attributes.getValue(i);
154 				if (key.equalsIgnoreCase("srsName")) {
155 					point.setSrsName(value);
156 				}
157 			}
158 			this.objStack.push(point);
159 			break;
160 		case "pos":
161 			final Pos pos = new Pos();
162 			for (int i = 0; i < attributes.getLength(); i++) {
163 				final String key = attributes.getQName(i);
164 				final String value = attributes.getValue(i);
165 				if (key.equalsIgnoreCase("dimension")) {
166 					pos.setDimension(Integer.parseInt(value));
167 				}
168 			}
169 			this.objStack.push(pos);
170 			break;
171 		case "reversegeocodepreference":
172 			this.objStack.push(new ReverseGeocodePreference());
173 			break;
174 		default:
175 			LOGGER.warn("Onbekend element '" + eName + "' wordt genegeerd.");
176 			break;
177 		}
178 	}
179 }