1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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 }