Search in sources :

Example 1 with FileSystemRecursiveIterator

use of com.helger.commons.io.file.FileSystemRecursiveIterator in project ph-css by phax.

the class AbstractFuncTestCSSReader method testReadBadButRecoverable.

protected final void testReadBadButRecoverable(@Nonnull final String sBaseDir) {
    final File aBaseDir = new File(sBaseDir);
    if (!aBaseDir.exists())
        throw new IllegalArgumentException("BaseDir " + sBaseDir + " does not exist!");
    for (final File aFile : new FileSystemRecursiveIterator(aBaseDir).withFilter(IFileFilter.filenameEndsWith(".css"))) {
        final String sKey = aFile.getAbsolutePath();
        if (m_bDebug)
            m_aLogger.info(sKey);
        // Handle each error as a fatal error!
        final CollectingCSSParseErrorHandler aErrorHdl = new CollectingCSSParseErrorHandler();
        m_aReaderSettings.setCustomErrorHandler(aErrorHdl.and(new LoggingCSSParseErrorHandler()));
        final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, m_aReaderSettings);
        assertNotNull(sKey, aCSS);
        assertTrue(sKey, aErrorHdl.hasParseErrors());
        assertTrue(sKey, aErrorHdl.getParseErrorCount() > 0);
        if (m_bDebug)
            m_aLogger.info(aErrorHdl.getAllParseErrors().toString());
        // Write optimized version and re-read it
        final String sCSS = new CSSWriter(m_aWriterSettings.setOptimizedOutput(true)).getCSSAsString(aCSS);
        assertNotNull(sKey, sCSS);
        if (m_bDebug)
            m_aLogger.info(sCSS);
        final CascadingStyleSheet aCSSReRead = CSSReader.readFromStringReader(sCSS, m_aReaderSettings);
        assertNotNull("Failed to parse:\n" + sCSS, aCSSReRead);
        assertEquals(sKey, aCSS, aCSSReRead);
    }
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CollectingCSSParseErrorHandler(com.helger.css.reader.errorhandler.CollectingCSSParseErrorHandler) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) CSSWriter(com.helger.css.writer.CSSWriter) File(java.io.File) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler)

Example 2 with FileSystemRecursiveIterator

use of com.helger.commons.io.file.FileSystemRecursiveIterator in project ph-css by phax.

the class AbstractFuncTestCSSReader method testReadBad.

protected final void testReadBad(@Nonnull final String sBaseDir) {
    final File aBaseDir = new File(sBaseDir);
    if (!aBaseDir.exists())
        throw new IllegalArgumentException("BaseDir " + sBaseDir + " does not exist!");
    for (final File aFile : new FileSystemRecursiveIterator(aBaseDir).withFilter(IFileFilter.filenameEndsWith(".css"))) {
        final String sKey = aFile.getAbsolutePath();
        if (m_bDebug)
            m_aLogger.info(sKey);
        // Handle each error as a fatal error!
        final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, m_aReaderSettings);
        assertNull(sKey, aCSS);
    }
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) File(java.io.File)

Example 3 with FileSystemRecursiveIterator

use of com.helger.commons.io.file.FileSystemRecursiveIterator in project ph-css by phax.

the class CSSVisitor30FuncTest method testVisitContent30.

@Test
public void testVisitContent30() {
    for (final File aFile : new FileSystemRecursiveIterator(new File("src/test/resources/testfiles/css30/good")).withFilter(IFileFilter.filenameEndsWith(".css"))) {
        final String sKey = aFile.getAbsolutePath();
        if (true)
            s_aLogger.info(sKey);
        final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, new CSSReaderSettings().setFallbackCharset(StandardCharsets.UTF_8).setCSSVersion(ECSSVersion.CSS30).setCustomErrorHandler(new LoggingCSSParseErrorHandler()).setBrowserCompliantMode(true));
        assertNotNull(sKey, aCSS);
        CSSVisitor.visitCSSUrl(aCSS, new MockUrlVisitor(sKey));
    }
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) File(java.io.File) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) Test(org.junit.Test)

Example 4 with FileSystemRecursiveIterator

use of com.helger.commons.io.file.FileSystemRecursiveIterator in project ph-schematron by phax.

the class SchematronTestHelper method _readDI.

@Nonnull
private static ICommonsList<SchematronTestFile> _readDI(@Nonnull final IReadableResource aRes) {
    if (false)
        ClassPathHelper.getAllClassPathEntries().forEach(x -> {
            System.out.println(x);
            if (new File(x).isDirectory()) {
                final FileSystemRecursiveIterator it = new FileSystemRecursiveIterator(new File(x));
                it.forEach(y -> System.out.println(StringHelper.getRepeated("  ", it.getLevel()) + y));
            }
        });
    ValueEnforcer.notNull(aRes, "Resource");
    ValueEnforcer.isTrue(aRes.exists(), () -> "Resource " + aRes + " does not exist!");
    final ICommonsList<SchematronTestFile> ret = new CommonsArrayList<>();
    final IMicroDocument aDoc = MicroReader.readMicroXML(aRes);
    if (aDoc == null)
        throw new IllegalArgumentException("Failed to open/parse " + aRes + " as XML");
    String sLastParentDirBaseName = null;
    for (final IMicroElement eItem : aDoc.getDocumentElement().getAllChildElements()) if (eItem.getTagName().equals("directory"))
        sLastParentDirBaseName = eItem.getAttributeValue("basename");
    else if (eItem.getTagName().equals("file"))
        ret.add(new SchematronTestFile(sLastParentDirBaseName, new ClassPathResource(eItem.getAttributeValue("name")), eItem.getAttributeValue("basename")));
    else
        throw new IllegalArgumentException("Cannot handle " + eItem);
    return ret;
}
Also used : CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) ClassPathHelper(com.helger.commons.lang.ClassPathHelper) StringHelper(com.helger.commons.string.StringHelper) IReadableResource(com.helger.commons.io.resource.IReadableResource) IMicroDocument(com.helger.xml.microdom.IMicroDocument) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) File(java.io.File) ValueEnforcer(com.helger.commons.ValueEnforcer) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) ICommonsList(com.helger.commons.collection.impl.ICommonsList) Nonempty(com.helger.commons.annotation.Nonempty) IMicroElement(com.helger.xml.microdom.IMicroElement) MicroReader(com.helger.xml.microdom.serialize.MicroReader) Nonnull(javax.annotation.Nonnull) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) IMicroElement(com.helger.xml.microdom.IMicroElement) IMicroDocument(com.helger.xml.microdom.IMicroDocument) File(java.io.File) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) Nonnull(javax.annotation.Nonnull)

