final GwtLocale locale, final ArgumentChunk listArg, final String val0,
final String val1, final JType elemType, final boolean isSafeHtml,
String listPattern, final boolean formatSecond, final Parameters params)
throws UnableToCompleteException {
final StringBuilder buf = new StringBuilder();
final StringGenerator gen = StringGenerator.create(buf, isSafeHtml);
try {
List<TemplateChunk> chunks = MessageFormatParser.parse(listPattern);
for (TemplateChunk chunk : chunks) {
chunk.accept(new DefaultTemplateChunkVisitor() {
@Override
public void visit(ArgumentChunk argChunk) throws UnableToCompleteException {
// The {0} argument in the list pattern always needs formatting,
// but the {1} argument is the part of the list already rendered
// (in either String of SafeHtml form) unless formatSecond is true.
if (argChunk.getArgumentNumber() == 0 || formatSecond) {
formatArg(logger, locale, gen, listArg,
argChunk.getArgumentNumber() == 0 ? val0 : val1, elemType,
params);
} else {
gen.appendExpression(val1, isSafeHtml, false, false);
}
}
@Override
public void visit(StringChunk stringChunk) {
gen.appendStringLiteral(stringChunk.getString());
}
});
}
} catch (ParseException e) {
logger.log(TreeLogger.ERROR,
"Internal error: can't parse list pattern '" + listPattern
+ "' for locale " + locale, e);
throw new UnableToCompleteException();
}
gen.completeString();
return buf;
}