KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependency > RegularExpressionSelectionCriteria


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.dependency;
34
35 import java.util.*;
36
37 import org.apache.log4j.*;
38 import org.apache.oro.text.perl.*;
39
40 import com.jeantessier.text.*;
41
42 public class RegularExpressionSelectionCriteria implements SelectionCriteria {
43     private Perl5Util perl = new Perl5Util(new MaximumCapacityPatternCache());
44
45     private List globalIncludes = new LinkedList();
46     private List globalExcludes = new LinkedList();
47     private boolean matchingPackages = true;
48     private List packageIncludes = new LinkedList();
49     private List packageExcludes = new LinkedList();
50     private boolean matchingClasses = true;
51     private List classIncludes = new LinkedList();
52     private List classExcludes = new LinkedList();
53     private boolean matchingFeatures = true;
54     private List featureIncludes = new LinkedList();
55     private List featureExcludes = new LinkedList();
56     
57     public RegularExpressionSelectionCriteria() {
58         getGlobalIncludes().add("//");
59     }
60     
61     public List getGlobalIncludes() {
62         return globalIncludes;
63     }
64
65     public void setGlobalIncludes(String JavaDoc globalIncludes) {
66         setGlobalIncludes(parseRE(globalIncludes));
67     }
68     
69     public void setGlobalIncludes(List globalIncludes) {
70         this.globalIncludes = globalIncludes;
71     }
72
73     public List getGlobalExcludes() {
74         return globalExcludes;
75     }
76
77     public void setGlobalExcludes(String JavaDoc globalExcludes) {
78         setGlobalExcludes(parseRE(globalExcludes));
79     }
80
81     public void setGlobalExcludes(List globalExcludes) {
82         this.globalExcludes = globalExcludes;
83     }
84
85     public boolean isMatchingPackages() {
86         return matchingPackages;
87     }
88
89     public void setMatchingPackages(boolean matchingPackages) {
90         this.matchingPackages = matchingPackages;
91     }
92
93     public List getPackageIncludes() {
94         return packageIncludes;
95     }
96
97     public void setPackageIncludes(String JavaDoc packageIncludes) {
98         setPackageIncludes(parseRE(packageIncludes));
99     }
100
101     public void setPackageIncludes(List packageIncludes) {
102         this.packageIncludes = packageIncludes;
103     }
104
105     public List getPackageExcludes() {
106         return packageExcludes;
107     }
108
109     public void setPackageExcludes(String JavaDoc packageExcludes) {
110         setPackageExcludes(parseRE(packageExcludes));
111     }
112
113     public void setPackageExcludes(List packageExcludes) {
114         this.packageExcludes = packageExcludes;
115     }
116
117     public boolean isMatchingClasses() {
118         return matchingClasses;
119     }
120
121     public void setMatchingClasses(boolean matchingClasses) {
122         this.matchingClasses = matchingClasses;
123     }
124
125     public List getClassIncludes() {
126         return classIncludes;
127     }
128
129     public void setClassIncludes(String JavaDoc classIncludes) {
130         setClassIncludes(parseRE(classIncludes));
131     }
132
133     public void setClassIncludes(List classIncludes) {
134         this.classIncludes = classIncludes;
135     }
136
137     public List getClassExcludes() {
138         return classExcludes;
139     }
140
141     public void setClassExcludes(String JavaDoc classExcludes) {
142         setClassExcludes(parseRE(classExcludes));
143     }
144
145     public void setClassExcludes(List classExcludes) {
146         this.classExcludes = classExcludes;
147     }
148
149     public boolean isMatchingFeatures() {
150         return matchingFeatures;
151     }
152
153     public void setMatchingFeatures(boolean matchingFeatures) {
154         this.matchingFeatures = matchingFeatures;
155     }
156
157     public List getFeatureIncludes() {
158         return featureIncludes;
159     }
160
161     public void setFeatureIncludes(String JavaDoc featureIncludes) {
162         setFeatureIncludes(parseRE(featureIncludes));
163     }
164
165     public void setFeatureIncludes(List featureIncludes) {
166         this.featureIncludes = featureIncludes;
167     }
168
169     public List getFeatureExcludes() {
170         return featureExcludes;
171     }
172
173     public void setFeatureExcludes(String JavaDoc featureExcludes) {
174         setFeatureExcludes(parseRE(featureExcludes));
175     }
176
177     public void setFeatureExcludes(List featureExcludes) {
178         this.featureExcludes = featureExcludes;
179     }
180
181     public boolean matches(PackageNode node) {
182         return isMatchingPackages() && matchesPackageName(node.getName());
183     }
184     
185     public boolean matches(ClassNode node) {
186         return isMatchingClasses() && matchesClassName(node.getName());
187     }
188     
189     public boolean matches(FeatureNode node) {
190         return isMatchingFeatures() && matchesFeatureName(node.getName());
191     }
192
193     public boolean matchesPackageName(String JavaDoc name) {
194         return matches(getGlobalIncludes(), getPackageIncludes(), name) &&
195             !matches(getGlobalExcludes(), getPackageExcludes(), name);
196     }
197
198     public boolean matchesClassName(String JavaDoc name) {
199         return matches(getGlobalIncludes(), getClassIncludes(), name) &&
200             !matches(getGlobalExcludes(), getClassExcludes(), name);
201     }
202
203     public boolean matchesFeatureName(String JavaDoc name) {
204         return matches(getGlobalIncludes(), getFeatureIncludes(), name) &&
205             !matches(getGlobalExcludes(), getFeatureExcludes(), name);
206     }
207
208     private boolean matches(List globalRegularExpressions, List regularExpressions, String JavaDoc name) {
209         boolean found = false;
210         Iterator i;
211
212         i = globalRegularExpressions.iterator();
213         while (!found && i.hasNext()) {
214             String JavaDoc condition = (String JavaDoc) i.next();
215             found = perl.match(condition, name);
216         }
217
218         i = regularExpressions.iterator();
219         while (!found && i.hasNext()) {
220             String JavaDoc condition = (String JavaDoc) i.next();
221             found = perl.match(condition, name);
222         }
223
224         return found;
225     }
226
227     // Should be private, but left at package-level for the unit tests.
228
static List parseRE(String JavaDoc re) {
229         List result = new LinkedList();
230
231         Logger logger = Logger.getLogger(RegularExpressionSelectionCriteria.class);
232         logger.debug("ParseRE \"" + re + "\"");
233
234         int length = re.length();
235         int start = 0;
236         int stop = -1;
237
238         while (start < length && stop < length) {
239             String JavaDoc separator = null;
240             
241             // Locate begining & determine separator
242
while (start < length && stop < start) {
243                 if (re.charAt(start) == 'm' && (start + 1) < length) {
244                     separator = re.substring(start + 1, start + 2);
245                     stop = start + 2;
246                 } else if (re.charAt(start) == '/') {
247                     separator = "/";
248                     stop = start + 1;
249                 } else {
250                     start++;
251                 }
252             }
253
254             logger.debug("start is " + start);
255             logger.debug("separator is " + separator);
256             
257             // Locate end
258
while (stop < length && start < stop) {
259                 stop = re.indexOf(separator, stop);
260                 logger.debug("indexOf() is " + stop);
261                 
262                 if (stop == -1 || re.charAt(stop - 1) != '\\') {
263
264                     if (stop == -1) {
265                         stop = length;
266                     } else {
267                         // Look for modifiers
268
stop++;
269                         while (stop < length && (re.charAt(stop) == 'g' ||
270                                                  re.charAt(stop) == 'i' ||
271                                                  re.charAt(stop) == 'm' ||
272                                                  re.charAt(stop) == 'o' ||
273                                                  re.charAt(stop) == 's' ||
274                                                  re.charAt(stop) == 'x')) {
275                             stop++;
276                         }
277                     }
278
279                     logger.debug("stop is " + stop);
280
281                     // Add candidate
282
logger.debug("candidate is \"" + re.substring(start, stop) + "\"");
283                     result.add(re.substring(start, stop));
284             
285                     // Move start
286
start = stop + 1;
287                 } else {
288                     stop++;
289                 }
290             }
291         }
292         
293         return result;
294     }
295 }
296
Popular Tags