001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant;
020:
021: import java.io.Serializable;
022: import java.util.ArrayList;
023: import java.util.Collections;
024: import java.util.Enumeration;
025: import java.util.HashMap;
026: import java.util.Hashtable;
027: import java.util.List;
028: import java.util.Locale;
029: import java.util.Map;
030: import java.util.Iterator;
031:
032: import org.apache.tools.ant.util.CollectionUtils;
033: import org.xml.sax.AttributeList;
034: import org.xml.sax.helpers.AttributeListImpl;
035:
036: /**
037: * Wrapper class that holds the attributes of an element, its children, and
038: * any text within it. It then takes care of configuring that element at
039: * runtime.
040: *
041: */
042: public class RuntimeConfigurable implements Serializable {
043:
044: /** Empty Hashtable. */
045: private static final Hashtable EMPTY_HASHTABLE = new Hashtable(0);
046:
047: /** Name of the element to configure. */
048: private String elementTag = null;
049:
050: /** List of child element wrappers. */
051: private List/*<RuntimeConfigurable>*/children = null;
052:
053: /** The element to configure. It is only used during
054: * maybeConfigure.
055: */
056: private transient Object wrappedObject = null;
057:
058: /** the creator used to make the wrapped object */
059: private transient IntrospectionHelper.Creator creator;
060:
061: /**
062: * XML attributes for the element.
063: * @deprecated since 1.6.x
064: */
065: private transient AttributeList attributes;
066:
067: /** Attribute names and values. While the XML spec doesn't require
068: * preserving the order ( AFAIK ), some ant tests do rely on the
069: * exact order. The following code is copied from AttributeImpl.
070: * We could also just use SAX2 Attributes and convert to SAX1 ( DOM
071: * attribute Nodes can also be stored in SAX2 Attributes )
072: * XXX under JDK 1.4 you can just use a LinkedHashMap for this purpose -jglick
073: * The only exception to this order is the treatment of
074: * refid. A number of datatypes check if refid is set
075: * when other attributes are set. This check will not
076: * work if the build script has the other attribute before
077: * the "refid" attribute, so now (ANT 1.7) the refid
078: * attribute will be processed first.
079: */
080: private List/*<String>*/attributeNames = null;
081:
082: /** Map of attribute names to values */
083: private Map/*<String,String>*/attributeMap = null;
084:
085: /** Text appearing within the element. */
086: private StringBuffer characters = null;
087:
088: /** Indicates if the wrapped object has been configured */
089: private boolean proxyConfigured = false;
090:
091: /** the polymorphic type */
092: private String polyType = null;
093:
094: /** the "id" of this Element if it has one */
095: private String id = null;
096:
097: /**
098: * Sole constructor creating a wrapper for the specified object.
099: *
100: * @param proxy The element to configure. Must not be <code>null</code>.
101: * @param elementTag The tag name generating this element.
102: */
103: public RuntimeConfigurable(Object proxy, String elementTag) {
104: setProxy(proxy);
105: setElementTag(elementTag);
106: // Most likely an UnknownElement
107: if (proxy instanceof Task) {
108: ((Task) proxy).setRuntimeConfigurableWrapper(this );
109: }
110: }
111:
112: /**
113: * Sets the element to configure.
114: *
115: * @param proxy The element to configure. Must not be <code>null</code>.
116: */
117: public synchronized void setProxy(Object proxy) {
118: wrappedObject = proxy;
119: proxyConfigured = false;
120: }
121:
122: /**
123: * Sets the creator of the element to be configured
124: * used to store the element in the parent.
125: *
126: * @param creator the creator object.
127: */
128: synchronized void setCreator(IntrospectionHelper.Creator creator) {
129: this .creator = creator;
130: }
131:
132: /**
133: * Get the object for which this RuntimeConfigurable holds the configuration
134: * information.
135: *
136: * @return the object whose configure is held by this instance.
137: */
138: public synchronized Object getProxy() {
139: return wrappedObject;
140: }
141:
142: /**
143: * Returns the id for this element.
144: * @return the id.
145: */
146: public synchronized String getId() {
147: return id;
148: }
149:
150: /**
151: * Get the polymorphic type for this element.
152: * @return the ant component type name, null if not set.
153: */
154: public synchronized String getPolyType() {
155: return polyType;
156: }
157:
158: /**
159: * Set the polymorphic type for this element.
160: * @param polyType the ant component type name, null if not set.
161: */
162: public synchronized void setPolyType(String polyType) {
163: this .polyType = polyType;
164: }
165:
166: /**
167: * Sets the attributes for the wrapped element.
168: *
169: * @deprecated since 1.6.x.
170: * @param attributes List of attributes defined in the XML for this
171: * element. May be <code>null</code>.
172: */
173: public synchronized void setAttributes(AttributeList attributes) {
174: this .attributes = new AttributeListImpl(attributes);
175: for (int i = 0; i < attributes.getLength(); i++) {
176: setAttribute(attributes.getName(i), attributes.getValue(i));
177: }
178: }
179:
180: /**
181: * Set an attribute to a given value.
182: *
183: * @param name the name of the attribute.
184: * @param value the attribute's value.
185: */
186: public synchronized void setAttribute(String name, String value) {
187: if (name.equalsIgnoreCase(ProjectHelper.ANT_TYPE)) {
188: this .polyType = value;
189: } else {
190: if (attributeNames == null) {
191: attributeNames = new ArrayList();
192: attributeMap = new HashMap();
193: }
194: if (name.toLowerCase(Locale.US).equals("refid")) {
195: attributeNames.add(0, name);
196: } else {
197: attributeNames.add(name);
198: }
199: attributeMap.put(name, value);
200: if (name.equals("id")) {
201: this .id = value;
202: }
203: }
204: }
205:
206: /**
207: * Delete an attribute. Not for the faint of heart.
208: * @param name the name of the attribute to be removed.
209: */
210: public synchronized void removeAttribute(String name) {
211: attributeNames.remove(name);
212: attributeMap.remove(name);
213: }
214:
215: /**
216: * Return the attribute map.
217: *
218: * @return Attribute name to attribute value map.
219: * @since Ant 1.6
220: */
221: public synchronized Hashtable getAttributeMap() {
222: return (attributeMap == null) ? EMPTY_HASHTABLE
223: : new Hashtable(attributeMap);
224: }
225:
226: /**
227: * Returns the list of attributes for the wrapped element.
228: *
229: * @deprecated Deprecated since Ant 1.6 in favor of {@link #getAttributeMap}.
230: * @return An AttributeList representing the attributes defined in the
231: * XML for this element. May be <code>null</code>.
232: */
233: public synchronized AttributeList getAttributes() {
234: return attributes;
235: }
236:
237: /**
238: * Adds a child element to the wrapped element.
239: *
240: * @param child The child element wrapper to add to this one.
241: * Must not be <code>null</code>.
242: */
243: public synchronized void addChild(RuntimeConfigurable child) {
244: children = (children == null) ? new ArrayList() : children;
245: children.add(child);
246: }
247:
248: /**
249: * Returns the child wrapper at the specified position within the list.
250: *
251: * @param index The index of the child to return.
252: *
253: * @return The child wrapper at position <code>index</code> within the
254: * list.
255: */
256: synchronized RuntimeConfigurable getChild(int index) {
257: return (RuntimeConfigurable) children.get(index);
258: }
259:
260: /**
261: * Returns an enumeration of all child wrappers.
262: * @return an enumeration of the child wrappers.
263: * @since Ant 1.6
264: */
265: public synchronized Enumeration getChildren() {
266: return (children == null) ? new CollectionUtils.EmptyEnumeration()
267: : Collections.enumeration(children);
268: }
269:
270: /**
271: * Adds characters from #PCDATA areas to the wrapped element.
272: *
273: * @param data Text to add to the wrapped element.
274: * Should not be <code>null</code>.
275: */
276: public synchronized void addText(String data) {
277: if (data.length() == 0) {
278: return;
279: }
280: characters = (characters == null) ? new StringBuffer(data)
281: : characters.append(data);
282: }
283:
284: /**
285: * Adds characters from #PCDATA areas to the wrapped element.
286: *
287: * @param buf A character array of the text within the element.
288: * Must not be <code>null</code>.
289: * @param start The start element in the array.
290: * @param count The number of characters to read from the array.
291: *
292: */
293: public synchronized void addText(char[] buf, int start, int count) {
294: if (count == 0) {
295: return;
296: }
297: characters = ((characters == null) ? new StringBuffer(count)
298: : characters).append(buf, start, count);
299: }
300:
301: /**
302: * Get the text content of this element. Various text chunks are
303: * concatenated, there is no way ( currently ) of keeping track of
304: * multiple fragments.
305: *
306: * @return the text content of this element.
307: * @since Ant 1.6
308: */
309: public synchronized StringBuffer getText() {
310: return (characters == null) ? new StringBuffer(0) : characters;
311: }
312:
313: /**
314: * Set the element tag.
315: * @param elementTag The tag name generating this element.
316: */
317: public synchronized void setElementTag(String elementTag) {
318: this .elementTag = elementTag;
319: }
320:
321: /**
322: * Returns the tag name of the wrapped element.
323: *
324: * @return The tag name of the wrapped element. This is unlikely
325: * to be <code>null</code>, but may be.
326: */
327: public synchronized String getElementTag() {
328: return elementTag;
329: }
330:
331: /**
332: * Configures the wrapped element and all its children.
333: * The attributes and text for the wrapped element are configured,
334: * and then each child is configured and added. Each time the
335: * wrapper is configured, the attributes and text for it are
336: * reset.
337: *
338: * If the element has an <code>id</code> attribute, a reference
339: * is added to the project as well.
340: *
341: * @param p The project containing the wrapped element.
342: * Must not be <code>null</code>.
343: *
344: * @exception BuildException if the configuration fails, for instance due
345: * to invalid attributes or children, or text being added to
346: * an element which doesn't accept it.
347: */
348: public void maybeConfigure(Project p) throws BuildException {
349: maybeConfigure(p, true);
350: }
351:
352: /**
353: * Configures the wrapped element. The attributes and text for
354: * the wrapped element are configured. Each time the wrapper is
355: * configured, the attributes and text for it are reset.
356: *
357: * If the element has an <code>id</code> attribute, a reference
358: * is added to the project as well.
359: *
360: * @param p The project containing the wrapped element.
361: * Must not be <code>null</code>.
362: *
363: * @param configureChildren ignored.
364:
365: *
366: * @exception BuildException if the configuration fails, for instance due
367: * to invalid attributes , or text being added to
368: * an element which doesn't accept it.
369: */
370: public synchronized void maybeConfigure(Project p,
371: boolean configureChildren) throws BuildException {
372:
373: if (proxyConfigured) {
374: return;
375: }
376:
377: // Configure the object
378: Object target = (wrappedObject instanceof TypeAdapter) ? ((TypeAdapter) wrappedObject)
379: .getProxy()
380: : wrappedObject;
381:
382: IntrospectionHelper ih = IntrospectionHelper.getHelper(p,
383: target.getClass());
384:
385: if (attributeNames != null) {
386: for (int i = 0; i < attributeNames.size(); i++) {
387: String name = (String) attributeNames.get(i);
388: String value = (String) attributeMap.get(name);
389:
390: // reflect these into the target
391: value = p.replaceProperties(value);
392: try {
393: ih.setAttribute(p, target, name, value);
394: } catch (UnsupportedAttributeException be) {
395: // id attribute must be set externally
396: if (name.equals("id")) {
397: // Do nothing
398: } else if (getElementTag() == null) {
399: throw be;
400: } else {
401: throw new BuildException(getElementTag()
402: + " doesn't support the \""
403: + be.getAttribute() + "\" attribute",
404: be);
405: }
406: } catch (BuildException be) {
407: if (name.equals("id")) {
408: // Assume that this is an not supported attribute type
409: // thrown for example by a dymanic attribute task
410: // Do nothing
411: } else {
412: throw be;
413: }
414: }
415: }
416: }
417:
418: if (characters != null) {
419: ProjectHelper.addText(p, wrappedObject, characters
420: .substring(0));
421: }
422:
423: if (id != null) {
424: p.addReference(id, wrappedObject);
425: }
426: proxyConfigured = true;
427: }
428:
429: /**
430: * Reconfigure the element, even if it has already been configured.
431: *
432: * @param p the project instance for this configuration.
433: */
434: public void reconfigure(Project p) {
435: proxyConfigured = false;
436: maybeConfigure(p);
437: }
438:
439: /**
440: * Apply presets, attributes and text are set if not currently set.
441: * Nested elements are prepended.
442: *
443: * @param r a <code>RuntimeConfigurable</code> value.
444: */
445: public void applyPreSet(RuntimeConfigurable r) {
446: // Attributes
447: if (r.attributeMap != null) {
448: for (Iterator i = r.attributeMap.keySet().iterator(); i
449: .hasNext();) {
450: String name = (String) i.next();
451: if (attributeMap == null
452: || attributeMap.get(name) == null) {
453: setAttribute(name, (String) r.attributeMap
454: .get(name));
455: }
456: }
457: }
458: // poly type
459:
460: polyType = (polyType == null) ? r.polyType : polyType;
461:
462: // Children (this is a shadow of UnknownElement#children)
463: if (r.children != null) {
464: List newChildren = new ArrayList();
465: newChildren.addAll(r.children);
466: if (children != null) {
467: newChildren.addAll(children);
468: }
469: children = newChildren;
470: }
471:
472: // Text
473: if (r.characters != null) {
474: if (characters == null
475: || characters.toString().trim().length() == 0) {
476: characters = new StringBuffer(r.characters.toString());
477: }
478: }
479: }
480: }
|