Methods Summary |
---|
public void | execute()Do the execution.
log("!! KeySubst is deprecated. Use Filter + Copy instead. !!");
log("Performing Substitutions");
if (source == null || dest == null) {
log("Source and destinations must not be null");
return;
}
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(source));
dest.delete();
bw = new BufferedWriter(new FileWriter(dest));
String line = null;
String newline = null;
line = br.readLine();
while (line != null) {
if (line.length() == 0) {
bw.newLine();
} else {
newline = KeySubst.replace(line, replacements);
bw.write(newline);
bw.newLine();
}
line = br.readLine();
}
bw.flush();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
// ignore
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
// ignore
}
}
}
|
public static void | main(java.lang.String[] args)A test method.
try {
Hashtable hash = new Hashtable();
hash.put("VERSION", "1.0.3");
hash.put("b", "ffff");
System.out.println(KeySubst.replace("$f ${VERSION} f ${b} jj $",
hash));
} catch (Exception e) {
e.printStackTrace();
}
|
public static java.lang.String | replace(java.lang.String origString, java.util.Hashtable keys)Does replacement on text using the hashtable of keys.
StringBuffer finalString = new StringBuffer();
int index = 0;
int i = 0;
String key = null;
while ((index = origString.indexOf("${", i)) > -1) {
key = origString.substring(index + 2, origString.indexOf("}",
index + 3));
finalString.append (origString.substring(i, index));
if (keys.containsKey(key)) {
finalString.append (keys.get(key));
} else {
finalString.append ("${");
finalString.append (key);
finalString.append ("}");
}
i = index + 3 + key.length();
}
finalString.append (origString.substring(i));
return finalString.toString();
|
public void | setDest(java.io.File dest)Set the destination file.
this.dest = dest;
|
public void | setKeys(java.lang.String keys)Sets the keys.
Format string is like this:
name=value*name2=value
Names are case sensitive.
Use the setSep() method to change the * to something else
if you need to use * as a name or value.
if (keys != null && keys.length() > 0) {
StringTokenizer tok =
new StringTokenizer(keys, this.sep, false);
while (tok.hasMoreTokens()) {
String token = tok.nextToken().trim();
StringTokenizer itok =
new StringTokenizer(token, "=", false);
String name = itok.nextToken();
String value = itok.nextToken();
replacements.put(name, value);
}
}
|
public void | setSep(java.lang.String sep)Sets the separator between name=value arguments
in setKeys(). By default it is "*".
this.sep = sep;
|
public void | setSrc(java.io.File s)Set the source file.
this.source = s;
|