Search in sources :

Example 6 with ValidationReport

use of org.exist.validation.ValidationReport in project exist by eXist-db.

the class Jing method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // Check input parameters
    if (args.length != 2) {
        return Sequence.EMPTY_SEQUENCE;
    }
    final ValidationReport report = new ValidationReport();
    InputSource instance = null;
    InputSource grammar = null;
    try {
        report.start();
        // Get inputstream of XML instance document
        instance = Shared.getInputSource(args[0].itemAt(0), context);
        // Validate using resource specified in second parameter
        grammar = Shared.getInputSource(args[1].itemAt(0), context);
        // Special setup for compact notation
        final String grammarUrl = grammar.getSystemId();
        final SchemaReader schemaReader = ((grammarUrl != null) && (grammarUrl.endsWith(".rnc"))) ? CompactSchemaReader.getInstance() : null;
        // Setup validation properties. see Jing interface
        final PropertyMapBuilder properties = new PropertyMapBuilder();
        ValidateProperty.ERROR_HANDLER.put(properties, report);
        // Register resolver for xmldb:exist:/// embedded URLs
        final ExistResolver resolver = new ExistResolver(brokerPool);
        ValidateProperty.URI_RESOLVER.put(properties, resolver);
        ValidateProperty.ENTITY_RESOLVER.put(properties, resolver);
        // Setup driver
        final ValidationDriver driver = new ValidationDriver(properties.toPropertyMap(), schemaReader);
        // Load schema
        driver.loadSchema(grammar);
        // Validate XML instance
        driver.validate(instance);
    } catch (final MalformedURLException ex) {
        LOG.error(ex.getMessage());
        report.setException(ex);
    } catch (final Throwable ex) {
        LOG.error(ex);
        report.setException(ex);
    } finally {
        Shared.closeInputSource(instance);
        Shared.closeInputSource(grammar);
        report.stop();
    }
    // Create response
    if (isCalledAs("jing")) {
        final Sequence result = new ValueSequence();
        result.add(new BooleanValue(report.isValid()));
        return result;
    } else /* isCalledAs("jing-report") */
    {
        context.pushDocumentContext();
        try {
            final MemTreeBuilder builder = context.getDocumentBuilder();
            final NodeImpl result = Shared.writeReport(report, builder);
            return result;
        } finally {
            context.popDocumentContext();
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) CompactSchemaReader(com.thaiopensource.validate.rng.CompactSchemaReader) SchemaReader(com.thaiopensource.validate.SchemaReader) MalformedURLException(java.net.MalformedURLException) NodeImpl(org.exist.dom.memtree.NodeImpl) ValidationDriver(com.thaiopensource.validate.ValidationDriver) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) ValidationReport(org.exist.validation.ValidationReport) BooleanValue(org.exist.xquery.value.BooleanValue) ExistResolver(org.exist.validation.resolver.unstable.ExistResolver) ValueSequence(org.exist.xquery.value.ValueSequence) PropertyMapBuilder(com.thaiopensource.util.PropertyMapBuilder)

Aggregations

ValidationReport (org.exist.validation.ValidationReport)6 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)5 NodeImpl (org.exist.dom.memtree.NodeImpl)4 InputSource (org.xml.sax.InputSource)4 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)3 BooleanValue (org.exist.xquery.value.BooleanValue)3 Sequence (org.exist.xquery.value.Sequence)3 ValueSequence (org.exist.xquery.value.ValueSequence)3 XMLReader (org.xml.sax.XMLReader)3 StringReader (java.io.StringReader)2 SAXAdapter (org.exist.dom.memtree.SAXAdapter)2 XPathException (org.exist.xquery.XPathException)2 SAXException (org.xml.sax.SAXException)2 Either (com.evolvedbinary.j8fu.Either)1 PropertyMapBuilder (com.thaiopensource.util.PropertyMapBuilder)1 SchemaReader (com.thaiopensource.validate.SchemaReader)1 ValidationDriver (com.thaiopensource.validate.ValidationDriver)1 CompactSchemaReader (com.thaiopensource.validate.rng.CompactSchemaReader)1 StreamSource (javax.xml.transform.stream.StreamSource)1