another small exception-change
[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   private 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   public static String findValueForKey(Map aMap, String aKey) throws Exception {
30     Object node;
31     Iterator i;
32     List parts = splitString(aKey, NODE_SEPARATOR);
33     String location = "";
34
35     node = aMap;
36
37     i = parts.iterator();
38
39     while(i.hasNext()) {
40       String part = (String) i.next();
41
42       if (!(node instanceof Map)) {
43         throw new Exception( "Can't expand key " + aKey + ": " + location + " is not a map" );
44       }
45
46       node = ((Map) node).get(part);
47
48       if (location.length()>0) {
49         location=location + NODE_SEPARATOR;
50       }
51
52       location = location + part;
53
54       if (node == null) {
55         throw new ParameterExpanderExc( "Can't expand key {1}: {2} does not exist", new Object[]{aKey,location} );
56       }
57     }
58
59     if (node instanceof Map) {
60       node = ((Map) node).get(DEFAULT_KEY);
61     }
62
63     if (!(node instanceof String))
64       throw new ParameterExpanderExc( "Can't expand key {1}: {2} is not a string but a {3}", new Object[]{aKey,location,node.getClass().getName()} );
65
66     return (String) node;
67   }
68
69   public static String expandExpression(Map aMap, String anExpression) throws Exception {
70     int previousPosition = 0;
71     int position;
72     int endOfNamePosition;
73     StringBuffer result = new StringBuffer();
74
75     while ((position=anExpression.indexOf("$", previousPosition))>=0) {
76       result.append(anExpression.substring(previousPosition, position));
77
78       if (position>=anExpression.length()-1) {
79         result.append(anExpression.substring(position, anExpression.length()));
80         previousPosition=anExpression.length();
81       }
82       else
83       {
84         if (anExpression.charAt(position+1) == '{') {
85           endOfNamePosition=anExpression.indexOf('}', position);
86           if (endOfNamePosition>=0) {
87             result.append(findValueForKey(aMap, anExpression.substring(position+2, endOfNamePosition)));
88             previousPosition=endOfNamePosition+1;
89           }
90           else {
91             throw new ParameterExpanderExc("Missing } in {1}",new Object[]{anExpression});
92           }
93         }
94         else
95         {
96           previousPosition=position+2;
97           result.append(anExpression.charAt(position+1));
98         }
99       }
100     }
101     result.append(anExpression.substring(previousPosition, anExpression.length()));
102
103     return result.toString();
104   }
105
106   public static class ParameterExpanderExc extends Exc {
107     public ParameterExpanderExc(String msg, Object[] objects) {
108       super(msg, objects);
109     }
110   }
111 }
112
113