1 28 29 package com.idaremedia.antx.condition.solo; 30 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Stack ; 34 35 import org.apache.tools.ant.BuildException; 36 import org.apache.tools.ant.Location; 37 import org.apache.tools.ant.Project; 38 import org.apache.tools.ant.taskdefs.condition.Condition; 39 import org.apache.tools.ant.types.Reference; 40 41 import com.idaremedia.antx.AntX; 42 import com.idaremedia.antx.apis.Requester; 43 44 58 59 public abstract class RuleTask extends BooleanRule 60 { 61 64 protected RuleTask() 65 { 66 super(AntX.rules,false); 67 } 68 69 70 74 protected RuleTask(String iam) 75 { 76 super(iam,false); 77 } 78 79 80 87 protected RuleTask(String iam, boolean embed) 88 { 89 super(iam,embed); 90 } 91 92 93 97 104 public final void xaddCondition(Condition c) 105 { 106 if (isReferral()) { 107 throw new BuildException 108 (uistrs().get("task.too.many.attrs"),getLocation()); 109 } 110 super.xaddCondition(c); 111 edited("addCond"); 112 } 113 114 115 126 public final boolean xaddRootCondition(Condition c) 127 { 128 if (isReferral()) { 129 throw new BuildException 130 (uistrs().get("task.too.many.attrs"),getLocation()); 131 } 132 boolean ok = super.xaddRootCondition(c); 133 if (ok) { 134 edited("addCond"); 135 } 136 return ok; 137 } 138 139 143 148 protected void setEvalResult(boolean istrue, String listing) 149 { 150 String up = getUpdateProperty(); 151 if (up!=null) { 152 if (istrue) { 153 String upv = getUpdatePropertyValue(); 154 log("RuleTask's condition true; setting '"+up+ "' to "+upv, 155 Project.MSG_DEBUG); 156 157 checkIfProperty_(up,true); 158 getProject().setNewProperty(up, upv); 159 160 } else { 161 log("RuleTask's condition false; not setting '"+up+"'", 162 Project.MSG_DEBUG); 163 } 164 } 165 } 166 167 168 175 public void verifyNoCircularDependency(Stack stk, Requester clnt) 176 { 177 if (!m_circularityChecked) { 178 if (isReferral()) { 179 ShareableCondition referral= getReferral(); 180 if (stk.contains(referral)) { 181 String refId = getReferralReference().getRefId(); 182 String ermsg = uistrs().get("brul.circular.referals",refId); 183 clnt.problem(ermsg,Project.MSG_ERR); 184 throw new BuildException(ermsg,clnt.getLocation()); 185 } 186 stk.push(referral); 187 if (referral instanceof BooleanRule) { 188 ((BooleanRule)referral).verifyNoCircularDependency(stk,clnt); 189 } 190 stk.pop(); 191 } else if (this instanceof ShareableCondition) { if (stk.contains(this)) { 193 String ermsg = uistrs().get("brul.circular.referals",getId()); 194 clnt.problem(ermsg,Project.MSG_ERR); 195 throw new BuildException(ermsg,clnt.getLocation()); 196 } 197 } 198 m_circularityChecked = true; 199 } 200 } 201 202 203 214 protected final boolean defaultSingleConditionEval() 215 throws BuildException 216 { 217 Condition c = getRootCondition(); 218 219 if (c==null) { 220 String msgid= isEmpty() 221 ? "brul.err.atleast.one.condition" 222 : "brul.err.only.one.condition"; 223 String ermsg = uistrs().get(msgid); 224 log(ermsg,Project.MSG_ERR); 225 throw new BuildException(ermsg, getLocation()); 226 } 227 228 boolean istrue = c.eval(); 229 setEvalResult(istrue,getConditionNames(1)); 230 return istrue; 231 } 232 233 234 243 protected final boolean defaultMultipleConditionEval() 244 throws BuildException 245 { 246 List l= getConditions(); 247 248 if (!l.isEmpty()) { 249 250 int N=0; 251 boolean stopASAP= isStopQuickEvaluation(); Boolean answer = null; 253 boolean isOR = Operator.OR.equals(getDefaultCombineOperator()); 254 boolean isAND = !isOR; 255 256 Iterator itr= l.iterator(); 257 while (itr.hasNext()) { 258 boolean t = ((Condition)itr.next()).eval(); 259 N++; 260 if (t) { 261 if (isOR) { 262 answer=Boolean.TRUE; 263 if (stopASAP) { 264 break; } 266 } 267 } else if (isAND) { 268 answer= Boolean.FALSE; 269 if (stopASAP) { 270 break; } 272 } 273 } 274 boolean istrue; 275 if (answer!=null) { 276 istrue= answer.booleanValue(); 277 } else if (isOR) { 278 istrue= N!=l.size(); 279 } else { 280 istrue= N==l.size(); 281 } 282 setEvalResult(istrue,getConditionNames(N)); 283 return istrue; 284 } 285 286 String ermsg= uistrs().get("brul.err.atleast.one.condition"); 287 log(ermsg,Project.MSG_ERR); 288 throw new BuildException(ermsg, getLocation()); 289 } 290 291 292 299 protected final synchronized boolean runReferral() 300 { 301 if (isReferral()) { 302 if (!m_circularityChecked) { 303 verifyNoCircularDependency(new Stack (), 304 new Requester.ForComponent(this)); 305 } 306 return true; 307 308 } else if (!m_circularityChecked) { 309 if (!isEmpty()) { 310 Stack stk= new Stack (); 311 Iterator itr= getConditions().iterator(); 312 while (itr.hasNext()) { 313 Object oc = itr.next(); 314 if (oc instanceof BooleanRule) { ((BooleanRule)oc).verifyNoCircularDependency 316 (stk, new Requester.ForComponent(this)); 317 } 318 stk.clear(); 319 } 320 } 321 m_circularityChecked = true; 322 } 323 return false; 324 } 325 326 330 338 protected final ShareableCondition getReferencedReferral(Reference r) 339 { 340 require_(r!=null,"refToObj- nonzro refid"); 341 342 Object object = r.getReferencedObject(getProject()); 343 if (object instanceof ShareableCondition) { 344 return ((ShareableCondition)object); 345 } 346 String ermsg = uistrs().get("brul.bad.ruleid",r.getRefId()); 347 log(ermsg,Project.MSG_ERR); 348 throw new BuildException(ermsg); 349 } 350 351 352 353 357 protected abstract boolean isIncompatibleReferral(ShareableCondition rule); 358 359 360 361 368 protected final void setReferralReference(Reference r) 369 { 370 require_(r!=null,"setReferalId- nonzro ref"); 371 checkModify("setReferalId"); 372 373 if (isIndependentlyEdited() || !isEmpty()) { 374 throw new BuildException 375 (uistrs().get("task.too.many.attrs"),getLocation()); 376 } 377 m_referralReference = r; 378 } 379 380 381 382 386 protected final Reference getReferralReference() 387 { 388 return m_referralReference; 389 } 390 391 392 393 397 protected boolean isReferral() 398 { 399 return getReferralReference()!=null; 400 } 401 402 403 404 410 protected final synchronized ShareableCondition getReferral() 411 { 412 if (m_referral==null && m_referralReference!=null) { 413 ShareableCondition rule = getReferencedReferral(m_referralReference); 414 if (isIncompatibleReferral(rule)) { 415 String ermsg = uistrs().get("brul.referal.mismatch", 416 m_referralReference.getRefId(),getTaskName()); 417 log(ermsg, Project.MSG_ERR); 418 throw new BuildException(ermsg,getLocation()); 419 } 420 m_referral = rule; 421 } 422 return m_referral; 423 } 424 425 426 427 431 protected final boolean isIndependentlyEdited() 432 { 433 return m_attrsEdited>0; 434 } 435 436 437 438 443 protected void edited(String what) 444 { 445 m_attrsEdited++; 446 } 447 448 449 451 461 public class Referee implements ShareableConditionUser 462 { 463 466 public Referee() 467 { } 468 469 472 public String getMsgId() 473 { 474 return RuleTask.this.getMsgId(); 475 } 476 477 480 public Location getLocation() 481 { 482 return RuleTask.this.getLocation(); 483 } 484 485 488 public String getUpdateProperty() 489 { 490 return RuleTask.this.getUpdateProperty(); 491 } 492 493 496 public String getUpdateVariable() 497 { 498 return RuleTask.this.getUpdateVariable(); 499 } 500 501 504 public String getUpdateValue() 505 { 506 return RuleTask.this.getUpdatePropertyValue(); 507 } 508 } 509 510 514 private Reference m_referralReference; private ShareableCondition m_referral; private int m_attrsEdited; 518 519 private boolean m_circularityChecked;} 521 522 523 | Popular Tags |