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.wfsv.xml.v1_1_0;
06:
07: import org.geoserver.ows.XmlRequestReader;
08: import org.geoserver.wfs.WFS;
09: import org.geoserver.wfs.WFSException;
10: import org.geotools.util.Version;
11: import org.geotools.xml.Parser;
12: import org.vfny.geoserver.global.NameSpaceInfo;
13: import org.xml.sax.InputSource;
14: import java.io.Reader;
15: import java.util.Iterator;
16: import java.util.Map;
17:
18: import javax.xml.namespace.QName;
19:
20: /**
21: * (JD) TODO: This class shares too much code with the normal wfs xml reader. We
22: * need to find a way to factor out the common code, be it through subclassing
23: * or through a utl
24: */
25: public class WfsvXmlReader extends XmlRequestReader {
26: private WFS wfs;
27: private WFSVConfiguration configuration;
28:
29: public WfsvXmlReader(String element, WFS wfs,
30: WFSVConfiguration configuration) {
31: super (new QName(org.geoserver.wfsv.xml.v1_1_0.WFSV.NAMESPACE,
32: element), new Version("1.1.0"), "wfsv");
33: this .wfs = wfs;
34: this .configuration = configuration;
35: }
36:
37: public Object read(Object request, Reader reader, Map kvp)
38: throws Exception {
39: // TODO: make this configurable?
40: configuration.getProperties().add(
41: Parser.Properties.PARSE_UNKNOWN_ELEMENTS);
42:
43: Parser parser = new Parser(configuration);
44: if (wfs.getCiteConformanceHacks())
45: parser.setValidating(true);
46:
47: //"inject" namespace mappings
48: NameSpaceInfo[] namespaces = configuration.getCatalog()
49: .getNameSpaces();
50: for (int i = 0; i < namespaces.length; i++) {
51: if (namespaces[i].isDefault())
52: continue;
53:
54: parser.getNamespaces().declarePrefix(
55: namespaces[i].getPrefix(), namespaces[i].getURI());
56: }
57:
58: // set the input source with the correct encoding
59: InputSource source = new InputSource(reader);
60: source.setEncoding(wfs.getCharSet().name());
61:
62: Object parsed = parser.parse(source);
63:
64: // valid request? this should definitley be a configuration option
65: // TODO: HACK, disabling validation for transaction
66: if (!"Transaction"
67: .equalsIgnoreCase(getElement().getLocalPart())) {
68: if (!parser.getValidationErrors().isEmpty()) {
69: WFSException exception = new WFSException(
70: "Invalid request", "InvalidParameterValue");
71:
72: for (Iterator e = parser.getValidationErrors()
73: .iterator(); e.hasNext();) {
74: Exception error = (Exception) e.next();
75: exception.getExceptionText().add(
76: error.getLocalizedMessage());
77: }
78:
79: throw exception;
80: }
81: }
82:
83: return parsed;
84: }
85: }
|