Search in sources :

Example 1 with IError

use of com.helger.commons.error.IError in project ph-schematron by phax.

the class Schematron method execute.

@Override
public void execute() throws BuildException {
    boolean bCanRun = false;
    if (m_aSchematronFile == null)
        _error("No Schematron file specified!");
    else if (m_aSchematronFile.exists() && !m_aSchematronFile.isFile())
        _error("The specified Schematron file " + m_aSchematronFile + " is not a file!");
    else if (m_eSchematronProcessingEngine == null)
        _error("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())
        _error("No XML resources to be validated specified! Add e.g. a <fileset> element.");
    else if (m_aSvrlDirectory != null && !m_aSvrlDirectory.exists() && !m_aSvrlDirectory.mkdirs())
        _error("Failed to create the SVRL directory " + m_aSvrlDirectory);
    else
        bCanRun = true;
    if (bCanRun) {
        // 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 CollectingTransformErrorListener aErrorHdl = new CollectingTransformErrorListener();
                    final SchematronResourceSCH aRealSCH = new SchematronResourceSCH(new FileSystemResource(m_aSchematronFile));
                    aRealSCH.setPhase(m_sPhaseName);
                    aRealSCH.setLanguageCode(m_sLanguageCode);
                    aRealSCH.setErrorListener(aErrorHdl);
                    aRealSCH.setURIResolver(getURIResolver());
                    aRealSCH.setEntityResolver(getEntityResolver());
                    aRealSCH.isValidSchematron();
                    aSch = aRealSCH;
                    aSCHErrors = aErrorHdl.getErrorList();
                    break;
                }
            case XSLT:
                {
                    // SCH
                    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.isValidSchematron();
                    aSch = aRealSCH;
                    aSCHErrors = aErrorHdl.getErrorList();
                    break;
                }
            default:
                _error("No handler for processing engine '" + m_eSchematronProcessingEngine + "'");
                break;
        }
        if (aSCHErrors != null) {
            // Error validating the Schematrons!!
            boolean bAnyParsingError = false;
            for (final IError aError : aSCHErrors) if (aError.getErrorLevel().isGE(EErrorLevel.ERROR)) {
                log("Error in Schematron: " + aError.getAsString(aDisplayLocale), Project.MSG_ERR);
                bAnyParsingError = true;
            } else if (aError.getErrorLevel().isGE(EErrorLevel.WARN))
                log("Warning in Schematron: " + aError.getAsString(aDisplayLocale), Project.MSG_WARN);
            if (bAnyParsingError)
                _error("The provided Schematron file contains errors. See log for details.");
            else {
                log("Successfully parsed Schematron file '" + m_aSchematronFile.getPath() + "'", Project.MSG_INFO);
                // 2. for all XML files that match the pattern
                _performValidation(aSch, m_aResCollections, m_aSvrlDirectory, m_bExpectSuccess);
            }
        }
    }
}
Also used : OverrideOnDemand(com.helger.commons.annotation.OverrideOnDemand) Task(org.apache.tools.ant.Task) IError(com.helger.commons.error.IError) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) URIResolver(javax.xml.transform.URIResolver) ResourceCollection(org.apache.tools.ant.types.ResourceCollection) FileResource(org.apache.tools.ant.types.resources.FileResource) SchematronResourceXSLT(com.helger.schematron.xslt.SchematronResourceXSLT) AbstractSVRLMessage(com.helger.schematron.svrl.AbstractSVRLMessage) SVRLHelper(com.helger.schematron.svrl.SVRLHelper) ESchematronMode(com.helger.schematron.ESchematronMode) Locale(java.util.Locale) Project(org.apache.tools.ant.Project) CollectingTransformErrorListener(com.helger.xml.transform.CollectingTransformErrorListener) Nonnull(javax.annotation.Nonnull) TransformSourceFactory(com.helger.xml.transform.TransformSourceFactory) Nullable(javax.annotation.Nullable) EntityResolver(org.xml.sax.EntityResolver) ErrorTextProvider(com.helger.commons.error.ErrorTextProvider) Resource(org.apache.tools.ant.types.Resource) EErrorLevel(com.helger.commons.error.level.EErrorLevel) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) StringHelper(com.helger.commons.string.StringHelper) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) CollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler) BuildException(org.apache.tools.ant.BuildException) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) File(java.io.File) SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) XMLCatalog(org.apache.tools.ant.types.XMLCatalog) ISchematronResource(com.helger.schematron.ISchematronResource) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) ICommonsList(com.helger.commons.collection.impl.ICommonsList) SchematronResourceSCH(com.helger.schematron.xslt.SchematronResourceSCH) FileProvider(org.apache.tools.ant.types.resources.FileProvider) ICommonsMap(com.helger.commons.collection.impl.ICommonsMap) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure) IErrorList(com.helger.commons.error.list.IErrorList) ResourceUtils(org.apache.tools.ant.util.ResourceUtils) Locale(java.util.Locale) SchematronResourceXSLT(com.helger.schematron.xslt.SchematronResourceXSLT) ISchematronResource(com.helger.schematron.ISchematronResource) SchematronResourceSCH(com.helger.schematron.xslt.SchematronResourceSCH) IErrorList(com.helger.commons.error.list.IErrorList) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) CollectingTransformErrorListener(com.helger.xml.transform.CollectingTransformErrorListener) IError(com.helger.commons.error.IError) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure) CollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler)

