Java源码示例:com.helger.commons.io.file.FileSystemRecursiveIterator
示例1
@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)
{}
}
}
示例2
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);
}
}
示例3
@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));
}
}
示例4
@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;
}
示例5
@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);
}
}
示例6
@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);
}
}
示例7
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);
}
}
示例8
@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);
}
示例9
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"));
}
示例10
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"));
}
示例11
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"));
}
示例12
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"));
}
示例13
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");
}
示例14
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);
}
}