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