01: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
02: * This code is licensed under the GPL 2.0 license, availible at the root
03: * application directory.
04: */
05: package org.geoserver.wfs.xml.v1_0_0;
06:
07: import org.geoserver.ows.XmlRequestReader;
08: import org.geoserver.wfs.WFSException;
09: import org.geotools.util.Version;
10: import org.geotools.xml.Parser;
11: import org.vfny.geoserver.global.NameSpaceInfo;
12:
13: import java.io.Reader;
14: import java.util.Iterator;
15: import java.util.Map;
16:
17: import javax.xml.namespace.QName;
18:
19: /**
20: * Xml reader for wfs 1.0.0 xml requests.
21: *
22: * @author Justin Deoliveira, The Open Planning Project
23: *
24: * TODO: there is too much duplication with the 1.1.0 reader, factor it out.
25: */
26: public class WfsXmlReader extends XmlRequestReader {
27: /**
28: * Xml Configuration
29: */
30: WFSConfiguration configuration;
31:
32: public WfsXmlReader(String element, WFSConfiguration configuration) {
33: super (new QName(WFS.NAMESPACE, element), new Version("1.0.0"),
34: "wfs");
35: this .configuration = configuration;
36: }
37:
38: public Object read(Object request, Reader reader, Map kvp)
39: throws Exception {
40: //check the strict flag to determine if we should validate or not
41: Boolean strict = (Boolean) kvp.get("strict");
42: if (strict == null) {
43: strict = Boolean.FALSE;
44: }
45:
46: //create the parser instance
47: Parser parser = new Parser(configuration);
48:
49: //"inject" namespace mappings
50: NameSpaceInfo[] namespaces = configuration.getCatalog()
51: .getNameSpaces();
52: for (int i = 0; i < namespaces.length; i++) {
53: if (namespaces[i].isDefault())
54: continue;
55:
56: parser.getNamespaces().declarePrefix(
57: namespaces[i].getPrefix(), namespaces[i].getURI());
58: }
59: //set validation based on strict or not
60: parser.setValidating(strict.booleanValue());
61:
62: //parse
63: Object parsed = parser.parse(reader);
64:
65: //if strict was set, check for validation errors and throw an exception
66: if (strict.booleanValue()
67: && !parser.getValidationErrors().isEmpty()) {
68: WFSException exception = new WFSException(
69: "Invalid request", "InvalidParameterValue");
70:
71: for (Iterator e = parser.getValidationErrors().iterator(); e
72: .hasNext();) {
73: Exception error = (Exception) e.next();
74: exception.getExceptionText().add(
75: error.getLocalizedMessage());
76: }
77:
78: throw exception;
79: }
80:
81: return parsed;
82: }
83: }
|