@Test public void testInvalidPrintfFlags() { try { int unused = PARSER.parsePrintfTerm(null, 0, "%0s", 0, 1, 2); fail("expected ParseException"); } catch (ParseException expected) { assertThat(expected.getMessage()).contains("[%0s]"); } }
@Override public int parsePrintfTerm( MessageBuilder<?> builder, int index, String message, int termStart, int specStart, int formatStart) throws ParseException { int termEnd = formatStart + 1; char typeChar = message.charAt(formatStart); boolean isUpperCase = (typeChar & 0x20) == 0; FormatOptions options = FormatOptions.parse(message, specStart, formatStart, isUpperCase); Parameter parameter; FormatChar formatChar = FormatChar.of(typeChar); if (formatChar != null) { if (!options.areValidFor(formatChar)) { throw ParseException.withBounds("invalid format specifier", message, termStart, termEnd); } parameter = SimpleParameter.of(index, formatChar, options); } else if (typeChar == 't' || typeChar == 'T') { if (!options.validate(FLAG_LEFT_ALIGN | FLAG_UPPER_CASE, false)) { throw ParseException.withBounds( "invalid format specification", message, termStart, termEnd); } termEnd += 1; if (termEnd > message.length()) { throw ParseException.atPosition("truncated format specifier", message, termStart); } DateTimeFormat dateTimeFormat = DateTimeFormat.of(message.charAt(formatStart + 1)); if (dateTimeFormat == null) { throw ParseException.atPosition("illegal date/time conversion", message, formatStart + 1); } parameter = DateTimeParameter.of(dateTimeFormat, options, index); } else if (typeChar == 'h' || typeChar == 'H') { if (!options.validate(FLAG_LEFT_ALIGN | FLAG_UPPER_CASE, false)) { throw ParseException.withBounds( "invalid format specification", message, termStart, termEnd); } parameter = wrapHexParameter(options, index); } else { throw ParseException.withBounds( "invalid format specification", message, termStart, formatStart + 1); } builder.addParameter(termStart, termEnd, parameter); return termEnd; }
DefaultPrintfMessageParser extends PrintfMessageParser { @Override public int parsePrintfTerm( MessageBuilder<?> builder, int index, String message, int termStart, int specStart, int formatStart) throws ParseException { int termEnd = formatStart + 1; char typeChar = message.charAt(formatStart); boolean isUpperCase = (typeChar & 0x20) == 0; FormatOptions options = FormatOptions.parse(message, specStart, formatStart, isUpperCase); Parameter parameter; FormatChar formatChar = FormatChar.of(typeChar); if (formatChar != null) { if (!options.areValidFor(formatChar)) { throw ParseException.withBounds("invalid format specifier", message, termStart, termEnd); } parameter = SimpleParameter.of(index, formatChar, options); } else if (typeChar == 't' || typeChar == 'T') { if (!options.validate(FLAG_LEFT_ALIGN | FLAG_UPPER_CASE, false)) { throw ParseException.withBounds( "invalid format specification", message, termStart, termEnd); } termEnd += 1; if (termEnd > message.length()) { throw ParseException.atPosition("truncated format specifier", message, termStart); } DateTimeFormat dateTimeFormat = DateTimeFormat.of(message.charAt(formatStart + 1)); if (dateTimeFormat == null) { throw ParseException.atPosition("illegal date/time conversion", message, formatStart + 1); } parameter = DateTimeParameter.of(dateTimeFormat, options, index); } else if (typeChar == 'h' || typeChar == 'H') { if (!options.validate(FLAG_LEFT_ALIGN | FLAG_UPPER_CASE, false)) { throw ParseException.withBounds( "invalid format specification", message, termStart, termEnd); } parameter = wrapHexParameter(options, index); } else { throw ParseException.withBounds( "invalid format specification", message, termStart, formatStart + 1); } builder.addParameter(termStart, termEnd, parameter); return termEnd; } }
DefaultPrintfMessageParser extends PrintfMessageParser { @Override public int parsePrintfTerm( MessageBuilder<?> builder, int index, String message, int termStart, int specStart, int formatStart) throws ParseException { int termEnd = formatStart + 1; char typeChar = message.charAt(formatStart); boolean isUpperCase = (typeChar & 0x20) == 0; FormatOptions options = FormatOptions.parse(message, specStart, formatStart, isUpperCase); Parameter parameter; FormatChar formatChar = FormatChar.of(typeChar); if (formatChar != null) { if (!options.areValidFor(formatChar)) { throw ParseException.withBounds("invalid format specifier", message, termStart, termEnd); } parameter = SimpleParameter.of(index, formatChar, options); } else if (typeChar == 't' || typeChar == 'T') { if (!options.validate(FLAG_LEFT_ALIGN | FLAG_UPPER_CASE, false)) { throw ParseException.withBounds( "invalid format specification", message, termStart, termEnd); } termEnd += 1; if (termEnd > message.length()) { throw ParseException.atPosition("truncated format specifier", message, termStart); } DateTimeFormat dateTimeFormat = DateTimeFormat.of(message.charAt(formatStart + 1)); if (dateTimeFormat == null) { throw ParseException.atPosition("illegal date/time conversion", message, formatStart + 1); } parameter = DateTimeParameter.of(dateTimeFormat, options, index); } else if (typeChar == 'h' || typeChar == 'H') { if (!options.validate(FLAG_LEFT_ALIGN | FLAG_UPPER_CASE, false)) { throw ParseException.withBounds( "invalid format specification", message, termStart, termEnd); } parameter = wrapHexParameter(options, index); } else { throw ParseException.withBounds( "invalid format specification", message, termStart, formatStart + 1); } builder.addParameter(termStart, termEnd, parameter); return termEnd; } private  DefaultPrintfMessageParser(); }
DefaultPrintfMessageParser extends PrintfMessageParser { @Override public int parsePrintfTerm( MessageBuilder<?> builder, int index, String message, int termStart, int specStart, int formatStart) throws ParseException { int termEnd = formatStart + 1; char typeChar = message.charAt(formatStart); boolean isUpperCase = (typeChar & 0x20) == 0; FormatOptions options = FormatOptions.parse(message, specStart, formatStart, isUpperCase); Parameter parameter; FormatChar formatChar = FormatChar.of(typeChar); if (formatChar != null) { if (!options.areValidFor(formatChar)) { throw ParseException.withBounds("invalid format specifier", message, termStart, termEnd); } parameter = SimpleParameter.of(index, formatChar, options); } else if (typeChar == 't' || typeChar == 'T') { if (!options.validate(FLAG_LEFT_ALIGN | FLAG_UPPER_CASE, false)) { throw ParseException.withBounds( "invalid format specification", message, termStart, termEnd); } termEnd += 1; if (termEnd > message.length()) { throw ParseException.atPosition("truncated format specifier", message, termStart); } DateTimeFormat dateTimeFormat = DateTimeFormat.of(message.charAt(formatStart + 1)); if (dateTimeFormat == null) { throw ParseException.atPosition("illegal date/time conversion", message, formatStart + 1); } parameter = DateTimeParameter.of(dateTimeFormat, options, index); } else if (typeChar == 'h' || typeChar == 'H') { if (!options.validate(FLAG_LEFT_ALIGN | FLAG_UPPER_CASE, false)) { throw ParseException.withBounds( "invalid format specification", message, termStart, termEnd); } parameter = wrapHexParameter(options, index); } else { throw ParseException.withBounds( "invalid format specification", message, termStart, formatStart + 1); } builder.addParameter(termStart, termEnd, parameter); return termEnd; } private  DefaultPrintfMessageParser(); static PrintfMessageParser getInstance(); @Override int parsePrintfTerm(
      MessageBuilder<?> builder,
      int index,
      String message,
      int termStart,
      int specStart,
      int formatStart); }
