Implement single quotes in MessageFormat

This commit is contained in:
Joshua Warner 2014-01-27 09:44:15 -07:00
parent ed119938b0
commit 65ca5752da
2 changed files with 90 additions and 18 deletions

View File

@ -12,6 +12,24 @@ package java.text;
import java.util.Locale;
/**
* A minimalist Java message formatter.
* The string is a sequence of letters, copied verbatim unless the letter
* is "{" or "'". If the letter is a "{", this begins a parameter, which
* is a base-10 number. {0} will be replaced by the first argument to
* format, {1} is the second argument, and so on.
* If the letter is a single tick ("'"), then all characters
* until the mathcing tick are outputted verbatim(this is useful for escaping
* the { character).
* <h3>Examples</h3>
* <table>
* <tr><th>format</th><th>Args</th><th>Result</th></tr>
* <tr><td>There are {0} grapes</td><td>six</td><td>There are six grapes</td></tr>
* <tr><td>{2} + {1} = {0}</td><td>5 2 3</td><td>3 + 2 = 5</td></tr>
* <tr><td>{0} and {0} and {0}</td><td>again</td><td>again and again and again</td></tr>
* <tr><td>Joe''s age is {0}, not '{0}'</td><td>30</td><td>Joe's age is 30, not {0}</td></tr>
* </table>
*/
public class MessageFormat extends Format {
private String pattern;
private final Locale locale;
@ -25,31 +43,61 @@ public class MessageFormat extends Format {
this(pattern, Locale.getDefault());
}
public StringBuffer format(Object[] args, StringBuffer target,
FieldPosition p)
{
// todo: handle other format substitutions and escapes, and make
// this more efficient:
String result = pattern;
int length = args.length;
for (int i = 0; i < length; i++) {
result = result.replace("{" + i + "}", String.valueOf(args[i]));
public StringBuffer format(Object args[], StringBuffer target, FieldPosition pos) {
int i=0;
int len=pattern.length();
while (i < len) {
char ch = pattern.charAt(i);
if (ch == '{') {
// Param should be a number
int num=0;
while (i < (len-1)) {
i++;
ch = pattern.charAt(i);
if ((ch >= '0') && (ch <= '9')) {
num = num * 10 + (ch - '0');
} else if (ch == '}') {
target.append((args[num] == null) ? "null" : args[num].toString());
break;
} else {
throw new IllegalArgumentException("Character within {} isn't digit: " + ch);
}
}
} else if (ch == '\'') {
// Char is a literal string
i++;
ch = pattern.charAt(i);
if (ch == '\'') {
target.append('\'');
} else {
while (ch != '\'') {
target.append(ch);
i++;
ch = pattern.charAt(i);
}
}
} else {
target.append(ch);
}
i++;
}
return target.append(result);
return target;
}
public StringBuffer format(Object args, StringBuffer target, FieldPosition p)
{
public static String format(String message, Object... args) {
return new MessageFormat(message).format(args, new StringBuffer(), new FieldPosition(0)).toString();
}
public StringBuffer format(Object args, StringBuffer target, FieldPosition p) {
return format((Object[]) args, target, p);
}
public static String format(String pattern, Object ... args) {
return new MessageFormat
(pattern).format(args, new StringBuffer(), new FieldPosition(0))
.toString();
}
public void applyPattern(String pattern) {
this.pattern = pattern;
}
public String toPattern() {
return pattern;
}
}

View File

@ -0,0 +1,24 @@
import java.text.MessageFormat;
public class MessageFormatTest {
private static void assertEquals(Object a, Object b) {
if(!a.equals(b)) {
throw new RuntimeException("[" + a + "] != [" + b + "]");
}
}
public static void main(String[] args) {
assertEquals("Hi there", MessageFormat.format("Hi there", "a"));
assertEquals("Hi there", MessageFormat.format("Hi {0}here", "t"));
assertEquals("Hi a!a!a", MessageFormat.format("Hi {0}!{0}!{0}", "a"));
assertEquals("Hi There", MessageFormat.format("{1} {0}", "There", "Hi"));
assertEquals("6 There 4", MessageFormat.format("{1} {2} {0}", 4, 6, "There"));
assertEquals("Zero and {0} aren't the same", MessageFormat.format("{0} and '{0}' aren''t the same","Zero"));
assertEquals("There are six grapes", MessageFormat.format("There are {0} grapes", "six"));
assertEquals("3 + 2 = 5", MessageFormat.format("{2} + {1} = {0}", 5, 2, 3));
assertEquals("again and again and again", MessageFormat.format("{0} and {0} and {0}", "again"));
assertEquals("Joe's age is 30, not {0}", MessageFormat.format("Joe''s age is {0}, not '{0}'", 30));
}
}