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.types.selectors;
  18. import java.io.BufferedReader;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.IOException;
  22. import java.io.InputStreamReader;
  23. import org.apache.tools.ant.BuildException;
  24. import org.apache.tools.ant.Project;
  25. import org.apache.tools.ant.types.Parameter;
  26. /**
  27. * Selector that filters files based on whether they contain a
  28. * particular string.
  29. *
  30. * @since 1.5
  31. */
  32. public class ContainsSelector extends BaseExtendSelector {
  33. private String contains = null;
  34. private boolean casesensitive = true;
  35. private boolean ignorewhitespace = false;
  36. /** Key to used for parameterized custom selector */
  37. public static final String EXPRESSION_KEY = "expression";
  38. /** Used for parameterized custom selector */
  39. public static final String CONTAINS_KEY = "text";
  40. /** Used for parameterized custom selector */
  41. public static final String CASE_KEY = "casesensitive";
  42. /** Used for parameterized custom selector */
  43. public static final String WHITESPACE_KEY = "ignorewhitespace";
  44. /**
  45. * Creates a new <code>ContainsSelector</code> instance.
  46. *
  47. */
  48. public ContainsSelector() {
  49. }
  50. /**
  51. * @return a string describing this object
  52. */
  53. public String toString() {
  54. StringBuffer buf = new StringBuffer("{containsselector text: ");
  55. buf.append(contains);
  56. buf.append(" casesensitive: ");
  57. if (casesensitive) {
  58. buf.append("true");
  59. } else {
  60. buf.append("false");
  61. }
  62. buf.append(" ignorewhitespace: ");
  63. if (ignorewhitespace) {
  64. buf.append("true");
  65. } else {
  66. buf.append("false");
  67. }
  68. buf.append("}");
  69. return buf.toString();
  70. }
  71. /**
  72. * The string to search for within a file.
  73. *
  74. * @param contains the string that a file must contain to be selected.
  75. */
  76. public void setText(String contains) {
  77. this.contains = contains;
  78. }
  79. /**
  80. * Whether to ignore case in the string being searched.
  81. *
  82. * @param casesensitive whether to pay attention to case sensitivity
  83. */
  84. public void setCasesensitive(boolean casesensitive) {
  85. this.casesensitive = casesensitive;
  86. }
  87. /**
  88. * Whether to ignore whitespace in the string being searched.
  89. *
  90. * @param ignorewhitespace whether to ignore any whitespace
  91. * (spaces, tabs, etc.) in the searchstring
  92. */
  93. public void setIgnorewhitespace(boolean ignorewhitespace) {
  94. this.ignorewhitespace = ignorewhitespace;
  95. }
  96. /**
  97. * When using this as a custom selector, this method will be called.
  98. * It translates each parameter into the appropriate setXXX() call.
  99. *
  100. * @param parameters the complete set of parameters for this selector
  101. */
  102. public void setParameters(Parameter[] parameters) {
  103. super.setParameters(parameters);
  104. if (parameters != null) {
  105. for (int i = 0; i < parameters.length; i++) {
  106. String paramname = parameters[i].getName();
  107. if (CONTAINS_KEY.equalsIgnoreCase(paramname)) {
  108. setText(parameters[i].getValue());
  109. } else if (CASE_KEY.equalsIgnoreCase(paramname)) {
  110. setCasesensitive(Project.toBoolean(
  111. parameters[i].getValue()));
  112. } else if (WHITESPACE_KEY.equalsIgnoreCase(paramname)) {
  113. setIgnorewhitespace(Project.toBoolean(
  114. parameters[i].getValue()));
  115. } else {
  116. setError("Invalid parameter " + paramname);
  117. }
  118. }
  119. }
  120. }
  121. /**
  122. * Checks to make sure all settings are kosher. In this case, it
  123. * means that the pattern attribute has been set.
  124. *
  125. */
  126. public void verifySettings() {
  127. if (contains == null) {
  128. setError("The text attribute is required");
  129. }
  130. }
  131. /**
  132. * The heart of the matter. This is where the selector gets to decide
  133. * on the inclusion of a file in a particular fileset.
  134. *
  135. * @param basedir the base directory the scan is being done from
  136. * @param filename is the name of the file to check
  137. * @param file is a java.io.File object the selector can use
  138. * @return whether the file should be selected or not
  139. */
  140. public boolean isSelected(File basedir, String filename, File file) {
  141. // throw BuildException on error
  142. validate();
  143. if (file.isDirectory()) {
  144. return true;
  145. }
  146. String userstr = contains;
  147. if (!casesensitive) {
  148. userstr = contains.toLowerCase();
  149. }
  150. if (ignorewhitespace) {
  151. userstr = SelectorUtils.removeWhitespace(userstr);
  152. }
  153. BufferedReader in = null;
  154. try {
  155. in = new BufferedReader(new InputStreamReader(
  156. new FileInputStream(file)));
  157. String teststr = in.readLine();
  158. while (teststr != null) {
  159. if (!casesensitive) {
  160. teststr = teststr.toLowerCase();
  161. }
  162. if (ignorewhitespace) {
  163. teststr = SelectorUtils.removeWhitespace(teststr);
  164. }
  165. if (teststr.indexOf(userstr) > -1) {
  166. return true;
  167. }
  168. teststr = in.readLine();
  169. }
  170. return false;
  171. } catch (IOException ioe) {
  172. throw new BuildException("Could not read file " + filename);
  173. } finally {
  174. if (in != null) {
  175. try {
  176. in.close();
  177. } catch (Exception e) {
  178. throw new BuildException("Could not close file "
  179. + filename);
  180. }
  181. }
  182. }
  183. }
  184. }