use of org.exist.validation.resolver.AnyUriResolver in project exist by eXist-db.
the class Validator method validateParse.
/**
* Validate XML data from reader using specified grammar.
*
* @param grammarUrl User supplied path to grammar.
* @param stream XML input.
* @return Validation report containing all validation info.
*/
public ValidationReport validateParse(InputStream stream, String grammarUrl) {
logger.debug("Start validation.");
final ValidationReport report = new ValidationReport();
final ValidationContentHandler contenthandler = new ValidationContentHandler();
try {
final XMLReader xmlReader = getXMLReader(contenthandler, report);
if (grammarUrl == null) {
// Scenario 1 : no params - use system catalog
logger.debug("Validation using system catalog.");
xmlReader.setProperty(XMLReaderObjectFactory.APACHE_PROPERTIES_INTERNAL_ENTITYRESOLVER, systemCatalogResolver);
} else if (grammarUrl.endsWith(".xml")) {
// Scenario 2 : path to catalog (xml)
logger.debug("Validation using user specified catalog '{}'.", grammarUrl);
final eXistXMLCatalogResolver resolver = new eXistXMLCatalogResolver();
resolver.setCatalogList(new String[] { grammarUrl });
xmlReader.setProperty(XMLReaderObjectFactory.APACHE_PROPERTIES_INTERNAL_ENTITYRESOLVER, resolver);
} else if (grammarUrl.endsWith("/")) {
// Scenario 3 : path to collection ("/"): search.
logger.debug("Validation using searched grammar, start from '{}'.", grammarUrl);
final SearchResourceResolver resolver = new SearchResourceResolver(grammarUrl, brokerPool);
xmlReader.setProperty(XMLReaderObjectFactory.APACHE_PROPERTIES_INTERNAL_ENTITYRESOLVER, resolver);
} else {
// Scenario 4 : path to grammar (xsd, dtd) specified.
logger.debug("Validation using specified grammar '{}'.", grammarUrl);
final AnyUriResolver resolver = new AnyUriResolver(grammarUrl);
xmlReader.setProperty(XMLReaderObjectFactory.APACHE_PROPERTIES_INTERNAL_ENTITYRESOLVER, resolver);
}
logger.debug("Validation started.");
report.start();
final InputSource source = new InputSource(stream);
xmlReader.parse(source);
logger.debug("Validation stopped.");
report.stop();
report.setNamespaceUri(contenthandler.getNamespaceUri());
if (!report.isValid()) {
logger.debug("Document is not valid.");
}
} catch (final ParserConfigurationException | SAXException | IOException ex) {
logger.error(ex);
report.setThrowable(ex);
} finally {
report.stop();
logger.debug("Validation performed in {} msec.", report.getValidationDuration());
}
return report;
}
Aggregations