1 10 11 package org.mule.util; 12 13 import java.io.UnsupportedEncodingException ; 14 import java.text.MessageFormat ; 15 import java.util.ArrayList ; 16 import java.util.Arrays ; 17 import java.util.Collection ; 18 import java.util.List ; 19 import java.util.Map ; 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 31 public class StringMessageUtils 33 { 34 public static final String DEFAULT_ENCODING = "UTF-8"; 36 public static final String DEFAULT_OS_ENCODING = System.getProperty("file.encoding"); 37 38 public static final int MAX_ELEMENTS = 50; 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, '*', 80); 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 int padding; 128 try 129 { 130 padding = trimLength - messages.get(i).toString().getBytes(getOSEncoding()).length; 131 } 132 catch (UnsupportedEncodingException 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 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(getEncoding()); 178 } 179 catch (UnsupportedEncodingException e) 180 { 181 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 return null; 196 } 197 } 198 199 private static String getEncoding() 200 { 201 return System.getProperty(MuleProperties.MULE_ENCODING_SYSTEM_PROPERTY, DEFAULT_ENCODING); 206 } 207 208 private static String getOSEncoding() 209 { 210 return System.getProperty(MuleProperties.MULE_OS_ENCODING_SYSTEM_PROPERTY, DEFAULT_OS_ENCODING); 215 } 216 217 222 public static String toString(Object o) 223 { 224 if (o == null) 225 { 226 return "null"; 227 } 228 else if (o instanceof Class ) 229 { 230 return ((Class )o).getName(); 231 } 232 else if (o instanceof Map ) 233 { 234 return MapUtils.toString((Map )o, false); 235 } 236 else if (o.getClass().isArray()) 237 { 238 return ArrayUtils.toString(o, MAX_ELEMENTS); 239 } 240 else if (o instanceof Collection ) 241 { 242 return CollectionUtils.toString((Collection )o, MAX_ELEMENTS); 243 } 244 else 245 { 246 return o.toString(); 247 } 248 } 249 250 } 251 | Popular Tags |