1 /* 2 * Copyright 2002-2010 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.util; 18 19 import java.util.Comparator; 20 import java.util.Map; 21 22 /** 23 * Strategy interface for <code>String</code>-based path matching. 24 * 25 * <p>Used by {@link org.springframework.core.io.support.PathMatchingResourcePatternResolver}, 26 * {@link org.springframework.web.servlet.handler.AbstractUrlHandlerMapping}, 27 * {@link org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver}, 28 * and {@link org.springframework.web.servlet.mvc.WebContentInterceptor}. 29 * 30 * <p>The default implementation is {@link AntPathMatcher}, supporting the 31 * Ant-style pattern syntax. 32 * 33 * @author Juergen Hoeller 34 * @since 1.2 35 * @see AntPathMatcher 36 */ 37 public interface PathMatcher { 38 39 /** 40 * Does the given <code>path</code> represent a pattern that can be matched 41 * by an implementation of this interface? 42 * <p>If the return value is <code>false</code>, then the {@link #match} 43 * method does not have to be used because direct equality comparisons 44 * on the static path Strings will lead to the same result. 45 * @param path the path String to check 46 * @return <code>true</code> if the given <code>path</code> represents a pattern 47 */ 48 boolean isPattern(String path); 49 50 /** 51 * Match the given <code>path</code> against the given <code>pattern</code>, 52 * according to this PathMatcher's matching strategy. 53 * @param pattern the pattern to match against 54 * @param path the path String to test 55 * @return <code>true</code> if the supplied <code>path</code> matched, 56 * <code>false</code> if it didn't 57 */ 58 boolean match(String pattern, String path); 59 60 /** 61 * Match the given <code>path</code> against the corresponding part of the given 62 * <code>pattern</code>, according to this PathMatcher's matching strategy. 63 * <p>Determines whether the pattern at least matches as far as the given base 64 * path goes, assuming that a full path may then match as well. 65 * @param pattern the pattern to match against 66 * @param path the path String to test 67 * @return <code>true</code> if the supplied <code>path</code> matched, 68 * <code>false</code> if it didn't 69 */ 70 boolean matchStart(String pattern, String path); 71 72 /** 73 * Given a pattern and a full path, determine the pattern-mapped part. 74 * <p>This method is supposed to find out which part of the path is matched 75 * dynamically through an actual pattern, that is, it strips off a statically 76 * defined leading path from the given full path, returning only the actually 77 * pattern-matched part of the path. 78 * <p>For example: For "myroot/*.html" as pattern and "myroot/myfile.html" 79 * as full path, this method should return "myfile.html". The detailed 80 * determination rules are specified to this PathMatcher's matching strategy. 81 * <p>A simple implementation may return the given full path as-is in case 82 * of an actual pattern, and the empty String in case of the pattern not 83 * containing any dynamic parts (i.e. the <code>pattern</code> parameter being 84 * a static path that wouldn't qualify as an actual {@link #isPattern pattern}). 85 * A sophisticated implementation will differentiate between the static parts 86 * and the dynamic parts of the given path pattern. 87 * @param pattern the path pattern 88 * @param path the full path to introspect 89 * @return the pattern-mapped part of the given <code>path</code> 90 * (never <code>null</code>) 91 */ 92 String extractPathWithinPattern(String pattern, String path); 93 94 /** 95 * Given a pattern and a full path, extract the URI template variables. URI template 96 * variables are expressed through curly brackets ('{' and '}'). 97 * <p>For example: For pattern "/hotels/{hotel}" and path "/hotels/1", this method will 98 * return a map containing "hotel"->"1". 99 * @param pattern the path pattern, possibly containing URI templates 100 * @param path the full path to extract template variables from 101 * @return a map, containing variable names as keys; variables values as values 102 */ 103 Map<String, String> extractUriTemplateVariables(String pattern, String path); 104 105 /** 106 * Given a full path, returns a {@link Comparator} suitable for sorting patterns 107 * in order of explicitness for that path. 108 * <p>The full algorithm used depends on the underlying implementation, but generally, 109 * the returned <code>Comparator</code> will 110 * {@linkplain java.util.Collections#sort(java.util.List, java.util.Comparator) sort} 111 * a list so that more specific patterns come before generic patterns. 112 * @param path the full path to use for comparison 113 * @return a comparator capable of sorting patterns in order of explicitness 114 */ 115 Comparator<String> getPatternComparator(String path); 116 117 /** 118 * Combines two patterns into a new pattern that is returned. 119 * <p>The full algorithm used for combining the two pattern depends on the underlying implementation. 120 * @param pattern1 the first pattern 121 * @param pattern2 the second pattern 122 * @return the combination of the two patterns 123 * @throws IllegalArgumentException when the two patterns cannot be combined 124 */ 125 String combine(String pattern1, String pattern2); 126 127 }