File Source: nofollowdecorator.java

     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   * Adds a rel="nofollow" attribute into all links within comment
    12   * and TrackBack content.
    13   * 
    14   * @author Simon Brown
    15   */
         /* 
    P/P   *  Method: void net.sourceforge.pebble.decorator.NoFollowDecorator()
          */
    16  public class NoFollowDecorator extends ContentDecoratorSupport {
    17  
    18    /** the regex used to find HTML links */
           /* 
    P/P     *  Method: net.sourceforge.pebble.decorator.NoFollowDecorator__static_init
            * 
            *  Postconditions:
            *    init'ed(HTML_LINK_PATTERN)
            */
    19    private static Pattern HTML_LINK_PATTERN = Pattern.compile("<a.*?href=.*?>", Pattern.CASE_INSENSITIVE);
    20  
    21    /**
    22     * Decorates the specified comment.
    23     *
    24     * @param context the context in which the decoration is running
    25     * @param comment the comment to be decorated
    26     */
    27    public void decorate(ContentDecoratorContext context, Comment comment) {
             /* 
    P/P       *  Method: void decorate(ContentDecoratorContext, Comment)
              * 
              *  Preconditions:
              *    comment != null
              *    (soft) HTML_LINK_PATTERN != null
              *    init'ed(comment.body)
              * 
              *  Postconditions:
              *    init'ed(comment.body)
              */
    28      comment.setBody(addNoFollowLinks(comment.getBody()));
    29    }
    30  
    31    /**
    32     * Decorates the specified TrackBack.
    33     *
    34     * @param context   the context in which the decoration is running
    35     * @param trackBack the TrackBack to be decorated
    36     */
    37    public void decorate(ContentDecoratorContext context, TrackBack trackBack) {
             /* 
    P/P       *  Method: void decorate(ContentDecoratorContext, TrackBack)
              * 
              *  Preconditions:
              *    trackBack != null
              *    (soft) HTML_LINK_PATTERN != null
              *    init'ed(trackBack.excerpt)
              * 
              *  Postconditions:
              *    trackBack.excerpt != null
              */
    38      trackBack.setExcerpt(addNoFollowLinks(trackBack.getExcerpt()));
    39    }
    40  
    41    /**
    42     * Adds rel="nofollow" links too any links in the specified string.
    43     *
    44     * @param   html    a String containing HTML
    45     * @return  the same String with rel="nofollow" in anchor tags
    46     */
    47    private String addNoFollowLinks(String html) {
             /* 
    P/P       *  Method: String addNoFollowLinks(String)
              * 
              *  Preconditions:
              *    (soft) HTML_LINK_PATTERN != null
              * 
              *  Presumptions:
              *    java.lang.String:indexOf(...)@63 <= 232-6
              *    java.util.regex.Pattern:matcher(...)@52 != null
              * 
              *  Postconditions:
              *    init'ed(return_value)
              * 
              *  Test Vectors:
              *    html: Addr_Set{null}, Inverse{null}
              *    java.lang.String:indexOf(...)@63: {-231..-2, 0..232-6}, {-1}
              *    java.lang.String:indexOf(...)@72: {-231..-2, 0..232-1}, {-1}
              *    java.lang.String:length(...)@48: {1..232-1}, {0}
              *    java.util.regex.Matcher:find(...)@55: {1}, {0}
              */
    48      if (html == null || html.length() == 0) {
    49        return html;
    50      }
    51  
    52      Matcher m = HTML_LINK_PATTERN.matcher(html);
    53      StringBuffer buf = new StringBuffer();
    54  
    55      while (m.find()) {
    56        int start = m.start();
    57        int end = m.end();
    58  
    59        String link = html.substring(start, end);
    60        buf.append(html.substring(0, start));
    61  
    62        // add the nofollow, if necessary
    63        int startOfRelIndex = link.indexOf("rel=\"");
    64        if (startOfRelIndex == -1) {
    65          // no rel link, add one
    66          buf.append(link.substring(0, link.length() - 1));
    67          buf.append(" rel=\"nofollow\">");
    68        } else {
    69          int endOfRelIndex = link.indexOf("\"", startOfRelIndex+5);
    70          String rel = link.substring(startOfRelIndex+5, endOfRelIndex);
    71          rel = rel.toLowerCase();
    72          if (rel.indexOf("nofollow") == -1) {
    73            // rel exists, but without nofollow
    74            buf.append(link.substring(0, endOfRelIndex));
    75            buf.append(" nofollow");
    76            buf.append(link.substring(endOfRelIndex));
    77          } else {
    78            // nofollow exists
    79            buf.append(link);
    80          }
    81        }
    82  
    83        html = html.substring(end, html.length());
    84        m = HTML_LINK_PATTERN.matcher(html);
    85      }
    86  
    87      buf.append(html);
    88  
    89      return buf.toString();
    90    }
    91  
    92  }








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