Fields Summary |
---|
private static final char | DEFAULT_BEGIN_TOKENDefault "begin token" character. |
private static final char | DEFAULT_END_TOKENDefault "end token" character. |
private String | queuedDataData to be used before reading from stream again |
private String | replaceDatareplacement test from a token |
private int | replaceIndexIndex into replacement data |
private int | queueIndexIndex into queue data |
private Hashtable | hashHashtable to hold the replacee-replacer pairs (String to String). |
private char | beginTokenCharacter marking the beginning of a token. |
private char | endTokenCharacter marking the end of a token. |
Methods Summary |
---|
public void | addConfiguredToken(org.apache.tools.ant.filters.ReplaceTokens$Token token)Adds a token element to the map of tokens to replace.
hash.put(token.getKey(), token.getValue());
|
public java.io.Reader | chain(java.io.Reader rdr)Creates a new ReplaceTokens using the passed in
Reader for instantiation.
ReplaceTokens newFilter = new ReplaceTokens(rdr);
newFilter.setBeginToken(getBeginToken());
newFilter.setEndToken(getEndToken());
newFilter.setTokens(getTokens());
newFilter.setInitialized(true);
return newFilter;
|
private char | getBeginToken()Returns the "begin token" character.
return beginToken;
|
private char | getEndToken()Returns the "end token" character.
return endToken;
|
private int | getNextChar()
if (queueIndex != -1) {
final int ch = queuedData.charAt(queueIndex++);
if (queueIndex >= queuedData.length()) {
queueIndex = -1;
}
return ch;
}
return in.read();
|
private java.util.Properties | getPropertiesFromFile(java.lang.String fileName)Returns properties from a specified properties file.
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.Hashtable | getTokens()Returns the map of tokens which will be replaced.
return hash;
|
private void | initialize()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 int | read()Returns the next character in the filtered stream, replacing tokens
from the original stream.
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 void | setBeginToken(char beginToken)Sets the "begin token" character.
this.beginToken = beginToken;
|
public void | setEndToken(char endToken)Sets the "end token" character.
this.endToken = endToken;
|
private void | setTokens(java.util.Hashtable hash)Sets the map of tokens to replace.
this.hash = hash;
|