📄 selectorutils.java
字号:
if (!containsStar) { // No '*'s, so we make a shortcut if (patIdxEnd != strIdxEnd) { return false; // Pattern and string do not have the same size } for (int i = 0; i <= patIdxEnd; i++) { ch = patArr[i]; if (ch != '?') { if (isCaseSensitive && ch != strArr[i]) { return false; // Character mismatch } if (!isCaseSensitive && Character.toUpperCase(ch) != Character.toUpperCase(strArr[i])) { return false; // Character mismatch } } } return true; // String matches against pattern } if (patIdxEnd == 0) { return true; // Pattern contains only '*', which matches anything } // Process characters before first star while ((ch = patArr[patIdxStart]) != '*' && strIdxStart <= strIdxEnd) { if (ch != '?') { if (isCaseSensitive && ch != strArr[strIdxStart]) { return false; // Character mismatch } if (!isCaseSensitive && Character.toUpperCase(ch) != Character.toUpperCase(strArr[strIdxStart])) { return false; // Character mismatch } } patIdxStart++; strIdxStart++; } if (strIdxStart > strIdxEnd) { // All characters in the string are used. Check if only '*'s are // left in the pattern. If so, we succeeded. Otherwise failure. for (int i = patIdxStart; i <= patIdxEnd; i++) { if (patArr[i] != '*') { return false; } } return true; } // Process characters after last star while ((ch = patArr[patIdxEnd]) != '*' && strIdxStart <= strIdxEnd) { if (ch != '?') { if (isCaseSensitive && ch != strArr[strIdxEnd]) { return false; // Character mismatch } if (!isCaseSensitive && Character.toUpperCase(ch) != Character.toUpperCase(strArr[strIdxEnd])) { return false; // Character mismatch } } patIdxEnd--; strIdxEnd--; } if (strIdxStart > strIdxEnd) { // All characters in the string are used. Check if only '*'s are // left in the pattern. If so, we succeeded. Otherwise failure. for (int i = patIdxStart; i <= patIdxEnd; i++) { if (patArr[i] != '*') { return false; } } return true; } // process pattern between stars. padIdxStart and patIdxEnd point // always to a '*'. while (patIdxStart != patIdxEnd && strIdxStart <= strIdxEnd) { int patIdxTmp = -1; for (int i = patIdxStart + 1; i <= patIdxEnd; i++) { if (patArr[i] == '*') { patIdxTmp = i; break; } } if (patIdxTmp == patIdxStart + 1) { // Two stars next to each other, skip the first one. patIdxStart++; continue; } // Find the pattern between padIdxStart & padIdxTmp in str between // strIdxStart & strIdxEnd int patLength = (patIdxTmp - patIdxStart - 1); int strLength = (strIdxEnd - strIdxStart + 1); int foundIdx = -1; strLoop: for (int i = 0; i <= strLength - patLength; i++) { for (int j = 0; j < patLength; j++) { ch = patArr[patIdxStart + j + 1]; if (ch != '?') { if (isCaseSensitive && ch != strArr[strIdxStart + i + j]) { continue strLoop; } if (!isCaseSensitive && Character.toUpperCase(ch) != Character.toUpperCase(strArr[strIdxStart + i + j])) { continue strLoop; } } } foundIdx = strIdxStart + i; break; } if (foundIdx == -1) { return false; } patIdxStart = patIdxTmp; strIdxStart = foundIdx + patLength; } // All characters in the string are used. Check if only '*'s are left // in the pattern. If so, we succeeded. Otherwise failure. for (int i = patIdxStart; i <= patIdxEnd; i++) { if (patArr[i] != '*') { return false; } } return true; } /** * Breaks a path up into a Vector of path elements, tokenizing on * <code>File.separator</code>. * * @param path Path to tokenize. Must not be <code>null</code>. * * @return a Vector of path elements from the tokenized path */ public static Vector tokenizePath (String path) { return tokenizePath(path, File.separator); } /** * Breaks a path up into a Vector of path elements, tokenizing on * * @param path Path to tokenize. Must not be <code>null</code>. * @param separator the separator against which to tokenize. * * @return a Vector of path elements from the tokenized path * @since Ant 1.6 */ public static Vector tokenizePath (String path, String separator) { Vector ret = new Vector(); if (FileUtils.isAbsolutePath(path)) { String[] s = FILE_UTILS.dissect(path); ret.add(s[0]); path = s[1]; } StringTokenizer st = new StringTokenizer(path, separator); while (st.hasMoreTokens()) { ret.addElement(st.nextToken()); } return ret; } /** * Same as {@link #tokenizePath tokenizePath} but hopefully faster. */ private static String[] tokenizePathAsArray(String path) { String root = null; if (FileUtils.isAbsolutePath(path)) { String[] s = FILE_UTILS.dissect(path); root = s[0]; path = s[1]; } char sep = File.separatorChar; int start = 0; int len = path.length(); int count = 0; for (int pos = 0; pos < len; pos++) { if (path.charAt(pos) == sep) { if (pos != start) { count++; } start = pos + 1; } } if (len != start) { count++; } String[] l = new String[count + ((root == null) ? 0 : 1)]; if (root != null) { l[0] = root; count = 1; } else { count = 0; } start = 0; for (int pos = 0; pos < len; pos++) { if (path.charAt(pos) == sep) { if (pos != start) { String tok = path.substring(start, pos); l[count++] = tok; } start = pos + 1; } } if (len != start) { String tok = path.substring(start); l[count/*++*/] = tok; } return l; } /** * Returns dependency information on these two files. If src has been * modified later than target, it returns true. If target doesn't exist, * it likewise returns true. Otherwise, target is newer than src and * is not out of date, thus the method returns false. It also returns * false if the src file doesn't even exist, since how could the * target then be out of date. * * @param src the original file * @param target the file being compared against * @param granularity the amount in seconds of slack we will give in * determining out of dateness * @return whether the target is out of date */ public static boolean isOutOfDate(File src, File target, int granularity) { if (!src.exists()) { return false; } if (!target.exists()) { return true; } if ((src.lastModified() - granularity) > target.lastModified()) { return true; } return false; } /** * Returns dependency information on these two resources. If src has been * modified later than target, it returns true. If target doesn't exist, * it likewise returns true. Otherwise, target is newer than src and * is not out of date, thus the method returns false. It also returns * false if the src file doesn't even exist, since how could the * target then be out of date. * * @param src the original resource * @param target the resource being compared against * @param granularity the int amount in seconds of slack we will give in * determining out of dateness * @return whether the target is out of date */ public static boolean isOutOfDate(Resource src, Resource target, int granularity) { return isOutOfDate(src, target, (long) granularity); } /** * Returns dependency information on these two resources. If src has been * modified later than target, it returns true. If target doesn't exist, * it likewise returns true. Otherwise, target is newer than src and * is not out of date, thus the method returns false. It also returns * false if the src file doesn't even exist, since how could the * target then be out of date. * * @param src the original resource * @param target the resource being compared against * @param granularity the long amount in seconds of slack we will give in * determining out of dateness * @return whether the target is out of date */ public static boolean isOutOfDate(Resource src, Resource target, long granularity) { long sourceLastModified = src.getLastModified(); // Check if source exists - use sourceLastModified for file resources // as it quicker that checking exists() again, however string reources // have a last modified time of 0 boolean sourceExists = (src instanceof FileResource) ? sourceLastModified != 0L : src.isExists(); long targetLastModified = target.getLastModified(); if (targetLastModified == 0L) { return true; } return (sourceLastModified - granularity) > targetLastModified; } /** * "Flattens" a string by removing all whitespace (space, tab, linefeed, * carriage return, and formfeed). This uses StringTokenizer and the * default set of tokens as documented in the single arguement constructor. * * @param input a String to remove all whitespace. * @return a String that has had all whitespace removed. */ public static String removeWhitespace(String input) { StringBuffer result = new StringBuffer(); if (input != null) { StringTokenizer st = new StringTokenizer(input); while (st.hasMoreTokens()) { result.append(st.nextToken()); } } return result.toString(); } /** * Tests if a string contains stars or question marks * @param input a String which one wants to test for containing wildcard * @return true if the string contains at least a star or a question mark */ public static boolean hasWildcards(String input) { return (input.indexOf('*') != -1 || input.indexOf('?') != -1); } /** * removes from a pattern all tokens to the right containing wildcards * @param input the input string * @return the leftmost part of the pattern without wildcards */ public static String rtrimWildcardTokens(String input) { String[] tokens = tokenizePathAsArray(input); StringBuffer sb = new StringBuffer(); for (int i = 0; i < tokens.length; i++) { if (hasWildcards(tokens[i])) { break; } if (i > 0 && sb.charAt(sb.length() - 1) != File.separatorChar) { sb.append(File.separator); } sb.append(tokens[i]); } return sb.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -