1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.util; |
12 | |
|
13 | |
import org.mule.MuleServer; |
14 | |
import org.mule.api.MuleRuntimeException; |
15 | |
import org.mule.config.i18n.CoreMessages; |
16 | |
|
17 | |
import java.io.UnsupportedEncodingException; |
18 | |
import java.text.MessageFormat; |
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Arrays; |
21 | |
import java.util.Collection; |
22 | |
import java.util.List; |
23 | |
import java.util.Map; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public final class StringMessageUtils |
30 | |
{ |
31 | |
|
32 | |
public static final int MAX_ELEMENTS = 50; |
33 | |
public static final int DEFAULT_MESSAGE_WIDTH = 80; |
34 | |
|
35 | |
|
36 | |
private StringMessageUtils () |
37 | 0 | { |
38 | |
|
39 | 0 | } |
40 | |
|
41 | |
public static String getFormattedMessage(String msg, Object[] arguments) |
42 | |
{ |
43 | 6 | if (arguments != null) |
44 | |
{ |
45 | 10 | for (int i = 0; i < arguments.length; i++) |
46 | |
{ |
47 | 6 | arguments[i] = toString(arguments[i]); |
48 | |
} |
49 | |
} |
50 | 6 | return MessageFormat.format(msg, arguments); |
51 | |
} |
52 | |
|
53 | |
public static String getBoilerPlate(String message) |
54 | |
{ |
55 | 0 | return getBoilerPlate(message, '*', DEFAULT_MESSAGE_WIDTH); |
56 | |
} |
57 | |
|
58 | |
public static String getBoilerPlate(String message, char c, int maxlength) |
59 | |
{ |
60 | 1166 | return getBoilerPlate(new ArrayList(Arrays.asList(new String[]{message})), c, maxlength); |
61 | |
} |
62 | |
|
63 | |
public static String getBoilerPlate(List messages, char c, int maxlength) |
64 | |
{ |
65 | |
int size; |
66 | 1176 | StringBuffer buf = new StringBuffer(messages.size() * maxlength); |
67 | 1176 | int trimLength = maxlength - (c == ' ' ? 2 : 4); |
68 | |
|
69 | 3136 | for (int i = 0; i < messages.size(); i++) |
70 | |
{ |
71 | 1960 | size = messages.get(i).toString().length(); |
72 | 1960 | if (size > trimLength) |
73 | |
{ |
74 | 750 | String temp = messages.get(i).toString(); |
75 | 750 | int k = i; |
76 | |
int x; |
77 | |
int len; |
78 | 750 | messages.remove(i); |
79 | 2282 | while (temp.length() > 0) |
80 | |
{ |
81 | 1532 | len = (trimLength <= temp.length() ? trimLength : temp.length()); |
82 | 1532 | String msg = temp.substring(0, len); |
83 | 1532 | x = msg.indexOf(SystemUtils.LINE_SEPARATOR); |
84 | |
|
85 | 1532 | if (x > -1) |
86 | |
{ |
87 | 0 | msg = msg.substring(0, x); |
88 | 0 | len = x + 1; |
89 | |
} |
90 | |
else |
91 | |
{ |
92 | 1532 | x = msg.lastIndexOf(' '); |
93 | 1532 | if (x > -1 && len == trimLength) |
94 | |
{ |
95 | 772 | msg = msg.substring(0, x); |
96 | 772 | len = x + 1; |
97 | |
} |
98 | |
} |
99 | 1532 | if (msg.startsWith(" ")) |
100 | |
{ |
101 | 0 | msg = msg.substring(1); |
102 | |
} |
103 | |
|
104 | 1532 | temp = temp.substring(len); |
105 | 1532 | messages.add(k, msg); |
106 | 1532 | k++; |
107 | 1532 | } |
108 | |
} |
109 | |
} |
110 | |
|
111 | 1176 | buf.append(SystemUtils.LINE_SEPARATOR); |
112 | 1176 | if (c != ' ') |
113 | |
{ |
114 | 1176 | buf.append(StringUtils.repeat(c, maxlength)); |
115 | |
} |
116 | |
|
117 | 3136 | for (int i = 0; i < messages.size(); i++) |
118 | |
{ |
119 | 1960 | buf.append(SystemUtils.LINE_SEPARATOR); |
120 | 1960 | if (c != ' ') |
121 | |
{ |
122 | 1960 | buf.append(c); |
123 | |
} |
124 | 1960 | buf.append(" "); |
125 | 1960 | buf.append(messages.get(i)); |
126 | |
|
127 | 1960 | String osEncoding = CharSetUtils.defaultCharsetName(); |
128 | |
int padding; |
129 | |
try |
130 | |
{ |
131 | 1960 | padding = trimLength - messages.get(i).toString().getBytes(osEncoding).length; |
132 | |
} |
133 | 0 | catch (UnsupportedEncodingException ueex) |
134 | |
{ |
135 | 0 | throw new MuleRuntimeException(CoreMessages.failedToConvertStringUsingEncoding(osEncoding), ueex); |
136 | 1960 | } |
137 | 1960 | if (padding > 0) |
138 | |
{ |
139 | 1898 | buf.append(StringUtils.repeat(' ', padding)); |
140 | |
} |
141 | 1960 | buf.append(' '); |
142 | 1960 | if (c != ' ') |
143 | |
{ |
144 | 1960 | buf.append(c); |
145 | |
} |
146 | |
} |
147 | 1176 | buf.append(SystemUtils.LINE_SEPARATOR); |
148 | 1176 | if (c != ' ') |
149 | |
{ |
150 | 1176 | buf.append(StringUtils.repeat(c, maxlength)); |
151 | |
} |
152 | 1176 | return buf.toString(); |
153 | |
} |
154 | |
|
155 | |
public static String truncate(String message, int length, boolean includeCount) |
156 | |
{ |
157 | 6 | if (message == null) |
158 | |
{ |
159 | 0 | return null; |
160 | |
} |
161 | 6 | if (message.length() <= length) |
162 | |
{ |
163 | 2 | return message; |
164 | |
} |
165 | 4 | String result = message.substring(0, length) + "..."; |
166 | 4 | if (includeCount) |
167 | |
{ |
168 | 2 | result += "[" + length + " of " + message.length() + "]"; |
169 | |
} |
170 | 4 | return result; |
171 | |
} |
172 | |
|
173 | |
public static byte[] getBytes(String string) |
174 | |
{ |
175 | |
try |
176 | |
{ |
177 | 4 | return string.getBytes(MuleServer.getMuleContext().getConfiguration().getDefaultEncoding()); |
178 | |
} |
179 | 0 | catch (UnsupportedEncodingException e) |
180 | |
{ |
181 | |
|
182 | 0 | return null; |
183 | |
} |
184 | |
} |
185 | |
|
186 | |
public static String getString(byte[] bytes, String encoding) |
187 | |
{ |
188 | |
try |
189 | |
{ |
190 | 0 | return new String(bytes, encoding); |
191 | |
} |
192 | 0 | catch (UnsupportedEncodingException e) |
193 | |
{ |
194 | |
|
195 | 0 | return null; |
196 | |
} |
197 | |
} |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
public static String toString(Object o) |
205 | |
{ |
206 | 198 | if (o == null) |
207 | |
{ |
208 | 0 | return "null"; |
209 | |
} |
210 | 198 | else if (o instanceof Class) |
211 | |
{ |
212 | 12 | return ((Class) o).getName(); |
213 | |
} |
214 | 186 | else if (o instanceof Map) |
215 | |
{ |
216 | 4 | return MapUtils.toString((Map) o, false); |
217 | |
} |
218 | 182 | else if (o.getClass().isArray()) |
219 | |
{ |
220 | 54 | return ArrayUtils.toString(o, MAX_ELEMENTS); |
221 | |
} |
222 | 128 | else if (o instanceof Collection) |
223 | |
{ |
224 | 46 | return CollectionUtils.toString((Collection) o, MAX_ELEMENTS); |
225 | |
} |
226 | |
else |
227 | |
{ |
228 | 82 | return o.toString(); |
229 | |
} |
230 | |
} |
231 | |
|
232 | |
} |