Lines 18-23
Link Here
|
18 |
package org.apache.tools.ant; |
18 |
package org.apache.tools.ant; |
19 |
|
19 |
|
20 |
import java.util.Hashtable; |
20 |
import java.util.Hashtable; |
|
|
21 |
import java.util.Properties; |
22 |
import java.util.Stack; |
21 |
import java.util.Vector; |
23 |
import java.util.Vector; |
22 |
import java.util.Enumeration; |
24 |
import java.util.Enumeration; |
23 |
|
25 |
|
Lines 411-416
Link Here
|
411 |
properties.put(name, value); |
413 |
properties.put(name, value); |
412 |
} |
414 |
} |
413 |
|
415 |
|
|
|
416 |
/** |
417 |
* Set a new inherited user property, which cannot be overwritten |
418 |
* by set/unset property calls. If a user property with the |
419 |
* same name is already present the value is not overridden. |
420 |
* Also marks this property as a property that has not come |
421 |
* from the command line. |
422 |
* |
423 |
* @param ns The namespace for the property (currently not used). |
424 |
* @param n Name of property |
425 |
* @param v Value to set |
426 |
*/ |
427 |
public synchronized void setNewInheritedProperty(String ns, |
428 |
String n, String v) { |
429 |
if (getUserProperty(null, n) == null) { |
430 |
setInheritedProperty(null, n, v); |
431 |
} else { |
432 |
project.log("Override ignored for " + n, Project.MSG_VERBOSE); |
433 |
} |
434 |
} |
435 |
|
436 |
/** |
437 |
* Adds a set of unexpanded properties. |
438 |
* <p>The properties are resolved, prefixed, expanded and added with |
439 |
* {@link #setNewProperty(String, String, Object)}.</p> |
440 |
* @param props The unexpanded properties. |
441 |
* @param prefix The prefix to add to each property name |
442 |
* after properties resolved. |
443 |
* <code>null</code> if no prefix. |
444 |
*/ |
445 |
public synchronized void addNewProperties(String ns |
446 |
, Properties props, String prefix) { |
447 |
addUnexpandedProperties(ns, props, prefix, false); |
448 |
} |
449 |
|
450 |
/** |
451 |
* Adds a set of unexpanded inherited user properties. |
452 |
* <p>The properties are resolved, prefixed, expanded and added with |
453 |
* {@link #setNewInheritedProperty(String, String, String)}.</p> |
454 |
* @param props The unexpanded inherited user properties. |
455 |
* @param prefix The prefix to add to each property name |
456 |
* after properties resolved. |
457 |
* <code>null</code> if no prefix. |
458 |
*/ |
459 |
public synchronized void addNewInheritedProperties(String ns |
460 |
, Properties props, String prefix) { |
461 |
addUnexpandedProperties(ns, props, prefix, true); |
462 |
} |
463 |
|
464 |
/** |
465 |
* Iterate through a set of properties, |
466 |
* resolve them then assign them |
467 |
* @param props The properties to iterate over. |
468 |
* @param prefix The prefix to add to each name. |
469 |
* @param userProperties Whether to be added as user properties. |
470 |
*/ |
471 |
private void addUnexpandedProperties(String ns |
472 |
, Properties props, String prefix, boolean userProperties) { |
473 |
resolveAllProperties(props); |
474 |
Enumeration e = props.keys(); |
475 |
while (e.hasMoreElements()) { |
476 |
String propertyName = (String) e.nextElement(); |
477 |
String propertyValue = props.getProperty(propertyName); |
478 |
|
479 |
String v = replaceProperties(null, propertyValue, null); |
480 |
|
481 |
if (prefix != null) { |
482 |
propertyName = prefix + propertyName; |
483 |
} |
484 |
|
485 |
if (userProperties) { |
486 |
setNewInheritedProperty(null, propertyName, v); |
487 |
} else { |
488 |
setNewProperty(null, propertyName, v); |
489 |
} |
490 |
} |
491 |
} |
492 |
|
493 |
/** |
494 |
* Resolve properties inside a properties hashtable |
495 |
* @param props Properties object to resolve |
496 |
*/ |
497 |
private void resolveAllProperties(Properties props) throws BuildException { |
498 |
for (Enumeration e = props.keys(); e.hasMoreElements();) { |
499 |
String propertyName = (String) e.nextElement(); |
500 |
Stack referencesSeen = new Stack(); |
501 |
resolve(props, propertyName, referencesSeen); |
502 |
} |
503 |
} |
504 |
|
505 |
/** |
506 |
* Recursively expand the named property using the project's |
507 |
* reference table and the given set of properties - fail if a |
508 |
* circular definition is detected. |
509 |
* |
510 |
* @param props Properties object to resolve |
511 |
* @param name Name of the property to resolve |
512 |
* @param referencesSeen Stack of all property names that have |
513 |
* been tried to expand before coming here. |
514 |
*/ |
515 |
private void resolve(Properties props, String name, Stack referencesSeen) |
516 |
throws BuildException { |
517 |
if (referencesSeen.contains(name)) { |
518 |
throw new BuildException("Property " + name + " was circularly " |
519 |
+ "defined."); |
520 |
} |
521 |
|
522 |
String propertyValue = props.getProperty(name); |
523 |
Vector fragments = new Vector(); |
524 |
Vector propertyRefs = new Vector(); |
525 |
parsePropertyString(propertyValue, fragments, propertyRefs); |
526 |
|
527 |
if (propertyRefs.size() != 0) { |
528 |
referencesSeen.push(name); |
529 |
StringBuffer sb = new StringBuffer(); |
530 |
Enumeration i = fragments.elements(); |
531 |
Enumeration j = propertyRefs.elements(); |
532 |
while (i.hasMoreElements()) { |
533 |
String fragment = (String) i.nextElement(); |
534 |
if (fragment == null) { |
535 |
String propertyName = (String) j.nextElement(); |
536 |
Object o = getProperty(null, propertyName); |
537 |
fragment = (o == null) ? null : o.toString(); |
538 |
if (fragment == null) { |
539 |
if (props.containsKey(propertyName)) { |
540 |
resolve(props, propertyName, referencesSeen); |
541 |
fragment = props.getProperty(propertyName); |
542 |
} else { |
543 |
fragment = "${" + propertyName + "}"; |
544 |
} |
545 |
} |
546 |
} |
547 |
sb.append(fragment); |
548 |
} |
549 |
propertyValue = sb.toString(); |
550 |
props.put(name, propertyValue); |
551 |
referencesSeen.pop(); |
552 |
} |
553 |
} |
554 |
|
414 |
// -------------------- Getting properties -------------------- |
555 |
// -------------------- Getting properties -------------------- |
415 |
|
556 |
|
416 |
/** |
557 |
/** |