001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.vfny.geoserver.wcs.requests;
006:
007: import com.vividsolutions.jts.geom.Envelope;
008: import org.vfny.geoserver.wcs.servlets.WCService;
009: import org.xml.sax.Attributes;
010: import org.xml.sax.ContentHandler;
011: import org.xml.sax.SAXException;
012: import org.xml.sax.helpers.XMLFilterImpl;
013: import java.util.ArrayList;
014: import java.util.HashMap;
015: import java.util.logging.Level;
016: import java.util.logging.Logger;
017: import javax.servlet.http.HttpServletRequest;
018:
019: /**
020: * DOCUMENT ME!
021: *
022: * @author $Author: Alessio Fabiani (alessio.fabiani@gmail.com) $ (last modification)
023: * @author $Author: Simone Giannecchini (simboss1@gmail.com) $ (last modification)
024: */
025: public class CoverageHandler extends XMLFilterImpl implements
026: ContentHandler {
027: /** Class logger */
028: private static Logger LOGGER = org.geotools.util.logging.Logging
029: .getLogger("org.vfny.geoserver.requests.wcs");
030:
031: /** Service handling the request */
032: private WCService service = null;
033: private CoverageRequest request = null;
034: private String currentTag = new String();
035: private Double[] coordinates = new Double[4];
036: private Double[] lowers = new Double[2];
037: private Double[] highers = new Double[2];
038: private Double[] origin = new Double[2];
039: private Double[] offsetVector = new Double[2];
040: private boolean insideEnvelope = false;
041: private boolean insideGrid = false;
042: private boolean insideRange = false;
043: private int paramNum = -1;
044: private ArrayList paramNames = new ArrayList();
045: private HashMap params = new HashMap();
046: private int minTmp;
047:
048: /**
049: * Empty constructor.
050: */
051: public CoverageHandler(WCService service) {
052: super ();
053: this .service = service;
054: request = new CoverageRequest(service);
055: }
056:
057: public CoverageRequest getRequest(HttpServletRequest req) {
058: request.setHttpServletRequest(req);
059:
060: return request;
061: }
062:
063: /**
064: * Notes the start of the element and sets type names and query attributes.
065: *
066: * @param namespaceURI URI for namespace appended to element.
067: * @param localName Local name of element.
068: * @param rawName Raw name of element.
069: * @param atts Element attributes.
070: * @throws SAXException When the XML is not well formed.
071: */
072: public void startElement(String namespaceURI, String localName,
073: String rawName, Attributes atts) throws SAXException {
074: if (LOGGER.isLoggable(Level.FINEST)) {
075: LOGGER.finest("at start element: " + localName);
076: }
077:
078: // at start of element, set inside flag to whatever tag we are inside
079: currentTag = localName;
080:
081: if (currentTag.equals("GetCoverage")) {
082: final int length = atts.getLength();
083: String curAtt;
084:
085: for (int i = 0; i < length; i++) {
086: curAtt = atts.getLocalName(i);
087:
088: if (curAtt.equals("service")) {
089: request.setService(atts.getValue(i));
090: } else if (curAtt.equals("version")) {
091: request.setVersion(atts.getValue(i));
092: }
093: }
094: } else if (currentTag.equals("Envelope")) {
095: insideEnvelope = true;
096:
097: final int length = atts.getLength();
098: String curAtt;
099:
100: for (int i = 0; i < length; i++) {
101: curAtt = atts.getLocalName(i);
102:
103: if (curAtt.equals("srsName")) {
104: request.setCRS(atts.getValue(i));
105:
106: if (request.getResponseCRS() == null) {
107: request.setResponseCRS(atts.getValue(i));
108: }
109: }
110: }
111: } else if (currentTag.equals("Grid")
112: || currentTag.equals("RectifiedGrid")) {
113: insideGrid = true;
114:
115: final int length = atts.getLength();
116: String curAtt;
117:
118: for (int i = 0; i < length; i++) {
119: curAtt = atts.getLocalName(i);
120:
121: if (curAtt.equals("dimension")) {
122: request.setGridDimension(atts.getValue(i));
123: }
124: }
125: } else if (currentTag.equals("rangeSubset")) {
126: insideRange = true;
127: } else if (currentTag.equals("axisSubset") && insideRange) {
128: final int length = atts.getLength();
129: String curAtt;
130:
131: for (int i = 0; i < length; i++) {
132: curAtt = atts.getLocalName(i);
133:
134: if (curAtt.equals("name")) {
135: paramNames.add(atts.getValue(i));
136: paramNum++;
137: }
138: }
139: }
140: }
141:
142: /**
143: * Notes the end of the element exists query or bounding box.
144: *
145: * @param namespaceURI URI for namespace appended to element.
146: * @param localName Local name of element.
147: * @param rawName Raw name of element.
148: * @throws SAXException When the XML is not well formed.
149: */
150: public void endElement(String namespaceURI, String localName,
151: String rawName) throws SAXException {
152: LOGGER.finer("at end element: " + localName);
153: currentTag = localName;
154:
155: if (currentTag.equals("Envelope")) {
156: insideEnvelope = false;
157: } else if (currentTag.equals("Grid")
158: || currentTag.equals("RectifiedGrid")) {
159: insideGrid = false;
160: } else if (currentTag.equals("rangeSubset")) {
161: insideRange = false;
162: request.setParameters(params);
163: }
164:
165: currentTag = "";
166: }
167:
168: /**
169: * Checks if inside parsed element and adds its contents to the appropriate variable.
170: *
171: * @param ch URI for namespace appended to element.
172: * @param start Local name of element.
173: * @param length Raw name of element.
174: * @throws SAXException When the XML is not well formed.
175: */
176: public void characters(char[] ch, int start, int length)
177: throws SAXException {
178: String s = new String(ch, start, length);
179: s = s.trim();
180:
181: // if inside a property element, add the element
182: if (currentTag.equals("sourceCoverage")) {
183: if (LOGGER.isLoggable(Level.FINEST)) {
184: LOGGER.finest(new StringBuffer("found Coverage name: ")
185: .append(s).toString());
186: }
187:
188: request.setCoverage(s);
189: } else if (currentTag.equals("interpolationMethod")) {
190: if (LOGGER.isLoggable(Level.FINEST)) {
191: LOGGER.finest(new StringBuffer(
192: "found Interpolation Method: ").append(s)
193: .toString());
194: }
195:
196: request.setInterpolation(s);
197: } else if (currentTag.equals("crs")) {
198: if (LOGGER.isLoggable(Level.FINEST)) {
199: LOGGER.finest(new StringBuffer("found Output CRS: ")
200: .append(s).toString());
201: }
202:
203: request.setCRS(s);
204:
205: if (request.getResponseCRS() == null) {
206: request.setResponseCRS(s);
207: }
208: } else if (currentTag.equals("responseCrs")) {
209: if (LOGGER.isLoggable(Level.FINEST)) {
210: LOGGER.finest(new StringBuffer("found Output CRS: ")
211: .append(s).toString());
212: }
213:
214: request.setResponseCRS(s);
215: } else if (currentTag.equals("format")) {
216: if (LOGGER.isLoggable(Level.FINEST)) {
217: LOGGER.finest(new StringBuffer("found Output Format: ")
218: .append(s).toString());
219: }
220:
221: request.setOutputFormat(s);
222: } else if (currentTag.equals("pos") && insideEnvelope) {
223: if (LOGGER.isLoggable(Level.FINEST)) {
224: LOGGER.finest(new StringBuffer(
225: "found Envelope Coordinates: ").append(s)
226: .toString());
227: }
228:
229: if (coordinates[0] == null) {
230: String[] coords = s.split(" ");
231:
232: try {
233: double arg0 = Double.parseDouble(coords[0]);
234: double arg1 = Double.parseDouble(coords[1]);
235:
236: coordinates[0] = new Double(arg0);
237: coordinates[1] = new Double(arg1);
238: } catch (NumberFormatException e) {
239: coordinates[0] = null;
240: coordinates[1] = null;
241: }
242: } else if (coordinates[2] == null) {
243: String[] coords = s.split(" ");
244:
245: try {
246: double arg0 = Double.parseDouble(coords[0]);
247: double arg1 = Double.parseDouble(coords[1]);
248:
249: coordinates[2] = new Double(arg0);
250: coordinates[3] = new Double(arg1);
251:
252: Envelope env = new Envelope(coordinates[0]
253: .doubleValue(), coordinates[2]
254: .doubleValue(), coordinates[1]
255: .doubleValue(), coordinates[3]
256: .doubleValue());
257: request.setEnvelope(env);
258: } catch (NumberFormatException e) {
259: coordinates[2] = null;
260: coordinates[3] = null;
261: }
262: }
263: } else if (currentTag.equals("low") && insideGrid) {
264: if (LOGGER.isLoggable(Level.FINEST)) {
265: LOGGER.finest(new StringBuffer("found Grid Lowers: ")
266: .append(s).toString());
267: }
268:
269: String[] coords = s.split(" ");
270:
271: try {
272: double arg0 = Double.parseDouble(coords[0]);
273: double arg1 = Double.parseDouble(coords[1]);
274:
275: lowers[0] = new Double(arg0);
276: lowers[1] = new Double(arg1);
277:
278: request.setGridLow(lowers);
279: } catch (NumberFormatException e) {
280: lowers[0] = null;
281: lowers[1] = null;
282: }
283: } else if (currentTag.equals("high") && insideGrid) {
284: if (LOGGER.isLoggable(Level.FINEST)) {
285: LOGGER.finest(new StringBuffer("found Grid Highers: ")
286: .append(s).toString());
287: }
288:
289: String[] coords = s.split(" ");
290:
291: try {
292: double arg0 = Double.parseDouble(coords[0]);
293: double arg1 = Double.parseDouble(coords[1]);
294:
295: highers[0] = new Double(arg0);
296: highers[1] = new Double(arg1);
297:
298: request.setGridHigh(highers);
299: } catch (NumberFormatException e) {
300: highers[0] = null;
301: highers[1] = null;
302: }
303: } else if (currentTag.equals("pos") && insideGrid) {
304: if (LOGGER.isLoggable(Level.FINEST)) {
305: LOGGER.finest(new StringBuffer("found Grid Origin: ")
306: .append(s).toString());
307: }
308:
309: String[] coords = s.split(" ");
310:
311: try {
312: double arg0 = Double.parseDouble(coords[0]);
313: double arg1 = Double.parseDouble(coords[1]);
314:
315: origin[0] = new Double(arg0);
316: origin[1] = new Double(arg1);
317:
318: request.setGridOrigin(origin);
319: } catch (NumberFormatException e) {
320: origin[0] = null;
321: origin[1] = null;
322: }
323: } else if (currentTag.equals("offsetVector") && insideGrid) {
324: if (LOGGER.isLoggable(Level.FINEST)) {
325: LOGGER.finest(new StringBuffer(
326: "found Grid Offset-Vector: ").append(s)
327: .toString());
328: }
329:
330: String[] coords = s.split(" ");
331:
332: try {
333: double arg0 = Double.parseDouble(coords[0]);
334: double arg1 = Double.parseDouble(coords[1]);
335:
336: if (offsetVector[0] == null) {
337: offsetVector[0] = new Double(arg0);
338: } else {
339: offsetVector[1] = new Double(arg1);
340: request.setOffsetVector(offsetVector);
341: }
342: } catch (NumberFormatException e) {
343: offsetVector[0] = null;
344: offsetVector[1] = null;
345: }
346: } else if (currentTag.equals("singleValue") && insideRange
347: && (paramNum == (paramNames.size() - 1))) {
348: if (LOGGER.isLoggable(Level.FINEST)) {
349: LOGGER.finest(new StringBuffer("found axisSubset{"
350: + paramNames.get(paramNum)
351: + "} > singleValue: ").append(s).toString());
352: }
353:
354: final String key = (String) paramNames.get(paramNum);
355:
356: if (params.get(key) == null) {
357: params.put(key, s);
358: }
359: } else if (currentTag.equals("min") && insideRange
360: && (paramNum == (paramNames.size() - 1))) {
361: if (LOGGER.isLoggable(Level.FINEST)) {
362: LOGGER.finest(new StringBuffer("found axisSubset{"
363: + paramNames.get(paramNum) + "} > min: ")
364: .append(s).toString());
365: }
366:
367: minTmp = (int) Math.round(Double.parseDouble(s));
368: } else if (currentTag.equals("max") && insideRange
369: && (paramNum == (paramNames.size() - 1))) {
370: if (LOGGER.isLoggable(Level.FINEST)) {
371: LOGGER.finest(new StringBuffer("found axisSubset{"
372: + paramNames.get(paramNum) + "} > max: ")
373: .append(s).toString());
374: }
375:
376: final String key = (String) paramNames.get(paramNum);
377:
378: if (params.get(key) == null) {
379: int maxTmp = (int) Math.round(Double.parseDouble(s));
380: params.put(key, minTmp + "/" + maxTmp);
381: minTmp = 0;
382: }
383: }
384:
385: currentTag = "";
386: }
387: }
|