Example 2 with IError

use of com.helger.commons.error.IError in project ph-schematron by phax.

the class SchematronResourceXSLTCache method createSchematronXSLTProvider.

@Nullable
public static SchematronProviderXSLTPrebuild createSchematronXSLTProvider(@Nonnull final IReadableResource aXSLTResource, @Nullable final ErrorListener aCustomErrorListener, @Nullable final URIResolver aCustomURIResolver) {
    if (s_aLogger.isInfoEnabled())
        s_aLogger.info("Compiling XSLT instance " + aXSLTResource.toString());
    final CollectingTransformErrorListener aCEH = new CollectingTransformErrorListener();
    final SchematronProviderXSLTPrebuild aXSLTPreprocessor = new SchematronProviderXSLTPrebuild(aXSLTResource, aCEH.andThen(aCustomErrorListener != null ? aCustomErrorListener : new LoggingTransformErrorListener(Locale.US)), aCustomURIResolver);
    if (!aXSLTPreprocessor.isValidSchematron()) {
        // Schematron is invalid -> parsing failed
        s_aLogger.warn("The XSLT resource '" + aXSLTResource.getResourceID() + "' is invalid!");
        for (final IError aError : aCEH.getErrorList()) s_aLogger.warn("  " + aError.getAsString(Locale.US));
        return null;
    }
    // If it is a valid schematron, there must be a result XSLT present!
    if (aXSLTPreprocessor.getXSLTDocument() == null) {
        // leave the check anyway.
        throw new IllegalStateException("No XSLT document retrieved from XSLT resource '" + aXSLTResource.getResourceID() + "'!");
    }
    // Create the main validator for the schematron
    return aXSLTPreprocessor;
}
Also used : LoggingTransformErrorListener(com.helger.xml.transform.LoggingTransformErrorListener) CollectingTransformErrorListener(com.helger.xml.transform.CollectingTransformErrorListener) IError(com.helger.commons.error.IError) Nullable(javax.annotation.Nullable)

Example 3 with IError

use of com.helger.commons.error.IError in project ph-schematron by phax.

