File Source: abstracttagsdecorator.java

         /* 
    P/P   *  Method: net.sourceforge.pebble.decorator.AbstractTagsDecorator__static_init
          */
     1  package net.sourceforge.pebble.decorator;
     2  
     3  import java.util.Iterator;
     4  
     5  import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
     6  import net.sourceforge.pebble.domain.PageBasedContent;
     7  import net.sourceforge.pebble.domain.Tag;
     8  import net.sourceforge.pebble.util.I18n;
     9  
    10  /**
    11   * Generates tag links for inclusion in the body of blog entries,
    12   * when rendered as HTML.
    13   * 
    14   * @author Simon Brown
    15   */
    16  public abstract class AbstractTagsDecorator extends ContentDecoratorSupport {
    17    private final String resourceKey;
    18    private final String target;
    19  
    20    /**
    21     * Extended Parameters for generating Links to different Tagging sites - like Technorati.
    22     * @param resourceKey is used to determine the label for the tags from pebbles resource files
    23     * @param openLinkInNewWindow set to true to generate links with 'target="_blank"' 
    24     */
    25  
           /* 
    P/P     *  Method: void net.sourceforge.pebble.decorator.AbstractTagsDecorator(String, bool)
            * 
            *  Postconditions:
            *    this.resourceKey == resourceKey
            *    init'ed(this.resourceKey)
            *    this.target == One-of{&" target="_blank"", &""}
            *    this.target in Addr_Set{&"",&" target="_blank""}
            */
    26    public AbstractTagsDecorator(String resourceKey, boolean openLinkInNewWindow) {
    27  	this.resourceKey = resourceKey;
    28  	target=openLinkInNewWindow ? " target=\"_blank\"":"";
    29    }
    30  	
    31    /**
    32     * Default constructors makes Tags use the standard label (key tag.tags) and open links
    33     * in the same browser window. 
    34     */
    35  
           /* 
    P/P     *  Method: void net.sourceforge.pebble.decorator.AbstractTagsDecorator()
            * 
            *  Postconditions:
            *    this.resourceKey == &"tag.tags"
            *    this.target == &""
            */
    36    public AbstractTagsDecorator() {
    37  	this.resourceKey = "tag.tags";
    38  	target="";
    39    }
    40  	
    41    protected String generateDecorationHtml(ContentDecoratorContext context, PageBasedContent content) {
             /* 
    P/P       *  Method: String generateDecorationHtml(ContentDecoratorContext, PageBasedContent)
              * 
              *  Preconditions:
              *    context != null
              *    (soft) content != null
              * 
              *  Presumptions:
              *    java.util.Iterator:next(...)@56 != null
              * 
              *  Postconditions:
              *    return_value != null
              * 
              *  Test Vectors:
              *    java.lang.String:equals(...)@58: {1}, {0}
              *    java.util.Iterator:hasNext(...)@49: {0}, {1}
              *    java.util.Iterator:hasNext(...)@54: {1}, {0}
              *    java.util.Iterator:hasNext(...)@68: {0}, {1}
              * 
              *  Preconditions:
              *    init'ed(context.media)
              *    (soft) init'ed(content.blog)
              *    (soft) init'ed(content.tagsAsList)
              * 
              *  Test Vectors:
              *    context.media: {-231..-1, 1..232-1}, {0}
              *    tag.name@56: Addr_Set{null}, Inverse{null}
              */
    42      StringBuffer buf = new StringBuffer();
    43  
    44      if (context.getMedia() == ContentDecoratorContext.HTML_PAGE) {
    45        Iterator<Tag> tags = content.getAllTags().iterator();
    46  
    47        String baseUrl = getBaseUrl(content);
    48  
    49        if (tags.hasNext()) {
    50          buf.append("<div class=\"tags\"><span>");
    51  		buf.append(I18n.getMessage(content.getBlog(), resourceKey));
    52          buf.append(" : </span>");
    53  
    54          while (tags.hasNext()) {
    55  
    56            Tag tag = tags.next();
    57  
    58  		  if (tag.getName() != null && !tag.getName().equals("")) {
    59  
    60  			  buf.append("<a href=\"");
    61  			  buf.append(baseUrl);
    62  			  buf.append(tag.getName() + "\"");
    63  			  buf.append(target);
    64  			  buf.append(" rel=\"tag\">");
    65  			  buf.append(tag.getName());
    66  			  buf.append("</a>");
    67  
    68  			  if (tags.hasNext()) {
    69  				buf.append(", ");
    70  			  }
    71  		  }
    72          }
    73          buf.append("</div>");
    74  
    75        }
    76      }
    77  
    78      return buf.toString();
    79    }
    80  
    81    /**
    82     * Gets the base URL for tag links, complete with trailing slash.
    83     *
    84     * @param content   the owning content
    85     * @return  a URL as a String
    86     */
    87    public abstract String getBaseUrl(PageBasedContent content);
    88  }








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