Exemplo n.º 1
0
  /**
   * Splits the input string into strings separated by the delimiter and put them into a Vector.
   *
   * @param delimiter splitting delimiter
   * @param input input string
   * @return returns a Vector containing the substrings of the input that occur between delimiter.
   *     Returns the Vector with original string as only the element when the delimiter is not found
   *     in the input string.
   * @exception throws a FrameworkException when either input/delimiter is null.
   */
  public static Vector split(String delimiter, String input) throws FrameworkException {
    if (input == null || delimiter == null) {
      throw new FrameworkException(
          "RegexUtils: split(): input or delimiter is null."
              + "input ="
              + input
              + ", "
              + "delimiter = "
              + delimiter);
    }

    Perl5Util util = new Perl5Util();
    Vector result = new Vector();

    // convert the delimiter to the perl5 format
    delimiter = makePerl5MatchPattern(delimiter);

    try {
      // Populate the vector argument with split strings
      util.split(result, delimiter, input, util.SPLIT_ALL);
    } catch (MalformedPerl5PatternException e) {
      throw new FrameworkException("RegexUtils: split():" + e.getMessage());
    }

    return result;
  }
  // Overridden Methods
  @Override
  protected void inspectContents(File file, String contents) {
    StringBuffer buf = new StringBuffer();

    // Split it into lines
    StringSplitter splitter = new StringSplitter(contents, getLineEnding());

    // Scan each line
    int totalMatchesForThisFile = 0;
    int lineCount = 1;
    while (splitter.hasMoreElements()) {
      String line = (String) splitter.nextElement();

      input = new PatternMatcherInput(line);
      try {
        while (util.match(regex, input)) {
          result = util.getMatch();
          totalMatchesForThisFile++;
          totalNumberOfMatches++;
          buf.append(
              "  line: " + lineCount + " position: " + result.beginOffset(0) + getLineEnding());
        }
      } catch (MalformedPerl5PatternException e) {
        System.out.println("MalformedPerl5PatternException: " + e.getMessage());
        System.out.println("Valid expression: [m]/pattern/[i][m][s][x]");
        return;
      }

      lineCount++;
    }

    // Output the results
    if (totalMatchesForThisFile > 0) {
      if (totalMatchesForThisFile == 1) {
        System.out.println(
            "Found " + totalMatchesForThisFile + " match in file: " + file.getPath());
      } else {
        System.out.println(
            "Found " + totalMatchesForThisFile + " matches in file: " + file.getPath());
      }
      System.out.print(buf.toString());
      System.out.println("");
    }
  }