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