1
2
3
4
5
6
7
8
9
10 package org.dom4j.io;
11
12 import java.io.IOException;
13
14 import org.dom4j.Document;
15 import org.xml.sax.ContentHandler;
16 import org.xml.sax.ErrorHandler;
17 import org.xml.sax.SAXException;
18 import org.xml.sax.XMLReader;
19 import org.xml.sax.helpers.DefaultHandler;
20
21 /*** <p><code>SAXValidator</code> validates an XML document by
22 * writing the document to a text buffer and parsing it with a validating
23 * SAX parser.
24 * This could be implemented much more efficiently by validating against the
25 * dom4j object model directly but at least allows the reuse of existing
26 * SAX based validating parsers.</p>
27 *
28 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
29 * @version $Revision: 1.8 $
30 */
31 public class SAXValidator {
32
33 /*** <code>XMLReader</code> used to parse the SAX events */
34 private XMLReader xmlReader;
35
36 /*** ErrorHandler class to use */
37 private ErrorHandler errorHandler;
38
39
40
41 public SAXValidator() {
42 }
43
44 public SAXValidator(XMLReader xmlReader) {
45 this.xmlReader = xmlReader;
46 }
47
48
49
50 /*** Validates the given <code>Document</code> by writing it to a
51 * validating SAX Parser.
52 *
53 * @param document is the Document to validate
54 * @throws SAXException if a validation error occurs
55 */
56 public void validate(Document document) throws SAXException {
57 if (document != null) {
58 XMLReader xmlReader = getXMLReader();
59 if ( errorHandler != null ) {
60 xmlReader.setErrorHandler( errorHandler );
61 }
62 try {
63 xmlReader.parse( new DocumentInputSource( document ) );
64 }
65 catch (IOException e) {
66 throw new RuntimeException(
67 "Caught and exception that should never happen: " + e
68 );
69 }
70 }
71 }
72
73
74
75
76
77 /*** @return the <code>XMLReader</code> used to parse SAX events
78 */
79 public XMLReader getXMLReader() throws SAXException {
80 if (xmlReader == null) {
81 xmlReader = createXMLReader();
82 configureReader();
83 }
84 return xmlReader;
85 }
86
87 /*** Sets the <code>XMLReader</code> used to parse SAX events
88 *
89 * @param xmlReader is the <code>XMLReader</code> to parse SAX events
90 */
91 public void setXMLReader(XMLReader xmlReader) throws SAXException {
92 this.xmlReader = xmlReader;
93 configureReader();
94 }
95
96 /*** @return the <code>ErrorHandler</code> used by SAX
97 */
98 public ErrorHandler getErrorHandler() {
99 return errorHandler;
100 }
101
102 /*** Sets the <code>ErrorHandler</code> used by the SAX
103 * <code>XMLReader</code>.
104 *
105 * @param errorHandler is the <code>ErrorHandler</code> used by SAX
106 */
107 public void setErrorHandler(ErrorHandler errorHandler) {
108 this.errorHandler = errorHandler;
109 }
110
111
112
113 /*** Factory Method to allow alternate methods of
114 * creating and configuring XMLReader objects
115 */
116 protected XMLReader createXMLReader() throws SAXException {
117 return SAXHelper.createXMLReader( true );
118 }
119
120 /*** Configures the XMLReader before use */
121 protected void configureReader() throws SAXException {
122 ContentHandler handler = xmlReader.getContentHandler();
123 if ( handler == null ) {
124 xmlReader.setContentHandler( new DefaultHandler() );
125 }
126
127
128 xmlReader.setFeature(
129 "http://xml.org/sax/features/validation",
130 true
131 );
132
133
134 xmlReader.setFeature(
135 "http://xml.org/sax/features/namespaces",
136 true
137 );
138 xmlReader.setFeature(
139 "http://xml.org/sax/features/namespace-prefixes",
140 false
141 );
142 }
143
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192