/**
   * the regular expression pattern to match in the file(s); required if no nested <regexp> is
   * used
   *
   * @param match the match attribute.
   */
  public void setMatch(String match) {
    if (regex != null) {
      throw new BuildException("Only one regular expression is allowed");
    }

    regex = new RegularExpression();
    regex.setPattern(match);
  }
  /**
   * 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());
      }
    }
  }