Search in sources :

Example 1 with DefaultSVRLErrorLevelDeterminator

use of com.helger.schematron.svrl.DefaultSVRLErrorLevelDeterminator in project ph-schematron by phax.

the class Schematron method execute.

@Override
public void execute() throws BuildException {
    boolean bCanRun = false;
    if (m_aSchematronFile == null)
        _errorOrFail("No Schematron file specified!");
    else if (m_aSchematronFile.exists() && !m_aSchematronFile.isFile())
        _errorOrFail("The specified Schematron file " + m_aSchematronFile + " is not a file!");
    else if (m_eSchematronProcessingEngine == null)
        _errorOrFail("An invalid Schematron processing instance is specified! Only one of the following values is allowed: " + StringHelper.getImplodedMapped(", ", ESchematronMode.values(), x -> "'" + x.getID() + "'"));
    else if (m_aResCollections.isEmpty())
        _errorOrFail("No XML resources to be validated specified! Add e.g. a <fileset> element.");
    else if (m_aSvrlDirectory != null && !m_aSvrlDirectory.exists() && !m_aSvrlDirectory.mkdirs())
        _errorOrFail("Failed to create the SVRL directory " + m_aSvrlDirectory);
    else
        bCanRun = true;
    if (bCanRun) {
        // Set error level
        if (m_aErrorRoles.isNotEmpty()) {
            // Set global default error level determinator
            SVRLHelper.setErrorLevelDeterminator(new DefaultSVRLErrorLevelDeterminator() {

                @Override
                @Nonnull
                public IErrorLevel getErrorLevelFromString(@Nullable final String sFlag) {
                    if (sFlag != null) {
                        // Check custom error roles; #66
                        for (final Schematron.ErrorRole aCustomRole : m_aErrorRoles) if (aCustomRole.equalsIgnoreCase(sFlag))
                            return EErrorLevel.ERROR;
                    }
                    // Fall back to default
                    return super.getErrorLevelFromString(sFlag);
                }
            });
        }
        // 1. Parse Schematron file
        final Locale aDisplayLocale = Locale.US;
        ISchematronResource aSch = null;
        IErrorList aSCHErrors = null;
        switch(m_eSchematronProcessingEngine) {
            case PURE:
                {
                    // pure
                    final CollectingPSErrorHandler aErrorHdl = new CollectingPSErrorHandler();
                    final SchematronResourcePure aRealSCH = new SchematronResourcePure(new FileSystemResource(m_aSchematronFile));
                    aRealSCH.setPhase(m_sPhaseName);
                    aRealSCH.setErrorHandler(aErrorHdl);
                    aRealSCH.setEntityResolver(getEntityResolver());
                    aRealSCH.validateCompletely();
                    aSch = aRealSCH;
                    aSCHErrors = aErrorHdl.getAllErrors();
                    break;
                }
            case SCHEMATRON:
                {
                    // SCH
                    final IStringMap aParams = new StringMap();
                    m_aParameters.forEach(x -> x.addToMap(aParams));
                    if (aParams.isNotEmpty())
                        _info("Using the following custom parameters: " + aParams);
                    final CollectingTransformErrorListener aErrorHdl = new CollectingTransformErrorListener();
                    final SchematronResourceSCH aRealSCH = new SchematronResourceSCH(new FileSystemResource(m_aSchematronFile));
                    aRealSCH.setPhase(m_sPhaseName);
                    aRealSCH.setLanguageCode(m_sLanguageCode);
                    aRealSCH.setForceCacheResult(m_bForceCacheResult);
                    aRealSCH.setErrorListener(aErrorHdl);
                    aRealSCH.setURIResolver(getURIResolver());
                    aRealSCH.setEntityResolver(getEntityResolver());
                    aRealSCH.parameters().setAll(aParams);
                    aRealSCH.isValidSchematron();
                    aSch = aRealSCH;
                    aSCHErrors = aErrorHdl.getErrorList();
                    break;
                }
            case SCHXSLT_XSLT2:
                {
                    // SchXslt
                    final IStringMap aParams = new StringMap();
                    m_aParameters.forEach(x -> x.addToMap(aParams));
                    if (aParams.isNotEmpty())
                        _info("Using the following custom parameters: " + aParams);
                    final CollectingTransformErrorListener aErrorHdl = new CollectingTransformErrorListener();
                    final SchematronResourceSchXslt_XSLT2 aRealSCH = new SchematronResourceSchXslt_XSLT2(new FileSystemResource(m_aSchematronFile));
                    aRealSCH.setPhase(m_sPhaseName);
                    aRealSCH.setLanguageCode(m_sLanguageCode);
                    aRealSCH.setForceCacheResult(m_bForceCacheResult);
                    aRealSCH.setErrorListener(aErrorHdl);
                    aRealSCH.setURIResolver(getURIResolver());
                    aRealSCH.setEntityResolver(getEntityResolver());
                    aRealSCH.parameters().setAll(aParams);
                    aRealSCH.isValidSchematron();
                    aSch = aRealSCH;
                    aSCHErrors = aErrorHdl.getErrorList();
                    break;
                }
            case XSLT:
                {
                    // XSLT
                    final IStringMap aParams = new StringMap();
                    m_aParameters.forEach(x -> x.addToMap(aParams));
                    if (aParams.isNotEmpty())
                        _info("Using the following custom parameters: " + aParams);
                    final CollectingTransformErrorListener aErrorHdl = new CollectingTransformErrorListener();
                    final SchematronResourceXSLT aRealSCH = new SchematronResourceXSLT(new FileSystemResource(m_aSchematronFile));
                    // phase and language are ignored because this was decided when the
                    // XSLT was created
                    aRealSCH.setErrorListener(aErrorHdl);
                    aRealSCH.setURIResolver(getURIResolver());
                    aRealSCH.setEntityResolver(getEntityResolver());
                    aRealSCH.parameters().setAll(aParams);
                    aRealSCH.isValidSchematron();
                    aSch = aRealSCH;
                    aSCHErrors = aErrorHdl.getErrorList();
                    break;
                }
            default:
                _errorOrFail("No handler for processing engine '" + m_eSchematronProcessingEngine + "'");
                break;
        }
        boolean bAnyParsingError = false;
        if (aSCHErrors != null)
            // Error validating the Schematrons!!
            for (final IError aError : aSCHErrors) if (aError.getErrorLevel().isGE(EErrorLevel.ERROR)) {
                _error("Error in Schematron: " + aError.getAsString(aDisplayLocale));
                bAnyParsingError = true;
            } else if (aError.getErrorLevel().isGE(EErrorLevel.WARN))
                _warn("Warning in Schematron: " + aError.getAsString(aDisplayLocale));
            else
                _info("Information in Schematron: " + aError.getAsString(aDisplayLocale));
        if (bAnyParsingError)
            _errorOrFail("The provided Schematron file contains errors. See log for details.");
        else {
            // Schematron is okay
            _info("Successfully parsed Schematron file '" + m_aSchematronFile.getPath() + "'");
            // Start validation
            _performValidation(aSch, m_aResCollections, m_aSvrlDirectory, m_bExpectSuccess);
        }
    }
}
Also used : FileResource(org.apache.tools.ant.types.resources.FileResource) SchematronResourceXSLT(com.helger.schematron.xslt.SchematronResourceXSLT) ESchematronMode(com.helger.schematron.ESchematronMode) Locale(java.util.Locale) Map(java.util.Map) TransformSourceFactory(com.helger.xml.transform.TransformSourceFactory) EntityResolver(org.xml.sax.EntityResolver) ErrorTextProvider(com.helger.commons.error.ErrorTextProvider) DefaultSVRLErrorLevelDeterminator(com.helger.schematron.svrl.DefaultSVRLErrorLevelDeterminator) Resource(org.apache.tools.ant.types.Resource) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) SVRLResourceError(com.helger.schematron.svrl.SVRLResourceError) Serializable(java.io.Serializable) ISchematronResource(com.helger.schematron.ISchematronResource) SchematronResourceSchXslt_XSLT2(com.helger.schematron.schxslt.xslt2.SchematronResourceSchXslt_XSLT2) ICommonsList(com.helger.commons.collection.impl.ICommonsList) FileProvider(org.apache.tools.ant.types.resources.FileProvider) ICommonsMap(com.helger.commons.collection.impl.ICommonsMap) IErrorList(com.helger.commons.error.list.IErrorList) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) OverrideOnDemand(com.helger.commons.annotation.OverrideOnDemand) SchematronResourceSCH(com.helger.schematron.sch.SchematronResourceSCH) IError(com.helger.commons.error.IError) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) URIResolver(javax.xml.transform.URIResolver) UsedViaReflection(com.helger.commons.annotation.UsedViaReflection) ResourceCollection(org.apache.tools.ant.types.ResourceCollection) StringMap(com.helger.commons.collection.attr.StringMap) AbstractSVRLMessage(com.helger.schematron.svrl.AbstractSVRLMessage) TransformerCustomizerSCH(com.helger.schematron.sch.TransformerCustomizerSCH) SVRLHelper(com.helger.schematron.svrl.SVRLHelper) IErrorLevel(com.helger.commons.error.level.IErrorLevel) CollectingTransformErrorListener(com.helger.xml.transform.CollectingTransformErrorListener) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) SVRLNamespaceContext(com.helger.schematron.svrl.SVRLNamespaceContext) EErrorLevel(com.helger.commons.error.level.EErrorLevel) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) FileOperations(com.helger.commons.io.file.FileOperations) StringHelper(com.helger.commons.string.StringHelper) CollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) XMLCatalog(org.apache.tools.ant.types.XMLCatalog) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) SchematronOutputType(com.helger.schematron.svrl.jaxb.SchematronOutputType) IStringMap(com.helger.commons.collection.attr.IStringMap) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure) ResourceUtils(org.apache.tools.ant.util.ResourceUtils) Locale(java.util.Locale) StringMap(com.helger.commons.collection.attr.StringMap) IStringMap(com.helger.commons.collection.attr.IStringMap) ISchematronResource(com.helger.schematron.ISchematronResource) Nonnull(javax.annotation.Nonnull) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) CollectingTransformErrorListener(com.helger.xml.transform.CollectingTransformErrorListener) IError(com.helger.commons.error.IError) SchematronResourceXSLT(com.helger.schematron.xslt.SchematronResourceXSLT) SchematronResourceSCH(com.helger.schematron.sch.SchematronResourceSCH) SchematronResourceSchXslt_XSLT2(com.helger.schematron.schxslt.xslt2.SchematronResourceSchXslt_XSLT2) IStringMap(com.helger.commons.collection.attr.IStringMap) IErrorLevel(com.helger.commons.error.level.IErrorLevel) IErrorList(com.helger.commons.error.list.IErrorList) DefaultSVRLErrorLevelDeterminator(com.helger.schematron.svrl.DefaultSVRLErrorLevelDeterminator) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure) CollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler)

