| 44 | } |
| 45 | |
| 46 | public StringBuffer format(Object args[], StringBuffer target, FieldPosition pos) { |
| 47 | int i=0; |
| 48 | int len=pattern.length(); |
| 49 | |
| 50 | while (i < len) { |
| 51 | char ch = pattern.charAt(i); |
| 52 | if (ch == '{') { |
| 53 | // Param should be a number |
| 54 | int num=0; |
| 55 | while (i < (len-1)) { |
| 56 | i++; |
| 57 | ch = pattern.charAt(i); |
| 58 | if ((ch >= '0') && (ch <= '9')) { |
| 59 | num = num * 10 + (ch - '0'); |
| 60 | } else if (ch == '}') { |
| 61 | target.append((args[num] == null) ? "null" : args[num].toString()); |
| 62 | break; |
| 63 | } else { |
| 64 | throw new IllegalArgumentException("Character within {} isn't digit: " + ch); |
| 65 | } |
| 66 | } |
| 67 | } else if (ch == '\'') { |
| 68 | // Char is a literal string |
| 69 | i++; |
| 70 | ch = pattern.charAt(i); |
| 71 | if (ch == '\'') { |
| 72 | target.append('\''); |
| 73 | } else { |
| 74 | while (ch != '\'') { |
| 75 | target.append(ch); |
| 76 | i++; |
| 77 | ch = pattern.charAt(i); |
| 78 | } |
| 79 | } |
| 80 | } else { |
| 81 | target.append(ch); |
| 82 | } |
| 83 | i++; |
| 84 | } |
| 85 | return target; |
| 86 | } |
| 87 | |
| 88 | public static String format(String message, Object... args) { |
| 89 | return new MessageFormat(message).format(args, new StringBuffer(), new FieldPosition(0)).toString(); |