1 18 19 package org.apache.tools.ant.types; 20 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.Project; 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.ListIterator ; 28 29 69 public class Assertions extends DataType implements Cloneable { 70 71 74 private Boolean enableSystemAssertions; 75 76 79 private ArrayList assertionList = new ArrayList (); 80 81 82 86 public void addEnable(EnabledAssertion assertion) { 87 checkChildrenAllowed(); 88 assertionList.add(assertion); 89 } 90 91 95 public void addDisable(DisabledAssertion assertion) { 96 checkChildrenAllowed(); 97 assertionList.add(assertion); 98 } 99 100 106 public void setEnableSystemAssertions(Boolean enableSystemAssertions) { 107 checkAttributesAllowed(); 108 this.enableSystemAssertions = enableSystemAssertions; 109 } 110 111 120 public void setRefid(Reference ref) { 121 if (assertionList.size() > 0 || enableSystemAssertions != null) { 122 throw tooManyAttributes(); 123 } 124 super.setRefid(ref); 125 } 126 127 131 private Assertions getFinalReference() { 132 if (getRefid() == null) { 133 return this; 134 } else { 135 Object o = getRefid().getReferencedObject(getProject()); 136 if (!(o instanceof Assertions)) { 137 throw new BuildException("reference is of wrong type"); 138 } 139 return (Assertions) o; 140 } 141 } 142 143 147 public int size() { 148 Assertions clause = getFinalReference(); 149 return clause.getFinalSize(); 150 } 151 152 153 157 private int getFinalSize() { 158 return assertionList.size() + (enableSystemAssertions != null ? 1 : 0); 159 } 160 161 166 public void applyAssertions(List commandList) { 167 getProject().log("Applying assertions", Project.MSG_DEBUG); 168 Assertions clause = getFinalReference(); 169 if (Boolean.TRUE.equals(clause.enableSystemAssertions)) { 171 getProject().log("Enabling system assertions", Project.MSG_DEBUG); 172 commandList.add("-enablesystemassertions"); 173 } else if (Boolean.FALSE.equals(clause.enableSystemAssertions)) { 174 getProject().log("disabling system assertions", Project.MSG_DEBUG); 175 commandList.add("-disablesystemassertions"); 176 } 177 178 Iterator it = clause.assertionList.iterator(); 180 while (it.hasNext()) { 181 BaseAssertion assertion = (BaseAssertion) it.next(); 182 String arg = assertion.toCommand(); 183 getProject().log("adding assertion " + arg, Project.MSG_DEBUG); 184 commandList.add(arg); 185 } 186 } 187 188 192 public void applyAssertions(CommandlineJava command) { 193 Assertions clause = getFinalReference(); 194 if (Boolean.TRUE.equals(clause.enableSystemAssertions)) { 196 addVmArgument(command, "-enablesystemassertions"); 197 } else if (Boolean.FALSE.equals(clause.enableSystemAssertions)) { 198 addVmArgument(command, "-disablesystemassertions"); 199 } 200 201 Iterator it = clause.assertionList.iterator(); 203 while (it.hasNext()) { 204 BaseAssertion assertion = (BaseAssertion) it.next(); 205 String arg = assertion.toCommand(); 206 addVmArgument(command, arg); 207 } 208 } 209 210 215 public void applyAssertions(final ListIterator commandIterator) { 216 getProject().log("Applying assertions", Project.MSG_DEBUG); 217 Assertions clause = getFinalReference(); 218 if (Boolean.TRUE.equals(clause.enableSystemAssertions)) { 220 getProject().log("Enabling system assertions", Project.MSG_DEBUG); 221 commandIterator.add("-enablesystemassertions"); 222 } else if (Boolean.FALSE.equals(clause.enableSystemAssertions)) { 223 getProject().log("disabling system assertions", Project.MSG_DEBUG); 224 commandIterator.add("-disablesystemassertions"); 225 } 226 227 Iterator it = clause.assertionList.iterator(); 229 while (it.hasNext()) { 230 BaseAssertion assertion = (BaseAssertion) it.next(); 231 String arg = assertion.toCommand(); 232 getProject().log("adding assertion " + arg, Project.MSG_DEBUG); 233 commandIterator.add(arg); 234 } 235 } 236 237 242 private static void addVmArgument(CommandlineJava command, String arg) { 243 Commandline.Argument argument; 244 argument = command.createVmArgument(); 245 argument.setValue(arg); 246 } 247 248 255 public Object clone() throws CloneNotSupportedException { 256 Assertions that = (Assertions) super.clone(); 257 that.assertionList = (ArrayList ) assertionList.clone(); 258 return that; 259 } 260 261 264 265 public abstract static class BaseAssertion { 266 private String packageName; 267 private String className; 268 269 273 public void setClass(String className) { 274 this.className = className; 275 } 276 277 281 public void setPackage(String packageName) { 282 this.packageName = packageName; 283 } 284 285 290 protected String getClassName() { 291 return className; 292 } 293 294 299 protected String getPackageName() { 300 return packageName; 301 } 302 303 307 public abstract String getCommandPrefix(); 308 309 314 public String toCommand() { 315 if (getPackageName() != null && getClassName() != null) { 317 throw new BuildException("Both package and class have been set"); 318 } 319 StringBuffer command = new StringBuffer (getCommandPrefix()); 320 if (getPackageName() != null) { 322 command.append(':'); 324 command.append(getPackageName()); 325 if (!command.toString().endsWith("...")) { 326 command.append("..."); 328 } 329 } else if (getClassName() != null) { 330 command.append(':'); 332 command.append(getClassName()); 333 } 334 return command.toString(); 335 } 336 } 337 338 339 342 public static class EnabledAssertion extends BaseAssertion { 343 347 public String getCommandPrefix() { 348 return "-ea"; 349 } 350 351 } 352 353 356 public static class DisabledAssertion extends BaseAssertion { 357 361 public String getCommandPrefix() { 362 return "-da"; 363 } 364 365 } 366 } 367 | Popular Tags |