View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.util;
20  
21  import java.net.URI;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.regex.Pattern;
25  
26  public abstract class PatternMatcher
27  {
28      public abstract void matched (URI uri) throws Exception;
29      
30      
31      /**
32       * Find jar names from the provided list matching a pattern.
33       * 
34       * If the pattern is null and isNullInclusive is true, then
35       * all jar names will match.
36       * 
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. 
44       * 
45       * Eg "aaa-.*\\.jar|bbb-.*\\.jar"
46       * Will iterate over the jar names and match
47       * in any order.
48       * 
49       * Eg "aaa-*\\.jar,bbb-.*\\.jar"
50       * Will iterate over the jar names, matching
51       * all those starting with "aaa-" first, then "bbb-".
52       *
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
56       * @throws Exception
57       */
58      public void match (Pattern pattern, URI[] uris, boolean isNullInclusive)
59      throws Exception
60      {
61          if (uris!=null)
62          {
63              String[] patterns = (pattern==null?null:pattern.pattern().split(","));
64  
65              List<Pattern> subPatterns = new ArrayList<Pattern>();
66              for (int i=0; patterns!=null && i<patterns.length;i++)
67              {
68                  subPatterns.add(Pattern.compile(patterns[i]));
69              }
70              if (subPatterns.isEmpty())
71                  subPatterns.add(pattern);
72  
73              if (subPatterns.isEmpty())
74              {
75                  matchPatterns(null, uris, isNullInclusive);
76              }
77              else
78              {
79                  //for each subpattern, iterate over all the urls, processing those that match
80                  for (Pattern p : subPatterns)
81                  {
82                      matchPatterns(p, uris, isNullInclusive);
83                  }
84              }
85          }
86      }
87  
88  
89      public void matchPatterns (Pattern pattern, URI[] uris, boolean isNullInclusive)
90      throws Exception
91      {
92          for (int i=0; i<uris.length;i++)
93          {
94              URI uri = uris[i];
95              String s = uri.toString();
96              if ((pattern == null && isNullInclusive)
97                      ||
98                      (pattern!=null && pattern.matcher(s).matches()))
99              {
100                 matched(uris[i]);
101             }
102         }
103     }
104 }