Aggregations

OverrideOnDemand (com.helger.commons.annotation.OverrideOnDemand)1 UsedViaReflection (com.helger.commons.annotation.UsedViaReflection)1 IStringMap (com.helger.commons.collection.attr.IStringMap)1 StringMap (com.helger.commons.collection.attr.StringMap)1 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 CommonsHashMap (com.helger.commons.collection.impl.CommonsHashMap)1 ICommonsList (com.helger.commons.collection.impl.ICommonsList)1 ICommonsMap (com.helger.commons.collection.impl.ICommonsMap)1 ErrorTextProvider (com.helger.commons.error.ErrorTextProvider)1 IError (com.helger.commons.error.IError)1 EErrorLevel (com.helger.commons.error.level.EErrorLevel)1 IErrorLevel (com.helger.commons.error.level.IErrorLevel)1 IErrorList (com.helger.commons.error.list.IErrorList)1 FileOperations (com.helger.commons.io.file.FileOperations)1 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)1 StringHelper (com.helger.commons.string.StringHelper)1 ESchematronMode (com.helger.schematron.ESchematronMode)1 ISchematronResource (com.helger.schematron.ISchematronResource)1 SchematronResourcePure (com.helger.schematron.pure.SchematronResourcePure)1 CollectingPSErrorHandler (com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler)1