source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/RuntimeConfigurable.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 15.4 KB
Line 
1/*
2 * Copyright 2000-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant;
19
20import java.io.Serializable;
21import java.util.ArrayList;
22import java.util.Collections;
23import java.util.Enumeration;
24import java.util.HashMap;
25import java.util.Hashtable;
26import java.util.List;
27import java.util.Locale;
28import java.util.Map;
29import java.util.Iterator;
30
31import org.apache.tools.ant.util.CollectionUtils;
32import org.xml.sax.AttributeList;
33import org.xml.sax.helpers.AttributeListImpl;
34
35/**
36 * Wrapper class that holds the attributes of an element, its children, and
37 * any text within it. It then takes care of configuring that element at
38 * runtime.
39 *
40 */
41public class RuntimeConfigurable implements Serializable {
42
43 /** Polymorphic attribute (May be XML NS attribute later) */
44 private static final String ANT_TYPE = "ant-type";
45
46 /** Name of the element to configure. */
47 private String elementTag = null;
48
49 /** List of child element wrappers. */
50 private List/*<RuntimeConfigurable>*/ children = null;
51
52 /** The element to configure. It is only used during
53 * maybeConfigure.
54 */
55 private transient Object wrappedObject = null;
56
57 /** the creator used to make the wrapped object */
58 private transient IntrospectionHelper.Creator creator;
59
60 /**
61 * @deprecated
62 * XML attributes for the element.
63 */
64 private transient AttributeList attributes;
65
66 /** Attribute names and values. While the XML spec doesn't require
67 * preserving the order ( AFAIK ), some ant tests do rely on the
68 * exact order. The following code is copied from AttributeImpl.
69 * We could also just use SAX2 Attributes and convert to SAX1 ( DOM
70 * attribute Nodes can also be stored in SAX2 Attributes )
71 * XXX under JDK 1.4 you can just use a LinkedHashMap for this purpose -jglick
72 */
73 private List/*<String>*/ attributeNames = null;
74
75 /** Map of attribute names to values */
76 private Map/*<String,String>*/ attributeMap = null;
77
78 /** Text appearing within the element. */
79 private StringBuffer characters = null;
80
81 /** Indicates if the wrapped object has been configured */
82 private boolean proxyConfigured = false;
83
84 /** the polymorphic type */
85 private String polyType = null;
86
87 /**
88 * Sole constructor creating a wrapper for the specified object.
89 *
90 * @param proxy The element to configure. Must not be <code>null</code>.
91 * @param elementTag The tag name generating this element.
92 * Should not be <code>null</code>.
93 */
94 public RuntimeConfigurable(Object proxy, String elementTag) {
95 wrappedObject = proxy;
96 this.elementTag = elementTag;
97 proxyConfigured = false;
98 // Most likely an UnknownElement
99 if (proxy instanceof Task) {
100 ((Task) proxy).setRuntimeConfigurableWrapper(this);
101 }
102 }
103
104 /**
105 * Sets the element to configure.
106 *
107 * @param proxy The element to configure. Must not be <code>null</code>.
108 */
109 public void setProxy(Object proxy) {
110 wrappedObject = proxy;
111 proxyConfigured = false;
112 }
113
114 /**
115 * Sets the creator of the element to be configured
116 * used to store the element in the parent;
117 *
118 * @param creator the creator object
119 */
120 void setCreator(IntrospectionHelper.Creator creator) {
121 this.creator = creator;
122 }
123
124 /**
125 * Get the object for which this RuntimeConfigurable holds the configuration
126 * information
127 *
128 * @return the object whose configure is held by this instance.
129 */
130 public Object getProxy() {
131 return wrappedObject;
132 }
133
134 /**
135 * get the polymorphic type for this element
136 * @return the ant component type name, null if not set
137 */
138 public String getPolyType() {
139 return polyType;
140 }
141
142 /**
143 * set the polymorphic type for this element
144 * @param polyType the ant component type name, null if not set
145 */
146 public void setPolyType(String polyType) {
147 this.polyType = polyType;
148 }
149
150 /**
151 * Sets the attributes for the wrapped element.
152 *
153 * @deprecated
154 * @param attributes List of attributes defined in the XML for this
155 * element. May be <code>null</code>.
156 */
157 public void setAttributes(AttributeList attributes) {
158 this.attributes = new AttributeListImpl(attributes);
159 for (int i = 0; i < attributes.getLength(); i++) {
160 setAttribute(attributes.getName(i), attributes.getValue(i));
161 }
162 }
163
164 /**
165 * Set an attribute to a given value
166 *
167 * @param name the name of the attribute.
168 * @param value the attribute's value.
169 */
170 public void setAttribute(String name, String value) {
171 if (name.equalsIgnoreCase(ANT_TYPE)) {
172 this.polyType = value;
173 } else {
174 if (attributeNames == null) {
175 attributeNames = new ArrayList();
176 attributeMap = new HashMap();
177 }
178 attributeNames.add(name);
179 attributeMap.put(name, value);
180 }
181 }
182
183 /** Return the attribute map.
184 *
185 * @return Attribute name to attribute value map
186 * @since Ant 1.6
187 */
188 public Hashtable getAttributeMap() {
189 if (attributeMap != null) {
190 return new Hashtable(attributeMap);
191 } else {
192 return new Hashtable(1);
193 }
194 }
195
196 /**
197 * Returns the list of attributes for the wrapped element.
198 *
199 * @deprecated Deprecated since Ant 1.6 in favor of {@link #getAttributeMap}.
200 * @return An AttributeList representing the attributes defined in the
201 * XML for this element. May be <code>null</code>.
202 */
203 public AttributeList getAttributes() {
204 return attributes;
205 }
206
207 /**
208 * Adds a child element to the wrapped element.
209 *
210 * @param child The child element wrapper to add to this one.
211 * Must not be <code>null</code>.
212 */
213 public void addChild(RuntimeConfigurable child) {
214 if (children == null) {
215 children = new ArrayList();
216 }
217 children.add(child);
218 }
219
220 /**
221 * Returns the child wrapper at the specified position within the list.
222 *
223 * @param index The index of the child to return.
224 *
225 * @return The child wrapper at position <code>index</code> within the
226 * list.
227 */
228 RuntimeConfigurable getChild(int index) {
229 return (RuntimeConfigurable) children.get(index);
230 }
231
232 /**
233 * Returns an enumeration of all child wrappers.
234 * @return an enumeration of the child wrappers.
235 * @since Ant 1.6
236 */
237 public Enumeration getChildren() {
238 if (children != null) {
239 return Collections.enumeration(children);
240 } else {
241 return new CollectionUtils.EmptyEnumeration();
242 }
243 }
244
245 /**
246 * Adds characters from #PCDATA areas to the wrapped element.
247 *
248 * @param data Text to add to the wrapped element.
249 * Should not be <code>null</code>.
250 */
251 public void addText(String data) {
252 if (data.length() == 0) {
253 return;
254 }
255 if (characters != null) {
256 characters.append(data);
257 } else {
258 characters = new StringBuffer(data);
259 }
260 }
261
262 /**
263 * Adds characters from #PCDATA areas to the wrapped element.
264 *
265 * @param buf A character array of the text within the element.
266 * Must not be <code>null</code>.
267 * @param start The start element in the array.
268 * @param count The number of characters to read from the array.
269 *
270 */
271 public void addText(char[] buf, int start, int count) {
272 if (count == 0) {
273 return;
274 }
275 if (characters == null) {
276 characters = new StringBuffer(count);
277 }
278 characters.append(buf, start, count);
279 }
280
281 /** Get the text content of this element. Various text chunks are
282 * concatenated, there is no way ( currently ) of keeping track of
283 * multiple fragments.
284 *
285 * @return the text content of this element.
286 * @since Ant 1.6
287 */
288 public StringBuffer getText() {
289 if (characters != null) {
290 return characters;
291 } else {
292 return new StringBuffer(0);
293 }
294 }
295
296 /**
297 * Returns the tag name of the wrapped element.
298 *
299 * @return The tag name of the wrapped element. This is unlikely
300 * to be <code>null</code>, but may be.
301 */
302 public String getElementTag() {
303 return elementTag;
304 }
305
306 /**
307 * Configures the wrapped element and all its children.
308 * The attributes and text for the wrapped element are configured,
309 * and then each child is configured and added. Each time the
310 * wrapper is configured, the attributes and text for it are
311 * reset.
312 *
313 * If the element has an <code>id</code> attribute, a reference
314 * is added to the project as well.
315 *
316 * @param p The project containing the wrapped element.
317 * Must not be <code>null</code>.
318 *
319 * @exception BuildException if the configuration fails, for instance due
320 * to invalid attributes or children, or text being added to
321 * an element which doesn't accept it.
322 */
323 public void maybeConfigure(Project p) throws BuildException {
324 maybeConfigure(p, true);
325 }
326
327 /**
328 * Configures the wrapped element. The attributes and text for
329 * the wrapped element are configured. Each time the wrapper is
330 * configured, the attributes and text for it are reset.
331 *
332 * If the element has an <code>id</code> attribute, a reference
333 * is added to the project as well.
334 *
335 * @param p The project containing the wrapped element.
336 * Must not be <code>null</code>.
337 *
338 * @param configureChildren Whether to configure child elements as
339 * well. if true, child elements will be configured after the
340 * wrapped element.
341 *
342 * @exception BuildException if the configuration fails, for instance due
343 * to invalid attributes or children, or text being added to
344 * an element which doesn't accept it.
345 */
346 public void maybeConfigure(Project p, boolean configureChildren)
347 throws BuildException {
348 String id = null;
349
350 if (proxyConfigured) {
351 return;
352 }
353
354 // Configure the object
355 Object target = (wrappedObject instanceof TypeAdapter)
356 ? ((TypeAdapter) wrappedObject).getProxy() : wrappedObject;
357
358 //PropertyHelper ph=PropertyHelper.getPropertyHelper(p);
359 IntrospectionHelper ih =
360 IntrospectionHelper.getHelper(p, target.getClass());
361
362 if (attributeNames != null) {
363 for (int i = 0; i < attributeNames.size(); i++) {
364 String name = (String) attributeNames.get(i);
365 String value = (String) attributeMap.get(name);
366
367 // reflect these into the target
368 value = p.replaceProperties(value);
369 try {
370 ih.setAttribute(p, target,
371 name.toLowerCase(Locale.US), value);
372 } catch (BuildException be) {
373 // id attribute must be set externally
374 if (!name.equals("id")) {
375 throw be;
376 }
377 }
378 }
379 id = (String) attributeMap.get("id");
380 }
381
382 if (characters != null) {
383 ProjectHelper.addText(p, wrappedObject, characters.substring(0));
384 }
385
386 Enumeration e = getChildren();
387 while (e.hasMoreElements()) {
388 RuntimeConfigurable child
389 = (RuntimeConfigurable) e.nextElement();
390 if (child.wrappedObject instanceof Task) {
391 Task childTask = (Task) child.wrappedObject;
392 childTask.setRuntimeConfigurableWrapper(child);
393 }
394
395 if ((child.creator != null) && configureChildren) {
396 child.maybeConfigure(p);
397 child.creator.store();
398 continue;
399 }
400 /*
401 * backwards compatibility - element names of nested
402 * elements have been all lower-case in Ant, except for
403 * tasks in TaskContainers.
404 *
405 * For TaskContainers, we simply skip configuration here.
406 */
407 String tag = child.getElementTag().toLowerCase(Locale.US);
408 if (configureChildren
409 && ih.supportsNestedElement(tag)) {
410 child.maybeConfigure(p);
411 ProjectHelper.storeChild(p, target, child.wrappedObject,
412 tag);
413 }
414 }
415
416 if (id != null) {
417 p.addReference(id, wrappedObject);
418 }
419 proxyConfigured = true;
420 }
421
422 /**
423 * Reconfigure the element, even if it has already been configured.
424 *
425 * @param p the project instance for this configuration.
426 */
427 public void reconfigure(Project p) {
428 proxyConfigured = false;
429 maybeConfigure(p);
430 }
431
432
433 /**
434 * Apply presets, attributes and text are set if not currently set.
435 * nested elements are prepended.
436 *
437 * @param r a <code>RuntimeConfigurable</code> value
438 */
439 public void applyPreSet(RuntimeConfigurable r) {
440 // Attributes
441 if (r.attributeMap != null) {
442 for (Iterator i = r.attributeMap.keySet().iterator(); i.hasNext();) {
443 String name = (String) i.next();
444 if (attributeMap == null || attributeMap.get(name) == null) {
445 setAttribute(name, (String) r.attributeMap.get(name));
446 }
447 }
448 }
449 // poly type
450 if (r.polyType != null && polyType == null) {
451 polyType = r.polyType;
452 }
453
454 // Children (this is a shadow of unknownElement#children)
455 if (r.children != null) {
456 List newChildren = new ArrayList();
457 newChildren.addAll(r.children);
458 if (children != null) {
459 newChildren.addAll(children);
460 }
461 children = newChildren;
462 }
463
464 // Text
465 if (r.characters != null) {
466 if (characters == null
467 || characters.toString().trim().length() == 0) {
468 characters =
469 new StringBuffer(r.characters.toString());
470 }
471 }
472 }
473}
Note: See TracBrowser for help on using the repository browser.