/*
* =========================================================================================
* Copyright 2013-2014 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 *workers are able to process multiple metrics*
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(".*");
}
return Pattern.compile(patternBuilder.toString());
}
}