KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Vector JavaDoc;
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.types.Parameter;
25
26 /**
27  * Filter which includes only those lines that contain all the user-specified
28  * strings.
29  *
30  * Example:
31  *
32  * <pre>&lt;linecontains&gt;
33  * &lt;contains value=&quot;foo&quot;&gt;
34  * &lt;contains value=&quot;bar&quot;&gt;
35  * &lt;/linecontains&gt;</pre>
36  *
37  * Or:
38  *
39  * <pre>&lt;filterreader classname=&quot;org.apache.tools.ant.filters.LineContains&quot;&gt;
40  * &lt;param type=&quot;contains&quot; value=&quot;foo&quot;/&gt;
41  * &lt;param type=&quot;contains&quot; value=&quot;bar&quot;/&gt;
42  * &lt;/filterreader&gt;</pre>
43  *
44  * This will include only those lines that contain <code>foo</code> and
45  * <code>bar</code>.
46  *
47  */

48 public final class LineContains
49     extends BaseParamFilterReader
50     implements ChainableReader {
51     /** Parameter name for the words to filter on. */
52     private static final String JavaDoc CONTAINS_KEY = "contains";
53
54     /** Parameter name for the words to filter on. */
55     private static final String JavaDoc NEGATE_KEY = "negate";
56
57     /** Vector that holds the strings that input lines must contain. */
58     private Vector JavaDoc contains = new Vector JavaDoc();
59
60     /**
61      * Remaining line to be read from this filter, or <code>null</code> if
62      * the next call to <code>read()</code> should read the original stream
63      * to find the next matching line.
64      */

65     private String JavaDoc line = null;
66
67     private boolean negate = false;
68
69     /**
70      * Constructor for "dummy" instances.
71      *
72      * @see BaseFilterReader#BaseFilterReader()
73      */

74     public LineContains() {
75         super();
76     }
77
78     /**
79      * Creates a new filtered reader.
80      *
81      * @param in A Reader object providing the underlying stream.
82      * Must not be <code>null</code>.
83      */

84     public LineContains(final Reader JavaDoc in) {
85         super(in);
86     }
87
88     /**
89      * Returns the next character in the filtered stream, only including
90      * lines from the original stream which contain all of the specified words.
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         int ch = -1;
105
106         if (line != null) {
107             ch = line.charAt(0);
108             if (line.length() == 1) {
109                 line = null;
110             } else {
111                 line = line.substring(1);
112             }
113         } else {
114             final int containsSize = contains.size();
115
116             for (line = readLine(); line != null; line = readLine()) {
117                 boolean matches = true;
118                 for (int i = 0; matches && i < containsSize; i++) {
119                     String JavaDoc containsStr = (String JavaDoc) contains.elementAt(i);
120                     matches = line.indexOf(containsStr) >= 0;
121                 }
122                 if (matches ^ isNegated()) {
123                     break;
124                 }
125             }
126             if (line != null) {
127                 return read();
128             }
129         }
130         return ch;
131     }
132
133     /**
134      * Adds a <code>contains</code> element.
135      *
136      * @param contains The <code>contains</code> element to add.
137      * Must not be <code>null</code>.
138      */

139     public void addConfiguredContains(final Contains contains) {
140         this.contains.addElement(contains.getValue());
141     }
142
143     /**
144      * Set the negation mode. Default false (no negation).
145      * @param b the boolean negation mode to set.
146      */

147     public void setNegate(boolean b) {
148         negate = b;
149     }
150
151     /**
152      * Find out whether we have been negated.
153      * @return boolean negation flag.
154      */

155     public boolean isNegated() {
156         return negate;
157     }
158
159     /**
160      * Sets the vector of words which must be contained within a line read
161      * from the original stream in order for it to match this filter.
162      *
163      * @param contains A vector of words which must be contained within a line
164      * in order for it to match in this filter. Must not be <code>null</code>.
165      */

166     private void setContains(final Vector JavaDoc contains) {
167         this.contains = contains;
168     }
169
170     /**
171      * Returns the vector of words which must be contained within a line read
172      * from the original stream in order for it to match this filter.
173      *
174      * @return the vector of words which must be contained within a line read
175      * from the original stream in order for it to match this filter. The
176      * returned object is "live" - in other words, changes made to the
177      * returned object are mirrored in the filter.
178      */

179     private Vector JavaDoc getContains() {
180         return contains;
181     }
182
183     /**
184      * Creates a new LineContains using the passed in
185      * Reader for instantiation.
186      *
187      * @param rdr A Reader object providing the underlying stream.
188      * Must not be <code>null</code>.
189      *
190      * @return a new filter based on this configuration, but filtering
191      * the specified reader
192      */

193     public Reader JavaDoc chain(final Reader JavaDoc rdr) {
194         LineContains newFilter = new LineContains(rdr);
195         newFilter.setContains(getContains());
196         newFilter.setNegate(isNegated());
197         return newFilter;
198     }
199
200     /**
201      * Parses the parameters to add user-defined contains strings.
202      */

203     private void initialize() {
204         Parameter[] params = getParameters();
205         if (params != null) {
206             for (int i = 0; i < params.length; i++) {
207                 if (CONTAINS_KEY.equals(params[i].getType())) {
208                     contains.addElement(params[i].getValue());
209                 } else if (NEGATE_KEY.equals(params[i].getType())) {
210                     setNegate(Project.toBoolean(params[i].getValue()));
211                 }
212             }
213         }
214     }
215
216     /**
217      * Holds a contains element
218      */

219     public static class Contains {
220
221         /** User defined contains string */
222         private String JavaDoc value;
223
224         /**
225          * Sets the contains string
226          *
227          * @param contains The contains string to set.
228          * Must not be <code>null</code>.
229          */

230         public final void setValue(String JavaDoc contains) {
231             value = contains;
232         }
233
234         /**
235          * Returns the contains string.
236          *
237          * @return the contains string for this element
238          */

239         public final String JavaDoc getValue() {
240             return value;
241         }
242     }
243 }
244
Popular Tags