1 28 29 package com.idaremedia.antx.condition; 30 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.StringTokenizer ; 34 35 import org.apache.tools.ant.Project; 36 import org.apache.tools.ant.taskdefs.condition.Condition; 37 38 import com.idaremedia.antx.AntXFixture; 39 import com.idaremedia.antx.AssertableProjectComponent; 40 import com.idaremedia.antx.ExportedProperties; 41 import com.idaremedia.antx.FlexString; 42 import com.idaremedia.antx.helpers.InnerString; 43 import com.idaremedia.antx.helpers.Strings; 44 import com.idaremedia.antx.helpers.Tk; 45 import com.idaremedia.antx.ownhelpers.InnerProperties; 46 import com.idaremedia.antx.parameters.Handling; 47 import com.idaremedia.antx.parameters.IgnoreWhitespaceEnabled; 48 import com.idaremedia.antx.parameters.IsA; 49 import com.idaremedia.antx.parameters.MalformedCheckEnabled; 50 import com.idaremedia.antx.parameters.PropertySource; 51 import com.idaremedia.antx.parameters.TrimEnabled; 52 53 64 65 public abstract class CheckSetCondition extends AssertableProjectComponent 66 implements Condition, TrimEnabled, IgnoreWhitespaceEnabled, MalformedCheckEnabled 67 { 68 72 protected CheckSetCondition(String iam) 73 { 74 super(iam); 75 } 76 77 78 83 protected CheckSetCondition(String iam, String properties, final Project P) 84 { 85 super(iam); 86 setProject(P); 87 setProperties(properties); 88 } 89 90 91 92 96 protected void setNegate(boolean negate) 97 { 98 m_isNegate = negate; 99 } 100 101 102 105 public final boolean isNegate() 106 { 107 return m_isNegate; 108 } 109 110 111 112 117 protected final void setTruesOnly() 118 { 119 m_truesOnly = true; 120 } 121 122 123 124 129 protected final boolean isTruesOnly() 130 { 131 return m_truesOnly; 132 } 133 134 135 139 143 public void setTrueProperty(String property) 144 { 145 m_updateProperty = property; 146 m_isVar = false; 147 } 148 149 150 154 public final String getTrueProperty() 155 { 156 return m_isVar ? null : m_updateProperty; 157 } 158 159 160 164 public void setTrueVariable(String variable) 165 { 166 m_updateProperty = variable; 167 m_isVar = true; 168 } 169 170 171 176 public final String getTrueVariable() 177 { 178 return m_isVar ? m_updateProperty : null; 179 } 180 181 182 186 public final boolean isUpdateVariable() 187 { 188 return m_isVar; 189 } 190 191 192 196 public void setWhitespace(Handling response) 197 { 198 m_ignoreWS= Handling.IGNORE.equals 199 (Handling.simplifyIgnoreOrNot(response,Handling.ACCEPT)); 200 } 201 202 203 207 public final boolean ignoreWhitespace() 208 { 209 return m_ignoreWS; 210 } 211 212 213 218 public void setTrim(boolean trim) 219 { 220 m_willTrim = trim; 221 } 222 223 224 228 public final boolean willTrim() 229 { 230 return m_willTrim; 231 } 232 233 234 241 public void setMalformed(Handling response) 242 { 243 if (Handling.isYes(response,Handling.REJECT)) { 245 m_checkUnresolved= false; 246 } 247 else { 249 m_checkUnresolved= true; 250 } 251 } 252 253 254 259 public final Handling getMalformedHandling() 260 { 261 return m_checkUnresolved ? Handling.REJECT : Handling.ACCEPT; 262 } 263 264 265 269 public final void setProperties(String properties) 270 { 271 StringTokenizer st= new StringTokenizer (properties,","); 272 InnerString holder= new InnerString(); 273 while (st.hasMoreTokens()) { 274 holder.setValue(st.nextToken()); 275 includeItem(new FlexString(holder.toString(getProject()),true)); 276 } 277 } 278 279 280 289 public final void setPropertiesLike(String pattern) 290 { 291 InnerProperties holder = new InnerProperties(); 292 holder.setProject(getProject()); 293 holder.setLike(pattern); 294 holder.setDomain(PropertySource.SCRIPT.getValue()); 295 addConfiguredProperties(holder); 296 } 297 298 299 300 305 public final void setVariables(String variables) 306 { 307 StringTokenizer st= new StringTokenizer (variables,","); 308 InnerString holder= new InnerString(); 309 while (st.hasMoreTokens()) { 310 holder.setValue(st.nextToken()); 311 FlexString item = new FlexString(holder.toString(getProject()),false); 312 item.setIsExported(true); 313 includeItem(item); 314 } 315 } 316 317 318 319 324 public final void setReferences(String references) 325 { 326 StringTokenizer st= new StringTokenizer (references,","); 327 InnerString holder= new InnerString(); 328 while (st.hasMoreTokens()) { 329 holder.setValue(st.nextToken()); 330 FlexString item = new FlexString(holder.toString(getProject()),false); 331 item.setIsReference(true); 332 includeItem(item); 333 } 334 } 335 336 337 341 346 protected final String getUpdateProperty() 347 { 348 return m_updateProperty; 349 } 350 351 352 356 List getIncludes() 357 { 358 return m_includedItems; 359 } 360 361 362 369 protected final Iterator getIncludesIterator() 370 { 371 return new FlexIncludesIterator(new FlexOuterCondition() { 372 public Project getProject() { 373 return CheckSetCondition.this.getProject(); 374 } 375 public List getIncludes() { 376 return CheckSetCondition.this.getIncludes(); 377 } 378 public boolean willTrim() { 379 return CheckSetCondition.this.willTrim(); 380 } 381 public boolean ignoreWhitespace() { 382 return false; 383 } 384 }); 385 } 386 387 388 392 protected final void includeItem(FlexString s) 393 { 394 s.setProject(getProject()); 395 s.setTrim(willTrim()); 396 getIncludes().add(s); 397 } 398 399 400 407 public final void includeItem(InnerProperties decl) 408 { 409 getIncludes().add(decl); 410 } 411 412 413 418 public void addConfiguredProperty(InnerString holder) 419 { 420 require_(holder!=null,"addP- nonzro prop"); 421 includeItem(new FlexString(holder.toString(getProject()),true)); 422 } 423 424 425 430 public void addConfiguredVariable(InnerString holder) 431 { 432 require_(holder!=null,"addV- nonzro xprop"); 433 FlexString s = new FlexString(holder.toString(getProject())); 434 s.setIsExported(true); 435 includeItem(s); 436 } 437 438 439 443 public final void addConfiguredVar(InnerString holder) 444 { 445 addConfiguredVariable(holder); 446 } 447 448 449 454 public void addConfiguredReference(InnerString holder) 455 { 456 require_(holder!=null,"addRef- nonzro refid"); 457 FlexString s = new FlexString(holder.toString(getProject())); 458 s.setIsReference(true); 459 includeItem(s); 460 } 461 462 463 470 public void addConfiguredProperties(InnerProperties decl) 471 { 472 require_(decl!=null,"addProps- nonzro decl"); 473 includeItem(decl); 474 setMalformed(Handling.REJECT); 475 } 476 477 478 482 488 public boolean eval() 489 { 490 verifyInProject_("eval"); 491 492 final Project P = getProject(); 493 boolean notsets = isNegate(); 494 boolean istrue = notsets ? true : false; 495 int ALL = getIncludes().size(); 496 497 if (ALL!=0) { 498 Boolean answer = null; 499 boolean ignoreWS = ignoreWhitespace(); 500 boolean iffTrue = isTruesOnly(); 501 Iterator itr = getIncludesIterator(); 502 int N=0; 503 504 while (itr.hasNext()) { 505 FlexString xv = (FlexString)itr.next(); 506 String v = null; 507 508 if (xv.isProperty() && m_checkUnresolved) { 509 if (LocalPropertyExaminer.verifyProperty(P,xv)) { 510 v = xv.getValue(P); 511 } else { 512 log("CheckSet dropped '"+xv.get()+"' property because"+ 513 " contains unresolved references", Project.MSG_DEBUG); 514 } 515 } else { 516 v = xv.getValue(P); 517 } 518 N++; 519 if (v!=null && ignoreWS && Tk.isWhitespace(v)) { 520 v = null; 521 } 522 if (v==null) { 523 if (notsets) { continue; } 524 answer= Boolean.FALSE; 525 break; 526 } else if (notsets) { 527 if (!iffTrue || (Tk.string2PosBool(v)==Boolean.TRUE)) { 528 answer= Boolean.FALSE; 529 break; 530 } 531 } else if (iffTrue) { 532 if (Tk.string2PosBool(v)!=Boolean.TRUE) { 533 answer= Boolean.FALSE; 534 break; 535 } 536 } 537 } 538 if (answer!=null) { 539 istrue = answer.booleanValue(); 540 } else { 541 istrue = true; 542 } 543 } 544 545 if (istrue && m_updateProperty!=null) { 546 String what = Strings.TRUE; 547 if (isUpdateVariable()) { 548 log("CheckSet was true; setting true-variable '"+m_updateProperty+"'", 549 Project.MSG_DEBUG); 550 ExportedProperties.set(m_updateProperty,what); 551 } else { 552 log("CheckSet was true; setting true-property '"+m_updateProperty+"'", 553 Project.MSG_DEBUG); 554 getProject().setNewProperty(m_updateProperty,what); 555 } 556 } 557 558 return istrue; 559 } 560 561 562 563 571 protected void xsetFromURIFragment(String uriFragment) 572 { 573 String list = uriFragment; 574 IsA isa= IsA.PROPERTY; 575 576 int i = uriFragment.lastIndexOf("?"); 577 if (i>0) { 578 list = uriFragment.substring(0,i++); 579 if (i<uriFragment.length()) { 580 String isaName = uriFragment.substring(i); 581 isa = IsA.from(isaName,isa); 582 } 583 } 584 switch (isa.getIndex()) { 585 case IsA.VARIABLE_INDEX: { 586 setVariables(list); 587 break; 588 } 589 case IsA.REFERENCE_INDEX:{ 590 setReferences(list); 591 break; 592 } 593 default: { 594 setProperties(list); 595 } 596 } 597 } 598 599 600 private String m_updateProperty; 601 private boolean m_isVar; 602 private boolean m_isNegate; 603 private boolean m_ignoreWS, m_willTrim; 604 private List m_includedItems= AntXFixture.newList(5); 605 private boolean m_checkUnresolved; 606 private boolean m_truesOnly; 607 } 608 609 610 | Popular Tags |