View Javadoc

1   //
2   // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
3   // All rights reserved.
4   //
5   // Redistribution and use in source and binary forms, with or without modification, 
6   // are permitted provided that the following conditions are met:
7   //
8   // * Redistributions of source code must retain the above copyright notice, 
9   //	 this list of conditions and the following disclaimer.
10  // * Redistributions in binary form must reproduce the above copyright notice, 
11  //	 this list of conditions and the following disclaimer in the documentation 
12  //	 and/or other materials provided with the distribution.
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
14  //	 nor the names of its contributors may be used to endorse or promote products 
15  //	 derived from this software without specific prior written permission.
16  //
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
26  // POSSIBILITY OF SUCH DAMAGE.
27  //
28  
29  package org.objectledge.xml;
30  
31  import java.io.IOException;
32  import java.net.URL;
33  
34  import javax.xml.parsers.ParserConfigurationException;
35  import javax.xml.parsers.SAXParser;
36  import javax.xml.parsers.SAXParserFactory;
37  
38  import org.xml.sax.InputSource;
39  import org.xml.sax.SAXException;
40  import org.xml.sax.XMLReader;
41  
42  import com.sun.msv.grammar.Grammar;
43  import com.sun.msv.verifier.DocumentDeclaration;
44  import com.sun.msv.verifier.Verifier;
45  import com.sun.msv.verifier.regexp.REDocumentDeclaration;
46  
47  /***
48   * Validates XML files against schemata using MSV library.
49   *
50   * <p>The primary schema language used throughout ObjectLedge project is RelaxNG, but the MSV
51   * library determines the schema languague using XML namespace of the top level element. At the
52   * moment XML based schema languages supported by MSV and thus by XMLValidator are RelaxNG,
53   * W3C XSD, and others.</p>
54   * 
55   * @author <a href="rafal@caltha.pl">Rafal Krzewski</a>
56   * @author <a href="dgajda@caltha.pl">Damian Gajda</a>
57   * @version $Id: XMLValidator.java,v 1.10 2004/06/01 11:13:11 zwierzem Exp $
58   */
59  public class XMLValidator
60  {
61  	private XMLGrammarCache grammarCache;
62  	
63      private SAXParser saxParser;
64      private ExceptionErrorHandler errorHandler = new ExceptionErrorHandler();
65  
66  	/*** Pathname of the relaxng schema.*/
67  	public static final String RELAXNG_SCHEMA = "org/objectledge/xml/relaxng.rng";
68  
69      /***
70       * Creates a new instance of the validator.
71       * 
72       * @param grammarCache system grammar cache for retrieving grammars used for validation 
73       * @throws ParserConfigurationException if the JAXP parser factory is misconfigured.
74       * @throws SAXException if the JAXP parser factory is misconfigured.
75       */
76      public XMLValidator(XMLGrammarCache grammarCache) 
77          throws ParserConfigurationException, SAXException
78      {
79  		this.grammarCache = grammarCache;
80      	
81          SAXParserFactory parserFactory = SAXParserFactory.newInstance();
82          parserFactory.setNamespaceAware(true);
83          parserFactory.setValidating(false);
84          saxParser = parserFactory.newSAXParser();
85      }
86      
87      /***
88       * Validates given XML file using specified schema.
89       * 
90       * @param fileUrl the URL of the file to be validated.
91       * @param schemaUrl the URL of the schema to be used.
92       * @throws ParserConfigurationException if parser is badly configured.
93       * @throws IOException if the schema does not exist.
94       * @throws SAXException if the schema is malformed.
95       */
96      public void validate(URL fileUrl, URL schemaUrl)
97          throws SAXException, ParserConfigurationException, IOException
98      {
99          Verifier verifier = getVerifier(schemaUrl);
100         XMLReader reader = saxParser.getXMLReader();
101         InputSource source = new InputSource(fileUrl.toString());
102 		reader.setContentHandler(verifier);
103 		reader.setDTDHandler(verifier);
104         reader.parse(source);
105     }
106     
107     /***
108      * Returns a thread-exclusive verifier instance.
109      * 
110      * @param schemaUrl the URL of the schema to be used.
111      * @return a thread-exclusive verifier instance.
112      * @throws ParserConfigurationException if parser is badly configured.
113      * @throws IOException if the schema does not exist.
114      * @throws SAXException if the schema is malformed.
115      */
116     public Verifier getVerifier(URL schemaUrl)
117         throws SAXException, ParserConfigurationException, IOException
118     {
119 		Grammar grammar = grammarCache.getGrammar(schemaUrl);
120         DocumentDeclaration documentDeclaration = new REDocumentDeclaration(grammar); 
121         return new Verifier(documentDeclaration, errorHandler); 
122     }
123 }