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 21 kB

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