KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > StringMessageUtils


1 /*
2  * $Id: StringMessageUtils.java 4259 2006-12-14 03:12:07Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.util;
12
13 import java.io.UnsupportedEncodingException JavaDoc;
14 import java.text.MessageFormat JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.commons.lang.SystemUtils;
22 import org.mule.MuleRuntimeException;
23 import org.mule.config.MuleProperties;
24 import org.mule.config.i18n.CoreMessageConstants;
25 import org.mule.config.i18n.Message;
26
27 /**
28  * <code>StringMessageHelper</code> contains some useful methods for formatting
29  * message strings for logging or exceptions.
30  */

31 // @ThreadSafe
32
public class StringMessageUtils
33 {
34     // Character encoding properties
35
public static final String JavaDoc DEFAULT_ENCODING = "UTF-8";
36     public static final String JavaDoc DEFAULT_OS_ENCODING = System.getProperty("file.encoding");
37
38     // The maximum number of Collection and Array elements used for messages
39
public static final int MAX_ELEMENTS = 50;
40
41     public static String JavaDoc getFormattedMessage(String JavaDoc msg, Object JavaDoc[] arguments)
42     {
43         if (arguments != null)
44         {
45             for (int i = 0; i < arguments.length; i++)
46             {
47                 arguments[i] = toString(arguments[i]);
48             }
49         }
50         return MessageFormat.format(msg, arguments);
51     }
52
53     public static String JavaDoc getBoilerPlate(String JavaDoc message)
54     {
55         return getBoilerPlate(message, '*', 80);
56     }
57
58     public static String JavaDoc getBoilerPlate(String JavaDoc message, char c, int maxlength)
59     {
60         return getBoilerPlate(new ArrayList JavaDoc(Arrays.asList(new String JavaDoc[]{message})), c, maxlength);
61     }
62
63     public static String JavaDoc getBoilerPlate(List JavaDoc messages, char c, int maxlength)
64     {
65         int size;
66         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(messages.size() * maxlength);
67         int trimLength = maxlength - (c == ' ' ? 2 : 4);
68
69         for (int i = 0; i < messages.size(); i++)
70         {
71             size = messages.get(i).toString().length();
72             if (size > trimLength)
73             {
74                 String JavaDoc temp = messages.get(i).toString();
75                 int k = i;
76                 int x;
77                 int len;
78                 messages.remove(i);
79                 while (temp.length() > 0)
80                 {
81                     len = (trimLength < temp.length() ? trimLength : temp.length());
82                     String JavaDoc msg = temp.substring(0, len);
83                     x = msg.indexOf(SystemUtils.LINE_SEPARATOR);
84
85                     if (x > -1)
86                     {
87                         msg = msg.substring(0, x);
88                         len = x + 1;
89                     }
90                     else
91                     {
92                         x = msg.lastIndexOf(' ');
93                         if (x > -1 && len == trimLength)
94                         {
95                             msg = msg.substring(0, x);
96                             len = x + 1;
97                         }
98                     }
99                     if (msg.startsWith(" "))
100                     {
101                         msg = msg.substring(1);
102                     }
103
104                     temp = temp.substring(len);
105                     messages.add(k, msg);
106                     k++;
107                 }
108             }
109         }
110
111         buf.append(SystemUtils.LINE_SEPARATOR);
112         if (c != ' ')
113         {
114             buf.append(StringUtils.repeat(c, maxlength));
115         }
116
117         for (int i = 0; i < messages.size(); i++)
118         {
119             buf.append(SystemUtils.LINE_SEPARATOR);
120             if (c != ' ')
121             {
122                 buf.append(c);
123             }
124             buf.append(" ");
125             buf.append(messages.get(i));
126
127             int padding;
128             try
129             {
130                 padding = trimLength - messages.get(i).toString().getBytes(getOSEncoding()).length;
131             }
132             catch (UnsupportedEncodingException JavaDoc ueex)
133             {
134                 throw new MuleRuntimeException(new Message(
135                     CoreMessageConstants.FAILED_TO_CONVERT_STRING_USING_X_ENCODING, getOSEncoding()));
136             }
137             if (padding > 0)
138             {
139                 buf.append(StringUtils.repeat(' ', padding));
140             }
141             buf.append(' ');
142             if (c != ' ')
143             {
144                 buf.append(c);
145             }
146         }
147         buf.append(SystemUtils.LINE_SEPARATOR);
148         if (c != ' ')
149         {
150             buf.append(StringUtils.repeat(c, maxlength));
151         }
152         return buf.toString();
153     }
154
155     public static String JavaDoc truncate(String JavaDoc message, int length, boolean includeCount)
156     {
157         if (message == null)
158         {
159             return null;
160         }
161         if (message.length() <= length)
162         {
163             return message;
164         }
165         String JavaDoc result = message.substring(0, length) + "...";
166         if (includeCount)
167         {
168             result += "[" + length + " of " + message.length() + "]";
169         }
170         return result;
171     }
172
173     public static byte[] getBytes(String JavaDoc string)
174     {
175         try
176         {
177             return string.getBytes(getEncoding());
178         }
179         catch (UnsupportedEncodingException JavaDoc e)
180         {
181             // We can ignore this as the encoding is validated on start up
182
return null;
183         }
184     }
185
186     public static String JavaDoc getString(byte[] bytes, String JavaDoc encoding)
187     {
188         try
189         {
190             return new String JavaDoc(bytes, encoding);
191         }
192         catch (UnsupportedEncodingException JavaDoc e)
193         {
194             // We can ignore this as the encoding is validated on start up
195
return null;
196         }
197     }
198
199     private static String JavaDoc getEncoding()
200     {
201         // Note that the org.mule.encoding property will not be set by Mule until the
202
// MuleManager.initialise method is called, thus if you need to set an
203
// encoding other than UTF-8 before the Manager is invoked, you can set this
204
// property on the JVM
205
return System.getProperty(MuleProperties.MULE_ENCODING_SYSTEM_PROPERTY, DEFAULT_ENCODING);
206     }
207
208     private static String JavaDoc getOSEncoding()
209     {
210         // Note that the org.mule.osEncoding property will not be set by Mule until
211
// the MuleManager.initialise method is called, thus if you need to set an
212
// encoding other than UTF-8 before the Manager is invoked, you can set this
213
// property on the JVM
214
return System.getProperty(MuleProperties.MULE_OS_ENCODING_SYSTEM_PROPERTY, DEFAULT_OS_ENCODING);
215     }
216
217     /**
218      * @see {@link ArrayUtils#toString(Object, int)}
219      * @see {@link CollectionUtils#toString(Collection, int)}
220      * @see {@link MapUtils#toString(Map, boolean)}
221      */

222     public static String JavaDoc toString(Object JavaDoc o)
223     {
224         if (o == null)
225         {
226             return "null";
227         }
228         else if (o instanceof Class JavaDoc)
229         {
230             return ((Class JavaDoc)o).getName();
231         }
232         else if (o instanceof Map JavaDoc)
233         {
234             return MapUtils.toString((Map JavaDoc)o, false);
235         }
236         else if (o.getClass().isArray())
237         {
238             return ArrayUtils.toString(o, MAX_ELEMENTS);
239         }
240         else if (o instanceof Collection JavaDoc)
241         {
242             return CollectionUtils.toString((Collection JavaDoc)o, MAX_ELEMENTS);
243         }
244         else
245         {
246             return o.toString();
247         }
248     }
249
250 }
251
Popular Tags