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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 org.xml.sax.AttributeList;
  60. import org.xml.sax.Attributes;
  61. import org.xml.sax.helpers.AttributeListImpl;
  62. import org.xml.sax.helpers.AttributesImpl;
  63. /**
  64. * Wrapper class that holds the attributes of an element, its children, and
  65. * any text within it. It then takes care of configuring that element at
  66. * runtime.
  67. *
  68. * This uses SAX2 and a more flexible substitution mechansim, based on
  69. * o.a.tomcat.util.IntrospectionUtil.
  70. *
  71. * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  72. * @author Costin Manolache
  73. */
  74. public class RuntimeConfigurable2 extends RuntimeConfigurable {
  75. /** Name of the element to configure. */
  76. private String elementTag = null;
  77. /** List of child element wrappers. */
  78. private Vector children = new Vector();
  79. /** The element to configure. */
  80. private Object wrappedObject = null;
  81. /** XML attributes for the element. */
  82. private Attributes attributes;
  83. /** Text appearing within the element. */
  84. private StringBuffer characters = new StringBuffer();
  85. /**
  86. * Sole constructor creating a wrapper for the specified object.
  87. *
  88. * @param proxy The element to configure. Must not be <code>null</code>.
  89. * @param elementTag The tag name generating this element.
  90. * Should not be <code>null</code>.
  91. */
  92. public RuntimeConfigurable2(Object proxy, String elementTag) {
  93. super( proxy, elementTag );
  94. wrappedObject = proxy;
  95. this.elementTag = elementTag;
  96. }
  97. /**
  98. * Sets the element to configure. This is used when the real type of
  99. * an element isn't known at the time of wrapper creation.
  100. *
  101. * @param proxy The element to configure. Must not be <code>null</code>.
  102. */
  103. void setProxy(Object proxy) {
  104. wrappedObject = proxy;
  105. }
  106. /**
  107. * Sets the attributes for the wrapped element.
  108. *
  109. * @param attributes List of attributes defined in the XML for this
  110. * element. May be <code>null</code>.
  111. * @deprecated It shouldn't be called by anyone except ProjectHelper
  112. */
  113. public void setAttributes(AttributeList attributes) {
  114. // this.attributes = new AttributeListImpl(attributes);
  115. }
  116. public void setAttributes2(Attributes attributes) {
  117. this.attributes=new AttributesImpl( attributes );
  118. }
  119. /**
  120. * Returns the list of attributes for the wrapped element.
  121. *
  122. * @return An AttributeList representing the attributes defined in the
  123. * XML for this element. May be <code>null</code>.
  124. * @deprecated only for bkwd compatibility
  125. */
  126. public AttributeList getAttributes() {
  127. return sax1Attributes( attributes );
  128. }
  129. public Attributes getAttributes2() {
  130. return attributes;
  131. }
  132. public static AttributeList sax1Attributes( Attributes sax2Att ) {
  133. AttributeListImpl sax1Att=new AttributeListImpl();
  134. int length = sax2Att.getLength();
  135. if (length > 0) {
  136. for (int i = 0; i < length; i++) {
  137. // System.out.println("Attributes: " + sax2Att.getQName(i) + " " +
  138. // sax2Att.getValue(i));
  139. sax1Att.addAttribute( sax2Att.getQName(i),
  140. sax2Att.getType(i),
  141. sax2Att.getValue(i));
  142. }
  143. }
  144. return sax1Att;
  145. }
  146. /**
  147. * Adds a child element to the wrapped element.
  148. *
  149. * @param child The child element wrapper to add to this one.
  150. * Must not be <code>null</code>.
  151. */
  152. public void addChild(RuntimeConfigurable child) {
  153. children.addElement(child);
  154. }
  155. /**
  156. * Returns the child wrapper at the specified position within the list.
  157. *
  158. * @param index The index of the child to return.
  159. *
  160. * @return The child wrapper at position <code>index</code> within the
  161. * list.
  162. */
  163. RuntimeConfigurable getChild(int index) {
  164. return (RuntimeConfigurable) children.elementAt(index);
  165. }
  166. /**
  167. * Adds characters from #PCDATA areas to the wrapped element.
  168. *
  169. * @param data Text to add to the wrapped element.
  170. * Should not be <code>null</code>.
  171. */
  172. public void addText(String data) {
  173. characters.append(data);
  174. }
  175. /**
  176. * Adds characters from #PCDATA areas to the wrapped element.
  177. *
  178. * @param buf A character array of the text within the element.
  179. * Must not be <code>null</code>.
  180. * @param start The start element in the array.
  181. * @param count The number of characters to read from the array.
  182. *
  183. */
  184. public void addText(char[] buf, int start, int count) {
  185. addText(new String(buf, start, count));
  186. }
  187. /**
  188. * Returns the tag name of the wrapped element.
  189. *
  190. * @return The tag name of the wrapped element. This is unlikely
  191. * to be <code>null</code>, but may be.
  192. */
  193. public String getElementTag() {
  194. return elementTag;
  195. }
  196. /**
  197. * Configures the wrapped element and all its children.
  198. * The attributes and text for the wrapped element are configured,
  199. * and then each child is configured and added. Each time the
  200. * wrapper is configured, the attributes and text for it are
  201. * reset.
  202. *
  203. * If the element has an <code>id</code> attribute, a reference
  204. * is added to the project as well.
  205. *
  206. * @param p The project containing the wrapped element.
  207. * Must not be <code>null</code>.
  208. *
  209. * @exception BuildException if the configuration fails, for instance due
  210. * to invalid attributes or children, or text being added to
  211. * an element which doesn't accept it.
  212. */
  213. public void maybeConfigure(Project p) throws BuildException {
  214. String id = null;
  215. if (attributes != null) {
  216. configure(wrappedObject, attributes, p);
  217. id = attributes.getValue("id");
  218. attributes = null;
  219. }
  220. if (characters.length() != 0) {
  221. ProjectHelper.addText(p, wrappedObject, characters.toString());
  222. characters.setLength(0);
  223. }
  224. Enumeration enum = children.elements();
  225. while (enum.hasMoreElements()) {
  226. RuntimeConfigurable2 child
  227. = (RuntimeConfigurable2) enum.nextElement();
  228. if (child.wrappedObject instanceof Task) {
  229. Task childTask = (Task) child.wrappedObject;
  230. childTask.setRuntimeConfigurableWrapper(child);
  231. childTask.maybeConfigure();
  232. } else {
  233. child.maybeConfigure(p);
  234. }
  235. ProjectHelper.storeChild(p, wrappedObject, child.wrappedObject,
  236. child.getElementTag().toLowerCase(Locale.US));
  237. }
  238. if (id != null) {
  239. p.addReference(id, wrappedObject);
  240. }
  241. }
  242. public static void configure( Object target, Attributes attrs, Project project )
  243. throws BuildException
  244. {
  245. if (target instanceof TaskAdapter) {
  246. target = ((TaskAdapter) target).getProxy();
  247. }
  248. IntrospectionHelper ih =
  249. IntrospectionHelper.getHelper(target.getClass());
  250. project.addBuildListener(ih);
  251. for (int i = 0; i < attrs.getLength(); i++) {
  252. // reflect these into the target
  253. String value = RuntimeConfigurable2.replaceProperties(project, attrs.getValue(i));
  254. try {
  255. ih.setAttribute(project, target,
  256. attrs.getQName(i).toLowerCase(Locale.US), value);
  257. } catch (BuildException be) {
  258. // id attribute must be set externally
  259. if (!attrs.getQName(i).equals("id")) {
  260. throw be;
  261. }
  262. }
  263. }
  264. }
  265. public static String replaceProperties( Project project ,String value ) {
  266. if (value == null) {
  267. return null;
  268. }
  269. Vector fragments = new Vector();
  270. Vector propertyRefs = new Vector();
  271. ProjectHelper.parsePropertyString(value, fragments, propertyRefs);
  272. StringBuffer sb = new StringBuffer();
  273. Enumeration i = fragments.elements();
  274. Enumeration j = propertyRefs.elements();
  275. while (i.hasMoreElements()) {
  276. String fragment = (String) i.nextElement();
  277. if (fragment == null) {
  278. String propertyName = (String) j.nextElement();
  279. Object repl=project.getProperty( propertyName );
  280. if( repl==null) {
  281. // Try a dynamic substitiution using ref
  282. repl=processReference( project, propertyName );
  283. }
  284. if (repl==null ) {
  285. project.log("Property ${" + propertyName
  286. + "} has not been set", Project.MSG_VERBOSE);
  287. fragment="${" + propertyName + "}";
  288. } else {
  289. fragment = (String) repl;
  290. }
  291. }
  292. sb.append(fragment);
  293. }
  294. return sb.toString();
  295. }
  296. /** Use the reference table to generate values for ${} substitution.
  297. * To preserve backward compat ( as much as possible ) we'll only process
  298. * ids with a 'namespace-like' syntax.
  299. *
  300. * Currently we support:
  301. * dom:idName:/xpath/like/syntax - the referenced node must be a DOM, we'll use
  302. * XPath to extract a node. ( a simplified syntax is handled
  303. * directly, XXX used for 'real' xpaths ).
  304. * toString:idName - we use toString on the referenced object
  305. * bean:idName.propertyName - we get the idName and call the getter for the property.
  306. */
  307. static String processReference( Project project, String name ) {
  308. if( name.startsWith("dom:") ) {
  309. name=name.substring( 4 );
  310. int idx=name.indexOf(":");
  311. if( idx<0 ) return null;
  312. String objName=name.substring( 0, idx );
  313. String path=name.substring( idx );
  314. System.out.println("XXX dom: " + objName + " " + path );
  315. Object v=project.getReference( objName );
  316. if( v==null ) return null;
  317. }
  318. if( name.startsWith( "toString:" )) {
  319. name=name.substring( "toString:".length());
  320. Object v=project.getReference( name );
  321. if( v==null ) return null;
  322. return v.toString();
  323. }
  324. if( name.startsWith( "bean:" )) {
  325. name=name.substring( "toString:".length());
  326. int idx=name.indexOf(":");
  327. if( idx<0 ) return null;
  328. String objName=name.substring( 0, idx );
  329. String path=name.substring( idx );
  330. System.out.println("XXX bean: " + objName + " " + path );
  331. Object v=project.getReference( objName );
  332. if( v==null ) return null;
  333. return v.toString();
  334. }
  335. // If everything else fails, use toString()
  336. return null;
  337. }
  338. }