Example 5 with FileSystemRecursiveIterator

use of com.helger.commons.io.file.FileSystemRecursiveIterator in project ph-css by phax.

the class AbstractFuncTestCSSReader method testReadGood.

protected final void testReadGood(@Nonnull final String sBaseDir) {
    final File aBaseDir = new File(sBaseDir);
    if (!aBaseDir.exists())
        throw new IllegalArgumentException("BaseDir " + sBaseDir + " does not exist!");
    for (final File aFile : new FileSystemRecursiveIterator(aBaseDir).withFilter(IFileFilter.filenameEndsWith(".css"))) {
        final String sKey = aFile.getAbsolutePath();
        if (m_bDebug)
            m_aLogger.info("Filename: " + sKey);
        final CollectingCSSParseErrorHandler aErrorHdl = new CollectingCSSParseErrorHandler();
        m_aReaderSettings.setCustomErrorHandler(aErrorHdl.and(new LoggingCSSParseErrorHandler()));
        final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, m_aReaderSettings);
        assertNotNull(sKey, aCSS);
        // May have errors or not
        if (m_bDebug)
            m_aLogger.info("Parse errors: " + aErrorHdl.getAllParseErrors().toString());
        CommonsTestHelper.testDefaultSerialization(aCSS);
        // Write optimized version and compare it
        String sCSS = new CSSWriter(m_aWriterSettings.setOptimizedOutput(true)).getCSSAsString(aCSS);
        assertNotNull(sKey, sCSS);
        if (m_bDebug)
            m_aLogger.info("Created CSS: " + sCSS);
        final CascadingStyleSheet aCSSReRead = CSSReader.readFromStringReader(sCSS, m_aReaderSettings);
        assertNotNull("Failed to parse " + sKey + ":\n" + sCSS, aCSSReRead);
        assertEquals(sKey + "\n" + sCSS, aCSS, aCSSReRead);
        // Write non-optimized version and compare it
        sCSS = new CSSWriter(m_aWriterSettings.setOptimizedOutput(false)).getCSSAsString(aCSS);
        assertNotNull(sKey, sCSS);
        if (m_bDebug)
            m_aLogger.info("Read and re-created CSS: " + sCSS);
        assertEquals(sKey, aCSS, CSSReader.readFromStringReader(sCSS, m_aReaderSettings));
        // Write non-optimized and code-removed version and ensure it is not
        // null
        sCSS = new CSSWriter(m_aWriterSettings.setOptimizedOutput(false).setRemoveUnnecessaryCode(true)).getCSSAsString(aCSS);
        assertNotNull(sKey, sCSS);
        assertNotNull(sKey, CSSReader.readFromStringReader(sCSS, m_aReaderSettings));
        // Restore value :)
        m_aWriterSettings.setRemoveUnnecessaryCode(false);
    }
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CollectingCSSParseErrorHandler(com.helger.css.reader.errorhandler.CollectingCSSParseErrorHandler) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) CSSWriter(com.helger.css.writer.CSSWriter) File(java.io.File) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler)

Aggregations

FileSystemRecursiveIterator (com.helger.commons.io.file.FileSystemRecursiveIterator)7 File (java.io.File)7 CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)6 LoggingCSSParseErrorHandler (com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler)3 CollectingCSSParseErrorHandler (com.helger.css.reader.errorhandler.CollectingCSSParseErrorHandler)2 CSSWriter (com.helger.css.writer.CSSWriter)2 Test (org.junit.Test)2 ValueEnforcer (com.helger.commons.ValueEnforcer)1 Nonempty (com.helger.commons.annotation.Nonempty)1 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 CommonsLinkedHashMap (com.helger.commons.collection.impl.CommonsLinkedHashMap)1 ICommonsList (com.helger.commons.collection.impl.ICommonsList)1 ICommonsOrderedMap (com.helger.commons.collection.impl.ICommonsOrderedMap)1 IFileFilter (com.helger.commons.io.file.IFileFilter)1 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)1 IReadableResource (com.helger.commons.io.resource.IReadableResource)1 NonBlockingStringWriter (com.helger.commons.io.stream.NonBlockingStringWriter)1 ClassPathHelper (com.helger.commons.lang.ClassPathHelper)1 StringHelper (com.helger.commons.string.StringHelper)1 Wrapper (com.helger.commons.wrapper.Wrapper)1