SPR-5812 - Custom regex matching for @PathVariable

This commit is contained in:
Arjen Poutsma 2009-06-12 12:58:27 +00:00
parent 0096930a72
commit ed98bf0668
2 changed files with 34 additions and 7 deletions

View File

@ -23,11 +23,11 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
* Package-protected helper class for {@link AntPathMatcher}. * Package-protected helper class for {@link AntPathMatcher}. Tests whether or not a string matches against a pattern
* Tests whether or not a string matches against a pattern using a regular expression. * using a regular expression.
* *
* <p>The pattern may contain special characters: '*' means zero or more characters; * <p>The pattern may contain special characters: '*' means zero or more characters; '?' means one and only one
* '?' means one and only one character; '{' and '}' indicate a URI template pattern. * character; '{' and '}' indicate a URI template pattern.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 3.0 * @since 3.0
@ -36,6 +36,8 @@ class AntPatchStringMatcher {
private static final Pattern GLOB_PATTERN = Pattern.compile("\\?|\\*|\\{([^/]+?)\\}"); private static final Pattern GLOB_PATTERN = Pattern.compile("\\?|\\*|\\{([^/]+?)\\}");
private static final String DEFAULT_VARIABLE_PATTERN = "(.*)";
private final Pattern pattern; private final Pattern pattern;
private String str; private String str;
@ -65,13 +67,24 @@ class AntPatchStringMatcher {
patternBuilder.append(".*"); patternBuilder.append(".*");
} }
else if (match.startsWith("{") && match.endsWith("}")) { else if (match.startsWith("{") && match.endsWith("}")) {
patternBuilder.append("(.*)"); int colonIdx = match.indexOf(':');
variableNames.add(m.group(1)); if (colonIdx == -1) {
patternBuilder.append(DEFAULT_VARIABLE_PATTERN);
variableNames.add(m.group(1));
}
else {
String variablePattern = match.substring(colonIdx + 1, match.length() - 1);
patternBuilder.append('(');
patternBuilder.append(variablePattern);
patternBuilder.append(')');
String variableName = match.substring(1, colonIdx);
variableNames.add(variableName);
}
} }
end = m.end(); end = m.end();
} }
patternBuilder.append(quote(pattern, end, pattern.length())); patternBuilder.append(quote(pattern, end, pattern.length()));
return Pattern.compile(patternBuilder.toString()); return Pattern.compile(patternBuilder.toString());
} }
private String quote(String s, int start, int end) { private String quote(String s, int start, int end) {

View File

@ -334,6 +334,20 @@ public class AntPathMatcherTests {
assertEquals(expected, result); assertEquals(expected, result);
} }
@Test
public void extractUriTemplateVariablesCustomRegex() {
Map<String, String> result = pathMatcher
.extractUriTemplateVariables("{symbolicName:[\\w\\.]+}-{version:[\\w\\.]+}.jar",
"com.example-1.0.0.jar");
assertEquals("com.example", result.get("symbolicName"));
assertEquals("1.0.0", result.get("version"));
result = pathMatcher.extractUriTemplateVariables("{symbolicName:[\\w\\.]+}-sources-{version:[\\w\\.]+}.jar",
"com.example-sources-1.0.0.jar");
assertEquals("com.example", result.get("symbolicName"));
assertEquals("1.0.0", result.get("version"));
}
@Test @Test
public void combine() { public void combine() {
assertEquals("", pathMatcher.combine(null, null)); assertEquals("", pathMatcher.combine(null, null));