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

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant;
  19. import java.io.Serializable;
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.Enumeration;
  23. import java.util.Hashtable;
  24. import java.util.LinkedHashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import org.apache.tools.ant.attribute.EnableAttribute;
  28. import org.apache.tools.ant.taskdefs.MacroDef.Attribute;
  29. import org.apache.tools.ant.taskdefs.MacroInstance;
  30. import org.xml.sax.AttributeList;
  31. import org.xml.sax.helpers.AttributeListImpl;
  32. /**
  33. * Wrapper class that holds the attributes of an element, its children, and
  34. * any text within it. It then takes care of configuring that element at
  35. * runtime.
  36. */
  37. public class RuntimeConfigurable implements Serializable {
  38. /** Serialization version */
  39. private static final long serialVersionUID = 1L;
  40. /** Empty Hashtable. */
  41. private static final Hashtable<String, Object> EMPTY_HASHTABLE =
  42. new Hashtable<>(0);
  43. /** Name of the element to configure. */
  44. private String elementTag = null;
  45. /** List of child element wrappers. */
  46. // picking ArrayList rather than List as arrayList is Serializable
  47. private List<RuntimeConfigurable> children = null;
  48. /** The element to configure. It is only used during
  49. * maybeConfigure.
  50. */
  51. private transient Object wrappedObject = null;
  52. /**
  53. * XML attributes for the element.
  54. * @deprecated since 1.6.x
  55. */
  56. private transient AttributeList attributes;
  57. // The following is set to true if any of the attributes are namespaced
  58. private transient boolean namespacedAttribute = false;
  59. /** Attribute names and values. While the XML spec doesn't require
  60. * preserving the order (AFAIK), some ant tests do rely on the
  61. * exact order.
  62. * The only exception to this order is the treatment of
  63. * refid. A number of datatypes check if refid is set
  64. * when other attributes are set. This check will not
  65. * work if the build script has the other attribute before
  66. * the "refid" attribute, so now (ANT 1.7) the refid
  67. * attribute will be processed first.
  68. */
  69. private LinkedHashMap<String, Object> attributeMap = null;
  70. /** Text appearing within the element. */
  71. private StringBuffer characters = null;
  72. /** Indicates if the wrapped object has been configured */
  73. private boolean proxyConfigured = false;
  74. /** the polymorphic type */
  75. private String polyType = null;
  76. /** the "id" of this Element if it has one */
  77. private String id = null;
  78. /**
  79. * Sole constructor creating a wrapper for the specified object.
  80. *
  81. * @param proxy The element to configure. Must not be <code>null</code>.
  82. * @param elementTag The tag name generating this element.
  83. */
  84. public RuntimeConfigurable(Object proxy, String elementTag) {
  85. setProxy(proxy);
  86. setElementTag(elementTag);
  87. // Most likely an UnknownElement
  88. if (proxy instanceof Task) {
  89. ((Task) proxy).setRuntimeConfigurableWrapper(this);
  90. }
  91. }
  92. /**
  93. * Sets the element to configure.
  94. *
  95. * @param proxy The element to configure. Must not be <code>null</code>.
  96. */
  97. public synchronized void setProxy(Object proxy) {
  98. wrappedObject = proxy;
  99. proxyConfigured = false;
  100. }
  101. private static class EnableAttributeConsumer {
  102. public void add(EnableAttribute b) {
  103. // Ignore
  104. }
  105. }
  106. /**
  107. * contains the attribute component name and boolean restricted set to true when
  108. * the attribute is in one of the name spaces managed by ant (if and unless currently)
  109. * @since Ant 1.9.3
  110. */
  111. private static class AttributeComponentInformation {
  112. String componentName;
  113. boolean restricted;
  114. private AttributeComponentInformation(String componentName, boolean restricted) {
  115. this.componentName = componentName;
  116. this.restricted = restricted;
  117. }
  118. public String getComponentName() {
  119. return componentName;
  120. }
  121. public boolean isRestricted() {
  122. return restricted;
  123. }
  124. }
  125. /**
  126. *
  127. * @param name the name of the attribute.
  128. * @param componentHelper current component helper
  129. * @return AttributeComponentInformation instance
  130. */
  131. private AttributeComponentInformation isRestrictedAttribute(String name, ComponentHelper componentHelper) {
  132. if (name.indexOf(':') == -1) {
  133. return new AttributeComponentInformation(null, false);
  134. }
  135. String componentName = attrToComponent(name);
  136. String ns = ProjectHelper.extractUriFromComponentName(componentName);
  137. if (componentHelper.getRestrictedDefinitions(
  138. ProjectHelper.nsToComponentName(ns)) == null) {
  139. return new AttributeComponentInformation(null, false);
  140. }
  141. return new AttributeComponentInformation(componentName, true);
  142. }
  143. /**
  144. * Check if an UE is enabled.
  145. * This looks tru the attributes and checks if there
  146. * are any Ant attributes, and if so, the method calls the
  147. * isEnabled() method on them.
  148. * @param owner the UE that owns this RC.
  149. * @return true if enabled, false if any of the ant attributes return
  150. * false.
  151. * @since 1.9.1
  152. */
  153. public boolean isEnabled(UnknownElement owner) {
  154. if (!namespacedAttribute) {
  155. return true;
  156. }
  157. ComponentHelper componentHelper = ComponentHelper
  158. .getComponentHelper(owner.getProject());
  159. IntrospectionHelper ih
  160. = IntrospectionHelper.getHelper(
  161. owner.getProject(), EnableAttributeConsumer.class);
  162. for (int i = 0; i < attributeMap.keySet().size(); ++i) {
  163. String name = (String) attributeMap.keySet().toArray()[i];
  164. AttributeComponentInformation attributeComponentInformation = isRestrictedAttribute(name, componentHelper);
  165. if (!attributeComponentInformation.isRestricted()) {
  166. continue;
  167. }
  168. String value = (String) attributeMap.get(name);
  169. EnableAttribute enable = null;
  170. try {
  171. enable = (EnableAttribute)
  172. ih.createElement(
  173. owner.getProject(), new EnableAttributeConsumer(),
  174. attributeComponentInformation.getComponentName());
  175. } catch (BuildException ex) {
  176. throw new BuildException(
  177. "Unsupported attribute " + attributeComponentInformation.getComponentName());
  178. }
  179. if (enable == null) {
  180. continue;
  181. }
  182. value = owner.getProject().replaceProperties(value); // FixMe: need to make config
  183. if (!enable.isEnabled(owner, value)) {
  184. return false;
  185. }
  186. }
  187. return true;
  188. }
  189. private String attrToComponent(String a) {
  190. // need to remove the prefix
  191. int p1 = a.lastIndexOf(':');
  192. int p2 = a.lastIndexOf(':', p1 - 1);
  193. return a.substring(0, p2) + a.substring(p1);
  194. }
  195. /**
  196. * Sets the creator of the element to be configured
  197. * used to store the element in the parent.
  198. *
  199. * @param creator the creator object.
  200. */
  201. synchronized void setCreator(IntrospectionHelper.Creator creator) {
  202. }
  203. /**
  204. * Get the object for which this RuntimeConfigurable holds the configuration
  205. * information.
  206. *
  207. * @return the object whose configure is held by this instance.
  208. */
  209. public synchronized Object getProxy() {
  210. return wrappedObject;
  211. }
  212. /**
  213. * Returns the id for this element.
  214. * @return the id.
  215. */
  216. public synchronized String getId() {
  217. return id;
  218. }
  219. /**
  220. * Get the polymorphic type for this element.
  221. * @return the ant component type name, null if not set.
  222. */
  223. public synchronized String getPolyType() {
  224. return polyType;
  225. }
  226. /**
  227. * Set the polymorphic type for this element.
  228. * @param polyType the ant component type name, null if not set.
  229. */
  230. public synchronized void setPolyType(String polyType) {
  231. this.polyType = polyType;
  232. }
  233. /**
  234. * Sets the attributes for the wrapped element.
  235. *
  236. * @param attributes List of attributes defined in the XML for this
  237. * element. May be <code>null</code>.
  238. * @deprecated since 1.6.x.
  239. */
  240. @Deprecated
  241. public synchronized void setAttributes(AttributeList attributes) {
  242. this.attributes = new AttributeListImpl(attributes);
  243. for (int i = 0; i < attributes.getLength(); i++) {
  244. setAttribute(attributes.getName(i), attributes.getValue(i));
  245. }
  246. }
  247. /**
  248. * Set an attribute to a given value.
  249. *
  250. * @param name the name of the attribute.
  251. * @param value the attribute's value.
  252. */
  253. public synchronized void setAttribute(String name, String value) {
  254. if (name.indexOf(':') != -1) {
  255. namespacedAttribute = true;
  256. }
  257. setAttribute(name, (Object) value);
  258. }
  259. /**
  260. * Set an attribute to a given value.
  261. *
  262. * @param name the name of the attribute.
  263. * @param value the attribute's value.
  264. * @since 1.9
  265. */
  266. public synchronized void setAttribute(String name, Object value) {
  267. if (name.equalsIgnoreCase(ProjectHelper.ANT_TYPE)) {
  268. this.polyType = value == null ? null : value.toString();
  269. } else {
  270. if (attributeMap == null) {
  271. attributeMap = new LinkedHashMap<>();
  272. }
  273. if ("refid".equalsIgnoreCase(name) && !attributeMap.isEmpty()) {
  274. LinkedHashMap<String, Object> newAttributeMap = new LinkedHashMap<>();
  275. newAttributeMap.put(name, value);
  276. newAttributeMap.putAll(attributeMap);
  277. attributeMap = newAttributeMap;
  278. } else {
  279. attributeMap.put(name, value);
  280. }
  281. if ("id".equals(name)) {
  282. this.id = value == null ? null : value.toString();
  283. }
  284. }
  285. }
  286. /**
  287. * Delete an attribute. Not for the faint of heart.
  288. * @param name the name of the attribute to be removed.
  289. */
  290. public synchronized void removeAttribute(String name) {
  291. attributeMap.remove(name);
  292. }
  293. /**
  294. * Return the attribute map.
  295. *
  296. * @return Attribute name to attribute value map.
  297. * @since Ant 1.6
  298. */
  299. public synchronized Hashtable<String, Object> getAttributeMap() {
  300. return (attributeMap == null)
  301. ? EMPTY_HASHTABLE : new Hashtable<>(attributeMap);
  302. }
  303. /**
  304. * Returns the list of attributes for the wrapped element.
  305. *
  306. * @return An AttributeList representing the attributes defined in the
  307. * XML for this element. May be <code>null</code>.
  308. * @deprecated Deprecated since Ant 1.6 in favor of {@link #getAttributeMap}.
  309. */
  310. @Deprecated
  311. public synchronized AttributeList getAttributes() {
  312. return attributes;
  313. }
  314. /**
  315. * Adds a child element to the wrapped element.
  316. *
  317. * @param child The child element wrapper to add to this one.
  318. * Must not be <code>null</code>.
  319. */
  320. public synchronized void addChild(RuntimeConfigurable child) {
  321. children = (children == null) ? new ArrayList<>() : children;
  322. children.add(child);
  323. }
  324. /**
  325. * Returns the child wrapper at the specified position within the list.
  326. *
  327. * @param index The index of the child to return.
  328. *
  329. * @return The child wrapper at position <code>index</code> within the
  330. * list.
  331. */
  332. synchronized RuntimeConfigurable getChild(int index) {
  333. return children.get(index);
  334. }
  335. /**
  336. * Returns an enumeration of all child wrappers.
  337. * @return an enumeration of the child wrappers.
  338. * @since Ant 1.6
  339. */
  340. public synchronized Enumeration<RuntimeConfigurable> getChildren() {
  341. return (children == null) ? Collections.emptyEnumeration()
  342. : Collections.enumeration(children);
  343. }
  344. /**
  345. * Adds characters from #PCDATA areas to the wrapped element.
  346. *
  347. * @param data Text to add to the wrapped element.
  348. * Should not be <code>null</code>.
  349. */
  350. public synchronized void addText(String data) {
  351. if (data.length() == 0) {
  352. return;
  353. }
  354. characters = (characters == null)
  355. ? new StringBuffer(data) : characters.append(data);
  356. }
  357. /**
  358. * Adds characters from #PCDATA areas to the wrapped element.
  359. *
  360. * @param buf A character array of the text within the element.
  361. * Must not be <code>null</code>.
  362. * @param start The start element in the array.
  363. * @param count The number of characters to read from the array.
  364. *
  365. */
  366. public synchronized void addText(char[] buf, int start, int count) {
  367. if (count == 0) {
  368. return;
  369. }
  370. characters = ((characters == null)
  371. ? new StringBuffer(count) : characters).append(buf, start, count);
  372. }
  373. /**
  374. * Get the text content of this element. Various text chunks are
  375. * concatenated, there is no way (currently) of keeping track of
  376. * multiple fragments.
  377. *
  378. * @return the text content of this element.
  379. * @since Ant 1.6
  380. */
  381. public synchronized StringBuffer getText() {
  382. return (characters == null) ? new StringBuffer(0) : characters;
  383. }
  384. /**
  385. * Set the element tag.
  386. * @param elementTag The tag name generating this element.
  387. */
  388. public synchronized void setElementTag(String elementTag) {
  389. this.elementTag = elementTag;
  390. }
  391. /**
  392. * Returns the tag name of the wrapped element.
  393. *
  394. * @return The tag name of the wrapped element. This is unlikely
  395. * to be <code>null</code>, but may be.
  396. */
  397. public synchronized String getElementTag() {
  398. return elementTag;
  399. }
  400. /**
  401. * Configures the wrapped element and all its children.
  402. * The attributes and text for the wrapped element are configured,
  403. * and then each child is configured and added. Each time the
  404. * wrapper is configured, the attributes and text for it are
  405. * reset.
  406. * <p>
  407. * If the element has an <code>id</code> attribute, a reference
  408. * is added to the project as well.
  409. * </p>
  410. *
  411. * @param p The project containing the wrapped element.
  412. * Must not be <code>null</code>.
  413. *
  414. * @exception BuildException if the configuration fails, for instance due
  415. * to invalid attributes or children, or text being added to
  416. * an element which doesn't accept it.
  417. */
  418. public void maybeConfigure(Project p) throws BuildException {
  419. maybeConfigure(p, true);
  420. }
  421. /**
  422. * Configures the wrapped element. The attributes and text for
  423. * the wrapped element are configured. Each time the wrapper is
  424. * configured, the attributes and text for it are reset.
  425. * <p>
  426. * If the element has an <code>id</code> attribute, a reference
  427. * is added to the project as well.
  428. * </p>
  429. *
  430. * @param p The project containing the wrapped element.
  431. * Must not be <code>null</code>.
  432. *
  433. * @param configureChildren ignored.
  434. *
  435. * @exception BuildException if the configuration fails, for instance due
  436. * to invalid attributes, or text being added to
  437. * an element which doesn't accept it.
  438. */
  439. public synchronized void maybeConfigure(Project p, boolean configureChildren)
  440. throws BuildException {
  441. if (proxyConfigured) {
  442. return;
  443. }
  444. // Configure the object
  445. Object target = (wrappedObject instanceof TypeAdapter)
  446. ? ((TypeAdapter) wrappedObject).getProxy() : wrappedObject;
  447. IntrospectionHelper ih =
  448. IntrospectionHelper.getHelper(p, target.getClass());
  449. ComponentHelper componentHelper = ComponentHelper.getComponentHelper(p);
  450. if (attributeMap != null) {
  451. for (Map.Entry<String, Object> entry : attributeMap.entrySet()) {
  452. String name = entry.getKey();
  453. // skip restricted attributes such as if:set
  454. AttributeComponentInformation attributeComponentInformation = isRestrictedAttribute(name, componentHelper);
  455. if (attributeComponentInformation.isRestricted()) {
  456. continue;
  457. }
  458. Object value = entry.getValue();
  459. // reflect these into the target, defer for
  460. // MacroInstance where properties are expanded for the
  461. // nested sequential
  462. Object attrValue;
  463. if (value instanceof Evaluable<?>) {
  464. attrValue = ((Evaluable<?>) value).eval();
  465. } else {
  466. attrValue = PropertyHelper.getPropertyHelper(p).parseProperties(value.toString());
  467. }
  468. if (target instanceof MacroInstance) {
  469. for (Attribute attr : ((MacroInstance) target).getMacroDef().getAttributes()) {
  470. if (attr.getName().equals(name)) {
  471. if (!attr.isDoubleExpanding()) {
  472. attrValue = value;
  473. }
  474. break;
  475. }
  476. }
  477. }
  478. try {
  479. ih.setAttribute(p, target, name, attrValue);
  480. } catch (UnsupportedAttributeException be) {
  481. // id attribute must be set externally
  482. if ("id".equals(name)) {
  483. // Do nothing
  484. } else if (getElementTag() == null) {
  485. throw be;
  486. } else {
  487. throw new BuildException(
  488. getElementTag() + " doesn't support the \""
  489. + be.getAttribute() + "\" attribute", be);
  490. }
  491. } catch (BuildException be) {
  492. if ("id".equals(name)) {
  493. // Assume that this is an not supported attribute type
  494. // thrown for example by a dynamic attribute task
  495. // Do nothing
  496. } else {
  497. throw be;
  498. }
  499. }
  500. }
  501. }
  502. if (characters != null) {
  503. ProjectHelper.addText(p, wrappedObject, characters.substring(0));
  504. }
  505. if (id != null) {
  506. p.addReference(id, wrappedObject);
  507. }
  508. proxyConfigured = true;
  509. }
  510. /**
  511. * Reconfigure the element, even if it has already been configured.
  512. *
  513. * @param p the project instance for this configuration.
  514. */
  515. public void reconfigure(Project p) {
  516. proxyConfigured = false;
  517. maybeConfigure(p);
  518. }
  519. /**
  520. * Apply presets, attributes and text are set if not currently set.
  521. * Nested elements are prepended.
  522. *
  523. * @param r a <code>RuntimeConfigurable</code> value.
  524. */
  525. public void applyPreSet(RuntimeConfigurable r) {
  526. // Attributes
  527. if (r.attributeMap != null) {
  528. for (String name : r.attributeMap.keySet()) {
  529. if (attributeMap == null || attributeMap.get(name) == null) {
  530. setAttribute(name, (String) r.attributeMap.get(name));
  531. }
  532. }
  533. }
  534. // poly type
  535. polyType = (polyType == null) ? r.polyType : polyType;
  536. // Children (this is a shadow of UnknownElement#children)
  537. if (r.children != null) {
  538. List<RuntimeConfigurable> newChildren = new ArrayList<>();
  539. newChildren.addAll(r.children);
  540. if (children != null) {
  541. newChildren.addAll(children);
  542. }
  543. children = newChildren;
  544. }
  545. // Text
  546. if (r.characters != null) {
  547. if (characters == null
  548. || characters.toString().trim().isEmpty()) {
  549. characters = new StringBuffer(r.characters.toString());
  550. }
  551. }
  552. }
  553. }