1 48 49 package net.sf.antcontrib.logic; 50 51 import org.apache.tools.ant.BuildException; 52 import org.apache.tools.ant.taskdefs.Sequential; 53 import org.apache.tools.ant.taskdefs.condition.Condition; 54 import org.apache.tools.ant.taskdefs.condition.ConditionBase; 55 56 107 public class IfTask extends ConditionBase { 108 109 private Sequential thenTasks = null; 110 private Sequential elseTasks = null; 111 112 118 public void addThen(Sequential t) { 119 if (thenTasks != null) { 120 throw new BuildException("You must not nest more than one <then> into <if>"); 121 } 122 thenTasks = t; 123 } 124 125 131 public void addElse(Sequential e) { 132 if (elseTasks != null) { 133 throw new BuildException("You must not nest more than one <else> into <if>"); 134 } 135 elseTasks = e; 136 } 137 138 public void execute() throws BuildException { 139 if (countConditions() > 1) { 140 throw new BuildException("You must not nest more than one condition into <if>"); 141 } 142 if (countConditions() < 1) { 143 throw new BuildException("You must nest a condition into <if>"); 144 } 145 Condition c = (Condition) getConditions().nextElement(); 146 if (c.eval()) { 147 if (thenTasks != null) { 148 thenTasks.perform(); 149 } 150 } else { 151 if (elseTasks != null) { 152 elseTasks.perform(); 153 } 154 } 155 } 156 } 157 | Popular Tags |