Coverage Report - org.apache.tiles.request.render.ChainedDelegateRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
ChainedDelegateRenderer
72%
13/18
60%
6/10
3.25
 
 1  
 /*
 2  
  * $Id: ChainedDelegateRenderer.java 1306435 2012-03-28 15:39:11Z nlebas $
 3  
  *
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  * http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 package org.apache.tiles.request.render;
 22  
 
 23  
 import java.io.IOException;
 24  
 import java.util.ArrayList;
 25  
 import java.util.List;
 26  
 
 27  
 import org.apache.tiles.request.Request;
 28  
 
 29  
 /**
 30  
  * Renders an attribute that has no associated renderer using delegation to
 31  
  * other renderers.
 32  
  *
 33  
  * @version $Rev: 1306435 $ $Date: 2012-03-29 02:39:11 +1100 (Thu, 29 Mar 2012) $
 34  
  */
 35  
 public class ChainedDelegateRenderer implements Renderer {
 36  
 
 37  
     /**
 38  
      * The list of chained renderers.
 39  
      */
 40  
     private List<Renderer> renderers;
 41  
 
 42  
     /**
 43  
      * Constructor.
 44  
      */
 45  5
     public ChainedDelegateRenderer() {
 46  5
         renderers = new ArrayList<Renderer>();
 47  5
     }
 48  
 
 49  
     /**
 50  
      * Adds an attribute renderer to the list. The first inserted this way, the
 51  
      * first is checked when rendering.
 52  
      *
 53  
      * @param renderer The renderer to add.
 54  
      */
 55  
     public void addAttributeRenderer(Renderer renderer) {
 56  15
         renderers.add(renderer);
 57  15
     }
 58  
 
 59  
 
 60  
     @Override
 61  
     public void render(String value, Request request) throws IOException {
 62  5
         if (value == null) {
 63  1
             throw new NullPointerException("The attribute value is null");
 64  
         }
 65  
 
 66  4
         for (Renderer renderer : renderers) {
 67  9
             if (renderer.isRenderable(value, request)) {
 68  3
                 renderer.render(value, request);
 69  3
                 return;
 70  
             }
 71  6
         }
 72  
 
 73  1
         throw new CannotRenderException("Cannot renderer value '" + value + "'");
 74  
     }
 75  
 
 76  
     /** {@inheritDoc} */
 77  
     public boolean isRenderable(String value, Request request) {
 78  0
         for (Renderer renderer : renderers) {
 79  0
             if (renderer.isRenderable(value, request)) {
 80  0
                 return true;
 81  
             }
 82  0
         }
 83  0
         return false;
 84  
     }
 85  
 }