the class SchematronValidationMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    StaticLoggerBinder.getSingleton().setMavenLog(getLog());
    if (m_aSchematronFile == null)
        throw new MojoExecutionException("No Schematron file specified!");
    if (m_aSchematronFile.exists() && !m_aSchematronFile.isFile())
        throw new MojoExecutionException("The specified Schematron file " + m_aSchematronFile + " is not a file!");
    if (m_sSchematronProcessingEngine == null)
        throw new MojoExecutionException("An invalid Schematron processing instance is specified! Only one of the following values is allowed: " + StringHelper.getImplodedMapped(", ", ESchematronMode.values(), x -> "'" + x.getID() + "'"));
    if (m_aXmlDirectory == null && m_aXmlErrorDirectory == null)
        throw new MojoExecutionException("No XML directory specified - positive or negative directory must be present!");
    if (m_aXmlDirectory != null) {
        if (m_aXmlDirectory.exists() && !m_aXmlDirectory.isDirectory())
            throw new MojoExecutionException("The specified XML directory " + m_aXmlDirectory + " is not a directory!");
        if (StringHelper.hasNoText(m_sXmlIncludes))
            throw new MojoExecutionException("No XML include pattern specified!");
        if (m_aSvrlDirectory != null) {
            if (!m_aSvrlDirectory.exists() && !m_aSvrlDirectory.mkdirs())
                throw new MojoExecutionException("Failed to create the SVRL directory " + m_aSvrlDirectory);
        }
    }
    if (m_aXmlErrorDirectory != null) {
        if (m_aXmlErrorDirectory.exists() && !m_aXmlErrorDirectory.isDirectory())
            throw new MojoExecutionException("The specified erroneous XML directory " + m_aXmlErrorDirectory + " is not a directory!");
        if (StringHelper.hasNoText(m_sXmlErrorIncludes))
            throw new MojoExecutionException("No erroneous XML include pattern specified!");
        if (m_aSvrlErrorDirectory != null) {
            if (!m_aSvrlErrorDirectory.exists() && !m_aSvrlErrorDirectory.mkdirs())
                throw new MojoExecutionException("Failed to create the erroneous SVRL directory " + m_aSvrlErrorDirectory);
        }
    }
    // 1. Parse Schematron file
    final Locale aDisplayLocale = Locale.US;
    ISchematronResource aSch;
    IErrorList aSCHErrors;
    switch(ESchematronMode.getFromIDOrNull(m_sSchematronProcessingEngine)) {
        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.validateCompletely();
                aSch = aRealSCH;
                aSCHErrors = aErrorHdl.getAllErrors();
                break;
            }
        case SCHEMATRON:
            {
                // SCH
                final CollectingTransformErrorListener aErrorHdl = new CollectingTransformErrorListener();
                final SchematronResourceSCH aRealSCH = new SchematronResourceSCH(new FileSystemResource(m_aSchematronFile));
                aRealSCH.setPhase(m_sPhaseName);
                aRealSCH.setLanguageCode(languageCode);
                aRealSCH.setErrorListener(aErrorHdl);
                aRealSCH.isValidSchematron();
                aSch = aRealSCH;
                aSCHErrors = aErrorHdl.getErrorList();
                break;
            }
        case XSLT:
            {
                // SCH
                final CollectingTransformErrorListener aErrorHdl = new CollectingTransformErrorListener();
                final SchematronResourceXSLT aRealSCH = new SchematronResourceXSLT(new FileSystemResource(m_aSchematronFile));
                // phase is ignored
                aRealSCH.setErrorListener(aErrorHdl);
                aRealSCH.isValidSchematron();
                aSch = aRealSCH;
                aSCHErrors = aErrorHdl.getErrorList();
                break;
            }
        default:
            throw new MojoExecutionException("No handler for processing engine '" + m_sSchematronProcessingEngine + "'");
    }
    if (aSCHErrors != null) {
        // Error validating the Schematrons!!
        boolean bAnyError = false;
        for (final IError aError : aSCHErrors) if (aError.getErrorLevel().isGE(EErrorLevel.ERROR)) {
            getLog().error("Error in Schematron: " + aError.getAsString(aDisplayLocale));
            bAnyError = true;
        } else if (aError.getErrorLevel().isGE(EErrorLevel.WARN))
            getLog().warn("Warning in Schematron: " + aError.getAsString(aDisplayLocale));
        if (bAnyError)
            throw new MojoExecutionException("The provided Schematron file contains errors. See log for details.");
    }
    getLog().info("Successfully parsed Schematron file '" + m_aSchematronFile.getPath() + "'");
    // 2. for all XML files that match the pattern
    if (m_aXmlDirectory != null)
        _performValidation(aSch, m_aXmlDirectory, m_sXmlIncludes, m_sXmlExcludes, m_aSvrlDirectory, true);
    if (m_aXmlErrorDirectory != null)
        _performValidation(aSch, m_aXmlErrorDirectory, m_sXmlErrorIncludes, m_sXmlErrorExcludes, m_aSvrlErrorDirectory, false);
}
Also used : BuildContext(org.sonatype.plexus.build.incremental.BuildContext) IError(com.helger.commons.error.IError) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) Component(org.apache.maven.plugins.annotations.Component) Parameter(org.apache.maven.plugins.annotations.Parameter) SchematronResourceXSLT(com.helger.schematron.xslt.SchematronResourceXSLT) Mojo(org.apache.maven.plugins.annotations.Mojo) SVRLHelper(com.helger.schematron.svrl.SVRLHelper) ESchematronMode(com.helger.schematron.ESchematronMode) MavenProject(org.apache.maven.project.MavenProject) Locale(java.util.Locale) CollectingTransformErrorListener(com.helger.xml.transform.CollectingTransformErrorListener) LifecyclePhase(org.apache.maven.plugins.annotations.LifecyclePhase) Nonnull(javax.annotation.Nonnull) TransformSourceFactory(com.helger.xml.transform.TransformSourceFactory) Nullable(javax.annotation.Nullable) EErrorLevel(com.helger.commons.error.level.EErrorLevel) StringHelper(com.helger.commons.string.StringHelper) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) StaticLoggerBinder(org.slf4j.impl.StaticLoggerBinder) CollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) File(java.io.File) SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ISchematronResource(com.helger.schematron.ISchematronResource) ICommonsList(com.helger.commons.collection.impl.ICommonsList) SchematronResourceSCH(com.helger.schematron.xslt.SchematronResourceSCH) DirectoryScanner(org.codehaus.plexus.util.DirectoryScanner) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure) IErrorList(com.helger.commons.error.list.IErrorList) AbstractMojo(org.apache.maven.plugin.AbstractMojo) SVRLFailedAssert(com.helger.schematron.svrl.SVRLFailedAssert) AbstractTransformErrorListener(com.helger.xml.transform.AbstractTransformErrorListener) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Locale(java.util.Locale) ISchematronResource(com.helger.schematron.ISchematronResource) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) 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.xslt.SchematronResourceSCH) IErrorList(com.helger.commons.error.list.IErrorList) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure) CollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler)

