KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.tools.ant.types.RegularExpression;
26 import org.apache.tools.ant.util.regexp.Regexp;
27
28 /**
29  * Filter which includes only those lines that contain the user-specified
30  * regular expression matching strings.
31  *
32  * Example:
33  * <pre>&lt;linecontainsregexp&gt;
34  * &lt;regexp pattern=&quot;foo*&quot;&gt;
35  * &lt;/linecontainsregexp&gt;</pre>
36  *
37  * Or:
38  *
39  * <pre>&lt;filterreader classname=&quot;org.apache.tools.ant.filters.LineContainsRegExp&quot;&gt;
40  * &lt;param type=&quot;regexp&quot; value=&quot;foo*&quot;/&gt;
41  * &lt;/filterreader&gt;</pre>
42  *
43  * This will fetch all those lines that contain the pattern <code>foo</code>
44  *
45  */

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

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

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

82     public LineContainsRegExp(final Reader JavaDoc in) {
83         super(in);
84     }
85
86     /**
87      * Returns the next character in the filtered stream, only including
88      * lines from the original stream which match all of the specified
89      * regular expressions.
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 int read() throws IOException JavaDoc {
98         if (!getInitialized()) {
99             initialize();
100             setInitialized(true);
101         }
102
103         int ch = -1;
104
105         if (line != null) {
106             ch = line.charAt(0);
107             if (line.length() == 1) {
108                 line = null;
109             } else {
110                 line = line.substring(1);
111             }
112         } else {
113             final int regexpsSize = regexps.size();
114
115             for (line = readLine(); line != null; line = readLine()) {
116                 boolean matches = true;
117                 for (int i = 0; matches && i < regexpsSize; i++) {
118                     RegularExpression regexp
119                         = (RegularExpression) regexps.elementAt(i);
120                     Regexp re = regexp.getRegexp(getProject());
121                     matches = re.matches(line);
122                 }
123                 if (matches ^ isNegated()) {
124                     break;
125                 }
126             }
127             if (line != null) {
128                 return read();
129             }
130         }
131         return ch;
132     }
133
134     /**
135      * Adds a <code>regexp</code> element.
136      *
137      * @param regExp The <code>regexp</code> element to add.
138      * Must not be <code>null</code>.
139      */

140     public void addConfiguredRegexp(final RegularExpression regExp) {
141         this.regexps.addElement(regExp);
142     }
143
144     /**
145      * Sets the vector of regular expressions which must be contained within
146      * a line read from the original stream in order for it to match this
147      * filter.
148      *
149      * @param regexps A vector of regular expressions which must be contained
150      * within a line in order for it to match in this filter. Must not be
151      * <code>null</code>.
152      */

153     private void setRegexps(final Vector JavaDoc regexps) {
154         this.regexps = regexps;
155     }
156
157     /**
158      * Returns the vector of regular expressions which must be contained within
159      * a line read from the original stream in order for it to match this
160      * filter.
161      *
162      * @return the vector of regular expressions which must be contained within
163      * a line read from the original stream in order for it to match this
164      * filter. The returned object is "live" - in other words, changes made to
165      * the returned object are mirrored in the filter.
166      */

167     private Vector JavaDoc getRegexps() {
168         return regexps;
169     }
170
171     /**
172      * Creates a new LineContainsRegExp using the passed in
173      * Reader for instantiation.
174      *
175      * @param rdr A Reader object providing the underlying stream.
176      * Must not be <code>null</code>.
177      *
178      * @return a new filter based on this configuration, but filtering
179      * the specified reader
180      */

181     public Reader JavaDoc chain(final Reader JavaDoc rdr) {
182         LineContainsRegExp newFilter = new LineContainsRegExp(rdr);
183         newFilter.setRegexps(getRegexps());
184         newFilter.setNegate(isNegated());
185         return newFilter;
186     }
187
188     /**
189      * Set the negation mode. Default false (no negation).
190      * @param b the boolean negation mode to set.
191      */

192     public void setNegate(boolean b) {
193         negate = b;
194     }
195
196     /**
197      * Find out whether we have been negated.
198      * @return boolean negation flag.
199      */

200     public boolean isNegated() {
201         return negate;
202     }
203
204     /**
205      * Parses parameters to add user defined regular expressions.
206      */

207     private void initialize() {
208         Parameter[] params = getParameters();
209         if (params != null) {
210             for (int i = 0; i < params.length; i++) {
211                 if (REGEXP_KEY.equals(params[i].getType())) {
212                     String JavaDoc pattern = params[i].getValue();
213                     RegularExpression regexp = new RegularExpression();
214                     regexp.setPattern(pattern);
215                     regexps.addElement(regexp);
216                 } else if (NEGATE_KEY.equals(params[i].getType())) {
217                     setNegate(Project.toBoolean(params[i].getValue()));
218                 }
219             }
220         }
221     }
222 }
223
Popular Tags