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