Coverage Report - net.sourceforge.pebble.decorator.ContentDecoratorChain
 
Classes in this File Line Coverage Branch Coverage Complexity
ContentDecoratorChain
56%
18/32
44%
8/18
1.818
 
 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  
 package net.sourceforge.pebble.decorator;
 33  
 
 34  
 import net.sourceforge.pebble.domain.*;
 35  
 import net.sourceforge.pebble.api.decorator.ContentDecorator;
 36  
 import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
 37  
 
 38  
 import java.util.ArrayList;
 39  
 import java.util.List;
 40  
 
 41  
 /**
 42  
  * Manages a list of content decorators at runtime.
 43  
  *
 44  
  * @author    Simon Brown
 45  
  */
 46  
 public class ContentDecoratorChain implements ContentDecorator {
 47  
 
 48  
   /** the blog associated with this chain */
 49  
   private Blog blog;
 50  
 
 51  
   /** the list of decorators */
 52  2720
   private List<ContentDecorator> decorators = new ArrayList<ContentDecorator>();
 53  
 
 54  
   /**
 55  
    * Creates a new chain.
 56  
    *
 57  
    * @param blog The blog to decorate for
 58  
    */
 59  2720
   public ContentDecoratorChain(Blog blog) {
 60  2720
     setBlog(blog);
 61  2720
   }
 62  
 
 63  
   /**
 64  
    * Adds a new decorator.
 65  
    *
 66  
    * @param decorator   a ContentDecorator instance
 67  
    */
 68  
   public void add(ContentDecorator decorator) {
 69  19040
     decorators.add(decorator);
 70  19040
   }
 71  
 
 72  
   /**
 73  
    * Gets the list of decorators in use.
 74  
    *
 75  
    * @return The content decorators
 76  
    */
 77  
   public List<ContentDecorator> getContentDecorators() {
 78  0
     return new ArrayList<ContentDecorator>(decorators);
 79  
   }
 80  
 
 81  
   /**
 82  
    * Decorates the specified blog entry.
 83  
    *
 84  
    * @param context   the context in which the decoration is running
 85  
    * @param blogEntry the blog entry to be decorated
 86  
    */
 87  
   public void decorate(ContentDecoratorContext context, BlogEntry blogEntry) {
 88  28
     for (ContentDecorator decorator : decorators) {
 89  196
       decorator.decorate(context, blogEntry);
 90  
     }
 91  
 
 92  
     // if the view is detail, decorate the comments and TrackBacks too
 93  28
     if (context.getView() == ContentDecoratorContext.DETAIL_VIEW) {
 94  28
       for (Comment comment : blogEntry.getComments()) {
 95  8
         decorate(context, comment);
 96  
       }
 97  
 
 98  28
       for (TrackBack trackBack : blogEntry.getTrackBacks()) {
 99  0
         decorate(context, trackBack);
 100  
       }
 101  
     }
 102  28
   }
 103  
 
 104  
   /**
 105  
    * Decorates the specified comment.
 106  
    *
 107  
    * @param context the context in which the decoration is running
 108  
    * @param comment the comment to be decorated
 109  
    */
 110  
   public void decorate(ContentDecoratorContext context, Comment comment) {
 111  32
     for (ContentDecorator decorator : decorators) {
 112  224
       decorator.decorate(context, comment);
 113  
     }
 114  32
   }
 115  
 
 116  
   /**
 117  
    * Decorates the specified TrackBack.
 118  
    *
 119  
    * @param context   the context in which the decoration is running
 120  
    * @param trackBack the TrackBack to be decorated
 121  
    */
 122  
   public void decorate(ContentDecoratorContext context, TrackBack trackBack) {
 123  0
     for (ContentDecorator decorator : decorators) {
 124  0
       decorator.decorate(context, trackBack);
 125  
     }
 126  0
   }
 127  
 
 128  
   /**
 129  
    * Decorates the specified static page.
 130  
    *
 131  
    * @param context    the context in which the decoration is running
 132  
    * @param staticPage the static page to be decorated
 133  
    */
 134  
   public void decorate(ContentDecoratorContext context, StaticPage staticPage) {
 135  0
     for (ContentDecorator decorator : decorators) {
 136  0
       decorator.decorate(context, staticPage);
 137  
     }
 138  0
   }
 139  
 
 140  
   /**
 141  
    * Gets the blog to which this decorator is associated.
 142  
    *
 143  
    * @return a Blog instance
 144  
    */
 145  
   public Blog getBlog() {
 146  0
     return this.blog;
 147  
   }
 148  
 
 149  
   /**
 150  
    * Sets the blog to which this decorator is associated.
 151  
    *
 152  
    * @param blog a Blog instance
 153  
    */
 154  
   public void setBlog(Blog blog) {
 155  2720
     this.blog = blog;
 156  2720
   }
 157  
 
 158  
   /**
 159  
    * Gets the list of content decorators.
 160  
    *
 161  
    * @return  a List of ContentDecorator instances
 162  
    */
 163  
   public List<ContentDecorator> getDecorators() {
 164  0
     return new ArrayList<ContentDecorator>(decorators);
 165  
   }
 166  
 
 167  
   /**
 168  
    * Decorates the specified blog entries.
 169  
    *
 170  
    * @param context       the context
 171  
    * @param blogEntries   a List of BlogEntry instances
 172  
    */
 173  
   public static void decorate(ContentDecoratorContext context, List<BlogEntry> blogEntries) {
 174  0
     if (blogEntries != null) {
 175  0
       for (BlogEntry blogEntry : blogEntries) {
 176  0
         blogEntry.getBlog().getContentDecoratorChain().decorate(context, blogEntry);
 177  
       }
 178  
     }
 179  0
   }
 180  
 
 181  
 }