📄 directoryscanner.java
字号:
/** * Test whether or not a given path matches a given pattern. * * @param pattern The pattern to match against. Must not be * <code>null</code>. * @param str The path to match, as a String. Must not be * <code>null</code>. * * @return <code>true</code> if the pattern matches against the string, * or <code>false</code> otherwise. */ protected static boolean matchPath(String pattern, String str) { return SelectorUtils.matchPath(pattern, str); } /** * Test whether or not a given path matches a given pattern. * * @param pattern The pattern to match against. Must not be * <code>null</code>. * @param str The path to match, as a String. Must not be * <code>null</code>. * @param isCaseSensitive Whether or not matching should be performed * case sensitively. * * @return <code>true</code> if the pattern matches against the string, * or <code>false</code> otherwise. */ protected static boolean matchPath(String pattern, String str, boolean isCaseSensitive) { return SelectorUtils.matchPath(pattern, str, isCaseSensitive); } /** * Test whether or not a string matches against a pattern. * The pattern may contain two special characters:<br> * '*' means zero or more characters<br> * '?' means one and only one character * * @param pattern The pattern to match against. * Must not be <code>null</code>. * @param str The string which must be matched against the pattern. * Must not be <code>null</code>. * * @return <code>true</code> if the string matches against the pattern, * or <code>false</code> otherwise. */ public static boolean match(String pattern, String str) { return SelectorUtils.match(pattern, str); } /** * Test whether or not a string matches against a pattern. * The pattern may contain two special characters:<br> * '*' means zero or more characters<br> * '?' means one and only one character * * @param pattern The pattern to match against. * Must not be <code>null</code>. * @param str The string which must be matched against the pattern. * Must not be <code>null</code>. * @param isCaseSensitive Whether or not matching should be performed * case sensitively. * * * @return <code>true</code> if the string matches against the pattern, * or <code>false</code> otherwise. */ protected static boolean match(String pattern, String str, boolean isCaseSensitive) { return SelectorUtils.match(pattern, str, isCaseSensitive); } /** * Get the list of patterns that should be excluded by default. * * @return An array of <code>String</code> based on the current * contents of the <code>defaultExcludes</code> * <code>Vector</code>. * * @since Ant 1.6 */ public static String[] getDefaultExcludes() { return (String[]) defaultExcludes.toArray(new String[defaultExcludes .size()]); } /** * Add a pattern to the default excludes unless it is already a * default exclude. * * @param s A string to add as an exclude pattern. * @return <code>true</code> if the string was added; * <code>false</code> if it already existed. * * @since Ant 1.6 */ public static boolean addDefaultExclude(String s) { if (defaultExcludes.indexOf(s) == -1) { defaultExcludes.add(s); return true; } return false; } /** * Remove a string if it is a default exclude. * * @param s The string to attempt to remove. * @return <code>true</code> if <code>s</code> was a default * exclude (and thus was removed); * <code>false</code> if <code>s</code> was not * in the default excludes list to begin with. * * @since Ant 1.6 */ public static boolean removeDefaultExclude(String s) { return defaultExcludes.remove(s); } /** * Go back to the hardwired default exclude patterns. * * @since Ant 1.6 */ public static void resetDefaultExcludes() { defaultExcludes = new Vector(); for (int i = 0; i < DEFAULTEXCLUDES.length; i++) { defaultExcludes.add(DEFAULTEXCLUDES[i]); } } /** * Set the base directory to be scanned. This is the directory which is * scanned recursively. All '/' and '\' characters are replaced by * <code>File.separatorChar</code>, so the separator used need not match * <code>File.separatorChar</code>. * * @param basedir The base directory to scan. */ public void setBasedir(String basedir) { setBasedir(basedir == null ? (File) null : new File(basedir.replace('/', File.separatorChar).replace( '\\', File.separatorChar))); } /** * Set the base directory to be scanned. This is the directory which is * scanned recursively. * * @param basedir The base directory for scanning. */ public synchronized void setBasedir(File basedir) { this.basedir = basedir; } /** * Return the base directory to be scanned. * This is the directory which is scanned recursively. * * @return the base directory to be scanned. */ public synchronized File getBasedir() { return basedir; } /** * Find out whether include exclude patterns are matched in a * case sensitive way. * @return whether or not the scanning is case sensitive. * @since Ant 1.6 */ public synchronized boolean isCaseSensitive() { return isCaseSensitive; } /** * Set whether or not include and exclude patterns are matched * in a case sensitive way. * * @param isCaseSensitive whether or not the file system should be * regarded as a case sensitive one. */ public synchronized void setCaseSensitive(boolean isCaseSensitive) { this.isCaseSensitive = isCaseSensitive; } /** * Sets whether or not a missing base directory is an error * * @param errorOnMissingDir whether or not a missing base directory * is an error * @since Ant 1.7.1 */ public void setErrorOnMissingDir(boolean errorOnMissingDir) { this.errorOnMissingDir = errorOnMissingDir; } /** * Get whether or not a DirectoryScanner follows symbolic links. * * @return flag indicating whether symbolic links should be followed. * * @since Ant 1.6 */ public synchronized boolean isFollowSymlinks() { return followSymlinks; } /** * Set whether or not symbolic links should be followed. * * @param followSymlinks whether or not symbolic links should be followed. */ public synchronized void setFollowSymlinks(boolean followSymlinks) { this.followSymlinks = followSymlinks; } /** * Set the list of include patterns to use. All '/' and '\' characters * are replaced by <code>File.separatorChar</code>, so the separator used * need not match <code>File.separatorChar</code>. * <p> * When a pattern ends with a '/' or '\', "**" is appended. * * @param includes A list of include patterns. * May be <code>null</code>, indicating that all files * should be included. If a non-<code>null</code> * list is given, all elements must be * non-<code>null</code>. */ public synchronized void setIncludes(String[] includes) { if (includes == null) { this.includes = null; } else { this.includes = new String[includes.length]; for (int i = 0; i < includes.length; i++) { this.includes[i] = normalizePattern(includes[i]); } } } /** * Set the list of exclude patterns to use. All '/' and '\' characters * are replaced by <code>File.separatorChar</code>, so the separator used * need not match <code>File.separatorChar</code>. * <p> * When a pattern ends with a '/' or '\', "**" is appended. * * @param excludes A list of exclude patterns. * May be <code>null</code>, indicating that no files * should be excluded. If a non-<code>null</code> list is * given, all elements must be non-<code>null</code>. */ public synchronized void setExcludes(String[] excludes) { if (excludes == null) { this.excludes = null; } else { this.excludes = new String[excludes.length]; for (int i = 0; i < excludes.length; i++) { this.excludes[i] = normalizePattern(excludes[i]); } } } /** * Add to the list of exclude patterns to use. All '/' and '\' * characters are replaced by <code>File.separatorChar</code>, so * the separator used need not match <code>File.separatorChar</code>. * <p> * When a pattern ends with a '/' or '\', "**" is appended. * * @param excludes A list of exclude patterns. * May be <code>null</code>, in which case the * exclude patterns don't get changed at all. * * @since Ant 1.6.3 */ public synchronized void addExcludes(String[] excludes) { if (excludes != null && excludes.length > 0) { if (this.excludes != null && this.excludes.length > 0) { String[] tmp = new String[excludes.length + this.excludes.length]; System.arraycopy(this.excludes, 0, tmp, 0, this.excludes.length); for (int i = 0; i < excludes.length; i++) { tmp[this.excludes.length + i] = normalizePattern(excludes[i]); } this.excludes = tmp; } else { setExcludes(excludes); } } } /** * All '/' and '\' characters are replaced by * <code>File.separatorChar</code>, so the separator used need not * match <code>File.separatorChar</code>. * * <p> When a pattern ends with a '/' or '\', "**" is appended. * * @since Ant 1.6.3 */ private static String normalizePattern(String p) { String pattern = p.replace('/', File.separatorChar) .replace('\\', File.separatorChar); if (pattern.endsWith(File.separator)) { pattern += "**"; } return pattern; } /** * Set the selectors that will select the filelist. * * @param selectors specifies the selectors to be invoked on a scan. */ public synchronized void setSelectors(FileSelector[] selectors) { this.selectors = selectors; } /** * Return whether or not the scanner has included all the files or * directories it has come across so far. * * @return <code>true</code> if all files and directories which have * been found so far have been included. */ public synchronized boolean isEverythingIncluded() { return everythingIncluded; } /** * Scan for files which match at least one include pattern and don't match * any exclude patterns. If there are selectors then the files must pass * muster there, as well. Scans under basedir, if set; otherwise the * include patterns without leading wildcards specify the absolute paths of * the files that may be included. * * @exception IllegalStateException if the base directory was set * incorrectly (i.e. if it doesn't exist or isn't a directory). */ public void scan() throws IllegalStateException { synchronized (scanLock) { if (scanning) { while (scanning) { try { scanLock.wait(); } catch (InterruptedException e) { continue; } } if (illegal != null) { throw illegal; } return; } scanning = true; } try { synchronized (this) { illegal = null; clearResults(); // set in/excludes to reasonable defaults if needed: boolean nullIncludes = (includes == null); includes = nullIncludes ? new String[] {"**"} : includes; boolean nullExcludes = (excludes == null); excludes = nullExcludes ? new String[0] : excludes; if (basedir == null) { // if no basedir and no includes, nothing to do: if (nullIncludes) { return; } } else { if (!basedir.exists()) { if (errorOnMissingDir) { illegal = new IllegalStateException( "basedir " + basedir + " does not exist"); } else { // Nothing to do - basedir does not exist return; } } if (!basedir.isDirectory()) { illegal = new IllegalStateException("basedir " + basedir + " is not a directory"); } if (illegal != null) { throw illegal; } } if (isIncluded("")) { if (!isExcluded("")) { if (isSelected("", basedir)) { dirsIncluded.addElement(""); } else { dirsDeselected.addElement(""); } } else { dirsExcluded.addElement(""); } } else { dirsNotIncluded.addElement(""); } checkIncludePatterns(); clearCaches(); includes = nullIncludes ? null : includes; excludes = nullExcludes ? null : excludes; } } finally { synchronized (scanLock) { scanning = false; scanLock.notifyAll(); } } } /** * This routine is actually checking all the include patterns in * order to avoid scanning everything under base dir. * @since Ant 1.6 */ private void checkIncludePatterns() { Map newroots = new HashMap();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -