1 43 package net.jforum.util.preferences; 44 45 import java.util.HashMap ; 46 import java.util.Map ; 47 48 53 public class VariableExpander 54 { 55 private VariableStore variables; 56 57 private String pre; 58 private String post; 59 60 private Map cache; 61 62 public VariableExpander(VariableStore variables, String pre, String post) 63 { 64 this.variables = variables; 65 this.pre = pre; 66 this.post = post; 67 cache = new HashMap (); 68 } 69 70 public void clearCache() 71 { 72 cache.clear(); 73 } 74 75 public String expandVariables(String source) 76 { 77 String result = (String )this.cache.get(source); 78 79 if (source == null || result != null) { 80 return result; 81 } 82 83 int fIndex = source.indexOf(this.pre); 84 85 if (fIndex == -1) { 86 return source; 87 } 88 89 StringBuffer sb = new StringBuffer (source); 90 91 while (fIndex > -1) { 92 int lIndex = sb.indexOf(this.post); 93 94 int start = fIndex + this.pre.length(); 95 96 if (fIndex == 0) { 97 String varName = sb.substring(start, start + lIndex - this.pre.length()); 98 sb.replace(fIndex, fIndex + lIndex + 1, this.variables.getVariableValue(varName)); 99 } 100 else { 101 String varName = sb.substring(start, lIndex); 102 sb.replace(fIndex, lIndex + 1, this.variables.getVariableValue(varName)); 103 } 104 105 fIndex = sb.indexOf(this.pre); 106 } 107 108 result = sb.toString(); 109 110 this.cache.put(source, result); 111 112 return result; 113 } 114 } 115 | Popular Tags |