1 6 7 package SOFA.Connector.ECG; 8 9 14 public class VariableExpander { 15 16 protected java.util.HashMap subst; 17 18 19 public VariableExpander() { 20 subst=new java.util.HashMap (); 21 } 22 23 public void addSubst(String var, String value) throws VariableExpanderAmbiguityException { 24 String prev; 25 prev=(String )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 expand(String name) throws VariableExpanderUnresolvedNameException { 36 int i, start, end, len=name.length(); 37 StringBuffer str=new StringBuffer (); 38 String 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 val=(String )subst.get(inStr); 55 str.append(val); 56 } else { 57 String val=(String )subst.get(inStr.substring(end+1)); 58 String 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 extractVariable(String 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 e) { 88 return null; 89 } catch (IndexOutOfBoundsException e) { 90 return null; 91 } 92 } 93 94 } 95 | Popular Tags |