Search in sources :

Example 1 with SchematronValidationException

use of com.helger.schematron.pure.validation.SchematronValidationException in project ph-schematron by phax.

the class PSXPathValidationHandlerSVRL method _getErrorText.

/**
 * Get the error text from an assert or report element.
 *
 * @param aBoundContentElements
 *        The list of bound elements to be evaluated.
 * @param aSourceNode
 *        The XML node of the document currently validated.
 * @return A non-<code>null</code> String
 * @throws SchematronValidationException
 *         In case evaluating an XPath expression fails.
 */
@Nonnull
private String _getErrorText(@Nonnull final List<PSXPathBoundElement> aBoundContentElements, @Nonnull final Node aSourceNode) throws SchematronValidationException {
    final StringBuilder aSB = new StringBuilder();
    for (final PSXPathBoundElement aBoundElement : aBoundContentElements) {
        final Object aContent = aBoundElement.getElement();
        if (aContent instanceof String)
            aSB.append((String) aContent);
        else if (aContent instanceof PSName) {
            final PSName aName = (PSName) aContent;
            if (aName.hasPath()) {
                // XPath present
                try {
                    aSB.append((String) XPathEvaluationHelper.evaluate(aBoundElement.getBoundExpression(), aSourceNode, XPathConstants.STRING, m_sBaseURI));
                } catch (final XPathExpressionException ex) {
                    _error(aName, "Failed to evaluate XPath expression to a string: '" + aBoundElement.getExpression() + "'", ex.getCause() != null ? ex.getCause() : ex);
                    // Append the path so that something is present in the output
                    aSB.append(aName.getPath());
                }
            } else {
                // No XPath present
                aSB.append(aSourceNode.getNodeName());
            }
        } else if (aContent instanceof PSValueOf) {
            final PSValueOf aValueOf = (PSValueOf) aContent;
            try {
                aSB.append((String) XPathEvaluationHelper.evaluate(aBoundElement.getBoundExpression(), aSourceNode, XPathConstants.STRING, m_sBaseURI));
            } catch (final XPathExpressionException ex) {
                _error(aValueOf, "Failed to evaluate XPath expression to a string: '" + aBoundElement.getExpression() + "'", ex);
                // Append the path so that something is present in the output
                aSB.append(aValueOf.getSelect());
            }
        } else if (aContent instanceof PSEmph)
            aSB.append(((PSEmph) aContent).getAsText());
        else if (aContent instanceof PSDir)
            aSB.append(((PSDir) aContent).getAsText());
        else if (aContent instanceof PSSpan)
            aSB.append(((PSSpan) aContent).getAsText());
        else
            throw new SchematronValidationException("Unsupported assert/report content element: " + aContent);
    }
    return aSB.toString();
}
Also used : PSSpan(com.helger.schematron.pure.model.PSSpan) SchematronValidationException(com.helger.schematron.pure.validation.SchematronValidationException) PSName(com.helger.schematron.pure.model.PSName) PSDir(com.helger.schematron.pure.model.PSDir) PSEmph(com.helger.schematron.pure.model.PSEmph) XPathExpressionException(javax.xml.xpath.XPathExpressionException) PSXPathBoundElement(com.helger.schematron.pure.bound.xpath.PSXPathBoundElement) PSValueOf(com.helger.schematron.pure.model.PSValueOf) Nonnull(javax.annotation.Nonnull)

Example 2 with SchematronValidationException

use of com.helger.schematron.pure.validation.SchematronValidationException in project ph-schematron by phax.

the class PSXPathValidationHandlerSVRL method onSuccessfulReport.

