Coverage Report - net.sourceforge.pebble.decorator.SuffixDecorator
 
Classes in this File Line Coverage Branch Coverage Complexity
SuffixDecorator
0%
0/37
0%
0/16
11
 
 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 java.io.UnsupportedEncodingException;
 36  
 import java.net.URLEncoder;
 37  
 import java.util.*;
 38  
 
 39  
 import net.sourceforge.pebble.PluginProperties;
 40  
 import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
 41  
 import net.sourceforge.pebble.domain.Blog;
 42  
 import net.sourceforge.pebble.domain.BlogEntry;
 43  
 import net.sourceforge.pebble.util.I18n;
 44  
 
 45  
 /**
 46  
  * Adds user-defined suffix to each blog post. Suffix can contain
 47  
  * strings, eg ${blogEntry.title} or ${blogEntry.permaLink.encoded}
 48  
  * which will be substituted (naively).
 49  
  * 
 50  
  * @author Mike Bremford
 51  
  */
 52  0
 public class SuffixDecorator extends ContentDecoratorSupport {
 53  
 
 54  
     /**
 55  
      * The name of the configuration property which specifies the suffix
 56  
      */
 57  0
     public String SUFFIX = "SuffixDecorator.suffix";
 58  
 
 59  
     /**
 60  
      * Decorates the specified blog entry.
 61  
      * 
 62  
      * @param context the context in which the decoration is running
 63  
      * @param blogEntry the blog entry to be decorated
 64  
      */
 65  
     public void decorate(ContentDecoratorContext context, BlogEntry blogEntry) {
 66  0
         Blog blog = blogEntry.getBlog();
 67  0
         PluginProperties props = blog.getPluginProperties();
 68  
         String suffix;
 69  0
         synchronized(props) {
 70  0
             suffix = props.getProperty(SUFFIX);
 71  0
         }
 72  
 
 73  0
         if (suffix != null && suffix.trim().length() > 0) {
 74  0
             Map<String,String> map = new HashMap();
 75  0
             map.put("blogEntry.title", blogEntry.getTitle());
 76  0
             map.put("blogEntry.permalink", blogEntry.getPermalink());
 77  0
             map.put("blogEntry.excert", blogEntry.getExcerpt());
 78  0
             map.put("blogEntry.author", blogEntry.getAuthor());
 79  0
             map.put("blogEntry.id", blogEntry.getId());
 80  0
             map.put("blogEntry.subtitle", blogEntry.getSubtitle());
 81  0
             map.put("blogEntry.tags", blogEntry.getTags());
 82  
 
 83  
             // Scan over suffix matching for ${blogEntry.title} or ${blogEntry.title.encoded}
 84  
             // and replace the string with the appropriate property
 85  
 
 86  0
             for (Iterator<Map.Entry<String,String>> i = map.entrySet().iterator();i.hasNext();) {
 87  
                 try {
 88  0
                     Map.Entry<String,String> e = i.next();
 89  0
                     String val = e.getValue();
 90  0
                     if (val == null) {
 91  0
                         val = "";
 92  
                     }
 93  
 
 94  0
                     String key = "${"+e.getKey()+"}";
 95  0
                     for (int j=suffix.indexOf(key);j>=0;j=suffix.indexOf(key, j)) {
 96  0
                         suffix = suffix.substring(0, j) + val + suffix.substring(j + key.length());
 97  0
                         j += val.length() - key.length();
 98  
                     }
 99  
 
 100  0
                     key = "${"+e.getKey()+".encoded}";
 101  0
                     for (int j=suffix.indexOf(key);j>=0;j=suffix.indexOf(key, j)) {
 102  0
                         String eval = URLEncoder.encode(val, "UTF-8");
 103  0
                         suffix = suffix.substring(0, j) + eval + suffix.substring(j + key.length());
 104  0
                         j += eval.length() - key.length();
 105  
                     }
 106  0
                 } catch (UnsupportedEncodingException e) {
 107  0
                     throw new RuntimeException(e);      // Can't happem, UTF-8 is always there
 108  0
                 }
 109  
             }
 110  
 
 111  0
             String body = blogEntry.getBody();
 112  0
             if (body != null && body.trim().length() > 0) {
 113  0
                 blogEntry.setBody(body + suffix);
 114  
             }
 115  
         }
 116  0
     }
 117  
 
 118  
 }