Search in sources :

Example 1 with VOMSchematronType

use of com.helger.phive.engine.vom.v10.VOMSchematronType in project phive by phax.

the class VOM1Converter method convertToVES_XML.

@Nullable
public ValidationExecutorSet<IValidationSourceXML> convertToVES_XML(@Nonnull final VOMType aVOM, @Nonnull final ErrorList aErrorList) {
    ValueEnforcer.notNull(aVOM, "VOM");
    ValueEnforcer.notNull(aErrorList, "ErrorList");
    if (m_bPerformValidation) {
        VOM1Validator.validate(aVOM, m_aXmlSchemaResolver, m_aNamespaceContextResolver, m_aResourceResolver, m_aComplianceSettings, aErrorList);
        if (aErrorList.containsAtLeastOneError())
            return null;
    }
    // Create stub
    final ValidationExecutorSet<IValidationSourceXML> ret = new ValidationExecutorSet<>(_createVESID(aVOM.getId()), aVOM.getName(), false);
    if (m_aComplianceSettings.isAllowEdifact() && aVOM.getValidation().hasEdifactEntries()) {
        if (aVOM.getValidation().hasXsdEntries())
            throw new IllegalStateException("If Edifact is enabled and present, than no XSD can be present!");
        // Add all Edifacts
        for (final VOMEdifactType aEdifact : aVOM.getValidation().getEdifact()) ret.addExecutor(_createExecutorEdifact(aEdifact));
    } else {
        // Add all XSDs
        for (final VOMXSDType aXsd : aVOM.getValidation().getXsd()) ret.addExecutor(_createExecutorXSD(aXsd));
    }
    // Add all XSDs
    for (final VOMSchematronType aSchematron : aVOM.getValidation().getSchematron()) ret.addExecutor(_createExecutorSchematron(aSchematron));
    return ret;
}
Also used : ValidationExecutorSet(com.helger.phive.api.executorset.ValidationExecutorSet) VOMSchematronType(com.helger.phive.engine.vom.v10.VOMSchematronType) VOMEdifactType(com.helger.phive.engine.vom.v10.VOMEdifactType) IValidationSourceXML(com.helger.phive.engine.source.IValidationSourceXML) VOMXSDType(com.helger.phive.engine.vom.v10.VOMXSDType) Nullable(javax.annotation.Nullable)

Example 2 with VOMSchematronType

use of com.helger.phive.engine.vom.v10.VOMSchematronType in project phive by phax.

the class VOM1Validator method validateValidation.

public static void validateValidation(@Nonnull final String sXPath, @Nullable final VOMValidationType aValidation, @Nonnull final IVOMXmlSchemaResolver aXmlSchemaResolver, @Nonnull final IVOMNamespaceContextResolver aNamespaceContextResolver, @Nonnull final IVOMResourceResolver aResourceResolver, @Nonnull final VOM1ComplianceSettings aSettings, @Nonnull final ErrorList aErrorList) {
    if (aValidation == null) {
        aErrorList.add(_createError(EVOMErrorCode.REQUIRED, sXPath));
    } else {
        // validation element is present
        if (aValidation.getXsdCount() + aValidation.getEdifactCount() + aValidation.getSchematronCount() == 0)
            aErrorList.add(_createError(EVOMErrorCode.NO_VALIDATION_RULES, sXPath));
        else {
            // XSD
            // XML uses 1-based indices
            int nIndex = 1;
            for (final VOMXSDType aXSD : aValidation.getXsd()) {
                validateXSD(sXPath + "/xsd[" + nIndex + "]", aXSD, aXmlSchemaResolver, aErrorList);
                ++nIndex;
            }
            // Edifact
            if (aSettings.isAllowEdifact()) {
                nIndex = 1;
                for (final VOMEdifactType aEdifact : aValidation.getEdifact()) {
                    validateEdifact(sXPath + "/edifact[" + nIndex + "]", aEdifact, aErrorList);
                    ++nIndex;
                }
            } else {
                if (!aValidation.getEdifact().isEmpty())
                    aErrorList.add(_createError(EVOMErrorCode.EDIFACT_NOT_ALLOWED, sXPath));
            }
            // Schematron
            nIndex = 1;
            for (final VOMSchematronType aSchematron : aValidation.getSchematron()) {
                validateSchematron(sXPath + "/schematron[" + nIndex + "]", aSchematron, aNamespaceContextResolver, aResourceResolver, aErrorList);
                ++nIndex;
            }
        }
    }
}
Also used : VOMSchematronType(com.helger.phive.engine.vom.v10.VOMSchematronType) VOMEdifactType(com.helger.phive.engine.vom.v10.VOMEdifactType) VOMXSDType(com.helger.phive.engine.vom.v10.VOMXSDType)

