1 31 package org.blojsom.plugin.highlight; 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 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpServletResponse ; 40 import java.util.Map ; 41 import java.util.StringTokenizer ; 42 import java.util.regex.Matcher ; 43 import java.util.regex.Pattern ; 44 45 55 public class GoogleHighlightPlugin implements Plugin { 56 57 60 private static final String HEADER_REFERER = "referer"; 61 62 private static final String START_BOUNDRY = "(\\b"; 63 private static final String END_BOUNDRY = "\\b)"; 64 65 68 private static final String EXPRESSSION_GOOGLE = "^http:\\/\\/w?w?w?\\.?google.*"; 69 70 private static final String EXPRESSION_HTMLPREFIX = "(?<=>)([^<]+)?"; 71 private static final String EXPRESSION_HASTAGS = "<.+>"; 72 73 76 private static final String GOOGLE_QUERY = "^.*q=([^&]+)&?.*$"; 77 78 81 private static final String GOOGLE_CLEANQUOTES = "'/\'|\"/\""; 82 83 86 private static final String HIGHLIGHT_PLAINTEXT = "<span class=\"searchhighlight\">$1</span>"; 87 88 91 private static final String HIGHLIGHT_HTML = "$1<span class=\"searchhighlight\">$2</span>"; 92 93 98 public void init() throws PluginException { 99 } 100 101 102 108 private String [] extractQueryTokens(String referer) { 109 String [] result = null; 110 Matcher matcher = Pattern.compile(GOOGLE_QUERY, Pattern.CASE_INSENSITIVE).matcher(referer); 111 if (matcher.find()) { 112 String _query = matcher.group(1); 113 _query = _query.replaceAll(GOOGLE_CLEANQUOTES, ""); 114 StringTokenizer _st = new StringTokenizer (_query, "+, .", false); 115 result = new String [_st.countTokens()]; 116 int cnt = 0; 117 while (_st.hasMoreElements()) { 118 result[cnt] = _st.nextToken(); 119 cnt += 1; 120 } 121 } 122 123 return result; 124 } 125 126 137 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 138 String referer = httpServletRequest.getHeader(HEADER_REFERER); 139 140 if (referer != null && referer.matches(EXPRESSSION_GOOGLE)) { 141 String [] searchwords = extractQueryTokens(referer); 142 143 if (searchwords != null) { 144 Pattern hasTags = Pattern.compile(EXPRESSION_HASTAGS); 145 146 for (int x = 0; x < entries.length; x++) { 147 Entry entry = entries[x]; 148 Matcher matcher = hasTags.matcher(entry.getDescription()); 149 boolean isHtml = matcher.find(); 150 for (int y = 0; y < searchwords.length; y++) { 151 String word = searchwords[y]; 152 if (!isHtml) { 153 entry.setDescription(entry.getDescription().replaceAll(START_BOUNDRY + word + END_BOUNDRY, HIGHLIGHT_PLAINTEXT)); 154 } else { 155 entry.setDescription(entry.getDescription().replaceAll(EXPRESSION_HTMLPREFIX + START_BOUNDRY + word + END_BOUNDRY, HIGHLIGHT_HTML)); 156 } 157 } 158 } 159 } 160 } 161 162 return entries; 163 } 164 165 170 public void cleanup() throws PluginException { 171 } 172 173 178 public void destroy() throws PluginException { 179 } 180 } 181 | Popular Tags |