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.taskdefs.condition;
020:
021: import java.util.Enumeration;
022: import java.util.Vector;
023: import org.apache.tools.ant.Project;
024: import org.apache.tools.ant.DynamicElement;
025: import org.apache.tools.ant.ComponentHelper;
026: import org.apache.tools.ant.ProjectComponent;
027: import org.apache.tools.ant.taskdefs.Available;
028: import org.apache.tools.ant.taskdefs.Checksum;
029: import org.apache.tools.ant.taskdefs.UpToDate;
030:
031: /**
032: * Baseclass for the <condition> task as well as several
033: * conditions - ensures that the types of conditions inside the task
034: * and the "container" conditions are in sync.
035: *
036: * @since Ant 1.4
037: */
038: public abstract class ConditionBase extends ProjectComponent implements
039: DynamicElement {
040:
041: private static final String CONDITION_ANTLIB = "antlib:org.apache.tools.ant.types.conditions:";
042:
043: /**
044: * name of the component
045: */
046: private String taskName = "condition";
047:
048: /**
049: *
050: */
051: private Vector conditions = new Vector();
052:
053: /**
054: * Simple constructor.
055: */
056: protected ConditionBase() {
057: taskName = "component";
058: }
059:
060: /**
061: * Constructor that takes the name of the task in the task name.
062: * @param taskName the name of the task.
063: * @since Ant 1.7
064: */
065: protected ConditionBase(String taskName) {
066: this .taskName = taskName;
067: }
068:
069: /**
070: * Count the conditions.
071: *
072: * @return the number of conditions in the container
073: * @since 1.1
074: */
075: protected int countConditions() {
076: return conditions.size();
077: }
078:
079: /**
080: * Iterate through all conditions.
081: *
082: * @return an enumeration to use for iteration
083: * @since 1.1
084: */
085: protected final Enumeration getConditions() {
086: return conditions.elements();
087: }
088:
089: /**
090: * Sets the name to use in logging messages.
091: *
092: * @param name The name to use in logging messages.
093: * Should not be <code>null</code>.
094: * @since Ant 1.7
095: */
096: public void setTaskName(String name) {
097: this .taskName = name;
098: }
099:
100: /**
101: * Returns the name to use in logging messages.
102: *
103: * @return the name to use in logging messages.
104: * @since Ant 1.7
105: */
106: public String getTaskName() {
107: return taskName;
108: }
109:
110: /**
111: * Add an <available> condition.
112: * @param a an available condition
113: * @since 1.1
114: */
115: public void addAvailable(Available a) {
116: conditions.addElement(a);
117: }
118:
119: /**
120: * Add an <checksum> condition.
121: *
122: * @param c a Checksum condition
123: * @since 1.4, Ant 1.5
124: */
125: public void addChecksum(Checksum c) {
126: conditions.addElement(c);
127: }
128:
129: /**
130: * Add an <uptodate> condition.
131: *
132: * @param u an UpToDate condition
133: * @since 1.1
134: */
135: public void addUptodate(UpToDate u) {
136: conditions.addElement(u);
137: }
138:
139: /**
140: * Add an <not> condition "container".
141: *
142: * @param n a Not condition
143: * @since 1.1
144: */
145: public void addNot(Not n) {
146: conditions.addElement(n);
147: }
148:
149: /**
150: * Add an <and> condition "container".
151: *
152: * @param a an And condition
153: * @since 1.1
154: */
155: public void addAnd(And a) {
156: conditions.addElement(a);
157: }
158:
159: /**
160: * Add an <or> condition "container".
161: *
162: * @param o an Or condition
163: * @since 1.1
164: */
165: public void addOr(Or o) {
166: conditions.addElement(o);
167: }
168:
169: /**
170: * Add an <equals> condition.
171: *
172: * @param e an Equals condition
173: * @since 1.1
174: */
175: public void addEquals(Equals e) {
176: conditions.addElement(e);
177: }
178:
179: /**
180: * Add an <os> condition.
181: *
182: * @param o an Os condition
183: * @since 1.1
184: */
185: public void addOs(Os o) {
186: conditions.addElement(o);
187: }
188:
189: /**
190: * Add an <isset> condition.
191: *
192: * @param i an IsSet condition
193: * @since Ant 1.5
194: */
195: public void addIsSet(IsSet i) {
196: conditions.addElement(i);
197: }
198:
199: /**
200: * Add an <http> condition.
201: *
202: * @param h an Http condition
203: * @since Ant 1.5
204: */
205: public void addHttp(Http h) {
206: conditions.addElement(h);
207: }
208:
209: /**
210: * Add a <socket> condition.
211: *
212: * @param s a Socket condition
213: * @since Ant 1.5
214: */
215: public void addSocket(Socket s) {
216: conditions.addElement(s);
217: }
218:
219: /**
220: * Add a <filesmatch> condition.
221: *
222: * @param test a FilesMatch condition
223: * @since Ant 1.5
224: */
225: public void addFilesMatch(FilesMatch test) {
226: conditions.addElement(test);
227: }
228:
229: /**
230: * Add a <contains> condition.
231: *
232: * @param test a Contains condition
233: * @since Ant 1.5
234: */
235: public void addContains(Contains test) {
236: conditions.addElement(test);
237: }
238:
239: /**
240: * Add a <istrue> condition.
241: *
242: * @param test an IsTrue condition
243: * @since Ant 1.5
244: */
245: public void addIsTrue(IsTrue test) {
246: conditions.addElement(test);
247: }
248:
249: /**
250: * Add a <isfalse> condition.
251: *
252: * @param test an IsFalse condition
253: * @since Ant 1.5
254: */
255: public void addIsFalse(IsFalse test) {
256: conditions.addElement(test);
257: }
258:
259: /**
260: * Add an <isreference> condition.
261: *
262: * @param i an IsReference condition
263: * @since Ant 1.6
264: */
265: public void addIsReference(IsReference i) {
266: conditions.addElement(i);
267: }
268:
269: /**
270: * Add an <isfileselected> condition.
271: * @param test the condition
272: */
273: public void addIsFileSelected(IsFileSelected test) {
274: conditions.addElement(test);
275: }
276:
277: /**
278: * Add an arbitrary condition
279: * @param c a condition
280: * @since Ant 1.6
281: */
282: public void add(Condition c) {
283: conditions.addElement(c);
284: }
285:
286: /**
287: * Create a dynamically discovered condition. Built-in conditions can
288: * be discovered from the org.apache.tools.ant.taskdefs.condition
289: * antlib.
290: * @param name the condition to create.
291: * @return the dynamic condition if found, null otherwise.
292: */
293: public Object createDynamicElement(String name) {
294: Object cond = ComponentHelper.getComponentHelper(getProject())
295: .createComponent(CONDITION_ANTLIB + name);
296: if (!(cond instanceof Condition)) {
297: return null;
298: }
299: log("Dynamically discovered '" + name + "' " + cond,
300: Project.MSG_DEBUG);
301: add((Condition) cond);
302: return cond;
303: }
304:
305: }
|