|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| This report was generated with an evaluation server license. Purchase Clover or configure your license. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RelatedPostsDecorator | Line # 27 | 28 | 0% | 10 | 4 | 90,7% |
0.90697676
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (6) | |||
| Result | |||
|
0.81395346
|
net.sourceforge.pebble.decorator.RelatedPostsDecoratorTest.testBlogEntryWithRelatedPostsAndMediaIsHtml
net.sourceforge.pebble.decorator.RelatedPostsDecoratorTest.testBlogEntryWithRelatedPostsAndMediaIsHtml
|
1 PASS | |
|
0.81395346
|
net.sourceforge.pebble.decorator.RelatedPostsDecoratorTest.testBlogEntryWithRelatedPostsAndMediaIsHtml
net.sourceforge.pebble.decorator.RelatedPostsDecoratorTest.testBlogEntryWithRelatedPostsAndMediaIsHtml
|
1 PASS | |
|
0.7906977
|
net.sourceforge.pebble.decorator.RelatedPostsDecoratorTest.testBlogEntryMaximumSixRelatedPostsAndMediaIsHtml
net.sourceforge.pebble.decorator.RelatedPostsDecoratorTest.testBlogEntryMaximumSixRelatedPostsAndMediaIsHtml
|
1 PASS | |
|
0.7906977
|
net.sourceforge.pebble.decorator.RelatedPostsDecoratorTest.testBlogEntryMaximumSixRelatedPostsAndMediaIsHtml
net.sourceforge.pebble.decorator.RelatedPostsDecoratorTest.testBlogEntryMaximumSixRelatedPostsAndMediaIsHtml
|
1 PASS | |
|
0.74418604
|
net.sourceforge.pebble.decorator.RelatedPostsDecoratorTest.testBlogEntryNoRelatedPostsAndMediaIsHtml
net.sourceforge.pebble.decorator.RelatedPostsDecoratorTest.testBlogEntryNoRelatedPostsAndMediaIsHtml
|
1 PASS | |
|
0.74418604
|
net.sourceforge.pebble.decorator.RelatedPostsDecoratorTest.testBlogEntryNoRelatedPostsAndMediaIsHtml
net.sourceforge.pebble.decorator.RelatedPostsDecoratorTest.testBlogEntryNoRelatedPostsAndMediaIsHtml
|
1 PASS | |
| 1 | package net.sourceforge.pebble.decorator; | |
| 2 | ||
| 3 | import java.util.HashSet; | |
| 4 | import java.util.List; | |
| 5 | import java.util.Set; | |
| 6 | ||
| 7 | import net.sourceforge.pebble.PluginProperties; | |
| 8 | import net.sourceforge.pebble.api.decorator.ContentDecoratorContext; | |
| 9 | import net.sourceforge.pebble.domain.Blog; | |
| 10 | import net.sourceforge.pebble.domain.BlogEntry; | |
| 11 | import net.sourceforge.pebble.domain.Tag; | |
| 12 | import net.sourceforge.pebble.util.I18n; | |
| 13 | import net.sourceforge.pebble.util.StringUtils; | |
| 14 | ||
| 15 | import org.apache.commons.logging.Log; | |
| 16 | import org.apache.commons.logging.LogFactory; | |
| 17 | ||
| 18 | /** | |
| 19 | * Adds related posts to the current post. The posts are selected by matching | |
| 20 | * tags of the current post to the tags of other posts in the blog. One related | |
| 21 | * post per tag. | |
| 22 | * | |
| 23 | * Each blog entry can have up to six related posts or none. | |
| 24 | * | |
| 25 | * @author Alexander Zagniotov | |
| 26 | */ | |
| 27 | public class RelatedPostsDecorator extends ContentDecoratorSupport { | |
| 28 | ||
| 29 | private static final Log log = LogFactory.getLog(RelatedPostsDecorator.class); | |
| 30 | ||
| 31 | /** the name of the max number of posts property */ | |
| 32 | public static final String MAX_POSTS = "RelatedPostsDecorator.maxPosts"; | |
| 33 | ||
| 34 | /** | |
| 35 | * Decorates the specified blog entry. | |
| 36 | * | |
| 37 | * @param context | |
| 38 | * the context in which the decoration is running | |
| 39 | * @param blogEntry | |
| 40 | * the blog entry to be decorated | |
| 41 | */ | |
| 42 | 6 |
public void decorate(ContentDecoratorContext context, BlogEntry blogEntry) { |
| 43 | ||
| 44 | 6 | PluginProperties props = blogEntry.getBlog().getPluginProperties(); |
| 45 | 6 | int maxPosts = StringUtils.MAX_NUM_OF_POSTS; |
| 46 | ||
| 47 | 6 | if (props.hasProperty(RelatedPostsDecorator.MAX_POSTS)) { |
| 48 | 6 | try { |
| 49 | 6 | maxPosts = Integer.parseInt(props.getProperty(MAX_POSTS)); |
| 50 | } | |
| 51 | catch (NumberFormatException nfe) { | |
| 52 | 0 | log.error(nfe.getMessage()); |
| 53 | // do nothing, the value has already been defaulted | |
| 54 | } | |
| 55 | } | |
| 56 | ||
| 57 | 6 | Blog blog = blogEntry.getBlog(); |
| 58 | 6 | String body = blogEntry.getBody(); |
| 59 | ||
| 60 | 6 | if (body != null && body.trim().length() > 0) { |
| 61 | ||
| 62 | 6 | StringBuffer buf = new StringBuffer(); |
| 63 | 6 | buf.append(body); |
| 64 | 6 | buf.append("<p><b>" + I18n.getMessage(blog, "common.relatedPosts") + "</b><br />"); |
| 65 | ||
| 66 | // tags of the current entry | |
| 67 | 6 | List<Tag> currentEntryTags = blogEntry.getAllTags(); |
| 68 | ||
| 69 | // all blog entries of the current blog | |
| 70 | 6 | List<BlogEntry> allBlogEntries = (List<BlogEntry>) blog.getBlogEntries(); |
| 71 | ||
| 72 | // temporary holder for accumulated unique related posts. | |
| 73 | // using hash set assures that we wont have same related post twice for | |
| 74 | // different tags. | |
| 75 | 6 | Set<BlogEntry> relatedEntries = new HashSet<BlogEntry>(); |
| 76 | ||
| 77 | 6 | for (BlogEntry entry : allBlogEntries) { |
| 78 | ||
| 79 | // don't add current entry as a related post of it self, skip it | |
| 80 | 24 | if (entry.getTitle().equals(blogEntry.getTitle())) |
| 81 | 4 | continue; |
| 82 | ||
| 83 | // loop through each of the current entry tags, and try to find related | |
| 84 | // post by matching current tag to the posts tags | |
| 85 | 20 | for (Tag currentTag : currentEntryTags) { |
| 86 | 24 | if (entry.hasTag(currentTag.getName())) { |
| 87 | // if we successfully selected related post - create hyperlink for | |
| 88 | // it | |
| 89 | // TODO: Missing escaping -- XSS vulnerabilities here :( | |
| 90 | 16 | if (relatedEntries.add(entry)) |
| 91 | 16 | buf.append("<a href=\"" + entry.getPermalink() + "\" rel=\"bookmark\" title=\"" + entry.getTitle() |
| 92 | + "\">" + entry.getTitle() + "</a><br />"); | |
| 93 | } | |
| 94 | } | |
| 95 | ||
| 96 | // do not allow more than default amount of posts or | |
| 97 | // amount set through the RelatedPostsDecorator.maxPosts property | |
| 98 | 20 | if (relatedEntries.size() == maxPosts) { |
| 99 | 2 | break; |
| 100 | } | |
| 101 | } | |
| 102 | ||
| 103 | 6 | if (relatedEntries.size() == 0) |
| 104 | 2 | buf.append("<i>" + I18n.getMessage(blog, "common.noRelatedPosts") + "</i>"); |
| 105 | ||
| 106 | 6 | buf.append("</p><br />"); |
| 107 | 6 | blogEntry.setBody(buf.toString()); |
| 108 | } | |
| 109 | } | |
| 110 | } | |
|
||||||||||||