KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > filters > ExpandProperties


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.filters;
19
20 import java.io.IOException JavaDoc;
21 import java.io.Reader JavaDoc;
22 import org.apache.tools.ant.Project;
23
24 /**
25  * Expands Ant properties, if any, in the data.
26  * <p>
27  * Example:<br>
28  * <pre>&lt;expandproperties/&gt;</pre>
29  * Or:
30  * <pre>&lt;filterreader
31  * classname=&quot;org.apache.tools.ant.filters.ExpandProperties&quot;/&gt;</pre>
32  *
33  */

34 public final class ExpandProperties
35     extends BaseFilterReader
36     implements ChainableReader {
37     /** Data that must be read from, if not null. */
38     private String JavaDoc queuedData = null;
39
40     /**
41      * Constructor for "dummy" instances.
42      *
43      * @see BaseFilterReader#BaseFilterReader()
44      */

45     public ExpandProperties() {
46         super();
47     }
48
49     /**
50      * Creates a new filtered reader.
51      *
52      * @param in A Reader object providing the underlying stream.
53      * Must not be <code>null</code>.
54      */

55     public ExpandProperties(final Reader JavaDoc in) {
56         super(in);
57     }
58
59     /**
60      * Returns the next character in the filtered stream. The original
61      * stream is first read in fully, and the Ant properties are expanded.
62      * The results of this expansion are then queued so they can be read
63      * character-by-character.
64      *
65      * @return the next character in the resulting stream, or -1
66      * if the end of the resulting stream has been reached
67      *
68      * @exception IOException if the underlying stream throws an IOException
69      * during reading
70      */

71     public int read() throws IOException JavaDoc {
72
73         int ch = -1;
74
75         if (queuedData != null && queuedData.length() == 0) {
76             queuedData = null;
77         }
78
79         if (queuedData != null) {
80             ch = queuedData.charAt(0);
81             queuedData = queuedData.substring(1);
82             if (queuedData.length() == 0) {
83                 queuedData = null;
84             }
85         } else {
86             queuedData = readFully();
87             if (queuedData == null) {
88                 ch = -1;
89             } else {
90                 Project project = getProject();
91                 queuedData = project.replaceProperties(queuedData);
92                 return read();
93             }
94         }
95         return ch;
96     }
97
98     /**
99      * Creates a new ExpandProperties filter using the passed in
100      * Reader for instantiation.
101      *
102      * @param rdr A Reader object providing the underlying stream.
103      * Must not be <code>null</code>.
104      *
105      * @return a new filter based on this configuration, but filtering
106      * the specified reader
107      */

108     public Reader JavaDoc chain(final Reader JavaDoc rdr) {
109         ExpandProperties newFilter = new ExpandProperties(rdr);
110         newFilter.setProject(getProject());
111         return newFilter;
112     }
113 }
114
Popular Tags