1 28 29 package com.idaremedia.antx.condition; 30 31 import org.apache.tools.ant.BuildException; 32 import org.apache.tools.ant.Project; 33 import org.apache.tools.ant.taskdefs.condition.Condition; 34 35 import com.idaremedia.antx.AntX; 36 import com.idaremedia.antx.AntXFixture; 37 import com.idaremedia.antx.AssertableProjectComponent; 38 import com.idaremedia.antx.go.IffAnt; 39 import com.idaremedia.antx.helpers.Strings; 40 import com.idaremedia.antx.helpers.Tk; 41 import com.idaremedia.antx.parameters.TrueFalsePropertySetter; 42 43 67 68 public class IsAntVersion extends AssertableProjectComponent 69 implements Condition, TrueFalsePropertySetter, URIable 70 { 71 74 public IsAntVersion() 75 { 76 super(AntX.conditions); 77 } 78 79 80 84 public IsAntVersion(Project P, String pattern) 85 { 86 super(AntX.conditions); 87 setProject(P); 88 setLike(pattern); 89 } 90 91 95 private static final String VERSION_RE_0= "^.*version "; 96 private static final String VERSION_RE_N= " compiled .*$"; 97 98 99 103 public void setLike(String pattern) 104 { 105 require_(pattern!=null,"setLike- nonzro pattern"); 106 m_pattern = pattern; 107 } 108 109 110 117 public final void setIs(String version) 118 { 119 require_(!Tk.isWhitespace(version),"setIs- nonzro version"); 120 121 StringBuffer sb = AntXFixture.newStringBuffer(); 122 sb.append(VERSION_RE_0); 123 124 int N= version.length(); 125 for (int i=0;i<N;i++) { 126 char c = version.charAt(i); 127 if (c=='.') { 128 sb.append("\\."); 129 } else if (c=='+' && i==N-1) { 130 sb.append("(\\.[0-9]+)?(beta[1-9]+)?"); 131 } else { 132 sb.append(c); 133 } 134 } 135 136 sb.append(VERSION_RE_N); 137 setLike(sb.substring(0)); 138 139 log("[antversion] looking for version pattern='"+m_pattern+"'", 140 Project.MSG_DEBUG); 141 sb = null; 142 } 143 144 145 149 public final String getPattern() 150 { 151 return m_pattern; 152 } 153 154 155 160 public void setFalseProperty(String property) 161 { 162 require_(property!=null,"setFalsP- nonzro nam"); 163 m_falseProperty = property; 164 } 165 166 167 171 public final String getFalseProperty() 172 { 173 return m_falseProperty; 174 } 175 176 177 182 public void setTrueProperty(String property) 183 { 184 require_(property!=null,"setTrueP- nonzro nam"); 185 m_trueProperty = property; 186 } 187 188 189 193 public final String getTrueProperty() 194 { 195 return m_trueProperty; 196 } 197 198 199 206 public void setProperty(String property) 207 { 208 m_versionProperty = property; 209 } 210 211 212 213 218 public void xsetFromURI(String fragment) 219 { 220 setLike(fragment); 221 } 222 223 224 228 233 public boolean eval() 234 { 235 verifyCanEvaluate_("eval"); 236 237 boolean istrue; 238 239 if (m_versionProperty==null) { 240 istrue = IffAnt.pass(getPattern(),getProject(),false); 241 } else { 242 istrue = IffAnt.pass(getPattern(),m_versionProperty,getProject(),false); 243 } 244 245 if (m_trueProperty!=null && istrue) { 246 log("[antversion] was true; setting true-property '"+ 247 m_trueProperty+ 248 "' property", Project.MSG_DEBUG); 249 getProject().setNewProperty(m_trueProperty,Strings.TRUE); 250 } 251 252 if (m_falseProperty!=null && !istrue) { 253 log("[antversion] was false; setting false-property '"+ 254 m_falseProperty+ 255 "' property", Project.MSG_DEBUG); 256 getProject().setNewProperty(m_falseProperty,Strings.TRUE); 257 } 258 259 return istrue; 260 } 261 262 263 269 protected void verifyCanEvaluate_(String calr) 270 { 271 super.verifyInProject_(calr); 272 273 if (getPattern()==null) { 274 String ermsg = uistrs().get("type.needs.this.attr", 275 "antversion", "pattern|is"); 276 log(ermsg,Project.MSG_ERR); 277 throw new BuildException(ermsg); 278 } 279 } 280 281 282 private String m_falseProperty; 283 private String m_trueProperty; 284 private String m_pattern; 285 private String m_versionProperty; 286 } 287 288 289 | Popular Tags |