1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.log4j.helpers;
18
19
20 /***
21 * Formats messages according to very simple rules.
22 * See {@link #format(String,Object)} and
23 * {@link #format(String,Object,Object)} for more details.
24 *
25 * @author Ceki Gülcü
26 */
27 public final class MessageFormatter {
28 /***
29 * Private formatter since all methods and members are static.
30 */
31 private MessageFormatter() {
32 super();
33 }
34
35 /***
36 * Start of replacement block.
37 */
38 private static final char DELIM_START = '{';
39 /***
40 * End of replacement block.
41 */
42 private static final char DELIM_STOP = '}';
43
44 /***
45 * Performs single argument substitution for the 'messagePattern' passed as
46 * parameter.
47 * <p/>
48 * For example, <code>MessageFormatter.format("Hi {}.", "there");</code>
49 * will return the string "Hi there.".
50 * <p/>
51 * The {} pair is called the formatting element. It serves to designate the
52 * location where the argument needs to be inserted within the pattern.
53 *
54 * @param messagePattern
55 * The message pattern which will be parsed and formatted
56 * @param argument
57 * The argument to be inserted instead of the formatting element
58 * @return The formatted message
59 */
60 public static String format(final String messagePattern,
61 final Object argument) {
62 int j = messagePattern.indexOf(DELIM_START);
63 int len = messagePattern.length();
64 char escape = 'x';
65
66
67
68 if (j == -1 || (j + 1 == len)) {
69 return messagePattern;
70 } else {
71 char delimStop = messagePattern.charAt(j + 1);
72 if (j > 0) {
73 escape = messagePattern.charAt(j - 1);
74 }
75 if ((delimStop != DELIM_STOP) || (escape == '//')) {
76
77
78 return messagePattern;
79 } else {
80 StringBuffer sbuf = new StringBuffer(len + 20);
81 sbuf.append(messagePattern.substring(0, j));
82 sbuf.append(argument);
83 sbuf.append(messagePattern.substring(j + 2));
84 return sbuf.toString();
85 }
86 }
87 }
88
89 /***
90 * /**
91 * Performs a two argument substitution for the 'messagePattern' passed as
92 * parameter.
93 * <p/>
94 * For example, <code>MessageFormatter.format("Hi {}. My name is {}.",
95 * "there", "David");</code> will return the string
96 * "Hi there. My name is David.".
97 * <p/>
98 * The '{}' pair is called a formatting element. It serves to designate the
99 * location where the arguments need to be inserted within
100 * the message pattern.
101 *
102 * @param messagePattern
103 * The message pattern which will be parsed and formatted
104 * @param arg1
105 * The first argument to replace the first formatting element
106 * @param arg2
107 * The second argument to replace the second formatting element
108 * @return The formatted message
109 */
110 public static String format(final String messagePattern,
111 final Object arg1,
112 final Object arg2) {
113 int i = 0;
114 int len = messagePattern.length();
115
116 StringBuffer sbuf = new StringBuffer(messagePattern.length() + 50);
117
118 for (int l = 0; l < 2; l++) {
119 int j = messagePattern.indexOf(DELIM_START, i);
120
121 if (j == -1 || (j + 1 == len)) {
122
123 if (i == 0) {
124 return messagePattern;
125 } else {
126
127
128 sbuf.append(messagePattern.substring(i,
129 messagePattern.length()));
130 return sbuf.toString();
131 }
132 } else {
133 char delimStop = messagePattern.charAt(j + 1);
134 if ((delimStop != DELIM_STOP)) {
135
136 sbuf.append(messagePattern.substring(i,
137 messagePattern.length()));
138 return sbuf.toString();
139 }
140 sbuf.append(messagePattern.substring(i, j));
141 if (l == 0) {
142 sbuf.append(arg1);
143 } else {
144 sbuf.append(arg2);
145 }
146 i = j + 2;
147 }
148 }
149
150 sbuf.append(messagePattern.substring(i, messagePattern.length()));
151 return sbuf.toString();
152 }
153 }