Coverage Report - net.sourceforge.pebble.decorator.NoFollowDecorator
 
Classes in this File Line Coverage Branch Coverage Complexity
NoFollowDecorator
96%
31/32
80%
8/10
3
 
 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 net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
 36  
 import net.sourceforge.pebble.domain.Comment;
 37  
 import net.sourceforge.pebble.domain.TrackBack;
 38  
 
 39  
 import java.util.regex.Matcher;
 40  
 import java.util.regex.Pattern;
 41  
 
 42  
 /**
 43  
  * Adds a rel="nofollow" attribute into all links within comment
 44  
  * and TrackBack content.
 45  
  * 
 46  
  * @author Simon Brown
 47  
  */
 48  8
 public class NoFollowDecorator extends ContentDecoratorSupport {
 49  
 
 50  
   /** the regex used to find HTML links */
 51  4
   private static Pattern HTML_LINK_PATTERN = Pattern.compile("<a.*?href=.*?>", Pattern.CASE_INSENSITIVE);
 52  
 
 53  
   /**
 54  
    * Decorates the specified comment.
 55  
    *
 56  
    * @param context the context in which the decoration is running
 57  
    * @param comment the comment to be decorated
 58  
    */
 59  
   public void decorate(ContentDecoratorContext context, Comment comment) {
 60  80
     comment.setBody(addNoFollowLinks(comment.getBody()));
 61  80
   }
 62  
 
 63  
   /**
 64  
    * Decorates the specified TrackBack.
 65  
    *
 66  
    * @param context   the context in which the decoration is running
 67  
    * @param trackBack the TrackBack to be decorated
 68  
    */
 69  
   public void decorate(ContentDecoratorContext context, TrackBack trackBack) {
 70  16
     trackBack.setExcerpt(addNoFollowLinks(trackBack.getExcerpt()));
 71  16
   }
 72  
 
 73  
   /**
 74  
    * Adds rel="nofollow" links too any links in the specified string.
 75  
    *
 76  
    * @param   html    a String containing HTML
 77  
    * @return  the same String with rel="nofollow" in anchor tags
 78  
    */
 79  
   private String addNoFollowLinks(String html) {
 80  96
     if (html == null || html.length() == 0) {
 81  0
       return html;
 82  
     }
 83  
 
 84  96
     Matcher m = HTML_LINK_PATTERN.matcher(html);
 85  96
     StringBuffer buf = new StringBuffer();
 86  
 
 87  160
     while (m.find()) {
 88  64
       int start = m.start();
 89  64
       int end = m.end();
 90  
 
 91  64
       String link = html.substring(start, end);
 92  64
       buf.append(html.substring(0, start));
 93  
 
 94  
       // add the nofollow, if necessary
 95  64
       int startOfRelIndex = link.indexOf("rel=\"");
 96  64
       if (startOfRelIndex == -1) {
 97  
         // no rel link, add one
 98  48
         buf.append(link.substring(0, link.length() - 1));
 99  48
         buf.append(" rel=\"nofollow\">");
 100  
       } else {
 101  16
         int endOfRelIndex = link.indexOf("\"", startOfRelIndex+5);
 102  16
         String rel = link.substring(startOfRelIndex+5, endOfRelIndex);
 103  16
         rel = rel.toLowerCase();
 104  16
         if (rel.indexOf("nofollow") == -1) {
 105  
           // rel exists, but without nofollow
 106  8
           buf.append(link.substring(0, endOfRelIndex));
 107  8
           buf.append(" nofollow");
 108  8
           buf.append(link.substring(endOfRelIndex));
 109  
         } else {
 110  
           // nofollow exists
 111  8
           buf.append(link);
 112  
         }
 113  
       }
 114  
 
 115  64
       html = html.substring(end, html.length());
 116  64
       m = HTML_LINK_PATTERN.matcher(html);
 117  64
     }
 118  
 
 119  96
     buf.append(html);
 120  
 
 121  96
     return buf.toString();
 122  
   }
 123  
 
 124  
 }