/*
* =========================================================================================
* Copyright 2013 the kamon project
"\"
- escape the next character (treat it literally, even if it is itself a recognized metacharacter)"?"
- match any non-slash character"*"
- match zero or more non-slash characters"**"
- match zero or more characters, including slashes"/"
- match one or more slash characters. Consecutive {@code /} characters are collapsed down into one./**
in that the named directory is not itself included in the glob.
*
* See also: "Patterns" in the Ant Manual
*
* @param glob the glob to match
*
* @return the pattern
*/
private static Pattern getGlobPattern(final String glob) {
StringBuilder patternBuilder = new StringBuilder();
final Matcher m = GLOB_PATTERN.matcher(glob);
boolean lastWasSlash = false;
while (m.find()) {
lastWasSlash = false;
String grp;
if ((grp = m.group(1)) != null) {
// match a * or **
if (grp.length() == 2) {
// it's a **
patternBuilder.append(".*");
} else {
// it's a *
patternBuilder.append("[^/]*");
}
} else if ((grp = m.group(2)) != null) {
// match a '?' glob pattern; any non-slash character
patternBuilder.append("[^/]");
} else if ((grp = m.group(3)) != null) {
// backslash-escaped value
patternBuilder.append(Pattern.quote(m.group().substring(1)));
} else if ((grp = m.group(4)) != null) {
// match any number of / chars
patternBuilder.append("/+");
lastWasSlash = true;
} else {
// some other string
patternBuilder.append(Pattern.quote(m.group()));
}
}
if (lastWasSlash) {
// ends in /, append **
patternBuilder.append(".*");
} else {
patternBuilder.append("(?:/.*)?");
}
return Pattern.compile(patternBuilder.toString());
}
public int hashCode() {
return glob.hashCode() + 13;
}
public boolean equals(final Object obj) {
return obj instanceof GlobPathFilter && equals((GlobPathFilter) obj);
}
public boolean equals(final GlobPathFilter obj) {
return obj != null && obj.pattern.equals(pattern);
}
public String toString() {
final StringBuilder b = new StringBuilder();
b.append("match ");
if (glob != null) {
b.append('"').append(glob).append('"');
} else {
b.append('/').append(pattern).append('/');
}
return b.toString();
}
}