8 public class ParameterExpander {
10 final static String DEFAULT_KEY = "(default)";
11 final static String NODE_SEPARATOR = ".";
13 public static List splitString(String aString, String aSeparator) {
14 List result= new Vector();
15 int previousPosition = 0;
17 int endOfNamePosition;
19 while ((position = aString.indexOf(aSeparator, previousPosition))>=0) {
20 result.add(aString.substring(previousPosition, position));
21 previousPosition = position + aSeparator.length();
24 result.add(aString.substring(previousPosition, aString.length()));
29 private static Object findNode(String aKey, Map aMap, List aParts) throws Exception {
34 i = aParts.iterator();
37 String part = (String) i.next();
39 if (!(node instanceof Map)) {
40 throw new Exception( "Can't expand key " + aKey + ": " + location + " is not a map" );
43 node = ((Map) node).get(part);
45 if (location.length()>0) {
46 location=location + NODE_SEPARATOR;
49 location = location + part;
52 throw new ParameterExpanderExc( "Can't expand key {1}: {2} does not exist", new Object[]{aKey,location} );
59 public static Object findValueForKey(Map aMap, String aKey) throws Exception {
61 List parts = splitString(aKey, NODE_SEPARATOR);
63 node = findNode(aKey, aMap, parts);
68 public static String findStringForKey(Map aMap, String aKey) throws Exception {
69 Object expandedValue = findValueForKey(aMap, aKey);
71 if (!(expandedValue instanceof String))
72 throw new ParameterExpanderExc( "Value of key is not a string but a {1}", new Object[]{expandedValue.getClass().getName()} );
74 return (String) expandedValue;
77 public static void setValueForKey(Map aMap, String aKey, Object aValue) throws Exception {
78 List parts = splitString(aKey, NODE_SEPARATOR);
80 String key = (String) parts.get(parts.size()-1);
81 parts.remove(parts.size()-1);
83 Object node=findNode(aKey, aMap, parts);
85 if (node instanceof Map) {
86 ((Map) node).put(key, aValue);
89 throw new ParameterExpanderExc( "Can't set key {1}: not inside a Map", new Object[]{aKey} );
92 public static String expandExpression(Map aMap, String anExpression) throws Exception {
93 int previousPosition = 0;
95 int endOfNamePosition;
96 StringBuffer result = new StringBuffer();
98 while ((position=anExpression.indexOf("$", previousPosition))>=0) {
99 result.append(anExpression.substring(previousPosition, position));
101 if (position>=anExpression.length()-1) {
102 result.append(anExpression.substring(position, anExpression.length()));
103 previousPosition=anExpression.length();
107 if (anExpression.charAt(position+1) == '{') {
108 endOfNamePosition=anExpression.indexOf('}', position);
109 if (endOfNamePosition>=0) {
110 result.append(findStringForKey(aMap, anExpression.substring(position+2, endOfNamePosition)));
111 previousPosition=endOfNamePosition+1;
114 throw new ParameterExpanderExc("Missing } in {1}",new Object[]{anExpression});
119 previousPosition=position+2;
120 result.append(anExpression.charAt(position+1));
124 result.append(anExpression.substring(previousPosition, anExpression.length()));
126 return result.toString();
130 public static class ParameterExpanderExc extends Exc {
131 public ParameterExpanderExc(String msg, Object[] objects) {