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 java.util.Vector;
|
---|
22 | import org.apache.tools.ant.types.Parameter;
|
---|
23 | import org.apache.tools.ant.types.RegularExpression;
|
---|
24 | import org.apache.tools.ant.util.regexp.Regexp;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Filter which includes only those lines that contain the user-specified
|
---|
28 | * regular expression matching strings.
|
---|
29 | *
|
---|
30 | * Example:
|
---|
31 | * <pre><linecontainsregexp>
|
---|
32 | * <regexp pattern="foo*">
|
---|
33 | * </linecontainsregexp></pre>
|
---|
34 | *
|
---|
35 | * Or:
|
---|
36 | *
|
---|
37 | * <pre><filterreader classname="org.apache.tools.ant.filters.LineContainsRegExp">
|
---|
38 | * <param type="regexp" value="foo*"/>
|
---|
39 | * </filterreader></pre>
|
---|
40 | *
|
---|
41 | * This will fetch all those lines that contain the pattern <code>foo</code>
|
---|
42 | *
|
---|
43 | */
|
---|
44 | public final class LineContainsRegExp
|
---|
45 | extends BaseParamFilterReader
|
---|
46 | implements ChainableReader {
|
---|
47 | /** Parameter name for the regular expression to filter on. */
|
---|
48 | private static final String REGEXP_KEY = "regexp";
|
---|
49 |
|
---|
50 | /** Vector that holds the expressions that input lines must contain. */
|
---|
51 | private Vector regexps = new Vector();
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Remaining line to be read from this filter, or <code>null</code> if
|
---|
55 | * the next call to <code>read()</code> should read the original stream
|
---|
56 | * to find the next matching line.
|
---|
57 | */
|
---|
58 | private String line = null;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Constructor for "dummy" instances.
|
---|
62 | *
|
---|
63 | * @see BaseFilterReader#BaseFilterReader()
|
---|
64 | */
|
---|
65 | public LineContainsRegExp() {
|
---|
66 | super();
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Creates a new filtered reader.
|
---|
71 | *
|
---|
72 | * @param in A Reader object providing the underlying stream.
|
---|
73 | * Must not be <code>null</code>.
|
---|
74 | */
|
---|
75 | public LineContainsRegExp(final Reader in) {
|
---|
76 | super(in);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Returns the next character in the filtered stream, only including
|
---|
81 | * lines from the original stream which match all of the specified
|
---|
82 | * regular expressions.
|
---|
83 | *
|
---|
84 | * @return the next character in the resulting stream, or -1
|
---|
85 | * if the end of the resulting stream has been reached
|
---|
86 | *
|
---|
87 | * @exception IOException if the underlying stream throws an IOException
|
---|
88 | * during reading
|
---|
89 | */
|
---|
90 | public final int read() throws IOException {
|
---|
91 | if (!getInitialized()) {
|
---|
92 | initialize();
|
---|
93 | setInitialized(true);
|
---|
94 | }
|
---|
95 |
|
---|
96 | int ch = -1;
|
---|
97 |
|
---|
98 | if (line != null) {
|
---|
99 | ch = line.charAt(0);
|
---|
100 | if (line.length() == 1) {
|
---|
101 | line = null;
|
---|
102 | } else {
|
---|
103 | line = line.substring(1);
|
---|
104 | }
|
---|
105 | } else {
|
---|
106 | line = readLine();
|
---|
107 | final int regexpsSize = regexps.size();
|
---|
108 |
|
---|
109 | while (line != null) {
|
---|
110 | for (int i = 0; i < regexpsSize; i++) {
|
---|
111 | RegularExpression regexp = (RegularExpression)
|
---|
112 | regexps.elementAt(i);
|
---|
113 | Regexp re = regexp.getRegexp(getProject());
|
---|
114 | boolean matches = re.matches(line);
|
---|
115 | if (!matches) {
|
---|
116 | line = null;
|
---|
117 | break;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (line == null) {
|
---|
122 | // line didn't match
|
---|
123 | line = readLine();
|
---|
124 | } else {
|
---|
125 | break;
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (line != null) {
|
---|
130 | return read();
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | return ch;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Adds a <code>regexp</code> element.
|
---|
139 | *
|
---|
140 | * @param regExp The <code>regexp</code> element to add.
|
---|
141 | * Must not be <code>null</code>.
|
---|
142 | */
|
---|
143 | public final void addConfiguredRegexp(final RegularExpression regExp) {
|
---|
144 | this.regexps.addElement(regExp);
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Sets the vector of regular expressions which must be contained within
|
---|
149 | * a line read from the original stream in order for it to match this
|
---|
150 | * filter.
|
---|
151 | *
|
---|
152 | * @param regexps A vector of regular expressions which must be contained
|
---|
153 | * within a line in order for it to match in this filter. Must not be
|
---|
154 | * <code>null</code>.
|
---|
155 | */
|
---|
156 | private void setRegexps(final Vector regexps) {
|
---|
157 | this.regexps = regexps;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Returns the vector of regular expressions which must be contained within
|
---|
162 | * a line read from the original stream in order for it to match this
|
---|
163 | * filter.
|
---|
164 | *
|
---|
165 | * @return the vector of regular expressions which must be contained within
|
---|
166 | * a line read from the original stream in order for it to match this
|
---|
167 | * filter. The returned object is "live" - in other words, changes made to
|
---|
168 | * the returned object are mirrored in the filter.
|
---|
169 | */
|
---|
170 | private final Vector getRegexps() {
|
---|
171 | return regexps;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Creates a new LineContainsRegExp using the passed in
|
---|
176 | * Reader for instantiation.
|
---|
177 | *
|
---|
178 | * @param rdr A Reader object providing the underlying stream.
|
---|
179 | * Must not be <code>null</code>.
|
---|
180 | *
|
---|
181 | * @return a new filter based on this configuration, but filtering
|
---|
182 | * the specified reader
|
---|
183 | */
|
---|
184 | public final Reader chain(final Reader rdr) {
|
---|
185 | LineContainsRegExp newFilter = new LineContainsRegExp(rdr);
|
---|
186 | newFilter.setRegexps(getRegexps());
|
---|
187 | newFilter.setInitialized(true);
|
---|
188 | return newFilter;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Parses parameters to add user defined regular expressions.
|
---|
193 | */
|
---|
194 | private final void initialize() {
|
---|
195 | Parameter[] params = getParameters();
|
---|
196 | if (params != null) {
|
---|
197 | for (int i = 0; i < params.length; i++) {
|
---|
198 | if (REGEXP_KEY.equals(params[i].getType())) {
|
---|
199 | String pattern = params[i].getValue();
|
---|
200 | RegularExpression regexp = new RegularExpression();
|
---|
201 | regexp.setPattern(pattern);
|
---|
202 | regexps.addElement(regexp);
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|