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.geoserver.wfs.xml.v1_1_0;
006:
007: import org.geoserver.ows.XmlRequestReader;
008: import org.geoserver.wfs.WFS;
009: import org.geoserver.wfs.WFSException;
010: import org.geotools.util.Version;
011: import org.geotools.xml.Parser;
012: import org.vfny.geoserver.global.Data;
013: import org.vfny.geoserver.global.NameSpaceInfo;
014: import org.xml.sax.InputSource;
015: import java.io.Reader;
016: import java.util.Iterator;
017: import java.util.Map;
018:
019: import javax.xml.namespace.QName;
020:
021: /**
022: * Xml reader for wfs 1.1.0 xml requests.
023: *
024: * @author Justin Deoliveira, The Open Planning Project
025: *
026: * TODO: there is too much duplication with the 1.0.0 reader, factor it out.
027: */
028: public class WfsXmlReader extends XmlRequestReader {
029: /**
030: * WFs configuration
031: */
032: WFS wfs;
033:
034: /**
035: * Xml Configuration
036: */
037: WFSConfiguration configuration;
038:
039: public WfsXmlReader(String element, WFS wfs,
040: WFSConfiguration configuration) {
041: super (new QName(org.geoserver.wfs.xml.v1_1_0.WFS.NAMESPACE,
042: element), new Version("1.1.0"), "wfs");
043: this .wfs = wfs;
044: this .configuration = configuration;
045: }
046:
047: public Object read(Object request, Reader reader, Map kvp)
048: throws Exception {
049: //check the strict flag to determine if we should validate or not
050: Boolean strict = (Boolean) kvp.get("strict");
051: if (strict == null) {
052: strict = Boolean.FALSE;
053: }
054:
055: //check for cite compliance, we always validate for cite
056: if (wfs.getCiteConformanceHacks()) {
057: strict = Boolean.TRUE;
058: }
059:
060: //TODO: make this configurable?
061: configuration.getProperties().add(
062: Parser.Properties.PARSE_UNKNOWN_ELEMENTS);
063:
064: Parser parser = new Parser(configuration);
065: parser.setValidating(strict.booleanValue());
066:
067: //"inject" namespace mappings
068: NameSpaceInfo[] namespaces = configuration.getCatalog()
069: .getNameSpaces();
070: for (int i = 0; i < namespaces.length; i++) {
071: if (namespaces[i].isDefault())
072: continue;
073:
074: parser.getNamespaces().declarePrefix(
075: namespaces[i].getPrefix(), namespaces[i].getURI());
076: }
077:
078: //set the input source with the correct encoding
079: InputSource source = new InputSource(reader);
080: source.setEncoding(wfs.getCharSet().name());
081:
082: Object parsed = parser.parse(source);
083:
084: //TODO: HACK, disabling validation for transaction
085: if (!"Transaction"
086: .equalsIgnoreCase(getElement().getLocalPart())) {
087: if (!parser.getValidationErrors().isEmpty()) {
088: WFSException exception = new WFSException(
089: "Invalid request", "InvalidParameterValue");
090:
091: for (Iterator e = parser.getValidationErrors()
092: .iterator(); e.hasNext();) {
093: Exception error = (Exception) e.next();
094: exception.getExceptionText().add(
095: error.getLocalizedMessage());
096: }
097:
098: throw exception;
099: }
100: }
101:
102: return parsed;
103: }
104: }
|