FileDocCategorySizeDatePackage
ReplaceTokens.javaAPI DocApache Ant 1.7011671Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.filters

ReplaceTokens

public final class ReplaceTokens extends BaseParamFilterReader implements ChainableReader
Replaces tokens in the original input with user-supplied values. Example:
<replacetokens begintoken="#" endtoken="#">
<token key="DATE" value="${TODAY}"/>
</replacetokens>
Or:
<filterreader classname="org.apache.tools.ant.filters.ReplaceTokens">
<param type="tokenchar" name="begintoken" value="#"/>
<param type="tokenchar" name="endtoken" value="#"/>
<param type="token" name="DATE" value="${TODAY}"/>
</filterreader>

Fields Summary
private static final char
DEFAULT_BEGIN_TOKEN
Default "begin token" character.
private static final char
DEFAULT_END_TOKEN
Default "end token" character.
private String
queuedData
Data to be used before reading from stream again
private String
replaceData
replacement test from a token
private int
replaceIndex
Index into replacement data
private int
queueIndex
Index into queue data
private Hashtable
hash
Hashtable to hold the replacee-replacer pairs (String to String).
private char
beginToken
Character marking the beginning of a token.
private char
endToken
Character marking the end of a token.
Constructors Summary
public ReplaceTokens()
Constructor for "dummy" instances.

see
BaseFilterReader#BaseFilterReader()


               
      
        super();
    
public ReplaceTokens(Reader in)
Creates a new filtered reader.

param
in A Reader object providing the underlying stream. Must not be null.

        super(in);
    
Methods Summary
public voidaddConfiguredToken(org.apache.tools.ant.filters.ReplaceTokens$Token token)
Adds a token element to the map of tokens to replace.

param
token The token to add to the map of replacements. Must not be null.

        hash.put(token.getKey(), token.getValue());
    
public java.io.Readerchain(java.io.Reader rdr)
Creates a new ReplaceTokens using the passed in Reader for instantiation.

param
rdr A Reader object providing the underlying stream. Must not be null.
return
a new filter based on this configuration, but filtering the specified reader

        ReplaceTokens newFilter = new ReplaceTokens(rdr);
        newFilter.setBeginToken(getBeginToken());
        newFilter.setEndToken(getEndToken());
        newFilter.setTokens(getTokens());
        newFilter.setInitialized(true);
        return newFilter;
    
private chargetBeginToken()
Returns the "begin token" character.

return
the character used to denote the beginning of a token

        return beginToken;
    
private chargetEndToken()
Returns the "end token" character.

return
the character used to denote the end of a token

        return endToken;
    
private intgetNextChar()

        if (queueIndex != -1) {
            final int ch = queuedData.charAt(queueIndex++);
            if (queueIndex >= queuedData.length()) {
                queueIndex = -1;
            }
            return ch;
        }

        return in.read();
    
private java.util.PropertiesgetPropertiesFromFile(java.lang.String fileName)
Returns properties from a specified properties file.

param
fileName The file to load properties from.

        FileInputStream in = null;
        Properties props = new Properties();
        try {
            in = new FileInputStream(fileName);
            props.load(in);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            FileUtils.close(in);
        }

        return props;
    
private java.util.HashtablegetTokens()
Returns the map of tokens which will be replaced.

return
a map (String->String) of token keys to replacement values

        return hash;
    
private voidinitialize()
Initializes tokens and loads the replacee-replacer hashtable.

        Parameter[] params = getParameters();
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                if (params[i] != null) {
                    final String type = params[i].getType();
                    if ("tokenchar".equals(type)) {
                        final String name = params[i].getName();
                        String value = params[i].getValue();
                        if ("begintoken".equals(name)) {
                            if (value.length() == 0) {
                                throw new BuildException("Begin token cannot "
                                    + "be empty");
                            }
                            beginToken = params[i].getValue().charAt(0);
                        } else if ("endtoken".equals(name)) {
                            if (value.length() == 0) {
                                throw new BuildException("End token cannot "
                                    + "be empty");
                            }
                            endToken = params[i].getValue().charAt(0);
                        }
                    } else if ("token".equals(type)) {
                        final String name = params[i].getName();
                        final String value = params[i].getValue();
                        hash.put(name, value);
                    } else if ("propertiesfile".equals(type)) {
                        Properties props = getPropertiesFromFile(params[i].getValue());
                        for (Enumeration e = props.keys(); e.hasMoreElements();) {
                            String key = (String) e.nextElement();
                            String value = props.getProperty(key);
                            hash.put(key, value);
                        }
                    }
                }
            }
        }
    
public intread()
Returns the next character in the filtered stream, replacing tokens from the original stream.

return
the next character in the resulting stream, or -1 if the end of the resulting stream has been reached
exception
IOException if the underlying stream throws an IOException during reading

        if (!getInitialized()) {
            initialize();
            setInitialized(true);
        }

        if (replaceIndex != -1) {
            final int ch = replaceData.charAt(replaceIndex++);
            if (replaceIndex >= replaceData.length()) {
                replaceIndex = -1;
            }
            return ch;
        }

        int ch = getNextChar();

        if (ch == beginToken) {
            final StringBuffer key = new StringBuffer("");
            do  {
                ch = getNextChar();
                if (ch != -1) {
                    key.append((char) ch);
                } else {
                    break;
                }
            } while (ch != endToken);

            if (ch == -1) {
                if (queuedData == null || queueIndex == -1) {
                    queuedData = key.toString();
                } else {
                    queuedData
                        = key.toString() + queuedData.substring(queueIndex);
                }
                queueIndex = 0;
                return beginToken;
            } else {
                key.setLength(key.length() - 1);

                final String replaceWith = (String) hash.get(key.toString());
                if (replaceWith != null) {
                    if (replaceWith.length() > 0) {
                        replaceData = replaceWith;
                        replaceIndex = 0;
                    }
                    return read();
                } else {
                    String newData = key.toString() + endToken;
                    if (queuedData == null || queueIndex == -1) {
                        queuedData = newData;
                    } else {
                        queuedData = newData + queuedData.substring(queueIndex);
                    }
                    queueIndex = 0;
                    return beginToken;
                }
            }
        }
        return ch;
    
public voidsetBeginToken(char beginToken)
Sets the "begin token" character.

param
beginToken the character used to denote the beginning of a token

        this.beginToken = beginToken;
    
public voidsetEndToken(char endToken)
Sets the "end token" character.

param
endToken the character used to denote the end of a token

        this.endToken = endToken;
    
private voidsetTokens(java.util.Hashtable hash)
Sets the map of tokens to replace.

param
hash A map (String->String) of token keys to replacement values. Must not be null.

        this.hash = hash;