001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.filters;
019:
020: import java.io.FileInputStream;
021: import java.io.IOException;
022: import java.io.Reader;
023: import java.util.Enumeration;
024: import java.util.Hashtable;
025: import java.util.Properties;
026: import org.apache.tools.ant.BuildException;
027: import org.apache.tools.ant.types.Parameter;
028: import org.apache.tools.ant.util.FileUtils;
029:
030: /**
031: * Replaces tokens in the original input with user-supplied values.
032: *
033: * Example:
034: *
035: * <pre><replacetokens begintoken="#" endtoken="#">
036: * <token key="DATE" value="${TODAY}"/>
037: * </replacetokens></pre>
038: *
039: * Or:
040: *
041: * <pre><filterreader classname="org.apache.tools.ant.filters.ReplaceTokens">
042: * <param type="tokenchar" name="begintoken" value="#"/>
043: * <param type="tokenchar" name="endtoken" value="#"/>
044: * <param type="token" name="DATE" value="${TODAY}"/>
045: * </filterreader></pre>
046: *
047: */
048: public final class ReplaceTokens extends BaseParamFilterReader
049: implements ChainableReader {
050: /** Default "begin token" character. */
051: private static final char DEFAULT_BEGIN_TOKEN = '@';
052:
053: /** Default "end token" character. */
054: private static final char DEFAULT_END_TOKEN = '@';
055:
056: /** Data to be used before reading from stream again */
057: private String queuedData = null;
058:
059: /** replacement test from a token */
060: private String replaceData = null;
061:
062: /** Index into replacement data */
063: private int replaceIndex = -1;
064:
065: /** Index into queue data */
066: private int queueIndex = -1;
067:
068: /** Hashtable to hold the replacee-replacer pairs (String to String). */
069: private Hashtable hash = new Hashtable();
070:
071: /** Character marking the beginning of a token. */
072: private char beginToken = DEFAULT_BEGIN_TOKEN;
073:
074: /** Character marking the end of a token. */
075: private char endToken = DEFAULT_END_TOKEN;
076:
077: /**
078: * Constructor for "dummy" instances.
079: *
080: * @see BaseFilterReader#BaseFilterReader()
081: */
082: public ReplaceTokens() {
083: super ();
084: }
085:
086: /**
087: * Creates a new filtered reader.
088: *
089: * @param in A Reader object providing the underlying stream.
090: * Must not be <code>null</code>.
091: */
092: public ReplaceTokens(final Reader in) {
093: super (in);
094: }
095:
096: private int getNextChar() throws IOException {
097: if (queueIndex != -1) {
098: final int ch = queuedData.charAt(queueIndex++);
099: if (queueIndex >= queuedData.length()) {
100: queueIndex = -1;
101: }
102: return ch;
103: }
104:
105: return in.read();
106: }
107:
108: /**
109: * Returns the next character in the filtered stream, replacing tokens
110: * from the original stream.
111: *
112: * @return the next character in the resulting stream, or -1
113: * if the end of the resulting stream has been reached
114: *
115: * @exception IOException if the underlying stream throws an IOException
116: * during reading
117: */
118: public int read() throws IOException {
119: if (!getInitialized()) {
120: initialize();
121: setInitialized(true);
122: }
123:
124: if (replaceIndex != -1) {
125: final int ch = replaceData.charAt(replaceIndex++);
126: if (replaceIndex >= replaceData.length()) {
127: replaceIndex = -1;
128: }
129: return ch;
130: }
131:
132: int ch = getNextChar();
133:
134: if (ch == beginToken) {
135: final StringBuffer key = new StringBuffer("");
136: do {
137: ch = getNextChar();
138: if (ch != -1) {
139: key.append((char) ch);
140: } else {
141: break;
142: }
143: } while (ch != endToken);
144:
145: if (ch == -1) {
146: if (queuedData == null || queueIndex == -1) {
147: queuedData = key.toString();
148: } else {
149: queuedData = key.toString()
150: + queuedData.substring(queueIndex);
151: }
152: queueIndex = 0;
153: return beginToken;
154: } else {
155: key.setLength(key.length() - 1);
156:
157: final String replaceWith = (String) hash.get(key
158: .toString());
159: if (replaceWith != null) {
160: if (replaceWith.length() > 0) {
161: replaceData = replaceWith;
162: replaceIndex = 0;
163: }
164: return read();
165: } else {
166: String newData = key.toString() + endToken;
167: if (queuedData == null || queueIndex == -1) {
168: queuedData = newData;
169: } else {
170: queuedData = newData
171: + queuedData.substring(queueIndex);
172: }
173: queueIndex = 0;
174: return beginToken;
175: }
176: }
177: }
178: return ch;
179: }
180:
181: /**
182: * Sets the "begin token" character.
183: *
184: * @param beginToken the character used to denote the beginning of a token
185: */
186: public void setBeginToken(final char beginToken) {
187: this .beginToken = beginToken;
188: }
189:
190: /**
191: * Returns the "begin token" character.
192: *
193: * @return the character used to denote the beginning of a token
194: */
195: private char getBeginToken() {
196: return beginToken;
197: }
198:
199: /**
200: * Sets the "end token" character.
201: *
202: * @param endToken the character used to denote the end of a token
203: */
204: public void setEndToken(final char endToken) {
205: this .endToken = endToken;
206: }
207:
208: /**
209: * Returns the "end token" character.
210: *
211: * @return the character used to denote the end of a token
212: */
213: private char getEndToken() {
214: return endToken;
215: }
216:
217: /**
218: * Adds a token element to the map of tokens to replace.
219: *
220: * @param token The token to add to the map of replacements.
221: * Must not be <code>null</code>.
222: */
223: public void addConfiguredToken(final Token token) {
224: hash.put(token.getKey(), token.getValue());
225: }
226:
227: /**
228: * Returns properties from a specified properties file.
229: *
230: * @param fileName The file to load properties from.
231: */
232: private Properties getPropertiesFromFile(String fileName) {
233: FileInputStream in = null;
234: Properties props = new Properties();
235: try {
236: in = new FileInputStream(fileName);
237: props.load(in);
238: } catch (IOException ioe) {
239: ioe.printStackTrace();
240: } finally {
241: FileUtils.close(in);
242: }
243:
244: return props;
245: }
246:
247: /**
248: * Sets the map of tokens to replace.
249: *
250: * @param hash A map (String->String) of token keys to replacement
251: * values. Must not be <code>null</code>.
252: */
253: private void setTokens(final Hashtable hash) {
254: this .hash = hash;
255: }
256:
257: /**
258: * Returns the map of tokens which will be replaced.
259: *
260: * @return a map (String->String) of token keys to replacement
261: * values
262: */
263: private Hashtable getTokens() {
264: return hash;
265: }
266:
267: /**
268: * Creates a new ReplaceTokens using the passed in
269: * Reader for instantiation.
270: *
271: * @param rdr A Reader object providing the underlying stream.
272: * Must not be <code>null</code>.
273: *
274: * @return a new filter based on this configuration, but filtering
275: * the specified reader
276: */
277: public Reader chain(final Reader rdr) {
278: ReplaceTokens newFilter = new ReplaceTokens(rdr);
279: newFilter.setBeginToken(getBeginToken());
280: newFilter.setEndToken(getEndToken());
281: newFilter.setTokens(getTokens());
282: newFilter.setInitialized(true);
283: return newFilter;
284: }
285:
286: /**
287: * Initializes tokens and loads the replacee-replacer hashtable.
288: */
289: private void initialize() {
290: Parameter[] params = getParameters();
291: if (params != null) {
292: for (int i = 0; i < params.length; i++) {
293: if (params[i] != null) {
294: final String type = params[i].getType();
295: if ("tokenchar".equals(type)) {
296: final String name = params[i].getName();
297: String value = params[i].getValue();
298: if ("begintoken".equals(name)) {
299: if (value.length() == 0) {
300: throw new BuildException(
301: "Begin token cannot "
302: + "be empty");
303: }
304: beginToken = params[i].getValue().charAt(0);
305: } else if ("endtoken".equals(name)) {
306: if (value.length() == 0) {
307: throw new BuildException(
308: "End token cannot "
309: + "be empty");
310: }
311: endToken = params[i].getValue().charAt(0);
312: }
313: } else if ("token".equals(type)) {
314: final String name = params[i].getName();
315: final String value = params[i].getValue();
316: hash.put(name, value);
317: } else if ("propertiesfile".equals(type)) {
318: Properties props = getPropertiesFromFile(params[i]
319: .getValue());
320: for (Enumeration e = props.keys(); e
321: .hasMoreElements();) {
322: String key = (String) e.nextElement();
323: String value = props.getProperty(key);
324: hash.put(key, value);
325: }
326: }
327: }
328: }
329: }
330: }
331:
332: /**
333: * Holds a token
334: */
335: public static class Token {
336:
337: /** Token key */
338: private String key;
339:
340: /** Token value */
341: private String value;
342:
343: /**
344: * Sets the token key
345: *
346: * @param key The key for this token. Must not be <code>null</code>.
347: */
348: public final void setKey(String key) {
349: this .key = key;
350: }
351:
352: /**
353: * Sets the token value
354: *
355: * @param value The value for this token. Must not be <code>null</code>.
356: */
357: public final void setValue(String value) {
358: this .value = value;
359: }
360:
361: /**
362: * Returns the key for this token.
363: *
364: * @return the key for this token
365: */
366: public final String getKey() {
367: return key;
368: }
369:
370: /**
371: * Returns the value for this token.
372: *
373: * @return the value for this token
374: */
375: public final String getValue() {
376: return value;
377: }
378: }
379: }
|