entered some unwanted posting protection for indymedia.nl. Extended the localization...
[mir.git] / source / mir / util / ParameterExpander.java
1 package  mir.util;
2
3 import multex.Failure;
4 import multex.Exc;
5
6 import java.util.*;
7
8 public class ParameterExpander {
9
10   final static String DEFAULT_KEY = "(default)";
11   final static String NODE_SEPARATOR = ".";
12
13   public static List splitString(String aString, String aSeparator) {
14     List result= new Vector();
15     int previousPosition = 0;
16     int position;
17     int endOfNamePosition;
18
19     while ((position = aString.indexOf(aSeparator, previousPosition))>=0) {
20       result.add(aString.substring(previousPosition, position));
21       previousPosition = position + aSeparator.length();
22     }
23
24     result.add(aString.substring(previousPosition, aString.length()));
25
26     return result;
27   }
28
29   private static Object findNode(String aKey, Map aMap, List aParts) throws Exception {
30     Iterator i;
31     String location = "";
32     Object node = aMap;
33
34     i = aParts.iterator();
35
36     while(i.hasNext()) {
37       String part = (String) i.next();
38
39       if (!(node instanceof Map)) {
40         throw new Exception( "Can't expand key " + aKey + ": " + location + " is not a map" );
41       }
42
43       node = ((Map) node).get(part);
44
45       if (location.length()>0) {
46         location=location + NODE_SEPARATOR;
47       }
48
49       location = location + part;
50
51       if (node == null) {
52         throw new ParameterExpanderExc( "Can't expand key {1}: {2} does not exist", new Object[]{aKey,location} );
53       }
54     }
55
56     return node;
57   }
58
59   public static Object findValueForKey(Map aMap, String aKey) throws Exception {
60     Object node;
61     List parts = splitString(aKey, NODE_SEPARATOR);
62
63     node = findNode(aKey, aMap, parts);
64
65     return node;
66   }
67
68   public static String findStringForKey(Map aMap, String aKey) throws Exception {
69     Object expandedValue = findValueForKey(aMap, aKey);
70
71     if (!(expandedValue instanceof String))
72       throw new ParameterExpanderExc( "Value of key is not a string but a {1}", new Object[]{expandedValue.getClass().getName()} );
73
74     return (String) expandedValue;
75   }
76
77   public static void setValueForKey(Map aMap, String aKey, Object aValue) throws Exception {
78     List parts = splitString(aKey, NODE_SEPARATOR);
79
80     String key = (String) parts.get(parts.size()-1);
81     parts.remove(parts.size()-1);
82
83     Object node=findNode(aKey, aMap, parts);
84
85     if (node instanceof Map) {
86       ((Map) node).put(key, aValue);
87     }
88     else
89       throw new ParameterExpanderExc( "Can't set key {1}: not inside a Map", new Object[]{aKey} );
90   }
91
92   public static String expandExpression(Map aMap, String anExpression) throws Exception {
93     int previousPosition = 0;
94     int position;
95     int endOfNamePosition;
96     StringBuffer result = new StringBuffer();
97
98     while ((position=anExpression.indexOf("$", previousPosition))>=0) {
99       result.append(anExpression.substring(previousPosition, position));
100
101       if (position>=anExpression.length()-1) {
102         result.append(anExpression.substring(position, anExpression.length()));
103         previousPosition=anExpression.length();
104       }
105       else
106       {
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;
112           }
113           else {
114             throw new ParameterExpanderExc("Missing } in {1}",new Object[]{anExpression});
115           }
116         }
117         else
118         {
119           previousPosition=position+2;
120           result.append(anExpression.charAt(position+1));
121         }
122       }
123     }
124     result.append(anExpression.substring(previousPosition, anExpression.length()));
125
126     return result.toString();
127   }
128
129
130   public static class ParameterExpanderExc extends Exc {
131     public ParameterExpanderExc(String msg, Object[] objects) {
132       super(msg, objects);
133     }
134   }
135 }
136
137