Search in sources :

Example 11 with FileSystemResource

use of com.helger.commons.io.resource.FileSystemResource in project ph-css by phax.

the class Issue35Test method testIssue.

@Test
public void testIssue() {
    final String css = StreamHelper.getAllBytesAsString(new FileSystemResource("src/test/resources/testfiles/css30/good/issue35.css"), StandardCharsets.UTF_8);
    final CSSReaderSettings aSettings = new CSSReaderSettings().setCSSVersion(ECSSVersion.LATEST).setBrowserCompliantMode(false);
    final CascadingStyleSheet cascadingStyleSheet = CSSReader.readFromStringStream(css, aSettings);
    assertNotNull(cascadingStyleSheet);
    final CSSWriter writer = new CSSWriter(new CSSWriterSettings(ECSSVersion.LATEST, true));
    assertNotNull(writer.getCSSAsString(cascadingStyleSheet));
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) CSSWriter(com.helger.css.writer.CSSWriter) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) Test(org.junit.Test)

Example 12 with FileSystemResource

use of com.helger.commons.io.resource.FileSystemResource in project ph-css by phax.

the class CSSCompressMojo method _compressCSSFile.

private void _compressCSSFile(@Nonnull final File aChild) {
    // Compress the file only if the compressed file is older than the original
    // file. Note: lastModified on a non-existing file returns 0L
    final File aCompressed = new File(FilenameHelper.getWithoutExtension(aChild.getAbsolutePath()) + targetFileExtension);
    if (aCompressed.lastModified() < aChild.lastModified() || forceCompress) {
        if (verbose)
            getLog().info("Start compressing CSS file " + _getRelativePath(aChild));
        else
            getLog().debug("Start compressing CSS file " + _getRelativePath(aChild));
        final ICSSParseExceptionCallback aExHdl = (@Nonnull final ParseException ex) -> getLog().error("Failed to parse CSS file " + _getRelativePath(aChild), ex);
        final Charset aFallbackCharset = CharsetHelper.getCharsetFromName(sourceEncoding);
        final CSSReaderSettings aSettings = new CSSReaderSettings().setCSSVersion(ECSSVersion.CSS30).setFallbackCharset(aFallbackCharset).setCustomExceptionHandler(aExHdl).setBrowserCompliantMode(browserCompliantMode);
        final CascadingStyleSheet aCSS = CSSReader.readFromFile(aChild, aSettings);
        if (aCSS != null) {
            // We read it!
            final FileSystemResource aDestFile = new FileSystemResource(aCompressed);
            try {
                final CSSWriterSettings aWriterSettings = new CSSWriterSettings(ECSSVersion.CSS30);
                aWriterSettings.setOptimizedOutput(true);
                aWriterSettings.setRemoveUnnecessaryCode(removeUnnecessaryCode);
                aWriterSettings.setNewLineMode(newLineMode);
                aWriterSettings.setQuoteURLs(quoteURLs);
                aWriterSettings.setWriteNamespaceRules(writeNamespaceRules);
                aWriterSettings.setWriteFontFaceRules(writeFontFaceRules);
                aWriterSettings.setWriteKeyframesRules(writeKeyframesRules);
                aWriterSettings.setWriteMediaRules(writeMediaRules);
                aWriterSettings.setWritePageRules(writePageRules);
                aWriterSettings.setWriteViewportRules(writeViewportRules);
                aWriterSettings.setWriteSupportsRules(writeSupportsRules);
                aWriterSettings.setWriteUnknownRules(writeUnknownRules);
                final Charset aTargetCharset = CharsetHelper.getCharsetFromName(targetEncoding);
                new CSSWriter(aWriterSettings).writeCSS(aCSS, aDestFile.getWriter(aTargetCharset, EAppend.TRUNCATE));
            } catch (final IOException ex) {
                getLog().error("Failed to write compressed CSS file '" + aCompressed.toString() + "' to disk", ex);
            }
        }
    } else {
        if (verbose)
            getLog().info("Ignoring already compressed CSS file " + _getRelativePath(aChild));
        else
            getLog().debug("Ignoring already compressed CSS file " + _getRelativePath(aChild));
    }
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) Charset(java.nio.charset.Charset) ParseException(com.helger.css.parser.ParseException) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) CSSWriter(com.helger.css.writer.CSSWriter) IOException(java.io.IOException) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) File(java.io.File) ICSSParseExceptionCallback(com.helger.css.handler.ICSSParseExceptionCallback)

Example 13 with FileSystemResource

use of com.helger.commons.io.resource.FileSystemResource 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)

Example 14 with FileSystemResource

use of com.helger.commons.io.resource.FileSystemResource in project ph-schematron by phax.

the class SchematronPreprocess method execute.

