@@ -84,9 +84,10 @@ public final class SelectorUtils {
private SelectorUtils() {
}
/**
* Retrieves the instance of the Singleton.
*/
/**
* Retrieves the instance of the Singleton.
* @return singleton instance
*/
public static SelectorUtils getInstance() {
return instance;
}
@@ -110,6 +111,7 @@ public final class SelectorUtils {
public static boolean matchPatternStart(String pattern, String str) {
return matchPatternStart(pattern, str, true);
}
/**
* Tests whether or not a given path matches the start of a given
* pattern up to the first "**".
@@ -129,13 +131,13 @@ public final class SelectorUtils {
* pattern up to the first "**".
*/
public static boolean matchPatternStart(String pattern, String str,
boolean isCaseSensitive) {
boolean isCaseSensitive) {
// When str starts with a File.separator, pattern has to start with a
// File.separator.
// When pattern starts with a File.separator, str has to start with a
// File.separator.
if (str.startsWith(File.separator) !=
pattern.startsWith(File.separator)) {
if (str.startsWith(File.separator)
!= pattern.startsWith(File.separator)) {
return false;
}
@@ -143,9 +145,9 @@ public final class SelectorUtils {
String[] strDirs = tokenizePathAsArray(str);
int patIdxStart = 0;
int patIdxEnd = patDirs.length-1;
int patIdxEnd = patDirs.length - 1;
int strIdxStart = 0;
int strIdxEnd = strDirs.length-1;
int strIdxEnd = strDirs.length - 1;
// up to first '**'
while (patIdxStart <= patIdxEnd && strIdxStart <= strIdxEnd) {
@@ -153,8 +155,7 @@ public final class SelectorUtils {
if (patDir.equals("**")) {
break;
}
if (!match(patDir,strDirs[strIdxStart],
isCaseSensitive)) {
if (!match(patDir, strDirs[strIdxStart], isCaseSensitive)) {
return false;
}
patIdxStart++;
@@ -203,13 +204,13 @@ public final class SelectorUtils {
* or <code>false</code> otherwise.
*/
public static boolean matchPath(String pattern, String str,
boolean isCaseSensitive) {
boolean isCaseSensitive) {
// When str starts with a File.separator, pattern has to start with a
// File.separator.
// When pattern starts with a File.separator, str has to start with a
// File.separator.
if (str.startsWith(File.separator) !=
pattern.startsWith(File.separator)) {
if (str.startsWith(File.separator)
!= pattern.startsWith(File.separator)) {
return false;
}
@@ -217,9 +218,9 @@ public final class SelectorUtils {
String[] strDirs = tokenizePathAsArray(str);
int patIdxStart = 0;
int patIdxEnd = patDirs.length-1;
int patIdxEnd = patDirs.length - 1;
int strIdxStart = 0;
int strIdxEnd = strDirs.length-1;
int strIdxEnd = strDirs.length - 1;
// up to first '**'
while (patIdxStart <= patIdxEnd && strIdxStart <= strIdxEnd) {
@@ -227,9 +228,9 @@ public final class SelectorUtils {
if (patDir.equals("**")) {
break;
}
if (!match(patDir,strDirs[strIdxStart],
isCaseSensitive)) {
patDirs = null; strDirs = null;
if (!match(patDir, strDirs[strIdxStart], isCaseSensitive)) {
patDirs = null;
strDirs = null;
return false;
}
patIdxStart++;
@@ -239,7 +240,8 @@ public final class SelectorUtils {
// String is exhausted
for (int i = patIdxStart; i <= patIdxEnd; i++) {
if (!patDirs[i].equals("**")) {
patDirs = null; strDirs = null;
patDirs = null;
strDirs = null;
return false;
}
}
@@ -247,7 +249,8 @@ public final class SelectorUtils {
} else {
if (patIdxStart > patIdxEnd) {
// String not exhausted, but pattern is. Failure.
patDirs = null; strDirs = null;
patDirs = null;
strDirs = null;
return false;
}
}
@@ -258,9 +261,9 @@ public final class SelectorUtils {
if (patDir.equals("**")) {
break;
}
if (!match(patDir,strDirs[strIdxEnd],
isCaseSensitive)) {
patDirs = null; strDirs = null;
if (!match(patDir, strDirs[strIdxEnd], isCaseSensitive)) {
patDirs = null;
strDirs = null;
return false;
}
patIdxEnd--;
@@ -270,7 +273,8 @@ public final class SelectorUtils {
// String is exhausted
for (int i = patIdxStart; i <= patIdxEnd; i++) {
if (!patDirs[i].equals("**")) {
patDirs = null; strDirs = null;
patDirs = null;
strDirs = null;
return false;
}
}
@@ -279,48 +283,50 @@ public final class SelectorUtils {
while (patIdxStart != patIdxEnd && strIdxStart <= strIdxEnd) {
int patIdxTmp = -1;
for (int i = patIdxStart+1; i <= patIdxEnd; i++) {
for (int i = patIdxStart + 1; i <= patIdxEnd; i++) {
if (patDirs[i].equals("**")) {
patIdxTmp = i;
break;
}
}
if (patIdxTmp == patIdxStart+1) {
if (patIdxTmp == patIdxStart + 1) {
// '**/**' situation, so skip 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++) {
String subPat = patDirs[patIdxStart+j+1];
String subStr = strDirs[strIdxStart+i+j];
if (!match(subPat,subStr, isCaseSensitive)) {
continue strLoop;
}
}
foundIdx = strIdxStart+i;
break;
}
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++) {
String subPat = patDirs[patIdxStart + j + 1];
String subStr = strDirs[strIdxStart + i + j];
if (!match(subPat, subStr, isCaseSensitive)) {
continue strLoop;
}
}
foundIdx = strIdxStart + i;
break;
}
if (foundIdx == -1) {
patDirs = null; strDirs = null;
patDirs = null;
strDirs = null;
return false;
}
patIdxStart = patIdxTmp;
strIdxStart = foundIdx+patLength;
strIdxStart = foundIdx + patLength;
}
for (int i = patIdxStart; i <= patIdxEnd; i++) {
if (!patDirs[i].equals("**")) {
patDirs = null; strDirs = null;
patDirs = null;
strDirs = null;
return false;
}
}
@@ -364,13 +370,13 @@ strLoop:
* or <code>false</code> otherwise.
*/
public static boolean match(String pattern, String str,
boolean isCaseSensitive) {
boolean isCaseSensitive) {
char[] patArr = pattern.toCharArray();
char[] strArr = str.toCharArray();
int patIdxStart = 0;
int patIdxEnd = patArr.length-1;
int patIdxEnd = patArr.length - 1;
int strIdxStart = 0;
int strIdxEnd = strArr.length-1;
int strIdxEnd = strArr.length - 1;
char ch;
boolean containsStar = false;
@@ -390,12 +396,12 @@ strLoop:
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
}
if (!isCaseSensitive && Character.toUpperCase(ch)
!= Character.toUpperCase(strArr[i])) {
return false; // Character mismatch
}
}
}
return true; // String matches against pattern
@@ -406,14 +412,14 @@ strLoop:
}
// Process characters before first star
while((ch = patArr[patIdxStart]) != '*' && strIdxStart <= strIdxEnd) {
while ((ch = patArr[patIdxStart]) != '*' && strIdxStart <= strIdxEnd) {
if (ch != '?') {
if (isCaseSensitive && ch != strArr[strIdxStart]) {
return false;// Character mismatch
return false; // Character mismatch
}
if (!isCaseSensitive && Character.toUpperCase(ch) !=
Character.toUpperCase(strArr[strIdxStart])) {
return false;// Character mismatch
if (!isCaseSensitive && Character.toUpperCase(ch)
!= Character.toUpperCase(strArr[strIdxStart])) {
return false; // Character mismatch
}
}
patIdxStart++;
@@ -431,14 +437,14 @@ strLoop:
}
// Process characters after last star
while((ch = patArr[patIdxEnd]) != '*' && strIdxStart <= strIdxEnd) {
while ((ch = patArr[patIdxEnd]) != '*' && strIdxStart <= strIdxEnd) {
if (ch != '?') {
if (isCaseSensitive && ch != strArr[strIdxEnd]) {
return false;// Character mismatch
return false; // Character mismatch
}
if (!isCaseSensitive && Character.toUpperCase(ch) !=
Character.toUpperCase(strArr[strIdxEnd])) {
return false;// Character mismatch
if (!isCaseSensitive && Character.toUpperCase(ch)
!= Character.toUpperCase(strArr[strIdxEnd])) {
return false; // Character mismatch
}
}
patIdxEnd--;
@@ -459,38 +465,40 @@ strLoop:
// always to a '*'.
while (patIdxStart != patIdxEnd && strIdxStart <= strIdxEnd) {
int patIdxTmp = -1;
for (int i = patIdxStart+1; i <= patIdxEnd; i++) {
for (int i = patIdxStart + 1; i <= patIdxEnd; i++) {
if (patArr[i] == '*') {
patIdxTmp = i;
break;
}
}
if (patIdxTmp == patIdxStart+1) {
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;
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];
ch = patArr[patIdxStart + j + 1];
if (ch != '?') {
if (isCaseSensitive && ch != strArr[strIdxStart+i+j]) {
if (isCaseSensitive && ch != strArr[strIdxStart + i
+ j]) {
continue strLoop;
}
if (!isCaseSensitive && Character.toUpperCase(ch) !=
Character.toUpperCase(strArr[strIdxStart+i+j])) {
Character.toUpperCase(strArr[strIdxStart + i
+ j])) {
continue strLoop;
}
}
}
foundIdx = strIdxStart+i;
foundIdx = strIdxStart + i;
break;
}
@@ -499,7 +507,7 @@ strLoop:
}
patIdxStart = patIdxTmp;
strIdxStart = foundIdx+patLength;
strIdxStart = foundIdx + patLength;
}
// All characters in the string are used. Check if only '*'s are left
@@ -520,15 +528,15 @@ strLoop:
*
* @return a Vector of path elements from the tokenized path
*/
public static Vector tokenizePath (String path) {
public static Vector tokenizePath(String path) {
Vector ret = new Vector();
StringTokenizer st = new StringTokenizer(path,File.separator);
StringTokenizer st = new StringTokenizer(path, File.separator);
while (st.hasMoreTokens()) {
ret.addElement(st.nextToken());
}
return ret;
}
/**
* Same as {@link #tokenizePath tokenizePath} but hopefully faster.
*/
@@ -609,7 +617,7 @@ strLoop:
* determining out of dateness
* @return whether the target is out of date
*/
public static boolean isOutOfDate(Resource src, Resource target,
public static boolean isOutOfDate(Resource src, Resource target,
int granularity) {
if (!src.isExists()) {
return false;
@@ -623,7 +631,7 @@ strLoop:
return false;
}
/**
/**
* "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.
@@ -634,10 +642,10 @@ strLoop:
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());
}
StringTokenizer st = new StringTokenizer(input);
while (st.hasMoreTokens()) {
result.append(st.nextToken());
}
}
return result.toString();
}