source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/filters/PrefixLines.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 4.8 KB
Line 
1/*
2 * Copyright 2002,2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.tools.ant.filters;
18
19import java.io.IOException;
20import java.io.Reader;
21import org.apache.tools.ant.types.Parameter;
22
23/**
24 * Attaches a prefix to every line.
25 *
26 * Example:
27 * <pre>&lt;prefixlines prefix=&quot;Foo&quot;/&gt;</pre>
28 *
29 * Or:
30 *
31 * <pre>&lt;filterreader classname=&quot;org.apache.tools.ant.filters.PrefixLines&quot;&gt;
32 * &lt;param name=&quot;prefix&quot; value=&quot;Foo&quot;/&gt;
33 * &lt;/filterreader&gt;</pre>
34 *
35 */
36public final class PrefixLines
37 extends BaseParamFilterReader
38 implements ChainableReader {
39 /** Parameter name for the prefix. */
40 private static final String PREFIX_KEY = "prefix";
41
42 /** The prefix to be used. */
43 private String prefix = null;
44
45 /** Data that must be read from, if not null. */
46 private String queuedData = null;
47
48 /**
49 * Constructor for "dummy" instances.
50 *
51 * @see BaseFilterReader#BaseFilterReader()
52 */
53 public PrefixLines() {
54 super();
55 }
56
57 /**
58 * Creates a new filtered reader.
59 *
60 * @param in A Reader object providing the underlying stream.
61 * Must not be <code>null</code>.
62 */
63 public PrefixLines(final Reader in) {
64 super(in);
65 }
66
67 /**
68 * Returns the next character in the filtered stream. One line is read
69 * from the original input, and the prefix added. The resulting
70 * line is then used until it ends, at which point the next original line
71 * is read, etc.
72 *
73 * @return the next character in the resulting stream, or -1
74 * if the end of the resulting stream has been reached
75 *
76 * @exception IOException if the underlying stream throws an IOException
77 * during reading
78 */
79 public final int read() throws IOException {
80 if (!getInitialized()) {
81 initialize();
82 setInitialized(true);
83 }
84
85 int ch = -1;
86
87 if (queuedData != null && queuedData.length() == 0) {
88 queuedData = null;
89 }
90
91 if (queuedData != null) {
92 ch = queuedData.charAt(0);
93 queuedData = queuedData.substring(1);
94 if (queuedData.length() == 0) {
95 queuedData = null;
96 }
97 } else {
98 queuedData = readLine();
99 if (queuedData == null) {
100 ch = -1;
101 } else {
102 if (prefix != null) {
103 queuedData = prefix + queuedData;
104 }
105 return read();
106 }
107 }
108 return ch;
109 }
110
111 /**
112 * Sets the prefix to add at the start of each input line.
113 *
114 * @param prefix The prefix to add at the start of each input line.
115 * May be <code>null</code>, in which case no prefix
116 * is added.
117 */
118 public final void setPrefix(final String prefix) {
119 this.prefix = prefix;
120 }
121
122 /**
123 * Returns the prefix which will be added at the start of each input line.
124 *
125 * @return the prefix which will be added at the start of each input line
126 */
127 private final String getPrefix() {
128 return prefix;
129 }
130
131 /**
132 * Creates a new PrefixLines filter using the passed in
133 * Reader for instantiation.
134 *
135 * @param rdr A Reader object providing the underlying stream.
136 * Must not be <code>null</code>.
137 *
138 * @return a new filter based on this configuration, but filtering
139 * the specified reader
140 */
141 public final Reader chain(final Reader rdr) {
142 PrefixLines newFilter = new PrefixLines(rdr);
143 newFilter.setPrefix(getPrefix());
144 newFilter.setInitialized(true);
145 return newFilter;
146 }
147
148 /**
149 * Initializes the prefix if it is available from the parameters.
150 */
151 private final void initialize() {
152 Parameter[] params = getParameters();
153 if (params != null) {
154 for (int i = 0; i < params.length; i++) {
155 if (PREFIX_KEY.equals(params[i].getName())) {
156 prefix = params[i].getValue();
157 break;
158 }
159 }
160 }
161 }
162}
Note: See TracBrowser for help on using the repository browser.