📄 directoryscanner.java
字号:
if (isCaseSensitive() ? excludeNonPatterns.contains(name) : excludeNonPatterns.contains(name.toUpperCase())) { return true; } for (int i = 0; i < excludePatterns.length; i++) { if (matchPath(excludePatterns[i], name, isCaseSensitive())) { return true; } } return false; } /** * Test whether a file should be selected. * * @param name the filename to check for selecting. * @param file the java.io.File object for this filename. * @return <code>false</code> when the selectors says that the file * should not be selected, <code>true</code> otherwise. */ protected boolean isSelected(String name, File file) { if (selectors != null) { for (int i = 0; i < selectors.length; i++) { if (!selectors[i].isSelected(basedir, name, file)) { return false; } } } return true; } /** * Return the names of the files which matched at least one of the * include patterns and none of the exclude patterns. * The names are relative to the base directory. * * @return the names of the files which matched at least one of the * include patterns and none of the exclude patterns. */ public synchronized String[] getIncludedFiles() { if (filesIncluded == null) { throw new IllegalStateException("Must call scan() first"); } String[] files = new String[filesIncluded.size()]; filesIncluded.copyInto(files); Arrays.sort(files); return files; } /** * Return the count of included files. * @return <code>int</code>. * @since Ant 1.6.3 */ public synchronized int getIncludedFilesCount() { if (filesIncluded == null) { throw new IllegalStateException("Must call scan() first"); } return filesIncluded.size(); } /** * Return the names of the files which matched none of the include * patterns. The names are relative to the base directory. This involves * performing a slow scan if one has not already been completed. * * @return the names of the files which matched none of the include * patterns. * * @see #slowScan */ public synchronized String[] getNotIncludedFiles() { slowScan(); String[] files = new String[filesNotIncluded.size()]; filesNotIncluded.copyInto(files); return files; } /** * Return the names of the files which matched at least one of the * include patterns and at least one of the exclude patterns. * The names are relative to the base directory. This involves * performing a slow scan if one has not already been completed. * * @return the names of the files which matched at least one of the * include patterns and at least one of the exclude patterns. * * @see #slowScan */ public synchronized String[] getExcludedFiles() { slowScan(); String[] files = new String[filesExcluded.size()]; filesExcluded.copyInto(files); return files; } /** * <p>Return the names of the files which were selected out and * therefore not ultimately included.</p> * * <p>The names are relative to the base directory. This involves * performing a slow scan if one has not already been completed.</p> * * @return the names of the files which were deselected. * * @see #slowScan */ public synchronized String[] getDeselectedFiles() { slowScan(); String[] files = new String[filesDeselected.size()]; filesDeselected.copyInto(files); return files; } /** * Return the names of the directories which matched at least one of the * include patterns and none of the exclude patterns. * The names are relative to the base directory. * * @return the names of the directories which matched at least one of the * include patterns and none of the exclude patterns. */ public synchronized String[] getIncludedDirectories() { if (dirsIncluded == null) { throw new IllegalStateException("Must call scan() first"); } String[] directories = new String[dirsIncluded.size()]; dirsIncluded.copyInto(directories); Arrays.sort(directories); return directories; } /** * Return the count of included directories. * @return <code>int</code>. * @since Ant 1.6.3 */ public synchronized int getIncludedDirsCount() { if (dirsIncluded == null) { throw new IllegalStateException("Must call scan() first"); } return dirsIncluded.size(); } /** * Return the names of the directories which matched none of the include * patterns. The names are relative to the base directory. This involves * performing a slow scan if one has not already been completed. * * @return the names of the directories which matched none of the include * patterns. * * @see #slowScan */ public synchronized String[] getNotIncludedDirectories() { slowScan(); String[] directories = new String[dirsNotIncluded.size()]; dirsNotIncluded.copyInto(directories); return directories; } /** * Return the names of the directories which matched at least one of the * include patterns and at least one of the exclude patterns. * The names are relative to the base directory. This involves * performing a slow scan if one has not already been completed. * * @return the names of the directories which matched at least one of the * include patterns and at least one of the exclude patterns. * * @see #slowScan */ public synchronized String[] getExcludedDirectories() { slowScan(); String[] directories = new String[dirsExcluded.size()]; dirsExcluded.copyInto(directories); return directories; } /** * <p>Return the names of the directories which were selected out and * therefore not ultimately included.</p> * * <p>The names are relative to the base directory. This involves * performing a slow scan if one has not already been completed.</p> * * @return the names of the directories which were deselected. * * @see #slowScan */ public synchronized String[] getDeselectedDirectories() { slowScan(); String[] directories = new String[dirsDeselected.size()]; dirsDeselected.copyInto(directories); return directories; } /** * Add default exclusions to the current exclusions set. */ public synchronized void addDefaultExcludes() { int excludesLength = excludes == null ? 0 : excludes.length; String[] newExcludes; newExcludes = new String[excludesLength + defaultExcludes.size()]; if (excludesLength > 0) { System.arraycopy(excludes, 0, newExcludes, 0, excludesLength); } String[] defaultExcludesTemp = getDefaultExcludes(); for (int i = 0; i < defaultExcludesTemp.length; i++) { newExcludes[i + excludesLength] = defaultExcludesTemp[i].replace('/', File.separatorChar) .replace('\\', File.separatorChar); } excludes = newExcludes; } /** * Get the named resource. * @param name path name of the file relative to the dir attribute. * * @return the resource with the given name. * @since Ant 1.5.2 */ public synchronized Resource getResource(String name) { return new FileResource(basedir, name); } /** * Return a cached result of list performed on file, if * available. Invokes the method and caches the result otherwise. * * @param file File (dir) to list. * @since Ant 1.6 */ private String[] list(File file) { String[] files = (String[]) fileListMap.get(file); if (files == null) { files = file.list(); if (files != null) { fileListMap.put(file, files); } } return files; } /** * From <code>base</code> traverse the filesystem in order to find * a file that matches the given name. * * @param base base File (dir). * @param path file path. * @param cs whether to scan case-sensitively. * @return File object that points to the file in question or null. * * @since Ant 1.6.3 */ private File findFile(File base, String path, boolean cs) { if (FileUtils.isAbsolutePath(path)) { if (base == null) { String[] s = FILE_UTILS.dissect(path); base = new File(s[0]); path = s[1]; } else { File f = FILE_UTILS.normalize(path); String s = FILE_UTILS.removeLeadingPath(base, f); if (s.equals(f.getAbsolutePath())) { //removing base from path yields no change; path not child of base return null; } path = s; } } return findFile(base, SelectorUtils.tokenizePath(path), cs); } /** * From <code>base</code> traverse the filesystem in order to find * a file that matches the given stack of names. * * @param base base File (dir). * @param pathElements Vector of path elements (dirs...file). * @param cs whether to scan case-sensitively. * @return File object that points to the file in question or null. * * @since Ant 1.6.3 */ private File findFile(File base, Vector pathElements, boolean cs) { if (pathElements.size() == 0) { return base; } String current = (String) pathElements.remove(0); if (base == null) { return findFile(new File(current), pathElements, cs); } if (!base.isDirectory()) { return null; } String[] files = list(base); if (files == null) { throw new BuildException("IO error scanning directory " + base.getAbsolutePath()); } boolean[] matchCase = cs ? CS_SCAN_ONLY : CS_THEN_NON_CS; for (int i = 0; i < matchCase.length; i++) { for (int j = 0; j < files.length; j++) { if (matchCase[i] ? files[j].equals(current) : files[j].equalsIgnoreCase(current)) { return findFile(new File(base, files[j]), pathElements, cs); } } } return null; } /** * Do we have to traverse a symlink when trying to reach path from * basedir? * @param base base File (dir). * @param path file path. * @since Ant 1.6 */ private boolean isSymlink(File base, String path) { return isSymlink(base, SelectorUtils.tokenizePath(path)); } /** * Do we have to traverse a symlink when trying to reach path from * basedir? * @param base base File (dir). * @param pathElements Vector of path elements (dirs...file). * @since Ant 1.6 */ private boolean isSymlink(File base, Vector pathElements) { if (pathElements.size() > 0) { String current = (String) pathElements.remove(0); try { return FILE_UTILS.isSymbolicLink(base, current) || isSymlink(new File(base, current), pathElements); } catch (IOException ioe) { String msg = "IOException caught while checking " + "for links, couldn't get canonical path!"; // will be caught and redirected to Ant's logging system System.err.println(msg); } } return false; } /** * Has the directory with the given path relative to the base * directory already been scanned? * * <p>Registers the given directory as scanned as a side effect.</p> * * @since Ant 1.6 */ private boolean hasBeenScanned(String vpath) { return !scannedDirs.add(vpath); } /** * This method is of interest for testing purposes. The returned * Set is live and should not be modified. * @return the Set of relative directory names that have been scanned. */ /* package-private */ Set getScannedDirs() { return scannedDirs; } /** * Clear internal caches. * * @since Ant 1.6 */ private synchronized void clearCaches() { fileListMap.clear(); includeNonPatterns.clear(); excludeNonPatterns.clear(); includePatterns = null; excludePatterns = null; areNonPatternSetsReady = false; } /** * Ensure that the in|exclude "patterns" * have been properly divided up. * * @since Ant 1.6.3 */ private synchronized void ensureNonPatternSetsReady() { if (!areNonPatternSetsReady) { includePatterns = fillNonPatternSet(includeNonPatterns, includes); excludePatterns = fillNonPatternSet(excludeNonPatterns, excludes); areNonPatternSetsReady = true; } } /** * Add all patterns that are not real patterns (do not contain * wildcards) to the set and returns the real patterns. * * @param set Set to populate. * @param patterns String[] of patterns. * @since Ant 1.6.3 */ private String[] fillNonPatternSet(Set set, String[] patterns) { ArrayList al = new ArrayList(patterns.length); for (int i = 0; i < patterns.length; i++) { if (!SelectorUtils.hasWildcards(patterns[i])) { set.add(isCaseSensitive() ? patterns[i] : patterns[i].toUpperCase()); } else { al.add(patterns[i]); } } return set.size() == 0 ? patterns : (String[]) al.toArray(new String[al.size()]); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -