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.

RuntimeConfigurable2.java 9.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2002 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 "The Jakarta Project", "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 org.apache.tools.ant.helper.*;
  56. import java.util.Enumeration;
  57. import java.util.Locale;
  58. import java.util.Vector;
  59. import java.util.Hashtable;
  60. import org.xml.sax.AttributeList;
  61. import org.xml.sax.Attributes;
  62. import org.xml.sax.helpers.AttributeListImpl;
  63. import org.xml.sax.helpers.AttributesImpl;
  64. /**
  65. * Wrapper class that holds the attributes of an element, its children, and
  66. * any text within it. It then takes care of configuring that element at
  67. * runtime.
  68. *
  69. * This uses SAX2 and a more flexible substitution mechansim, based on
  70. * o.a.tomcat.util.IntrospectionUtil.
  71. *
  72. * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  73. * @author Costin Manolache
  74. */
  75. public class RuntimeConfigurable2 extends RuntimeConfigurable {
  76. /** Name of the element to configure. */
  77. private String elementTag = null;
  78. /** List of child element wrappers. */
  79. private Vector children = new Vector();
  80. /** The element to configure. */
  81. private Object wrappedObject = null;
  82. /** XML attributes for the element. */
  83. private Attributes attributes;
  84. /** Text appearing within the element. */
  85. private StringBuffer characters = new StringBuffer();
  86. /**
  87. * Sole constructor creating a wrapper for the specified object.
  88. *
  89. * @param proxy The element to configure. Must not be <code>null</code>.
  90. * @param elementTag The tag name generating this element.
  91. * Should not be <code>null</code>.
  92. */
  93. public RuntimeConfigurable2(Object proxy, String elementTag) {
  94. super( proxy, elementTag );
  95. wrappedObject = proxy;
  96. this.elementTag = elementTag;
  97. if( proxy instanceof Task )
  98. ((Task)proxy).setRuntimeConfigurableWrapper( this );
  99. }
  100. /**
  101. * Sets the element to configure. This is used when the real type of
  102. * an element isn't known at the time of wrapper creation.
  103. *
  104. * @param proxy The element to configure. Must not be <code>null</code>.
  105. */
  106. public void setProxy(Object proxy) {
  107. wrappedObject = proxy;
  108. }
  109. public Object getProxy() {
  110. return wrappedObject;
  111. }
  112. /**
  113. * Sets the attributes for the wrapped element.
  114. *
  115. * @param attributes List of attributes defined in the XML for this
  116. * element. May be <code>null</code>.
  117. * @deprecated It shouldn't be called by anyone except ProjectHelper
  118. */
  119. public void setAttributes(AttributeList attributes) {
  120. // this.attributes = new AttributeListImpl(attributes);
  121. }
  122. public void setAttributes2(Attributes attributes) {
  123. this.attributes=new AttributesImpl( attributes );
  124. }
  125. /**
  126. * Returns the list of attributes for the wrapped element.
  127. *
  128. * @return An AttributeList representing the attributes defined in the
  129. * XML for this element. May be <code>null</code>.
  130. * @deprecated only for bkwd compatibility
  131. */
  132. public AttributeList getAttributes() {
  133. return sax1Attributes( attributes );
  134. }
  135. public Attributes getAttributes2() {
  136. return attributes;
  137. }
  138. public static AttributeList sax1Attributes( Attributes sax2Att ) {
  139. AttributeListImpl sax1Att=new AttributeListImpl();
  140. int length = sax2Att.getLength();
  141. if (length > 0) {
  142. for (int i = 0; i < length; i++) {
  143. // System.out.println("Attributes: " + sax2Att.getQName(i) + " " +
  144. // sax2Att.getValue(i));
  145. sax1Att.addAttribute( sax2Att.getQName(i),
  146. sax2Att.getType(i),
  147. sax2Att.getValue(i));
  148. }
  149. }
  150. return sax1Att;
  151. }
  152. /**
  153. * Adds a child element to the wrapped element.
  154. *
  155. * @param child The child element wrapper to add to this one.
  156. * Must not be <code>null</code>.
  157. */
  158. public void addChild(RuntimeConfigurable child) {
  159. children.addElement(child);
  160. }
  161. /**
  162. * Returns the child wrapper at the specified position within the list.
  163. *
  164. * @param index The index of the child to return.
  165. *
  166. * @return The child wrapper at position <code>index</code> within the
  167. * list.
  168. */
  169. public RuntimeConfigurable getChild(int index) {
  170. return (RuntimeConfigurable) children.elementAt(index);
  171. }
  172. /**
  173. * Adds characters from #PCDATA areas to the wrapped element.
  174. *
  175. * @param data Text to add to the wrapped element.
  176. * Should not be <code>null</code>.
  177. */
  178. public void addText(String data) {
  179. characters.append(data);
  180. }
  181. /**
  182. * Adds characters from #PCDATA areas to the wrapped element.
  183. *
  184. * @param buf A character array of the text within the element.
  185. * Must not be <code>null</code>.
  186. * @param start The start element in the array.
  187. * @param count The number of characters to read from the array.
  188. *
  189. */
  190. public void addText(char[] buf, int start, int count) {
  191. addText(new String(buf, start, count));
  192. }
  193. /**
  194. * Returns the tag name of the wrapped element.
  195. *
  196. * @return The tag name of the wrapped element. This is unlikely
  197. * to be <code>null</code>, but may be.
  198. */
  199. public String getElementTag() {
  200. return elementTag;
  201. }
  202. /**
  203. * Configures the wrapped element and all its children.
  204. * The attributes and text for the wrapped element are configured,
  205. * and then each child is configured and added. Each time the
  206. * wrapper is configured, the attributes and text for it are
  207. * reset.
  208. *
  209. * If the element has an <code>id</code> attribute, a reference
  210. * is added to the project as well.
  211. *
  212. * @param p The project containing the wrapped element.
  213. * Must not be <code>null</code>.
  214. *
  215. * @exception BuildException if the configuration fails, for instance due
  216. * to invalid attributes or children, or text being added to
  217. * an element which doesn't accept it.
  218. */
  219. public void maybeConfigure(Project p) throws BuildException {
  220. String id = null;
  221. if (attributes != null) {
  222. PropertyHelper ph=PropertyHelper.getPropertyHelper(p);
  223. ph.configure(wrappedObject, attributes, p);
  224. id = attributes.getValue("id");
  225. attributes = null;
  226. }
  227. if (characters.length() != 0) {
  228. ProjectHelper.addText(p, wrappedObject, characters.toString());
  229. characters.setLength(0);
  230. }
  231. Enumeration enum = children.elements();
  232. while (enum.hasMoreElements()) {
  233. RuntimeConfigurable2 child
  234. = (RuntimeConfigurable2) enum.nextElement();
  235. if (child.wrappedObject instanceof Task) {
  236. Task childTask = (Task) child.wrappedObject;
  237. childTask.setRuntimeConfigurableWrapper(child);
  238. childTask.maybeConfigure();
  239. } else {
  240. child.maybeConfigure(p);
  241. }
  242. ProjectHelper.storeChild(p, wrappedObject, child.wrappedObject,
  243. child.getElementTag().toLowerCase(Locale.US));
  244. }
  245. if (id != null) {
  246. p.addReference(id, wrappedObject);
  247. }
  248. }
  249. }