File Source: socialbookmarksdecorator.java

     1  package net.sourceforge.pebble.decorator;
     2  
     3  import java.io.UnsupportedEncodingException;
     4  import java.net.URLEncoder;
     5  import java.util.ResourceBundle;
     6  
     7  import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
     8  import net.sourceforge.pebble.domain.Blog;
     9  import net.sourceforge.pebble.domain.BlogEntry;
    10  import net.sourceforge.pebble.util.I18n;
    11  
    12  /**
    13   * Adds links to the social bookmarking sites to add current blog entry
    14   * 
    15   * @author Alexander Zagniotov
    16   */
         /* 
    P/P   *  Method: void net.sourceforge.pebble.decorator.SocialBookmarksDecorator()
          */
    17  public class SocialBookmarksDecorator extends ContentDecoratorSupport {
    18  
    19  	private static final String TITLE = "&title=";
    20  	private static final String TITLE_FURL_YAHOO = "&t=";
    21  
    22  	private static final String SLASHDOT_URL = "http://slashdot.org/bookmark.pl?url=";
    23  	private static final String DIGG_URL = "http://digg.com/submit?url=";
    24  	private static final String REDDIT_URL = "http://reddit.com/submit?url=";
    25  	private static final String DELICIOUS_URL = "http://del.icio.us/post?url=";
    26  	private static final String STUMBLEUPON_URL = "http://www.stumbleupon.com/submit?url=";
    27  	private static final String GOOGLE_URL = "http://www.google.com/bookmarks/mark?op=edit&bkmk=";
    28  	private static final String TECHNORATI_URL = "http://technorati.com/faves?add=";
    29  	private static final String BLOGLINES_URL = "http://www.bloglines.com/sub/";
    30  	private static final String FACEBOOK_URL = "http://www.facebook.com/share.php?u=";
    31  	private static final String FURL_URL = "http://www.furl.net/storeIt.jsp?u=";
    32  	private static final String WINDOWSLIVE_URL = "https://favorites.live.com/quickadd.aspx?mkt=en-us&url=";
    33  	private static final String YAHOO_URL = "http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&u=";
    34  
    35  	private static final String SLASHDOT_IMG = "<img src=\"common/images/slashdot.png\" alt=\"Add this post to Slashdot\" border=\"0\" />";
    36  	private static final String DIGG_IMG = "<img src=\"common/images/digg.png\" alt=\"Add this post to Digg\" border=\"0\" />";
    37  	private static final String REDDIT_IMG = "<img src=\"common/images/reddit.png\" alt=\"Add this post to Reddit\" border=\"0\" />";
    38  	private static final String DELICIOUS_IMG = "<img src=\"common/images/delicious.png\" alt=\"Add this post to Delicious\" border=\"0\" />";
    39  	private static final String STUMBLEUPON_IMG = "<img src=\"common/images/stumbleupon.png\" alt=\"Add this post to Stumble it\" border=\"0\" />";
    40  	private static final String GOOGLE_IMG = "<img src=\"common/images/google.png\" alt=\"Add this post to Google\" border=\"0\" />";
    41  	private static final String TECHNORATI_IMG = "<img src=\"common/images/technorati.png\" alt=\"Add this post to Technorati\" border=\"0\" />";
    42  	private static final String BLOGLINES_IMG = "<img src=\"common/images/bloglines.png\" alt=\"Add this post to Bloglines\" border=\"0\" />";
    43  	private static final String FACEBOOK_IMG = "<img src=\"common/images/facebook.png\" alt=\"Add this post to Facebook\" border=\"0\" />";
    44  	private static final String FURL_IMG = "<img src=\"common/images/furl.png\" alt=\"Add this post to Furl\" border=\"0\" />";
    45  	private static final String WINDOWSLIVE_IMG = "<img src=\"common/images/windowslive.png\" alt=\"Add this post to Windows Live\" border=\"0\" />";
    46  	private static final String YAHOO_IMG = "<img src=\"common/images/yahoo.png\" alt=\"Add this post to Yahoo!\" border=\"0\" />";
    47  
    48  	private static final String SLASHDOT_ALT = "socialbookmark.addToSlashdot";
    49  	private static final String DIGG_ALT = "socialbookmark.addToDigg";
    50  	private static final String REDDIT_ALT = "socialbookmark.addToReddit";
    51  	private static final String DELICIOUS_ALT = "socialbookmark.addToDelicious";
    52  	private static final String STUMBLEUPON_ALT = "socialbookmark.addToStumbleupon";
    53  	private static final String GOOGLE_ALT = "socialbookmark.addToGoogle";
    54  	private static final String TECHNORATI_ALT = "socialbookmark.addToTechnorati";
    55  	private static final String BLOGLINES_ALT = "socialbookmark.addToBloglines";
    56  	private static final String FACEBOOK_ALT = "socialbookmark.addToFacebook";
    57  	private static final String FURL_ALT = "socialbookmark.addToFurl";
    58  	private static final String WINDOWSLIVE_ALT = "socialbookmark.addToWindowsLive";
    59  	private static final String YAHOO_ALT = "socialbookmark.addToYahoo";
    60  
        	 /* 
    P/P 	  *  Method: net.sourceforge.pebble.decorator.SocialBookmarksDecorator__static_init
        	  * 
        	  *  Postconditions:
        	  *    bookmarkingAltText == &new String[](SocialBookmarksDecorator__static_init#3)
        	  *    bookmarkingNames == &new String[](SocialBookmarksDecorator__static_init#2)
        	  *    bookmarkingSites == &new String[](SocialBookmarksDecorator__static_init#1)
        	  *    new String[](SocialBookmarksDecorator__static_init#1) num objects == 1
        	  *    new String[](SocialBookmarksDecorator__static_init#2) num objects == 1
        	  *    new String[](SocialBookmarksDecorator__static_init#3) num objects == 1
        	  *    bookmarkingSites.length == 12
        	  *    bookmarkingNames.length == 12
        	  *    bookmarkingAltText.length == 12
        	  *    bookmarkingSites[...] in Addr_Set{&"http:..slashdot.org.bookmark.pl?url=",&"http:..digg.com.submit?url=",&"http:..reddit.com.submit?url=",&"http:..del.icio.us.post?url=",&"http:..www.stumbleupon.com.submit?url=",&"http:..www.google.com.bookmarks.mark?op=edit&amp;amp;bkmk=",&"http:..technorati.com.faves?add=",&"http:..www.bloglines.com.sub.",&"http:..www.facebook.com.share.php?u=",&"http:..www.furl.net.storeIt.jsp?u=",&"https:..favorites.live.com.quickadd.aspx?mkt=en-us&amp;amp;url=",&"http:..bookmarks.yahoo.com.toolbar.savebm?opener=tb&amp;amp;u="}
        	  *    ...
        	  */
    61  	private static final String[] bookmarkingSites = { SLASHDOT_URL, DIGG_URL,
    62  			REDDIT_URL, DELICIOUS_URL, STUMBLEUPON_URL, GOOGLE_URL, TECHNORATI_URL, BLOGLINES_URL, FACEBOOK_URL,
    63  			FURL_URL, WINDOWSLIVE_URL, YAHOO_URL};
    64  
    65  	private static final String[] bookmarkingNames = { SLASHDOT_IMG, DIGG_IMG,
    66  			REDDIT_IMG, DELICIOUS_IMG, STUMBLEUPON_IMG, GOOGLE_IMG, TECHNORATI_IMG, BLOGLINES_IMG, FACEBOOK_IMG,
    67  			FURL_IMG, WINDOWSLIVE_IMG, YAHOO_IMG};
    68  
    69  	private static final String[] bookmarkingAltText = { SLASHDOT_ALT, DIGG_ALT,
    70  			REDDIT_ALT, DELICIOUS_ALT, STUMBLEUPON_ALT, GOOGLE_ALT, TECHNORATI_ALT, BLOGLINES_ALT, FACEBOOK_ALT,
    71  			FURL_ALT, WINDOWSLIVE_ALT, YAHOO_ALT};
    72  
    73  	/**
    74  	 * Decorates the specified blog entry.
    75  	 * 
    76  	 * @param context
    77  	 *            the context in which the decoration is running
    78  	 * @param blogEntry
    79  	 *            the blog entry to be decorated
    80  	 */
    81  	public void decorate(ContentDecoratorContext context, BlogEntry blogEntry) {
        		 /* 
    P/P 		  *  Method: void decorate(ContentDecoratorContext, BlogEntry)
        		  * 
        		  *  Preconditions:
        		  *    blogEntry != null
        		  *    (soft) init'ed(bookmarkingSites[...])
        		  *    (soft) init'ed(bookmarkingNames[...])
        		  *    (soft) init'ed(bookmarkingAltText[...])
        		  * 
        		  *  Presumptions:
        		  *    net.sourceforge.pebble.util.I18n:getBundle(...)@83 != null
        		  * 
        		  *  Test Vectors:
        		  *    java.lang.String:length(...)@86: {0}, {1..232-1}
        		  *    java.lang.String:length(...)@93: {0}, {1..232-1}
        		  * 
        		  *  Preconditions:
        		  *    blogEntry.blog != null
        		  *    init'ed(blogEntry.body)
        		  *    init'ed(blogEntry.excerpt)
        		  *    (soft) init'ed(blogEntry.originalPermalink)
        		  *    (soft) blogEntry.propertyChangeSupport != null
        		  *    (soft) init'ed(blogEntry.title)
        		  * 
        		  *  Postconditions:
        		  *    init'ed(blogEntry.body)
        		  *    init'ed(blogEntry.excerpt)
        		  *    possibly_updated(blogEntry.permalink)
        		  * 
        		  *  Preconditions:
        		  *    blogEntry.blog.properties != null
        		  *    (soft) init'ed(blogEntry.blog.id)
        		  *    (soft) net/sourceforge/pebble/domain/BlogManager.instance != null
        		  *    (soft) init'ed(net/sourceforge/pebble/domain/BlogManager.instance.multiBlog)
        		  * 
        		  *  Test Vectors:
        		  *    blogEntry.body: Addr_Set{null}, Inverse{null}
        		  *    blogEntry.excerpt: Addr_Set{null}, Inverse{null}
        		  */
    82  		Blog blog = blogEntry.getBlog();
    83  		ResourceBundle bundle = I18n.getBundle(blog.getLocale());
    84  
    85  		String body = blogEntry.getBody();
    86  		if (body != null && body.trim().length() > 0) {
    87  
    88  			String html = generateDecorationHtml(bundle, blogEntry);
    89  			blogEntry.setBody(body + html);
    90  		}
    91  
    92  		String excerpt = blogEntry.getExcerpt();
    93  		if (excerpt != null && excerpt.trim().length() > 0) {
    94  
    95  			String html = generateDecorationHtml(bundle, blogEntry);
    96  			blogEntry.setExcerpt(excerpt + html);
    97  		}
    98  	}
    99  
   100  	
   101  
   102  	private String generateDecorationHtml(ResourceBundle bundle,
   103  			BlogEntry blogEntry) {
        		 /* 
    P/P 		  *  Method: String generateDecorationHtml(ResourceBundle, BlogEntry)
        		  * 
        		  *  Preconditions:
        		  *    blogEntry != null
        		  *    bundle != null
        		  *    (soft) init'ed(bookmarkingSites[...])
        		  *    (soft) init'ed(bookmarkingNames[...])
        		  *    (soft) init'ed(bookmarkingAltText[...])
        		  * 
        		  *  Postconditions:
        		  *    return_value != null
        		  * 
        		  *  Test Vectors:
        		  *    bookmarkingSites[...]: Addr_Set{&"http:..technorati.com.faves?add="}, Addr_Set{&"http:..www.bloglines.com.sub."}, Addr_Set{&"http:..www.facebook.com.share.php?u="}, Addr_Set{&"http:..www.furl.net.storeIt.jsp?u="}, Addr_Set{&"http:..bookmarks.yahoo.com.toolbar.savebm?opener=tb&amp;amp;u="}, Inverse{&"http:..technorati.com.faves?add=",&"http:..www.bloglines.com.sub.",&"http:..www.facebook.com.share.php?u=",&"http:..www.furl.net.storeIt.jsp?u=",&"http:..bookmarks.yahoo.com.toolbar.savebm?opener=tb&amp;amp;u="}
        		  * 
        		  *  Preconditions:
        		  *    (soft) init'ed(blogEntry.blog.id)
        		  *    (soft) net/sourceforge/pebble/domain/BlogManager.instance != null
        		  *    (soft) init'ed(net/sourceforge/pebble/domain/BlogManager.instance.multiBlog)
        		  *    init'ed(blogEntry.title)
        		  *    (soft) blogEntry.blog != null
        		  *    (soft) init'ed(blogEntry.originalPermalink)
        		  * 
        		  *  Postconditions:
        		  *    possibly_updated(blogEntry.permalink)
        		  */
   104  		StringBuffer buf = new StringBuffer();
   105  		String permLink = blogEntry.getPermalink();
   106  		String title = "";
   107  		try {
   108  			title = URLEncoder.encode(blogEntry.getTitle(), "UTF-8");
   109  		}
   110  		catch(UnsupportedEncodingException e) {
   111  			title = blogEntry.getTitle();
   112  		}
   113  		buf.append("<div class=\"tags\"><span>");
   114  		buf.append(bundle.getString("common.bookmarks"));
   115  		buf.append(" : </span>&nbsp;");
   116  
   117  		for (int i = 0; i < bookmarkingSites.length; i++) {
   118  			buf.append("<a href=\"");
   119  			buf.append(bookmarkingSites[i] + permLink);
   120  
   121  
   122  			if (bookmarkingSites[i] != TECHNORATI_URL && 
   123  				bookmarkingSites[i] != BLOGLINES_URL && 
   124  				bookmarkingSites[i] != FACEBOOK_URL &&
   125  				bookmarkingSites[i] != FURL_URL &&
   126  				bookmarkingSites[i] != YAHOO_URL) {
   127  				buf.append(TITLE + title + "\"");
   128  			} 
   129  
   130  			else if (bookmarkingSites[i] == FURL_URL || bookmarkingSites[i] == YAHOO_URL) {
   131  				buf.append(TITLE_FURL_YAHOO + title + "\"");
   132  			}
   133  
   134  			else {
   135  				buf.append("\"");
   136  			}
   137  
   138  			buf.append(" target=\"_blank\"");
   139  			buf.append(" title=\"" + bundle.getString(bookmarkingAltText[i]) + "\">");
   140  			buf.append(bookmarkingNames[i]);
   141  			buf.append("</a>");
   142  
   143  			if (i < bookmarkingSites.length - 1) {
   144  				buf.append("&nbsp;&nbsp;&nbsp;&nbsp;");
   145  			}
   146  		}
   147  
   148  		buf.append("</div>");
   149  		return buf.toString();
   150  	}
   151  
   152  }








SofCheck Inspector Build Version : 2.22510
socialbookmarksdecorator.java 2010-Jun-25 19:40:32
socialbookmarksdecorator.class 2010-Jul-19 20:23:40