KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > xml > StringInterpolator


1 package jfun.yan.xml;
2
3 import java.util.ArrayList JavaDoc;
4
5 import jfun.util.dict.Dict;
6
7
8 class StringInterpolator {
9   /**
10    * Interpolate a string that may contain "${var}" pattern.
11    * @param src the source string.
12    * @param ctxt the context that contains variable definitions.
13    * @param loc the location.
14    * @return the string if no variable is found or all variables are unkown values;
15    * A Stmt object is returned if any variable is not a known value.
16    */

17   static Object JavaDoc interpolate(final String JavaDoc src, jfun.util.dict.Map ctxt, final Location loc){
18     if(src==null) return null;
19     final ArrayList JavaDoc tokens = new ArrayList JavaDoc();
20     final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
21     final int sz = src.length();
22     for(int i=0; i<sz; i++){
23       final char c = src.charAt(i);
24       if(c == '$'){
25         //could be interpolation
26
final char c1 = src.charAt(++i);
27         if(c1=='{'){
28           //interpolation.
29
final int start = ++i;
30           for(;;i++){
31             if(i>=sz){
32               throw new ConfigurationException(
33                   "invalid string interpolation syntax, '}' expected", loc);
34             }
35             final char c2 = src.charAt(i);
36             if(c2=='}'){
37               final int end = i;
38               final String JavaDoc var = src.substring(start, end);
39               final Object JavaDoc val = ctxt.get(var);
40               if(val==null){
41                 throw new ConfigurationException("unrecognized variable: "+var, loc);
42               }
43               if(val instanceof Stmt){
44                 tokens.add(buf.toString());
45                 buf.setLength(0);
46                 tokens.add(val);
47               }
48               else{
49                 buf.append(val);
50               }
51               break;
52             }
53           }
54         }
55
56         else{
57           buf.append(c);
58           if(c1!='$'){
59             buf.append(c1);
60           }
61         }
62       }
63       else{
64         buf.append(c);
65       }
66     }
67     if(buf.length()>0){
68       tokens.add(buf.toString());
69       buf.setLength(0);
70     }
71     return returnStmt(src, tokens, loc);
72   }
73
74   private static Object JavaDoc returnStmt(final String JavaDoc src, final ArrayList JavaDoc tokens, final Location loc) {
75     final int token_count = tokens.size();
76     if(token_count==0)
77       return "";
78     else if(token_count==1){
79       final Object JavaDoc tok = tokens.get(0);
80       return tok;
81     }
82     else{
83       final Object JavaDoc[] toks = tokens.toArray();
84       return new Stmt(){
85         public Class JavaDoc getType() {
86           return String JavaDoc.class;
87         }
88         public Object JavaDoc run(Dict frame, Runtime JavaDoc runtime) {
89           final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
90           for(int i=0; i<toks.length; i++){
91             final Object JavaDoc tok = toks[i];
92             if(tok instanceof Stmt){
93               buf.append(((Stmt)tok).run(frame, runtime));
94             }
95             else{
96               buf.append(tok);
97             }
98           }
99           return buf.toString();
100         }
101         public Location getLocation() {
102           return loc;
103         }
104         public String JavaDoc toString(){
105           return src;
106         }
107       };
108     }
109   }
110 }
111
Popular Tags