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.io.Serializable;
  19. import java.util.ArrayList;
  20. import java.util.Collections;
  21. import java.util.Enumeration;
  22. import java.util.HashMap;
  23. import java.util.Hashtable;
  24. import java.util.List;
  25. import java.util.Locale;
  26. import java.util.Map;
  27. import java.util.Iterator;
  28. import org.apache.tools.ant.util.CollectionUtils;
  29. import org.xml.sax.AttributeList;
  30. import org.xml.sax.helpers.AttributeListImpl;
  31. /**
  32. * Wrapper class that holds the attributes of an element, its children, and
  33. * any text within it. It then takes care of configuring that element at
  34. * runtime.
  35. *
  36. */
  37. public class RuntimeConfigurable implements Serializable {
  38. /** Polymorphic attribute (May be XML NS attribute later) */
  39. private static final String ANT_TYPE = "ant-type";
  40. /** Name of the element to configure. */
  41. private String elementTag = null;
  42. /** List of child element wrappers. */
  43. private List/*<RuntimeConfigurable>*/ children = null;
  44. /** The element to configure. It is only used during
  45. * maybeConfigure.
  46. */
  47. private transient Object wrappedObject = null;
  48. /** the creator used to make the wrapped object */
  49. private transient IntrospectionHelper.Creator creator;
  50. /**
  51. * @deprecated
  52. * XML attributes for the element.
  53. */
  54. private transient AttributeList attributes;
  55. /** Attribute names and values. While the XML spec doesn't require
  56. * preserving the order ( AFAIK ), some ant tests do rely on the
  57. * exact order. The following code is copied from AttributeImpl.
  58. * We could also just use SAX2 Attributes and convert to SAX1 ( DOM
  59. * attribute Nodes can also be stored in SAX2 Attributes )
  60. * XXX under JDK 1.4 you can just use a LinkedHashMap for this purpose -jglick
  61. */
  62. private List/*<String>*/ attributeNames = null;
  63. /** Map of attribute names to values */
  64. private Map/*<String,String>*/ attributeMap = null;
  65. /** Text appearing within the element. */
  66. private StringBuffer characters = null;
  67. /** Indicates if the wrapped object has been configured */
  68. private boolean proxyConfigured = false;
  69. /** the polymorphic type */
  70. private String polyType = null;
  71. /**
  72. * Sole constructor creating a wrapper for the specified object.
  73. *
  74. * @param proxy The element to configure. Must not be <code>null</code>.
  75. * @param elementTag The tag name generating this element.
  76. * Should not be <code>null</code>.
  77. */
  78. public RuntimeConfigurable(Object proxy, String elementTag) {
  79. wrappedObject = proxy;
  80. this.elementTag = elementTag;
  81. proxyConfigured = false;
  82. // Most likely an UnknownElement
  83. if (proxy instanceof Task) {
  84. ((Task) proxy).setRuntimeConfigurableWrapper(this);
  85. }
  86. }
  87. /**
  88. * Sets the element to configure.
  89. *
  90. * @param proxy The element to configure. Must not be <code>null</code>.
  91. */
  92. public void setProxy(Object proxy) {
  93. wrappedObject = proxy;
  94. proxyConfigured = false;
  95. }
  96. /**
  97. * Sets the creator of the element to be configured
  98. * used to store the element in the parent;
  99. *
  100. * @param creator the creator object
  101. */
  102. void setCreator(IntrospectionHelper.Creator creator) {
  103. this.creator = creator;
  104. }
  105. /**
  106. * Get the object for which this RuntimeConfigurable holds the configuration
  107. * information
  108. *
  109. * @return the object whose configure is held by this instance.
  110. */
  111. public Object getProxy() {
  112. return wrappedObject;
  113. }
  114. /**
  115. * get the polymorphic type for this element
  116. * @return the ant component type name, null if not set
  117. */
  118. public String getPolyType() {
  119. return polyType;
  120. }
  121. /**
  122. * set the polymorphic type for this element
  123. * @param polyType the ant component type name, null if not set
  124. */
  125. public void setPolyType(String polyType) {
  126. this.polyType = polyType;
  127. }
  128. /**
  129. * Sets the attributes for the wrapped element.
  130. *
  131. * @deprecated
  132. * @param attributes List of attributes defined in the XML for this
  133. * element. May be <code>null</code>.
  134. */
  135. public void setAttributes(AttributeList attributes) {
  136. this.attributes = new AttributeListImpl(attributes);
  137. for (int i = 0; i < attributes.getLength(); i++) {
  138. setAttribute(attributes.getName(i), attributes.getValue(i));
  139. }
  140. }
  141. /**
  142. * Set an attribute to a given value
  143. *
  144. * @param name the name of the attribute.
  145. * @param value the attribute's value.
  146. */
  147. public void setAttribute(String name, String value) {
  148. if (name.equalsIgnoreCase(ANT_TYPE)) {
  149. this.polyType = value;
  150. } else {
  151. if (attributeNames == null) {
  152. attributeNames = new ArrayList();
  153. attributeMap = new HashMap();
  154. }
  155. attributeNames.add(name);
  156. attributeMap.put(name, value);
  157. }
  158. }
  159. /** Return the attribute map.
  160. *
  161. * @return Attribute name to attribute value map
  162. */
  163. public Hashtable getAttributeMap() {
  164. if (attributeMap != null) {
  165. return new Hashtable(attributeMap);
  166. } else {
  167. return new Hashtable(1);
  168. }
  169. }
  170. /**
  171. * Returns the list of attributes for the wrapped element.
  172. *
  173. * @deprecated
  174. * @return An AttributeList representing the attributes defined in the
  175. * XML for this element. May be <code>null</code>.
  176. */
  177. public AttributeList getAttributes() {
  178. return attributes;
  179. }
  180. /**
  181. * Adds a child element to the wrapped element.
  182. *
  183. * @param child The child element wrapper to add to this one.
  184. * Must not be <code>null</code>.
  185. */
  186. public void addChild(RuntimeConfigurable child) {
  187. if (children == null) {
  188. children = new ArrayList();
  189. }
  190. children.add(child);
  191. }
  192. /**
  193. * Returns the child wrapper at the specified position within the list.
  194. *
  195. * @param index The index of the child to return.
  196. *
  197. * @return The child wrapper at position <code>index</code> within the
  198. * list.
  199. */
  200. RuntimeConfigurable getChild(int index) {
  201. return (RuntimeConfigurable) children.get(index);
  202. }
  203. /**
  204. * Returns an enumeration of all child wrappers.
  205. * @return an enumeration of the child wrappers.
  206. * @since Ant 1.5.1
  207. */
  208. public Enumeration getChildren() {
  209. if (children != null) {
  210. return Collections.enumeration(children);
  211. } else {
  212. return new CollectionUtils.EmptyEnumeration();
  213. }
  214. }
  215. /**
  216. * Adds characters from #PCDATA areas to the wrapped element.
  217. *
  218. * @param data Text to add to the wrapped element.
  219. * Should not be <code>null</code>.
  220. */
  221. public void addText(String data) {
  222. if (data.length() == 0) {
  223. return;
  224. }
  225. if (characters != null) {
  226. characters.append(data);
  227. } else {
  228. characters = new StringBuffer(data);
  229. }
  230. }
  231. /**
  232. * Adds characters from #PCDATA areas to the wrapped element.
  233. *
  234. * @param buf A character array of the text within the element.
  235. * Must not be <code>null</code>.
  236. * @param start The start element in the array.
  237. * @param count The number of characters to read from the array.
  238. *
  239. */
  240. public void addText(char[] buf, int start, int count) {
  241. if (count == 0) {
  242. return;
  243. }
  244. if (characters == null) {
  245. characters = new StringBuffer(count);
  246. }
  247. characters.append(buf, start, count);
  248. }
  249. /** Get the text content of this element. Various text chunks are
  250. * concatenated, there is no way ( currently ) of keeping track of
  251. * multiple fragments.
  252. *
  253. * @return the text content of this element.
  254. */
  255. public StringBuffer getText() {
  256. if (characters != null) {
  257. return characters;
  258. } else {
  259. return new StringBuffer(0);
  260. }
  261. }
  262. /**
  263. * Returns the tag name of the wrapped element.
  264. *
  265. * @return The tag name of the wrapped element. This is unlikely
  266. * to be <code>null</code>, but may be.
  267. */
  268. public String getElementTag() {
  269. return elementTag;
  270. }
  271. /**
  272. * Configures the wrapped element and all its children.
  273. * The attributes and text for the wrapped element are configured,
  274. * and then each child is configured and added. Each time the
  275. * wrapper is configured, the attributes and text for it are
  276. * reset.
  277. *
  278. * If the element has an <code>id</code> attribute, a reference
  279. * is added to the project as well.
  280. *
  281. * @param p The project containing the wrapped element.
  282. * Must not be <code>null</code>.
  283. *
  284. * @exception BuildException if the configuration fails, for instance due
  285. * to invalid attributes or children, or text being added to
  286. * an element which doesn't accept it.
  287. */
  288. public void maybeConfigure(Project p) throws BuildException {
  289. maybeConfigure(p, true);
  290. }
  291. /**
  292. * Configures the wrapped element. The attributes and text for
  293. * the wrapped element are configured. Each time the wrapper is
  294. * configured, the attributes and text for it are reset.
  295. *
  296. * If the element has an <code>id</code> attribute, a reference
  297. * is added to the project as well.
  298. *
  299. * @param p The project containing the wrapped element.
  300. * Must not be <code>null</code>.
  301. *
  302. * @param configureChildren Whether to configure child elements as
  303. * well. if true, child elements will be configured after the
  304. * wrapped element.
  305. *
  306. * @exception BuildException if the configuration fails, for instance due
  307. * to invalid attributes or children, or text being added to
  308. * an element which doesn't accept it.
  309. */
  310. public void maybeConfigure(Project p, boolean configureChildren)
  311. throws BuildException {
  312. String id = null;
  313. if (proxyConfigured) {
  314. return;
  315. }
  316. // Configure the object
  317. Object target = (wrappedObject instanceof TypeAdapter)
  318. ? ((TypeAdapter) wrappedObject).getProxy() : wrappedObject;
  319. //PropertyHelper ph=PropertyHelper.getPropertyHelper(p);
  320. IntrospectionHelper ih =
  321. IntrospectionHelper.getHelper(p, target.getClass());
  322. if (attributeNames != null) {
  323. for (int i = 0; i < attributeNames.size(); i++) {
  324. String name = (String) attributeNames.get(i);
  325. String value = (String) attributeMap.get(name);
  326. // reflect these into the target
  327. value = p.replaceProperties(value);
  328. try {
  329. ih.setAttribute(p, target,
  330. name.toLowerCase(Locale.US), value);
  331. } catch (BuildException be) {
  332. // id attribute must be set externally
  333. if (!name.equals("id")) {
  334. throw be;
  335. }
  336. }
  337. }
  338. id = (String) attributeMap.get("id");
  339. }
  340. if (characters != null) {
  341. ProjectHelper.addText(p, wrappedObject, characters.substring(0));
  342. }
  343. Enumeration e = getChildren();
  344. while (e.hasMoreElements()) {
  345. RuntimeConfigurable child
  346. = (RuntimeConfigurable) e.nextElement();
  347. if (child.wrappedObject instanceof Task) {
  348. Task childTask = (Task) child.wrappedObject;
  349. childTask.setRuntimeConfigurableWrapper(child);
  350. }
  351. if ((child.creator != null) && configureChildren) {
  352. child.maybeConfigure(p);
  353. child.creator.store();
  354. continue;
  355. }
  356. /*
  357. * backwards compatibility - element names of nested
  358. * elements have been all lower-case in Ant, except for
  359. * tasks in TaskContainers.
  360. *
  361. * For TaskContainers, we simply skip configuration here.
  362. */
  363. String tag = child.getElementTag().toLowerCase(Locale.US);
  364. if (configureChildren
  365. && ih.supportsNestedElement(tag)) {
  366. child.maybeConfigure(p);
  367. ProjectHelper.storeChild(p, target, child.wrappedObject,
  368. tag);
  369. }
  370. }
  371. if (id != null) {
  372. p.addReference(id, wrappedObject);
  373. }
  374. proxyConfigured = true;
  375. }
  376. /**
  377. * Reconfigure the element, even if it has already been configured.
  378. *
  379. * @param p the project instance for this configuration.
  380. */
  381. public void reconfigure(Project p) {
  382. proxyConfigured = false;
  383. maybeConfigure(p);
  384. }
  385. /**
  386. * Apply presets, attributes and text are set if not currently set.
  387. * nested elements are prepended.
  388. *
  389. * @param r a <code>RuntimeConfigurable</code> value
  390. */
  391. public void applyPreSet(RuntimeConfigurable r) {
  392. // Attributes
  393. if (r.attributeMap != null) {
  394. for (Iterator i = r.attributeMap.keySet().iterator(); i.hasNext();) {
  395. String name = (String) i.next();
  396. if (attributeMap == null || attributeMap.get(name) == null) {
  397. setAttribute(name, (String) r.attributeMap.get(name));
  398. }
  399. }
  400. }
  401. // poly type
  402. if (r.polyType != null && polyType == null) {
  403. polyType = r.polyType;
  404. }
  405. // Children (this is a shadow of unknownElement#children)
  406. if (r.children != null) {
  407. List newChildren = new ArrayList();
  408. newChildren.addAll(r.children);
  409. if (children != null) {
  410. newChildren.addAll(children);
  411. }
  412. children = newChildren;
  413. }
  414. // Text
  415. if (r.characters != null) {
  416. if (characters == null
  417. || characters.toString().trim().length() == 0) {
  418. characters =
  419. new StringBuffer(r.characters.toString());
  420. }
  421. }
  422. }
  423. }