DefaultPrintfMessageParser extends PrintfMessageParser { @Override public int parsePrintfTerm( MessageBuilder<?> builder, int index, String message, int termStart, int specStart, int formatStart) throws ParseException { int termEnd = formatStart + 1; char typeChar = message.charAt(formatStart); boolean isUpperCase = (typeChar & 0x20) == 0; FormatOptions options = FormatOptions.parse(message, specStart, formatStart, isUpperCase); Parameter parameter; FormatChar formatChar = FormatChar.of(typeChar); if (formatChar != null) { if (!options.areValidFor(formatChar)) { throw ParseException.withBounds("invalid format specifier", message, termStart, termEnd); } parameter = SimpleParameter.of(index, formatChar, options); } else if (typeChar == 't' || typeChar == 'T') { if (!options.validate(FLAG_LEFT_ALIGN | FLAG_UPPER_CASE, false)) { throw ParseException.withBounds( "invalid format specification", message, termStart, termEnd); } termEnd += 1; if (termEnd > message.length()) { throw ParseException.atPosition("truncated format specifier", message, termStart); } DateTimeFormat dateTimeFormat = DateTimeFormat.of(message.charAt(formatStart + 1)); if (dateTimeFormat == null) { throw ParseException.atPosition("illegal date/time conversion", message, formatStart + 1); } parameter = DateTimeParameter.of(dateTimeFormat, options, index); } else if (typeChar == 'h' || typeChar == 'H') { if (!options.validate(FLAG_LEFT_ALIGN | FLAG_UPPER_CASE, false)) { throw ParseException.withBounds( "invalid format specification", message, termStart, termEnd); } parameter = wrapHexParameter(options, index); } else { throw ParseException.withBounds( "invalid format specification", message, termStart, formatStart + 1); } builder.addParameter(termStart, termEnd, parameter); return termEnd; } private  DefaultPrintfMessageParser(); static PrintfMessageParser getInstance(); @Override int parsePrintfTerm(
      MessageBuilder<?> builder,
      int index,
      String message,
      int termStart,
      int specStart,
      int formatStart);  }