- made a producer for startpages per topic
[mir.git] / source / mir / util / ParameterExpander.java
1 package  mir.util;
2
3 import java.util.*;
4
5 public class ParameterExpander {
6
7   final static String DEFAULT_KEY = "(default)";
8   final static String NODE_SEPARATOR = ".";
9
10   private static List splitString(String aString, String aSeparator) {
11     List result= new Vector();
12     int previousPosition = 0;
13     int position;
14     int endOfNamePosition;
15
16     while ((position = aString.indexOf(aSeparator, previousPosition))>=0) {
17       result.add(aString.substring(previousPosition, position));
18       previousPosition = position + aSeparator.length();
19     }
20
21     result.add(aString.substring(previousPosition, aString.length()));
22
23     return result;
24   }
25
26   public static String findValueForKey(Map aMap, String aKey) throws Exception {
27     Object node;
28     Iterator i;
29     List parts = splitString(aKey, NODE_SEPARATOR);
30     String location = "";
31
32     node = aMap;
33
34     i = parts.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 Exception( "Can't expand key " + aKey + ": "+ location + " does not exist" );
53       }
54     }
55
56     if (node instanceof Map) {
57       node = ((Map) node).get(DEFAULT_KEY);
58     }
59
60     if (!(node instanceof String))
61       throw new Exception( "Can't expand key " + aKey + ": "+ location + " is not a string but a " + node.getClass().getName() );
62
63     return (String) node;
64   }
65
66   public static String expandExpression(Map aMap, String anExpression) throws Exception {
67     int previousPosition = 0;
68     int position;
69     int endOfNamePosition;
70     StringBuffer result = new StringBuffer();
71
72     while ((position=anExpression.indexOf("$", previousPosition))>=0) {
73       result.append(anExpression.substring(previousPosition, position));
74
75       if (position>=anExpression.length()-1) {
76         result.append(anExpression.substring(position, anExpression.length()));
77         previousPosition=anExpression.length();
78       }
79       else
80       {
81         if (anExpression.charAt(position+1) == '{') {
82           endOfNamePosition=anExpression.indexOf('}', position);
83           if (endOfNamePosition>=0) {
84             result.append(findValueForKey(aMap, anExpression.substring(position+2, endOfNamePosition)));
85             previousPosition=endOfNamePosition+1;
86           }
87           else {
88               throw new Exception("Missing } in " + anExpression);
89           }
90         }
91         else
92         {
93           previousPosition=position+2;
94           result.append(anExpression.charAt(position+1));
95         }
96       }
97     }
98     result.append(anExpression.substring(previousPosition, anExpression.length()));
99
100     return result.toString();
101   }
102 }
103
104