View Javadoc

1   /**
2    * Copyright (c) 2011, University of Konstanz, Distributed Systems Group
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *     * Redistributions of source code must retain the above copyright
8    *       notice, this list of conditions and the following disclaimer.
9    *     * Redistributions in binary form must reproduce the above copyright
10   *       notice, this list of conditions and the following disclaimer in the
11   *       documentation and/or other materials provided with the distribution.
12   *     * Neither the name of the University of Konstanz nor the
13   *       names of its contributors may be used to endorse or promote products
14   *       derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   */
27  package org.jaxrx.core;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  import javax.xml.XMLConstants;
32  import javax.xml.parsers.DocumentBuilder;
33  import javax.xml.parsers.DocumentBuilderFactory;
34  import javax.xml.parsers.ParserConfigurationException;
35  import javax.xml.transform.Source;
36  import javax.xml.transform.dom.DOMSource;
37  import javax.xml.transform.sax.SAXSource;
38  import javax.xml.validation.Schema;
39  import javax.xml.validation.SchemaFactory;
40  import javax.xml.validation.Validator;
41  import org.w3c.dom.Document;
42  import org.xml.sax.InputSource;
43  import org.xml.sax.SAXException;
44  
45  /**
46   * This class validates XML documents against a specified XML schema.
47   *
48   * @author Lukas Lewandowski, University of Konstanz
49   *
50   */
51  public final class SchemaChecker {
52  	/**
53  	 * The validation schema.
54  	 */
55  	private final String xslSchema;
56  
57  	/**
58  	 * Constructor.
59  	 *
60  	 * @param schema
61  	 *            schema to check
62  	 */
63  	public SchemaChecker(final String schema) {
64  		xslSchema = "/" + schema + ".xsd";
65  	}
66  
67  	/**
68  	 * This method parses an XML input with a W3C DOM implementation and
69  	 * validates it then with the available XML schema.
70  	 *
71  	 * @param input
72  	 *            The input stream containing the XML query.
73  	 * @return The parsed XML source as {@link Document}.
74  	 */
75  	public Document check(final InputStream input) {
76  		Document document;
77  		try {
78  			final DocumentBuilder docBuilder = DocumentBuilderFactory
79  					.newInstance().newDocumentBuilder();
80  			document = docBuilder.parse(input);
81  
82  			final InputStream is = getClass().getResourceAsStream(xslSchema);
83  			final Source source = new SAXSource(new InputSource(is));
84  			checkIsValid(document, source);
85  		} catch (final SAXException exce) {
86  			throw new JaxRxException(400, exce.getMessage());
87  		} catch (final ParserConfigurationException exce) {
88  			throw new JaxRxException(exce);
89  		} catch (final IOException exce) {
90  			throw new JaxRxException(exce);
91  		}
92  		return document;
93  	}
94  
95  	/**
96  	 * This method checks the parsed document if it is valid to a given XML
97  	 * schema. If not, an exception is thrown
98  	 *
99  	 * @param document
100 	 *            The parsed document.
101 	 * @param source
102 	 *            The {@link String} representation of the XML schema file.
103 	 * @throws SAXException
104 	 *             if the document is invalid
105 	 * @throws IOException
106 	 *             if the input cannot be read
107 	 */
108 	private void checkIsValid(final Document document, final Source source)
109 			throws SAXException, IOException {
110 
111 		final SchemaFactory schemaFactory = SchemaFactory
112 				.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
113 		final Schema schema = schemaFactory.newSchema(source);
114 		final Validator validator = schema.newValidator();
115 		validator.validate(new DOMSource(document));
116 	}
117 }