KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > selectors > BaseSelectorContainer


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.types.selectors;
20
21 import java.io.File JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Project;
27 import org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector;
28
29 /**
30  * This is the base class for selectors that can contain other selectors.
31  *
32  * @since 1.5
33  */

34 public abstract class BaseSelectorContainer extends BaseSelector
35         implements SelectorContainer {
36
37     private Vector JavaDoc selectorsList = new Vector JavaDoc();
38
39     /**
40      * Default constructor.
41      */

42     public BaseSelectorContainer() {
43     }
44
45     /**
46      * Indicates whether there are any selectors here.
47      * @return true if there are selectors
48      */

49     public boolean hasSelectors() {
50         return !(selectorsList.isEmpty());
51     }
52
53     /**
54      * Gives the count of the number of selectors in this container
55      * @return the number of selectors
56      */

57     public int selectorCount() {
58         return selectorsList.size();
59     }
60
61     /**
62      * Returns the set of selectors as an array.
63      * @param p the current project
64      * @return an array of selectors
65      */

66     public FileSelector[] getSelectors(Project p) {
67         FileSelector[] result = new FileSelector[selectorsList.size()];
68         selectorsList.copyInto(result);
69         return result;
70     }
71
72     /**
73      * Returns an enumerator for accessing the set of selectors.
74      * @return an enumerator for the selectors
75      */

76     public Enumeration JavaDoc selectorElements() {
77         return selectorsList.elements();
78     }
79
80     /**
81      * Convert the Selectors within this container to a string. This will
82      * just be a helper class for the subclasses that put their own name
83      * around the contents listed here.
84      *
85      * @return comma separated list of Selectors contained in this one
86      */

87     public String JavaDoc toString() {
88         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
89         Enumeration JavaDoc e = selectorElements();
90         if (e.hasMoreElements()) {
91             while (e.hasMoreElements()) {
92                 buf.append(e.nextElement().toString());
93                 if (e.hasMoreElements()) {
94                     buf.append(", ");
95                 }
96             }
97         }
98
99         return buf.toString();
100     }
101
102     /**
103      * Add a new selector into this container.
104      *
105      * @param selector the new selector to add
106      */

107     public void appendSelector(FileSelector selector) {
108         selectorsList.addElement(selector);
109     }
110
111     /**
112      * <p>This implementation validates the container by calling
113      * verifySettings() and then validates each contained selector
114      * provided that the selector implements the validate interface.
115      * </p>
116      * <p>Ordinarily, this will validate all the elements of a selector
117      * container even if the isSelected() method of some elements is
118      * never called. This has two effects:</p>
119      * <ul>
120      * <li>Validation will often occur twice.
121      * <li>Since it is not required that selectors derive from
122      * BaseSelector, there could be selectors in the container whose
123      * error conditions are not detected if their isSelected() call
124      * is never made.
125      * </ul>
126      */

127     public void validate() {
128         verifySettings();
129         String JavaDoc errmsg = getError();
130         if (errmsg != null) {
131             throw new BuildException(errmsg);
132         }
133         Enumeration JavaDoc e = selectorElements();
134         while (e.hasMoreElements()) {
135             Object JavaDoc o = e.nextElement();
136             if (o instanceof BaseSelector) {
137                 ((BaseSelector) o).validate();
138             }
139         }
140     }
141
142
143     /**
144      * Method that each selector will implement to create their selection
145      * behaviour. This is what makes SelectorContainer abstract.
146      *
147      * @param basedir the base directory the scan is being done from
148      * @param filename the name of the file to check
149      * @param file a java.io.File object for the filename that the selector
150      * can use
151      * @return whether the file should be selected or not
152      */

153     public abstract boolean isSelected(File JavaDoc basedir, String JavaDoc filename,
154                                        File JavaDoc file);
155
156
157     /* Methods below all add specific selectors */
158
159     /**
160      * add a "Select" selector entry on the selector list
161      * @param selector the selector to add
162      */

163     public void addSelector(SelectSelector selector) {
164         appendSelector(selector);
165     }
166
167     /**
168      * add an "And" selector entry on the selector list
169      * @param selector the selector to add
170      */

171     public void addAnd(AndSelector selector) {
172         appendSelector(selector);
173     }
174
175     /**
176      * add an "Or" selector entry on the selector list
177      * @param selector the selector to add
178      */

179     public void addOr(OrSelector selector) {
180         appendSelector(selector);
181     }
182
183     /**
184      * add a "Not" selector entry on the selector list
185      * @param selector the selector to add
186      */

187     public void addNot(NotSelector selector) {
188         appendSelector(selector);
189     }
190
191     /**
192      * add a "None" selector entry on the selector list
193      * @param selector the selector to add
194      */

195     public void addNone(NoneSelector selector) {
196         appendSelector(selector);
197     }
198
199     /**
200      * add a majority selector entry on the selector list
201      * @param selector the selector to add
202      */

203     public void addMajority(MajoritySelector selector) {
204         appendSelector(selector);
205     }
206
207     /**
208      * add a selector date entry on the selector list
209      * @param selector the selector to add
210      */

211     public void addDate(DateSelector selector) {
212         appendSelector(selector);
213     }
214
215     /**
216      * add a selector size entry on the selector list
217      * @param selector the selector to add
218      */

219     public void addSize(SizeSelector selector) {
220         appendSelector(selector);
221     }
222
223     /**
224      * add a selector filename entry on the selector list
225      * @param selector the selector to add
226      */

227     public void addFilename(FilenameSelector selector) {
228         appendSelector(selector);
229     }
230
231     /**
232      * add an extended selector entry on the selector list
233      * @param selector the selector to add
234      */

235     public void addCustom(ExtendSelector selector) {
236         appendSelector(selector);
237     }
238
239     /**
240      * add a contains selector entry on the selector list
241      * @param selector the selector to add
242      */

243     public void addContains(ContainsSelector selector) {
244         appendSelector(selector);
245     }
246
247     /**
248      * add a present selector entry on the selector list
249      * @param selector the selector to add
250      */

251     public void addPresent(PresentSelector selector) {
252         appendSelector(selector);
253     }
254
255     /**
256      * add a depth selector entry on the selector list
257      * @param selector the selector to add
258      */

259     public void addDepth(DepthSelector selector) {
260         appendSelector(selector);
261     }
262
263     /**
264      * add a depends selector entry on the selector list
265      * @param selector the selector to add
266      */

267     public void addDepend(DependSelector selector) {
268         appendSelector(selector);
269     }
270
271     /**
272      * adds a different selector to the selector list
273      * @param selector the selector to add
274      */

275     public void addDifferent(DifferentSelector selector) {
276         appendSelector(selector);
277     }
278
279     /**
280      * adds a type selector to the selector list
281      * @param selector the selector to add
282      */

283     public void addType(TypeSelector selector) {
284         appendSelector(selector);
285     }
286
287     /**
288      * add a regular expression selector entry on the selector list
289      * @param selector the selector to add
290      */

291     public void addContainsRegexp(ContainsRegexpSelector selector) {
292         appendSelector(selector);
293     }
294
295     /**
296      * add the modified selector
297      * @param selector the selector to add
298      * @since ant 1.6
299      */

300     public void addModified(ModifiedSelector selector) {
301         appendSelector(selector);
302     }
303
304     /**
305      * add an arbitary selector
306      * @param selector the selector to add
307      * @since Ant 1.6
308      */

309     public void add(FileSelector selector) {
310         appendSelector(selector);
311     }
312
313 }
314
Popular Tags