com.helger.commons.io.file.FileSystemRecursiveIterator Java Examples

The following examples show how to use com.helger.commons.io.file.FileSystemRecursiveIterator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CSSWriterFuncTest.java    From ph-css with Apache License 2.0 6 votes vote down vote up
@Test
public void testRead30Write21 () throws IOException
{
  for (final File aFile : new FileSystemRecursiveIterator (new File ("src/test/resources/testfiles/css30/good/artificial")).withFilter (IFileFilter.filenameEndsWith (".css")))
  {
    final String sKey = aFile.getAbsolutePath ();
    try
    {
      // read and interpret CSS 3.0
      final CascadingStyleSheet aCSS = CSSReader.readFromFile (aFile, StandardCharsets.UTF_8, ECSSVersion.CSS30);
      assertNotNull (sKey, aCSS);

      // write to CSS 2.1
      final NonBlockingStringWriter aSW = new NonBlockingStringWriter ();
      new CSSWriter (ECSSVersion.CSS21).writeCSS (aCSS, aSW);

      // This should throw an error
      fail (sKey + " should have thrown an exception but got: " + aSW.getAsString ());
    }
    catch (final IllegalStateException ex)
    {}
  }
}
 
Example #2
Source File: AbstractFuncTestCSSReader.java    From ph-css with Apache License 2.0 6 votes vote down vote up
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);
  }
}
 
Example #3
Source File: CSSVisitor30FuncTest.java    From ph-css with Apache License 2.0 6 votes vote down vote up
@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)
      LOGGER.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));
  }
}
 
Example #4
Source File: SchematronTestHelper.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #5
Source File: CSSWriterFuncTest.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@Test
public void testScanTestResourcesHandler21 ()
{
  for (final File aFile : new FileSystemRecursiveIterator (new File ("src/test/resources/testfiles/css21/good/artificial")).withFilter (IFileFilter.filenameEndsWith (".css")))
  {
    _testMe (aFile, ECSSVersion.CSS30);
  }
}
 
Example #6
Source File: CSSWriterFuncTest.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@Test
public void testScanTestResourcesHandler30 ()
{
  for (final File aFile : new FileSystemRecursiveIterator (new File ("src/test/resources/testfiles/css30/good/artificial")).withFilter (IFileFilter.filenameEndsWith (".css")))
  {
    _testMe (aFile, ECSSVersion.CSS30);
  }
}
 
Example #7
Source File: AbstractFuncTestCSSReader.java    From ph-css with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example #8
Source File: MainReadAllCSSOnDisc.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@SuppressFBWarnings ("DMI_HARDCODED_ABSOLUTE_FILENAME")
public static void main (final String [] args)
{
  int nFilesOK = 0;
  int nFilesError = 0;
  final ICommonsOrderedMap <File, ParseException> aErrors = new CommonsLinkedHashMap<> ();
  final Wrapper <File> aCurrentFile = new Wrapper<> ();
  final ICSSParseExceptionCallback aHdl = ex -> aErrors.put (aCurrentFile.get (), ex);
  for (final File aFile : new FileSystemRecursiveIterator (new File ("/")).withFilter (IFileFilter.filenameEndsWith (".css")))
  {
    if (false)
      LOGGER.info (aFile.getAbsolutePath ());
    aCurrentFile.set (aFile);
    final CascadingStyleSheet aCSS = CSSReader.readFromFile (aFile, StandardCharsets.UTF_8, ECSSVersion.CSS30, aHdl);
    if (aCSS == null)
    {
      nFilesError++;
      LOGGER.warn ("  " + aFile.getAbsolutePath () + " failed!");
    }
    else
      nFilesOK++;
  }

  LOGGER.info ("Done");
  for (final Map.Entry <File, ParseException> aEntry : aErrors.entrySet ())
    LOGGER.info ("  " + aEntry.getKey ().getAbsolutePath () + ":\n" + aEntry.getValue ().getMessage () + "\n");
  LOGGER.info ("OK: " + nFilesOK + "; Error: " + nFilesError);
}
 
Example #9
Source File: MainCreateEnumsGenericode21.java    From ph-ubl with Apache License 2.0 5 votes vote down vote up
public static void main (final String [] args) throws JCodeModelException, IOException
{
  for (final File aFile : new FileSystemRecursiveIterator (new File ("src/main/resources/codelists")).withFilter (IFileFilter.filenameEndsWith (".gc")))
  {
    final CodeListDocument aCodeList10 = new Genericode10CodeListMarshaller ().read (aFile);
    if (aCodeList10 != null)
      _createGenericode10 (aFile, aCodeList10);
  }
  new JCMWriter (s_aCodeModel).build (new File ("src/main/java"));
}
 
