1 | /*
|
---|
2 | * Copyright 2001-2005 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.taskdefs;
|
---|
19 |
|
---|
20 | import org.apache.tools.ant.BuildException;
|
---|
21 | import org.apache.tools.ant.Project;
|
---|
22 | import org.apache.tools.ant.taskdefs.condition.Condition;
|
---|
23 | import org.apache.tools.ant.taskdefs.condition.ConditionBase;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Task to set a property conditionally using <uptodate>, <available>,
|
---|
27 | * and many other supported conditions.
|
---|
28 | *
|
---|
29 | * <p>This task supports boolean logic as well as pluggable conditions
|
---|
30 | * to decide, whether a property should be set.</p>
|
---|
31 | *
|
---|
32 | * <p>This task does not extend Task to take advantage of
|
---|
33 | * ConditionBase.</p>
|
---|
34 | *
|
---|
35 | *
|
---|
36 | * @since Ant 1.4
|
---|
37 | *
|
---|
38 | * @ant.task category="control"
|
---|
39 | */
|
---|
40 | public class ConditionTask extends ConditionBase {
|
---|
41 |
|
---|
42 | private String property = null;
|
---|
43 | private String value = "true";
|
---|
44 | private String alternative = null;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * The name of the property to set. Required.
|
---|
48 | * @param p the name of the property
|
---|
49 | * @since Ant 1.4
|
---|
50 | */
|
---|
51 | public void setProperty(String p) {
|
---|
52 | property = p;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * The value for the property to set, if condition evaluates to true.
|
---|
57 | * Defaults to "true".
|
---|
58 | * @param v the value of the property
|
---|
59 | * @since Ant 1.4
|
---|
60 | */
|
---|
61 | public void setValue(String v) {
|
---|
62 | value = v;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * The value for the property to set, if condition evaluates to false.
|
---|
67 | * If this attribute is not specified, the property will not be set.
|
---|
68 | * @param e the alternate value of the property.
|
---|
69 | * @since Ant 1.6.3
|
---|
70 | */
|
---|
71 | public void setElse(String e) {
|
---|
72 | alternative = e;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * See whether our nested condition holds and set the property.
|
---|
77 | *
|
---|
78 | * @since Ant 1.4
|
---|
79 | * @exception BuildException if an error occurs
|
---|
80 | */
|
---|
81 | public void execute() throws BuildException {
|
---|
82 | if (countConditions() > 1) {
|
---|
83 | throw new BuildException("You must not nest more than one "
|
---|
84 | + "condition into <condition>");
|
---|
85 | }
|
---|
86 | if (countConditions() < 1) {
|
---|
87 | throw new BuildException("You must nest a condition into "
|
---|
88 | + "<condition>");
|
---|
89 | }
|
---|
90 | if (property == null) {
|
---|
91 | throw new BuildException("The property attribute is required.");
|
---|
92 | }
|
---|
93 | Condition c = (Condition) getConditions().nextElement();
|
---|
94 | if (c.eval()) {
|
---|
95 | log("Condition true; setting " + property + " to " + value,
|
---|
96 | Project.MSG_DEBUG);
|
---|
97 | getProject().setNewProperty(property, value);
|
---|
98 | } else if (alternative != null) {
|
---|
99 | log("Condition false; setting " + property + " to " + alternative,
|
---|
100 | Project.MSG_DEBUG);
|
---|
101 | getProject().setNewProperty(property, alternative);
|
---|
102 | } else {
|
---|
103 | log("Condition false; not setting " + property,
|
---|
104 | Project.MSG_DEBUG);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|