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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /*
  2. * Copyright 2000-2004 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;
  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(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. if (!handleChild(
  290. parentUri, ih, parent, child, childWrapper)) {
  291. if (!(parent instanceof TaskContainer)) {
  292. ih.throwNotSupported(getProject(), parent,
  293. child.getTag());
  294. } else {
  295. // a task container - anything could happen - just add the
  296. // child to the container
  297. TaskContainer container = (TaskContainer) parent;
  298. container.addTask(child);
  299. }
  300. }
  301. }
  302. }
  303. }
  304. /**
  305. * @return the component name - uses ProjectHelper#genComponentName()
  306. */
  307. protected String getComponentName() {
  308. return ProjectHelper.genComponentName(getNamespace(), getTag());
  309. }
  310. /**
  311. * This is used then the realobject of the UE is a PreSetDefinition.
  312. * This is also used when a presetdef is used on a presetdef
  313. * The attributes, elements and text are applied to this
  314. * UE.
  315. *
  316. * @param u an UnknownElement containing the attributes, elements and text
  317. */
  318. public void applyPreSet(UnknownElement u) {
  319. if (presetDefed) {
  320. return;
  321. }
  322. // Do the runtime
  323. getWrapper().applyPreSet(u.getWrapper());
  324. if (u.children != null) {
  325. List newChildren = new ArrayList();
  326. newChildren.addAll(u.children);
  327. if (children != null) {
  328. newChildren.addAll(children);
  329. }
  330. children = newChildren;
  331. }
  332. presetDefed = true;
  333. }
  334. /**
  335. * Creates a named task or data type. If the real object is a task,
  336. * it is configured up to the init() stage.
  337. *
  338. * @param ue The unknown element to create the real object for.
  339. * Must not be <code>null</code>.
  340. * @param w Ignored in this implementation.
  341. *
  342. * @return the task or data type represented by the given unknown element.
  343. */
  344. protected Object makeObject(UnknownElement ue, RuntimeConfigurable w) {
  345. ComponentHelper helper = ComponentHelper.getComponentHelper(
  346. getProject());
  347. String name = ue.getComponentName();
  348. Object o = helper.createComponent(ue, ue.getNamespace(), name);
  349. if (o == null) {
  350. throw getNotFoundException("task or type", name);
  351. }
  352. if (o instanceof PreSetDef.PreSetDefinition) {
  353. PreSetDef.PreSetDefinition def = (PreSetDef.PreSetDefinition) o;
  354. o = def.createObject(ue.getProject());
  355. ue.applyPreSet(def.getPreSets());
  356. if (o instanceof Task) {
  357. Task task = (Task) o;
  358. task.setTaskType(ue.getTaskType());
  359. task.setTaskName(ue.getTaskName());
  360. }
  361. }
  362. if (o instanceof Task) {
  363. Task task = (Task) o;
  364. task.setOwningTarget(getOwningTarget());
  365. task.init();
  366. }
  367. return o;
  368. }
  369. /**
  370. * Creates a named task and configures it up to the init() stage.
  371. *
  372. * @param ue The UnknownElement to create the real task for.
  373. * Must not be <code>null</code>.
  374. * @param w Ignored.
  375. *
  376. * @return the task specified by the given unknown element, or
  377. * <code>null</code> if the task name is not recognised.
  378. */
  379. protected Task makeTask(UnknownElement ue, RuntimeConfigurable w) {
  380. Task task = getProject().createTask(ue.getTag());
  381. if (task != null) {
  382. task.setLocation(getLocation());
  383. // UnknownElement always has an associated target
  384. task.setOwningTarget(getOwningTarget());
  385. task.init();
  386. }
  387. return task;
  388. }
  389. /**
  390. * Returns a very verbose exception for when a task/data type cannot
  391. * be found.
  392. *
  393. * @param what The kind of thing being created. For example, when
  394. * a task name could not be found, this would be
  395. * <code>"task"</code>. Should not be <code>null</code>.
  396. * @param elementName The name of the element which could not be found.
  397. * Should not be <code>null</code>.
  398. *
  399. * @return a detailed description of what might have caused the problem.
  400. */
  401. protected BuildException getNotFoundException(String what,
  402. String elementName) {
  403. String lSep = System.getProperty("line.separator");
  404. String msg = "Could not create " + what + " of type: " + elementName
  405. + "." + lSep + lSep
  406. + "Ant could not find the task or a class this "
  407. + "task relies upon." + lSep + lSep
  408. + "This is common and has a number of causes; the usual " + lSep
  409. + "solutions are to read the manual pages then download and" + lSep
  410. + "install needed JAR files, or fix the build file: " + lSep
  411. + " - You have misspelt '" + elementName + "'." + lSep
  412. + " Fix: check your spelling." + lSep
  413. + " - The task needs an external JAR file to execute" + lSep
  414. + " and this is not found at the right place in the classpath." + lSep
  415. + " Fix: check the documentation for dependencies." + lSep
  416. + " Fix: declare the task." + lSep
  417. + " - The task is an Ant optional task and the JAR file and/or libraries" + lSep
  418. + " implementing the functionality were not found at the time you" + lSep
  419. + " yourself built your installation of Ant from the Ant sources." + lSep
  420. + " Fix: Look in the ANT_HOME/lib for the 'ant-' JAR corresponding to the" + lSep
  421. + " task and make sure it contains more than merely a META-INF/MANIFEST.MF." + lSep
  422. + " If all it contains is the manifest, then rebuild Ant with the needed" + lSep
  423. + " libraries present in ${ant.home}/lib/optional/ , or alternatively," + lSep
  424. + " download a pre-built release version from apache.org" + lSep
  425. + " - The build file was written for a later version of Ant" + lSep
  426. + " Fix: upgrade to at least the latest release version of Ant" + lSep
  427. + " - The task is not an Ant core or optional task " + lSep
  428. + " and needs to be declared using <taskdef>." + lSep
  429. + " - You are attempting to use a task defined using " + lSep
  430. + " <presetdef> or <macrodef> but have spelt wrong or not " + lSep
  431. + " defined it at the point of use" + lSep
  432. + lSep
  433. + "Remember that for JAR files to be visible to Ant tasks implemented" + lSep
  434. + "in ANT_HOME/lib, the files must be in the same directory or on the" + lSep
  435. + "classpath" + lSep
  436. + lSep
  437. + "Please neither file bug reports on this problem, nor email the" + lSep
  438. + "Ant mailing lists, until all of these causes have been explored," + lSep
  439. + "as this is not an Ant bug.";
  440. return new BuildException(msg, getLocation());
  441. }
  442. /**
  443. * Returns the name to use in logging messages.
  444. *
  445. * @return the name to use in logging messages.
  446. */
  447. public String getTaskName() {
  448. //return elementName;
  449. return realThing == null
  450. || !(realThing instanceof Task) ? super.getTaskName()
  451. : ((Task) realThing).getTaskName();
  452. }
  453. /**
  454. * Returns the task instance after it has been created and if it is a task.
  455. *
  456. * @return a task instance or <code>null</code> if the real object is not
  457. * a task.
  458. */
  459. public Task getTask() {
  460. if (realThing instanceof Task) {
  461. return (Task) realThing;
  462. }
  463. return null;
  464. }
  465. /**
  466. * Return the configured object
  467. *
  468. * @return the real thing whatever it is
  469. *
  470. * @since ant 1.6
  471. */
  472. public Object getRealThing() {
  473. return realThing;
  474. }
  475. /**
  476. * Set the configured object
  477. *
  478. * @since ant 1.7
  479. */
  480. public void setRealThing(Object realThing) {
  481. this.realThing = realThing;
  482. }
  483. /**
  484. * Try to create a nested element of <code>parent</code> for the
  485. * given tag.
  486. *
  487. * @return whether the creation has been successful
  488. */
  489. private boolean handleChild(
  490. String parentUri,
  491. IntrospectionHelper ih,
  492. Object parent, UnknownElement child,
  493. RuntimeConfigurable childWrapper) {
  494. String childName = ProjectHelper.genComponentName(
  495. child.getNamespace(), child.getTag());
  496. if (ih.supportsNestedElement(parentUri, childName)) {
  497. IntrospectionHelper.Creator creator =
  498. ih.getElementCreator(
  499. getProject(), parentUri, parent, childName, child);
  500. creator.setPolyType(childWrapper.getPolyType());
  501. Object realChild = creator.create();
  502. if (realChild instanceof PreSetDef.PreSetDefinition) {
  503. PreSetDef.PreSetDefinition def =
  504. (PreSetDef.PreSetDefinition) realChild;
  505. realChild = creator.getRealObject();
  506. child.applyPreSet(def.getPreSets());
  507. }
  508. childWrapper.setCreator(creator);
  509. childWrapper.setProxy(realChild);
  510. if (realChild instanceof Task) {
  511. Task childTask = (Task) realChild;
  512. childTask.setRuntimeConfigurableWrapper(childWrapper);
  513. childTask.setTaskName(childName);
  514. childTask.setTaskType(childName);
  515. childTask.setLocation(child.getLocation());
  516. }
  517. child.handleChildren(realChild, childWrapper);
  518. return true;
  519. }
  520. return false;
  521. }
  522. /**
  523. * like contents equals, but ignores project
  524. * @param obj the object to check against
  525. * @return true if this unknownelement has the same contents the other
  526. */
  527. public boolean similar(Object obj) {
  528. if (obj == null) {
  529. return false;
  530. }
  531. if (!getClass().getName().equals(obj.getClass().getName())) {
  532. return false;
  533. }
  534. UnknownElement other = (UnknownElement) obj;
  535. // Are the names the same ?
  536. if (!equalsString(elementName, other.elementName)) {
  537. return false;
  538. }
  539. if (!namespace.equals(other.namespace)) {
  540. return false;
  541. }
  542. if (!qname.equals(other.qname)) {
  543. return false;
  544. }
  545. // Are attributes the same ?
  546. if (!getWrapper().getAttributeMap().equals(
  547. other.getWrapper().getAttributeMap())) {
  548. return false;
  549. }
  550. // Is the text the same?
  551. // Need to use equals on the string and not
  552. // on the stringbuffer as equals on the string buffer
  553. // does not compare the contents.
  554. if (!getWrapper().getText().toString().equals(
  555. other.getWrapper().getText().toString())) {
  556. return false;
  557. }
  558. // Are the sub elements the same ?
  559. if (children == null || children.size() == 0) {
  560. return other.children == null || other.children.size() == 0;
  561. }
  562. if (other.children == null) {
  563. return false;
  564. }
  565. if (children.size() != other.children.size()) {
  566. return false;
  567. }
  568. for (int i = 0; i < children.size(); ++i) {
  569. UnknownElement child = (UnknownElement) children.get(i);
  570. if (!child.similar(other.children.get(i))) {
  571. return false;
  572. }
  573. }
  574. return true;
  575. }
  576. private boolean equalsString(String a, String b) {
  577. if (a == null) {
  578. return b == null;
  579. }
  580. return a.equals(b);
  581. }
  582. }