001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.types.selectors;
020:
021: import java.util.Enumeration;
022: import java.util.Vector;
023:
024: import org.apache.tools.ant.Project;
025: import org.apache.tools.ant.types.DataType;
026: import org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector;
027:
028: /**
029: * This is the a base class a container of selectors - it does
030: * not need do be a selector itself.
031: *
032: * @since 1.7
033: */
034: public abstract class AbstractSelectorContainer extends DataType
035: implements SelectorContainer {
036:
037: private Vector selectorsList = new Vector();
038:
039: /**
040: * Indicates whether there are any selectors here.
041: * @return true if there are selectors
042: */
043: public boolean hasSelectors() {
044: return !(selectorsList.isEmpty());
045: }
046:
047: /**
048: * Gives the count of the number of selectors in this container
049: * @return the number of selectors
050: */
051: public int selectorCount() {
052: return selectorsList.size();
053: }
054:
055: /**
056: * Returns the set of selectors as an array.
057: * @param p the current project
058: * @return an array of selectors
059: */
060: public FileSelector[] getSelectors(Project p) {
061: FileSelector[] result = new FileSelector[selectorsList.size()];
062: selectorsList.copyInto(result);
063: return result;
064: }
065:
066: /**
067: * Returns an enumerator for accessing the set of selectors.
068: * @return an enumerator for the selectors
069: */
070: public Enumeration selectorElements() {
071: return selectorsList.elements();
072: }
073:
074: /**
075: * Convert the Selectors within this container to a string. This will
076: * just be a helper class for the subclasses that put their own name
077: * around the contents listed here.
078: *
079: * @return comma separated list of Selectors contained in this one
080: */
081: public String toString() {
082: StringBuffer buf = new StringBuffer();
083: Enumeration e = selectorElements();
084: if (e.hasMoreElements()) {
085: while (e.hasMoreElements()) {
086: buf.append(e.nextElement().toString());
087: if (e.hasMoreElements()) {
088: buf.append(", ");
089: }
090: }
091: }
092:
093: return buf.toString();
094: }
095:
096: /**
097: * Add a new selector into this container.
098: *
099: * @param selector the new selector to add
100: */
101: public void appendSelector(FileSelector selector) {
102: selectorsList.addElement(selector);
103: }
104:
105: /**
106: * <p>
107: * This validates each contained selector
108: * provided that the selector implements the validate interface.
109: * </p>
110: * <p>Ordinarily, this will validate all the elements of a selector
111: * container even if the isSelected() method of some elements is
112: * never called. This has two effects:</p>
113: * <ul>
114: * <li>Validation will often occur twice.
115: * <li>Since it is not required that selectors derive from
116: * BaseSelector, there could be selectors in the container whose
117: * error conditions are not detected if their isSelected() call
118: * is never made.
119: * </ul>
120: */
121: public void validate() {
122: Enumeration e = selectorElements();
123: while (e.hasMoreElements()) {
124: Object o = e.nextElement();
125: if (o instanceof BaseSelector) {
126: ((BaseSelector) o).validate();
127: }
128: }
129: }
130:
131: /* Methods below all add specific selectors */
132:
133: /**
134: * add a "Select" selector entry on the selector list
135: * @param selector the selector to add
136: */
137: public void addSelector(SelectSelector selector) {
138: appendSelector(selector);
139: }
140:
141: /**
142: * add an "And" selector entry on the selector list
143: * @param selector the selector to add
144: */
145: public void addAnd(AndSelector selector) {
146: appendSelector(selector);
147: }
148:
149: /**
150: * add an "Or" selector entry on the selector list
151: * @param selector the selector to add
152: */
153: public void addOr(OrSelector selector) {
154: appendSelector(selector);
155: }
156:
157: /**
158: * add a "Not" selector entry on the selector list
159: * @param selector the selector to add
160: */
161: public void addNot(NotSelector selector) {
162: appendSelector(selector);
163: }
164:
165: /**
166: * add a "None" selector entry on the selector list
167: * @param selector the selector to add
168: */
169: public void addNone(NoneSelector selector) {
170: appendSelector(selector);
171: }
172:
173: /**
174: * add a majority selector entry on the selector list
175: * @param selector the selector to add
176: */
177: public void addMajority(MajoritySelector selector) {
178: appendSelector(selector);
179: }
180:
181: /**
182: * add a selector date entry on the selector list
183: * @param selector the selector to add
184: */
185: public void addDate(DateSelector selector) {
186: appendSelector(selector);
187: }
188:
189: /**
190: * add a selector size entry on the selector list
191: * @param selector the selector to add
192: */
193: public void addSize(SizeSelector selector) {
194: appendSelector(selector);
195: }
196:
197: /**
198: * add a selector filename entry on the selector list
199: * @param selector the selector to add
200: */
201: public void addFilename(FilenameSelector selector) {
202: appendSelector(selector);
203: }
204:
205: /**
206: * add an extended selector entry on the selector list
207: * @param selector the selector to add
208: */
209: public void addCustom(ExtendSelector selector) {
210: appendSelector(selector);
211: }
212:
213: /**
214: * add a contains selector entry on the selector list
215: * @param selector the selector to add
216: */
217: public void addContains(ContainsSelector selector) {
218: appendSelector(selector);
219: }
220:
221: /**
222: * add a present selector entry on the selector list
223: * @param selector the selector to add
224: */
225: public void addPresent(PresentSelector selector) {
226: appendSelector(selector);
227: }
228:
229: /**
230: * add a depth selector entry on the selector list
231: * @param selector the selector to add
232: */
233: public void addDepth(DepthSelector selector) {
234: appendSelector(selector);
235: }
236:
237: /**
238: * add a depends selector entry on the selector list
239: * @param selector the selector to add
240: */
241: public void addDepend(DependSelector selector) {
242: appendSelector(selector);
243: }
244:
245: /**
246: * adds a different selector to the selector list
247: * @param selector the selector to add
248: */
249: public void addDifferent(DifferentSelector selector) {
250: appendSelector(selector);
251: }
252:
253: /**
254: * adds a type selector to the selector list
255: * @param selector the selector to add
256: */
257: public void addType(TypeSelector selector) {
258: appendSelector(selector);
259: }
260:
261: /**
262: * add a regular expression selector entry on the selector list
263: * @param selector the selector to add
264: */
265: public void addContainsRegexp(ContainsRegexpSelector selector) {
266: appendSelector(selector);
267: }
268:
269: /**
270: * add the modified selector
271: * @param selector the selector to add
272: * @since ant 1.6
273: */
274: public void addModified(ModifiedSelector selector) {
275: appendSelector(selector);
276: }
277:
278: /**
279: * add an arbitary selector
280: * @param selector the selector to add
281: * @since Ant 1.6
282: */
283: public void add(FileSelector selector) {
284: appendSelector(selector);
285: }
286:
287: }
|