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.IOException;
021: import java.io.Reader;
022: import org.apache.tools.ant.Project;
023:
024: /**
025: * Expands Ant properties, if any, in the data.
026: * <p>
027: * Example:<br>
028: * <pre><expandproperties/></pre>
029: * Or:
030: * <pre><filterreader
031: * classname="org.apache.tools.ant.filters.ExpandProperties"/></pre>
032: *
033: */
034: public final class ExpandProperties extends BaseFilterReader implements
035: ChainableReader {
036: /** Data that must be read from, if not null. */
037: private String queuedData = null;
038:
039: /**
040: * Constructor for "dummy" instances.
041: *
042: * @see BaseFilterReader#BaseFilterReader()
043: */
044: public ExpandProperties() {
045: super ();
046: }
047:
048: /**
049: * Creates a new filtered reader.
050: *
051: * @param in A Reader object providing the underlying stream.
052: * Must not be <code>null</code>.
053: */
054: public ExpandProperties(final Reader in) {
055: super (in);
056: }
057:
058: /**
059: * Returns the next character in the filtered stream. The original
060: * stream is first read in fully, and the Ant properties are expanded.
061: * The results of this expansion are then queued so they can be read
062: * character-by-character.
063: *
064: * @return the next character in the resulting stream, or -1
065: * if the end of the resulting stream has been reached
066: *
067: * @exception IOException if the underlying stream throws an IOException
068: * during reading
069: */
070: public int read() throws IOException {
071:
072: int ch = -1;
073:
074: if (queuedData != null && queuedData.length() == 0) {
075: queuedData = null;
076: }
077:
078: if (queuedData != null) {
079: ch = queuedData.charAt(0);
080: queuedData = queuedData.substring(1);
081: if (queuedData.length() == 0) {
082: queuedData = null;
083: }
084: } else {
085: queuedData = readFully();
086: if (queuedData == null) {
087: ch = -1;
088: } else {
089: Project project = getProject();
090: queuedData = project.replaceProperties(queuedData);
091: return read();
092: }
093: }
094: return ch;
095: }
096:
097: /**
098: * Creates a new ExpandProperties filter using the passed in
099: * Reader for instantiation.
100: *
101: * @param rdr A Reader object providing the underlying stream.
102: * Must not be <code>null</code>.
103: *
104: * @return a new filter based on this configuration, but filtering
105: * the specified reader
106: */
107: public Reader chain(final Reader rdr) {
108: ExpandProperties newFilter = new ExpandProperties(rdr);
109: newFilter.setProject(getProject());
110: return newFilter;
111: }
112: }
|