You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnknownElement.java 20 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * Copyright 2000-2006 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant;
  18. import java.util.ArrayList;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.io.IOException;
  22. import org.apache.tools.ant.taskdefs.PreSetDef;
  23. /**
  24. * Wrapper class that holds all the information necessary to create a task
  25. * or data type that did not exist when Ant started, or one which
  26. * has had its definition updated to use a different implementation class.
  27. *
  28. */
  29. public class UnknownElement extends Task {
  30. /**
  31. * Holds the name of the task/type or nested child element of a
  32. * task/type that hasn't been defined at parser time or has
  33. * been redefined since original creation.
  34. */
  35. private String elementName;
  36. /**
  37. * Holds the namespace of the element.
  38. */
  39. private String namespace = "";
  40. /**
  41. * Holds the namespace qname of the element.
  42. */
  43. private String qname;
  44. /**
  45. * The real object after it has been loaded.
  46. */
  47. private Object realThing;
  48. /**
  49. * List of child elements (UnknownElements).
  50. */
  51. private List/*<UnknownElement>*/ children = null;
  52. /** Specifies if a predefined definition has been done */
  53. private boolean presetDefed = false;
  54. /**
  55. * Creates an UnknownElement for the given element name.
  56. *
  57. * @param elementName The name of the unknown element.
  58. * Must not be <code>null</code>.
  59. */
  60. public UnknownElement (String elementName) {
  61. this.elementName = elementName;
  62. }
  63. /**
  64. * @return the list of nested UnknownElements for this UnknownElement.
  65. */
  66. public List getChildren() {
  67. return children;
  68. }
  69. /**
  70. * Returns the name of the XML element which generated this unknown
  71. * element.
  72. *
  73. * @return the name of the XML element which generated this unknown
  74. * element.
  75. */
  76. public String getTag() {
  77. return elementName;
  78. }
  79. /** Return the namespace of the XML element associated with this component.
  80. *
  81. * @return Namespace URI used in the xmlns declaration.
  82. */
  83. public String getNamespace() {
  84. return namespace;
  85. }
  86. /**
  87. * Set the namespace of the XML element associated with this component.
  88. * This method is typically called by the XML processor.
  89. * If the namespace is "ant:current", the component helper
  90. * is used to get the current antlib uri.
  91. *
  92. * @param namespace URI used in the xmlns declaration.
  93. */
  94. public void setNamespace(String namespace) {
  95. if (namespace.equals(ProjectHelper.ANT_CURRENT_URI)) {
  96. ComponentHelper helper = ComponentHelper.getComponentHelper(
  97. getProject());
  98. namespace = helper.getCurrentAntlibUri();
  99. }
  100. this.namespace = namespace == null ? "" : namespace;
  101. }
  102. /** Return the qname of the XML element associated with this component.
  103. *
  104. * @return namespace Qname used in the element declaration.
  105. */
  106. public String getQName() {
  107. return qname;
  108. }
  109. /** Set the namespace qname of the XML element.
  110. * This method is typically called by the XML processor.
  111. *
  112. * @param qname the qualified name of the element
  113. */
  114. public void setQName(String qname) {
  115. this.qname = qname;
  116. }
  117. /**
  118. * Get the RuntimeConfigurable instance for this UnknownElement, containing
  119. * the configuration information.
  120. *
  121. * @return the configuration info.
  122. */
  123. public RuntimeConfigurable getWrapper() {
  124. return super.getWrapper();
  125. }
  126. /**
  127. * Creates the real object instance and child elements, then configures
  128. * the attributes and text of the real object. This unknown element
  129. * is then replaced with the real object in the containing target's list
  130. * of children.
  131. *
  132. * @exception BuildException if the configuration fails
  133. */
  134. public void maybeConfigure() throws BuildException {
  135. //ProjectComponentHelper helper=ProjectComponentHelper.getProjectComponentHelper();
  136. //realThing = helper.createProjectComponent( this, getProject(), null,
  137. // this.getTag());
  138. configure(makeObject(this, getWrapper()));
  139. }
  140. /**
  141. * Configure the given object from this UnknownElement
  142. *
  143. * @param realObject the real object this UnknownElement is representing.
  144. *
  145. */
  146. public void configure(Object realObject) {
  147. realThing = realObject;
  148. getWrapper().setProxy(realThing);
  149. Task task = null;
  150. if (realThing instanceof Task) {
  151. task = (Task) realThing;
  152. task.setRuntimeConfigurableWrapper(getWrapper());
  153. // For Script to work. Ugly
  154. // The reference is replaced by RuntimeConfigurable
  155. this.getOwningTarget().replaceChild(this, (Task) realThing);
  156. }
  157. handleChildren(realThing, getWrapper());
  158. // configure attributes of the object and it's children. If it is
  159. // a task container, defer the configuration till the task container
  160. // attempts to use the task
  161. if (task != null) {
  162. task.maybeConfigure();
  163. } else {
  164. getWrapper().maybeConfigure(getProject());
  165. }
  166. }
  167. /**
  168. * Handles output sent to System.out by this task or its real task.
  169. *
  170. * @param output The output to log. Should not be <code>null</code>.
  171. */
  172. protected void handleOutput(String output) {
  173. if (realThing instanceof Task) {
  174. ((Task) realThing).handleOutput(output);
  175. } else {
  176. super.handleOutput(output);
  177. }
  178. }
  179. /**
  180. * @see Task#handleInput(byte[], int, int)
  181. *
  182. * @since Ant 1.6
  183. */
  184. protected int handleInput(byte[] buffer, int offset, int length)
  185. throws IOException {
  186. if (realThing instanceof Task) {
  187. return ((Task) realThing).handleInput(buffer, offset, length);
  188. } else {
  189. return super.handleInput(buffer, offset, length);
  190. }
  191. }
  192. /**
  193. * Handles output sent to System.out by this task or its real task.
  194. *
  195. * @param output The output to log. Should not be <code>null</code>.
  196. */
  197. protected void handleFlush(String output) {
  198. if (realThing instanceof Task) {
  199. ((Task) realThing).handleFlush(output);
  200. } else {
  201. super.handleFlush(output);
  202. }
  203. }
  204. /**
  205. * Handles error output sent to System.err by this task or its real task.
  206. *
  207. * @param output The error output to log. Should not be <code>null</code>.
  208. */
  209. protected void handleErrorOutput(String output) {
  210. if (realThing instanceof Task) {
  211. ((Task) realThing).handleErrorOutput(output);
  212. } else {
  213. super.handleErrorOutput(output);
  214. }
  215. }
  216. /**
  217. * Handles error output sent to System.err by this task or its real task.
  218. *
  219. * @param output The error output to log. Should not be <code>null</code>.
  220. */
  221. protected void handleErrorFlush(String output) {
  222. if (realThing instanceof Task) {
  223. ((Task) realThing).handleErrorOutput(output);
  224. } else {
  225. super.handleErrorOutput(output);
  226. }
  227. }
  228. /**
  229. * Executes the real object if it's a task. If it's not a task
  230. * (e.g. a data type) then this method does nothing.
  231. */
  232. public void execute() {
  233. if (realThing == null) {
  234. // plain impossible to get here, maybeConfigure should
  235. // have thrown an exception.
  236. throw new BuildException("Could not create task of type: "
  237. + elementName, getLocation());
  238. }
  239. if (realThing instanceof Task) {
  240. ((Task) realThing).execute();
  241. }
  242. // the task will not be reused ( a new init() will be called )
  243. // Let GC do its job
  244. realThing = null;
  245. // FIXME: the following should be done as well, but is
  246. // commented out for the moment as the unit tests fail
  247. // if it is done
  248. //getWrapper().setProxy(null);
  249. }
  250. /**
  251. * Adds a child element to this element.
  252. *
  253. * @param child The child element to add. Must not be <code>null</code>.
  254. */
  255. public void addChild(UnknownElement child) {
  256. if (children == null) {
  257. children = new ArrayList();
  258. }
  259. children.add(child);
  260. }
  261. /**
  262. * Creates child elements, creates children of the children
  263. * (recursively), and sets attributes of the child elements.
  264. *
  265. * @param parent The configured object for the parent.
  266. * Must not be <code>null</code>.
  267. *
  268. * @param parentWrapper The wrapper containing child wrappers
  269. * to be configured. Must not be <code>null</code>
  270. * if there are any children.
  271. *
  272. * @exception BuildException if the children cannot be configured.
  273. */
  274. protected void handleChildren(
  275. Object parent,
  276. RuntimeConfigurable parentWrapper)
  277. throws BuildException {
  278. if (parent instanceof TypeAdapter) {
  279. parent = ((TypeAdapter) parent).getProxy();
  280. }
  281. String parentUri = getNamespace();
  282. Class parentClass = parent.getClass();
  283. IntrospectionHelper ih = IntrospectionHelper.getHelper(getProject(), parentClass);
  284. if (children != null) {
  285. Iterator it = children.iterator();
  286. for (int i = 0; it.hasNext(); i++) {
  287. RuntimeConfigurable childWrapper = parentWrapper.getChild(i);
  288. UnknownElement child = (UnknownElement) it.next();
  289. try {
  290. if (!handleChild(
  291. parentUri, ih, parent, child, childWrapper)) {
  292. if (!(parent instanceof TaskContainer)) {
  293. ih.throwNotSupported(getProject(), parent,
  294. child.getTag());
  295. } else {
  296. // a task container - anything could happen - just add the
  297. // child to the container
  298. TaskContainer container = (TaskContainer) parent;
  299. container.addTask(child);
  300. }
  301. }
  302. } catch (UnsupportedElementException ex) {
  303. throw new BuildException(
  304. parentWrapper.getElementTag()
  305. + " doesn't support the nested \"" + ex.getElement()
  306. + "\" element.", ex);
  307. }
  308. }
  309. }
  310. }
  311. /**
  312. * @return the component name - uses ProjectHelper#genComponentName()
  313. */
  314. protected String getComponentName() {
  315. return ProjectHelper.genComponentName(getNamespace(), getTag());
  316. }
  317. /**
  318. * This is used then the realobject of the UE is a PreSetDefinition.
  319. * This is also used when a presetdef is used on a presetdef
  320. * The attributes, elements and text are applied to this
  321. * UE.
  322. *
  323. * @param u an UnknownElement containing the attributes, elements and text
  324. */
  325. public void applyPreSet(UnknownElement u) {
  326. if (presetDefed) {
  327. return;
  328. }
  329. // Do the runtime
  330. getWrapper().applyPreSet(u.getWrapper());
  331. if (u.children != null) {
  332. List newChildren = new ArrayList();
  333. newChildren.addAll(u.children);
  334. if (children != null) {
  335. newChildren.addAll(children);
  336. }
  337. children = newChildren;
  338. }
  339. presetDefed = true;
  340. }
  341. /**
  342. * Creates a named task or data type. If the real object is a task,
  343. * it is configured up to the init() stage.
  344. *
  345. * @param ue The unknown element to create the real object for.
  346. * Must not be <code>null</code>.
  347. * @param w Ignored in this implementation.
  348. *
  349. * @return the task or data type represented by the given unknown element.
  350. */
  351. protected Object makeObject(UnknownElement ue, RuntimeConfigurable w) {
  352. ComponentHelper helper = ComponentHelper.getComponentHelper(
  353. getProject());
  354. String name = ue.getComponentName();
  355. Object o = helper.createComponent(ue, ue.getNamespace(), name);
  356. if (o == null) {
  357. throw getNotFoundException("task or type", name);
  358. }
  359. if (o instanceof PreSetDef.PreSetDefinition) {
  360. PreSetDef.PreSetDefinition def = (PreSetDef.PreSetDefinition) o;
  361. o = def.createObject(ue.getProject());
  362. if (o == null) {
  363. throw getNotFoundException(
  364. "preset " + name,
  365. def.getPreSets().getComponentName());
  366. }
  367. ue.applyPreSet(def.getPreSets());
  368. if (o instanceof Task) {
  369. Task task = (Task) o;
  370. task.setTaskType(ue.getTaskType());
  371. task.setTaskName(ue.getTaskName());
  372. task.init();
  373. }
  374. }
  375. if (o instanceof UnknownElement) {
  376. o = ((UnknownElement) o).makeObject((UnknownElement) o, w);
  377. }
  378. if (o instanceof Task) {
  379. ((Task) o).setOwningTarget(getOwningTarget());
  380. }
  381. return o;
  382. }
  383. /**
  384. * Creates a named task and configures it up to the init() stage.
  385. *
  386. * @param ue The UnknownElement to create the real task for.
  387. * Must not be <code>null</code>.
  388. * @param w Ignored.
  389. *
  390. * @return the task specified by the given unknown element, or
  391. * <code>null</code> if the task name is not recognised.
  392. */
  393. protected Task makeTask(UnknownElement ue, RuntimeConfigurable w) {
  394. Task task = getProject().createTask(ue.getTag());
  395. if (task != null) {
  396. task.setLocation(getLocation());
  397. // UnknownElement always has an associated target
  398. task.setOwningTarget(getOwningTarget());
  399. task.init();
  400. }
  401. return task;
  402. }
  403. /**
  404. * Returns a very verbose exception for when a task/data type cannot
  405. * be found.
  406. *
  407. * @param what The kind of thing being created. For example, when
  408. * a task name could not be found, this would be
  409. * <code>"task"</code>. Should not be <code>null</code>.
  410. * @param name The name of the element which could not be found.
  411. * Should not be <code>null</code>.
  412. *
  413. * @return a detailed description of what might have caused the problem.
  414. */
  415. protected BuildException getNotFoundException(String what,
  416. String name) {
  417. ComponentHelper helper = ComponentHelper.getComponentHelper(getProject());
  418. String msg = helper.diagnoseCreationFailure(name, what);
  419. return new BuildException(msg, getLocation());
  420. }
  421. /**
  422. * Returns the name to use in logging messages.
  423. *
  424. * @return the name to use in logging messages.
  425. */
  426. public String getTaskName() {
  427. //return elementName;
  428. return realThing == null
  429. || !(realThing instanceof Task) ? super.getTaskName()
  430. : ((Task) realThing).getTaskName();
  431. }
  432. /**
  433. * Returns the task instance after it has been created and if it is a task.
  434. *
  435. * @return a task instance or <code>null</code> if the real object is not
  436. * a task.
  437. */
  438. public Task getTask() {
  439. if (realThing instanceof Task) {
  440. return (Task) realThing;
  441. }
  442. return null;
  443. }
  444. /**
  445. * Return the configured object
  446. *
  447. * @return the real thing whatever it is
  448. *
  449. * @since ant 1.6
  450. */
  451. public Object getRealThing() {
  452. return realThing;
  453. }
  454. /**
  455. * Set the configured object
  456. * @param realThing the configured object
  457. * @since ant 1.7
  458. */
  459. public void setRealThing(Object realThing) {
  460. this.realThing = realThing;
  461. }
  462. /**
  463. * Try to create a nested element of <code>parent</code> for the
  464. * given tag.
  465. *
  466. * @return whether the creation has been successful
  467. */
  468. private boolean handleChild(
  469. String parentUri,
  470. IntrospectionHelper ih,
  471. Object parent, UnknownElement child,
  472. RuntimeConfigurable childWrapper) {
  473. String childName = ProjectHelper.genComponentName(
  474. child.getNamespace(), child.getTag());
  475. if (ih.supportsNestedElement(parentUri, childName)) {
  476. IntrospectionHelper.Creator creator =
  477. ih.getElementCreator(
  478. getProject(), parentUri, parent, childName, child);
  479. creator.setPolyType(childWrapper.getPolyType());
  480. Object realChild = creator.create();
  481. if (realChild instanceof PreSetDef.PreSetDefinition) {
  482. PreSetDef.PreSetDefinition def =
  483. (PreSetDef.PreSetDefinition) realChild;
  484. realChild = creator.getRealObject();
  485. child.applyPreSet(def.getPreSets());
  486. }
  487. childWrapper.setCreator(creator);
  488. childWrapper.setProxy(realChild);
  489. if (realChild instanceof Task) {
  490. Task childTask = (Task) realChild;
  491. childTask.setRuntimeConfigurableWrapper(childWrapper);
  492. childTask.setTaskName(childName);
  493. childTask.setTaskType(childName);
  494. childTask.setLocation(child.getLocation());
  495. }
  496. child.handleChildren(realChild, childWrapper);
  497. return true;
  498. }
  499. return false;
  500. }
  501. /**
  502. * like contents equals, but ignores project
  503. * @param obj the object to check against
  504. * @return true if this unknownelement has the same contents the other
  505. */
  506. public boolean similar(Object obj) {
  507. if (obj == null) {
  508. return false;
  509. }
  510. if (!getClass().getName().equals(obj.getClass().getName())) {
  511. return false;
  512. }
  513. UnknownElement other = (UnknownElement) obj;
  514. // Are the names the same ?
  515. if (!equalsString(elementName, other.elementName)) {
  516. return false;
  517. }
  518. if (!namespace.equals(other.namespace)) {
  519. return false;
  520. }
  521. if (!qname.equals(other.qname)) {
  522. return false;
  523. }
  524. // Are attributes the same ?
  525. if (!getWrapper().getAttributeMap().equals(
  526. other.getWrapper().getAttributeMap())) {
  527. return false;
  528. }
  529. // Is the text the same?
  530. // Need to use equals on the string and not
  531. // on the stringbuffer as equals on the string buffer
  532. // does not compare the contents.
  533. if (!getWrapper().getText().toString().equals(
  534. other.getWrapper().getText().toString())) {
  535. return false;
  536. }
  537. // Are the sub elements the same ?
  538. if (children == null || children.size() == 0) {
  539. return other.children == null || other.children.size() == 0;
  540. }
  541. if (other.children == null) {
  542. return false;
  543. }
  544. if (children.size() != other.children.size()) {
  545. return false;
  546. }
  547. for (int i = 0; i < children.size(); ++i) {
  548. UnknownElement child = (UnknownElement) children.get(i);
  549. if (!child.similar(other.children.get(i))) {
  550. return false;
  551. }
  552. }
  553. return true;
  554. }
  555. private static boolean equalsString(String a, String b) {
  556. return (a == null) ? (b == null) : a.equals(b);
  557. }
  558. }