1 18 19 package org.apache.tools.ant.taskdefs.condition; 20 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.Project; 23 import org.apache.tools.ant.util.DeweyDecimal; 24 25 29 public class AntVersion implements Condition { 30 31 private String atLeast = null; 32 private String exactly = null; 33 34 39 public boolean eval() throws BuildException { 40 validate(); 41 DeweyDecimal actual = getVersion(); 42 if (null != atLeast) { 43 return actual.isGreaterThanOrEqual(new DeweyDecimal(atLeast)); 44 } 45 if (null != exactly) { 46 return actual.isEqual(new DeweyDecimal(exactly)); 47 } 48 return false; 50 } 51 52 private void validate() throws BuildException { 53 if (atLeast != null && exactly != null) { 54 throw new BuildException("Only one of atleast or exactly may be set."); 55 } 56 if (null == atLeast && null == exactly) { 57 throw new BuildException("One of atleast or exactly must be set."); 58 } 59 try { 60 if (atLeast != null) { 61 new DeweyDecimal(atLeast); 62 } else { 63 new DeweyDecimal(exactly); 64 } 65 } catch (NumberFormatException e) { 66 throw new BuildException("The argument is not a Dewey Decimal eg 1.1.0"); 67 } 68 } 69 70 private DeweyDecimal getVersion() { 71 Project p = new Project(); 72 p.init(); 73 char[] versionString = p.getProperty("ant.version").toCharArray(); 74 StringBuffer sb = new StringBuffer (); 75 boolean foundFirstDigit = false; 76 for (int i = 0; i < versionString.length; i++) { 77 if (Character.isDigit(versionString[i])) { 78 sb.append(versionString[i]); 79 foundFirstDigit = true; 80 } 81 if (versionString[i] == '.' && foundFirstDigit) { 82 sb.append(versionString[i]); 83 } 84 if (Character.isLetter(versionString[i]) && foundFirstDigit) { 85 break; 86 } 87 } 88 return new DeweyDecimal(sb.toString()); 89 } 90 91 95 public String getAtLeast() { 96 return atLeast; 97 } 98 99 105 public void setAtLeast(String atLeast) { 106 this.atLeast = atLeast; 107 } 108 109 113 public String getExactly() { 114 return exactly; 115 } 116 117 123 public void setExactly(String exactly) { 124 this.exactly = exactly; 125 } 126 } 127 | Popular Tags |