1 31 package org.blojsom.plugin.emoticons; 32 33 import org.blojsom.blog.Blog; 34 import org.blojsom.blog.Entry; 35 import org.blojsom.plugin.Plugin; 36 import org.blojsom.plugin.PluginException; 37 import org.blojsom.util.BlojsomUtils; 38 39 import javax.servlet.http.HttpServletRequest ; 40 import javax.servlet.http.HttpServletResponse ; 41 import java.util.*; 42 43 54 public class EnhancedEmoticonsPlugin implements Plugin { 55 56 private static final String BLOJSOM_PLUGIN_METADATA_EMOTICONS_DISABLED = "emoticons-disabled"; 57 58 private static final String EMOTICONS_PARAM = "emoticons"; 59 private static final String EMOTICONS_PATTERN_POSTFIX = ".pattern"; 60 private static final String IMG_OPEN = "<img SRC=\""; 61 private static final String IMG_CLOSE = "\""; 62 private static final String IMG_ALT_START = " alt=\""; 63 private static final String IMG_ALT_END = "\" />"; 64 private static final String EMOTICONS_CLASS = " class=\"emoticons\" "; 65 66 private Map _emoticons; 67 68 71 public EnhancedEmoticonsPlugin() { 72 } 73 74 79 public void setEmoticons(Map emoticons) { 80 _emoticons = emoticons; 81 } 82 83 89 public void init() throws PluginException { 90 } 91 92 99 private List parseEmoticons(String emoticons) { 100 List list = new ArrayList(); 101 StringTokenizer tok = new StringTokenizer(emoticons, "\t\n\r\f,; "); 102 103 while (tok.hasMoreTokens()) { 104 list.add(tok.nextToken()); 105 } 106 107 return list; 108 } 109 110 121 public org.blojsom.blog.Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, org.blojsom.blog.Entry[] entries) throws PluginException { 122 if (_emoticons == null) { 123 return entries; 124 } 125 126 List availableEmoticons = parseEmoticons((String ) _emoticons.get(EMOTICONS_PARAM)); 127 String blogBaseUrl = blog.getBlogBaseURL(); 128 129 for (int i = 0; i < entries.length; i++) { 130 Entry entry = entries[i]; 131 132 if (!BlojsomUtils.checkMapForKey(entry.getMetaData(), BLOJSOM_PLUGIN_METADATA_EMOTICONS_DISABLED)) { 133 String updatedDescription = entry.getDescription(); 134 Iterator iter = availableEmoticons.iterator(); 135 136 while (iter.hasNext()) { 137 String emoticon = (String ) iter.next(); 138 updatedDescription = replaceEmoticon(updatedDescription, emoticon, blogBaseUrl); 139 } 140 141 entry.setDescription(updatedDescription); 142 } 143 } 144 145 return entries; 146 } 147 148 158 private String replaceEmoticon(String emoticonString, String emoticon, String url) { 159 String emoticonImage, emoticonPattern; 160 emoticonImage = (String ) _emoticons.get(emoticon); 161 emoticonPattern = (String ) _emoticons.get(emoticon + EMOTICONS_PATTERN_POSTFIX); 162 163 if (!BlojsomUtils.checkNullOrBlank(emoticonImage)) { 164 StringBuffer imageReference = new StringBuffer (IMG_OPEN); 165 imageReference.append(url); 166 imageReference.append(emoticonImage); 167 imageReference.append(IMG_CLOSE); 168 imageReference.append(EMOTICONS_CLASS); 169 imageReference.append(IMG_ALT_START); 170 imageReference.append(emoticonImage); 171 imageReference.append(IMG_ALT_END); 172 173 return BlojsomUtils.replace(emoticonString, emoticonPattern, imageReference.toString()); 174 } 175 176 return emoticonString; 177 } 178 179 184 public void cleanup() throws PluginException { 185 } 186 187 192 public void destroy() throws PluginException { 193 } 194 } | Popular Tags |