Example #10
Source File: MainCreateEnumsGenericode23.java    From ph-ubl with Apache License 2.0 5 votes vote down vote up
public static void main (final String [] args) throws JCodeModelException, IOException
{
  for (final File aFile : new FileSystemRecursiveIterator (new File ("src/main/resources/codelists")).withFilter (IFileFilter.filenameEndsWith (".gc")))
    if (!aFile.getName ().equals ("BinaryObjectMimeCode-2.3-incl-deprecated.gc") &&
        !aFile.getName ().equals ("PackagingTypeCode-2.3-incl-deleted.gc") &&
        !aFile.getName ().equals ("UnitOfMeasureCode-2.3-incl-deleted.gc"))
    {
      final CodeListDocument aCodeList10 = new Genericode10CodeListMarshaller ().read (aFile);
      if (aCodeList10 != null)
        _createGenericode10 (aFile, aCodeList10);
    }
  new JCMWriter (s_aCodeModel).build (new File ("src/main/java"));
}
 
Example #11
Source File: MainCreateEnumsGenericode20.java    From ph-ubl with Apache License 2.0 5 votes vote down vote up
public static void main (final String [] args) throws JCodeModelException, IOException
{
  for (final File aFile : new FileSystemRecursiveIterator (new File ("src/main/resources/codelists")).withFilter (IFileFilter.filenameEndsWith (".gc")))
  {
    System.out.println (aFile.getName ());
    final CodeListDocument aCodeList04 = new Genericode04CodeListMarshaller ().read (aFile);
    if (aCodeList04 != null)
      _createGenericode04 (aFile, aCodeList04);
    else
      throw new IllegalStateException ("Failed to read codelist file " + aFile);
  }
  new JCMWriter (s_aCodeModel).build (new File ("src/main/java"));
}
 
Example #12
Source File: MainCreateEnumsGenericode22.java    From ph-ubl with Apache License 2.0 5 votes vote down vote up
public static void main (final String [] args) throws JCodeModelException, IOException
{
  for (final File aFile : new FileSystemRecursiveIterator (new File ("src/main/resources/codelists")).withFilter (IFileFilter.filenameEndsWith (".gc")))
  {
    final CodeListDocument aCodeList10 = new Genericode10CodeListMarshaller ().read (aFile);
    if (aCodeList10 != null)
      _createGenericode10 (aFile, aCodeList10);
  }
  new JCMWriter (s_aCodeModel).build (new File ("src/main/java"));
}
 
Example #13
Source File: FileRelativeIO.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public static void internalCheckAccessRights (@Nonnull final File aBasePath)
{
  // Check read/write/execute
  final StopWatch aSW = StopWatch.createdStarted ();
  if (LOGGER.isInfoEnabled ())
    LOGGER.info ("Checking file access in " + aBasePath);
  int nFiles = 0;
  int nDirs = 0;
  for (final File aFile : new FileSystemRecursiveIterator (aBasePath))
    if (aFile.isFile ())
    {
      // Check if files are read-write
      if (!aFile.canRead ())
        throw new IllegalArgumentException ("Cannot read file " + aFile);
      if (!aFile.canWrite ())
        if (LOGGER.isWarnEnabled ())
          LOGGER.warn ("Cannot write file " + aFile);
      ++nFiles;
    }
    else
      if (aFile.isDirectory ())
      {
        if (!aFile.canRead ())
          throw new IllegalArgumentException ("Cannot read in directory " + aFile);
        if (!aFile.canWrite ())
        {
          if (LOGGER.isWarnEnabled ())
            LOGGER.warn ("Cannot write in directory " + aFile);
        }
        if (!aFile.canExecute ())
        {
          if (LOGGER.isWarnEnabled ())
            LOGGER.warn ("Cannot execute in directory " + aFile);
        }
        ++nDirs;
      }
      else
      {
        if (LOGGER.isWarnEnabled ())
          LOGGER.warn ("Neither file nor directory: " + aFile);
      }

  if (LOGGER.isInfoEnabled ())
    LOGGER.info ("Finished checking file access for " +
                 nFiles +
                 " files and " +
                 nDirs +
                 " directories in " +
                 aSW.stopAndGetMillis () +
                 " milliseconds");
}
 
Example #14
Source File: AbstractFuncTestCSSReader.java    From ph-css with Apache License 2.0 4 votes vote down vote up
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);
  }
}