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