1 20 21 package com.methodhead.util; 22 23 import java.util.Map ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.regex.Pattern ; 27 import java.util.regex.Matcher ; 28 29 import org.apache.commons.beanutils.DynaClass; 30 import org.apache.commons.beanutils.DynaProperty; 31 import org.apache.commons.beanutils.BasicDynaClass; 32 import org.apache.commons.beanutils.BeanUtils; 33 import org.apache.commons.lang.exception.ExceptionUtils; 34 import org.apache.commons.lang.StringUtils; 35 import java.lang.reflect.InvocationTargetException ; 36 import java.security.MessageDigest ; 37 import java.security.NoSuchAlgorithmException ; 38 import org.apache.commons.codec.binary.Base64; 39 40 public class MhfStringUtils { 41 42 44 46 48 50 53 protected static Map extractProperties( 54 Object o ) { 55 56 Map fields = new HashMap (); 60 61 try { 62 Map properties = BeanUtils.describe( o ); 63 for ( Iterator iter = properties.keySet().iterator(); iter.hasNext(); ) { 64 String key = iter.next().toString(); 65 fields.put( key, BeanUtils.getProperty( o, key ) ); 66 } 67 } 68 catch ( IllegalAccessException e ) { 69 throw new RuntimeException ( 70 "Unexpected IllegalAccessException while getting properties for " + 71 "object." + ExceptionUtils.getStackTrace( e ) ); 72 } 73 catch ( InvocationTargetException e ) { 74 throw new RuntimeException ( 75 "Unexpected InvocationTargetException while getting properties for " + 76 "object." + ExceptionUtils.getStackTrace( e ) ); 77 } 78 catch ( NoSuchMethodException e ) { 79 throw new RuntimeException ( 80 "Unexpected NoSuchMethodException while getting properties for " + 81 "object." + ExceptionUtils.getStackTrace( e ) ); 82 } 83 84 return fields; 85 } 86 87 93 public static String merge( 94 String text, 95 Map fields ) { 96 97 Pattern pattern = Pattern.compile( "\\{(\\w+)\\}", Pattern.MULTILINE ); 101 Matcher matcher = pattern.matcher( text ); 102 103 while ( matcher.find() ) { 104 if ( !fields.containsKey( matcher.group( 1 ) ) ) 105 throw new RuntimeException ( 106 "Values don't include field \"" + matcher.group( 1 ) + 107 "\", required by mail template text." ); 108 } 109 110 String result = text; 114 for ( Iterator iter = fields.keySet().iterator(); iter.hasNext(); ) { 115 String key = iter.next().toString(); 116 result = 117 StringUtils.replace( 118 result, "{" + key + "}", fields.get( key ).toString() ); 119 } 120 121 return result; 122 } 123 124 132 public static String merge( 133 String text, 134 Object o ) { 135 136 return merge( text, extractProperties( o ) ); 140 } 141 142 146 public static String hashAndEncode( 147 String s ) { 148 149 try { 150 MessageDigest md5 = MessageDigest.getInstance( "MD5" ); 151 return new String ( Base64.encodeBase64( md5.digest( s.getBytes() ) ) ); 152 } 153 catch ( NoSuchAlgorithmException e ) { 154 throw new RuntimeException ( 155 "Unexpected NoSuchAlgorithmException:\n" + 156 ExceptionUtils.getStackTrace( e ) ); 157 } 158 } 159 160 162 } 164 | Popular Tags |