1 15 package org.apache.hivemind.impl; 16 17 import org.apache.commons.logging.Log; 18 import org.apache.commons.logging.LogFactory; 19 import org.apache.hivemind.ErrorHandler; 20 import org.apache.hivemind.Location; 21 import org.apache.hivemind.SymbolSource; 22 23 29 public class SymbolExpander 30 { 31 private ErrorHandler _errorHandler; 32 33 private SymbolSource _source; 34 35 public SymbolExpander(ErrorHandler handler, SymbolSource source) 36 { 37 _errorHandler = handler; 38 _source = source; 39 } 40 41 private static final Log LOG = LogFactory.getLog(SymbolExpander.class); 42 43 private static final int STATE_START = 0; 44 45 private static final int STATE_DOLLAR = 1; 46 47 private static final int STATE_COLLECT_SYMBOL_NAME = 2; 48 49 59 public String expandSymbols(String text, Location location) 60 { 61 StringBuffer result = new StringBuffer (text.length()); 62 char[] buffer = text.toCharArray(); 63 int state = STATE_START; 64 int blockStart = 0; 65 int blockLength = 0; 66 int symbolStart = -1; 67 int symbolLength = 0; 68 int i = 0; 69 int braceDepth = 0; 70 boolean anySymbols = false; 71 72 while (i < buffer.length) 73 { 74 char ch = buffer[i]; 75 76 switch (state) 77 { 78 case STATE_START: 79 80 if (ch == '$') 81 { 82 state = STATE_DOLLAR; 83 i++; 84 continue; 85 } 86 87 blockLength++; 88 i++; 89 continue; 90 91 case STATE_DOLLAR: 92 93 if (ch == '{') 94 { 95 state = STATE_COLLECT_SYMBOL_NAME; 96 i++; 97 98 symbolStart = i; 99 symbolLength = 0; 100 braceDepth = 1; 101 102 continue; 103 } 104 105 109 if (ch == '$') 110 { 111 114 anySymbols = true; 115 116 if (blockLength > 0) 117 result.append(buffer, blockStart, blockLength); 118 119 result.append(ch); 120 121 i++; 122 blockStart = i; 123 blockLength = 0; 124 state = STATE_START; 125 126 continue; 127 } 128 129 132 blockLength++; 133 134 state = STATE_START; 135 continue; 136 137 case STATE_COLLECT_SYMBOL_NAME: 138 139 if (ch != '}') 140 { 141 if (ch == '{') 142 braceDepth++; 143 144 i++; 145 symbolLength++; 146 continue; 147 } 148 149 braceDepth--; 150 151 if (braceDepth > 0) 152 { 153 i++; 154 symbolLength++; 155 continue; 156 } 157 158 160 162 if (symbolLength == 0) 163 blockLength += 3; 164 165 168 if (blockLength > 0) 169 result.append(buffer, blockStart, blockLength); 170 171 if (symbolLength > 0) 172 { 173 String variableName = text.substring(symbolStart, symbolStart 174 + symbolLength); 175 176 result.append(expandSymbol(variableName, location)); 177 178 anySymbols = true; 179 } 180 181 i++; 182 blockStart = i; 183 blockLength = 0; 184 185 187 state = STATE_START; 188 189 continue; 190 } 191 192 } 193 194 197 if (!anySymbols) 198 return text; 199 200 203 if (state == STATE_DOLLAR) 204 blockLength++; 205 206 if (state == STATE_COLLECT_SYMBOL_NAME) 207 blockLength += symbolLength + 2; 208 209 if (blockLength > 0) 210 result.append(buffer, blockStart, blockLength); 211 212 return result.toString(); 213 } 214 215 private String expandSymbol(String name, Location location) 216 { 217 String value = _source.valueForSymbol(name); 218 219 if (value != null) 220 return value; 221 222 _errorHandler.error(LOG, ImplMessages.noSuchSymbol(name), location, null); 223 224 return "${" + name + "}"; 225 } 226 227 } | Popular Tags |