1 31 package org.blojsom.plugin.nofollow; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.blojsom.blog.Blog; 36 import org.blojsom.blog.Comment; 37 import org.blojsom.blog.Entry; 38 import org.blojsom.blog.Trackback; 39 import org.blojsom.plugin.Plugin; 40 import org.blojsom.plugin.PluginException; 41 import org.blojsom.util.BlojsomUtils; 42 43 import javax.servlet.http.HttpServletRequest ; 44 import javax.servlet.http.HttpServletResponse ; 45 import java.util.Iterator ; 46 import java.util.Map ; 47 import java.util.regex.Matcher ; 48 import java.util.regex.Pattern ; 49 50 58 public class NoFollowPlugin implements Plugin { 59 60 private Log _logger = LogFactory.getLog(NoFollowPlugin.class); 61 62 private static final String HYPERLINK_REGEX = "<a\\s([^>]*\\s*href\\s*=[^>]*)>"; 63 private static final String ATTRIBUTE_REGEX = "[^=[\\p{Space}]]*\\s*=\\s*\"[^\"]*\"|[^=[\\p{Space}]]*\\s*=\\s*'[^']*'|[^=[\\p{Space}]]*\\s*=[^[\\p{Space}]]*"; 64 private static final String NOFOLLOW_REGEX = "\\s*nofollow\\s*"; 65 private static final String REL_ATTR_REGEX = "rel\\s*="; 66 private static final String REL_NOFOLLOW = " rel=\"nofollow\""; 67 68 71 public NoFollowPlugin() { 72 } 73 74 80 public void init() throws PluginException { 81 } 82 83 90 protected String noFollowFy(String text) { 91 if (BlojsomUtils.checkNullOrBlank(text)) { 92 return text; 93 } 94 95 StringBuffer updatedText = new StringBuffer (); 96 97 Pattern hyperlinkPattern = Pattern.compile(HYPERLINK_REGEX, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.UNICODE_CASE | Pattern.DOTALL); 98 Matcher hyperlinkMatcher = hyperlinkPattern.matcher(text); 99 100 Pattern attributePattern = Pattern.compile(ATTRIBUTE_REGEX, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.UNICODE_CASE | Pattern.DOTALL); 101 Pattern relAttrPattern = Pattern.compile(REL_ATTR_REGEX, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.UNICODE_CASE | Pattern.DOTALL); 102 Pattern noFollow = Pattern.compile(NOFOLLOW_REGEX, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.UNICODE_CASE | Pattern.DOTALL); 103 104 Matcher noFollowMatcher; 105 Matcher attributeMatcher; 106 107 int lastIndex = 0; 108 while (hyperlinkMatcher.find()) { 109 updatedText.append(text.substring(lastIndex, hyperlinkMatcher.start())); 110 String link = hyperlinkMatcher.group(); 111 attributeMatcher = attributePattern.matcher(link); 112 113 StringBuffer updatedLink = new StringBuffer (); 114 boolean shouldAddAttr = true; 115 116 while (attributeMatcher.find()) { 117 String attr = attributeMatcher.group(); 118 119 Matcher relAttrMatcher = relAttrPattern.matcher(attr); 120 while (relAttrMatcher.find()) { 121 noFollowMatcher = noFollow.matcher(attr); 122 if (!noFollowMatcher.matches()) { 123 int indexOfQuote = attr.lastIndexOf("\""); 124 if (indexOfQuote != -1) { 125 attr = attr.substring(0, indexOfQuote) + " nofollow\""; 126 shouldAddAttr = false; 127 } 128 } 129 } 130 131 updatedLink.append(attr); 132 } 133 134 if (shouldAddAttr) { 135 updatedLink.append(REL_NOFOLLOW); 136 } 137 138 updatedLink.append((">")); 139 updatedText.append(updatedLink); 140 lastIndex = hyperlinkMatcher.end(); 141 } 142 143 updatedText.append(text.substring(lastIndex)); 144 145 return updatedText.toString(); 146 } 147 148 159 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 160 for (int i = 0; i < entries.length; i++) { 161 Entry entry = entries[i]; 162 163 Iterator commentsIterator = entry.getComments().iterator(); 164 String commentText; 165 while (commentsIterator.hasNext()) { 166 Comment blogComment = (Comment) commentsIterator.next(); 167 168 commentText = blogComment.getComment(); 169 commentText = noFollowFy(commentText); 170 blogComment.setComment(commentText); 171 } 172 173 Iterator trackbacksIterator = entry.getTrackbacks().iterator(); 174 String trackbackText; 175 while (trackbacksIterator.hasNext()) { 176 Trackback trackback = (Trackback) trackbacksIterator.next(); 177 178 trackbackText = trackback.getExcerpt(); 179 trackbackText = noFollowFy(trackbackText); 180 trackback.setExcerpt(trackbackText); 181 } 182 } 183 184 return entries; 185 } 186 187 192 public void cleanup() throws PluginException { 193 } 194 195 200 public void destroy() throws PluginException { 201 } 202 } | Popular Tags |