From f7cd9865f411233211b03ee130cc2836732600af Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sun, 10 Oct 2010 21:09:59 +0000 Subject: [PATCH] polishing --- .../springframework/util/AntPathMatcher.java | 31 +++++++++---------- .../org/springframework/util/PathMatcher.java | 16 +++++----- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/util/AntPathMatcher.java b/org.springframework.core/src/main/java/org/springframework/util/AntPathMatcher.java index cd404f602b2..2ba5f3ca871 100644 --- a/org.springframework.core/src/main/java/org/springframework/util/AntPathMatcher.java +++ b/org.springframework.core/src/main/java/org/springframework/util/AntPathMatcher.java @@ -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"); * 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.LinkedHashMap; import java.util.Map; -import java.util.regex.Pattern; import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * 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; + /** Set the path separator to use for pattern parsing. Default is "/", as in Ant. */ public void setPathSeparator(String pathSeparator) { this.pathSeparator = (pathSeparator != null ? pathSeparator : DEFAULT_PATH_SEPARATOR); } + public boolean isPattern(String path) { return (path.indexOf('*') != -1 || path.indexOf('?') != -1); } @@ -70,19 +72,18 @@ public class AntPathMatcher implements PathMatcher { return doMatch(pattern, path, false, null); } + /** * Actually match the given path against the given pattern. - * * @param pattern the pattern to match against * @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 - * is sufficient) + * @param fullMatch whether a full pattern match is required (else a pattern match + * as far as the given base path goes is sufficient) * @return true if the supplied path matched, false if it didn't */ - protected boolean doMatch(String pattern, - String path, - boolean fullMatch, + protected boolean doMatch(String pattern, String path, boolean fullMatch, Map uriTemplateVariables) { + if (path.startsWith(this.pathSeparator) != pattern.startsWith(this.pathSeparator)) { 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:
'*' * means zero or more characters
'?' means one and only one character - * * @param pattern pattern to match against. Must not be null. * @param str string which must be matched against the pattern. Must not be null. * @return true if the string matches against the pattern, or false otherwise. @@ -271,9 +271,11 @@ public class AntPathMatcher implements PathMatcher { } /** - * Combines two patterns into a new pattern that is returned.

This implementation simply concatenates the two - * patterns, unless the first pattern 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.

For example: + * Combines two patterns into a new pattern that is returned. + *

This implementation simply concatenates the two patterns, unless the first pattern + * 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. + *

For example:

* * @@ -282,7 +284,6 @@ public class AntPathMatcher implements PathMatcher { * * *
Pattern 1Pattern 2Result
/hotels{@code * null}/hotels
{@code null}/hotels/hotels
/hotels/bookings/hotels/bookings
/hotelsbookings/hotels/bookings
/hotels/**{hotel}/hotels/**/{hotel}
/*.html/hotels.html/hotels.html
/*.html/hotels/hotels.html
/*.html/*.txtIllegalArgumentException
- * * @param pattern1 the first pattern * @param pattern2 the second pattern * @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. - * *

The returned Comparator 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 * generic patterns. So given a list with the following patterns:

  1. /hotels/new
  2. *
  3. /hotels/{hotel}
  4. /hotels/*
the returned comparator will sort this * list so that the order will be as indicated. - * *

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}. - * * @param path the full path to use for comparison * @return a comparator capable of sorting patterns in order of explicitness */ @@ -371,6 +369,7 @@ public class AntPathMatcher implements PathMatcher { return new AntPatternComparator(path); } + private static class AntPatternComparator implements Comparator { private final String path; diff --git a/org.springframework.core/src/main/java/org/springframework/util/PathMatcher.java b/org.springframework.core/src/main/java/org/springframework/util/PathMatcher.java index a51a3b466d5..5b8d4900e00 100644 --- a/org.springframework.core/src/main/java/org/springframework/util/PathMatcher.java +++ b/org.springframework.core/src/main/java/org/springframework/util/PathMatcher.java @@ -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"); * 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 '}'). *

For example: For pattern "/hotels/{hotel}" and path "/hotels/1", this method will * return a map containing "hotel"->"1". - * * @param pattern the path pattern, possibly containing URI templates * @param path the full path to extract template variables from * @return a map, containing variable names as keys; variables values as values @@ -104,11 +103,12 @@ public interface PathMatcher { Map extractUriTemplateVariables(String pattern, String path); /** - * Given a full path, returns a {@link Comparator} suitable for sorting patterns in order of explicitness for that path. - *

The full algorithm used depends on the underlying implementation, but generally, the returned - * Comparator will {@linkplain java.util.Collections#sort(java.util.List, java.util.Comparator) sort} a - * list so that more specific patterns (for that come before generic patterns. - * + * Given a full path, returns a {@link Comparator} suitable for sorting patterns + * in order of explicitness for that path. + *

The full algorithm used depends on the underlying implementation, but generally, + * the returned Comparator 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 * @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. *

The full algorithm used for combining the two pattern depends on the underlying implementation. - * * @param pattern1 the first pattern * @param pattern2 the second pattern * @return the combination of the two patterns * @throws IllegalArgumentException when the two patterns cannot be combined */ String combine(String pattern1, String pattern2); + }