Search in sources :

Example 1 with ErrorReporter

use of net.sf.saxon.lib.ErrorReporter in project ph-schematron by phax.

the class PSXPathBoundSchema method _createXPathContext.

@Nonnull
private XPath _createXPathContext() {
    final MapBasedNamespaceContext aNamespaceContext = getNamespaceContext();
    final XPath aXPathContext = XPathHelper.createNewXPath(m_aXPathConfig.getXPathFactory(), m_aXPathConfig.getXPathVariableResolver(), m_aXPathConfig.getXPathFunctionResolver(), aNamespaceContext);
    if ("net.sf.saxon.xpath.XPathEvaluator".equals(aXPathContext.getClass().getName())) {
        // Saxon implementation special handling
        final XPathEvaluator aSaxonXPath = (XPathEvaluator) aXPathContext;
        // Since 9.7.0-4 it must implement NamespaceResolver
        aSaxonXPath.setNamespaceContext(new SaxonNamespaceContext(aNamespaceContext));
        // Wrap the PSErrorHandler to a ErrorListener
        final Function<Configuration, ? extends ErrorReporter> factory = cfg -> {
            final IPSErrorHandler aErrHdl = getErrorHandler();
            return (final XmlProcessingError error) -> {
                final ILocation aLocation = error.getLocation() == null ? null : new SimpleLocation(error.getLocation().getSystemId(), error.getLocation().getLineNumber(), error.getLocation().getColumnNumber());
                aErrHdl.handleError(SingleError.builder().errorLevel(error.isWarning() ? EErrorLevel.WARN : EErrorLevel.ERROR).errorID(error.getErrorCode() != null ? error.getErrorCode().toString() : null).errorLocation(aLocation).errorText(error.getMessage()).linkedException(error.getCause()).build());
            };
        };
        aSaxonXPath.getConfiguration().setErrorReporterFactory(factory);
    }
    return aXPathContext;
}
Also used : MapBasedNamespaceContext(com.helger.xml.namespace.MapBasedNamespaceContext) XPath(javax.xml.xpath.XPath) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ErrorReporter(net.sf.saxon.lib.ErrorReporter) LoggerFactory(org.slf4j.LoggerFactory) PSPhase(com.helger.schematron.pure.model.PSPhase) ToStringGenerator(com.helger.commons.string.ToStringGenerator) AbstractPSBoundSchema(com.helger.schematron.pure.bound.AbstractPSBoundSchema) Configuration(net.sf.saxon.Configuration) XmlProcessingError(net.sf.saxon.s9api.XmlProcessingError) PSRule(com.helger.schematron.pure.model.PSRule) XPathFactoryConfigurationException(javax.xml.xpath.XPathFactoryConfigurationException) PSSchema(com.helger.schematron.pure.model.PSSchema) IXPathConfig(com.helger.schematron.pure.xpath.IXPathConfig) XPathHelper(com.helger.xml.xpath.XPathHelper) IPSElement(com.helger.schematron.pure.model.IPSElement) Map(java.util.Map) IPSErrorHandler(com.helger.schematron.pure.errorhandler.IPSErrorHandler) PSName(com.helger.schematron.pure.model.PSName) ILocation(com.helger.commons.location.ILocation) IPSQueryBinding(com.helger.schematron.pure.binding.IPSQueryBinding) XPathFunctionResolver(javax.xml.xpath.XPathFunctionResolver) SchematronValidationException(com.helger.schematron.pure.validation.SchematronValidationException) IPSHasMixedContent(com.helger.schematron.pure.model.IPSHasMixedContent) XPathConfigBuilder(com.helger.schematron.pure.xpath.XPathConfigBuilder) SingleError(com.helger.commons.error.SingleError) XPathVariableResolver(javax.xml.xpath.XPathVariableResolver) ICommonsList(com.helger.commons.collection.impl.ICommonsList) XPathEvaluator(net.sf.saxon.xpath.XPathEvaluator) SchematronBindException(com.helger.schematron.pure.binding.SchematronBindException) ICommonsMap(com.helger.commons.collection.impl.ICommonsMap) XPath(javax.xml.xpath.XPath) PSXPathVariables(com.helger.schematron.pure.binding.xpath.PSXPathVariables) SaxonNamespaceContext(com.helger.schematron.saxon.SaxonNamespaceContext) XPathExpression(javax.xml.xpath.XPathExpression) Function(java.util.function.Function) Node(org.w3c.dom.Node) IPSValidationHandler(com.helger.schematron.pure.validation.IPSValidationHandler) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) EErrorLevel(com.helger.commons.error.level.EErrorLevel) Logger(org.slf4j.Logger) NodeList(org.w3c.dom.NodeList) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) MapBasedNamespaceContext(com.helger.xml.namespace.MapBasedNamespaceContext) PSDiagnostic(com.helger.schematron.pure.model.PSDiagnostic) ValueEnforcer(com.helger.commons.ValueEnforcer) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) PSPattern(com.helger.schematron.pure.model.PSPattern) PSValueOf(com.helger.schematron.pure.model.PSValueOf) PSAssertReport(com.helger.schematron.pure.model.PSAssertReport) XPathEvaluationHelper(com.helger.schematron.pure.xpath.XPathEvaluationHelper) SimpleLocation(com.helger.commons.location.SimpleLocation) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe) XmlProcessingError(net.sf.saxon.s9api.XmlProcessingError) IPSErrorHandler(com.helger.schematron.pure.errorhandler.IPSErrorHandler) ILocation(com.helger.commons.location.ILocation) Configuration(net.sf.saxon.Configuration) SaxonNamespaceContext(com.helger.schematron.saxon.SaxonNamespaceContext) XPathEvaluator(net.sf.saxon.xpath.XPathEvaluator) SimpleLocation(com.helger.commons.location.SimpleLocation) Nonnull(javax.annotation.Nonnull)

