1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import org.apache.tools.ant.Project; 22 import org.apache.tools.ant.Task; 23 import org.apache.tools.ant.BuildException; 24 import org.apache.tools.ant.ExitStatusException; 25 import org.apache.tools.ant.taskdefs.condition.Condition; 26 import org.apache.tools.ant.taskdefs.condition.ConditionBase; 27 28 49 public class Exit extends Task { 50 51 private static class NestedCondition extends ConditionBase implements Condition { 52 public boolean eval() { 53 if (countConditions() != 1) { 54 throw new BuildException( 55 "A single nested condition is required."); 56 } 57 return ((Condition) (getConditions().nextElement())).eval(); 58 } 59 } 60 61 private String message; 62 private String ifCondition, unlessCondition; 63 private NestedCondition nestedCondition; 64 private Integer status; 65 66 71 public void setMessage(String value) { 72 this.message = value; 73 } 74 75 79 public void setIf(String c) { 80 ifCondition = c; 81 } 82 83 88 public void setUnless(String c) { 89 unlessCondition = c; 90 } 91 92 96 public void setStatus(int i) { 97 status = new Integer (i); 98 } 99 100 112 public void execute() throws BuildException { 113 boolean fail = (nestedConditionPresent()) ? testNestedCondition() 114 : (testIfCondition() && testUnlessCondition()); 115 if (fail) { 116 String text = null; 117 if (message != null && message.trim().length() > 0) { 118 text = message.trim(); 119 } else { 120 if (ifCondition != null && ifCondition.length() > 0 121 && getProject().getProperty(ifCondition) != null) { 122 text = "if=" + ifCondition; 123 } 124 if (unlessCondition != null && unlessCondition.length() > 0 125 && getProject().getProperty(unlessCondition) == null) { 126 if (text == null) { 127 text = ""; 128 } else { 129 text += " and "; 130 } 131 text += "unless=" + unlessCondition; 132 } 133 if (nestedConditionPresent()) { 134 text = "condition satisfied"; 135 } else { 136 if (text == null) { 137 text = "No message"; 138 } 139 } 140 } 141 log("failing due to " + text, Project.MSG_DEBUG); 142 throw ((status == null) ? new BuildException(text) 143 : new ExitStatusException(text, status.intValue())); 144 } 145 } 146 147 151 public void addText(String msg) { 152 if (message == null) { 153 message = ""; 154 } 155 message += getProject().replaceProperties(msg); 156 } 157 158 163 public ConditionBase createCondition() { 164 if (nestedCondition != null) { 165 throw new BuildException("Only one nested condition is allowed."); 166 } 167 nestedCondition = new NestedCondition(); 168 return nestedCondition; 169 } 170 171 175 private boolean testIfCondition() { 176 if (ifCondition == null || "".equals(ifCondition)) { 177 return true; 178 } 179 return getProject().getProperty(ifCondition) != null; 180 } 181 182 187 private boolean testUnlessCondition() { 188 if (unlessCondition == null || "".equals(unlessCondition)) { 189 return true; 190 } 191 return getProject().getProperty(unlessCondition) == null; 192 } 193 194 198 private boolean testNestedCondition() { 199 boolean result = nestedConditionPresent(); 200 201 if (result && ifCondition != null || unlessCondition != null) { 202 throw new BuildException("Nested conditions " 203 + "not permitted in conjunction with if/unless attributes"); 204 } 205 206 return result && nestedCondition.eval(); 207 } 208 209 213 private boolean nestedConditionPresent() { 214 return (nestedCondition != null); 215 } 216 217 } 218 | Popular Tags |