KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.LineTokenizer;
23 import org.apache.tools.ant.types.Parameter;
24
25 /**
26  * Reads the first <code>n</code> lines of a stream.
27  * (Default is first 10 lines.)
28  * <p>
29  * Example:
30  * <pre>&lt;headfilter lines=&quot;3&quot;/&gt;</pre>
31  * Or:
32  * <pre>&lt;filterreader classname=&quot;org.apache.tools.ant.filters.HeadFilter&quot;&gt;
33  * &lt;param name=&quot;lines&quot; value=&quot;3&quot;/&gt;
34  * &lt;/filterreader&gt;</pre>
35  *
36  */

37 public final class HeadFilter extends BaseParamFilterReader
38     implements ChainableReader {
39     /** Parameter name for the number of lines to be returned. */
40     private static final String JavaDoc LINES_KEY = "lines";
41
42     /** Parameter name for the number of lines to be skipped. */
43     private static final String JavaDoc SKIP_KEY = "skip";
44
45     /** Number of lines currently read in. */
46     private long linesRead = 0;
47
48     /** Default number of lines to show */
49     private static final int DEFAULT_NUM_LINES = 10;
50
51     /** Number of lines to be returned in the filtered stream. */
52     private long lines = DEFAULT_NUM_LINES;
53
54     /** Number of lines to be skipped. */
55     private long skip = 0;
56
57     /** A line tokenizer */
58     private LineTokenizer lineTokenizer = null;
59
60     /** the current line from the input stream */
61     private String JavaDoc line = null;
62     /** the position in the current line */
63     private int linePos = 0;
64
65     /**
66      * Constructor for "dummy" instances.
67      *
68      * @see BaseFilterReader#BaseFilterReader()
69      */

70     public HeadFilter() {
71         super();
72     }
73
74     /**
75      * Creates a new filtered reader.
76      *
77      * @param in A Reader object providing the underlying stream.
78      * Must not be <code>null</code>.
79      */

80     public HeadFilter(final Reader JavaDoc in) {
81         super(in);
82         lineTokenizer = new LineTokenizer();
83         lineTokenizer.setIncludeDelims(true);
84     }
85
86     /**
87      * Returns the next character in the filtered stream. If the desired
88      * number of lines have already been read, the resulting stream is
89      * effectively at an end. Otherwise, the next character from the
90      * underlying stream is read and returned.
91      *
92      * @return the next character in the resulting stream, or -1
93      * if the end of the resulting stream has been reached
94      *
95      * @exception IOException if the underlying stream throws an IOException
96      * during reading
97      */

98     public int read() throws IOException JavaDoc {
99         if (!getInitialized()) {
100             initialize();
101             setInitialized(true);
102         }
103
104         while (line == null || line.length() == 0) {
105             line = lineTokenizer.getToken(in);
106             if (line == null) {
107                 return -1;
108             }
109             line = headFilter(line);
110             linePos = 0;
111         }
112
113         int ch = line.charAt(linePos);
114         linePos++;
115         if (linePos == line.length()) {
116             line = null;
117         }
118         return ch;
119     }
120
121     /**
122      * Sets the number of lines to be returned in the filtered stream.
123      *
124      * @param lines the number of lines to be returned in the filtered stream
125      */

126     public void setLines(final long lines) {
127         this.lines = lines;
128     }
129
130     /**
131      * Returns the number of lines to be returned in the filtered stream.
132      *
133      * @return the number of lines to be returned in the filtered stream
134      */

135     private long getLines() {
136         return lines;
137     }
138
139     /**
140      * Sets the number of lines to be skipped in the filtered stream.
141      *
142      * @param skip the number of lines to be skipped in the filtered stream
143      */

144     public void setSkip(final long skip) {
145         this.skip = skip;
146     }
147
148     /**
149      * Returns the number of lines to be skipped in the filtered stream.
150      *
151      * @return the number of lines to be skipped in the filtered stream
152      */

153     private long getSkip() {
154         return skip;
155     }
156
157     /**
158      * Creates a new HeadFilter using the passed in
159      * Reader for instantiation.
160      *
161      * @param rdr A Reader object providing the underlying stream.
162      * Must not be <code>null</code>.
163      *
164      * @return a new filter based on this configuration, but filtering
165      * the specified reader
166      */

167     public Reader JavaDoc chain(final Reader JavaDoc rdr) {
168         HeadFilter newFilter = new HeadFilter(rdr);
169         newFilter.setLines(getLines());
170         newFilter.setSkip(getSkip());
171         newFilter.setInitialized(true);
172         return newFilter;
173     }
174
175     /**
176      * Scans the parameters list for the "lines" parameter and uses
177      * it to set the number of lines to be returned in the filtered stream.
178      * also scan for skip parameter.
179      */

180     private void initialize() {
181         Parameter[] params = getParameters();
182         if (params != null) {
183             for (int i = 0; i < params.length; i++) {
184                 if (LINES_KEY.equals(params[i].getName())) {
185                     lines = new Long JavaDoc(params[i].getValue()).longValue();
186                     continue;
187                 }
188                 if (SKIP_KEY.equals(params[i].getName())) {
189                     skip = new Long JavaDoc(params[i].getValue()).longValue();
190                     continue;
191                 }
192             }
193         }
194     }
195
196     /**
197      * implements a head filter on the input stream
198      */

199     private String JavaDoc headFilter(String JavaDoc line) {
200         linesRead++;
201         if (skip > 0) {
202             if ((linesRead - 1) < skip) {
203                 return null;
204             }
205         }
206
207         if (lines > 0) {
208             if (linesRead > (lines + skip)) {
209                 return null;
210             }
211         }
212         return line;
213     }
214 }
215
Popular Tags