Aggregations

IError (com.helger.commons.error.IError)3 CollectingTransformErrorListener (com.helger.xml.transform.CollectingTransformErrorListener)3 Nullable (javax.annotation.Nullable)3 ICommonsList (com.helger.commons.collection.impl.ICommonsList)2 EErrorLevel (com.helger.commons.error.level.EErrorLevel)2 IErrorList (com.helger.commons.error.list.IErrorList)2 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)2 StringHelper (com.helger.commons.string.StringHelper)2 ESchematronMode (com.helger.schematron.ESchematronMode)2 ISchematronResource (com.helger.schematron.ISchematronResource)2 SchematronResourcePure (com.helger.schematron.pure.SchematronResourcePure)2 CollectingPSErrorHandler (com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler)2 SVRLHelper (com.helger.schematron.svrl.SVRLHelper)2 SVRLMarshaller (com.helger.schematron.svrl.SVRLMarshaller)2 SchematronResourceSCH (com.helger.schematron.xslt.SchematronResourceSCH)2 SchematronResourceXSLT (com.helger.schematron.xslt.SchematronResourceXSLT)2 TransformSourceFactory (com.helger.xml.transform.TransformSourceFactory)2 File (java.io.File)2 Locale (java.util.Locale)2 Nonnull (javax.annotation.Nonnull)2