Search in sources :

Example 1 with IValidationType

use of com.helger.phive.api.IValidationType 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 IValidationType

use of com.helger.phive.api.IValidationType in project phive by phax.

the class ValidationExecutorSchematron method _createSchematronResource.

@Nonnull
private AbstractSchematronResource _createSchematronResource(@Nullable final Locale aLocale, @Nonnull final ErrorList aErrorList, @Nonnull final Consumer<ESchematronOutput> aSpecialOutputHdl) {
    final IValidationArtefact aArtefact = getValidationArtefact();
    // get the Schematron resource to be used for this validation artefact
    final IReadableResource aSCHRes = aArtefact.getRuleResource();
    final IValidationType aVT = aArtefact.getValidationArtefactType();
    if (aVT == EValidationType.SCHEMATRON_PURE) {
        final SchematronResourcePure aPureSCH = new SchematronResourcePure(aSCHRes);
        aPureSCH.setErrorHandler(new WrappedCollectingPSErrorHandler(aErrorList));
        // consecutive calls!
        return aPureSCH;
    }
    if (aVT == EValidationType.SCHEMATRON_SCH) {
        final SchematronResourceSCH aSCHSCH = new SchematronResourceSCH(aSCHRes);
        aSCHSCH.setErrorListener(new WrappedCollectingTransformErrorListener(aErrorList));
        if (aLocale != null && StringHelper.hasText(aLocale.getLanguage()))
            aSCHSCH.setLanguageCode(aLocale.getLanguage());
        return aSCHSCH;
    }
    if (aVT == EValidationType.SCHEMATRON_SCHXSLT) {
        final SchematronResourceSchXslt_XSLT2 aSCHSCH = new SchematronResourceSchXslt_XSLT2(aSCHRes);
        aSCHSCH.setErrorListener(new WrappedCollectingTransformErrorListener(aErrorList));
        if (aLocale != null && StringHelper.hasText(aLocale.getLanguage()))
            aSCHSCH.setLanguageCode(aLocale.getLanguage());
        return aSCHSCH;
    }
    if (aVT == EValidationType.SCHEMATRON_XSLT) {
        final SchematronResourceXSLT aSCHXSLT = new SchematronResourceXSLT(aSCHRes);
        aSCHXSLT.setErrorListener(new WrappedCollectingTransformErrorListener(aErrorList));
        return aSCHXSLT;
    }
    if (aVT == EValidationType.SCHEMATRON_OIOUBL) {
        final SchematronResourceXSLT aSCHXSLT = new SchematronResourceXSLT(aSCHRes);
        aSCHXSLT.setErrorListener(new WrappedCollectingTransformErrorListener(aErrorList));
        // Special output layout
        aSpecialOutputHdl.accept(ESchematronOutput.OIOUBL);
        return aSCHXSLT;
    }
    throw new IllegalStateException("Unsupported Schematron validation type: " + aVT);
}
Also used : IValidationType(com.helger.phive.api.IValidationType) SchematronResourceXSLT(com.helger.schematron.xslt.SchematronResourceXSLT) SchematronResourceSCH(com.helger.schematron.sch.SchematronResourceSCH) IValidationArtefact(com.helger.phive.api.artefact.IValidationArtefact) SchematronResourceSchXslt_XSLT2(com.helger.schematron.schxslt.xslt2.SchematronResourceSchXslt_XSLT2) IReadableResource(com.helger.commons.io.resource.IReadableResource) WrappedCollectingTransformErrorListener(com.helger.xml.transform.WrappedCollectingTransformErrorListener) WrappedCollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.WrappedCollectingPSErrorHandler) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure) Nonnull(javax.annotation.Nonnull)

Aggregations

IReadableResource (com.helger.commons.io.resource.IReadableResource)2 IValidationType (com.helger.phive.api.IValidationType)2 IError (com.helger.commons.error.IError)1 ErrorList (com.helger.commons.error.list.ErrorList)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 IValidationArtefact (com.helger.phive.api.artefact.IValidationArtefact)1 ValidationArtefact (com.helger.phive.api.artefact.ValidationArtefact)1 ValidationResult (com.helger.phive.api.result.ValidationResult)1 ValidationResultList (com.helger.phive.api.result.ValidationResultList)1 SchematronResourcePure (com.helger.schematron.pure.SchematronResourcePure)1 WrappedCollectingPSErrorHandler (com.helger.schematron.pure.errorhandler.WrappedCollectingPSErrorHandler)1 SchematronResourceSCH (com.helger.schematron.sch.SchematronResourceSCH)1 SchematronResourceSchXslt_XSLT2 (com.helger.schematron.schxslt.xslt2.SchematronResourceSchXslt_XSLT2)1 SchematronResourceXSLT (com.helger.schematron.xslt.SchematronResourceXSLT)1 WrappedCollectingTransformErrorListener (com.helger.xml.transform.WrappedCollectingTransformErrorListener)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1