Search in sources :

Example 1 with ValidationArtefact

use of com.helger.phive.api.artefact.ValidationArtefact in project phive by phax.

the class PhiveJsonHelper method getAsValidationResultList.

/**
 * Try to parse a JSON structure and convert it back to a
 * {@link ValidationResultList}.
 *
 * @param aJson
 *        The JSON to be read. May be <code>null</code>.
 * @param aValidationTypeResolver
 *        The validation type resolver to be used. May not be
 *        <code>null</code>.
 * @return <code>null</code> in case reverse operation fails.
 */
@Nullable
public static ValidationResultList getAsValidationResultList(@Nullable final IJsonObject aJson, @Nonnull final Function<String, IValidationType> aValidationTypeResolver) {
    ValueEnforcer.notNull(aValidationTypeResolver, "ValidationTypeResolver");
    if (aJson == null)
        return null;
    final IJsonArray aResults = aJson.getAsArray(JSON_RESULTS);
    if (aResults == null)
        return null;
    final ValidationResultList ret = new ValidationResultList();
    for (final IJson aResult : aResults) {
        final IJsonObject aResultObj = aResult.getAsObject();
        if (aResultObj != null) {
            final String sSuccess = aResultObj.getAsString(JSON_SUCCESS);
            final ETriState eSuccess = getAsTriState(sSuccess);
            if (eSuccess == null) {
                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("Failed to resolve TriState '" + sSuccess + "'");
                continue;
            }
            final String sValidationType = aResultObj.getAsString(JSON_ARTIFACT_TYPE);
            final IValidationType aValidationType = aValidationTypeResolver.apply(sValidationType);
            if (aValidationType == null) {
                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("Failed to resolve ValidationType '" + sValidationType + "'");
                continue;
            }
            final String sArtefactPathType = aResultObj.getAsString(JSON_ARTIFACT_PATH_TYPE);
            final String sArtefactPath = aResultObj.getAsString(JSON_ARTIFACT_PATH);
            final IReadableResource aRes = getAsValidationResource(sArtefactPathType, sArtefactPath);
            if (aRes == null) {
                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("Failed to resolve ValidationArtefact '" + sArtefactPathType + "' with path '" + sArtefactPath + "'");
                continue;
            }
            final ValidationArtefact aVA = new ValidationArtefact(aValidationType, aRes);
            if (eSuccess.isUndefined()) {
                // Ignored level
                ret.add(ValidationResult.createIgnoredResult(aVA));
            } else {
                // We have results
                final IJsonArray aItems = aResultObj.getAsArray(JSON_ITEMS);
                final ErrorList aErrorList = new ErrorList();
                for (final IJson aItem : aItems) {
                    final IJsonObject aItemObj = aItem.getAsObject();
                    if (aItemObj != null) {
                        final IError aError = getAsIError(aItemObj);
                        aErrorList.add(aError);
                    }
                }
                final ValidationResult aVR = new ValidationResult(aVA, aErrorList);
                ret.add(aVR);
            }
        }
    }
    return ret;
}
Also used : IValidationType(com.helger.phive.api.IValidationType) ETriState(com.helger.commons.state.ETriState) ErrorList(com.helger.commons.error.list.ErrorList) IJsonObject(com.helger.json.IJsonObject) ValidationResultList(com.helger.phive.api.result.ValidationResultList) IReadableResource(com.helger.commons.io.resource.IReadableResource) ValidationArtefact(com.helger.phive.api.artefact.ValidationArtefact) IJson(com.helger.json.IJson) IJsonArray(com.helger.json.IJsonArray) IError(com.helger.commons.error.IError) ValidationResult(com.helger.phive.api.result.ValidationResult) Nullable(javax.annotation.Nullable)

Example 2 with ValidationArtefact

use of com.helger.phive.api.artefact.ValidationArtefact 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)

Example 3 with ValidationArtefact

use of com.helger.phive.api.artefact.ValidationArtefact in project phive by phax.

the class VOM1Converter method _createExecutorXSD.

@Nonnull
private ValidationExecutorXSD _createExecutorXSD(@Nonnull final VOMXSDType aXsd) {
    final ValidationExecutorXSD ret;
    final String sBuiltIn = aXsd.getBuiltIn();
    if (StringHelper.hasText(sBuiltIn)) {
        LOGGER.info("Trying to resolve built-in XSD artifact '" + sBuiltIn + "'");
        final Schema aSchema = m_aXmlSchemaResolver.getXmlSchemaOfID(sBuiltIn);
        if (aSchema == null)
            throw new IllegalStateException("Failed to resolve built-in XSD artifact '" + sBuiltIn + "'");
        ret = new ValidationExecutorXSD(new ValidationArtefact(EValidationType.XSD, new ReadableResourceByteArray("built-in-" + sBuiltIn, ArrayHelper.EMPTY_BYTE_ARRAY)), () -> aSchema);
    } else {
        // External resource
        final VESID aVESID = _createVESID(aXsd.getResource());
        LOGGER.info("Trying to resolve XSD artifact with ID '" + aVESID.getAsSingleID() + "'");
        final IReadableResource aRes = m_aArtifactResolver.getArtifactOfID(aVESID);
        if (aRes == null)
            throw new IllegalStateException("Failed to resolve XSD artifact with ID '" + aVESID.getAsSingleID() + "'");
        ret = ValidationExecutorXSD.create(aRes);
    }
    if (aXsd.hasOptionEntries())
        LOGGER.warn("Ignoring all XSD options");
    return ret;
}
Also used : VESID(com.helger.phive.api.executorset.VESID) Schema(javax.xml.validation.Schema) ValidationArtefact(com.helger.phive.api.artefact.ValidationArtefact) IReadableResource(com.helger.commons.io.resource.IReadableResource) ValidationExecutorXSD(com.helger.phive.engine.xsd.ValidationExecutorXSD) ReadableResourceByteArray(com.helger.commons.io.resource.inmemory.ReadableResourceByteArray) Nonnull(javax.annotation.Nonnull)

Aggregations

IReadableResource (com.helger.commons.io.resource.IReadableResource)3 ValidationArtefact (com.helger.phive.api.artefact.ValidationArtefact)3 VESID (com.helger.phive.api.executorset.VESID)2 Nonnull (javax.annotation.Nonnull)2 IError (com.helger.commons.error.IError)1 ErrorList (com.helger.commons.error.list.ErrorList)1 ReadableResourceByteArray (com.helger.commons.io.resource.inmemory.ReadableResourceByteArray)1 ETriState (com.helger.commons.state.ETriState)1 IJson (com.helger.json.IJson)1 IJsonArray (com.helger.json.IJsonArray)1 IJsonObject (com.helger.json.IJsonObject)1 EValidationType (com.helger.phive.api.EValidationType)1 IValidationType (com.helger.phive.api.IValidationType)1 ValidationResult (com.helger.phive.api.result.ValidationResult)1 ValidationResultList (com.helger.phive.api.result.ValidationResultList)1 ValidationExecutorSchematron (com.helger.phive.engine.schematron.ValidationExecutorSchematron)1 VOMCustomError (com.helger.phive.engine.vom.v10.VOMCustomError)1 VOMNamespaceMappingType (com.helger.phive.engine.vom.v10.VOMNamespaceMappingType)1 VOMNamespacesType (com.helger.phive.engine.vom.v10.VOMNamespacesType)1 ValidationExecutorXSD (com.helger.phive.engine.xsd.ValidationExecutorXSD)1