1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.BufferedReader ; 22 import java.io.BufferedWriter ; 23 import java.io.File ; 24 import java.io.FileReader ; 25 import java.io.FileWriter ; 26 import java.io.IOException ; 27 import java.util.Hashtable ; 28 import java.util.StringTokenizer ; 29 import org.apache.tools.ant.BuildException; 30 import org.apache.tools.ant.Task; 31 32 42 public class KeySubst extends Task { 43 private File source = null; 44 private File dest = null; 45 private String sep = "*"; 46 private Hashtable replacements = new Hashtable (); 47 48 52 public void execute() throws BuildException { 53 log("!! KeySubst is deprecated. Use Filter + Copy instead. !!"); 54 log("Performing Substitutions"); 55 if (source == null || dest == null) { 56 log("Source and destinations must not be null"); 57 return; 58 } 59 BufferedReader br = null; 60 BufferedWriter bw = null; 61 try { 62 br = new BufferedReader (new FileReader (source)); 63 dest.delete(); 64 bw = new BufferedWriter (new FileWriter (dest)); 65 66 String line = null; 67 String newline = null; 68 line = br.readLine(); 69 while (line != null) { 70 if (line.length() == 0) { 71 bw.newLine(); 72 } else { 73 newline = KeySubst.replace(line, replacements); 74 bw.write(newline); 75 bw.newLine(); 76 } 77 line = br.readLine(); 78 } 79 bw.flush(); 80 } catch (IOException ioe) { 81 ioe.printStackTrace(); 82 } finally { 83 if (bw != null) { 84 try { 85 bw.close(); 86 } catch (IOException e) { 87 } 89 } 90 if (br != null) { 91 try { 92 br.close(); 93 } catch (IOException e) { 94 } 96 } 97 } 98 } 99 100 104 public void setSrc(File s) { 105 this.source = s; 106 } 107 108 112 public void setDest(File dest) { 113 this.dest = dest; 114 } 115 116 121 public void setSep(String sep) { 122 this.sep = sep; 123 } 124 137 public void setKeys(String keys) { 138 if (keys != null && keys.length() > 0) { 139 StringTokenizer tok = 140 new StringTokenizer (keys, this.sep, false); 141 while (tok.hasMoreTokens()) { 142 String token = tok.nextToken().trim(); 143 StringTokenizer itok = 144 new StringTokenizer (token, "=", false); 145 146 String name = itok.nextToken(); 147 String value = itok.nextToken(); 148 replacements.put(name, value); 149 } 150 } 151 } 152 153 154 158 public static void main(String [] args) { 159 try { 160 Hashtable hash = new Hashtable (); 161 hash.put("VERSION", "1.0.3"); 162 hash.put("b", "ffff"); 163 System.out.println(KeySubst.replace("$f ${VERSION} f ${b} jj $", 164 hash)); 165 } catch (Exception e) { 166 e.printStackTrace(); 167 } 168 } 169 170 177 public static String replace(String origString, Hashtable keys) 178 throws BuildException { 179 StringBuffer finalString = new StringBuffer (); 180 int index = 0; 181 int i = 0; 182 String key = null; 183 while ((index = origString.indexOf("${", i)) > -1) { 184 key = origString.substring(index + 2, origString.indexOf("}", 185 index + 3)); 186 finalString.append (origString.substring(i, index)); 187 if (keys.containsKey(key)) { 188 finalString.append (keys.get(key)); 189 } else { 190 finalString.append ("${"); 191 finalString.append (key); 192 finalString.append ("}"); 193 } 194 i = index + 3 + key.length(); 195 } 196 finalString.append (origString.substring(i)); 197 return finalString.toString(); 198 } 199 } 200 | Popular Tags |