/** * Scans single classpath directory. * @see #onEntry(ClassPathEntry) */ protected void scanClassPath(final File root) { String rootPath = root.getAbsolutePath(); if (!rootPath.endsWith(File.separator)) { rootPath += File.separatorChar; } final FindFile ff = FindFile.create().includeDirs(false).recursive(true).searchPath(rootPath); File file; while ((file = ff.nextFile()) != null) { final String filePath = file.getAbsolutePath(); try { if (StringUtil.endsWithIgnoreCase(filePath, CLASS_FILE_EXT)) { scanClassFile(filePath, rootPath, file, true); } else if (includeResources) { scanClassFile(filePath, rootPath, file, false); } } catch (RuntimeException rex) { if (!ignoreException) { throw rex; } } } }
/** * Clears all settings and removes all created bundle files from file system. */ public synchronized void reset() { if (strategy == Strategy.ACTION_MANAGED) { actionBundles.clear(); mirrors.clear(); } FindFile ff = new FindFile(); ff.includeDirs(false); ff.searchPath(new File(bundleFolder, staplerPath)); File f; int count = 0; while ((f = ff.nextFile()) != null) { f.delete(); count++; } if (log.isInfoEnabled()) { log.info("reset: " + count + " bundle files deleted."); } }
protected File nextFile() { while (index < files.length) { File file = files[index]; if (file == null) { index++; continue; } files[index] = null; index++; if (file.isFile()) { if (!includeFiles) { continue; } if (!acceptFile(file)) { continue; } } return file; } return null; } }
/** * Scans single classpath directory. * @see #onEntry(EntryData) */ protected void scanClassPath(File root) { String rootPath = root.getAbsolutePath(); if (!rootPath.endsWith(File.separator)) { rootPath += File.separatorChar; } FindFile ff = new FindFile().setIncludeDirs(false).setRecursive(true).searchPath(rootPath); File file; while ((file = ff.nextFile()) != null) { String filePath = file.getAbsolutePath(); try { if (StringUtil.endsWithIgnoreCase(filePath, CLASS_FILE_EXT)) { scanClassFile(filePath, rootPath, file, true); } else if (includeResources) { scanClassFile(filePath, rootPath, file, false); } } catch (RuntimeException rex) { if (!ignoreException) { throw rex; } } } }
@Test void testNonExisting() { FindFile wff = new FindFile(); wff.includeDirs(true); wff.includeFiles(true); wff.recursive(true); wff.walking(true); wff.searchPath(dataRoot + "/void"); wff.matchType(WildcardFindFile.Match.FULL_PATH); int count = 0; while (wff.nextFile() != null) { count++; } assertEquals(0, count); }
FindFile ff = new FindFile(); ff.includeDirs(true); ff.includeFiles(true); ff.recursive(false); ff.sortByName(); ff.searchPath(dataRoot); while ((f = ff.nextFile()) != null) { str.append(f.getName()).append(" | "); ff.reset(); ff.sortNone(); ff.sortByNameDesc(); str.setLength(0); while ((f = ff.nextFile()) != null) { str.append(f.getName()).append(" | "); ff.reset(); ff.sortNone(); ff.sortFoldersFirst(); ff.sortByName(); str.setLength(0); while ((f = ff.nextFile()) != null) { str.append(f.getName()).append(" | "); ff.reset(); ff.sortNone();
@Test void testJustFoldersAndFiles() { FindFile ff = new FindFile(); ff.includeDirs(false); ff.includeFiles(true); ff.recursive(false); ff.sortByName(); ff.searchPath(dataRoot); File f; StringBuilder str = new StringBuilder(); while ((f = ff.nextFile()) != null) { str.append(f.getName()).append(" | "); } assertEquals("alpha.txt | jodd1.txt | jodd10.txt | zero.txt | ", str.toString()); ff = new FindFile(); ff.includeDirs(true); ff.includeFiles(false); ff.recursive(false); ff.sortByName(); ff.searchPath(dataRoot); str = new StringBuilder(); while ((f = ff.nextFile()) != null) { str.append(f.getName()).append(" | "); } assertEquals("beta | sumo | ", str.toString()); }
@Test void testTwoRoots() { FindFile ff = new WildcardFindFile() .includeDirs(true) .includeFiles(true) .recursive(true) .walking(true) .searchPath(dataRoot + "/beta") .searchPath(dataRoot + "/sumo") .include("**"); int count = 0; while (ff.nextFile() != null) { count++; } assertEquals(5, count); ff.reset(); ff.includeDirs(false); count = 0; while (ff.nextFile() != null) { count++; } assertEquals(3, count); }
ff.includeDirs(true); ff.includeFiles(true); ff.recursive(true); ff.sortByName(); ff.walking(true); ff.searchPath(dataRoot); assertNull(ff.lastFile()); while ((f = ff.nextFile()) != null) { s2.append(f.getName()); s2.append(" | "); assertNotNull(ff.lastFile()); s2.setLength(0); ff.reset(); ff.walking(false); while ((f = ff.nextFile()) != null) { s2.append(f.getName()); s2.append(" | ");
/** * Specifies the search path. */ public T searchPath(URL... searchPath) { for (URL url : searchPath) { searchPath(url); } return (T) this; }
@Test void testFindAll() { List<File> foundedFiles = new WildcardFindFile() .include("**/*file/a*") .recursive(true) .includeDirs(true) .searchPath(dataRoot) .findAll(); assertEquals(2, foundedFiles.size()); List<String> names = foundedFiles.stream().map(File::getName).collect(Collectors.toList()); assertTrue(names.contains("a.png")); assertTrue(names.contains("a.txt")); }
/** * 13s */ @Test void testLiveHtmls() throws IOException { FindFile ff = new WildcardFindFile().include("**/*.html"); ff.searchPath(testLiveRoot); File file; boolean processed = false; while ((file = ff.nextFile()) != null) { processed = true; String name = file.getName(); System.out.println('+' + name); String content = FileUtil.readString(file); try { _parseEmpty(content); } catch (Exception ex) { ex.printStackTrace(); fail(ex.toString()); } } assertTrue(processed); }
/** * Performs scanning. */ @SuppressWarnings("StatementWithEmptyBody") public void find() { while (nextFile() != null) { } }
@Test void testDecoraParser() throws IOException { DecoraParser decoraParser = new DecoraParser(); FindFile ff = new WildcardFindFile().include("*.*ml"); ff.matchType(FindFile.Match.NAME); ff.searchPath(testDataRoot); File file; while ((file = ff.nextFile()) != null) { System.out.println("+" + file.getName()); char[] page = FileUtil.readString(file).toCharArray(); String decoratorFileName = StringUtil.replace(file.getAbsolutePath(), ".html", "-decora.htm"); char[] decorator = FileUtil.readString(decoratorFileName).toCharArray(); FastCharArrayWriter writer = new FastCharArrayWriter(); decoraParser.decorate(writer, page, decorator); String out = writer.toString(); String outFileName = StringUtil.replace(file.getAbsolutePath(), ".html", "-out.htm"); String outExpected = FileUtil.readString(outFileName); assertEquals(trimLines(outExpected), trimLines(out)); } }
@Test void testTwoAccept() { FindFile ff = new WildcardFindFile() .include("**/*file/a.png") .include("**/*file/a.txt") .recursive(true) .includeDirs(true) .searchPath(dataRoot); final MutableInteger countFiles = new MutableInteger(); final MutableInteger countDirs = new MutableInteger(); ff.forEach(f -> { if (f.isDirectory()) { countDirs.value++; } else { countFiles.value++; String path = f.getAbsolutePath(); path = FileNameUtil.separatorsToUnix(path); if (!path.startsWith("/")) { path = '/' + path; } boolean matched = path.equals(dataRoot + "/file/a.png") || path.equals(dataRoot + "/file/a.txt"); assertTrue(matched); } }); assertEquals(0, countDirs.value); assertEquals(2, countFiles.value); }
/** * Adds generic sorting. */ public T sortWith(Comparator<File> fileComparator) { addComparator(fileComparator); return (T) this; }
/** * Specifies a set of search paths. */ public T searchPath(File... searchPath) { for (File file : searchPath) { addPath(file); } return (T) this; }
init(); if (acceptFile(nextFile)) { lastFile = nextFile; return nextFile; if (acceptFile(folder)) { lastFile = folder; return folder;
public static FindFile create() { return new FindFile(); }
/** * Determine if file is accepted, based on include and exclude * rules. Called on each file entry (file or directory) and * returns <code>true</code> if file passes search criteria. * File is matched using {@link #getMatchingFilePath(java.io.File) matching file path}. * @see jodd.util.InExRules */ protected boolean acceptFile(File file) { String matchingFilePath = getMatchingFilePath(file); return rules.match(matchingFilePath); }