1 18 19 package org.apache.tools.ant; 20 21 import java.util.Hashtable ; 22 import java.util.Vector ; 23 import java.util.Enumeration ; 24 25 26 38 39 48 public class PropertyHelper { 49 50 private Project project; 51 private PropertyHelper next; 52 53 54 private Hashtable properties = new Hashtable (); 55 56 62 private Hashtable userProperties = new Hashtable (); 63 64 70 private Hashtable inheritedProperties = new Hashtable (); 71 72 75 protected PropertyHelper() { 76 } 77 78 80 81 83 88 public void setProject(Project p) { 89 this.project = p; 90 } 91 92 102 public void setNext(PropertyHelper next) { 103 this.next = next; 104 } 105 106 111 public PropertyHelper getNext() { 112 return next; 113 } 114 115 125 public static synchronized 126 PropertyHelper getPropertyHelper(Project project) { 127 PropertyHelper helper 128 = (PropertyHelper) project.getReference(MagicNames.REFID_PROPERTY_HELPER); 129 if (helper != null) { 130 return helper; 131 } 132 helper = new PropertyHelper(); 133 helper.setProject(project); 134 135 project.addReference(MagicNames.REFID_PROPERTY_HELPER, helper); 136 return helper; 137 } 138 139 141 162 public boolean setPropertyHook(String ns, String name, 163 Object value, 164 boolean inherited, boolean user, 165 boolean isNew) { 166 if (getNext() != null) { 167 boolean subst = getNext().setPropertyHook(ns, name, value, 168 inherited, user, isNew); 169 if (subst) { 171 return true; 172 } 173 } 174 return false; 175 } 176 177 185 public Object getPropertyHook(String ns, String name, boolean user) { 186 if (getNext() != null) { 187 Object o = getNext().getPropertyHook(ns, name, user); 188 if (o != null) { 189 return o; 190 } 191 } 192 if (name.startsWith("toString:")) { 194 name = name.substring("toString:".length()); 195 Object v = project.getReference(name); 196 return (v == null) ? null : v.toString(); 197 } 198 return null; 199 } 200 201 207 226 public void parsePropertyString(String value, Vector fragments, 227 Vector propertyRefs) 228 throws BuildException { 229 parsePropertyStringDefault(value, fragments, propertyRefs); 230 } 231 232 250 public String replaceProperties(String ns, String value, Hashtable keys) 251 throws BuildException { 252 if (value == null || value.indexOf('$') == -1) { 253 return value; 254 } 255 Vector fragments = new Vector (); 256 Vector propertyRefs = new Vector (); 257 parsePropertyString(value, fragments, propertyRefs); 258 259 StringBuffer sb = new StringBuffer (); 260 Enumeration i = fragments.elements(); 261 Enumeration j = propertyRefs.elements(); 262 263 while (i.hasMoreElements()) { 264 String fragment = (String ) i.nextElement(); 265 if (fragment == null) { 266 String propertyName = (String ) j.nextElement(); 267 Object replacement = null; 268 269 if (keys != null) { 272 replacement = keys.get(propertyName); 273 } 274 if (replacement == null) { 275 replacement = getProperty(ns, propertyName); 276 } 277 278 if (replacement == null) { 279 project.log("Property \"" + propertyName 280 + "\" has not been set", Project.MSG_VERBOSE); 281 } 282 fragment = (replacement != null) 283 ? replacement.toString() 284 : "${" + propertyName + "}"; 285 } 286 sb.append(fragment); 287 } 288 return sb.toString(); 289 } 290 291 295 296 305 public synchronized boolean setProperty(String ns, String name, 306 Object value, boolean verbose) { 307 if (null != userProperties.get(name)) { 309 if (verbose) { 310 project.log("Override ignored for user property \"" + name 311 + "\"", Project.MSG_VERBOSE); 312 } 313 return false; 314 } 315 316 boolean done = setPropertyHook(ns, name, value, false, false, false); 317 if (done) { 318 return true; 319 } 320 321 if (null != properties.get(name) && verbose) { 322 project.log("Overriding previous definition of property \"" + name 323 + "\"", Project.MSG_VERBOSE); 324 } 325 326 if (verbose) { 327 project.log("Setting project property: " + name + " -> " 328 + value, Project.MSG_DEBUG); 329 } 330 properties.put(name, value); 331 return true; 332 } 333 334 346 public synchronized void setNewProperty(String ns, String name, 347 Object value) { 348 if (null != properties.get(name)) { 349 project.log("Override ignored for property \"" + name 350 + "\"", Project.MSG_VERBOSE); 351 return; 352 } 353 354 boolean done = setPropertyHook(ns, name, value, false, false, true); 355 if (done) { 356 return; 357 } 358 359 project.log("Setting project property: " + name + " -> " 360 + value, Project.MSG_DEBUG); 361 if (name != null && value != null) { 362 properties.put(name, value); 363 } 364 } 365 366 375 public synchronized void setUserProperty(String ns, String name, 376 Object value) { 377 project.log("Setting ro project property: " + name + " -> " 378 + value, Project.MSG_DEBUG); 379 userProperties.put(name, value); 380 381 boolean done = setPropertyHook(ns, name, value, false, true, false); 382 if (done) { 383 return; 384 } 385 properties.put(name, value); 386 } 387 388 400 public synchronized void setInheritedProperty(String ns, String name, 401 Object value) { 402 inheritedProperties.put(name, value); 403 404 project.log("Setting ro project property: " + name + " -> " 405 + value, Project.MSG_DEBUG); 406 userProperties.put(name, value); 407 408 boolean done = setPropertyHook(ns, name, value, true, false, false); 409 if (done) { 410 return; 411 } 412 properties.put(name, value); 413 } 414 415 417 428 public synchronized Object getProperty(String ns, String name) { 429 if (name == null) { 430 return null; 431 } 432 433 Object o = getPropertyHook(ns, name, false); 434 if (o != null) { 435 return o; 436 } 437 438 return properties.get(name); 439 } 440 450 public synchronized Object getUserProperty(String ns, String name) { 451 if (name == null) { 452 return null; 453 } 454 Object o = getPropertyHook(ns, name, true); 455 if (o != null) { 456 return o; 457 } 458 return userProperties.get(name); 459 } 460 461 462 467 472 public Hashtable getProperties() { 473 return new Hashtable (properties); 474 } 477 478 482 public Hashtable getUserProperties() { 483 return new Hashtable (userProperties); 484 } 485 486 491 protected Hashtable getInternalProperties() { 492 return properties; 493 } 494 495 501 protected Hashtable getInternalUserProperties() { 502 return userProperties; 503 } 504 505 511 protected Hashtable getInternalInheritedProperties() { 512 return inheritedProperties; 513 } 514 515 516 528 public void copyInheritedProperties(Project other) { 529 Enumeration e = inheritedProperties.keys(); 530 while (e.hasMoreElements()) { 531 String arg = e.nextElement().toString(); 532 if (other.getUserProperty(arg) != null) { 533 continue; 534 } 535 Object value = inheritedProperties.get(arg); 536 other.setInheritedProperty(arg, value.toString()); 537 } 538 } 539 540 552 public void copyUserProperties(Project other) { 553 Enumeration e = userProperties.keys(); 554 while (e.hasMoreElements()) { 555 Object arg = e.nextElement(); 556 if (inheritedProperties.containsKey(arg)) { 557 continue; 558 } 559 Object value = userProperties.get(arg); 560 other.setUserProperty(arg.toString(), value.toString()); 561 } 562 } 563 564 569 572 static void parsePropertyStringDefault(String value, Vector fragments, 573 Vector propertyRefs) 574 throws BuildException { 575 int prev = 0; 576 int pos; 577 while ((pos = value.indexOf("$", prev)) >= 0) { 579 580 if (pos > 0) { 585 fragments.addElement(value.substring(prev, pos)); 586 } 587 if (pos == (value.length() - 1)) { 590 fragments.addElement("$"); 591 prev = pos + 1; 592 } else if (value.charAt(pos + 1) != '{') { 593 599 if (value.charAt(pos + 1) == '$') { 600 fragments.addElement("$"); 602 prev = pos + 2; 603 } else { 604 fragments.addElement(value.substring(pos, pos + 2)); 606 prev = pos + 2; 607 } 608 609 } else { 610 int endName = value.indexOf('}', pos); 612 if (endName < 0) { 613 throw new BuildException("Syntax error in property: " 614 + value); 615 } 616 String propertyName = value.substring(pos + 2, endName); 617 fragments.addElement(null); 618 propertyRefs.addElement(propertyName); 619 prev = endName + 1; 620 } 621 } 622 if (prev < value.length()) { 625 fragments.addElement(value.substring(prev)); 626 } 627 } 628 629 } 630 | Popular Tags |