in src/main/java/org/apache/log4j/pattern/ExtrasPatternParser.java [244:425]
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;
ExtrasFormattingInfo formattingInfo = ExtrasFormattingInfo.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(ExtrasFormattingInfo.getDefault());
}
currentLiteral.setLength(0);
currentLiteral.append(c); // append %
state = CONVERTER_STATE;
formattingInfo = ExtrasFormattingInfo.getDefault();
}
} else {
currentLiteral.append(c);
}
break;
case CONVERTER_STATE:
currentLiteral.append(c);
switch (c) {
case '-':
formattingInfo =
new ExtrasFormattingInfo(
true,
formattingInfo.isRightTruncated(),
formattingInfo.getMinLength(),
formattingInfo.getMaxLength());
break;
case '!':
formattingInfo =
new ExtrasFormattingInfo(
formattingInfo.isLeftAligned(),
true,
formattingInfo.getMinLength(),
formattingInfo.getMaxLength());
break;
case '.':
state = DOT_STATE;
break;
default:
if ((c >= '0') && (c <= '9')) {
formattingInfo =
new ExtrasFormattingInfo(
formattingInfo.isLeftAligned(),
formattingInfo.isRightTruncated(),
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 = ExtrasFormattingInfo.getDefault();
currentLiteral.setLength(0);
}
} // switch
break;
case MIN_STATE:
currentLiteral.append(c);
if ((c >= '0') && (c <= '9')) {
formattingInfo =
new ExtrasFormattingInfo(
formattingInfo.isLeftAligned(),
formattingInfo.isRightTruncated(),
(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 = ExtrasFormattingInfo.getDefault();
currentLiteral.setLength(0);
}
break;
case DOT_STATE:
currentLiteral.append(c);
if ((c >= '0') && (c <= '9')) {
formattingInfo =
new ExtrasFormattingInfo(
formattingInfo.isLeftAligned(),
formattingInfo.isRightTruncated(),
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 ExtrasFormattingInfo(
formattingInfo.isLeftAligned(),
formattingInfo.isRightTruncated(),
formattingInfo.getMinLength(),
(formattingInfo.getMaxLength() * 10) + (c - '0'));
} else {
i = finalizeConverter(
c, pattern, i, currentLiteral, formattingInfo,
converterRegistry, rules, patternConverters, formattingInfos);
state = LITERAL_STATE;
formattingInfo = ExtrasFormattingInfo.getDefault();
currentLiteral.setLength(0);
}
break;
} // switch
}
// while
if (currentLiteral.length() != 0) {
patternConverters.add(
new LiteralPatternConverter(currentLiteral.toString()));
formattingInfos.add(ExtrasFormattingInfo.getDefault());
}
}