1 18 19 package org.apache.tools.ant; 20 21 import java.util.ArrayList ; 22 import java.util.Enumeration ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.io.IOException ; 27 import org.apache.tools.ant.taskdefs.PreSetDef; 28 29 35 public class UnknownElement extends Task { 36 37 42 private String elementName; 43 44 47 private String namespace = ""; 48 49 52 private String qname; 53 54 57 private Object realThing; 58 59 62 private List children = null; 63 64 65 private boolean presetDefed = false; 66 67 73 public UnknownElement (String elementName) { 74 this.elementName = elementName; 75 } 76 77 80 public List getChildren() { 81 return children; 82 } 83 84 91 public String getTag() { 92 return elementName; 93 } 94 95 99 public String getNamespace() { 100 return namespace; 101 } 102 103 111 public void setNamespace(String namespace) { 112 if (namespace.equals(ProjectHelper.ANT_CURRENT_URI)) { 113 ComponentHelper helper = ComponentHelper.getComponentHelper( 114 getProject()); 115 namespace = helper.getCurrentAntlibUri(); 116 } 117 this.namespace = namespace == null ? "" : namespace; 118 } 119 120 124 public String getQName() { 125 return qname; 126 } 127 128 133 public void setQName(String qname) { 134 this.qname = qname; 135 } 136 137 138 144 public RuntimeConfigurable getWrapper() { 145 return super.getWrapper(); 146 } 147 148 156 public void maybeConfigure() throws BuildException { 157 if (realThing != null) { 158 return; 159 } 160 configure(makeObject(this, getWrapper())); 161 } 162 163 169 public void configure(Object realObject) { 170 realThing = realObject; 171 172 getWrapper().setProxy(realThing); 173 Task task = null; 174 if (realThing instanceof Task) { 175 task = (Task) realThing; 176 177 task.setRuntimeConfigurableWrapper(getWrapper()); 178 179 if (getWrapper().getId() != null) { 183 this.getOwningTarget().replaceChild(this, (Task) realThing); 184 } 185 } 186 187 188 192 if (task != null) { 193 task.maybeConfigure(); 194 } else { 195 getWrapper().maybeConfigure(getProject()); 196 } 197 198 handleChildren(realThing, getWrapper()); 199 } 200 201 206 protected void handleOutput(String output) { 207 if (realThing instanceof Task) { 208 ((Task) realThing).handleOutput(output); 209 } else { 210 super.handleOutput(output); 211 } 212 } 213 214 226 protected int handleInput(byte[] buffer, int offset, int length) 227 throws IOException { 228 if (realThing instanceof Task) { 229 return ((Task) realThing).handleInput(buffer, offset, length); 230 } else { 231 return super.handleInput(buffer, offset, length); 232 } 233 234 } 235 240 protected void handleFlush(String output) { 241 if (realThing instanceof Task) { 242 ((Task) realThing).handleFlush(output); 243 } else { 244 super.handleFlush(output); 245 } 246 } 247 248 253 protected void handleErrorOutput(String output) { 254 if (realThing instanceof Task) { 255 ((Task) realThing).handleErrorOutput(output); 256 } else { 257 super.handleErrorOutput(output); 258 } 259 } 260 261 262 267 protected void handleErrorFlush(String output) { 268 if (realThing instanceof Task) { 269 ((Task) realThing).handleErrorOutput(output); 270 } else { 271 super.handleErrorOutput(output); 272 } 273 } 274 275 279 public void execute() { 280 if (realThing == null) { 281 throw new BuildException("Could not create task of type: " 284 + elementName, getLocation()); 285 } 286 287 if (realThing instanceof Task) { 288 ((Task) realThing).execute(); 289 } 290 291 realThing = null; 295 getWrapper().setProxy(null); 296 297 } 298 299 304 public void addChild(UnknownElement child) { 305 if (children == null) { 306 children = new ArrayList (); 307 } 308 children.add(child); 309 } 310 311 324 protected void handleChildren( 325 Object parent, 326 RuntimeConfigurable parentWrapper) 327 throws BuildException { 328 if (parent instanceof TypeAdapter) { 329 parent = ((TypeAdapter) parent).getProxy(); 330 } 331 332 String parentUri = getNamespace(); 333 Class parentClass = parent.getClass(); 334 IntrospectionHelper ih = IntrospectionHelper.getHelper(getProject(), parentClass); 335 336 337 if (children != null) { 338 Iterator it = children.iterator(); 339 for (int i = 0; it.hasNext(); i++) { 340 RuntimeConfigurable childWrapper = parentWrapper.getChild(i); 341 UnknownElement child = (UnknownElement) it.next(); 342 try { 343 if (!handleChild( 344 parentUri, ih, parent, child, childWrapper)) { 345 if (!(parent instanceof TaskContainer)) { 346 ih.throwNotSupported(getProject(), parent, 347 child.getTag()); 348 } else { 349 TaskContainer container = (TaskContainer) parent; 352 container.addTask(child); 353 } 354 } 355 } catch (UnsupportedElementException ex) { 356 throw new BuildException( 357 parentWrapper.getElementTag() 358 + " doesn't support the nested \"" + ex.getElement() 359 + "\" element.", ex); 360 } 361 } 362 } 363 } 364 365 368 protected String getComponentName() { 369 return ProjectHelper.genComponentName(getNamespace(), getTag()); 370 } 371 372 380 public void applyPreSet(UnknownElement u) { 381 if (presetDefed) { 382 return; 383 } 384 getWrapper().applyPreSet(u.getWrapper()); 386 if (u.children != null) { 387 List newChildren = new ArrayList (); 388 newChildren.addAll(u.children); 389 if (children != null) { 390 newChildren.addAll(children); 391 } 392 children = newChildren; 393 } 394 presetDefed = true; 395 } 396 397 407 protected Object makeObject(UnknownElement ue, RuntimeConfigurable w) { 408 ComponentHelper helper = ComponentHelper.getComponentHelper( 409 getProject()); 410 String name = ue.getComponentName(); 411 Object o = helper.createComponent(ue, ue.getNamespace(), name); 412 if (o == null) { 413 throw getNotFoundException("task or type", name); 414 } 415 if (o instanceof PreSetDef.PreSetDefinition) { 416 PreSetDef.PreSetDefinition def = (PreSetDef.PreSetDefinition) o; 417 o = def.createObject(ue.getProject()); 418 if (o == null) { 419 throw getNotFoundException( 420 "preset " + name, 421 def.getPreSets().getComponentName()); 422 } 423 ue.applyPreSet(def.getPreSets()); 424 if (o instanceof Task) { 425 Task task = (Task) o; 426 task.setTaskType(ue.getTaskType()); 427 task.setTaskName(ue.getTaskName()); 428 task.init(); 429 } 430 } 431 if (o instanceof UnknownElement) { 432 o = ((UnknownElement) o).makeObject((UnknownElement) o, w); 433 } 434 if (o instanceof Task) { 435 ((Task) o).setOwningTarget(getOwningTarget()); 436 } 437 if (o instanceof ProjectComponent) { 438 ((ProjectComponent) o).setLocation(getLocation()); 439 } 440 return o; 441 } 442 443 453 protected Task makeTask(UnknownElement ue, RuntimeConfigurable w) { 454 Task task = getProject().createTask(ue.getTag()); 455 456 if (task != null) { 457 task.setLocation(getLocation()); 458 task.setOwningTarget(getOwningTarget()); 460 task.init(); 461 } 462 return task; 463 } 464 465 477 protected BuildException getNotFoundException(String what, 478 String name) { 479 ComponentHelper helper = ComponentHelper.getComponentHelper(getProject()); 480 String msg = helper.diagnoseCreationFailure(name, what); 481 return new BuildException(msg, getLocation()); 482 } 483 484 489 public String getTaskName() { 490 return realThing == null 492 || !(realThing instanceof Task) ? super.getTaskName() 493 : ((Task) realThing).getTaskName(); 494 } 495 496 502 public Task getTask() { 503 if (realThing instanceof Task) { 504 return (Task) realThing; 505 } 506 return null; 507 } 508 509 516 public Object getRealThing() { 517 return realThing; 518 } 519 520 525 public void setRealThing(Object realThing) { 526 this.realThing = realThing; 527 } 528 529 535 private boolean handleChild( 536 String parentUri, 537 IntrospectionHelper ih, 538 Object parent, UnknownElement child, 539 RuntimeConfigurable childWrapper) { 540 String childName = ProjectHelper.genComponentName( 541 child.getNamespace(), child.getTag()); 542 if (ih.supportsNestedElement(parentUri, childName)) { 543 IntrospectionHelper.Creator creator = 544 ih.getElementCreator( 545 getProject(), parentUri, parent, childName, child); 546 creator.setPolyType(childWrapper.getPolyType()); 547 Object realChild = creator.create(); 548 if (realChild instanceof PreSetDef.PreSetDefinition) { 549 PreSetDef.PreSetDefinition def = 550 (PreSetDef.PreSetDefinition) realChild; 551 realChild = creator.getRealObject(); 552 child.applyPreSet(def.getPreSets()); 553 } 554 childWrapper.setCreator(creator); 555 childWrapper.setProxy(realChild); 556 if (realChild instanceof Task) { 557 Task childTask = (Task) realChild; 558 childTask.setRuntimeConfigurableWrapper(childWrapper); 559 childTask.setTaskName(childName); 560 childTask.setTaskType(childName); 561 } 562 if (realChild instanceof ProjectComponent) { 563 ((ProjectComponent) realChild).setLocation(child.getLocation()); 564 } 565 childWrapper.maybeConfigure(getProject()); 566 child.handleChildren(realChild, childWrapper); 567 creator.store(); 568 return true; 569 } 570 return false; 571 } 572 573 578 public boolean similar(Object obj) { 579 if (obj == null) { 580 return false; 581 } 582 if (!getClass().getName().equals(obj.getClass().getName())) { 583 return false; 584 } 585 UnknownElement other = (UnknownElement) obj; 586 if (!equalsString(elementName, other.elementName)) { 588 return false; 589 } 590 if (!namespace.equals(other.namespace)) { 591 return false; 592 } 593 if (!qname.equals(other.qname)) { 594 return false; 595 } 596 if (!getWrapper().getAttributeMap().equals( 598 other.getWrapper().getAttributeMap())) { 599 return false; 600 } 601 if (!getWrapper().getText().toString().equals( 606 other.getWrapper().getText().toString())) { 607 return false; 608 } 609 if (children == null || children.size() == 0) { 611 return other.children == null || other.children.size() == 0; 612 } 613 if (other.children == null) { 614 return false; 615 } 616 if (children.size() != other.children.size()) { 617 return false; 618 } 619 for (int i = 0; i < children.size(); ++i) { 620 UnknownElement child = (UnknownElement) children.get(i); 621 if (!child.similar(other.children.get(i))) { 622 return false; 623 } 624 } 625 return true; 626 } 627 628 private static boolean equalsString(String a, String b) { 629 return (a == null) ? (b == null) : a.equals(b); 630 } 631 632 637 public UnknownElement copy(Project newProject) { 638 UnknownElement ret = new UnknownElement(getTag()); 639 ret.setNamespace(getNamespace()); 640 ret.setProject(newProject); 641 ret.setQName(getQName()); 642 ret.setTaskType(getTaskType()); 643 ret.setTaskName(getTaskName()); 644 ret.setLocation(getLocation()); 645 if (getOwningTarget() == null) { 646 Target t = new Target(); 647 t.setProject(getProject()); 648 ret.setOwningTarget(t); 649 } else { 650 ret.setOwningTarget(getOwningTarget()); 651 } 652 RuntimeConfigurable copyRC = new RuntimeConfigurable( 653 ret, getTaskName()); 654 copyRC.setPolyType(getWrapper().getPolyType()); 655 Map m = getWrapper().getAttributeMap(); 656 for (Iterator i = m.entrySet().iterator(); i.hasNext();) { 657 Map.Entry entry = (Map.Entry ) i.next(); 658 copyRC.setAttribute( 659 (String ) entry.getKey(), (String ) entry.getValue()); 660 } 661 copyRC.addText(getWrapper().getText().toString()); 662 663 for (Enumeration e = getWrapper().getChildren(); e.hasMoreElements();) { 664 RuntimeConfigurable r = (RuntimeConfigurable) e.nextElement(); 665 UnknownElement ueChild = (UnknownElement) r.getProxy(); 666 UnknownElement copyChild = ueChild.copy(newProject); 667 copyRC.addChild(copyChild.getWrapper()); 668 ret.addChild(copyChild); 669 } 670 return ret; 671 } 672 } 673 | Popular Tags |