/**
   * Invoke a regular expression (r) on a string (input) using substitutions (s) for a matching
   * regex.
   *
   * @param r a regular expression
   * @param s a Substitution
   * @param input the string to do the replacement on
   * @param options The options for the regular expression
   * @return the replacement result
   */
  protected String doReplace(RegularExpression r, Substitution s, String input, int options) {
    String res = input;
    Regexp regexp = r.getRegexp(getProject());

    if (regexp.matches(input, options)) {
      log("Found match; substituting", Project.MSG_DEBUG);
      res = regexp.substitute(input, s.getExpression(getProject()), options);
    }

    return res;
  }
  /**
   * Tests a regular expression against each line of text in a Resource.
   *
   * @param r the Resource to check.
   * @return whether the Resource is selected or not
   */
  public boolean isSelected(Resource r) {
    String teststr = null;
    BufferedReader in = null;

    // throw BuildException on error

    validate();

    if (r.isDirectory()) {
      return true;
    }

    if (myRegExp == null) {
      myRegExp = new RegularExpression();
      myRegExp.setPattern(userProvidedExpression);
      myExpression = myRegExp.getRegexp(getProject());
    }

    try {
      in = new BufferedReader(new InputStreamReader(r.getInputStream()));
    } catch (Exception e) {
      throw new BuildException("Could not get InputStream from " + r.toLongString(), e);
    }
    try {
      teststr = in.readLine();

      while (teststr != null) {

        if (myExpression.matches(
            teststr, RegexpUtil.asOptions(caseSensitive, multiLine, singleLine))) {
          return true;
        }
        teststr = in.readLine();
      }

      return false;
    } catch (IOException ioe) {
      throw new BuildException("Could not read " + r.toLongString());
    } finally {
      try {
        in.close();
      } catch (Exception e) {
        throw new BuildException("Could not close " + r.toLongString());
      }
    }
  }