This project has retired. For details please refer to its Attic page.
ChainedDelegateAttributeRenderer xref
View Javadoc

1   /*
2    * $Id: ChainedDelegateAttributeRenderer.java 821299 2009-10-03 12:15:05Z apetrelli $
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.renderer.impl;
22  
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.tiles.Attribute;
28  import org.apache.tiles.context.TilesRequestContext;
29  import org.apache.tiles.renderer.RendererException;
30  import org.apache.tiles.renderer.TypeDetectingAttributeRenderer;
31  
32  /***
33   * Renders an attribute that has no associated renderer using delegation to
34   * other renderers.
35   *
36   * @version $Rev: 821299 $ $Date: 2009-10-03 14:15:05 +0200 (sab, 03 ott 2009) $
37   * @since 2.2.1
38   */
39  public class ChainedDelegateAttributeRenderer extends AbstractBaseAttributeRenderer {
40  
41      /***
42       * The list of chained renderers.
43       */
44      private List<TypeDetectingAttributeRenderer> renderers;
45  
46      /***
47       * Constructor.
48       *
49       * @since 2.2.1
50       */
51      public ChainedDelegateAttributeRenderer() {
52          renderers = new ArrayList<TypeDetectingAttributeRenderer>();
53      }
54  
55      /***
56       * Adds an attribute renderer to the list. The first inserted this way, the
57       * first is checked when rendering.
58       *
59       * @param renderer The renderer to add.
60       */
61      public void addAttributeRenderer(TypeDetectingAttributeRenderer renderer) {
62          renderers.add(renderer);
63      }
64  
65      /*** {@inheritDoc} */
66      @Override
67      public void write(Object value, Attribute attribute,
68              TilesRequestContext request)
69              throws IOException {
70          if (value == null) {
71              throw new NullPointerException("The attribute value is null");
72          }
73  
74          for (TypeDetectingAttributeRenderer renderer : renderers) {
75              if (renderer.isRenderable(value, attribute, request)) {
76                  renderer.render(attribute, request);
77                  return;
78              }
79          }
80  
81          throw new RendererException("Type of the attribute not found, class '"
82                  + value.getClass() + "' value '" + value.toString() + "'");
83      }
84  }