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.

RuntimeConfigurable.java 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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.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. /** Empty Hashtable. */
  39. private static final Hashtable EMPTY_HASHTABLE = new Hashtable(0);
  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. * XML attributes for the element.
  52. * @deprecated since 1.6.x
  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. */
  77. public RuntimeConfigurable(Object proxy, String elementTag) {
  78. setProxy(proxy);
  79. setElementTag(elementTag);
  80. // Most likely an UnknownElement
  81. if (proxy instanceof Task) {
  82. ((Task) proxy).setRuntimeConfigurableWrapper(this);
  83. }
  84. }
  85. /**
  86. * Sets the element to configure.
  87. *
  88. * @param proxy The element to configure. Must not be <code>null</code>.
  89. */
  90. public synchronized void setProxy(Object proxy) {
  91. wrappedObject = proxy;
  92. proxyConfigured = false;
  93. }
  94. /**
  95. * Sets the creator of the element to be configured
  96. * used to store the element in the parent.
  97. *
  98. * @param creator the creator object.
  99. */
  100. synchronized void setCreator(IntrospectionHelper.Creator creator) {
  101. this.creator = creator;
  102. }
  103. /**
  104. * Get the object for which this RuntimeConfigurable holds the configuration
  105. * information.
  106. *
  107. * @return the object whose configure is held by this instance.
  108. */
  109. public synchronized Object getProxy() {
  110. return wrappedObject;
  111. }
  112. /**
  113. * Get the polymorphic type for this element.
  114. * @return the ant component type name, null if not set.
  115. */
  116. public synchronized String getPolyType() {
  117. return polyType;
  118. }
  119. /**
  120. * Set the polymorphic type for this element.
  121. * @param polyType the ant component type name, null if not set.
  122. */
  123. public synchronized void setPolyType(String polyType) {
  124. this.polyType = polyType;
  125. }
  126. /**
  127. * Sets the attributes for the wrapped element.
  128. *
  129. * @deprecated since 1.6.x.
  130. * @param attributes List of attributes defined in the XML for this
  131. * element. May be <code>null</code>.
  132. */
  133. public synchronized void setAttributes(AttributeList attributes) {
  134. this.attributes = new AttributeListImpl(attributes);
  135. for (int i = 0; i < attributes.getLength(); i++) {
  136. setAttribute(attributes.getName(i), attributes.getValue(i));
  137. }
  138. }
  139. /**
  140. * Set an attribute to a given value.
  141. *
  142. * @param name the name of the attribute.
  143. * @param value the attribute's value.
  144. */
  145. public synchronized void setAttribute(String name, String value) {
  146. if (name.equalsIgnoreCase(ProjectHelper.ANT_TYPE)) {
  147. this.polyType = value;
  148. } else {
  149. if (attributeNames == null) {
  150. attributeNames = new ArrayList();
  151. attributeMap = new HashMap();
  152. }
  153. attributeNames.add(name);
  154. attributeMap.put(name, value);
  155. }
  156. }
  157. /**
  158. * Delete an attribute. Not for the faint of heart.
  159. * @param name the name of the attribute to be removed.
  160. */
  161. public synchronized void removeAttribute(String name) {
  162. attributeNames.remove(name);
  163. attributeMap.remove(name);
  164. }
  165. /**
  166. * Return the attribute map.
  167. *
  168. * @return Attribute name to attribute value map.
  169. * @since Ant 1.6
  170. */
  171. public synchronized Hashtable getAttributeMap() {
  172. return (attributeMap == null)
  173. ? EMPTY_HASHTABLE : new Hashtable(attributeMap);
  174. }
  175. /**
  176. * Returns the list of attributes for the wrapped element.
  177. *
  178. * @deprecated Deprecated since Ant 1.6 in favor of {@link #getAttributeMap}.
  179. * @return An AttributeList representing the attributes defined in the
  180. * XML for this element. May be <code>null</code>.
  181. */
  182. public synchronized AttributeList getAttributes() {
  183. return attributes;
  184. }
  185. /**
  186. * Adds a child element to the wrapped element.
  187. *
  188. * @param child The child element wrapper to add to this one.
  189. * Must not be <code>null</code>.
  190. */
  191. public synchronized void addChild(RuntimeConfigurable child) {
  192. children = (children == null) ? new ArrayList() : children;
  193. children.add(child);
  194. }
  195. /**
  196. * Returns the child wrapper at the specified position within the list.
  197. *
  198. * @param index The index of the child to return.
  199. *
  200. * @return The child wrapper at position <code>index</code> within the
  201. * list.
  202. */
  203. synchronized RuntimeConfigurable getChild(int index) {
  204. return (RuntimeConfigurable) children.get(index);
  205. }
  206. /**
  207. * Returns an enumeration of all child wrappers.
  208. * @return an enumeration of the child wrappers.
  209. * @since Ant 1.6
  210. */
  211. public synchronized Enumeration getChildren() {
  212. return (children == null) ? new CollectionUtils.EmptyEnumeration()
  213. : Collections.enumeration(children);
  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 synchronized void addText(String data) {
  222. if (data.length() == 0) {
  223. return;
  224. }
  225. characters = (characters == null)
  226. ? new StringBuffer(data) : characters.append(data);
  227. }
  228. /**
  229. * Adds characters from #PCDATA areas to the wrapped element.
  230. *
  231. * @param buf A character array of the text within the element.
  232. * Must not be <code>null</code>.
  233. * @param start The start element in the array.
  234. * @param count The number of characters to read from the array.
  235. *
  236. */
  237. public synchronized void addText(char[] buf, int start, int count) {
  238. if (count == 0) {
  239. return;
  240. }
  241. characters = ((characters == null)
  242. ? new StringBuffer(count) : characters).append(buf, start, count);
  243. }
  244. /**
  245. * Get the text content of this element. Various text chunks are
  246. * concatenated, there is no way ( currently ) of keeping track of
  247. * multiple fragments.
  248. *
  249. * @return the text content of this element.
  250. * @since Ant 1.6
  251. */
  252. public synchronized StringBuffer getText() {
  253. return (characters == null) ? new StringBuffer(0) : characters;
  254. }
  255. /**
  256. * Set the element tag.
  257. * @param elementTag The tag name generating this element.
  258. */
  259. public synchronized void setElementTag(String elementTag) {
  260. this.elementTag = elementTag;
  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 synchronized 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 synchronized 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. IntrospectionHelper ih =
  320. IntrospectionHelper.getHelper(p, target.getClass());
  321. if (attributeNames != null) {
  322. for (int i = 0; i < attributeNames.size(); i++) {
  323. String name = (String) attributeNames.get(i);
  324. String value = (String) attributeMap.get(name);
  325. // reflect these into the target
  326. value = p.replaceProperties(value);
  327. try {
  328. ih.setAttribute(p, target, name, value);
  329. } catch (UnsupportedAttributeException be) {
  330. // id attribute must be set externally
  331. if (name.equals("id")) {
  332. // Do nothing
  333. } else if (getElementTag() == null) {
  334. throw be;
  335. } else {
  336. throw new BuildException(
  337. getElementTag() + " doesn't support the \""
  338. + be.getAttribute() + "\" attribute", be);
  339. }
  340. } catch (BuildException be) {
  341. if (name.equals("id")) {
  342. // Assume that this is an not supported attribute type
  343. // thrown for example by a dymanic attribute task
  344. // Do nothing
  345. } else {
  346. throw be;
  347. }
  348. }
  349. }
  350. id = (String) attributeMap.get("id");
  351. }
  352. if (characters != null) {
  353. ProjectHelper.addText(p, wrappedObject, characters.substring(0));
  354. }
  355. Enumeration e = getChildren();
  356. while (e.hasMoreElements()) {
  357. RuntimeConfigurable child = (RuntimeConfigurable) e.nextElement();
  358. synchronized (child) {
  359. if (child.wrappedObject instanceof Task) {
  360. Task childTask = (Task) child.wrappedObject;
  361. childTask.setRuntimeConfigurableWrapper(child);
  362. }
  363. if ((child.creator != null) && configureChildren) {
  364. child.maybeConfigure(p);
  365. child.creator.store();
  366. continue;
  367. }
  368. /*
  369. * backwards compatibility - element names of nested
  370. * elements have been all lower-case in Ant, except for
  371. * tasks in TaskContainers.
  372. *
  373. * For TaskContainers, we simply skip configuration here.
  374. */
  375. String tag = child.getElementTag().toLowerCase(Locale.US);
  376. if (configureChildren && ih.supportsNestedElement(tag)) {
  377. child.maybeConfigure(p);
  378. ProjectHelper.storeChild(p, target, child.wrappedObject, tag);
  379. }
  380. }
  381. }
  382. if (id != null) {
  383. p.addReference(id, wrappedObject);
  384. }
  385. proxyConfigured = true;
  386. }
  387. /**
  388. * Reconfigure the element, even if it has already been configured.
  389. *
  390. * @param p the project instance for this configuration.
  391. */
  392. public void reconfigure(Project p) {
  393. proxyConfigured = false;
  394. maybeConfigure(p);
  395. }
  396. /**
  397. * Apply presets, attributes and text are set if not currently set.
  398. * Nested elements are prepended.
  399. *
  400. * @param r a <code>RuntimeConfigurable</code> value.
  401. */
  402. public void applyPreSet(RuntimeConfigurable r) {
  403. // Attributes
  404. if (r.attributeMap != null) {
  405. for (Iterator i = r.attributeMap.keySet().iterator(); i.hasNext();) {
  406. String name = (String) i.next();
  407. if (attributeMap == null || attributeMap.get(name) == null) {
  408. setAttribute(name, (String) r.attributeMap.get(name));
  409. }
  410. }
  411. }
  412. // poly type
  413. polyType = (polyType == null) ? r.polyType : polyType;
  414. // Children (this is a shadow of UnknownElement#children)
  415. if (r.children != null) {
  416. List newChildren = new ArrayList();
  417. newChildren.addAll(r.children);
  418. if (children != null) {
  419. newChildren.addAll(children);
  420. }
  421. children = newChildren;
  422. }
  423. // Text
  424. if (r.characters != null) {
  425. if (characters == null
  426. || characters.toString().trim().length() == 0) {
  427. characters = new StringBuffer(r.characters.toString());
  428. }
  429. }
  430. }
  431. }