1 18 19 package org.apache.tools.ant; 20 21 import java.io.Serializable ; 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.Enumeration ; 25 import java.util.HashMap ; 26 import java.util.Hashtable ; 27 import java.util.List ; 28 import java.util.Locale ; 29 import java.util.Map ; 30 import java.util.Iterator ; 31 32 import org.apache.tools.ant.util.CollectionUtils; 33 import org.xml.sax.AttributeList ; 34 import org.xml.sax.helpers.AttributeListImpl ; 35 36 42 public class RuntimeConfigurable implements Serializable { 43 44 45 private static final Hashtable EMPTY_HASHTABLE = new Hashtable (0); 46 47 48 private String elementTag = null; 49 50 51 private List children = null; 52 53 56 private transient Object wrappedObject = null; 57 58 59 private transient IntrospectionHelper.Creator creator; 60 61 65 private transient AttributeList attributes; 66 67 80 private List attributeNames = null; 81 82 83 private Map attributeMap = null; 84 85 86 private StringBuffer characters = null; 87 88 89 private boolean proxyConfigured = false; 90 91 92 private String polyType = null; 93 94 95 private String id = null; 96 97 103 public RuntimeConfigurable(Object proxy, String elementTag) { 104 setProxy(proxy); 105 setElementTag(elementTag); 106 if (proxy instanceof Task) { 108 ((Task) proxy).setRuntimeConfigurableWrapper(this); 109 } 110 } 111 112 117 public synchronized void setProxy(Object proxy) { 118 wrappedObject = proxy; 119 proxyConfigured = false; 120 } 121 122 128 synchronized void setCreator(IntrospectionHelper.Creator creator) { 129 this.creator = creator; 130 } 131 132 138 public synchronized Object getProxy() { 139 return wrappedObject; 140 } 141 142 146 public synchronized String getId() { 147 return id; 148 } 149 150 154 public synchronized String getPolyType() { 155 return polyType; 156 } 157 158 162 public synchronized void setPolyType(String polyType) { 163 this.polyType = polyType; 164 } 165 166 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 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 210 public synchronized void removeAttribute(String name) { 211 attributeNames.remove(name); 212 attributeMap.remove(name); 213 } 214 215 221 public synchronized Hashtable getAttributeMap() { 222 return (attributeMap == null) 223 ? EMPTY_HASHTABLE : new Hashtable (attributeMap); 224 } 225 226 233 public synchronized AttributeList getAttributes() { 234 return attributes; 235 } 236 237 243 public synchronized void addChild(RuntimeConfigurable child) { 244 children = (children == null) ? new ArrayList () : children; 245 children.add(child); 246 } 247 248 256 synchronized RuntimeConfigurable getChild(int index) { 257 return (RuntimeConfigurable) children.get(index); 258 } 259 260 265 public synchronized Enumeration getChildren() { 266 return (children == null) ? new CollectionUtils.EmptyEnumeration() 267 : Collections.enumeration(children); 268 } 269 270 276 public synchronized void addText(String data) { 277 if (data.length() == 0) { 278 return; 279 } 280 characters = (characters == null) 281 ? new StringBuffer (data) : characters.append(data); 282 } 283 284 293 public synchronized void addText(char[] buf, int start, int count) { 294 if (count == 0) { 295 return; 296 } 297 characters = ((characters == null) 298 ? new StringBuffer (count) : characters).append(buf, start, count); 299 } 300 301 309 public synchronized StringBuffer getText() { 310 return (characters == null) ? new StringBuffer (0) : characters; 311 } 312 313 317 public synchronized void setElementTag(String elementTag) { 318 this.elementTag = elementTag; 319 } 320 321 327 public synchronized String getElementTag() { 328 return elementTag; 329 } 330 331 348 public void maybeConfigure(Project p) throws BuildException { 349 maybeConfigure(p, true); 350 } 351 352 370 public synchronized void maybeConfigure(Project p, boolean configureChildren) 371 throws BuildException { 372 373 if (proxyConfigured) { 374 return; 375 } 376 377 Object target = (wrappedObject instanceof TypeAdapter) 379 ? ((TypeAdapter) wrappedObject).getProxy() : wrappedObject; 380 381 IntrospectionHelper ih = 382 IntrospectionHelper.getHelper(p, target.getClass()); 383 384 if (attributeNames != null) { 385 for (int i = 0; i < attributeNames.size(); i++) { 386 String name = (String ) attributeNames.get(i); 387 String value = (String ) attributeMap.get(name); 388 389 value = p.replaceProperties(value); 391 try { 392 ih.setAttribute(p, target, name, value); 393 } catch (UnsupportedAttributeException be) { 394 if (name.equals("id")) { 396 } else if (getElementTag() == null) { 398 throw be; 399 } else { 400 throw new BuildException( 401 getElementTag() + " doesn't support the \"" 402 + be.getAttribute() + "\" attribute", be); 403 } 404 } catch (BuildException be) { 405 if (name.equals("id")) { 406 } else { 410 throw be; 411 } 412 } 413 } 414 } 415 416 if (characters != null) { 417 ProjectHelper.addText(p, wrappedObject, characters.substring(0)); 418 } 419 420 if (id != null) { 421 p.addReference(id, wrappedObject); 422 } 423 proxyConfigured = true; 424 } 425 426 431 public void reconfigure(Project p) { 432 proxyConfigured = false; 433 maybeConfigure(p); 434 } 435 436 437 443 public void applyPreSet(RuntimeConfigurable r) { 444 if (r.attributeMap != null) { 446 for (Iterator i = r.attributeMap.keySet().iterator(); i.hasNext();) { 447 String name = (String ) i.next(); 448 if (attributeMap == null || attributeMap.get(name) == null) { 449 setAttribute(name, (String ) r.attributeMap.get(name)); 450 } 451 } 452 } 453 455 polyType = (polyType == null) ? r.polyType : polyType; 456 457 if (r.children != null) { 459 List newChildren = new ArrayList (); 460 newChildren.addAll(r.children); 461 if (children != null) { 462 newChildren.addAll(children); 463 } 464 children = newChildren; 465 } 466 467 if (r.characters != null) { 469 if (characters == null 470 || characters.toString().trim().length() == 0) { 471 characters = new StringBuffer (r.characters.toString()); 472 } 473 } 474 } 475 } 476 | Popular Tags |