@Override
@Nonnull
public EContinue onSuccessfulReport(@Nonnull final PSAssertReport aAssertReport, @Nonnull final String sTestExpression, @Nonnull final Node aRuleMatchingNode, final int nNodeIndex, @Nullable final Object aContext) throws SchematronValidationException {
    if (!(aContext instanceof PSXPathBoundAssertReport))
        throw new SchematronValidationException("The passed context must be an XPath object but is a " + aContext);
    final PSXPathBoundAssertReport aBoundAssertReport = (PSXPathBoundAssertReport) aContext;
    final SuccessfulReport aSuccessfulReport = new SuccessfulReport();
    aSuccessfulReport.setFlag(aAssertReport.getFlag());
    aSuccessfulReport.setId(aAssertReport.getID());
    aSuccessfulReport.setLocation(_getPathToNode(aRuleMatchingNode));
    // TODO role
    aSuccessfulReport.setTest(sTestExpression);
    aSuccessfulReport.setText(_getErrorText(aBoundAssertReport.getAllBoundContentElements(), aRuleMatchingNode));
    _handleDiagnosticReferences(aAssertReport.getAllDiagnostics(), aSuccessfulReport.getDiagnosticReference(), aBoundAssertReport, aRuleMatchingNode);
    m_aSchematronOutput.getActivePatternAndFiredRuleAndFailedAssert().add(aSuccessfulReport);
    return EContinue.CONTINUE;
}
Also used : PSXPathBoundAssertReport(com.helger.schematron.pure.bound.xpath.PSXPathBoundAssertReport) SchematronValidationException(com.helger.schematron.pure.validation.SchematronValidationException) SuccessfulReport(org.oclc.purl.dsdl.svrl.SuccessfulReport) Nonnull(javax.annotation.Nonnull)

Example 3 with SchematronValidationException

use of com.helger.schematron.pure.validation.SchematronValidationException in project ph-schematron by phax.

the class PSXPathValidationHandlerSVRL method onFailedAssert.

@Override
@Nonnull
public EContinue onFailedAssert(@Nonnull final PSAssertReport aAssertReport, @Nonnull final String sTestExpression, @Nonnull final Node aRuleMatchingNode, final int nNodeIndex, @Nullable final Object aContext) throws SchematronValidationException {
    if (!(aContext instanceof PSXPathBoundAssertReport))
        throw new SchematronValidationException("The passed context must be an XPath object but is a " + aContext);
    final PSXPathBoundAssertReport aBoundAssertReport = (PSXPathBoundAssertReport) aContext;
    final FailedAssert aFailedAssert = new FailedAssert();
    aFailedAssert.setFlag(aAssertReport.getFlag());
    aFailedAssert.setId(aAssertReport.getID());
    aFailedAssert.setLocation(_getPathToNode(aRuleMatchingNode));
    // TODO role
    aFailedAssert.setTest(sTestExpression);
    aFailedAssert.setText(_getErrorText(aBoundAssertReport.getAllBoundContentElements(), aRuleMatchingNode));
    _handleDiagnosticReferences(aAssertReport.getAllDiagnostics(), aFailedAssert.getDiagnosticReference(), aBoundAssertReport, aRuleMatchingNode);
    m_aSchematronOutput.getActivePatternAndFiredRuleAndFailedAssert().add(aFailedAssert);
    return EContinue.CONTINUE;
}
Also used : PSXPathBoundAssertReport(com.helger.schematron.pure.bound.xpath.PSXPathBoundAssertReport) SchematronValidationException(com.helger.schematron.pure.validation.SchematronValidationException) FailedAssert(org.oclc.purl.dsdl.svrl.FailedAssert) Nonnull(javax.annotation.Nonnull)

Aggregations

SchematronValidationException (com.helger.schematron.pure.validation.SchematronValidationException)3 Nonnull (javax.annotation.Nonnull)3 PSXPathBoundAssertReport (com.helger.schematron.pure.bound.xpath.PSXPathBoundAssertReport)2 PSXPathBoundElement (com.helger.schematron.pure.bound.xpath.PSXPathBoundElement)1 PSDir (com.helger.schematron.pure.model.PSDir)1 PSEmph (com.helger.schematron.pure.model.PSEmph)1 PSName (com.helger.schematron.pure.model.PSName)1 PSSpan (com.helger.schematron.pure.model.PSSpan)1 PSValueOf (com.helger.schematron.pure.model.PSValueOf)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 FailedAssert (org.oclc.purl.dsdl.svrl.FailedAssert)1 SuccessfulReport (org.oclc.purl.dsdl.svrl.SuccessfulReport)1