1 58 package org.oddjob.ant; 59 60 import java.io.Serializable ; 61 import java.util.ArrayList ; 62 import java.util.Enumeration ; 63 import java.util.HashMap ; 64 import java.util.Hashtable ; 65 import java.util.List ; 66 import java.util.Locale ; 67 import java.util.Map ; 68 import java.util.Vector ; 69 70 import org.apache.tools.ant.IntrospectionHelper; 71 import org.apache.tools.ant.Project; 72 import org.oddjob.arooa.ArooaException; 73 import org.oddjob.arooa.PropertyHelper; 74 import org.oddjob.arooa.RuntimeConfiguration; 75 import org.oddjob.arooa.reflect.RegistryLookup; 76 77 78 85 public class AntJobRtc 86 implements RuntimeConfiguration, Serializable { 87 88 89 90 private String elementTag = null; 91 92 93 private final Vector children = new Vector (); 94 95 98 private final Object wrappedObject; 99 100 107 private List attributeNames = null; 108 109 110 private Map attributeMap = null; 111 112 113 private StringBuffer characters = null; 114 115 116 private boolean proxyConfigured = false; 117 118 private transient RegistryLookup registry; 119 120 private boolean nullAllowed = true; 121 122 private final Project project; 123 124 131 public AntJobRtc(Project project, Object proxy, String elementTag) { 132 this.project = project; 133 this.wrappedObject = proxy; 134 this.elementTag = elementTag; 135 } 136 137 143 public Object getWrappedObject() { 144 return wrappedObject; 145 } 146 147 public void setNullAllowed(boolean value) { 148 nullAllowed = value; 149 } 150 151 public boolean isNullAllowed() { 152 return nullAllowed; 153 } 154 155 161 public void setAttribute(String name, String value) { 162 if (attributeNames == null) { 163 attributeNames = new ArrayList (); 164 attributeMap = new HashMap (); 165 } 166 attributeNames.add(name); 167 PropertyHelper ph = new PropertyHelper(value); 168 attributeMap.put(name, ph); 169 if (ph.isConstant()) { 171 IntrospectionHelper ih = 172 IntrospectionHelper.getHelper(wrappedObject.getClass()); 173 ih.setAttribute(project, wrappedObject, name.toLowerCase(Locale.US), value); 174 } 175 } 176 177 183 public String getAttribute(String name) { 184 if (attributeMap == null) { 185 return null; 186 } 187 Object value = attributeMap.get(name); 188 if (value == null) { 189 return null; 190 } 191 return ((PropertyHelper)value).getValue(); 192 } 193 194 198 public Hashtable getAttributeMap() { 199 if (attributeMap != null) { 200 return new Hashtable (attributeMap); 201 } else { 202 return new Hashtable (1); 203 } 204 } 205 206 207 213 public void addChild(RuntimeConfiguration child) { 214 children.add(child); 215 } 216 217 225 RuntimeConfiguration getChild(int index) { 226 return (RuntimeConfiguration) children.get(index); 227 } 228 229 233 public Enumeration getChildren() { 234 return children.elements(); 235 } 236 237 243 public void addText(String data) { 244 if (data.length() == 0) { 245 return; 246 } 247 if (characters != null) { 248 characters.append(data); 249 } else { 250 characters = new StringBuffer (data); 251 } 252 } 253 254 263 public void addText(char[] buf, int start, int count) { 264 if (count == 0) { 265 return; 266 } 267 if (characters == null) { 268 characters = new StringBuffer (count); 269 } 270 characters.append(buf, start, count); 271 } 272 273 279 public StringBuffer getText() { 280 if (characters != null) { 281 return characters; 282 } else { 283 return new StringBuffer (0); 284 } 285 } 286 287 293 public String getElementTag() { 294 return elementTag; 295 } 296 297 302 public void configure() 303 throws ArooaException { 304 if (registry == null) { 305 throw new NullPointerException ("ResgistryLookup must be set"); 306 } else { 307 configure(this.registry, nullAllowed); 308 } 309 } 310 311 330 331 public void configure(RegistryLookup registry, boolean nullAllowed) 332 throws ArooaException { 333 334 IntrospectionHelper ih = 335 IntrospectionHelper.getHelper(wrappedObject.getClass()); 336 337 if (attributeNames != null) { 338 for (int i = 0; i < attributeNames.size(); i++) { 339 String name = (String ) attributeNames.get(i); 340 PropertyHelper propHelper = (PropertyHelper) attributeMap.get(name); 341 if (!propHelper.isConstant()) { 343 Object newValue = propHelper.replaceProperties(registry, nullAllowed); 345 if (newValue == null) { 347 newValue = propHelper.getValue(); 348 } 349 ih.setAttribute(project, wrappedObject, name.toLowerCase(Locale.US), newValue.toString()); 350 } 351 } 352 } 353 354 if (characters != null) { 355 ih.addText(project, wrappedObject, characters.substring(0)); 356 } 357 358 Enumeration e = getChildren(); 359 while (e.hasMoreElements()) { 360 RuntimeConfiguration child 361 = (RuntimeConfiguration) e.nextElement(); 362 363 child.configure(registry, nullAllowed); 364 if (!proxyConfigured) { 366 ih.storeElement(project, wrappedObject, child.getWrappedObject(), child.getElementTag()); 367 } 368 } 369 370 proxyConfigured = true; 371 } 372 373 376 public RegistryLookup getRegistryLookup() { 377 return registry; 378 } 379 380 383 public void setRegistryLookup(RegistryLookup registry) { 384 this.registry = registry ; 385 } 386 387 } 388 | Popular Tags |