1 18 package org.apache.tools.ant.filters; 19 20 import java.io.FileInputStream ; 21 import java.io.IOException ; 22 import java.io.Reader ; 23 import java.util.Enumeration ; 24 import java.util.Hashtable ; 25 import java.util.Properties ; 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.types.Parameter; 28 import org.apache.tools.ant.util.FileUtils; 29 30 48 public final class ReplaceTokens 49 extends BaseParamFilterReader 50 implements ChainableReader { 51 52 private static final char DEFAULT_BEGIN_TOKEN = '@'; 53 54 55 private static final char DEFAULT_END_TOKEN = '@'; 56 57 58 private String queuedData = null; 59 60 61 private String replaceData = null; 62 63 64 private int replaceIndex = -1; 65 66 67 private int queueIndex = -1; 68 69 70 private Hashtable hash = new Hashtable (); 71 72 73 private char beginToken = DEFAULT_BEGIN_TOKEN; 74 75 76 private char endToken = DEFAULT_END_TOKEN; 77 78 83 public ReplaceTokens() { 84 super(); 85 } 86 87 93 public ReplaceTokens(final Reader in) { 94 super(in); 95 } 96 97 private int getNextChar() throws IOException { 98 if (queueIndex != -1) { 99 final int ch = queuedData.charAt(queueIndex++); 100 if (queueIndex >= queuedData.length()) { 101 queueIndex = -1; 102 } 103 return ch; 104 } 105 106 return in.read(); 107 } 108 109 119 public int read() throws IOException { 120 if (!getInitialized()) { 121 initialize(); 122 setInitialized(true); 123 } 124 125 if (replaceIndex != -1) { 126 final int ch = replaceData.charAt(replaceIndex++); 127 if (replaceIndex >= replaceData.length()) { 128 replaceIndex = -1; 129 } 130 return ch; 131 } 132 133 int ch = getNextChar(); 134 135 if (ch == beginToken) { 136 final StringBuffer key = new StringBuffer (""); 137 do { 138 ch = getNextChar(); 139 if (ch != -1) { 140 key.append((char) ch); 141 } else { 142 break; 143 } 144 } while (ch != endToken); 145 146 if (ch == -1) { 147 if (queuedData == null || queueIndex == -1) { 148 queuedData = key.toString(); 149 } else { 150 queuedData 151 = key.toString() + queuedData.substring(queueIndex); 152 } 153 queueIndex = 0; 154 return beginToken; 155 } else { 156 key.setLength(key.length() - 1); 157 158 final String replaceWith = (String ) hash.get(key.toString()); 159 if (replaceWith != null) { 160 if (replaceWith.length() > 0) { 161 replaceData = replaceWith; 162 replaceIndex = 0; 163 } 164 return read(); 165 } else { 166 String newData = key.toString() + endToken; 167 if (queuedData == null || queueIndex == -1) { 168 queuedData = newData; 169 } else { 170 queuedData = newData + queuedData.substring(queueIndex); 171 } 172 queueIndex = 0; 173 return beginToken; 174 } 175 } 176 } 177 return ch; 178 } 179 180 185 public void setBeginToken(final char beginToken) { 186 this.beginToken = beginToken; 187 } 188 189 194 private char getBeginToken() { 195 return beginToken; 196 } 197 198 203 public void setEndToken(final char endToken) { 204 this.endToken = endToken; 205 } 206 207 212 private char getEndToken() { 213 return endToken; 214 } 215 216 222 public void addConfiguredToken(final Token token) { 223 hash.put(token.getKey(), token.getValue()); 224 } 225 226 231 private Properties getPropertiesFromFile (String fileName) { 232 FileInputStream in = null; 233 Properties props = new Properties (); 234 try { 235 in = new FileInputStream (fileName); 236 props.load(in); 237 } catch (IOException ioe) { 238 ioe.printStackTrace(); 239 } finally { 240 FileUtils.close(in); 241 } 242 243 return props; 244 } 245 246 252 private void setTokens(final Hashtable hash) { 253 this.hash = hash; 254 } 255 256 262 private Hashtable getTokens() { 263 return hash; 264 } 265 266 276 public Reader chain(final Reader rdr) { 277 ReplaceTokens newFilter = new ReplaceTokens(rdr); 278 newFilter.setBeginToken(getBeginToken()); 279 newFilter.setEndToken(getEndToken()); 280 newFilter.setTokens(getTokens()); 281 newFilter.setInitialized(true); 282 return newFilter; 283 } 284 285 288 private void initialize() { 289 Parameter[] params = getParameters(); 290 if (params != null) { 291 for (int i = 0; i < params.length; i++) { 292 if (params[i] != null) { 293 final String type = params[i].getType(); 294 if ("tokenchar".equals(type)) { 295 final String name = params[i].getName(); 296 String value = params[i].getValue(); 297 if ("begintoken".equals(name)) { 298 if (value.length() == 0) { 299 throw new BuildException("Begin token cannot " 300 + "be empty"); 301 } 302 beginToken = params[i].getValue().charAt(0); 303 } else if ("endtoken".equals(name)) { 304 if (value.length() == 0) { 305 throw new BuildException("End token cannot " 306 + "be empty"); 307 } 308 endToken = params[i].getValue().charAt(0); 309 } 310 } else if ("token".equals(type)) { 311 final String name = params[i].getName(); 312 final String value = params[i].getValue(); 313 hash.put(name, value); 314 } else if ("propertiesfile".equals(type)) { 315 Properties props = getPropertiesFromFile(params[i].getValue()); 316 for (Enumeration e = props.keys(); e.hasMoreElements();) { 317 String key = (String ) e.nextElement(); 318 String value = props.getProperty(key); 319 hash.put(key, value); 320 } 321 } 322 } 323 } 324 } 325 } 326 327 330 public static class Token { 331 332 333 private String key; 334 335 336 private String value; 337 338 343 public final void setKey(String key) { 344 this.key = key; 345 } 346 347 352 public final void setValue(String value) { 353 this.value = value; 354 } 355 356 361 public final String getKey() { 362 return key; 363 } 364 365 370 public final String getValue() { 371 return value; 372 } 373 } 374 } 375 | Popular Tags |