Example 2 with ErrorReporter

use of net.sf.saxon.lib.ErrorReporter in project coffeefilter by nineml.

the class TestDriver method deepEqual.

private boolean deepEqual(XdmValue left, XdmValue right, TestResult result) throws SaxonApiException {
    if (result.expected == null) {
        result.expected = new ArrayList<>();
        result.expected.add(left);
        result.actual = new ArrayList<>();
        result.actual.add(right);
        result.deepEqualMessages = new ArrayList<>();
    }
    QName a = new QName("", "a");
    QName b = new QName("", "b");
    XPathCompiler compiler = processor.newXPathCompiler();
    compiler.declareVariable(a);
    compiler.declareVariable(b);
    XPathExecutable exec;
    if (runningEE) {
        compiler.declareNamespace("saxon", "http://saxon.sf.net/");
        exec = compiler.compile("saxon:deep-equal($a,$b, (), '?')");
    } else {
        exec = compiler.compile("deep-equal($a,$b)");
    }
    XPathSelector selector = exec.load();
    selector.setVariable(a, left);
    selector.setVariable(b, right);
    CaptureErrors capture = new CaptureErrors();
    ErrorReporter saveReporter = selector.getUnderlyingXPathContext().getErrorReporter();
    selector.getUnderlyingXPathContext().setErrorReporter(capture);
    XdmSequenceIterator<XdmItem> iter = selector.iterator();
    XdmAtomicValue item = (XdmAtomicValue) iter.next();
    selector.getUnderlyingXPathContext().setErrorReporter(saveReporter);
    if (capture.messages.isEmpty()) {
        result.deepEqualMessages.add("");
    } else {
        result.deepEqualMessages.add(capture.messages.get(0));
    }
    return item.getBooleanValue();
}
Also used : StandardErrorReporter(net.sf.saxon.lib.StandardErrorReporter) ErrorReporter(net.sf.saxon.lib.ErrorReporter)

Aggregations

ErrorReporter (net.sf.saxon.lib.ErrorReporter)2 ValueEnforcer (com.helger.commons.ValueEnforcer)1 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 CommonsHashMap (com.helger.commons.collection.impl.CommonsHashMap)1 ICommonsList (com.helger.commons.collection.impl.ICommonsList)1 ICommonsMap (com.helger.commons.collection.impl.ICommonsMap)1 SingleError (com.helger.commons.error.SingleError)1 EErrorLevel (com.helger.commons.error.level.EErrorLevel)1 ILocation (com.helger.commons.location.ILocation)1 SimpleLocation (com.helger.commons.location.SimpleLocation)1 ToStringGenerator (com.helger.commons.string.ToStringGenerator)1 IPSQueryBinding (com.helger.schematron.pure.binding.IPSQueryBinding)1 SchematronBindException (com.helger.schematron.pure.binding.SchematronBindException)1 PSXPathVariables (com.helger.schematron.pure.binding.xpath.PSXPathVariables)1 AbstractPSBoundSchema (com.helger.schematron.pure.bound.AbstractPSBoundSchema)1 IPSErrorHandler (com.helger.schematron.pure.errorhandler.IPSErrorHandler)1 IPSElement (com.helger.schematron.pure.model.IPSElement)1 IPSHasMixedContent (com.helger.schematron.pure.model.IPSHasMixedContent)1 PSAssertReport (com.helger.schematron.pure.model.PSAssertReport)1 PSDiagnostic (com.helger.schematron.pure.model.PSDiagnostic)1