1 package org.eclipse.jetty.util;
2
3 import java.net.URI;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.regex.Pattern;
7
8 public abstract class PatternMatcher
9 {
10 public abstract void matched (URI uri) throws Exception;
11
12
13 /**
14 * Find jar names from the provided list matching a pattern.
15 *
16 * If the pattern is null and isNullInclusive is true, then
17 * all jar names will match.
18 *
19 * A pattern is a set of acceptable jar names. Each acceptable
20 * jar name is a regex. Each regex can be separated by either a
21 * "," or a "|". If you use a "|" this or's together the jar
22 * name patterns. This means that ordering of the matches is
23 * unimportant to you. If instead, you want to match particular
24 * jar names, and you want to match them in order, you should
25 * separate the regexs with "," instead.
26 *
27 * Eg "aaa-.*\\.jar|bbb-.*\\.jar"
28 * Will iterate over the jar names and match
29 * in any order.
30 *
31 * Eg "aaa-*\\.jar,bbb-.*\\.jar"
32 * Will iterate over the jar names, matching
33 * all those starting with "aaa-" first, then "bbb-".
34 *
35 * @param pattern the pattern
36 * @param uris the uris to test the pattern against
37 * @param isNullInclusive if true, an empty pattern means all names match, if false, none match
38 * @throws Exception
39 */
40 public void match (Pattern pattern, URI[] uris, boolean isNullInclusive)
41 throws Exception
42 {
43 if (uris!=null)
44 {
45 String[] patterns = (pattern==null?null:pattern.pattern().split(","));
46
47 List<Pattern> subPatterns = new ArrayList<Pattern>();
48 for (int i=0; patterns!=null && i<patterns.length;i++)
49 {
50 subPatterns.add(Pattern.compile(patterns[i]));
51 }
52 if (subPatterns.isEmpty())
53 subPatterns.add(pattern);
54
55 if (subPatterns.isEmpty())
56 {
57 matchPatterns(null, uris, isNullInclusive);
58 }
59 else
60 {
61 //for each subpattern, iterate over all the urls, processing those that match
62 for (Pattern p : subPatterns)
63 {
64 matchPatterns(p, uris, isNullInclusive);
65 }
66 }
67 }
68 }
69
70
71 public void matchPatterns (Pattern pattern, URI[] uris, boolean isNullInclusive)
72 throws Exception
73 {
74 for (int i=0; i<uris.length;i++)
75 {
76 URI uri = uris[i];
77 String s = uri.toString();
78 if ((pattern == null && isNullInclusive)
79 ||
80 (pattern!=null && pattern.matcher(s).matches()))
81 {
82 matched(uris[i]);
83 }
84 }
85 }
86 }