Debian Codegraph @

src/main/java/net/sf/antcontrib/logic/Switch.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
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 package net.sf.antcontrib.logic;
17
18 import java.util.Vector;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.Task;
22 import org.apache.tools.ant.taskdefs.Sequential;
23
24 /***
25 * Task definition for the ANT task to switch on a particular value.
26 *
27 * <pre>
28 *
29 * Usage:
30 *
31 * Task declaration in the project:
32 * <code>
33 * &lt;taskdef name="switch" classname="net.sf.antcontrib.logic.Switch" /&gt;
34 * </code>
35 *
36 * Task calling syntax:
37 * <code>
38 * &lt;switch value="value" [caseinsensitive="true|false"] &gt;
39 * &lt;case value="val"&gt;
40 * &lt;property name="propname" value="propvalue" /&gt; |
41 * &lt;antcall target="targetname" /&gt; |
42 * any other tasks
43 * &lt;/case&gt;
44 * [
45 * &lt;default&gt;
46 * &lt;property name="propname" value="propvalue" /&gt; |
47 * &lt;antcall target="targetname" /&gt; |
48 * any other tasks
49 * &lt;/default&gt;
50 * ]
51 * &lt;/switch&gt;
52 * </code>
53 *
54 *
55 * Attributes:
56 * value -&gt; The value to switch on
57 * caseinsensitive -&gt; Should we do case insensitive comparisons?
58 * (default is false)
59 *
60 * Subitems:
61 * case --&gt; An individual case to consider, if the value that
62 * is being switched on matches to value attribute of
63 * the case, then the nested tasks will be executed.
64 * default --&gt; The default case for when no match is found.
65 *
66 *
67 * Crude Example:
68 *
69 * <code>
70 * &lt;switch value=&quot;${foo}&quot;&gt;
71 * &lt;case value=&quot;bar&quot;&gt;
72 * &lt;echo message=&quot;The value of property foo is bar&quot; /&gt;
73 * &lt;/case&gt;
74 * &lt;case value=&quot;baz&quot;&gt;
75 * &lt;echo message=&quot;The value of property foo is baz&quot; /&gt;
76 * &lt;/case&gt;
77 * &lt;default&gt;
78 * &lt;echo message=&quot;The value of property foo is not sensible&quot; /&gt;
79 * &lt;/default&gt;
80 * &lt;/switch&gt;
81 * </code>
82 *
83 * </pre>
84 *
85 * @author <a href="mailto:[email protected]">Matthew Inger</a>
86 * @author <a href="mailto:[email protected]">Stefan Bodewig</a>
87 */
88 public class Switch extends Task
89 {
90 private String value;
91 private Vector cases;
92 private Sequential defaultCase;
93 private boolean caseInsensitive;
94
95 /***
96 * Default Constructor
97 */
98 public Switch()
99 {
100 cases = new Vector();
101 }
102
103 public void execute()
104 throws BuildException
105 {
106 if (value == null)
107 throw new BuildException("Value is missing");
108 if (cases.size() == 0 && defaultCase == null)
109 throw new BuildException("No cases supplied");
110
111 Sequential selectedCase = defaultCase;
112
113 int sz = cases.size();
114 for (int i=0;i<sz;i++)
115 {
116 Case c = (Case)(cases.elementAt(i));
117
118 String cvalue = c.value;
119 if (cvalue == null) {
120 throw new BuildException("Value is required for case.");
121 }
122 String mvalue = value;
123
124 if (caseInsensitive)
125 {
126 cvalue = cvalue.toUpperCase();
127 mvalue = mvalue.toUpperCase();
128 }
129
130 if (cvalue.equals(mvalue) && c != defaultCase)
131 selectedCase = c;
132 }
133
134 if (selectedCase == null) {
135 throw new BuildException("No case matched the value " + value
136 + " and no default has been specified.");
137 }
138 selectedCase.perform();
139 }
140
141 /***
142 * Sets the value being switched on
143 */
144 public void setValue(String value)
145 {
146 this.value = value;
147 }
148
149 public void setCaseInsensitive(boolean c)
150 {
151 caseInsensitive = c;
152 }
153
154 public final class Case extends Sequential
155 {
156 private String value;
157
158 public Case()
159 {
160 super();
161 }
162
163 public void setValue(String value)
164 {
165 this.value = value;
166 }
167
168 public void execute()
169 throws BuildException
170 {
171 super.execute();
172 }
173
174 public boolean equals(Object o)
175 {
176 boolean res = false;
177 Case c = (Case)o;
178 if (c.value.equals(value))
179 res = true;
180 return res;
181 }
182 }
183
184 /***
185 * Creates the &lt;case&gt; tag
186 */
187 public Switch.Case createCase()
188 throws BuildException
189 {
190 Switch.Case res = new Switch.Case();
191 cases.addElement(res);
192 return res;
193 }
194
195 /***
196 * Creates the &lt;default&gt; tag
197 */
198 public void addDefault(Sequential res)
199 throws BuildException
200 {
201 if (defaultCase != null)
202 throw new BuildException("Cannot specify multiple default cases");
203
204 defaultCase = res;
205 }
206
207 }