mirror of
https://github.com/corda/corda.git
synced 2025-01-05 20:54:13 +00:00
Implement single quotes in MessageFormat
This commit is contained in:
parent
ed119938b0
commit
65ca5752da
@ -12,6 +12,24 @@ package java.text;
|
|||||||
|
|
||||||
import java.util.Locale;
|
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 {
|
public class MessageFormat extends Format {
|
||||||
private String pattern;
|
private String pattern;
|
||||||
private final Locale locale;
|
private final Locale locale;
|
||||||
@ -25,31 +43,61 @@ public class MessageFormat extends Format {
|
|||||||
this(pattern, Locale.getDefault());
|
this(pattern, Locale.getDefault());
|
||||||
}
|
}
|
||||||
|
|
||||||
public StringBuffer format(Object[] args, StringBuffer target,
|
public StringBuffer format(Object args[], StringBuffer target, FieldPosition pos) {
|
||||||
FieldPosition p)
|
int i=0;
|
||||||
{
|
int len=pattern.length();
|
||||||
// todo: handle other format substitutions and escapes, and make
|
|
||||||
// this more efficient:
|
while (i < len) {
|
||||||
String result = pattern;
|
char ch = pattern.charAt(i);
|
||||||
int length = args.length;
|
if (ch == '{') {
|
||||||
for (int i = 0; i < length; i++) {
|
// Param should be a number
|
||||||
result = result.replace("{" + i + "}", String.valueOf(args[i]));
|
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);
|
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) {
|
public void applyPattern(String pattern) {
|
||||||
this.pattern = pattern;
|
this.pattern = pattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String toPattern() {
|
||||||
|
return pattern;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
24
test/MessageFormatTest.java
Normal file
24
test/MessageFormatTest.java
Normal 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user