| 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 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
@author |
| 15 |
|
|
|
|
|
| 94,7% |
Uncovered Elements: 2 (38) |
Complexity: 8 |
Complexity Density: 0,3 |
|
| 16 |
|
public class NoFollowDecorator extends ContentDecoratorSupport { |
| 17 |
|
|
| 18 |
|
|
| 19 |
|
private static Pattern HTML_LINK_PATTERN = Pattern.compile("<a.*?href=.*?>", Pattern.CASE_INSENSITIVE); |
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
@param |
| 25 |
|
@param |
| 26 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 27 |
40
|
public void decorate(ContentDecoratorContext context, Comment comment) {... |
| 28 |
40
|
comment.setBody(addNoFollowLinks(comment.getBody())); |
| 29 |
|
} |
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
@param |
| 35 |
|
@param |
| 36 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 37 |
8
|
public void decorate(ContentDecoratorContext context, TrackBack trackBack) {... |
| 38 |
8
|
trackBack.setExcerpt(addNoFollowLinks(trackBack.getExcerpt())); |
| 39 |
|
} |
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
@param |
| 45 |
|
@return |
| 46 |
|
|
|
|
|
| 93,9% |
Uncovered Elements: 2 (33) |
Complexity: 6 |
Complexity Density: 0,24 |
|
| 47 |
48
|
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 |
|
|
| 63 |
32
|
int startOfRelIndex = link.indexOf("rel=\""); |
| 64 |
32
|
if (startOfRelIndex == -1) { |
| 65 |
|
|
| 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 |
|
|
| 74 |
4
|
buf.append(link.substring(0, endOfRelIndex)); |
| 75 |
4
|
buf.append(" nofollow"); |
| 76 |
4
|
buf.append(link.substring(endOfRelIndex)); |
| 77 |
|
} else { |
| 78 |
|
|
| 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 |
|
} |