Example 3 with VOMSchematronType

use of com.helger.phive.engine.vom.v10.VOMSchematronType in project phive by phax.

the class VOM1Validator method validateSchematron.

public static void validateSchematron(@Nonnull final String sXPath, @Nullable final VOMSchematronType aSchematron, @Nonnull final IVOMNamespaceContextResolver aNamespaceContextResolver, @Nonnull final IVOMResourceResolver aResourceResolver, @Nonnull final ErrorList aErrorList) {
    if (aSchematron == null) {
        aErrorList.add(_createError(EVOMErrorCode.REQUIRED, sXPath));
    } else {
        final String sBuiltIn = aSchematron.getBuiltIn();
        if (sBuiltIn != null) {
            final IReadableResource aRes = aResourceResolver.getResourceOfID(sBuiltIn);
            if (aRes == null)
                aErrorList.add(_createError(EVOMErrorCode.BUILTIN_RESOURCE_NOT_FOUND.getErrorMessage(sBuiltIn), sXPath + "/builtin"));
        } else {
            // Resource
            validateCoordinates(sXPath + "/resource", aSchematron.getResource(), aErrorList);
        }
        // TODO prerequisite
        validateNamespaces(sXPath + "/namespaces", aSchematron.getNamespaces(), aNamespaceContextResolver, aErrorList);
        // Custom errors
        if (aSchematron.hasCustomErrorEntries()) {
            int nIndex = 1;
            int nErrors = 0;
            final ICommonsSet<String> aIDs = new CommonsHashSet<>();
            for (final VOMCustomError aCustomError : aSchematron.getCustomError()) {
                final String sLocalXPath = sXPath + "/customError[" + nIndex + ']';
                final String sID = aCustomError.getId();
                if (StringHelper.hasNoText(sID)) {
                    aErrorList.add(_createError(EVOMErrorCode.REQUIRED_NOT_EMPTY, sLocalXPath + "/id"));
                    ++nErrors;
                } else {
                    if (aIDs.contains(sID))
                        aErrorList.add(_createWarn(EVOMErrorCode.CUSTOM_ERROR_ALREADY_MAPPED.getErrorMessage(sID), sLocalXPath + "/id"));
                    aIDs.add(sID);
                }
                ++nIndex;
            }
            if (aIDs.isEmpty() && nErrors == 0)
                aErrorList.add(_createWarn(EVOMErrorCode.CUSTOM_ERRORS_EMPTY, sXPath));
        }
        validateOptions(sXPath, "/option", aSchematron.getOption(), aErrorList);
    }
}
Also used : VOMCustomError(com.helger.phive.engine.vom.v10.VOMCustomError) IReadableResource(com.helger.commons.io.resource.IReadableResource) CommonsHashSet(com.helger.commons.collection.impl.CommonsHashSet)

Example 4 with VOMSchematronType

use of com.helger.phive.engine.vom.v10.VOMSchematronType in project phive by phax.

the class VOM1Converter method _createExecutorSchematron.

