1 | /* |
2 | * Copyright (c) 2001-2009, 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.oro.text.perl.*; |
38 | import org.apache.oro.text.*; |
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<String> globalIncludes = new LinkedList<String>(); |
46 | private List<String> globalExcludes = new LinkedList<String>(); |
47 | private boolean matchingPackages = true; |
48 | private List<String> packageIncludes = new LinkedList<String>(); |
49 | private List<String> packageExcludes = new LinkedList<String>(); |
50 | private boolean matchingClasses = true; |
51 | private List<String> classIncludes = new LinkedList<String>(); |
52 | private List<String> classExcludes = new LinkedList<String>(); |
53 | private boolean matchingFeatures = true; |
54 | private List<String> featureIncludes = new LinkedList<String>(); |
55 | private List<String> featureExcludes = new LinkedList<String>(); |
56 | |
57 | public RegularExpressionSelectionCriteria() { |
58 | // Do nothing |
59 | } |
60 | |
61 | public RegularExpressionSelectionCriteria(String initialGlobalIncludes) { |
62 | setGlobalIncludes(initialGlobalIncludes); |
63 | } |
64 | |
65 | public List<String> getGlobalIncludes() { |
66 | return globalIncludes; |
67 | } |
68 | |
69 | public void setGlobalIncludes(String globalIncludes) { |
70 | setGlobalIncludes(RegularExpressionParser.parseRE(globalIncludes)); |
71 | } |
72 | |
73 | public void setGlobalIncludes(List<String> globalIncludes) { |
74 | this.globalIncludes = globalIncludes; |
75 | } |
76 | |
77 | public List<String> getGlobalExcludes() { |
78 | return globalExcludes; |
79 | } |
80 | |
81 | public void setGlobalExcludes(String globalExcludes) { |
82 | setGlobalExcludes(RegularExpressionParser.parseRE(globalExcludes)); |
83 | } |
84 | |
85 | public void setGlobalExcludes(List<String> globalExcludes) { |
86 | this.globalExcludes = globalExcludes; |
87 | } |
88 | |
89 | public boolean isMatchingPackages() { |
90 | return matchingPackages; |
91 | } |
92 | |
93 | public void setMatchingPackages(boolean matchingPackages) { |
94 | this.matchingPackages = matchingPackages; |
95 | } |
96 | |
97 | public List<String> getPackageIncludes() { |
98 | return packageIncludes; |
99 | } |
100 | |
101 | public void setPackageIncludes(String packageIncludes) { |
102 | setPackageIncludes(RegularExpressionParser.parseRE(packageIncludes)); |
103 | } |
104 | |
105 | public void setPackageIncludes(List<String> packageIncludes) { |
106 | this.packageIncludes = packageIncludes; |
107 | } |
108 | |
109 | public List<String> getPackageExcludes() { |
110 | return packageExcludes; |
111 | } |
112 | |
113 | public void setPackageExcludes(String packageExcludes) { |
114 | setPackageExcludes(RegularExpressionParser.parseRE(packageExcludes)); |
115 | } |
116 | |
117 | public void setPackageExcludes(List<String> packageExcludes) { |
118 | this.packageExcludes = packageExcludes; |
119 | } |
120 | |
121 | public boolean isMatchingClasses() { |
122 | return matchingClasses; |
123 | } |
124 | |
125 | public void setMatchingClasses(boolean matchingClasses) { |
126 | this.matchingClasses = matchingClasses; |
127 | } |
128 | |
129 | public List<String> getClassIncludes() { |
130 | return classIncludes; |
131 | } |
132 | |
133 | public void setClassIncludes(String classIncludes) { |
134 | setClassIncludes(RegularExpressionParser.parseRE(classIncludes)); |
135 | } |
136 | |
137 | public void setClassIncludes(List<String> classIncludes) { |
138 | this.classIncludes = classIncludes; |
139 | } |
140 | |
141 | public List<String> getClassExcludes() { |
142 | return classExcludes; |
143 | } |
144 | |
145 | public void setClassExcludes(String classExcludes) { |
146 | setClassExcludes(RegularExpressionParser.parseRE(classExcludes)); |
147 | } |
148 | |
149 | public void setClassExcludes(List<String> classExcludes) { |
150 | this.classExcludes = classExcludes; |
151 | } |
152 | |
153 | public boolean isMatchingFeatures() { |
154 | return matchingFeatures; |
155 | } |
156 | |
157 | public void setMatchingFeatures(boolean matchingFeatures) { |
158 | this.matchingFeatures = matchingFeatures; |
159 | } |
160 | |
161 | public List<String> getFeatureIncludes() { |
162 | return featureIncludes; |
163 | } |
164 | |
165 | public void setFeatureIncludes(String featureIncludes) { |
166 | setFeatureIncludes(RegularExpressionParser.parseRE(featureIncludes)); |
167 | } |
168 | |
169 | public void setFeatureIncludes(List<String> featureIncludes) { |
170 | this.featureIncludes = featureIncludes; |
171 | } |
172 | |
173 | public List<String> getFeatureExcludes() { |
174 | return featureExcludes; |
175 | } |
176 | |
177 | public void setFeatureExcludes(String featureExcludes) { |
178 | setFeatureExcludes(RegularExpressionParser.parseRE(featureExcludes)); |
179 | } |
180 | |
181 | public void setFeatureExcludes(List<String> featureExcludes) { |
182 | this.featureExcludes = featureExcludes; |
183 | } |
184 | |
185 | public boolean matches(PackageNode node) { |
186 | return isMatchingPackages() && matchesPackageName(node.getName()); |
187 | } |
188 | |
189 | public boolean matches(ClassNode node) { |
190 | return isMatchingClasses() && matchesClassName(node.getName()); |
191 | } |
192 | |
193 | public boolean matches(FeatureNode node) { |
194 | return isMatchingFeatures() && matchesFeatureName(node.getName()); |
195 | } |
196 | |
197 | public boolean matchesPackageName(String name) { |
198 | return matches(getGlobalIncludes(), getPackageIncludes(), name) && |
199 | !matches(getGlobalExcludes(), getPackageExcludes(), name); |
200 | } |
201 | |
202 | public boolean matchesClassName(String name) { |
203 | return matches(getGlobalIncludes(), getClassIncludes(), name) && |
204 | !matches(getGlobalExcludes(), getClassExcludes(), name); |
205 | } |
206 | |
207 | public boolean matchesFeatureName(String name) { |
208 | return matches(getGlobalIncludes(), getFeatureIncludes(), name) && |
209 | !matches(getGlobalExcludes(), getFeatureExcludes(), name); |
210 | } |
211 | |
212 | private boolean matches(List<String> globalRegularExpressions, List<String> regularExpressions, String name) { |
213 | boolean found = false; |
214 | Iterator<String> i; |
215 | |
216 | i = globalRegularExpressions.iterator(); |
217 | while (!found && i.hasNext()) { |
218 | String regex = i.next(); |
219 | try { |
220 | found = perl.match(regex, name); |
221 | } catch (MalformedCachePatternException ex) { |
222 | throw new MatchException(regex, ex); |
223 | } |
224 | } |
225 | |
226 | i = regularExpressions.iterator(); |
227 | while (!found && i.hasNext()) { |
228 | String regex = i.next(); |
229 | try { |
230 | found = perl.match(regex, name); |
231 | } catch (MalformedCachePatternException ex) { |
232 | throw new MatchException(regex, ex); |
233 | } |
234 | } |
235 | |
236 | return found; |
237 | } |
238 | } |