@Override
public void execute() throws BuildException {
    boolean bCanRun = false;
    if (m_aSrcFile == null)
        _error("No source Schematron file specified!");
    else if (m_aSrcFile.exists() && !m_aSrcFile.isFile())
        _error("The specified source Schematron file " + m_aSrcFile + " is not a file!");
    else if (m_aDstFile == null)
        _error("No destination Schematron file specified!");
    else if (m_aDstFile.exists() && !m_aDstFile.isFile())
        _error("The specified destination Schematron file " + m_aDstFile + " is not a file!");
    else
        bCanRun = true;
    if (bCanRun)
        try {
            // Read source
            final PSSchema aSchema = new PSReader(new FileSystemResource(m_aSrcFile)).readSchema();
            // Setup preprocessor
            final PSPreprocessor aPreprocessor = new PSPreprocessor(PSXPathQueryBinding.getInstance());
            aPreprocessor.setKeepTitles(m_bKeepTitles);
            aPreprocessor.setKeepDiagnostics(m_bKeepDiagnostics);
            aPreprocessor.setKeepReports(m_bKeepReports);
            aPreprocessor.setKeepEmptyPatterns(m_bKeepEmptyPatterns);
            aPreprocessor.setKeepEmptySchema(true);
            // Main pre-processing
            final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema(aSchema);
            // Write the result file
            new PSWriter(new PSWriterSettings().setXMLWriterSettings(new XMLWriterSettings())).writeToFile(aPreprocessedSchema, m_aDstFile);
            log("Successfully pre-processed Schematron " + m_aSrcFile + " to " + m_aDstFile);
        } catch (final SchematronReadException | SchematronPreprocessException ex) {
            _error("Error processing Schemtron " + m_aSrcFile.getAbsolutePath(), ex);
        }
}
Also used : XMLWriterSettings(com.helger.xml.serialize.write.XMLWriterSettings) PSWriter(com.helger.schematron.pure.exchange.PSWriter) PSWriterSettings(com.helger.schematron.pure.exchange.PSWriterSettings) PSReader(com.helger.schematron.pure.exchange.PSReader) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) PSPreprocessor(com.helger.schematron.pure.preprocess.PSPreprocessor) PSSchema(com.helger.schematron.pure.model.PSSchema)

Example 15 with FileSystemResource

use of com.helger.commons.io.resource.FileSystemResource in project ph-schematron by phax.

the class Issue20140523Test method validateAndProduceSVRL.

public static void validateAndProduceSVRL(final File schematron, final File xml) throws Exception {
    final IReadableResource aSchematron = new FileSystemResource(schematron.getAbsoluteFile());
    final IReadableResource anXMLSource = new FileSystemResource(xml.getAbsoluteFile());
    final AbstractSchematronResource pure = new SchematronResourcePure(aSchematron);
    final SchematronOutputType aSVRL = pure.applySchematronValidationToSVRL(anXMLSource);
    assertNotNull(aSVRL);
    if (false)
        System.out.println(new SVRLMarshaller().getAsString(aSVRL));
}
Also used : SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) IReadableResource(com.helger.commons.io.resource.IReadableResource) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) AbstractSchematronResource(com.helger.schematron.AbstractSchematronResource) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure)

Aggregations

FileSystemResource (com.helger.commons.io.resource.FileSystemResource)25 SchematronOutputType (org.oclc.purl.dsdl.svrl.SchematronOutputType)18 SVRLMarshaller (com.helger.schematron.svrl.SVRLMarshaller)14 SchematronResourcePure (com.helger.schematron.pure.SchematronResourcePure)13 IReadableResource (com.helger.commons.io.resource.IReadableResource)8 AbstractSchematronResource (com.helger.schematron.AbstractSchematronResource)6 SchematronResourceSCH (com.helger.schematron.xslt.SchematronResourceSCH)6 LoggingPSErrorHandler (com.helger.schematron.pure.errorhandler.LoggingPSErrorHandler)5 ISchematronResource (com.helger.schematron.ISchematronResource)4 PSReader (com.helger.schematron.pure.exchange.PSReader)4 PSSchema (com.helger.schematron.pure.model.PSSchema)4 File (java.io.File)4 Test (org.junit.Test)4 PSPreprocessor (com.helger.schematron.pure.preprocess.PSPreprocessor)3 XMLWriterSettings (com.helger.xml.serialize.write.XMLWriterSettings)3 ICommonsList (com.helger.commons.collection.impl.ICommonsList)2 IError (com.helger.commons.error.IError)2 EErrorLevel (com.helger.commons.error.level.EErrorLevel)2 IErrorList (com.helger.commons.error.list.IErrorList)2 StringHelper (com.helger.commons.string.StringHelper)2