|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| XMLPropertyCache.java | 91.7% | 95.8% | 100% | 95% |
|
||||||||||||||
| 1 |
package junit.extensions.xml;
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
/**
|
|
| 5 |
* <p>Title: XMLObjectCache</p>
|
|
| 6 |
* <p>Description: Multi level hashmap to contain
|
|
| 7 |
* name value pairs. A parent level can be explicitly
|
|
| 8 |
* accessed by prefixing the name with "../"
|
|
| 9 |
* otherwise the parent level will only be searched
|
|
| 10 |
* if the object does not exist at the current level. </p>
|
|
| 11 |
* <p>Copyright: Copyright (c) 2003</p>
|
|
| 12 |
* <p>Company: jfcunit project</p>
|
|
| 13 |
* @author Kevin Wilson
|
|
| 14 |
* @version 1.0
|
|
| 15 |
*/
|
|
| 16 |
public class XMLPropertyCache extends XMLObjectCache { |
|
| 17 |
/**
|
|
| 18 |
* Empty contructor. Parent is assumed to be null. It can be
|
|
| 19 |
* set at a later time.
|
|
| 20 |
*/
|
|
| 21 | 786 |
public XMLPropertyCache() {
|
| 22 | 786 |
this(null); |
| 23 |
} |
|
| 24 |
|
|
| 25 |
/**
|
|
| 26 |
* Constructor.
|
|
| 27 |
* @param parent Parent object cache to be set.
|
|
| 28 |
*/
|
|
| 29 | 786 |
public XMLPropertyCache(final XMLPropertyCache parent) {
|
| 30 | 786 |
super(parent);
|
| 31 |
} |
|
| 32 |
|
|
| 33 |
/**
|
|
| 34 |
* Get a object from the cache. If the object does not
|
|
| 35 |
* exist in the cache then try the parent cache. If a parent
|
|
| 36 |
* cache does not exist then try the System properties.
|
|
| 37 |
* @param name Name of the value to be retrieved.
|
|
| 38 |
* @return Return the value.
|
|
| 39 |
*/
|
|
| 40 | 6144 |
public Object get(final String name) {
|
| 41 | 6144 |
Object o = super.get(name);
|
| 42 |
|
|
| 43 | 6144 |
if (o == null) { |
| 44 | 6008 |
o = System.getProperty(name); |
| 45 |
} |
|
| 46 |
|
|
| 47 | 6144 |
return o;
|
| 48 |
} |
|
| 49 |
|
|
| 50 |
/**
|
|
| 51 |
* Resolve any parameter names in the string given, and
|
|
| 52 |
* make the appropriate substitutions.
|
|
| 53 |
* @param s String String which may contain the property names.
|
|
| 54 |
* @return String Resuting string which will have the property values
|
|
| 55 |
* substituted.
|
|
| 56 |
*/
|
|
| 57 | 1719 |
public final String resolve(final String s) {
|
| 58 | 1719 |
if (s == null) { |
| 59 | 90 |
return null; |
| 60 |
} |
|
| 61 |
|
|
| 62 | 1629 |
int index = -1;
|
| 63 | 1629 |
String str = new String(s);
|
| 64 |
|
|
| 65 | 1629 |
do {
|
| 66 | 1636 |
index = str.lastIndexOf("${");
|
| 67 |
|
|
| 68 | 1636 |
if (index != -1) {
|
| 69 | 7 |
int lindex = str.indexOf("}", index); |
| 70 |
|
|
| 71 | 7 |
String variableName = str.substring(index + 2, lindex); |
| 72 |
|
|
| 73 | 7 |
if (variableName.length() == 0) {
|
| 74 | 0 |
throw new RuntimeException( |
| 75 |
"Variable name expected in ${} cannot resolve :" + s);
|
|
| 76 |
} |
|
| 77 |
|
|
| 78 | 7 |
Object value = get(variableName); |
| 79 |
|
|
| 80 | 7 |
if (value == null) { |
| 81 | 2 |
System.err.println("Evaluation of \"" + s
|
| 82 |
+ "\" Variable not found assuming empty string for:"
|
|
| 83 |
+ variableName); |
|
| 84 | 2 |
value = "";
|
| 85 |
} |
|
| 86 |
|
|
| 87 | 7 |
String valueString = value.toString(); |
| 88 | 7 |
str = str.substring(0, index) + valueString |
| 89 |
+ str.substring(lindex + 1); |
|
| 90 |
} |
|
| 91 | 1636 |
} while (index != -1);
|
| 92 |
|
|
| 93 | 1629 |
return str;
|
| 94 |
} |
|
| 95 |
} |
|
| 96 |
|
|
||||||||||