KickJava   Java API By Example, From Geeks To Geeks.

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


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.types.Parameter;
23
24 /**
25  * Attaches a prefix to every line.
26  *
27  * Example:
28  * <pre>&lt;prefixlines prefix=&quot;Foo&quot;/&gt;</pre>
29  *
30  * Or:
31  *
32  * <pre>&lt;filterreader classname=&quot;org.apache.tools.ant.filters.PrefixLines&quot;&gt;
33  * &lt;param name=&quot;prefix&quot; value=&quot;Foo&quot;/&gt;
34  * &lt;/filterreader&gt;</pre>
35  *
36  */

37 public final class PrefixLines
38     extends BaseParamFilterReader
39     implements ChainableReader {
40     /** Parameter name for the prefix. */
41     private static final String JavaDoc PREFIX_KEY = "prefix";
42
43     /** The prefix to be used. */
44     private String JavaDoc prefix = null;
45
46     /** Data that must be read from, if not null. */
47     private String JavaDoc queuedData = null;
48
49     /**
50      * Constructor for "dummy" instances.
51      *
52      * @see BaseFilterReader#BaseFilterReader()
53      */

54     public PrefixLines() {
55         super();
56     }
57
58     /**
59      * Creates a new filtered reader.
60      *
61      * @param in A Reader object providing the underlying stream.
62      * Must not be <code>null</code>.
63      */

64     public PrefixLines(final Reader JavaDoc in) {
65         super(in);
66     }
67
68     /**
69      * Returns the next character in the filtered stream. One line is read
70      * from the original input, and the prefix added. The resulting
71      * line is then used until it ends, at which point the next original line
72      * is read, etc.
73      *
74      * @return the next character in the resulting stream, or -1
75      * if the end of the resulting stream has been reached
76      *
77      * @exception IOException if the underlying stream throws an IOException
78      * during reading
79      */

80     public int read() throws IOException JavaDoc {
81         if (!getInitialized()) {
82             initialize();
83             setInitialized(true);
84         }
85
86         int ch = -1;
87
88         if (queuedData != null && queuedData.length() == 0) {
89             queuedData = null;
90         }
91
92         if (queuedData != null) {
93             ch = queuedData.charAt(0);
94             queuedData = queuedData.substring(1);
95             if (queuedData.length() == 0) {
96                 queuedData = null;
97             }
98         } else {
99             queuedData = readLine();
100             if (queuedData == null) {
101                 ch = -1;
102             } else {
103                 if (prefix != null) {
104                     queuedData = prefix + queuedData;
105                 }
106                 return read();
107             }
108         }
109         return ch;
110     }
111
112     /**
113      * Sets the prefix to add at the start of each input line.
114      *
115      * @param prefix The prefix to add at the start of each input line.
116      * May be <code>null</code>, in which case no prefix
117      * is added.
118      */

119     public void setPrefix(final String JavaDoc prefix) {
120         this.prefix = prefix;
121     }
122
123     /**
124      * Returns the prefix which will be added at the start of each input line.
125      *
126      * @return the prefix which will be added at the start of each input line
127      */

128     private String JavaDoc getPrefix() {
129         return prefix;
130     }
131
132     /**
133      * Creates a new PrefixLines filter using the passed in
134      * Reader for instantiation.
135      *
136      * @param rdr A Reader object providing the underlying stream.
137      * Must not be <code>null</code>.
138      *
139      * @return a new filter based on this configuration, but filtering
140      * the specified reader
141      */

142     public Reader JavaDoc chain(final Reader JavaDoc rdr) {
143         PrefixLines newFilter = new PrefixLines(rdr);
144         newFilter.setPrefix(getPrefix());
145         newFilter.setInitialized(true);
146         return newFilter;
147     }
148
149     /**
150      * Initializes the prefix if it is available from the parameters.
151      */

152     private void initialize() {
153         Parameter[] params = getParameters();
154         if (params != null) {
155             for (int i = 0; i < params.length; i++) {
156                 if (PREFIX_KEY.equals(params[i].getName())) {
157                     prefix = params[i].getValue();
158                     break;
159                 }
160             }
161         }
162     }
163 }
164
Popular Tags