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

9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. @Deprecated
  57. private transient AttributeList attributes;
  58. // The following is set to true if any of the attributes are namespaced
  59. private transient boolean namespacedAttribute = false;
  60. /** Attribute names and values. While the XML spec doesn't require
  61. * preserving the order (AFAIK), some ant tests do rely on the
  62. * exact order.
  63. * The only exception to this order is the treatment of
  64. * refid. A number of datatypes check if refid is set
  65. * when other attributes are set. This check will not
  66. * work if the build script has the other attribute before
  67. * the "refid" attribute, so now (ANT 1.7) the refid
  68. * attribute will be processed first.
  69. */
  70. private LinkedHashMap<String, Object> attributeMap = null;
  71. /** Text appearing within the element. */
  72. private StringBuffer characters = null;
  73. /** Indicates if the wrapped object has been configured */
  74. private boolean proxyConfigured = false;
  75. /** the polymorphic type */
  76. private String polyType = null;
  77. /** the "id" of this Element if it has one */
  78. private String id = null;
  79. /**
  80. * Sole constructor creating a wrapper for the specified object.
  81. *
  82. * @param proxy The element to configure. Must not be <code>null</code>.
  83. * @param elementTag The tag name generating this element.
  84. */
  85. public RuntimeConfigurable(Object proxy, String elementTag) {
  86. setProxy(proxy);
  87. setElementTag(elementTag);
  88. // Most likely an UnknownElement
  89. if (proxy instanceof Task) {
  90. ((Task) proxy).setRuntimeConfigurableWrapper(this);
  91. }
  92. }
  93. /**
  94. * Sets the element to configure.
  95. *
  96. * @param proxy The element to configure. Must not be <code>null</code>.
  97. */
  98. public synchronized void setProxy(Object proxy) {
  99. wrappedObject = proxy;
  100. proxyConfigured = false;
  101. }
  102. private static class EnableAttributeConsumer {
  103. public void add(EnableAttribute b) {
  104. // Ignore
  105. }
  106. }
  107. /**
  108. * contains the attribute component name and boolean restricted set to true when
  109. * the attribute is in one of the name spaces managed by ant (if and unless currently)
  110. * @since Ant 1.9.3
  111. */
  112. private static class AttributeComponentInformation {
  113. String componentName;
  114. boolean restricted;
  115. private AttributeComponentInformation(String componentName, boolean restricted) {
  116. this.componentName = componentName;
  117. this.restricted = restricted;
  118. }
  119. public String getComponentName() {
  120. return componentName;
  121. }
  122. public boolean isRestricted() {
  123. return restricted;
  124. }
  125. }
  126. /**
  127. *
  128. * @param name the name of the attribute.
  129. * @param componentHelper current component helper
  130. * @return AttributeComponentInformation instance
  131. */
  132. private AttributeComponentInformation isRestrictedAttribute(String name, ComponentHelper componentHelper) {
  133. if (!name.contains(":")) {
  134. return new AttributeComponentInformation(null, false);
  135. }
  136. String componentName = attrToComponent(name);
  137. String ns = ProjectHelper.extractUriFromComponentName(componentName);
  138. if (componentHelper.getRestrictedDefinitions(
  139. ProjectHelper.nsToComponentName(ns)) == null) {
  140. return new AttributeComponentInformation(null, false);
  141. }
  142. return new AttributeComponentInformation(componentName, true);
  143. }
  144. /**
  145. * Check if an UE is enabled.
  146. * This looks tru the attributes and checks if there
  147. * are any Ant attributes, and if so, the method calls the
  148. * isEnabled() method on them.
  149. * @param owner the UE that owns this RC.
  150. * @return true if enabled, false if any of the ant attributes return
  151. * false.
  152. * @since 1.9.1
  153. */
  154. @SuppressWarnings("deprecated")
  155. public boolean isEnabled(UnknownElement owner) {
  156. if (!namespacedAttribute) {
  157. return true;
  158. }
  159. ComponentHelper componentHelper = ComponentHelper
  160. .getComponentHelper(owner.getProject());
  161. IntrospectionHelper ih = IntrospectionHelper.getHelper(owner.getProject(),
  162. EnableAttributeConsumer.class);
  163. for (Map.Entry<String, Object> entry : attributeMap.entrySet()) {
  164. AttributeComponentInformation attributeComponentInformation
  165. = isRestrictedAttribute(entry.getKey(), componentHelper);
  166. if (!attributeComponentInformation.isRestricted()) {
  167. continue;
  168. }
  169. String value = (String) entry.getValue();
  170. EnableAttribute enable = null;
  171. try {
  172. enable = (EnableAttribute)
  173. ih.createElement(
  174. owner.getProject(), new EnableAttributeConsumer(),
  175. attributeComponentInformation.getComponentName());
  176. } catch (BuildException ex) {
  177. throw new BuildException(
  178. "Unsupported attribute " + attributeComponentInformation.getComponentName());
  179. }
  180. if (enable == null) {
  181. continue;
  182. }
  183. value = owner.getProject().replaceProperties(value); // FixMe: need to make config
  184. if (!enable.isEnabled(owner, value)) {
  185. return false;
  186. }
  187. }
  188. return true;
  189. }
  190. private String attrToComponent(String a) {
  191. // need to remove the prefix
  192. int p1 = a.lastIndexOf(':');
  193. int p2 = a.lastIndexOf(':', p1 - 1);
  194. return a.substring(0, p2) + a.substring(p1);
  195. }
  196. /**
  197. * Sets the creator of the element to be configured
  198. * used to store the element in the parent.
  199. *
  200. * @param creator the creator object.
  201. */
  202. synchronized void setCreator(IntrospectionHelper.Creator creator) {
  203. }
  204. /**
  205. * Get the object for which this RuntimeConfigurable holds the configuration
  206. * information.
  207. *
  208. * @return the object whose configure is held by this instance.
  209. */
  210. public synchronized Object getProxy() {
  211. return wrappedObject;
  212. }
  213. /**
  214. * Returns the id for this element.
  215. * @return the id.
  216. */
  217. public synchronized String getId() {
  218. return id;
  219. }
  220. /**
  221. * Get the polymorphic type for this element.
  222. * @return the ant component type name, null if not set.
  223. */
  224. public synchronized String getPolyType() {
  225. return polyType;
  226. }
  227. /**
  228. * Set the polymorphic type for this element.
  229. * @param polyType the ant component type name, null if not set.
  230. */
  231. public synchronized void setPolyType(String polyType) {
  232. this.polyType = polyType;
  233. }
  234. /**
  235. * Sets the attributes for the wrapped element.
  236. *
  237. * @param attributes List of attributes defined in the XML for this
  238. * element. May be <code>null</code>.
  239. * @deprecated since 1.6.x.
  240. */
  241. @Deprecated
  242. public synchronized void setAttributes(AttributeList attributes) {
  243. this.attributes = new AttributeListImpl(attributes);
  244. for (int i = 0; i < attributes.getLength(); i++) {
  245. setAttribute(attributes.getName(i), attributes.getValue(i));
  246. }
  247. }
  248. /**
  249. * Set an attribute to a given value.
  250. *
  251. * @param name the name of the attribute.
  252. * @param value the attribute's value.
  253. */
  254. public synchronized void setAttribute(String name, String value) {
  255. if (name.contains(":")) {
  256. namespacedAttribute = true;
  257. }
  258. setAttribute(name, (Object) value);
  259. }
  260. /**
  261. * Set an attribute to a given value.
  262. *
  263. * @param name the name of the attribute.
  264. * @param value the attribute's value.
  265. * @since 1.9
  266. */
  267. public synchronized void setAttribute(String name, Object value) {
  268. if (name.equalsIgnoreCase(ProjectHelper.ANT_TYPE)) {
  269. this.polyType = value == null ? null : value.toString();
  270. } else {
  271. if (attributeMap == null) {
  272. attributeMap = new LinkedHashMap<>();
  273. }
  274. if ("refid".equalsIgnoreCase(name) && !attributeMap.isEmpty()) {
  275. LinkedHashMap<String, Object> newAttributeMap = new LinkedHashMap<>();
  276. newAttributeMap.put(name, value);
  277. newAttributeMap.putAll(attributeMap);
  278. attributeMap = newAttributeMap;
  279. } else {
  280. attributeMap.put(name, value);
  281. }
  282. if ("id".equals(name)) {
  283. this.id = value == null ? null : value.toString();
  284. }
  285. }
  286. }
  287. /**
  288. * Delete an attribute. Not for the faint of heart.
  289. * @param name the name of the attribute to be removed.
  290. */
  291. public synchronized void removeAttribute(String name) {
  292. attributeMap.remove(name);
  293. }
  294. /**
  295. * Return the attribute map.
  296. *
  297. * @return Attribute name to attribute value map.
  298. * @since Ant 1.6
  299. */
  300. public synchronized Hashtable<String, Object> getAttributeMap() {
  301. return (attributeMap == null)
  302. ? EMPTY_HASHTABLE : new Hashtable<>(attributeMap);
  303. }
  304. /**
  305. * Returns the list of attributes for the wrapped element.
  306. *
  307. * @return An AttributeList representing the attributes defined in the
  308. * XML for this element. May be <code>null</code>.
  309. * @deprecated Deprecated since Ant 1.6 in favor of {@link #getAttributeMap}.
  310. */
  311. @Deprecated
  312. public synchronized AttributeList getAttributes() {
  313. return attributes;
  314. }
  315. /**
  316. * Adds a child element to the wrapped element.
  317. *
  318. * @param child The child element wrapper to add to this one.
  319. * Must not be <code>null</code>.
  320. */
  321. public synchronized void addChild(RuntimeConfigurable child) {
  322. children = (children == null) ? new ArrayList<>() : children;
  323. children.add(child);
  324. }
  325. /**
  326. * Returns the child wrapper at the specified position within the list.
  327. *
  328. * @param index The index of the child to return.
  329. *
  330. * @return The child wrapper at position <code>index</code> within the
  331. * list.
  332. */
  333. synchronized RuntimeConfigurable getChild(int index) {
  334. return children.get(index);
  335. }
  336. /**
  337. * Returns an enumeration of all child wrappers.
  338. * @return an enumeration of the child wrappers.
  339. * @since Ant 1.6
  340. */
  341. public synchronized Enumeration<RuntimeConfigurable> getChildren() {
  342. return (children == null) ? Collections.emptyEnumeration()
  343. : Collections.enumeration(children);
  344. }
  345. /**
  346. * Adds characters from #PCDATA areas to the wrapped element.
  347. *
  348. * @param data Text to add to the wrapped element.
  349. * Should not be <code>null</code>.
  350. */
  351. public synchronized void addText(String data) {
  352. if (data.isEmpty()) {
  353. return;
  354. }
  355. characters = (characters == null)
  356. ? new StringBuffer(data) : characters.append(data);
  357. }
  358. /**
  359. * Adds characters from #PCDATA areas to the wrapped element.
  360. *
  361. * @param buf A character array of the text within the element.
  362. * Must not be <code>null</code>.
  363. * @param start The start element in the array.
  364. * @param count The number of characters to read from the array.
  365. *
  366. */
  367. public synchronized void addText(char[] buf, int start, int count) {
  368. if (count == 0) {
  369. return;
  370. }
  371. characters = ((characters == null)
  372. ? new StringBuffer(count) : characters).append(buf, start, count);
  373. }
  374. /**
  375. * Get the text content of this element. Various text chunks are
  376. * concatenated, there is no way (currently) of keeping track of
  377. * multiple fragments.
  378. *
  379. * @return the text content of this element.
  380. * @since Ant 1.6
  381. */
  382. public synchronized StringBuffer getText() {
  383. return (characters == null) ? new StringBuffer(0) : characters;
  384. }
  385. /**
  386. * Set the element tag.
  387. * @param elementTag The tag name generating this element.
  388. */
  389. public synchronized void setElementTag(String elementTag) {
  390. this.elementTag = elementTag;
  391. }
  392. /**
  393. * Returns the tag name of the wrapped element.
  394. *
  395. * @return The tag name of the wrapped element. This is unlikely
  396. * to be <code>null</code>, but may be.
  397. */
  398. public synchronized String getElementTag() {
  399. return elementTag;
  400. }
  401. /**
  402. * Configures the wrapped element and all its children.
  403. * The attributes and text for the wrapped element are configured,
  404. * and then each child is configured and added. Each time the
  405. * wrapper is configured, the attributes and text for it are
  406. * reset.
  407. * <p>
  408. * If the element has an <code>id</code> attribute, a reference
  409. * is added to the project as well.
  410. * </p>
  411. *
  412. * @param p The project containing the wrapped element.
  413. * Must not be <code>null</code>.
  414. *
  415. * @exception BuildException if the configuration fails, for instance due
  416. * to invalid attributes or children, or text being added to
  417. * an element which doesn't accept it.
  418. */
  419. public void maybeConfigure(Project p) throws BuildException {
  420. maybeConfigure(p, true);
  421. }
  422. /**
  423. * Configures the wrapped element. The attributes and text for
  424. * the wrapped element are configured. Each time the wrapper is
  425. * configured, the attributes and text for it are reset.
  426. * <p>
  427. * If the element has an <code>id</code> attribute, a reference
  428. * is added to the project as well.
  429. * </p>
  430. *
  431. * @param p The project containing the wrapped element.
  432. * Must not be <code>null</code>.
  433. *
  434. * @param configureChildren ignored.
  435. *
  436. * @exception BuildException if the configuration fails, for instance due
  437. * to invalid attributes, or text being added to
  438. * an element which doesn't accept it.
  439. */
  440. public synchronized void maybeConfigure(Project p, boolean configureChildren)
  441. throws BuildException {
  442. if (proxyConfigured) {
  443. return;
  444. }
  445. // Configure the object
  446. Object target = (wrappedObject instanceof TypeAdapter)
  447. ? ((TypeAdapter) wrappedObject).getProxy() : wrappedObject;
  448. IntrospectionHelper ih =
  449. IntrospectionHelper.getHelper(p, target.getClass());
  450. ComponentHelper componentHelper = ComponentHelper.getComponentHelper(p);
  451. if (attributeMap != null) {
  452. for (Map.Entry<String, Object> entry : attributeMap.entrySet()) {
  453. String name = entry.getKey();
  454. // skip restricted attributes such as if:set
  455. AttributeComponentInformation attributeComponentInformation = isRestrictedAttribute(name, componentHelper);
  456. if (attributeComponentInformation.isRestricted()) {
  457. continue;
  458. }
  459. Object value = entry.getValue();
  460. // reflect these into the target, defer for
  461. // MacroInstance where properties are expanded for the
  462. // nested sequential
  463. Object attrValue;
  464. if (value instanceof Evaluable<?>) {
  465. attrValue = ((Evaluable<?>) value).eval();
  466. } else {
  467. attrValue = PropertyHelper.getPropertyHelper(p).parseProperties(value.toString());
  468. }
  469. if (target instanceof MacroInstance) {
  470. for (Attribute attr : ((MacroInstance) target).getMacroDef().getAttributes()) {
  471. if (attr.getName().equals(name)) {
  472. if (!attr.isDoubleExpanding()) {
  473. attrValue = value;
  474. }
  475. break;
  476. }
  477. }
  478. }
  479. try {
  480. ih.setAttribute(p, target, name, attrValue);
  481. } catch (UnsupportedAttributeException be) {
  482. // id attribute must be set externally
  483. if (!"id".equals(name)) {
  484. 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. }
  492. } catch (BuildException be) {
  493. if (!"id".equals(name)) {
  494. throw be;
  495. }
  496. // Assume that this is an not supported attribute type
  497. // thrown for example by a dynamic attribute task --
  498. // do nothing
  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<>(r.children);
  539. if (children != null) {
  540. newChildren.addAll(children);
  541. }
  542. children = newChildren;
  543. }
  544. // Text
  545. if (r.characters != null) {
  546. if (characters == null
  547. || characters.toString().trim().isEmpty()) {
  548. characters = new StringBuffer(r.characters.toString());
  549. }
  550. }
  551. }
  552. }