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();
}
}
}
Aggregations