KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > util > MhfStringUtils


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.util;
22
23 import java.util.Map JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.regex.Pattern JavaDoc;
27 import java.util.regex.Matcher JavaDoc;
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 JavaDoc;
36 import java.security.MessageDigest JavaDoc;
37 import java.security.NoSuchAlgorithmException JavaDoc;
38 import org.apache.commons.codec.binary.Base64;
39
40 public class MhfStringUtils {
41
42   // constructors /////////////////////////////////////////////////////////////
43

44   // constants ////////////////////////////////////////////////////////////////
45

46   // classes //////////////////////////////////////////////////////////////////
47

48   // methods //////////////////////////////////////////////////////////////////
49

50   /**
51    * Utility method to extract the properties of <tt>o</tt> as a map.
52    */

53   protected static Map JavaDoc extractProperties(
54     Object JavaDoc o ) {
55
56     //
57
// convert the readable properties of o into a map of strings
58
//
59
Map JavaDoc fields = new HashMap JavaDoc();
60
61     try {
62       Map JavaDoc properties = BeanUtils.describe( o );
63       for ( Iterator JavaDoc iter = properties.keySet().iterator(); iter.hasNext(); ) {
64         String JavaDoc key = iter.next().toString();
65         fields.put( key, BeanUtils.getProperty( o, key ) );
66       }
67     }
68     catch ( IllegalAccessException JavaDoc e ) {
69       throw new RuntimeException JavaDoc(
70         "Unexpected IllegalAccessException while getting properties for " +
71         "object." + ExceptionUtils.getStackTrace( e ) );
72     }
73     catch ( InvocationTargetException JavaDoc e ) {
74       throw new RuntimeException JavaDoc(
75         "Unexpected InvocationTargetException while getting properties for " +
76         "object." + ExceptionUtils.getStackTrace( e ) );
77     }
78     catch ( NoSuchMethodException JavaDoc e ) {
79       throw new RuntimeException JavaDoc(
80         "Unexpected NoSuchMethodException while getting properties for " +
81         "object." + ExceptionUtils.getStackTrace( e ) );
82     }
83
84     return fields;
85   }
86
87   /**
88    * Utility method to merge the values in <tt>fields</tt> into <tt>text</tt>,
89    * returning the resulting message. The keys and values are treated as
90    * <tt>String</tt>s. If the template references a field that is not provided
91    * by <tt>fields</tt>, a <tt>RuntimeException</tt> is thrown.
92    */

93   public static String JavaDoc merge(
94     String JavaDoc text,
95     Map JavaDoc fields ) {
96
97     //
98
// verify fields provides all fields we'll need
99
//
100
Pattern JavaDoc pattern = Pattern.compile( "\\{(\\w+)\\}", Pattern.MULTILINE );
101     Matcher JavaDoc matcher = pattern.matcher( text );
102
103     while ( matcher.find() ) {
104       if ( !fields.containsKey( matcher.group( 1 ) ) )
105         throw new RuntimeException JavaDoc(
106           "Values don't include field \"" + matcher.group( 1 ) +
107           "\", required by mail template text." );
108     }
109
110     //
111
// substitute the fields
112
//
113
String JavaDoc result = text;
114     for ( Iterator JavaDoc iter = fields.keySet().iterator(); iter.hasNext(); ) {
115       String JavaDoc key = iter.next().toString();
116       result =
117         StringUtils.replace(
118           result, "{" + key + "}", fields.get( key ).toString() );
119     }
120
121     return result;
122   }
123
124   /**
125    * Merges the properties of <tt>o</tt> into the template, returning the
126    * resulting message. The properties of are treated as <tt>String</tt>s.
127    * <tt>DynaBeans</tt> are supported. Keep in mind that inherent properties
128    * of objects (e.g., "class") are not omitted. If the template references a
129    * property that is not provided by <tt>o</tt>, a <tt>RuntimeException</tt>
130    * is thrown.
131    */

132   public static String JavaDoc merge(
133     String JavaDoc text,
134     Object JavaDoc o ) {
135
136     //
137
// merge as with a map
138
//
139
return merge( text, extractProperties( o ) );
140   }
141
142   /**
143    * Encrypts <tt>s</tt> by MD5 hashing it and Base64 encoding the
144    * result.
145    */

146   public static String JavaDoc hashAndEncode(
147     String JavaDoc s ) {
148
149     try {
150       MessageDigest JavaDoc md5 = MessageDigest.getInstance( "MD5" );
151       return new String JavaDoc( Base64.encodeBase64( md5.digest( s.getBytes() ) ) );
152     }
153     catch ( NoSuchAlgorithmException JavaDoc e ) {
154       throw new RuntimeException JavaDoc(
155         "Unexpected NoSuchAlgorithmException:\n" +
156         ExceptionUtils.getStackTrace( e ) );
157     }
158   }
159
160   // properties ///////////////////////////////////////////////////////////////
161

162   // attributes ///////////////////////////////////////////////////////////////
163
}
164
Popular Tags