FileDocCategorySizeDatePackage
KeySubst.javaAPI DocApache Ant 1.706296Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.taskdefs

KeySubst

public class KeySubst extends org.apache.tools.ant.Task
Keyword substitution. Input file is written to output file. Do not make input file same as output file. Keywords in input files look like this: @foo@. See the docs for the setKeys method to understand how to do the substitutions.
since
Ant 1.1
deprecated
KeySubst is deprecated since Ant 1.1. Use Filter + Copy instead.

Fields Summary
private File
source
private File
dest
private String
sep
private Hashtable
replacements
Constructors Summary
Methods Summary
public voidexecute()
Do the execution.

throws
BuildException on error


                
         
        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 voidmain(java.lang.String[] args)
A test method.

param
args not used

        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.Stringreplace(java.lang.String origString, java.util.Hashtable keys)
Does replacement on text using the hashtable of keys.

param
origString an input string
param
keys mapping of keys to values
return
the string with the replacements in it.
throws
BuildException on error

        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 voidsetDest(java.io.File dest)
Set the destination file.

param
dest the destination file

        this.dest = dest;
    
public voidsetKeys(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.

param
keys a String 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 voidsetSep(java.lang.String sep)
Sets the separator between name=value arguments in setKeys(). By default it is "*".

param
sep the separator string

        this.sep = sep;
    
public voidsetSrc(java.io.File s)
Set the source file.

param
s the source file

        this.source = s;