1 16 package com.blandware.atleap.common.util; 17 18 import com.blandware.atleap.common.Constants; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 import java.io.UnsupportedEncodingException ; 23 import java.lang.reflect.Array ; 24 import java.util.*; 25 26 27 36 public final class ConvertUtil { 37 39 41 47 public static Map convertBundleToMap(ResourceBundle rb) { 48 Map map = new HashMap(); 49 50 for ( Enumeration keys = rb.getKeys(); keys.hasMoreElements(); ) { 51 String key = (String ) keys.nextElement(); 52 map.put(key, rb.getString(key)); 53 } 54 55 return map; 56 } 57 58 64 public static Properties convertBundleToProperties(ResourceBundle rb) { 65 Properties props = new Properties(); 66 67 for ( Enumeration keys = rb.getKeys(); keys.hasMoreElements(); ) { 68 String key = (String ) keys.nextElement(); 69 props.put(key, rb.getString(key)); 70 } 71 72 return props; 73 } 74 75 82 public static String convertListToString(List list, String delimiter) { 83 return convertListToString(list, delimiter, null); 84 } 85 86 95 public static String convertListToString(List list, String delimiter, char encloser) { 96 return convertListToString(list, delimiter, new Character (encloser)); 97 } 98 99 108 private static String convertListToString(List list, String delimiter, Character encloser) { 109 110 if ( list == null || list.size() == 0 ) { 111 return new String (); 112 } 113 114 StringBuffer sb = new StringBuffer (); 115 for ( Iterator i = list.iterator(); i.hasNext(); ) { 116 String next = String.valueOf(i.next()); 117 if ( encloser != null ) { 118 next = encloser + next + encloser; 119 } 120 sb.append(next); 121 if ( i.hasNext() ) { 122 sb.append(delimiter); 123 } 124 } 125 126 return sb.toString(); 127 } 128 129 138 public static List convertStringToList(String string, String delimiter, boolean trim) { 139 if ( string == null || string.length() == 0 ) { 140 return new LinkedList(); 141 } 142 String [] members = string.split(delimiter); 143 List list = Collections.synchronizedList(new LinkedList()); 144 for ( int i = 0; i < members.length; i++ ) { 145 String member = members[i]; 146 if ( trim ) { 147 member = member.trim(); 148 } 149 list.add(member); 150 } 151 return list; 152 } 153 154 160 public static Set convertArrayToSet(Object [] anArray) { 161 Set set = new HashSet(); 162 for ( int i = 0; i < anArray.length; i++ ) { 163 set.add(anArray[i]); 164 } 165 return set; 166 } 167 168 175 public static Long [] convertToLongArray(List values) { 176 Long [] result = new Long [values.size()]; 177 for ( int i = 0; i < values.size(); i++ ) { 178 Object value = values.get(i); 179 if ( !(value instanceof String ) ) { 180 throw new ClassCastException ("Unable to convert instance of class " + value.getClass().getName() + " to java.lang.Long"); 181 } 182 result[i] = Long.valueOf((String ) value); 183 } 184 return result; 185 } 186 187 193 public static String convertToString(byte[] content) { 194 return convertToString(content, null); 195 } 196 197 204 public static String convertToString(byte[] content, String encoding) { 205 if ( content == null ) { 206 return null; 207 } 208 if ( encoding == null ) { 209 encoding = Constants.DEFAULT_ENCODING; 210 } 211 212 String value = null; 213 try { 214 value = new String (content, encoding); 215 } catch ( UnsupportedEncodingException ex ) { 216 return new String (content); 217 } 218 return value; 219 } 220 221 222 228 public static byte[] convertToByteArray(String content) { 229 return convertToByteArray(content, null); 230 } 231 232 239 public static byte[] convertToByteArray(String content, String encoding) { 240 241 Log log = LogFactory.getLog(ConvertUtil.class); 242 243 if ( content == null ) { 244 return null; 245 } 246 if ( encoding == null ) { 247 encoding = Constants.DEFAULT_ENCODING; 248 } 249 250 if ( log.isDebugEnabled() ) { 251 log.debug("Converting to byte array using: " + encoding); 252 } 253 254 byte[] value; 255 try { 256 value = content.getBytes(encoding); 257 } catch ( UnsupportedEncodingException ex ) { 258 if ( log.isWarnEnabled() ) { 259 log.warn(ex); 260 } 261 return content.getBytes(); 262 } 263 return value; 264 } 265 266 267 276 public static Object [] convertPrimitivesToObjects(Object primitiveArray) { 277 if ( primitiveArray == null ) { 278 return null; 279 } 280 281 if ( !primitiveArray.getClass().isArray() ) { 282 throw new IllegalArgumentException ("Specified object is not array"); 283 } 284 285 if ( primitiveArray instanceof Object [] ) { 286 throw new IllegalArgumentException ("Specified object is not primitive array"); 287 } 288 289 int length = Array.getLength(primitiveArray); 290 Object [] result = new Object [length]; 291 for ( int i = 0; i < length; i++ ) { 292 result[i] = Array.get(primitiveArray, i); 293 } 294 295 return result; 296 } 297 298 314 public static List convertCollectionToList(Object collection) { 315 316 if ( collection == null ) { 317 return null; 318 } 319 320 List list = null; 321 322 if ( collection instanceof Collection ) { 323 list = new ArrayList((Collection) collection); 324 } else if ( collection instanceof Enumeration ) { 325 list = new ArrayList(); 326 Enumeration e = (Enumeration) collection; 327 while ( e.hasMoreElements() ) { 328 list.add(e.nextElement()); 329 } 330 } else if ( collection instanceof Iterator ) { 331 list = new ArrayList(); 332 Iterator i = (Iterator) collection; 333 while ( i.hasNext() ) { 334 list.add(i.next()); 335 } 336 } else if ( collection instanceof Map ) { 337 list = new ArrayList(((Map) collection).entrySet()); 338 } else if ( collection instanceof String ) { 339 list = Arrays.asList(convertPrimitivesToObjects(((String ) collection).toCharArray())); 340 } else if ( collection instanceof Object [] ) { 341 list = Arrays.asList((Object []) collection); 342 } else if ( collection.getClass().isArray() ) { 343 list = Arrays.asList(convertPrimitivesToObjects(collection)); 344 } else { 345 throw new IllegalArgumentException ("Class '" + collection.getClass().getName() + "' is not convertable to java.util.List"); 347 } 348 349 return list; 350 } 351 } 352 | Popular Tags |