polishing

This commit is contained in:
Juergen Hoeller 2010-10-10 21:09:59 +00:00
parent ebe8052d55
commit f7cd9865f4
2 changed files with 23 additions and 24 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,8 +19,8 @@ package org.springframework.util;
import java.util.Comparator; import java.util.Comparator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* PathMatcher implementation for Ant-style path patterns. Examples are provided below. * PathMatcher implementation for Ant-style path patterns. Examples are provided below.
@ -53,11 +53,13 @@ public class AntPathMatcher implements PathMatcher {
private String pathSeparator = DEFAULT_PATH_SEPARATOR; private String pathSeparator = DEFAULT_PATH_SEPARATOR;
/** Set the path separator to use for pattern parsing. Default is "/", as in Ant. */ /** Set the path separator to use for pattern parsing. Default is "/", as in Ant. */
public void setPathSeparator(String pathSeparator) { public void setPathSeparator(String pathSeparator) {
this.pathSeparator = (pathSeparator != null ? pathSeparator : DEFAULT_PATH_SEPARATOR); this.pathSeparator = (pathSeparator != null ? pathSeparator : DEFAULT_PATH_SEPARATOR);
} }
public boolean isPattern(String path) { public boolean isPattern(String path) {
return (path.indexOf('*') != -1 || path.indexOf('?') != -1); return (path.indexOf('*') != -1 || path.indexOf('?') != -1);
} }
@ -70,19 +72,18 @@ public class AntPathMatcher implements PathMatcher {
return doMatch(pattern, path, false, null); return doMatch(pattern, path, false, null);
} }
/** /**
* Actually match the given <code>path</code> against the given <code>pattern</code>. * Actually match the given <code>path</code> against the given <code>pattern</code>.
*
* @param pattern the pattern to match against * @param pattern the pattern to match against
* @param path the path String to test * @param path the path String to test
* @param fullMatch whether a full pattern match is required (else a pattern match as far as the given base path goes * @param fullMatch whether a full pattern match is required (else a pattern match
* is sufficient) * as far as the given base path goes is sufficient)
* @return <code>true</code> if the supplied <code>path</code> matched, <code>false</code> if it didn't * @return <code>true</code> if the supplied <code>path</code> matched, <code>false</code> if it didn't
*/ */
protected boolean doMatch(String pattern, protected boolean doMatch(String pattern, String path, boolean fullMatch,
String path,
boolean fullMatch,
Map<String, String> uriTemplateVariables) { Map<String, String> uriTemplateVariables) {
if (path.startsWith(this.pathSeparator) != pattern.startsWith(this.pathSeparator)) { if (path.startsWith(this.pathSeparator) != pattern.startsWith(this.pathSeparator)) {
return false; return false;
} }
@ -210,7 +211,6 @@ public class AntPathMatcher implements PathMatcher {
/** /**
* Tests whether or not a string matches against a pattern. The pattern may contain two special characters:<br> '*' * Tests whether or not a string matches against a pattern. The pattern may contain two special characters:<br> '*'
* means zero or more characters<br> '?' means one and only one character * means zero or more characters<br> '?' means one and only one character
*
* @param pattern pattern to match against. Must not be <code>null</code>. * @param pattern pattern to match against. Must not be <code>null</code>.
* @param str string which must be matched against the pattern. Must not be <code>null</code>. * @param str string which must be matched against the pattern. Must not be <code>null</code>.
* @return <code>true</code> if the string matches against the pattern, or <code>false</code> otherwise. * @return <code>true</code> if the string matches against the pattern, or <code>false</code> otherwise.
@ -271,9 +271,11 @@ public class AntPathMatcher implements PathMatcher {
} }
/** /**
* Combines two patterns into a new pattern that is returned. <p>This implementation simply concatenates the two * Combines two patterns into a new pattern that is returned.
* patterns, unless the first pattern contains a file extension match (such as {@code *.html}. In that case, the second * <p>This implementation simply concatenates the two patterns, unless the first pattern
* pattern should be included in the first, or an {@code IllegalArgumentException} is thrown. <p>For example: <table> * contains a file extension match (such as {@code *.html}. In that case, the second pattern
* should be included in the first, or an {@code IllegalArgumentException} is thrown.
* <p>For example: <table>
* <tr><th>Pattern 1</th><th>Pattern 2</th><th>Result</th></tr> <tr><td>/hotels</td><td>{@code * <tr><th>Pattern 1</th><th>Pattern 2</th><th>Result</th></tr> <tr><td>/hotels</td><td>{@code
* null}</td><td>/hotels</td></tr> <tr><td>{@code null}</td><td>/hotels</td><td>/hotels</td></tr> * null}</td><td>/hotels</td></tr> <tr><td>{@code null}</td><td>/hotels</td><td>/hotels</td></tr>
* <tr><td>/hotels</td><td>/bookings</td><td>/hotels/bookings</td></tr> <tr><td>/hotels</td><td>bookings</td><td>/hotels/bookings</td></tr> * <tr><td>/hotels</td><td>/bookings</td><td>/hotels/bookings</td></tr> <tr><td>/hotels</td><td>bookings</td><td>/hotels/bookings</td></tr>
@ -282,7 +284,6 @@ public class AntPathMatcher implements PathMatcher {
* <tr><td>/hotels/&#42;&#42;</td><td>{hotel}</td><td>/hotels/&#42;&#42;/{hotel}</td></tr> * <tr><td>/hotels/&#42;&#42;</td><td>{hotel}</td><td>/hotels/&#42;&#42;/{hotel}</td></tr>
* <tr><td>/*.html</td><td>/hotels.html</td><td>/hotels.html</td></tr> <tr><td>/*.html</td><td>/hotels</td><td>/hotels.html</td></tr> * <tr><td>/*.html</td><td>/hotels.html</td><td>/hotels.html</td></tr> <tr><td>/*.html</td><td>/hotels</td><td>/hotels.html</td></tr>
* <tr><td>/*.html</td><td>/*.txt</td><td>IllegalArgumentException</td></tr> </table> * <tr><td>/*.html</td><td>/*.txt</td><td>IllegalArgumentException</td></tr> </table>
*
* @param pattern1 the first pattern * @param pattern1 the first pattern
* @param pattern2 the second pattern * @param pattern2 the second pattern
* @return the combination of the two patterns * @return the combination of the two patterns
@ -354,16 +355,13 @@ public class AntPathMatcher implements PathMatcher {
/** /**
* Given a full path, returns a {@link Comparator} suitable for sorting patterns in order of explicitness. * Given a full path, returns a {@link Comparator} suitable for sorting patterns in order of explicitness.
*
* <p>The returned <code>Comparator</code> will {@linkplain java.util.Collections#sort(java.util.List, * <p>The returned <code>Comparator</code> will {@linkplain java.util.Collections#sort(java.util.List,
* java.util.Comparator) sort} a list so that more specific patterns (without uri templates or wild cards) come before * java.util.Comparator) sort} a list so that more specific patterns (without uri templates or wild cards) come before
* generic patterns. So given a list with the following patterns: <ol> <li><code>/hotels/new</code></li> * generic patterns. So given a list with the following patterns: <ol> <li><code>/hotels/new</code></li>
* <li><code>/hotels/{hotel}</code></li> <li><code>/hotels/*</code></li> </ol> the returned comparator will sort this * <li><code>/hotels/{hotel}</code></li> <li><code>/hotels/*</code></li> </ol> the returned comparator will sort this
* list so that the order will be as indicated. * list so that the order will be as indicated.
*
* <p>The full path given as parameter is used to test for exact matches. So when the given path is {@code /hotels/2}, * <p>The full path given as parameter is used to test for exact matches. So when the given path is {@code /hotels/2},
* the pattern {@code /hotels/2} will be sorted before {@code /hotels/1}. * the pattern {@code /hotels/2} will be sorted before {@code /hotels/1}.
*
* @param path the full path to use for comparison * @param path the full path to use for comparison
* @return a comparator capable of sorting patterns in order of explicitness * @return a comparator capable of sorting patterns in order of explicitness
*/ */
@ -371,6 +369,7 @@ public class AntPathMatcher implements PathMatcher {
return new AntPatternComparator(path); return new AntPatternComparator(path);
} }
private static class AntPatternComparator implements Comparator<String> { private static class AntPatternComparator implements Comparator<String> {
private final String path; private final String path;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -96,7 +96,6 @@ public interface PathMatcher {
* variables are expressed through curly brackets ('{' and '}'). * variables are expressed through curly brackets ('{' and '}').
* <p>For example: For pattern "/hotels/{hotel}" and path "/hotels/1", this method will * <p>For example: For pattern "/hotels/{hotel}" and path "/hotels/1", this method will
* return a map containing "hotel"->"1". * return a map containing "hotel"->"1".
*
* @param pattern the path pattern, possibly containing URI templates * @param pattern the path pattern, possibly containing URI templates
* @param path the full path to extract template variables from * @param path the full path to extract template variables from
* @return a map, containing variable names as keys; variables values as values * @return a map, containing variable names as keys; variables values as values
@ -104,11 +103,12 @@ public interface PathMatcher {
Map<String, String> extractUriTemplateVariables(String pattern, String path); Map<String, String> extractUriTemplateVariables(String pattern, String path);
/** /**
* Given a full path, returns a {@link Comparator} suitable for sorting patterns in order of explicitness for that path. * Given a full path, returns a {@link Comparator} suitable for sorting patterns
* <p>The full algorithm used depends on the underlying implementation, but generally, the returned * in order of explicitness for that path.
* <code>Comparator</code> will {@linkplain java.util.Collections#sort(java.util.List, java.util.Comparator) sort} a * <p>The full algorithm used depends on the underlying implementation, but generally,
* list so that more specific patterns (for that come before generic patterns. * the returned <code>Comparator</code> will
* * {@linkplain java.util.Collections#sort(java.util.List, java.util.Comparator) sort}
* a list so that more specific patterns come before generic patterns.
* @param path the full path to use for comparison * @param path the full path to use for comparison
* @return a comparator capable of sorting patterns in order of explicitness * @return a comparator capable of sorting patterns in order of explicitness
*/ */
@ -117,11 +117,11 @@ public interface PathMatcher {
/** /**
* Combines two patterns into a new pattern that is returned. * Combines two patterns into a new pattern that is returned.
* <p>The full algorithm used for combining the two pattern depends on the underlying implementation. * <p>The full algorithm used for combining the two pattern depends on the underlying implementation.
*
* @param pattern1 the first pattern * @param pattern1 the first pattern
* @param pattern2 the second pattern * @param pattern2 the second pattern
* @return the combination of the two patterns * @return the combination of the two patterns
* @throws IllegalArgumentException when the two patterns cannot be combined * @throws IllegalArgumentException when the two patterns cannot be combined
*/ */
String combine(String pattern1, String pattern2); String combine(String pattern1, String pattern2);
} }