KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > antcontrib > logic > Switch


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2001-2002 Ant-Contrib project. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Ant-Contrib project (http://sourceforge.net/projects/ant-contrib)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The name Ant-Contrib must not be used to endorse or promote products
26  * derived from this software without prior written permission. For
27  * written permission, please contact
28  * ant-contrib-developers@lists.sourceforge.net.
29  *
30  * 5. Products derived from this software may not be called "Ant-Contrib"
31  * nor may "Ant-Contrib" appear in their names without prior written
32  * permission of the Ant-Contrib project.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL THE ANT-CONTRIB PROJECT OR ITS
38  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
41  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
44  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45  * SUCH DAMAGE.
46  * ====================================================================
47  */

48 package net.sf.antcontrib.logic;
49
50 import org.apache.tools.ant.BuildException;
51 import org.apache.tools.ant.Task;
52 import org.apache.tools.ant.taskdefs.Sequential;
53 import java.util.Vector JavaDoc;
54
55 /***
56  * Task definition for the ANT task to switch on a particular value.
57  *
58  * <pre>
59  *
60  * Usage:
61  *
62  * Task declaration in the project:
63  * <code>
64  * &lt;taskdef name="switch" classname="net.sf.antcontrib.logic.Switch" /&gt;
65  * </code>
66  *
67  * Task calling syntax:
68  * <code>
69  * &lt;switch value="value" [caseinsensitive="true|false"] &gt;
70  * &lt;case value="val"&gt;
71  * &lt;property name="propname" value="propvalue" /&gt; |
72  * &lt;antcall target="targetname" /&gt; |
73  * any other tasks
74  * &lt;/case&gt;
75  * [
76  * &lt;default&gt;
77  * &lt;property name="propname" value="propvalue" /&gt; |
78  * &lt;antcall target="targetname" /&gt; |
79  * any other tasks
80  * &lt;/default&gt;
81  * ]
82  * &lt;/switch&gt;
83  * </code>
84  *
85  *
86  * Attributes:
87  * value -&gt; The value to switch on
88  * caseinsensitive -&gt; Should we do case insensitive comparisons?
89  * (default is false)
90  *
91  * Subitems:
92  * case --&gt; An individual case to consider, if the value that
93  * is being switched on matches to value attribute of
94  * the case, then the nested tasks will be executed.
95  * default --&gt; The default case for when no match is found.
96  *
97  *
98  * Crude Example:
99  *
100  * <code>
101  * &lt;switch value=&quot;${foo}&quot;&gt;
102  * &lt;case value=&quot;bar&quot;&gt;
103  * &lt;echo message=&quot;The value of property foo is bar&quot; /&gt;
104  * &lt;/case&gt;
105  * &lt;case value=&quot;baz&quot;&gt;
106  * &lt;echo message=&quot;The value of property foo is baz&quot; /&gt;
107  * &lt;/case&gt;
108  * &lt;default&gt;
109  * &lt;echo message=&quot;The value of property foo is not sensible&quot; /&gt;
110  * &lt;/default&gt;
111  * &lt;/switch&gt;
112  * </code>
113  *
114  * </pre>
115  *
116  * @author <a HREF="mailto:mattinger@mindless.com">Matthew Inger</a>
117  * @author <a HREF="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
118  */

119 public class Switch extends Task
120 {
121     private String JavaDoc value;
122     private Vector JavaDoc cases;
123     private Sequential defaultCase;
124     private boolean caseInsensitive;
125     
126     /***
127      * Default Constructor
128      */

129     public Switch()
130     {
131         cases = new Vector JavaDoc();
132     }
133
134     public void execute()
135         throws BuildException
136     {
137         if (value == null)
138             throw new BuildException("Value is missing");
139         if (cases.size() == 0 && defaultCase == null)
140             throw new BuildException("No cases supplied");
141
142         Sequential selectedCase = defaultCase;
143
144         int sz = cases.size();
145         for (int i=0;i<sz;i++)
146         {
147             Case c = (Case)(cases.elementAt(i));
148
149             String JavaDoc cvalue = c.value;
150             if (cvalue == null) {
151                 throw new BuildException("Value is required for case.");
152             }
153             String JavaDoc mvalue = value;
154
155             if (caseInsensitive)
156             {
157                 cvalue = cvalue.toUpperCase();
158                 mvalue = mvalue.toUpperCase();
159             }
160
161             if (cvalue.equals(mvalue) && c != defaultCase)
162                 selectedCase = c;
163         }
164
165         if (selectedCase == null) {
166             throw new BuildException("No case matched the value " + value
167                                      + " and no default has been specified.");
168         }
169         selectedCase.perform();
170     }
171
172     /***
173      * Sets the value being switched on
174      */

175     public void setValue(String JavaDoc value)
176     {
177         this.value = value;
178     }
179
180     public void setCaseInsensitive(boolean c)
181     {
182         caseInsensitive = c;
183     }
184
185     public final class Case extends Sequential
186     {
187         private String JavaDoc value;
188
189         public Case()
190         {
191             super();
192         }
193         
194         public void setValue(String JavaDoc value)
195         {
196             this.value = value;
197         }
198
199         public void execute()
200             throws BuildException
201         {
202             super.execute();
203         }
204
205         public boolean equals(Object JavaDoc o)
206         {
207             boolean res = false;
208             Case c = (Case)o;
209             if (c.value.equals(value))
210                 res = true;
211             return res;
212         }
213     }
214
215     /***
216      * Creates the &lt;case&gt; tag
217      */

218     public Switch.Case createCase()
219         throws BuildException
220     {
221         Switch.Case res = new Switch.Case();
222         cases.add(res);
223         return res;
224     }
225
226     /***
227      * Creates the &lt;default&gt; tag
228      */

229     public void addDefault(Sequential res)
230         throws BuildException
231     {
232         if (defaultCase != null)
233             throw new BuildException("Cannot specify multiple default cases");
234
235         defaultCase = res;
236     }
237
238 }
239
Popular Tags