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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. public Object getProxy() {
  122. return wrappedObject;
  123. }
  124. /**
  125. * Sets the attributes for the wrapped element.
  126. *
  127. * @deprecated
  128. * @param attributes List of attributes defined in the XML for this
  129. * element. May be <code>null</code>.
  130. */
  131. public void setAttributes(AttributeList attributes) {
  132. this.attributes = new AttributeListImpl(attributes);
  133. for (int i = 0; i < attributes.getLength(); i++) {
  134. setAttribute(attributes.getName(i), attributes.getValue(i));
  135. }
  136. }
  137. public void setAttribute(String name, String value) {
  138. attributeNames.addElement(name);
  139. attributeMap.put(name, value);
  140. }
  141. /** Return the attribute map.
  142. *
  143. * @return Attribute name to attribute value map
  144. */
  145. public Hashtable getAttributeMap() {
  146. return attributeMap;
  147. }
  148. /**
  149. * Returns the list of attributes for the wrapped element.
  150. *
  151. * @deprecated
  152. * @return An AttributeList representing the attributes defined in the
  153. * XML for this element. May be <code>null</code>.
  154. */
  155. public AttributeList getAttributes() {
  156. return attributes;
  157. }
  158. /**
  159. * Adds a child element to the wrapped element.
  160. *
  161. * @param child The child element wrapper to add to this one.
  162. * Must not be <code>null</code>.
  163. */
  164. public void addChild(RuntimeConfigurable child) {
  165. children.addElement(child);
  166. }
  167. /**
  168. * Returns the child wrapper at the specified position within the list.
  169. *
  170. * @param index The index of the child to return.
  171. *
  172. * @return The child wrapper at position <code>index</code> within the
  173. * list.
  174. */
  175. RuntimeConfigurable getChild(int index) {
  176. return (RuntimeConfigurable) children.elementAt(index);
  177. }
  178. /**
  179. * Returns an enumeration of all child wrappers.
  180. *
  181. * @since Ant 1.5.1
  182. */
  183. Enumeration getChildren() {
  184. return children.elements();
  185. }
  186. /**
  187. * Adds characters from #PCDATA areas to the wrapped element.
  188. *
  189. * @param data Text to add to the wrapped element.
  190. * Should not be <code>null</code>.
  191. */
  192. public void addText(String data) {
  193. characters.append(data);
  194. }
  195. /**
  196. * Adds characters from #PCDATA areas to the wrapped element.
  197. *
  198. * @param buf A character array of the text within the element.
  199. * Must not be <code>null</code>.
  200. * @param start The start element in the array.
  201. * @param count The number of characters to read from the array.
  202. *
  203. */
  204. public void addText(char[] buf, int start, int count) {
  205. addText(new String(buf, start, count));
  206. }
  207. /** Get the text content of this element. Various text chunks are
  208. * concatenated, there is no way ( currently ) of keeping track of
  209. * multiple fragments.
  210. *
  211. * @return the text content of this element.
  212. */
  213. public StringBuffer getText() {
  214. return characters;
  215. }
  216. /**
  217. * Returns the tag name of the wrapped element.
  218. *
  219. * @return The tag name of the wrapped element. This is unlikely
  220. * to be <code>null</code>, but may be.
  221. */
  222. public String getElementTag() {
  223. return elementTag;
  224. }
  225. /**
  226. * Configures the wrapped element and all its children.
  227. * The attributes and text for the wrapped element are configured,
  228. * and then each child is configured and added. Each time the
  229. * wrapper is configured, the attributes and text for it are
  230. * reset.
  231. *
  232. * If the element has an <code>id</code> attribute, a reference
  233. * is added to the project as well.
  234. *
  235. * @param p The project containing the wrapped element.
  236. * Must not be <code>null</code>.
  237. *
  238. * @exception BuildException if the configuration fails, for instance due
  239. * to invalid attributes or children, or text being added to
  240. * an element which doesn't accept it.
  241. */
  242. public void maybeConfigure(Project p) throws BuildException {
  243. maybeConfigure(p, true);
  244. }
  245. /**
  246. * Configures the wrapped element. The attributes and text for
  247. * the wrapped element are configured. Each time the wrapper is
  248. * configured, the attributes and text for it are reset.
  249. *
  250. * If the element has an <code>id</code> attribute, a reference
  251. * is added to the project as well.
  252. *
  253. * @param p The project containing the wrapped element.
  254. * Must not be <code>null</code>.
  255. *
  256. * @param configureChildren Whether to configure child elements as
  257. * well. if true, child elements will be configured after the
  258. * wrapped element.
  259. *
  260. * @exception BuildException if the configuration fails, for instance due
  261. * to invalid attributes or children, or text being added to
  262. * an element which doesn't accept it.
  263. */
  264. public void maybeConfigure(Project p, boolean configureChildren)
  265. throws BuildException {
  266. String id = null;
  267. if (proxyConfigured) {
  268. return;
  269. }
  270. // Configure the object
  271. Object target = (wrappedObject instanceof TaskAdapter) ?
  272. ((TaskAdapter) wrappedObject).getProxy() : wrappedObject;
  273. //PropertyHelper ph=PropertyHelper.getPropertyHelper(p);
  274. IntrospectionHelper ih =
  275. IntrospectionHelper.getHelper(p, target.getClass());
  276. for (int i = 0; i < attributeNames.size(); i++) {
  277. String name = (String) attributeNames.elementAt(i);
  278. String value = (String) attributeMap.get(name);
  279. // reflect these into the target
  280. value = p.replaceProperties(value);
  281. try {
  282. ih.setAttribute(p, target,
  283. name.toLowerCase(Locale.US), value);
  284. } catch (BuildException be) {
  285. // id attribute must be set externally
  286. if (!name.equals("id")) {
  287. throw be;
  288. }
  289. }
  290. }
  291. id = (String) attributeMap.get("id");
  292. if (characters.length() != 0) {
  293. ProjectHelper.addText(p, wrappedObject, characters.substring(0));
  294. }
  295. Enumeration enum = children.elements();
  296. while (enum.hasMoreElements()) {
  297. RuntimeConfigurable child
  298. = (RuntimeConfigurable) enum.nextElement();
  299. if (child.wrappedObject instanceof Task) {
  300. Task childTask = (Task) child.wrappedObject;
  301. childTask.setRuntimeConfigurableWrapper(child);
  302. }
  303. if (configureChildren) {
  304. if (child.wrappedObject instanceof Task) {
  305. Task childTask = (Task) child.wrappedObject;
  306. // we don't configure tasks of task containers These
  307. // we be configured at the time they are used.
  308. if (!(target instanceof TaskContainer)) {
  309. childTask.maybeConfigure();
  310. }
  311. } else {
  312. child.maybeConfigure(p);
  313. }
  314. Object container = wrappedObject;
  315. if (container instanceof TaskAdapter) {
  316. container = ((TaskAdapter) container).getProxy();
  317. }
  318. ProjectHelper.storeChild(p, container, child.wrappedObject,
  319. child.getElementTag()
  320. .toLowerCase(Locale.US));
  321. }
  322. }
  323. if (id != null) {
  324. p.addReference(id, wrappedObject);
  325. }
  326. proxyConfigured = true;
  327. }
  328. }