Search in sources :

Example 1 with ValidationSourceXML

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());
}
Also used : ErrorList(com.helger.commons.error.list.ErrorList) IValidationArtefact(com.helger.phive.api.artefact.IValidationArtefact) XPathExpressionException(javax.xml.xpath.XPathExpressionException) SAXParseException(org.xml.sax.SAXParseException) NodeList(org.w3c.dom.NodeList) Schema(javax.xml.validation.Schema) ValidationSourceXML(com.helger.phive.engine.source.ValidationSourceXML) IValidationSourceXML(com.helger.phive.engine.source.IValidationSourceXML) IValidationSourceXML(com.helger.phive.engine.source.IValidationSourceXML) ValidationResult(com.helger.phive.api.result.ValidationResult) Nonnull(javax.annotation.Nonnull)

Aggregations

ErrorList (com.helger.commons.error.list.ErrorList)1 IValidationArtefact (com.helger.phive.api.artefact.IValidationArtefact)1 ValidationResult (com.helger.phive.api.result.ValidationResult)1 IValidationSourceXML (com.helger.phive.engine.source.IValidationSourceXML)1 ValidationSourceXML (com.helger.phive.engine.source.ValidationSourceXML)1 Nonnull (javax.annotation.Nonnull)1 Schema (javax.xml.validation.Schema)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 NodeList (org.w3c.dom.NodeList)1 SAXParseException (org.xml.sax.SAXParseException)1