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 | */
|
---|
17 | package org.apache.tools.ant.filters;
|
---|
18 |
|
---|
19 | import java.io.IOException;
|
---|
20 | import java.io.Reader;
|
---|
21 | import org.apache.tools.ant.util.LineTokenizer;
|
---|
22 | import org.apache.tools.ant.types.Parameter;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Reads the first <code>n</code> lines of a stream.
|
---|
26 | * (Default is first 10 lines.)
|
---|
27 | * <p>
|
---|
28 | * Example:
|
---|
29 | * <pre><headfilter lines="3"/></pre>
|
---|
30 | * Or:
|
---|
31 | * <pre><filterreader classname="org.apache.tools.ant.filters.HeadFilter">
|
---|
32 | * <param name="lines" value="3"/>
|
---|
33 | * </filterreader></pre>
|
---|
34 | *
|
---|
35 | */
|
---|
36 | public final class HeadFilter extends BaseParamFilterReader
|
---|
37 | implements ChainableReader {
|
---|
38 | /** Parameter name for the number of lines to be returned. */
|
---|
39 | private static final String LINES_KEY = "lines";
|
---|
40 |
|
---|
41 | /** Parameter name for the number of lines to be skipped. */
|
---|
42 | private static final String SKIP_KEY = "skip";
|
---|
43 |
|
---|
44 | /** Number of lines currently read in. */
|
---|
45 | private long linesRead = 0;
|
---|
46 |
|
---|
47 | /** Default number of lines to show */
|
---|
48 | private static final int DEFAULT_NUM_LINES = 10;
|
---|
49 |
|
---|
50 | /** Number of lines to be returned in the filtered stream. */
|
---|
51 | private long lines = DEFAULT_NUM_LINES;
|
---|
52 |
|
---|
53 | /** Number of lines to be skipped. */
|
---|
54 | private long skip = 0;
|
---|
55 |
|
---|
56 | /** A line tokenizer */
|
---|
57 | private LineTokenizer lineTokenizer = null;
|
---|
58 |
|
---|
59 | /** the current line from the input stream */
|
---|
60 | private String line = null;
|
---|
61 | /** the position in the current line */
|
---|
62 | private int linePos = 0;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Constructor for "dummy" instances.
|
---|
66 | *
|
---|
67 | * @see BaseFilterReader#BaseFilterReader()
|
---|
68 | */
|
---|
69 | public HeadFilter() {
|
---|
70 | super();
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Creates a new filtered reader.
|
---|
75 | *
|
---|
76 | * @param in A Reader object providing the underlying stream.
|
---|
77 | * Must not be <code>null</code>.
|
---|
78 | */
|
---|
79 | public HeadFilter(final Reader in) {
|
---|
80 | super(in);
|
---|
81 | lineTokenizer = new LineTokenizer();
|
---|
82 | lineTokenizer.setIncludeDelims(true);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Returns the next character in the filtered stream. If the desired
|
---|
87 | * number of lines have already been read, the resulting stream is
|
---|
88 | * effectively at an end. Otherwise, the next character from the
|
---|
89 | * underlying stream is read and returned.
|
---|
90 | *
|
---|
91 | * @return the next character in the resulting stream, or -1
|
---|
92 | * if the end of the resulting stream has been reached
|
---|
93 | *
|
---|
94 | * @exception IOException if the underlying stream throws an IOException
|
---|
95 | * during reading
|
---|
96 | */
|
---|
97 | public final int read() throws IOException {
|
---|
98 | if (!getInitialized()) {
|
---|
99 | initialize();
|
---|
100 | setInitialized(true);
|
---|
101 | }
|
---|
102 |
|
---|
103 | while (line == null || line.length() == 0) {
|
---|
104 | line = lineTokenizer.getToken(in);
|
---|
105 | if (line == null) {
|
---|
106 | return -1;
|
---|
107 | }
|
---|
108 | line = headFilter(line);
|
---|
109 | linePos = 0;
|
---|
110 | }
|
---|
111 |
|
---|
112 | int ch = line.charAt(linePos);
|
---|
113 | linePos++;
|
---|
114 | if (linePos == line.length()) {
|
---|
115 | line = null;
|
---|
116 | }
|
---|
117 | return ch;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Sets the number of lines to be returned in the filtered stream.
|
---|
122 | *
|
---|
123 | * @param lines the number of lines to be returned in the filtered stream
|
---|
124 | */
|
---|
125 | public final void setLines(final long lines) {
|
---|
126 | this.lines = lines;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Returns the number of lines to be returned in the filtered stream.
|
---|
131 | *
|
---|
132 | * @return the number of lines to be returned in the filtered stream
|
---|
133 | */
|
---|
134 | private final long getLines() {
|
---|
135 | return lines;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Sets the number of lines to be skipped in the filtered stream.
|
---|
140 | *
|
---|
141 | * @param skip the number of lines to be skipped in the filtered stream
|
---|
142 | */
|
---|
143 | public final void setSkip(final long skip) {
|
---|
144 | this.skip = skip;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Returns the number of lines to be skipped in the filtered stream.
|
---|
149 | *
|
---|
150 | * @return the number of lines to be skipped in the filtered stream
|
---|
151 | */
|
---|
152 | private final long getSkip() {
|
---|
153 | return skip;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Creates a new HeadFilter using the passed in
|
---|
158 | * Reader for instantiation.
|
---|
159 | *
|
---|
160 | * @param rdr A Reader object providing the underlying stream.
|
---|
161 | * Must not be <code>null</code>.
|
---|
162 | *
|
---|
163 | * @return a new filter based on this configuration, but filtering
|
---|
164 | * the specified reader
|
---|
165 | */
|
---|
166 | public final Reader chain(final Reader rdr) {
|
---|
167 | HeadFilter newFilter = new HeadFilter(rdr);
|
---|
168 | newFilter.setLines(getLines());
|
---|
169 | newFilter.setSkip(getSkip());
|
---|
170 | newFilter.setInitialized(true);
|
---|
171 | return newFilter;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Scans the parameters list for the "lines" parameter and uses
|
---|
176 | * it to set the number of lines to be returned in the filtered stream.
|
---|
177 | * also scan for skip parameter.
|
---|
178 | */
|
---|
179 | private final void initialize() {
|
---|
180 | Parameter[] params = getParameters();
|
---|
181 | if (params != null) {
|
---|
182 | for (int i = 0; i < params.length; i++) {
|
---|
183 | if (LINES_KEY.equals(params[i].getName())) {
|
---|
184 | lines = new Long(params[i].getValue()).longValue();
|
---|
185 | continue;
|
---|
186 | }
|
---|
187 | if (SKIP_KEY.equals(params[i].getName())) {
|
---|
188 | skip = new Long(params[i].getValue()).longValue();
|
---|
189 | continue;
|
---|
190 | }
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * implements a head filter on the input stream
|
---|
197 | */
|
---|
198 | private String headFilter(String line) {
|
---|
199 | linesRead++;
|
---|
200 | if (skip > 0) {
|
---|
201 | if ((linesRead - 1) < skip) {
|
---|
202 | return null;
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | if (lines > 0) {
|
---|
207 | if (linesRead > (lines + skip)) {
|
---|
208 | return null;
|
---|
209 | }
|
---|
210 | }
|
---|
211 | return line;
|
---|
212 | }
|
---|
213 | }
|
---|