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