Coverage Report - net.sourceforge.pebble.decorator.SocialBookmarksDecorator
 
Classes in this File Line Coverage Branch Coverage Complexity
SocialBookmarksDecorator
95%
39/41
84%
22/26
8
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, Simon Brown
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions are met:
 7  
  *
 8  
  *   - Redistributions of source code must retain the above copyright
 9  
  *     notice, this list of conditions and the following disclaimer.
 10  
  *
 11  
  *   - Redistributions in binary form must reproduce the above copyright
 12  
  *     notice, this list of conditions and the following disclaimer in
 13  
  *     the documentation and/or other materials provided with the
 14  
  *     distribution.
 15  
  *
 16  
  *   - Neither the name of Pebble nor the names of its contributors may
 17  
  *     be used to endorse or promote products derived from this software
 18  
  *     without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 31  
  */
 32  
 
 33  
 package net.sourceforge.pebble.decorator;
 34  
 
 35  
 import java.io.UnsupportedEncodingException;
 36  
 import java.net.URLEncoder;
 37  
 import java.util.ResourceBundle;
 38  
 
 39  
 import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
 40  
 import net.sourceforge.pebble.domain.Blog;
 41  
 import net.sourceforge.pebble.domain.BlogEntry;
 42  
 import net.sourceforge.pebble.util.I18n;
 43  
 
 44  
 /**
 45  
  * Adds links to the social bookmarking sites to add current blog entry
 46  
  * 
 47  
  * @author Alexander Zagniotov
 48  
  */
 49  4
 public class SocialBookmarksDecorator extends ContentDecoratorSupport {
 50  
 
 51  
         private static final String TITLE = "&title=";
 52  
         private static final String TITLE_FURL_YAHOO = "&t=";
 53  
 
 54  
         private static final String SLASHDOT_URL = "http://slashdot.org/bookmark.pl?url=";
 55  
         private static final String DIGG_URL = "http://digg.com/submit?url=";
 56  
         private static final String REDDIT_URL = "http://reddit.com/submit?url=";
 57  
         private static final String DELICIOUS_URL = "http://del.icio.us/post?url=";
 58  
         private static final String STUMBLEUPON_URL = "http://www.stumbleupon.com/submit?url=";
 59  
         private static final String GOOGLE_URL = "http://www.google.com/bookmarks/mark?op=edit&bkmk=";
 60  
         private static final String TECHNORATI_URL = "http://technorati.com/faves?add=";
 61  
         private static final String BLOGLINES_URL = "http://www.bloglines.com/sub/";
 62  
         private static final String FACEBOOK_URL = "http://www.facebook.com/share.php?u=";
 63  
         private static final String FURL_URL = "http://www.furl.net/storeIt.jsp?u=";
 64  
         private static final String WINDOWSLIVE_URL = "https://favorites.live.com/quickadd.aspx?mkt=en-us&url=";
 65  
         private static final String YAHOO_URL = "http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&u=";
 66  
 
 67  
         private static final String SLASHDOT_IMG = "<img src=\"common/images/slashdot.png\" alt=\"Add this post to Slashdot\" border=\"0\" />";
 68  
         private static final String DIGG_IMG = "<img src=\"common/images/digg.png\" alt=\"Add this post to Digg\" border=\"0\" />";
 69  
         private static final String REDDIT_IMG = "<img src=\"common/images/reddit.png\" alt=\"Add this post to Reddit\" border=\"0\" />";
 70  
         private static final String DELICIOUS_IMG = "<img src=\"common/images/delicious.png\" alt=\"Add this post to Delicious\" border=\"0\" />";
 71  
         private static final String STUMBLEUPON_IMG = "<img src=\"common/images/stumbleupon.png\" alt=\"Add this post to Stumble it\" border=\"0\" />";
 72  
         private static final String GOOGLE_IMG = "<img src=\"common/images/google.png\" alt=\"Add this post to Google\" border=\"0\" />";
 73  
         private static final String TECHNORATI_IMG = "<img src=\"common/images/technorati.png\" alt=\"Add this post to Technorati\" border=\"0\" />";
 74  
         private static final String BLOGLINES_IMG = "<img src=\"common/images/bloglines.png\" alt=\"Add this post to Bloglines\" border=\"0\" />";
 75  
         private static final String FACEBOOK_IMG = "<img src=\"common/images/facebook.png\" alt=\"Add this post to Facebook\" border=\"0\" />";
 76  
         private static final String FURL_IMG = "<img src=\"common/images/furl.png\" alt=\"Add this post to Furl\" border=\"0\" />";
 77  
         private static final String WINDOWSLIVE_IMG = "<img src=\"common/images/windowslive.png\" alt=\"Add this post to Windows Live\" border=\"0\" />";
 78  
         private static final String YAHOO_IMG = "<img src=\"common/images/yahoo.png\" alt=\"Add this post to Yahoo!\" border=\"0\" />";
 79  
 
 80  
         private static final String SLASHDOT_ALT = "socialbookmark.addToSlashdot";
 81  
         private static final String DIGG_ALT = "socialbookmark.addToDigg";
 82  
         private static final String REDDIT_ALT = "socialbookmark.addToReddit";
 83  
         private static final String DELICIOUS_ALT = "socialbookmark.addToDelicious";
 84  
         private static final String STUMBLEUPON_ALT = "socialbookmark.addToStumbleupon";
 85  
         private static final String GOOGLE_ALT = "socialbookmark.addToGoogle";
 86  
         private static final String TECHNORATI_ALT = "socialbookmark.addToTechnorati";
 87  
         private static final String BLOGLINES_ALT = "socialbookmark.addToBloglines";
 88  
         private static final String FACEBOOK_ALT = "socialbookmark.addToFacebook";
 89  
         private static final String FURL_ALT = "socialbookmark.addToFurl";
 90  
         private static final String WINDOWSLIVE_ALT = "socialbookmark.addToWindowsLive";
 91  
         private static final String YAHOO_ALT = "socialbookmark.addToYahoo";
 92  
 
 93  4
         private static final String[] bookmarkingSites = { SLASHDOT_URL, DIGG_URL,
 94  
                         REDDIT_URL, DELICIOUS_URL, STUMBLEUPON_URL, GOOGLE_URL, TECHNORATI_URL, BLOGLINES_URL, FACEBOOK_URL,
 95  
                         FURL_URL, WINDOWSLIVE_URL, YAHOO_URL};
 96  
 
 97  4
         private static final String[] bookmarkingNames = { SLASHDOT_IMG, DIGG_IMG,
 98  
                         REDDIT_IMG, DELICIOUS_IMG, STUMBLEUPON_IMG, GOOGLE_IMG, TECHNORATI_IMG, BLOGLINES_IMG, FACEBOOK_IMG,
 99  
                         FURL_IMG, WINDOWSLIVE_IMG, YAHOO_IMG};
 100  
 
 101  4
         private static final String[] bookmarkingAltText = { SLASHDOT_ALT, DIGG_ALT,
 102  
                         REDDIT_ALT, DELICIOUS_ALT, STUMBLEUPON_ALT, GOOGLE_ALT, TECHNORATI_ALT, BLOGLINES_ALT, FACEBOOK_ALT,
 103  
                         FURL_ALT, WINDOWSLIVE_ALT, YAHOO_ALT};
 104  
 
 105  
         /**
 106  
          * Decorates the specified blog entry.
 107  
          * 
 108  
          * @param context
 109  
          *            the context in which the decoration is running
 110  
          * @param blogEntry
 111  
          *            the blog entry to be decorated
 112  
          */
 113  
         public void decorate(ContentDecoratorContext context, BlogEntry blogEntry) {
 114  4
                 Blog blog = blogEntry.getBlog();
 115  4
                 ResourceBundle bundle = I18n.getBundle(blog.getLocale());
 116  
 
 117  4
                 String body = blogEntry.getBody();
 118  4
                 if (body != null && body.trim().length() > 0) {
 119  
 
 120  4
                         String html = generateDecorationHtml(bundle, blogEntry);
 121  4
                         blogEntry.setBody(body + html);
 122  
                 }
 123  
 
 124  4
                 String excerpt = blogEntry.getExcerpt();
 125  4
                 if (excerpt != null && excerpt.trim().length() > 0) {
 126  
 
 127  4
                         String html = generateDecorationHtml(bundle, blogEntry);
 128  4
                         blogEntry.setExcerpt(excerpt + html);
 129  
                 }
 130  4
         }
 131  
 
 132  
         
 133  
 
 134  
         private String generateDecorationHtml(ResourceBundle bundle,
 135  
                         BlogEntry blogEntry) {
 136  8
                 StringBuffer buf = new StringBuffer();
 137  8
                 String permLink = blogEntry.getPermalink();
 138  8
                 String title = "";
 139  
                 try {
 140  8
                         title = URLEncoder.encode(blogEntry.getTitle(), "UTF-8");
 141  
                 }
 142  0
                 catch(UnsupportedEncodingException e) {
 143  0
                         title = blogEntry.getTitle();
 144  8
                 }
 145  8
                 buf.append("<div class=\"tags\"><span>");
 146  8
                 buf.append(bundle.getString("common.bookmarks"));
 147  8
                 buf.append(" : </span>&nbsp;");
 148  
 
 149  104
                 for (int i = 0; i < bookmarkingSites.length; i++) {
 150  96
                         buf.append("<a href=\"");
 151  96
                         buf.append(bookmarkingSites[i] + permLink);
 152  
 
 153  
 
 154  96
                         if (bookmarkingSites[i] != TECHNORATI_URL && 
 155  
                                 bookmarkingSites[i] != BLOGLINES_URL && 
 156  
                                 bookmarkingSites[i] != FACEBOOK_URL &&
 157  
                                 bookmarkingSites[i] != FURL_URL &&
 158  
                                 bookmarkingSites[i] != YAHOO_URL) {
 159  56
                                 buf.append(TITLE + title + "\"");
 160  
                         } 
 161  
 
 162  40
                         else if (bookmarkingSites[i] == FURL_URL || bookmarkingSites[i] == YAHOO_URL) {
 163  16
                                 buf.append(TITLE_FURL_YAHOO + title + "\"");
 164  
                         }
 165  
 
 166  
                         else {
 167  24
                                 buf.append("\"");
 168  
                         }
 169  
 
 170  96
                         buf.append(" target=\"_blank\"");
 171  96
                         buf.append(" title=\"" + bundle.getString(bookmarkingAltText[i]) + "\">");
 172  96
                         buf.append(bookmarkingNames[i]);
 173  96
                         buf.append("</a>");
 174  
 
 175  96
                         if (i < bookmarkingSites.length - 1) {
 176  88
                                 buf.append("&nbsp;&nbsp;&nbsp;&nbsp;");
 177  
                         }
 178  
                 }
 179  
 
 180  8
                 buf.append("</div>");
 181  8
                 return buf.toString();
 182  
         }
 183  
 
 184  
 }