Coverage Report - net.sourceforge.pebble.decorator.EscapeMarkupDecorator
 
Classes in this File Line Coverage Branch Coverage Complexity
EscapeMarkupDecorator
100%
23/23
100%
6/6
2.333
 
 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.domain.BlogEntry;
 36  
 import net.sourceforge.pebble.domain.StaticPage;
 37  
 import net.sourceforge.pebble.util.StringUtils;
 38  
 import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
 39  
 
 40  
 import java.util.regex.Matcher;
 41  
 import java.util.regex.Pattern;
 42  
 
 43  
 /**
 44  
  * Escapes < and > tags in the excerpt/body of blog entries and
 45  
  * the body of static pages.
 46  
  * 
 47  
  * @author Simon Brown
 48  
  */
 49  2732
 public class EscapeMarkupDecorator extends ContentDecoratorSupport {
 50  
 
 51  
   private static final String ESCAPE_START_TAG = "<escape>";
 52  
   private static final String ESCAPE_END_TAG = "</escape>";
 53  
 
 54  
   /**
 55  
    * Decorates the specified blog entry.
 56  
    *
 57  
    * @param context   the context in which the decoration is running
 58  
    * @param blogEntry the blog entry to be decorated
 59  
    */
 60  
   public void decorate(ContentDecoratorContext context, BlogEntry blogEntry) {
 61  84
     String escapedBody = escape(blogEntry.getBody());
 62  84
     blogEntry.setBody(escapedBody);
 63  
 
 64  84
     String escapedExcerpt = escape(blogEntry.getExcerpt());
 65  84
     blogEntry.setExcerpt(escapedExcerpt);
 66  84
   }
 67  
 
 68  
   /**
 69  
    * Decorates the specified static page.
 70  
    *
 71  
    * @param context    the context in which the decoration is running
 72  
    * @param staticPage the static page to be decorated
 73  
    */
 74  
   public void decorate(ContentDecoratorContext context, StaticPage staticPage) {
 75  28
     String escapedBody = escape(staticPage.getBody());
 76  28
     staticPage.setBody(escapedBody);
 77  28
   }
 78  
 
 79  
   private String escape(String content) {
 80  
     // is there work to do?
 81  196
     if (content == null || content.length() == 0) {
 82  116
       return "";
 83  
     }
 84  
 
 85  
     // this pattern says "take the shortest match you can find where there are
 86  
     // one or more characters between escape tags"
 87  
     //  - the match is case insensitive and DOTALL means that newlines are
 88  
     //  - considered as a character match
 89  80
     Pattern p = Pattern.compile(ESCAPE_START_TAG + ".+?" + ESCAPE_END_TAG,
 90  
         Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
 91  80
     Matcher m = p.matcher(content);
 92  
 
 93  
     // while there are blocks to be escaped
 94  128
     while (m.find()) {
 95  48
       int start = m.start();
 96  48
       int end = m.end();
 97  
 
 98  
       // grab the text, strip off the escape tags and transform it
 99  48
       String textToEscape = content.substring(start, end);
 100  48
       textToEscape = textToEscape.substring(ESCAPE_START_TAG.length(), textToEscape.length() - ESCAPE_END_TAG.length());
 101  48
       textToEscape = StringUtils.transformHTML(textToEscape);
 102  
 
 103  
       // now add it back into the original text
 104  48
       content = content.substring(0, start) + textToEscape + content.substring(end, content.length());
 105  48
       m = p.matcher(content);
 106  48
     }
 107  
 
 108  80
     return content;
 109  
   }
 110  
 
 111  
 }