public static void parse()

in src/main/java/org/apache/log4j/pattern/PatternParser.java [244:407]


  public static void parse(
    final String pattern, final List patternConverters,
    final List formattingInfos, final Map converterRegistry, final Map rules) {
    if (pattern == null) {
      throw new NullPointerException("pattern");
    }

    StringBuffer currentLiteral = new StringBuffer(32);

    int patternLength = pattern.length();
    int state = LITERAL_STATE;
    char c;
    int i = 0;
    FormattingInfo formattingInfo = FormattingInfo.getDefault();

    while (i < patternLength) {
      c = pattern.charAt(i++);

      switch (state) {
      case LITERAL_STATE:

        // In literal state, the last char is always a literal.
        if (i == patternLength) {
          currentLiteral.append(c);

          continue;
        }

        if (c == ESCAPE_CHAR) {
          // peek at the next char.
          switch (pattern.charAt(i)) {
          case ESCAPE_CHAR:
            currentLiteral.append(c);
            i++; // move pointer

            break;

          default:

            if (currentLiteral.length() != 0) {
              patternConverters.add(
                new LiteralPatternConverter(currentLiteral.toString()));
              formattingInfos.add(FormattingInfo.getDefault());
            }

            currentLiteral.setLength(0);
            currentLiteral.append(c); // append %
            state = CONVERTER_STATE;
            formattingInfo = FormattingInfo.getDefault();
          }
        } else {
          currentLiteral.append(c);
        }

        break;

      case CONVERTER_STATE:
        currentLiteral.append(c);

        switch (c) {
        case '-':
          formattingInfo =
            new FormattingInfo(
              true, formattingInfo.getMinLength(),
              formattingInfo.getMaxLength());

          break;

        case '.':
          state = DOT_STATE;

          break;

        default:

          if ((c >= '0') && (c <= '9')) {
            formattingInfo =
              new FormattingInfo(
                formattingInfo.isLeftAligned(), c - '0',
                formattingInfo.getMaxLength());
            state = MIN_STATE;
          } else {
            i = finalizeConverter(
                c, pattern, i, currentLiteral, formattingInfo,
                converterRegistry, rules, patternConverters, formattingInfos);

            // Next pattern is assumed to be a literal.
            state = LITERAL_STATE;
            formattingInfo = FormattingInfo.getDefault();
            currentLiteral.setLength(0);
          }
        } // switch

        break;

      case MIN_STATE:
        currentLiteral.append(c);

        if ((c >= '0') && (c <= '9')) {
          formattingInfo =
            new FormattingInfo(
              formattingInfo.isLeftAligned(),
              (formattingInfo.getMinLength() * 10) + (c - '0'),
              formattingInfo.getMaxLength());
        } else if (c == '.') {
          state = DOT_STATE;
        } else {
          i = finalizeConverter(
              c, pattern, i, currentLiteral, formattingInfo,
              converterRegistry, rules, patternConverters, formattingInfos);
          state = LITERAL_STATE;
          formattingInfo = FormattingInfo.getDefault();
          currentLiteral.setLength(0);
        }

        break;

      case DOT_STATE:
        currentLiteral.append(c);

        if ((c >= '0') && (c <= '9')) {
          formattingInfo =
            new FormattingInfo(
              formattingInfo.isLeftAligned(), formattingInfo.getMinLength(),
              c - '0');
          state = MAX_STATE;
        } else {
            LogLog.error(
              "Error occured in position " + i
              + ".\n Was expecting digit, instead got char \"" + c + "\".");

          state = LITERAL_STATE;
        }

        break;

      case MAX_STATE:
        currentLiteral.append(c);

        if ((c >= '0') && (c <= '9')) {
          formattingInfo =
            new FormattingInfo(
              formattingInfo.isLeftAligned(), formattingInfo.getMinLength(),
              (formattingInfo.getMaxLength() * 10) + (c - '0'));
        } else {
          i = finalizeConverter(
              c, pattern, i, currentLiteral, formattingInfo,
              converterRegistry, rules, patternConverters, formattingInfos);
          state = LITERAL_STATE;
          formattingInfo = FormattingInfo.getDefault();
          currentLiteral.setLength(0);
        }

        break;
      } // switch
    }

    // while
    if (currentLiteral.length() != 0) {
      patternConverters.add(
        new LiteralPatternConverter(currentLiteral.toString()));
      formattingInfos.add(FormattingInfo.getDefault());
    }
  }