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