@Nonnull
private ValidationExecutorSchematron _createExecutorSchematron(@Nonnull final VOMSchematronType aSchematron) {
    final IReadableResource aRes;
    {
        final String sBuiltIn = aSchematron.getBuiltIn();
        if (StringHelper.hasText(sBuiltIn)) {
            LOGGER.info("Trying to resolve built-in Schematron artifact '" + sBuiltIn + "'");
            aRes = m_aResourceResolver.getResourceOfID(sBuiltIn);
            if (aRes == null)
                throw new IllegalStateException("Failed to resolve built-in Schematron artifact '" + sBuiltIn + "'");
        } else {
            // External resource
            final VESID aVESID = _createVESID(aSchematron.getResource());
            LOGGER.info("Trying to resolve Schematron artifact with ID '" + aVESID.getAsSingleID() + "'");
            aRes = m_aArtifactResolver.getArtifactOfID(aVESID);
            if (aRes == null)
                throw new IllegalStateException("Failed to resolve Schematron artifact with ID '" + aVESID.getAsSingleID() + "'");
        }
    }
    final EValidationType eValidationType;
    if (StringHelper.hasNoText(aSchematron.getType()))
        eValidationType = EValidationType.SCHEMATRON_SCH;
    else
        switch(aSchematron.getType()) {
            case "pure":
                eValidationType = EValidationType.SCHEMATRON_PURE;
                break;
            case "sch":
                eValidationType = EValidationType.SCHEMATRON_SCH;
                break;
            case "xslt":
                eValidationType = EValidationType.SCHEMATRON_XSLT;
                break;
            case "schxslt":
                eValidationType = EValidationType.SCHEMATRON_SCHXSLT;
                break;
            case "oioubl":
                eValidationType = EValidationType.SCHEMATRON_OIOUBL;
                break;
            default:
                throw new IllegalStateException("The Schematron type '" + aSchematron.getType() + "' is unsupported.");
        }
    if (aSchematron.getPrerequisiteCount() > 1)
        throw new IllegalStateException("Currently only 1 prerequsite path is supported");
    final MapBasedNamespaceContext aNamespaceContext = new MapBasedNamespaceContext();
    final VOMNamespacesType aNamespaces = aSchematron.getNamespaces();
    if (aNamespaces != null) {
        final String sBuiltIn = aNamespaces.getBuiltIn();
        if (sBuiltIn != null) {
            // Built-in
            LOGGER.info("Trying to resolve built-in namespace context '" + sBuiltIn + "'");
            final MapBasedNamespaceContext ret = m_aNamespaceContextResolver.getNamespaceContextOfID(sBuiltIn);
            if (ret == null)
                throw new IllegalStateException("Failed to resolve built-in namespace context with ID '" + sBuiltIn + "'");
            aNamespaceContext.addMappings(ret);
        } else {
        // Start empty
        }
        for (final VOMNamespaceMappingType aMapping : aNamespaces.getMapping()) {
            // Default to the default namespace prefix
            final String sPrefix = StringHelper.getNotNull(aMapping.getPrefix(), "");
            aNamespaceContext.setMapping(sPrefix, aMapping.getNamespace());
        }
    }
    final ValidationExecutorSchematron ret = new ValidationExecutorSchematron(new ValidationArtefact(eValidationType, aRes), aSchematron.hasPrerequisiteEntries() ? aSchematron.getPrerequisiteAtIndex(0) : null, aNamespaceContext.hasAnyMapping() ? aNamespaceContext : null);
    // Custom errors afterwards (optional)
    for (final VOMCustomError aCE : aSchematron.getCustomError()) ret.addCustomErrorLevel(aCE.getId(), getAsErrorLevel(aCE.getLevel()));
    if (aSchematron.hasOptionEntries())
        LOGGER.warn("Ignoring all Schematron options");
    return ret;
}
Also used : MapBasedNamespaceContext(com.helger.xml.namespace.MapBasedNamespaceContext) VESID(com.helger.phive.api.executorset.VESID) VOMCustomError(com.helger.phive.engine.vom.v10.VOMCustomError) EValidationType(com.helger.phive.api.EValidationType) IReadableResource(com.helger.commons.io.resource.IReadableResource) VOMNamespaceMappingType(com.helger.phive.engine.vom.v10.VOMNamespaceMappingType) ValidationArtefact(com.helger.phive.api.artefact.ValidationArtefact) ValidationExecutorSchematron(com.helger.phive.engine.schematron.ValidationExecutorSchematron) VOMNamespacesType(com.helger.phive.engine.vom.v10.VOMNamespacesType) Nonnull(javax.annotation.Nonnull)

Aggregations

IReadableResource (com.helger.commons.io.resource.IReadableResource)2 VOMCustomError (com.helger.phive.engine.vom.v10.VOMCustomError)2 VOMEdifactType (com.helger.phive.engine.vom.v10.VOMEdifactType)2 VOMSchematronType (com.helger.phive.engine.vom.v10.VOMSchematronType)2 VOMXSDType (com.helger.phive.engine.vom.v10.VOMXSDType)2 CommonsHashSet (com.helger.commons.collection.impl.CommonsHashSet)1 EValidationType (com.helger.phive.api.EValidationType)1 ValidationArtefact (com.helger.phive.api.artefact.ValidationArtefact)1 VESID (com.helger.phive.api.executorset.VESID)1 ValidationExecutorSet (com.helger.phive.api.executorset.ValidationExecutorSet)1 ValidationExecutorSchematron (com.helger.phive.engine.schematron.ValidationExecutorSchematron)1 IValidationSourceXML (com.helger.phive.engine.source.IValidationSourceXML)1 VOMNamespaceMappingType (com.helger.phive.engine.vom.v10.VOMNamespaceMappingType)1 VOMNamespacesType (com.helger.phive.engine.vom.v10.VOMNamespacesType)1 MapBasedNamespaceContext (com.helger.xml.namespace.MapBasedNamespaceContext)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1