]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.repo/src/org/argeo/slc/repo/internal/springutil/AntPathMatcher.java
Continue to remove dependencies with Spring.
[gpl/argeo-slc.git] / org.argeo.slc.repo / src / org / argeo / slc / repo / internal / springutil / AntPathMatcher.java
1 /*
2 * Copyright 2002-2007 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.argeo.slc.repo.internal.springutil;
18
19 /**
20 * PathMatcher implementation for Ant-style path patterns. Examples are provided
21 * below.
22 *
23 * <p>
24 * Part of this mapping code has been kindly borrowed from
25 * <a href="http://ant.apache.org">Apache Ant</a>.
26 *
27 * <p>
28 * The mapping matches URLs using the following rules:<br>
29 * <ul>
30 * <li>? matches one character</li>
31 * <li>* matches zero or more characters</li>
32 * <li>** matches zero or more 'directories' in a path</li>
33 * </ul>
34 *
35 * <p>
36 * Some examples:<br>
37 * <ul>
38 * <li><code>com/t?st.jsp</code> - matches <code>com/test.jsp</code> but also
39 * <code>com/tast.jsp</code> or <code>com/txst.jsp</code></li>
40 * <li><code>com/*.jsp</code> - matches all <code>.jsp</code> files in the
41 * <code>com</code> directory</li>
42 * <li><code>com/&#42;&#42;/test.jsp</code> - matches all <code>test.jsp</code>
43 * files underneath the <code>com</code> path</li>
44 * <li><code>org/springframework/&#42;&#42;/*.jsp</code> - matches all
45 * <code>.jsp</code> files underneath the <code>org/springframework</code>
46 * path</li>
47 * <li><code>org/&#42;&#42;/servlet/bla.jsp</code> - matches
48 * <code>org/springframework/servlet/bla.jsp</code> but also
49 * <code>org/springframework/testing/servlet/bla.jsp</code> and
50 * <code>org/servlet/bla.jsp</code></li>
51 * </ul>
52 *
53 * @author Alef Arendsen
54 * @author Juergen Hoeller
55 * @author Rob Harrop
56 * @since 16.07.2003
57 */
58 public class AntPathMatcher implements PathMatcher {
59
60 /** Default path separator: "/" */
61 public static final String DEFAULT_PATH_SEPARATOR = "/";
62
63 private String pathSeparator = DEFAULT_PATH_SEPARATOR;
64
65 /**
66 * Set the path separator to use for pattern parsing. Default is "/", as in Ant.
67 */
68 public void setPathSeparator(String pathSeparator) {
69 this.pathSeparator = (pathSeparator != null ? pathSeparator : DEFAULT_PATH_SEPARATOR);
70 }
71
72 public boolean isPattern(String path) {
73 return (path.indexOf('*') != -1 || path.indexOf('?') != -1);
74 }
75
76 public boolean match(String pattern, String path) {
77 return doMatch(pattern, path, true);
78 }
79
80 public boolean matchStart(String pattern, String path) {
81 return doMatch(pattern, path, false);
82 }
83
84 /**
85 * Actually match the given <code>path</code> against the given
86 * <code>pattern</code>.
87 *
88 * @param pattern the pattern to match against
89 * @param path the path String to test
90 * @param fullMatch whether a full pattern match is required (else a pattern
91 * match as far as the given base path goes is sufficient)
92 * @return <code>true</code> if the supplied <code>path</code> matched,
93 * <code>false</code> if it didn't
94 */
95 protected boolean doMatch(String pattern, String path, boolean fullMatch) {
96 if (path.startsWith(this.pathSeparator) != pattern.startsWith(this.pathSeparator)) {
97 return false;
98 }
99
100 // String[] pattDirs = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator);
101 // String[] pathDirs = StringUtils.tokenizeToStringArray(path, this.pathSeparator);
102 // mbaudier - 2020-03-13 : Use standard Java call:
103 String[] pattDirs = pattern.split(this.pathSeparator);
104 String[] pathDirs = path.split(this.pathSeparator);
105
106 int pattIdxStart = 0;
107 int pattIdxEnd = pattDirs.length - 1;
108 int pathIdxStart = 0;
109 int pathIdxEnd = pathDirs.length - 1;
110
111 // Match all elements up to the first **
112 while (pattIdxStart <= pattIdxEnd && pathIdxStart <= pathIdxEnd) {
113 String patDir = pattDirs[pattIdxStart];
114 if ("**".equals(patDir)) {
115 break;
116 }
117 if (!matchStrings(patDir, pathDirs[pathIdxStart])) {
118 return false;
119 }
120 pattIdxStart++;
121 pathIdxStart++;
122 }
123
124 if (pathIdxStart > pathIdxEnd) {
125 // Path is exhausted, only match if rest of pattern is * or **'s
126 if (pattIdxStart > pattIdxEnd) {
127 return (pattern.endsWith(this.pathSeparator) ? path.endsWith(this.pathSeparator)
128 : !path.endsWith(this.pathSeparator));
129 }
130 if (!fullMatch) {
131 return true;
132 }
133 if (pattIdxStart == pattIdxEnd && pattDirs[pattIdxStart].equals("*") && path.endsWith(this.pathSeparator)) {
134 return true;
135 }
136 for (int i = pattIdxStart; i <= pattIdxEnd; i++) {
137 if (!pattDirs[i].equals("**")) {
138 return false;
139 }
140 }
141 return true;
142 } else if (pattIdxStart > pattIdxEnd) {
143 // String not exhausted, but pattern is. Failure.
144 return false;
145 } else if (!fullMatch && "**".equals(pattDirs[pattIdxStart])) {
146 // Path start definitely matches due to "**" part in pattern.
147 return true;
148 }
149
150 // up to last '**'
151 while (pattIdxStart <= pattIdxEnd && pathIdxStart <= pathIdxEnd) {
152 String patDir = pattDirs[pattIdxEnd];
153 if (patDir.equals("**")) {
154 break;
155 }
156 if (!matchStrings(patDir, pathDirs[pathIdxEnd])) {
157 return false;
158 }
159 pattIdxEnd--;
160 pathIdxEnd--;
161 }
162 if (pathIdxStart > pathIdxEnd) {
163 // String is exhausted
164 for (int i = pattIdxStart; i <= pattIdxEnd; i++) {
165 if (!pattDirs[i].equals("**")) {
166 return false;
167 }
168 }
169 return true;
170 }
171
172 while (pattIdxStart != pattIdxEnd && pathIdxStart <= pathIdxEnd) {
173 int patIdxTmp = -1;
174 for (int i = pattIdxStart + 1; i <= pattIdxEnd; i++) {
175 if (pattDirs[i].equals("**")) {
176 patIdxTmp = i;
177 break;
178 }
179 }
180 if (patIdxTmp == pattIdxStart + 1) {
181 // '**/**' situation, so skip one
182 pattIdxStart++;
183 continue;
184 }
185 // Find the pattern between padIdxStart & padIdxTmp in str between
186 // strIdxStart & strIdxEnd
187 int patLength = (patIdxTmp - pattIdxStart - 1);
188 int strLength = (pathIdxEnd - pathIdxStart + 1);
189 int foundIdx = -1;
190
191 strLoop: for (int i = 0; i <= strLength - patLength; i++) {
192 for (int j = 0; j < patLength; j++) {
193 String subPat = (String) pattDirs[pattIdxStart + j + 1];
194 String subStr = (String) pathDirs[pathIdxStart + i + j];
195 if (!matchStrings(subPat, subStr)) {
196 continue strLoop;
197 }
198 }
199 foundIdx = pathIdxStart + i;
200 break;
201 }
202
203 if (foundIdx == -1) {
204 return false;
205 }
206
207 pattIdxStart = patIdxTmp;
208 pathIdxStart = foundIdx + patLength;
209 }
210
211 for (int i = pattIdxStart; i <= pattIdxEnd; i++) {
212 if (!pattDirs[i].equals("**")) {
213 return false;
214 }
215 }
216
217 return true;
218 }
219
220 /**
221 * Tests whether or not a string matches against a pattern. The pattern may
222 * contain two special characters:<br>
223 * '*' means zero or more characters<br>
224 * '?' means one and only one character
225 *
226 * @param pattern pattern to match against. Must not be <code>null</code>.
227 * @param str string which must be matched against the pattern. Must not be
228 * <code>null</code>.
229 * @return <code>true</code> if the string matches against the pattern, or
230 * <code>false</code> otherwise.
231 */
232 private boolean matchStrings(String pattern, String str) {
233 char[] patArr = pattern.toCharArray();
234 char[] strArr = str.toCharArray();
235 int patIdxStart = 0;
236 int patIdxEnd = patArr.length - 1;
237 int strIdxStart = 0;
238 int strIdxEnd = strArr.length - 1;
239 char ch;
240
241 boolean containsStar = false;
242 for (int i = 0; i < patArr.length; i++) {
243 if (patArr[i] == '*') {
244 containsStar = true;
245 break;
246 }
247 }
248
249 if (!containsStar) {
250 // No '*'s, so we make a shortcut
251 if (patIdxEnd != strIdxEnd) {
252 return false; // Pattern and string do not have the same size
253 }
254 for (int i = 0; i <= patIdxEnd; i++) {
255 ch = patArr[i];
256 if (ch != '?') {
257 if (ch != strArr[i]) {
258 return false;// Character mismatch
259 }
260 }
261 }
262 return true; // String matches against pattern
263 }
264
265 if (patIdxEnd == 0) {
266 return true; // Pattern contains only '*', which matches anything
267 }
268
269 // Process characters before first star
270 while ((ch = patArr[patIdxStart]) != '*' && strIdxStart <= strIdxEnd) {
271 if (ch != '?') {
272 if (ch != strArr[strIdxStart]) {
273 return false;// Character mismatch
274 }
275 }
276 patIdxStart++;
277 strIdxStart++;
278 }
279 if (strIdxStart > strIdxEnd) {
280 // All characters in the string are used. Check if only '*'s are
281 // left in the pattern. If so, we succeeded. Otherwise failure.
282 for (int i = patIdxStart; i <= patIdxEnd; i++) {
283 if (patArr[i] != '*') {
284 return false;
285 }
286 }
287 return true;
288 }
289
290 // Process characters after last star
291 while ((ch = patArr[patIdxEnd]) != '*' && strIdxStart <= strIdxEnd) {
292 if (ch != '?') {
293 if (ch != strArr[strIdxEnd]) {
294 return false;// Character mismatch
295 }
296 }
297 patIdxEnd--;
298 strIdxEnd--;
299 }
300 if (strIdxStart > strIdxEnd) {
301 // All characters in the string are used. Check if only '*'s are
302 // left in the pattern. If so, we succeeded. Otherwise failure.
303 for (int i = patIdxStart; i <= patIdxEnd; i++) {
304 if (patArr[i] != '*') {
305 return false;
306 }
307 }
308 return true;
309 }
310
311 // process pattern between stars. padIdxStart and patIdxEnd point
312 // always to a '*'.
313 while (patIdxStart != patIdxEnd && strIdxStart <= strIdxEnd) {
314 int patIdxTmp = -1;
315 for (int i = patIdxStart + 1; i <= patIdxEnd; i++) {
316 if (patArr[i] == '*') {
317 patIdxTmp = i;
318 break;
319 }
320 }
321 if (patIdxTmp == patIdxStart + 1) {
322 // Two stars next to each other, skip the first one.
323 patIdxStart++;
324 continue;
325 }
326 // Find the pattern between padIdxStart & padIdxTmp in str between
327 // strIdxStart & strIdxEnd
328 int patLength = (patIdxTmp - patIdxStart - 1);
329 int strLength = (strIdxEnd - strIdxStart + 1);
330 int foundIdx = -1;
331 strLoop: for (int i = 0; i <= strLength - patLength; i++) {
332 for (int j = 0; j < patLength; j++) {
333 ch = patArr[patIdxStart + j + 1];
334 if (ch != '?') {
335 if (ch != strArr[strIdxStart + i + j]) {
336 continue strLoop;
337 }
338 }
339 }
340
341 foundIdx = strIdxStart + i;
342 break;
343 }
344
345 if (foundIdx == -1) {
346 return false;
347 }
348
349 patIdxStart = patIdxTmp;
350 strIdxStart = foundIdx + patLength;
351 }
352
353 // All characters in the string are used. Check if only '*'s are left
354 // in the pattern. If so, we succeeded. Otherwise failure.
355 for (int i = patIdxStart; i <= patIdxEnd; i++) {
356 if (patArr[i] != '*') {
357 return false;
358 }
359 }
360
361 return true;
362 }
363
364 /**
365 * Given a pattern and a full path, determine the pattern-mapped part.
366 * <p>
367 * For example:
368 * <ul>
369 * <li>'<code>/docs/cvs/commit.html</code>' and
370 * '<code>/docs/cvs/commit.html</code> to ''</li>
371 * <li>'<code>/docs/*</code>' and '<code>/docs/cvs/commit</code> to
372 * '<code>cvs/commit</code>'</li>
373 * <li>'<code>/docs/cvs/*.html</code>' and '<code>/docs/cvs/commit.html</code>
374 * to '<code>commit.html</code>'</li>
375 * <li>'<code>/docs/**</code>' and '<code>/docs/cvs/commit</code> to
376 * '<code>cvs/commit</code>'</li>
377 * <li>'<code>/docs/**\/*.html</code>' and '<code>/docs/cvs/commit.html</code>
378 * to '<code>cvs/commit.html</code>'</li>
379 * <li>'<code>/*.html</code>' and '<code>/docs/cvs/commit.html</code> to
380 * '<code>docs/cvs/commit.html</code>'</li>
381 * <li>'<code>*.html</code>' and '<code>/docs/cvs/commit.html</code> to
382 * '<code>/docs/cvs/commit.html</code>'</li>
383 * <li>'<code>*</code>' and '<code>/docs/cvs/commit.html</code> to
384 * '<code>/docs/cvs/commit.html</code>'</li>
385 * </ul>
386 * <p>
387 * Assumes that {@link #match} returns <code>true</code> for
388 * '<code>pattern</code>' and '<code>path</code>', but does <strong>not</strong>
389 * enforce this.
390 */
391 public String extractPathWithinPattern(String pattern, String path) {
392 // String[] patternParts = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator);
393 // String[] pathParts = StringUtils.tokenizeToStringArray(path, this.pathSeparator);
394 // mbaudier - 2020-03-13 : Use standard Java call:
395 String[] patternParts = pattern.split(this.pathSeparator);
396 String[] pathParts = path.split(this.pathSeparator);
397
398 StringBuffer buffer = new StringBuffer();
399
400 // Add any path parts that have a wildcarded pattern part.
401 int puts = 0;
402 for (int i = 0; i < patternParts.length; i++) {
403 String patternPart = patternParts[i];
404 if ((patternPart.indexOf('*') > -1 || patternPart.indexOf('?') > -1) && pathParts.length >= i + 1) {
405 if (puts > 0 || (i == 0 && !pattern.startsWith(this.pathSeparator))) {
406 buffer.append(this.pathSeparator);
407 }
408 buffer.append(pathParts[i]);
409 puts++;
410 }
411 }
412
413 // Append any trailing path parts.
414 for (int i = patternParts.length; i < pathParts.length; i++) {
415 if (puts > 0 || i > 0) {
416 buffer.append(this.pathSeparator);
417 }
418 buffer.append(pathParts[i]);
419 }
420
421 return buffer.toString();
422 }
423
424 }