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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "Ant" and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant;
  55. import java.util.Enumeration;
  56. import java.util.Locale;
  57. import java.util.Vector;
  58. import java.util.Hashtable;
  59. import java.io.Serializable;
  60. import org.xml.sax.AttributeList;
  61. import org.xml.sax.helpers.AttributeListImpl;
  62. /**
  63. * Wrapper class that holds the attributes of an element, its children, and
  64. * any text within it. It then takes care of configuring that element at
  65. * runtime.
  66. *
  67. * @author Stefan Bodewig
  68. */
  69. public class RuntimeConfigurable implements Serializable {
  70. /** Name of the element to configure. */
  71. private String elementTag = null;
  72. /** List of child element wrappers. */
  73. private Vector children = new Vector();
  74. /** The element to configure. It is only used during
  75. * maybeConfigure.
  76. */
  77. private transient Object wrappedObject = null;
  78. /**
  79. * @deprecated
  80. * XML attributes for the element.
  81. */
  82. private transient AttributeList attributes;
  83. /** Attribute names and values. While the XML spec doesn't require
  84. * preserving the order ( AFAIK ), some ant tests do rely on the
  85. * exact order. The following code is copied from AttributeImpl.
  86. * We could also just use SAX2 Attributes and convert to SAX1 ( DOM
  87. * attribute Nodes can also be stored in SAX2 Attributges )
  88. */
  89. private Vector attributeNames = new Vector();
  90. /** Map of attribute names to values */
  91. private Hashtable attributeMap = new Hashtable();
  92. /** Text appearing within the element. */
  93. private StringBuffer characters = new StringBuffer();
  94. /** Indicates if the wrapped object has been configured */
  95. private boolean proxyConfigured = false;
  96. /**
  97. * Sole constructor creating a wrapper for the specified object.
  98. *
  99. * @param proxy The element to configure. Must not be <code>null</code>.
  100. * @param elementTag The tag name generating this element.
  101. * Should not be <code>null</code>.
  102. */
  103. public RuntimeConfigurable(Object proxy, String elementTag) {
  104. wrappedObject = proxy;
  105. this.elementTag = elementTag;
  106. proxyConfigured = false;
  107. // Most likely an UnknownElement
  108. if (proxy instanceof Task) {
  109. ((Task) proxy).setRuntimeConfigurableWrapper(this);
  110. }
  111. }
  112. /**
  113. * Sets the element to configure.
  114. *
  115. * @param proxy The element to configure. Must not be <code>null</code>.
  116. */
  117. void setProxy(Object proxy) {
  118. wrappedObject = proxy;
  119. proxyConfigured = false;
  120. }
  121. /**
  122. * Get the object for which this RuntimeConfigurable holds the configuration
  123. * information
  124. *
  125. * @return the object whose configure is held by this instance.
  126. */
  127. public Object getProxy() {
  128. return wrappedObject;
  129. }
  130. /**
  131. * Sets the attributes for the wrapped element.
  132. *
  133. * @deprecated
  134. * @param attributes List of attributes defined in the XML for this
  135. * element. May be <code>null</code>.
  136. */
  137. public void setAttributes(AttributeList attributes) {
  138. this.attributes = new AttributeListImpl(attributes);
  139. for (int i = 0; i < attributes.getLength(); i++) {
  140. setAttribute(attributes.getName(i), attributes.getValue(i));
  141. }
  142. }
  143. /**
  144. * Set an attribute to a given value
  145. *
  146. * @param name the name of the attribute.
  147. * @param value the attribute's value.
  148. */
  149. public void setAttribute(String name, String value) {
  150. attributeNames.addElement(name);
  151. attributeMap.put(name, value);
  152. }
  153. /** Return the attribute map.
  154. *
  155. * @return Attribute name to attribute value map
  156. */
  157. public Hashtable getAttributeMap() {
  158. return attributeMap;
  159. }
  160. /**
  161. * Returns the list of attributes for the wrapped element.
  162. *
  163. * @deprecated
  164. * @return An AttributeList representing the attributes defined in the
  165. * XML for this element. May be <code>null</code>.
  166. */
  167. public AttributeList getAttributes() {
  168. return attributes;
  169. }
  170. /**
  171. * Adds a child element to the wrapped element.
  172. *
  173. * @param child The child element wrapper to add to this one.
  174. * Must not be <code>null</code>.
  175. */
  176. public void addChild(RuntimeConfigurable child) {
  177. children.addElement(child);
  178. }
  179. /**
  180. * Returns the child wrapper at the specified position within the list.
  181. *
  182. * @param index The index of the child to return.
  183. *
  184. * @return The child wrapper at position <code>index</code> within the
  185. * list.
  186. */
  187. RuntimeConfigurable getChild(int index) {
  188. return (RuntimeConfigurable) children.elementAt(index);
  189. }
  190. /**
  191. * Returns an enumeration of all child wrappers.
  192. *
  193. * @since Ant 1.5.1
  194. */
  195. Enumeration getChildren() {
  196. return children.elements();
  197. }
  198. /**
  199. * Adds characters from #PCDATA areas to the wrapped element.
  200. *
  201. * @param data Text to add to the wrapped element.
  202. * Should not be <code>null</code>.
  203. */
  204. public void addText(String data) {
  205. characters.append(data);
  206. }
  207. /**
  208. * Adds characters from #PCDATA areas to the wrapped element.
  209. *
  210. * @param buf A character array of the text within the element.
  211. * Must not be <code>null</code>.
  212. * @param start The start element in the array.
  213. * @param count The number of characters to read from the array.
  214. *
  215. */
  216. public void addText(char[] buf, int start, int count) {
  217. addText(new String(buf, start, count));
  218. }
  219. /** Get the text content of this element. Various text chunks are
  220. * concatenated, there is no way ( currently ) of keeping track of
  221. * multiple fragments.
  222. *
  223. * @return the text content of this element.
  224. */
  225. public StringBuffer getText() {
  226. return characters;
  227. }
  228. /**
  229. * Returns the tag name of the wrapped element.
  230. *
  231. * @return The tag name of the wrapped element. This is unlikely
  232. * to be <code>null</code>, but may be.
  233. */
  234. public String getElementTag() {
  235. return elementTag;
  236. }
  237. /**
  238. * Configures the wrapped element and all its children.
  239. * The attributes and text for the wrapped element are configured,
  240. * and then each child is configured and added. Each time the
  241. * wrapper is configured, the attributes and text for it are
  242. * reset.
  243. *
  244. * If the element has an <code>id</code> attribute, a reference
  245. * is added to the project as well.
  246. *
  247. * @param p The project containing the wrapped element.
  248. * Must not be <code>null</code>.
  249. *
  250. * @exception BuildException if the configuration fails, for instance due
  251. * to invalid attributes or children, or text being added to
  252. * an element which doesn't accept it.
  253. */
  254. public void maybeConfigure(Project p) throws BuildException {
  255. maybeConfigure(p, true);
  256. }
  257. /**
  258. * Configures the wrapped element. The attributes and text for
  259. * the wrapped element are configured. Each time the wrapper is
  260. * configured, the attributes and text for it are reset.
  261. *
  262. * If the element has an <code>id</code> attribute, a reference
  263. * is added to the project as well.
  264. *
  265. * @param p The project containing the wrapped element.
  266. * Must not be <code>null</code>.
  267. *
  268. * @param configureChildren Whether to configure child elements as
  269. * well. if true, child elements will be configured after the
  270. * wrapped element.
  271. *
  272. * @exception BuildException if the configuration fails, for instance due
  273. * to invalid attributes or children, or text being added to
  274. * an element which doesn't accept it.
  275. */
  276. public void maybeConfigure(Project p, boolean configureChildren)
  277. throws BuildException {
  278. String id = null;
  279. if (proxyConfigured) {
  280. return;
  281. }
  282. // Configure the object
  283. Object target = (wrappedObject instanceof TaskAdapter) ?
  284. ((TaskAdapter) wrappedObject).getProxy() : wrappedObject;
  285. //PropertyHelper ph=PropertyHelper.getPropertyHelper(p);
  286. IntrospectionHelper ih =
  287. IntrospectionHelper.getHelper(p, target.getClass());
  288. for (int i = 0; i < attributeNames.size(); i++) {
  289. String name = (String) attributeNames.elementAt(i);
  290. String value = (String) attributeMap.get(name);
  291. // reflect these into the target
  292. value = p.replaceProperties(value);
  293. try {
  294. ih.setAttribute(p, target,
  295. name.toLowerCase(Locale.US), value);
  296. } catch (BuildException be) {
  297. // id attribute must be set externally
  298. if (!name.equals("id")) {
  299. throw be;
  300. }
  301. }
  302. }
  303. id = (String) attributeMap.get("id");
  304. if (characters.length() != 0) {
  305. ProjectHelper.addText(p, wrappedObject, characters.substring(0));
  306. }
  307. Enumeration enum = children.elements();
  308. while (enum.hasMoreElements()) {
  309. RuntimeConfigurable child
  310. = (RuntimeConfigurable) enum.nextElement();
  311. if (child.wrappedObject instanceof Task) {
  312. Task childTask = (Task) child.wrappedObject;
  313. childTask.setRuntimeConfigurableWrapper(child);
  314. }
  315. if (configureChildren) {
  316. /*
  317. * backwards compatibility - element names of nested
  318. * elements have been all lower-case in Ant, except for
  319. * TaskContainers
  320. */
  321. /* XXX
  322. *
  323. * For some reason we don't throw an exception here if
  324. * we find the nested element is unsupported, probably
  325. * because this will happen somewhere else.
  326. */
  327. String tag = child.getElementTag();
  328. if (ih.supportsNestedElement(tag.toLowerCase(Locale.US))) {
  329. tag = tag.toLowerCase(Locale.US);
  330. } else if (!ih.supportsNestedElement(tag)) {
  331. continue;
  332. }
  333. child.maybeConfigure(p);
  334. ProjectHelper.storeChild(p, target, child.wrappedObject,
  335. tag);
  336. }
  337. }
  338. if (id != null) {
  339. p.addReference(id, wrappedObject);
  340. }
  341. proxyConfigured = true;
  342. }
  343. /**
  344. * Reconfigure the element, even if it has already been configured.
  345. *
  346. * @param p the project instance for this configuration.
  347. */
  348. public void reconfigure(Project p) {
  349. proxyConfigured = false;
  350. maybeConfigure(p);
  351. }
  352. }