KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > Connector > ECG > VariableExpander


1 /*
2  * VariableExpander.java
3  *
4  * Created on 6. duben 2002, 9:43
5  */

6
7 package SOFA.Connector.ECG;
8
9 /**
10  *
11  * @author ghort
12  * @version
13  */

14 public class VariableExpander {
15
16     protected java.util.HashMap JavaDoc subst;
17     
18     /** Creates new VariableExpander */
19     public VariableExpander() {
20         subst=new java.util.HashMap JavaDoc();
21     }
22
23     public void addSubst(String JavaDoc var, String JavaDoc value) throws VariableExpanderAmbiguityException {
24         String JavaDoc prev;
25         prev=(String JavaDoc)subst.get(var);
26         if (prev!=null) {
27             if (!prev.equals(value)) {
28                 throw new VariableExpanderAmbiguityException("Duplicate and ambiguious definition of variable '"+var+"'.");
29             }
30         } else {
31             subst.put(var,value);
32         }
33     }
34     
35     public String JavaDoc expand(String JavaDoc name) throws VariableExpanderUnresolvedNameException {
36         int i, start, end, len=name.length();
37         StringBuffer JavaDoc str=new StringBuffer JavaDoc();
38         String JavaDoc inStr;
39
40         for (i=0;i<len;i++) {
41             start=name.indexOf('{',i);
42
43             if (start==-1 || !(start<len-2 && (end=name.indexOf('}',start+2))!=-1)) {
44                 str.append(name.substring(i));
45                 break;
46             } else if (start>0) {
47                 str.append(name.substring(i,start));
48             }
49
50             i=end;
51             
52             inStr=name.substring(start+1,end);
53             if ((end=inStr.lastIndexOf(':'))==-1 || end==inStr.length()-1) {
54                 String JavaDoc val=(String JavaDoc)subst.get(inStr);
55                 str.append(val);
56             } else {
57                 String JavaDoc val=(String JavaDoc)subst.get(inStr.substring(end+1));
58                 String JavaDoc prefix=inStr.substring(0,end);
59                 
60                 end=val.lastIndexOf('.');
61                 if (end!=-1) {
62                     str.append(val.substring(0,end+1));
63                 }
64                 str.append(prefix);
65                 if (end<val.length()-1) {
66                     str.append(val.substring(end+1));
67                 }
68             }
69         }
70         return str.toString();
71     }
72
73     static public String JavaDoc extractVariable(String JavaDoc name) {
74         try {
75             byte[] varName=name.getBytes();
76             
77             if (varName.length>2 && varName[0]=='{' && varName[varName.length-1]=='}') {
78                 int i;
79                 
80                 for (i=1;i<varName.length-2;i++) {
81                     if (varName[i]=='}') {
82                         return null;
83                     }
84                 }
85             }
86             return name.substring(1,name.length()-1);
87         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
88             return null;
89         } catch (IndexOutOfBoundsException JavaDoc e) {
90             return null;
91         }
92     }
93     
94 }
95
Popular Tags