|
6 | 6 | import com.thaiopensource.validate.auto.AutoSchemaReader; |
7 | 7 | import com.thaiopensource.xml.sax.CountingErrorHandler; |
8 | 8 | import com.thaiopensource.xml.sax.ErrorHandlerImpl; |
| 9 | +import org.xml.sax.ContentHandler; |
9 | 10 | import org.xml.sax.DTDHandler; |
10 | 11 | import org.xml.sax.ErrorHandler; |
11 | 12 | import org.xml.sax.InputSource; |
@@ -165,6 +166,35 @@ public boolean validate(InputSource in) throws SAXException, IOException { |
165 | 166 | } |
166 | 167 | } |
167 | 168 |
|
| 169 | + /** |
| 170 | + * Validates the events of a producer against the currently loaded schema. This can be called |
| 171 | + * multiple times in order to validate multiple documents. |
| 172 | + * |
| 173 | + * @param producer a producer that delivers SAX events to the handlers. |
| 174 | + * @return <code>true</code> if the document is valid; <code>false</code> otherwise |
| 175 | + * @throws java.lang.IllegalStateException if there is no currently loaded schema |
| 176 | + * @throws java.io.IOException if an I/O error occurred |
| 177 | + * @throws org.xml.sax.SAXException if an XMLReader or ErrorHandler threw a SAXException |
| 178 | + */ |
| 179 | + public boolean validate(SaxProducer saxProducer) throws SAXException, IOException { |
| 180 | + if (schema == null) |
| 181 | + throw new IllegalStateException("cannot validate without schema"); |
| 182 | + if (validator == null) |
| 183 | + validator = schema.createValidator(instanceProperties); |
| 184 | + |
| 185 | + eh.reset(); |
| 186 | + |
| 187 | + DTDHandler dh = validator.getDTDHandler(); |
| 188 | + |
| 189 | + try { |
| 190 | + saxProducer.produce(validator.getContentHandler(), dh, eh); |
| 191 | + return !eh.getHadErrorOrFatalError(); |
| 192 | + } |
| 193 | + finally { |
| 194 | + validator.reset(); |
| 195 | + } |
| 196 | + } |
| 197 | + |
168 | 198 | /** |
169 | 199 | * Get the actual properties of the loaded schema |
170 | 200 | * @return a PropertyMap with the schema properties |
@@ -207,4 +237,22 @@ static public InputSource fileInputSource(File file) { |
207 | 237 | static public InputSource uriOrFileInputSource(String uriOrFile) { |
208 | 238 | return new InputSource(UriOrFile.toUri(uriOrFile)); |
209 | 239 | } |
| 240 | + |
| 241 | + /** |
| 242 | + * A producer of SAX events. |
| 243 | + */ |
| 244 | + public interface SaxProducer { |
| 245 | + /** |
| 246 | + * Causes the producer to process and send SAX events to the handlers. |
| 247 | + * |
| 248 | + * @param contentHandler the content handler. |
| 249 | + * @dtdHandler the DTD handler, or null if absent. |
| 250 | + * @param errorHandler the error handler. |
| 251 | + * |
| 252 | + * @throws IOException if an error occurs during processing. |
| 253 | + * @throws SAXException if a SAXException occurs when calling one of the handlers. |
| 254 | + */ |
| 255 | + public void produce(ContentHandler contentHandler, DTDHandler dtdHandler, ErrorHandler errorHandler) |
| 256 | + throws IOException, SAXException; |
| 257 | + } |
210 | 258 | } |
0 commit comments