/* * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.antcontrib.logic; import java.util.Vector; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; import org.apache.tools.ant.taskdefs.Sequential; /*** * Task definition for the ANT task to switch on a particular value. * *
 *
 * Usage:
 *
 *   Task declaration in the project:
 *   
 *     <taskdef name="switch" classname="net.sf.antcontrib.logic.Switch" />
 *   
 *
 *   Task calling syntax:
 *    
 *     <switch value="value" [caseinsensitive="true|false"] >
 *       <case value="val">
 *         <property name="propname" value="propvalue" /> |
 *         <antcall target="targetname" /> |
 *         any other tasks
 *       </case>
 *      [
 *       <default>
 *         <property name="propname" value="propvalue" /> |
 *         <antcall target="targetname" /> |
 *         any other tasks
 *       </default> 
 *      ]
 *     </switch>
 *    
 *
 *
 *   Attributes:
 *       value           -> The value to switch on
 *       caseinsensitive -> Should we do case insensitive comparisons?
 *                          (default is false)
 *
 *   Subitems:
 *       case     --> An individual case to consider, if the value that
 *                    is being switched on matches to value attribute of
 *                    the case, then the nested tasks will be executed.
 *       default  --> The default case for when no match is found.
 *
 * 
 * Crude Example:
 *
 *     
 *     <switch value="${foo}">
 *       <case value="bar">
 *         <echo message="The value of property foo is bar" />
 *       </case>
 *       <case value="baz">
 *         <echo message="The value of property foo is baz" />
 *       </case>
 *       <default>
 *         <echo message="The value of property foo is not sensible" />
 *       </default>
 *     </switch>
 *     
 *
 * 
* * @author Matthew Inger * @author Stefan Bodewig */ public class Switch extends Task { private String value; private Vector cases; private Sequential defaultCase; private boolean caseInsensitive; /*** * Default Constructor */ public Switch() { cases = new Vector(); } public void execute() throws BuildException { if (value == null) throw new BuildException("Value is missing"); if (cases.size() == 0 && defaultCase == null) throw new BuildException("No cases supplied"); Sequential selectedCase = defaultCase; int sz = cases.size(); for (int i=0;i