1. /*
  2. * Copyright 2003-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.types.Parameter;
  25. import org.apache.tools.ant.types.RegularExpression;
  26. import org.apache.tools.ant.util.regexp.Regexp;
  27. /**
  28. * Selector that filters files based on a regular expression.
  29. *
  30. * @since Ant 1.6
  31. */
  32. public class ContainsRegexpSelector extends BaseExtendSelector {
  33. private String userProvidedExpression = null;
  34. private RegularExpression myRegExp = null;
  35. private Regexp myExpression = null;
  36. /** Key to used for parameterized custom selector */
  37. public static final String EXPRESSION_KEY = "expression";
  38. /**
  39. * Creates a new <code>ContainsRegexpSelector</code> instance.
  40. */
  41. public ContainsRegexpSelector() {
  42. }
  43. /**
  44. * @return a string describing this object
  45. */
  46. public String toString() {
  47. StringBuffer buf = new StringBuffer(
  48. "{containsregexpselector expression: ");
  49. buf.append(userProvidedExpression);
  50. buf.append("}");
  51. return buf.toString();
  52. }
  53. /**
  54. * The regular expression used to search the file.
  55. *
  56. * @param theexpression this must match a line in the file to be selected.
  57. */
  58. public void setExpression(String theexpression) {
  59. this.userProvidedExpression = theexpression;
  60. }
  61. /**
  62. * When using this as a custom selector, this method will be called.
  63. * It translates each parameter into the appropriate setXXX() call.
  64. *
  65. * @param parameters the complete set of parameters for this selector
  66. */
  67. public void setParameters(Parameter[] parameters) {
  68. super.setParameters(parameters);
  69. if (parameters != null) {
  70. for (int i = 0; i < parameters.length; i++) {
  71. String paramname = parameters[i].getName();
  72. if (EXPRESSION_KEY.equalsIgnoreCase(paramname)) {
  73. setExpression(parameters[i].getValue());
  74. } else {
  75. setError("Invalid parameter " + paramname);
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * Checks that an expression was specified.
  82. *
  83. */
  84. public void verifySettings() {
  85. if (userProvidedExpression == null) {
  86. setError("The expression attribute is required");
  87. }
  88. }
  89. /**
  90. * Tests a regular expression against each line of text in the file.
  91. *
  92. * @param basedir the base directory the scan is being done from
  93. * @param filename is the name of the file to check
  94. * @param file is a java.io.File object the selector can use
  95. * @return whether the file should be selected or not
  96. */
  97. public boolean isSelected(File basedir, String filename, File file) {
  98. String teststr = null;
  99. BufferedReader in = null;
  100. // throw BuildException on error
  101. validate();
  102. if (file.isDirectory()) {
  103. return true;
  104. }
  105. if (myRegExp == null) {
  106. myRegExp = new RegularExpression();
  107. myRegExp.setPattern(userProvidedExpression);
  108. myExpression = myRegExp.getRegexp(getProject());
  109. }
  110. try {
  111. in = new BufferedReader(new InputStreamReader(
  112. new FileInputStream(file)));
  113. teststr = in.readLine();
  114. while (teststr != null) {
  115. if (myExpression.matches(teststr)) {
  116. return true;
  117. }
  118. teststr = in.readLine();
  119. }
  120. return false;
  121. } catch (IOException ioe) {
  122. throw new BuildException("Could not read file " + filename);
  123. } finally {
  124. if (in != null) {
  125. try {
  126. in.close();
  127. } catch (Exception e) {
  128. throw new BuildException("Could not close file "
  129. + filename);
  130. }
  131. }
  132. }
  133. }
  134. }