Skip to content

Commit a78a75f

Browse files
authored
Add a SAX API to the Validation Driver (#265)
1 parent 12275d1 commit a78a75f

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

mod/validate/src/main/com/thaiopensource/validate/ValidationDriver.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.thaiopensource.validate.auto.AutoSchemaReader;
77
import com.thaiopensource.xml.sax.CountingErrorHandler;
88
import com.thaiopensource.xml.sax.ErrorHandlerImpl;
9+
import org.xml.sax.ContentHandler;
910
import org.xml.sax.DTDHandler;
1011
import org.xml.sax.ErrorHandler;
1112
import org.xml.sax.InputSource;
@@ -165,6 +166,35 @@ public boolean validate(InputSource in) throws SAXException, IOException {
165166
}
166167
}
167168

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+
168198
/**
169199
* Get the actual properties of the loaded schema
170200
* @return a PropertyMap with the schema properties
@@ -207,4 +237,22 @@ static public InputSource fileInputSource(File file) {
207237
static public InputSource uriOrFileInputSource(String uriOrFile) {
208238
return new InputSource(UriOrFile.toUri(uriOrFile));
209239
}
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+
}
210258
}

0 commit comments

Comments
 (0)