Exemplo n.º 1
0
  /**
   * This function locates the first match on the begin token and replace all the pattern with the
   * replacement the area after the begin token is located.
   *
   * @param pattern pattern string to match
   * @param input input string
   * @param replacement replacement string
   * @param beginToken begin token string
   * @return processed string. If there's no match on the begin token, it returns the original input
   *     by default.
   * @exception throws a FrameworkException when the input/pattern/replacement is null.
   */
  public static String replaceAllWithBeginToken(
      String pattern, String input, String replacement, String beginToken)
      throws FrameworkException {
    if (input == null || pattern == null || replacement == null) {
      throw new FrameworkException(
          "RegexUtils: replaceAllWithBeginToken: input or pattern or replacement is null."
              + "\ninput = "
              + input
              + "\npattern = "
              + pattern
              + "\nreplacement = "
              + replacement);
    }

    Perl5Util util = new Perl5Util();

    // default return value
    String result = input;

    // conversion to the valid perl5 pattern regex
    String formatPattern = makePerl5MatchPattern(pattern);

    // if the beginToken is null, just do the regular replaceAll
    if (beginToken == null) {
      result = replaceAll(formatPattern, input, replacement);
    }
    // find if there is begin token in the input string
    else if (util.match(beginToken, input)) {
      // pre has the string before the match and the mathced pattern
      String pre = input.substring(0, util.endOffset(0));

      // get the rest of the string after the begin token is found
      input = input.substring(util.endOffset(0));

      result = replaceAll(pattern, input, replacement);
      result = pre + result;
    }

    return result;
  }
Exemplo n.º 2
0
  /**
   * This function substitues ALL the occurence of the pattern in the input with the replacement
   * passed in. The substitution is done only in the range bound by the begin token and the end
   * token.
   *
   * @param pattern pattern string to match
   * @param input input string
   * @param replacement replacement string to replace with
   * @param beginToken begin boundary token
   * @param endToken end boundary token
   * @param beginTokens[] an array of beginTokens - this is used for negative look ahead where there
   *     are other begin tokens to be recognized.
   * @return returns processed string if both beginToken and the endToken are successfully found,
   *     returns the unproessed original input otherwise.
   * @exception throws a FrameworkException when either pattern/input/replacement is null.
   */
  public static String replaceAll(
      String pattern,
      String input,
      String replacement,
      String beginToken,
      String endToken,
      String beginTokens[])
      throws FrameworkException {
    if ((pattern == null) || (replacement == null) || (input == null)) {
      Debug.log(
          Debug.ALL_ERRORS,
          "RegexUtils: replaceAll(): pattern or replacement or input is null. "
              + "\npattern = "
              + pattern
              + "\ninput = "
              + input
              + "\nreplacement = "
              + replacement);
      throw new FrameworkException(
          "RegexUtils: replaceAll(): pattern or replacement or input is null.");
    }

    Perl5Util util = new Perl5Util();
    StringBuffer resultBuffer = new StringBuffer();

    if ((beginToken == null) || (endToken == null)) {
      // either beginToken or endToken cannot be null, however both can be null.
      throw new FrameworkException(
          "RegexUtils: replaceAll(): Either begin or end token is null. BeginToken = "
              + beginToken
              + ", "
              + "endToken = "
              + endToken);
    } else // do pattern match
    {
      // making the Perl5 regular expression format pattern
      String begin = makePerl5MatchPattern(beginToken);
      String end = makePerl5MatchPattern(endToken);
      boolean beginTokenMatch = true;

      // if begin token found
      while (beginTokenMatch) {

        // check the input for each iteration. when there is no input, break out of the loop.
        if (input == null) break;

        beginTokenMatch = util.match(begin, input);

        if (beginTokenMatch) {
          String negativeLookAheadPattern =
              makePerl5MatchPatternNegativeLookAhead(pattern, replacement);
          int beginOffsetForBeginToken = util.beginOffset(0);
          int endOffsetForBeginToken = util.endOffset(0);

          // the input after the begin token
          String subInput = input.substring(endOffsetForBeginToken);

          // if end token passed in was an empty string
          if (endToken.equals("")) {
            String result = null;
            try {
              result = replaceAllWithBeginToken(pattern, input, replacement, beginToken);
            } catch (FrameworkException e) {
              Debug.log(Debug.ALL_ERRORS, "RegexUtils: replaceAll() failed." + e.getMessage());
            }
            return result;
          } else if (util.match(end, subInput)) // endToken found
          {

            // begin offset for the end token relative to the input not subInput
            int beginOffsetForEndToken = endOffsetForBeginToken + util.beginOffset(0);
            int endOffsetForEndToken = endOffsetForBeginToken + util.endOffset(0);

            // pre is the string before the beginToken and the beginToken
            String pre = input.substring(0, endOffsetForBeginToken);
            resultBuffer.append(pre);

            // current is the string between the beginToken and the endToken
            String current = input.substring(endOffsetForBeginToken, beginOffsetForEndToken);

            // theRest is the rest of the input string after the endToken
            String theRest = input.substring(endOffsetForEndToken);

            // current is the string between begin token and the endtoken
            current = skipOrphanedBeginToken(begin, current, resultBuffer);

            if (isOtherBeginTokenThere(beginToken, current, beginTokens) == false) {
              current = replaceAll(pattern, current, replacement);
            } // isOtherBeginTokenThere

            resultBuffer.append(current);
            resultBuffer.append(input.substring(beginOffsetForEndToken, endOffsetForEndToken));
            input = theRest;
          } else // endToken not found
          {
            resultBuffer.append(input);
            break;
          }
        } // if beginToken found
        else // beginToken not found
        {
          resultBuffer.append(input);
          break;
        }
      } // while begin token found
    } // else do pattern match

    return resultBuffer.toString();
  }