Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
27   92   8   9
8   48   0,3   3
3     2,67  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  NoFollowDecorator       Line # 16 27 0% 8 2 94,7% 0.94736844
 
  (4)
 
1    package net.sourceforge.pebble.decorator;
2   
3    import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
4    import net.sourceforge.pebble.domain.Comment;
5    import net.sourceforge.pebble.domain.TrackBack;
6   
7    import java.util.regex.Matcher;
8    import java.util.regex.Pattern;
9   
10    /**
11    * Adds a rel="nofollow" attribute into all links within comment
12    * and TrackBack content.
13    *
14    * @author Simon Brown
15    */
 
16    public class NoFollowDecorator extends ContentDecoratorSupport {
17   
18    /** the regex used to find HTML links */
19    private static Pattern HTML_LINK_PATTERN = Pattern.compile("<a.*?href=.*?>", Pattern.CASE_INSENSITIVE);
20   
21    /**
22    * Decorates the specified comment.
23    *
24    * @param context the context in which the decoration is running
25    * @param comment the comment to be decorated
26    */
 
27  40 toggle public void decorate(ContentDecoratorContext context, Comment comment) {
28  40 comment.setBody(addNoFollowLinks(comment.getBody()));
29    }
30   
31    /**
32    * Decorates the specified TrackBack.
33    *
34    * @param context the context in which the decoration is running
35    * @param trackBack the TrackBack to be decorated
36    */
 
37  8 toggle public void decorate(ContentDecoratorContext context, TrackBack trackBack) {
38  8 trackBack.setExcerpt(addNoFollowLinks(trackBack.getExcerpt()));
39    }
40   
41    /**
42    * Adds rel="nofollow" links too any links in the specified string.
43    *
44    * @param html a String containing HTML
45    * @return the same String with rel="nofollow" in anchor tags
46    */
 
47  48 toggle private String addNoFollowLinks(String html) {
48  48 if (html == null || html.length() == 0) {
49  0 return html;
50    }
51   
52  48 Matcher m = HTML_LINK_PATTERN.matcher(html);
53  48 StringBuffer buf = new StringBuffer();
54   
55  80 while (m.find()) {
56  32 int start = m.start();
57  32 int end = m.end();
58   
59  32 String link = html.substring(start, end);
60  32 buf.append(html.substring(0, start));
61   
62    // add the nofollow, if necessary
63  32 int startOfRelIndex = link.indexOf("rel=\"");
64  32 if (startOfRelIndex == -1) {
65    // no rel link, add one
66  24 buf.append(link.substring(0, link.length() - 1));
67  24 buf.append(" rel=\"nofollow\">");
68    } else {
69  8 int endOfRelIndex = link.indexOf("\"", startOfRelIndex+5);
70  8 String rel = link.substring(startOfRelIndex+5, endOfRelIndex);
71  8 rel = rel.toLowerCase();
72  8 if (rel.indexOf("nofollow") == -1) {
73    // rel exists, but without nofollow
74  4 buf.append(link.substring(0, endOfRelIndex));
75  4 buf.append(" nofollow");
76  4 buf.append(link.substring(endOfRelIndex));
77    } else {
78    // nofollow exists
79  4 buf.append(link);
80    }
81    }
82   
83  32 html = html.substring(end, html.length());
84  32 m = HTML_LINK_PATTERN.matcher(html);
85    }
86   
87  48 buf.append(html);
88   
89  48 return buf.toString();
90    }
91   
92    }