use of com.helger.phive.engine.source.ValidationSourceXML in project phive by phax.
the class ValidationExecutorXSDPartial method applyValidation.
@Nonnull
public ValidationResult applyValidation(@Nonnull final IValidationSourceXML aSource, @Nullable final Locale aLocale) {
ValueEnforcer.notNull(aSource, "Source");
final IValidationArtefact aVA = getValidationArtefact();
NodeList aNodeSet;
try {
aNodeSet = (NodeList) m_aPartialContext.getXPathExpression().evaluate(aSource.getNode(), XPathConstants.NODESET);
} catch (final XPathExpressionException ex) {
throw new IllegalStateException(ex);
}
final ErrorList aErrorList = new ErrorList();
final int nMatchingNodes = aNodeSet.getLength();
if (m_aPartialContext.hasMinNodeCount())
if (nMatchingNodes < m_aPartialContext.getMinNodeCount()) {
// Too little matches found
aErrorList.add(SingleError.builderFatalError().errorLocation(aVA.getRuleResourcePath()).errorText("The minimum number of result nodes (" + m_aPartialContext.getMinNodeCount() + ") is not met").build());
}
if (m_aPartialContext.hasMaxNodeCount())
if (nMatchingNodes > m_aPartialContext.getMaxNodeCount()) {
// Too little matches found
aErrorList.add(SingleError.builderFatalError().errorLocation(aVA.getRuleResourcePath()).errorText("The maximum number of result nodes (" + m_aPartialContext.getMaxNodeCount() + ") is not met").build());
}
if (nMatchingNodes == 0) {
// No match found - nothing to do
return new ValidationResult(aVA, aErrorList);
}
// Find the XML schema required for validation
// as we don't have a node, we need to trust the implementation class
final Schema aSchema = m_aSchemaProvider.get();
assert aSchema != null;
for (int i = 0; i < aNodeSet.getLength(); ++i) {
// Build a partial source
final IValidationSourceXML aRealSource = new ValidationSourceXML(aSource.getSystemID(), aNodeSet.item(i), true);
try {
// Apply the XML schema validation
XMLSchemaValidationHelper.validate(aSchema, aRealSource.getAsTransformSource(), aErrorList, aLocale);
} catch (final IllegalArgumentException ex) {
// Happens when non-XML document is trying to be parsed
if (ex.getCause() instanceof SAXParseException) {
aErrorList.add(AbstractSAXErrorHandler.getSaxParseError(EErrorLevel.FATAL_ERROR, (SAXParseException) ex.getCause()));
} else {
aErrorList.add(SingleError.builderFatalError().errorLocation(aVA.getRuleResourcePath()).errorFieldName("Context[" + i + "]").errorText("The document to be validated is not an XML document").linkedException(ex).build());
}
}
}
// Build result object
return new ValidationResult(aVA, aErrorList.getAllFailures());
}
Aggregations