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