1 | /*
|
---|
2 | * Copyright 2002-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.types.selectors;
|
---|
19 |
|
---|
20 | import java.io.File;
|
---|
21 | import java.util.Enumeration;
|
---|
22 | import java.util.Vector;
|
---|
23 |
|
---|
24 | import org.apache.tools.ant.BuildException;
|
---|
25 | import org.apache.tools.ant.Project;
|
---|
26 | import org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * This is the base class for selectors that can contain other selectors.
|
---|
30 | *
|
---|
31 | * @since 1.5
|
---|
32 | */
|
---|
33 | public abstract class BaseSelectorContainer extends BaseSelector
|
---|
34 | implements SelectorContainer {
|
---|
35 |
|
---|
36 | private Vector selectorsList = new Vector();
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Default constructor.
|
---|
40 | */
|
---|
41 | public BaseSelectorContainer() {
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Indicates whether there are any selectors here.
|
---|
46 | * @return true if there are selectors
|
---|
47 | */
|
---|
48 | public boolean hasSelectors() {
|
---|
49 | return !(selectorsList.isEmpty());
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Gives the count of the number of selectors in this container
|
---|
54 | * @return the number of selectors
|
---|
55 | */
|
---|
56 | public int selectorCount() {
|
---|
57 | return selectorsList.size();
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Returns the set of selectors as an array.
|
---|
62 | * @param p the current project
|
---|
63 | * @return an array of selectors
|
---|
64 | */
|
---|
65 | public FileSelector[] getSelectors(Project p) {
|
---|
66 | FileSelector[] result = new FileSelector[selectorsList.size()];
|
---|
67 | selectorsList.copyInto(result);
|
---|
68 | return result;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Returns an enumerator for accessing the set of selectors.
|
---|
73 | * @return an enumerator for the selectors
|
---|
74 | */
|
---|
75 | public Enumeration selectorElements() {
|
---|
76 | return selectorsList.elements();
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Convert the Selectors within this container to a string. This will
|
---|
81 | * just be a helper class for the subclasses that put their own name
|
---|
82 | * around the contents listed here.
|
---|
83 | *
|
---|
84 | * @return comma separated list of Selectors contained in this one
|
---|
85 | */
|
---|
86 | public String toString() {
|
---|
87 | StringBuffer buf = new StringBuffer();
|
---|
88 | Enumeration e = selectorElements();
|
---|
89 | if (e.hasMoreElements()) {
|
---|
90 | while (e.hasMoreElements()) {
|
---|
91 | buf.append(e.nextElement().toString());
|
---|
92 | if (e.hasMoreElements()) {
|
---|
93 | buf.append(", ");
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | return buf.toString();
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Add a new selector into this container.
|
---|
103 | *
|
---|
104 | * @param selector the new selector to add
|
---|
105 | */
|
---|
106 | public void appendSelector(FileSelector selector) {
|
---|
107 | selectorsList.addElement(selector);
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * <p>This implementation validates the container by calling
|
---|
112 | * verifySettings() and then validates each contained selector
|
---|
113 | * provided that the selector implements the validate interface.
|
---|
114 | * </p>
|
---|
115 | * <p>Ordinarily, this will validate all the elements of a selector
|
---|
116 | * container even if the isSelected() method of some elements is
|
---|
117 | * never called. This has two effects:</p>
|
---|
118 | * <ul>
|
---|
119 | * <li>Validation will often occur twice.
|
---|
120 | * <li>Since it is not required that selectors derive from
|
---|
121 | * BaseSelector, there could be selectors in the container whose
|
---|
122 | * error conditions are not detected if their isSelected() call
|
---|
123 | * is never made.
|
---|
124 | * </ul>
|
---|
125 | */
|
---|
126 | public void validate() {
|
---|
127 | verifySettings();
|
---|
128 | String errmsg = getError();
|
---|
129 | if (errmsg != null) {
|
---|
130 | throw new BuildException(errmsg);
|
---|
131 | }
|
---|
132 | Enumeration e = selectorElements();
|
---|
133 | while (e.hasMoreElements()) {
|
---|
134 | Object o = e.nextElement();
|
---|
135 | if (o instanceof BaseSelector) {
|
---|
136 | ((BaseSelector) o).validate();
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Method that each selector will implement to create their selection
|
---|
144 | * behaviour. This is what makes SelectorContainer abstract.
|
---|
145 | *
|
---|
146 | * @param basedir the base directory the scan is being done from
|
---|
147 | * @param filename the name of the file to check
|
---|
148 | * @param file a java.io.File object for the filename that the selector
|
---|
149 | * can use
|
---|
150 | * @return whether the file should be selected or not
|
---|
151 | */
|
---|
152 | public abstract boolean isSelected(File basedir, String filename,
|
---|
153 | File file);
|
---|
154 |
|
---|
155 |
|
---|
156 | /* Methods below all add specific selectors */
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * add a "Select" selector entry on the selector list
|
---|
160 | * @param selector the selector to add
|
---|
161 | */
|
---|
162 | public void addSelector(SelectSelector selector) {
|
---|
163 | appendSelector(selector);
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * add an "And" selector entry on the selector list
|
---|
168 | * @param selector the selector to add
|
---|
169 | */
|
---|
170 | public void addAnd(AndSelector selector) {
|
---|
171 | appendSelector(selector);
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * add an "Or" selector entry on the selector list
|
---|
176 | * @param selector the selector to add
|
---|
177 | */
|
---|
178 | public void addOr(OrSelector selector) {
|
---|
179 | appendSelector(selector);
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * add a "Not" selector entry on the selector list
|
---|
184 | * @param selector the selector to add
|
---|
185 | */
|
---|
186 | public void addNot(NotSelector selector) {
|
---|
187 | appendSelector(selector);
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * add a "None" selector entry on the selector list
|
---|
192 | * @param selector the selector to add
|
---|
193 | */
|
---|
194 | public void addNone(NoneSelector selector) {
|
---|
195 | appendSelector(selector);
|
---|
196 | }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * add a majority selector entry on the selector list
|
---|
200 | * @param selector the selector to add
|
---|
201 | */
|
---|
202 | public void addMajority(MajoritySelector selector) {
|
---|
203 | appendSelector(selector);
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * add a selector date entry on the selector list
|
---|
208 | * @param selector the selector to add
|
---|
209 | */
|
---|
210 | public void addDate(DateSelector selector) {
|
---|
211 | appendSelector(selector);
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * add a selector size entry on the selector list
|
---|
216 | * @param selector the selector to add
|
---|
217 | */
|
---|
218 | public void addSize(SizeSelector selector) {
|
---|
219 | appendSelector(selector);
|
---|
220 | }
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * add a selector filename entry on the selector list
|
---|
224 | * @param selector the selector to add
|
---|
225 | */
|
---|
226 | public void addFilename(FilenameSelector selector) {
|
---|
227 | appendSelector(selector);
|
---|
228 | }
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * add an extended selector entry on the selector list
|
---|
232 | * @param selector the selector to add
|
---|
233 | */
|
---|
234 | public void addCustom(ExtendSelector selector) {
|
---|
235 | appendSelector(selector);
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * add a contains selector entry on the selector list
|
---|
240 | * @param selector the selector to add
|
---|
241 | */
|
---|
242 | public void addContains(ContainsSelector selector) {
|
---|
243 | appendSelector(selector);
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * add a present selector entry on the selector list
|
---|
248 | * @param selector the selector to add
|
---|
249 | */
|
---|
250 | public void addPresent(PresentSelector selector) {
|
---|
251 | appendSelector(selector);
|
---|
252 | }
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * add a depth selector entry on the selector list
|
---|
256 | * @param selector the selector to add
|
---|
257 | */
|
---|
258 | public void addDepth(DepthSelector selector) {
|
---|
259 | appendSelector(selector);
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * add a depends selector entry on the selector list
|
---|
264 | * @param selector the selector to add
|
---|
265 | */
|
---|
266 | public void addDepend(DependSelector selector) {
|
---|
267 | appendSelector(selector);
|
---|
268 | }
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * adds a different selector to the selector list
|
---|
272 | * @param selector the selector to add
|
---|
273 | */
|
---|
274 | public void addDifferent(DifferentSelector selector) {
|
---|
275 | appendSelector(selector);
|
---|
276 | }
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * adds a type selector to the selector list
|
---|
280 | * @param selector the selector to add
|
---|
281 | */
|
---|
282 | public void addType(TypeSelector selector) {
|
---|
283 | appendSelector(selector);
|
---|
284 | }
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * add a regular expression selector entry on the selector list
|
---|
288 | * @param selector the selector to add
|
---|
289 | */
|
---|
290 | public void addContainsRegexp(ContainsRegexpSelector selector) {
|
---|
291 | appendSelector(selector);
|
---|
292 | }
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * add the modified selector
|
---|
296 | * @param selector the selector to add
|
---|
297 | * @since ant 1.6
|
---|
298 | */
|
---|
299 | public void addModified(ModifiedSelector selector) {
|
---|
300 | appendSelector(selector);
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * add an arbitary selector
|
---|
305 | * @param selector the selector to add
|
---|
306 | * @since Ant 1.6
|
---|
307 | */
|
---|
308 | public void add(FileSelector selector) {
|
---|
309 | appendSelector(selector);
|
---|
310 | }
|
---|
311 |
|
---|
312 | }
|
---|