KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > ConditionTask


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.taskdefs;
20
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.Project;
23 import org.apache.tools.ant.taskdefs.condition.Condition;
24 import org.apache.tools.ant.taskdefs.condition.ConditionBase;
25
26 /**
27  * Task to set a property conditionally using <uptodate>, <available>,
28  * and many other supported conditions.
29  *
30  * <p>This task supports boolean logic as well as pluggable conditions
31  * to decide, whether a property should be set.</p>
32  *
33  * <p>This task does not extend Task to take advantage of
34  * ConditionBase.</p>
35  *
36  * @since Ant 1.4
37  *
38  * @ant.task category="control"
39  */

40 public class ConditionTask extends ConditionBase {
41
42     private String JavaDoc property = null;
43     private String JavaDoc value = "true";
44     private String JavaDoc alternative = null;
45
46     /**
47      * Constructor, names this task "condition".
48      */

49     public ConditionTask() {
50         super("condition");
51     }
52
53     /**
54      * The name of the property to set. Required.
55      * @param p the name of the property
56      * @since Ant 1.4
57      */

58     public void setProperty(String JavaDoc p) {
59         property = p;
60     }
61
62     /**
63      * The value for the property to set, if condition evaluates to true.
64      * Defaults to "true".
65      * @param v the value of the property
66      * @since Ant 1.4
67      */

68     public void setValue(String JavaDoc v) {
69         value = v;
70     }
71
72     /**
73      * The value for the property to set, if condition evaluates to false.
74      * If this attribute is not specified, the property will not be set.
75      * @param e the alternate value of the property.
76      * @since Ant 1.6.3
77      */

78     public void setElse(String JavaDoc e) {
79         alternative = e;
80     }
81
82     /**
83      * See whether our nested condition holds and set the property.
84      *
85      * @since Ant 1.4
86      * @exception BuildException if an error occurs
87      */

88     public void execute() throws BuildException {
89         if (countConditions() > 1) {
90             throw new BuildException("You must not nest more than one "
91                 + "condition into <"
92                 + getTaskName() + ">");
93         }
94         if (countConditions() < 1) {
95             throw new BuildException("You must nest a condition into <"
96                 + getTaskName() + ">");
97         }
98         if (property == null) {
99             throw new BuildException("The property attribute is required.");
100         }
101         Condition c = (Condition) getConditions().nextElement();
102         if (c.eval()) {
103             log("Condition true; setting " + property + " to " + value,
104                 Project.MSG_DEBUG);
105             getProject().setNewProperty(property, value);
106         } else if (alternative != null) {
107             log("Condition false; setting " + property + " to " + alternative,
108                 Project.MSG_DEBUG);
109             getProject().setNewProperty(property, alternative);
110         } else {
111             log("Condition false; not setting " + property,
112                 Project.MSG_DEBUG);
113         }
114     }
115 }
116
Popular Tags