KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > util > BasicListMatcher


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.util;
22
23 import java.util.*;
24
25 /**
26  * This StringMatcher tests whether strings match an entry in a given list of
27  * regular expressions. The list is given as a comma-separated string or as a
28  * List of strings. An exclamation mark preceding a list entry acts as a
29  * negator: if the expression matches, a negative match is returned, without
30  * considering any subsequent entries. If none of the entries match, a positive
31  * match is returned depending on whether the last regular expression had a
32  * negator or not.
33  * <p>
34  * The individual regular expression matching is delegated to a StringMatcher
35  * that is created by the {@link #createBasicMatcher(String)} method. If it is
36  * not overridden, this method returns a BasicMatcher.
37  *
38  * @see BasicMatcher
39  * @author Eric Lafortune
40  */

41 public class BasicListMatcher implements StringMatcher
42 {
43     private static final char REGULAR_EXPRESSION_SEPARATOR = ',';
44     private static final char REGULAR_EXPRESSION_NEGATOR = '!';
45
46     private StringMatcher[] regularExpressionMatchers;
47     private boolean[] negatedRegularExpressions;
48
49
50     /**
51      * Creates a new BasicListMatcher.
52      * @param regularExpression the comma-separated list of regular expressions
53      * against which strings will be matched.
54      */

55     public BasicListMatcher(String JavaDoc regularExpression)
56     {
57         this(ListUtil.commaSeparatedList(regularExpression));
58     }
59
60
61     /**
62      * Creates a new BasicListMatcher.
63      * @param regularExpressionList the list of regular expressions against which
64      * strings will be matched.
65      */

66     public BasicListMatcher(List regularExpressionList)
67     {
68         // Collect the regular expressions in arrays.
69
int regularExpressionCount = regularExpressionList.size();
70
71         regularExpressionMatchers = new StringMatcher[regularExpressionCount];
72         negatedRegularExpressions = new boolean[regularExpressionCount];
73
74         for (int index = 0; index < regularExpressionCount; index++)
75         {
76             String JavaDoc regularExpression = (String JavaDoc)regularExpressionList.get(index);
77
78             // Does the regular expression start with an exclamation mark?
79
if (regularExpression.length() > 0 &&
80                 regularExpression.charAt(0) == REGULAR_EXPRESSION_NEGATOR)
81             {
82                 // Trim the regular expression.
83
regularExpression = regularExpression.substring(1);
84
85                 // Remember the negator.
86
negatedRegularExpressions[index] = true;
87             }
88
89             regularExpressionMatchers[index] =
90                 createBasicMatcher(regularExpression);
91         }
92     }
93
94
95     /**
96      * Creates a new StringMatcher for the given regular expression.
97      */

98     protected StringMatcher createBasicMatcher(String JavaDoc regularExpression)
99     {
100         return new BasicMatcher(regularExpression);
101     }
102
103
104     // Implementations for StringMatcher.
105

106     public boolean matches(String JavaDoc string)
107     {
108         boolean result = true;
109
110         for (int index = 0; index < regularExpressionMatchers.length; index++)
111         {
112             result = negatedRegularExpressions[index];
113
114             if (regularExpressionMatchers[index].matches(string))
115             {
116                 return !result;
117             }
118         }
119
120         return result;
121     }
122
123
124     /**
125      * A main method for testing string matching.
126      */

127     public static void main(String JavaDoc[] args)
128     {
129         try
130         {
131             System.out.println("Regular expression ["+args[0]+"]");
132             BasicListMatcher matcher =
133                 new BasicListMatcher(args[0]);
134
135             for (int index = 1; index < args.length; index++)
136             {
137                 String JavaDoc string = args[index];
138                 System.out.print("String ["+string+"]");
139                 System.out.println(" -> match = "+matcher.matches(args[index]));
140             }
141         }
142         catch (Exception JavaDoc ex)
143         {
144             ex.printStackTrace();
145         }
146     }
147 }
148
Popular Tags