1 28 29 package com.idaremedia.antx.solo; 30 31 import java.text.DecimalFormat ; 32 33 import org.apache.tools.ant.BuildException; 34 import org.apache.tools.ant.Project; 35 36 import com.idaremedia.antx.AntX; 37 import com.idaremedia.antx.AssertableTask; 38 import com.idaremedia.antx.ExportScope; 39 import com.idaremedia.antx.FlexString; 40 import com.idaremedia.antx.apis.AntLibFriendly; 41 import com.idaremedia.antx.helpers.Tk; 42 import com.idaremedia.antx.parameters.TransformEnabled; 43 import com.idaremedia.antx.parameters.TransformHelper; 44 import com.idaremedia.antx.parameters.ValueTransform; 45 46 70 71 public class ExportTask extends AssertableTask 72 implements TransformEnabled, AntLibFriendly 73 { 74 private static final String SRC_ATTRS = "value|fromproperty|fromvariable"; 75 76 80 83 public ExportTask() 84 { 85 super(AntX.nopackage); 86 } 87 88 89 92 public ExportTask(String iam) 93 { 94 super(iam); 95 } 96 97 98 101 public void setProject(Project project) 102 { 103 super.setProject(project); 104 m_value.setProject(project); 105 } 106 107 108 112 118 public void setScope(ExportScope scope) 119 { 120 require_(scope!=null,"setScop- nonzro"); 121 m_scope = scope; 122 } 123 124 125 130 public final ExportScope getScope() 131 { 132 return m_scope; 133 } 134 135 136 140 public void setName(String name) 141 { 142 require_(!Tk.isWhitespace(name),"setNam- nonwspc name"); 143 m_name = name; 144 } 145 146 147 150 public final void setVar(String name) 151 { 152 setName(name); 153 } 154 155 156 160 public final String getName() 161 { 162 return m_name; 163 } 164 165 166 171 public void setValue(String value) 172 { 173 require_(value!=null,"setValu- nonzro valu"); 174 m_value.set(value); 175 m_value.setIsLiteral(); 176 } 177 178 179 184 public void setFromProperty(String property) 185 { 186 require_(property!=null,"setFromProp- nonzro name"); 187 m_value.set(property); 188 m_value.setIsProperty(true); 189 } 190 191 192 198 public void setFromVariable(String variable) 199 { 200 require_(variable!=null,"setFromVar- nonzro name"); 201 m_value.set(variable); 202 m_value.setIsExported(true); 203 } 204 205 206 210 public final void setFromVar(String variable) 211 { 212 setFromVariable(variable); 213 } 214 215 216 224 public final String getValue() 225 { 226 return m_value.getValue(); 227 } 228 229 230 234 public final void setOp(Modification op) 235 { 236 require_(op!=null,"setModf- nonzro op"); 237 m_Op= op; 238 } 239 240 241 245 public final Modification getOp() 246 { 247 return m_Op; 248 } 249 250 251 256 public final void setTransform(ValueTransform t) 257 { 258 m_T = t==null ? null : ValueTransform.from(t.getIndex()); } 260 261 262 268 public final ValueTransform getTransform() 269 { 270 return m_T==null ? ValueTransform.NONE : m_T; 271 } 272 273 274 275 281 public void setCopyProperty(String property) 282 { 283 require_(property!=null,"setP- nonzro propnam"); 284 m_copyProperty = property; 285 } 286 287 288 293 public final String getCopyProperty() 294 { 295 return m_copyProperty; 296 } 297 298 302 307 protected final boolean isUndefined() 308 { 309 return m_value.isUndefined(); 310 } 311 312 313 318 protected final void setTheProperty(String value) 319 { 320 ExportScope.setTheProperty(getScope(),getProject(),getName(), 321 value,true); 322 } 323 324 325 330 protected final String getTheProperty() 331 { 332 return ExportScope.getTheProperty(getScope(),getProject(), 333 getName()); 334 } 335 336 337 343 protected final void setShortcutValue(String value) 344 { 345 m_shortcutValue = value; 346 } 347 348 349 354 protected void fixDefaults() 355 { 356 if (isUndefined()) { 357 int op= getOp().getIndex(); 358 switch (op) { 359 case Modification.INC_BY1_INDEX: 360 case Modification.DEC_BY1_INDEX: { 361 setShortcutValue("1"); 362 break; 363 } 364 case Modification.NOW_INDEX: 366 case Modification.MINUS_NOW_INDEX: { 367 setShortcutValue(String.valueOf(System.currentTimeMillis())); 368 break; 369 } 370 } 371 } 372 } 373 374 375 383 protected Number initialValue(Number like) 384 { 385 if (like instanceof Double ) { 386 return INITIAL_NUMF; 387 } 388 return INITIAL_NUMI; 389 } 390 391 392 398 protected String shortcutValue() 399 { 400 return m_shortcutValue; 401 } 402 403 404 411 protected String getWorkValue() 412 { 413 String value = shortcutValue(); 414 if (value==null) { 415 value = getValue(); 416 } 417 return value; 418 } 419 420 421 422 431 protected String getExportValue(String value, Number numberValue) 432 { 433 if (value==null) { 434 if (m_T==ValueTransform.DECIMAL) { 436 synchronized(DFI) { 437 return DFI.format(numberValue); 438 } 439 } 440 value = numberValue.toString(); 441 } 442 if (m_T!=null) { 443 value = TransformHelper.apply(m_T,value,getProject()); 444 } 445 return value; 446 } 447 448 449 450 455 protected void verifyCanExecute_(String calr) 456 { 457 verifyInProject_(calr); 458 459 if (getName()==null) { 460 String ermsg = uistrs().get("task.needs.name",getTaskName()); 461 log(ermsg, Project.MSG_ERR); 462 throw new BuildException(ermsg,getLocation()); 463 } 464 465 fixDefaults(); 466 467 if (isUndefined() && shortcutValue()==null) { 468 if (getCopyProperty()!=null && 469 getOp().getIndex()==Modification.SET_INDEX) { return; 471 } 472 String ermsg = uistrs().get 473 ("task.needs.this.attr",getTaskName(),SRC_ATTRS); 474 log(ermsg, Project.MSG_ERR); 475 throw new BuildException(ermsg,getLocation()); 476 } 477 } 478 479 480 481 488 protected void doDeltaOperation(boolean negate) 489 { 490 String value = getWorkValue(); 491 Number delta = Tk.numberFrom(value); 492 if (delta==Tk.NO_NUM_NUM) { 493 String ermsg = uistrs().get("export.value.NAN",value); 494 log(ermsg, Project.MSG_ERR); 495 throw new BuildException(ermsg, getLocation()); 496 } 497 498 String oldvalue = getTheProperty(); 499 Number current; 500 if (oldvalue!=null) { 501 current = Tk.numberFrom(oldvalue); 502 } else { 503 current = initialValue(delta); } 505 if (current==Tk.NO_NUM_NUM) { 506 String ermsg = uistrs().get("export.current.NAN",getName(),oldvalue); 507 log(ermsg, Project.MSG_ERR); 508 throw new BuildException(ermsg, getLocation()); 509 } 510 511 if (current instanceof Double || delta instanceof Double ) { 512 double delta_ = delta.doubleValue(); 513 if (negate) { 514 delta_ = -delta_; 515 } 516 current = new Double (current.doubleValue()+delta_); 517 } else { 518 long delta_ = delta.longValue(); 519 if (negate) { 520 delta_ = -delta_; 521 } 522 current = new Long (current.longValue()+delta_); 523 } 524 setTheProperty(getExportValue(null,current)); 525 } 526 527 528 529 534 protected void doStringOperation() 535 { 536 String value = null; 537 int opI= getOp().getIndex(); 538 if (opI==Modification.STRLEN_INDEX) { 539 value = String.valueOf(getWorkValue().length()); 540 } 541 else if (opI==Modification.STRCAT_INDEX) { 542 value = getTheProperty(); 543 if (value==null) { 544 value = ""; 545 } 546 value = value + getWorkValue(); 547 } 548 setTheProperty(getExportValue(value,null)); 549 } 550 551 552 553 554 559 protected void doAssignOperation() 560 { 561 String value = getWorkValue(); 562 if (value==null) { 563 String ermsg = uistrs().get("export.value.undefined",m_value.get()); 564 log(ermsg,Project.MSG_ERR); 565 throw new BuildException(ermsg,getLocation()); 566 } 567 setTheProperty(getExportValue(value,Tk.NO_NUM_NUM)); 568 } 569 570 571 572 575 private boolean isJustCopy() 576 { 577 return isUndefined() && 578 shortcutValue()==null && 579 getCopyProperty()!=null; 580 } 581 582 583 584 590 public void execute() throws BuildException 591 { 592 verifyCanExecute_("execute"); 593 594 if (isJustCopy()) { 595 getProject().setNewProperty(getCopyProperty(), 596 getExportValue(getTheProperty(),Tk.NO_NUM_NUM)); 597 return; 598 } 599 600 switch (getOp().getIndex()) { 601 case Modification.NOW_INDEX: { 602 if (!isUndefined()) { 603 String warning = uistrs().get 604 ("task.one.or.other.attr", "now", SRC_ATTRS); 605 log(warning, Project.MSG_WARN); 606 } 607 } 609 case Modification.SET_INDEX: { 610 doAssignOperation(); 611 break; 612 } 613 case Modification.MINUS_NOW_INDEX: { 614 if (!isUndefined()) { 615 Number from = Tk.numberFrom(getValue()); 616 if (from!=Tk.NO_NUM_NUM) { 617 long diff = System.currentTimeMillis() - from.longValue(); 618 if (diff<0) { 619 from= Tk.NO_NUM_NUM; 620 } else { 621 setShortcutValue(String.valueOf(diff)); 622 doAssignOperation(); 623 break; 624 } 625 } 626 if (from==Tk.NO_NUM_NUM) { 627 String ermsg = uistrs().get("export.value.NAN",m_value.get()); 628 log(ermsg, Project.MSG_ERR); 629 throw new BuildException(ermsg, getLocation()); 630 } 631 } 632 doDeltaOperation(true); 633 break; 634 } 635 case Modification.INC_INDEX: 636 case Modification.INC_BY1_INDEX: { 637 doDeltaOperation(false); 638 break; 639 } 640 case Modification.DEC_INDEX: 641 case Modification.DEC_BY1_INDEX: { 642 doDeltaOperation(true); 643 break; 644 } 645 default: { 646 doStringOperation(); 647 break; 648 } 649 } 650 651 if (getCopyProperty()!=null) { 652 getProject().setNewProperty(getCopyProperty(), getTheProperty()); 653 } 654 } 655 656 657 private static final Long INITIAL_NUMI= new Long (0); 658 private static final Double INITIAL_NUMF= new Double (0.0f); 659 private static final DecimalFormat DFI = new DecimalFormat ("#.###"); 661 private String m_name; 662 private FlexString m_value = new FlexString(); 663 private String m_copyProperty; 664 private Modification m_Op= Modification.SET; 665 private ExportScope m_scope= ExportScope.DEFAULT_SCOPE; 666 private String m_shortcutValue; 667 private ValueTransform m_T;} 669 670 671 | Popular Tags |