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:
006: /*
007: * Created on April 20, 2005
008: *
009: */
010: package org.vfny.geoserver.util;
011:
012: import org.apache.xerces.parsers.SAXParser;
013: import org.vfny.geoserver.global.GeoserverDataDirectory;
014: import org.xml.sax.InputSource;
015: import org.xml.sax.SAXException;
016: import org.xml.sax.SAXParseException;
017: import org.xml.sax.helpers.DefaultHandler;
018: import java.io.File;
019: import java.io.InputStream;
020: import java.io.InputStreamReader;
021: import java.io.Reader;
022: import java.util.ArrayList;
023: import java.util.List;
024: import javax.servlet.ServletContext;
025:
026: public class GETMAPValidator {
027: public GETMAPValidator() {
028: }
029:
030: /**
031: * validates against the "normal" location of the schema (ie. ".../capabilities/sld/StyleLayerDescriptor.xsd"
032: * uses the geoserver_home patch
033: * @param xml
034: * @param servContext servlet context
035: * @return
036: */
037: public List validateGETMAP(InputStream xml,
038: ServletContext servContext) {
039: // File schemaFile = new File( GeoserverDataDirectory.getGeoserverDataDirectory(servContext),"/data/capabilities/sld/StyledLayerDescriptor.xsd");
040: File schemaFile = new File(GeoserverDataDirectory
041: .getGeoserverDataDirectory(),
042: "/data/capabilities/sld/GetMap.xsd");
043:
044: try {
045: return validateGETMAP(xml, schemaFile.toURL().toString());
046: } catch (Exception e) {
047: ArrayList al = new ArrayList();
048: al.add(new SAXException(e));
049:
050: return al;
051: }
052: }
053:
054: public static String getErrorMessage(InputStream xml, List errors) {
055: return getErrorMessage(new InputStreamReader(xml), errors);
056: }
057:
058: /**
059: * returns a better formated error message - suitable for framing.
060: * There's a more complex version in StylesEditorAction.
061: *
062: * This will kick out a VERY LARGE errorMessage.
063: *
064: * @param xml
065: * @param errors
066: */
067: public static String getErrorMessage(Reader xml, List errors) {
068: return SLDValidator.getErrorMessage(xml, errors);
069: }
070:
071: public List validateGETMAP(InputStream xml, String SchemaUrl) {
072: return validateGETMAP(new InputSource(xml), SchemaUrl);
073: }
074:
075: public List validateGETMAP(InputSource xml,
076: ServletContext servContext) {
077: File schemaFile = new File(GeoserverDataDirectory
078: .getGeoserverDataDirectory(),
079: "/data/capabilities/sld/GetMap.xsd");
080:
081: try {
082: return validateGETMAP(xml, schemaFile.toURL().toString());
083: } catch (Exception e) {
084: ArrayList al = new ArrayList();
085: al.add(new SAXException(e));
086:
087: return al;
088: }
089: }
090:
091: /**
092: * validate a GETMAP against the schema
093: *
094: * @param xml input stream representing the GETMAP file
095: * @param SchemaUrl location of the schemas. Normally use ".../capabilities/sld/StyleLayerDescriptor.xsd"
096: * @return list of SAXExceptions (0 if the file's okay)
097: */
098: public List validateGETMAP(InputSource xml, String SchemaUrl) {
099: SAXParser parser = new SAXParser();
100:
101: try {
102: parser.setFeature("http://xml.org/sax/features/validation",
103: true);
104: parser.setFeature(
105: "http://apache.org/xml/features/validation/schema",
106: true);
107: parser
108: .setFeature(
109: "http://apache.org/xml/features/validation/schema-full-checking",
110: false);
111:
112: parser
113: .setProperty(
114: "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
115: SchemaUrl);
116: // parser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation","http://www.opengis.net/sld "+SchemaUrl);
117: parser
118: .setProperty(
119: "http://apache.org/xml/properties/schema/external-schemaLocation",
120: "http://www.opengis.net/ows " + SchemaUrl);
121:
122: Validator handler = new Validator();
123: parser.setErrorHandler(handler);
124: parser.parse(xml);
125:
126: return handler.errors;
127: } catch (java.io.IOException ioe) {
128: ArrayList al = new ArrayList();
129: al.add(new SAXParseException(ioe.getLocalizedMessage(),
130: null));
131:
132: return al;
133: } catch (SAXException e) {
134: ArrayList al = new ArrayList();
135: al
136: .add(new SAXParseException(e.getLocalizedMessage(),
137: null));
138:
139: return al;
140: }
141: }
142:
143: // errors in the document will be put in "errors".
144: // if errors.size() ==0 then there were no errors.
145: private class Validator extends DefaultHandler {
146: public ArrayList errors = new ArrayList();
147:
148: public void error(SAXParseException exception)
149: throws SAXException {
150: if (!(exception.getMessage()
151: .startsWith("TargetNamespace.2: Expecting no namespace, but the schema document has a target name"))) {
152: errors.add(exception);
153: }
154: }
155:
156: public void fatalError(SAXParseException exception)
157: throws SAXException {
158: errors.add(exception);
159: }
160:
161: public void warning(SAXParseException exception)
162: throws SAXException {
163: //do nothing
164: }
165: }
166: }
|