1 31 package org.blojsom.plugin.statistics; 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.Map ; 42 import java.util.StringTokenizer ; 43 44 51 public class WordCountPlugin implements Plugin { 52 53 public static final String WORD_COUNT_PLUGIN_HELPER = "WORD_COUNT_PLUGIN_HELPER"; 54 public static final String BLOJSOM_PLUGIN_WORD_COUNT_METADATA = "blojsom-plugin-word-count"; 55 56 59 public WordCountPlugin() { 60 } 61 62 70 public void init() throws PluginException { 71 } 72 73 84 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 85 for (int i = 0; i < entries.length; i++) { 86 Entry entry = entries[i]; 87 Map entryMetaData = entry.getMetaData(); 88 89 if (entry.getDescription() == null) { 90 entryMetaData.put(BLOJSOM_PLUGIN_WORD_COUNT_METADATA, new Integer (0)); 91 } else { 92 String entryWithoutTags = entry.getDescription(); 93 entryWithoutTags = entryWithoutTags.replaceAll("\\<.*?\\>", ""); 94 StringTokenizer tokenizer = new StringTokenizer (entryWithoutTags); 95 entryMetaData.put(BLOJSOM_PLUGIN_WORD_COUNT_METADATA, new Integer (tokenizer.countTokens())); 96 } 97 98 entry.setMetaData(entryMetaData); 99 } 100 101 context.put(WORD_COUNT_PLUGIN_HELPER, new WordCountHelper()); 102 103 return entries; 104 } 105 106 112 public void cleanup() throws PluginException { 113 } 114 115 121 public void destroy() throws PluginException { 122 } 123 124 127 public class WordCountHelper { 128 129 135 public Integer countWords(String text) { 136 if (BlojsomUtils.checkNullOrBlank(text)) { 137 return new Integer (0); 138 } else { 139 String textWithoutTokens = text.replaceAll("\\<.*?\\>", ""); 140 StringTokenizer tokenizer = new StringTokenizer (textWithoutTokens); 141 142 return new Integer (tokenizer.countTokens()); 143 } 144 } 145 } 146 } | Popular Tags |