diff --git a/classpath/java/text/MessageFormat.java b/classpath/java/text/MessageFormat.java
index 91cc977504..b69d960dfe 100644
--- a/classpath/java/text/MessageFormat.java
+++ b/classpath/java/text/MessageFormat.java
@@ -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).
+ *
Examples
+ *
+ * format | Args | Result |
+ * There are {0} grapes | six | There are six grapes |
+ * {2} + {1} = {0} | 5 2 3 | 3 + 2 = 5 |
+ * {0} and {0} and {0} | again | again and again and again |
+ * Joe''s age is {0}, not '{0}' | 30 | Joe's age is 30, not {0} |
+ *
+ */
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;
+ }
}
diff --git a/test/MessageFormatTest.java b/test/MessageFormatTest.java
new file mode 100644
index 0000000000..63914ee44e
--- /dev/null
+++ b/test/MessageFormatTest.java
@@ -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));
+ }
+
+}
\ No newline at end of file