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 KiB

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