Search in sources :

Example 1 with AnyUriResolver

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;
}
Also used : SearchResourceResolver(org.exist.validation.resolver.SearchResourceResolver) InputSource(org.xml.sax.InputSource) org.exist.validation.resolver.eXistXMLCatalogResolver(org.exist.validation.resolver.eXistXMLCatalogResolver) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) AnyUriResolver(org.exist.validation.resolver.AnyUriResolver) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Aggregations

IOException (java.io.IOException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 AnyUriResolver (org.exist.validation.resolver.AnyUriResolver)1 SearchResourceResolver (org.exist.validation.resolver.SearchResourceResolver)1 org.exist.validation.resolver.eXistXMLCatalogResolver (org.exist.validation.resolver.eXistXMLCatalogResolver)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1 XMLReader (org.xml.sax.XMLReader)1