2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.regex.Pattern;
26 public abstract class PatternMatcher
28 public abstract void matched (URI uri) throws Exception;
32 * Find jar names from the provided list matching a pattern.
34 * If the pattern is null and isNullInclusive is true, then
35 * all jar names will match.
37 * A pattern is a set of acceptable jar names. Each acceptable
38 * jar name is a regex. Each regex can be separated by either a
39 * "," or a "|". If you use a "|" this or's together the jar
40 * name patterns. This means that ordering of the matches is
41 * unimportant to you. If instead, you want to match particular
42 * jar names, and you want to match them in order, you should
43 * separate the regexs with "," instead.
45 * Eg "aaa-.*\\.jar|bbb-.*\\.jar"
46 * Will iterate over the jar names and match
49 * Eg "aaa-*\\.jar,bbb-.*\\.jar"
50 * Will iterate over the jar names, matching
51 * all those starting with "aaa-" first, then "bbb-".
53 * @param pattern the pattern
54 * @param uris the uris to test the pattern against
55 * @param isNullInclusive if true, an empty pattern means all names match, if false, none match
58 public void match (Pattern pattern, URI[] uris, boolean isNullInclusive)
63 String[] patterns = (pattern==null?null:pattern.pattern().split(","));
65 List<Pattern> subPatterns = new ArrayList<Pattern>();
66 for (int i=0; patterns!=null && i<patterns.length;i++)
68 subPatterns.add(Pattern.compile(patterns[i]));
70 if (subPatterns.isEmpty())
71 subPatterns.add(pattern);
73 if (subPatterns.isEmpty())
75 matchPatterns(null, uris, isNullInclusive);
79 //for each subpattern, iterate over all the urls, processing those that match
80 for (Pattern p : subPatterns)
82 matchPatterns(p, uris, isNullInclusive);
89 public void matchPatterns (Pattern pattern, URI[] uris, boolean isNullInclusive)
92 for (int i=0; i<uris.length;i++)
95 String s = uri.toString();
96 if ((pattern == null && isNullInclusive)
98 (pattern!=null && pattern.matcher(s).matches()))