View Javadoc

1   /*
2    * $Id: StringMessageUtils.java 11359 2008-03-14 11:16:15Z tcarlson $
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 CPAL v1.0
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 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   * Useful methods for formatting message strings for logging or exceptions.
27   */
28  // @ThreadSafe
29  public final class StringMessageUtils
30  {
31      // The maximum number of Collection and Array elements used for messages
32      public static final int MAX_ELEMENTS = 50;
33      public static final int DEFAULT_MESSAGE_WIDTH = 80;
34  
35      /** Do not instanciate. */
36      private StringMessageUtils ()
37      {
38          // no-op
39      }
40  
41      public static String getFormattedMessage(String msg, Object[] 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 getBoilerPlate(String message)
54      {
55          return getBoilerPlate(message, '*', DEFAULT_MESSAGE_WIDTH);
56      }
57  
58      public static String getBoilerPlate(String message, char c, int maxlength)
59      {
60          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          StringBuffer buf = new StringBuffer(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 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 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             String osEncoding = CharSetUtils.defaultCharsetName();
128             int padding;
129             try
130             {
131                 padding = trimLength - messages.get(i).toString().getBytes(osEncoding).length;
132             }
133             catch (UnsupportedEncodingException ueex)
134             {
135                 throw new MuleRuntimeException(CoreMessages.failedToConvertStringUsingEncoding(osEncoding), ueex);
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 truncate(String 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 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 string)
174     {
175         try
176         {
177             return string.getBytes(MuleServer.getMuleContext().getConfiguration().getDefaultEncoding());
178         }
179         catch (UnsupportedEncodingException 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 getString(byte[] bytes, String encoding)
187     {
188         try
189         {
190             return new String(bytes, encoding);
191         }
192         catch (UnsupportedEncodingException e)
193         {
194             // We can ignore this as the encoding is validated on start up
195             return null;
196         }
197     }
198 
199     /**
200      * @see {@link ArrayUtils#toString(Object, int)}
201      * @see {@link CollectionUtils#toString(Collection, int)}
202      * @see {@link MapUtils#toString(Map, boolean)}
203      */
204     public static String toString(Object o)
205     {
206         if (o == null)
207         {
208             return "null";
209         }
210         else if (o instanceof Class)
211         {
212             return ((Class) o).getName();
213         }
214         else if (o instanceof Map)
215         {
216             return MapUtils.toString((Map) o, false);
217         }
218         else if (o.getClass().isArray())
219         {
220             return ArrayUtils.toString(o, MAX_ELEMENTS);
221         }
222         else if (o instanceof Collection)
223         {
224             return CollectionUtils.toString((Collection) o, MAX_ELEMENTS);
225         }
226         else
227         {
228             return o.toString();
229         }
230     }
231 
232 }