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

1   /*
2    * $Id: BasicRendererFactory.java 820373 2009-09-30 18:26:58Z 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.util.HashMap;
24  import java.util.Map;
25  
26  import org.apache.tiles.TilesApplicationContext;
27  import org.apache.tiles.TilesContainer;
28  import org.apache.tiles.awareness.TilesApplicationContextAware;
29  import org.apache.tiles.awareness.TilesContainerAware;
30  import org.apache.tiles.awareness.TilesRequestContextFactoryAware;
31  import org.apache.tiles.context.TilesRequestContextFactory;
32  import org.apache.tiles.evaluator.AttributeEvaluatorFactory;
33  import org.apache.tiles.evaluator.AttributeEvaluatorFactoryAware;
34  import org.apache.tiles.reflect.ClassUtil;
35  import org.apache.tiles.renderer.AttributeRenderer;
36  import org.apache.tiles.renderer.RendererException;
37  import org.apache.tiles.renderer.RendererFactory;
38  
39  /***
40   * Basic renderer factory implementation.
41   *
42   * @version $Rev: 820373 $ $Date: 2009-09-30 20:26:58 +0200 (mer, 30 set 2009) $
43   * @since 2.1.0
44   */
45  public class BasicRendererFactory implements RendererFactory,
46          TilesContainerAware, TilesRequestContextFactoryAware,
47          TilesApplicationContextAware, AttributeEvaluatorFactoryAware {
48  
49      /***
50       * The type renderers init parameter name.
51       *
52       * @since 2.1.0
53       */
54      public static final String TYPE_RENDERERS_INIT_PARAM =
55          "org.apache.tiles.renderer.impl.BasicRendererFactory.TYPE_RENDERERS";
56  
57      /***
58       * The default renderer init parameter.
59       *
60       * @since 2.1.0
61       */
62      public static final String DEFAULT_RENDERER_INIT_PARAM =
63          "org.apache.tiles.rendere.impl.BasicRendererFactory.DEFAULT_RENDERER";
64  
65      /***
66       * The default renderer class name.
67       *
68       * @since 2.1.0
69       * @deprecated Do not use, the default renderer class should be determined
70       * when building the container.
71       */
72      public static final String DEFAULT_RENDERER_CLASS_NAME =
73          UntypedAttributeRenderer.class.getName();
74  
75      /***
76       * The default renderer name/renderer class map.
77       *
78       * @since 2.1.0
79       */
80      protected static final Map<String, String> DEFAULT_TYPE_2_RENDERER;
81  
82      /***
83       * The Tiles context factory.
84       *
85       * @since 2.1.1
86       */
87      protected TilesRequestContextFactory contextFactory;
88  
89      /***
90       * The Tiles application context.
91       *
92       * @since 2.1.0
93       */
94      protected TilesApplicationContext applicationContext;
95  
96      /***
97       * The Tiles container.
98       *
99       * @since 2.1.0
100      */
101     protected TilesContainer container;
102 
103     /***
104      * The attribute evaluator.
105      *
106      * @since 2.2.0
107      */
108     protected AttributeEvaluatorFactory attributeEvaluatorFactory;
109 
110     /***
111      * The renderer name/renderer map.
112      *
113      * @since 2.1.0
114      */
115     protected Map<String, AttributeRenderer> renderers;
116 
117     /***
118      * The default renderer.
119      *
120      * @since 2.1.0
121      */
122     protected AttributeRenderer defaultRenderer;
123 
124     static {
125         DEFAULT_TYPE_2_RENDERER = new HashMap<String, String>();
126         DEFAULT_TYPE_2_RENDERER.put("string", StringAttributeRenderer.class
127                 .getName());
128         DEFAULT_TYPE_2_RENDERER.put("definition",
129                 DefinitionAttributeRenderer.class.getName());
130         DEFAULT_TYPE_2_RENDERER.put("template", TemplateAttributeRenderer.class
131                 .getName());
132     }
133 
134     /***
135      * Constructor.
136      *
137      * @since 2.1.0
138      */
139     public BasicRendererFactory() {
140         renderers = new HashMap<String, AttributeRenderer>();
141     }
142 
143     /*** {@inheritDoc} */
144     public void init(Map<String, String> parameters) {
145         String defaultRendererParam = parameters.get(DEFAULT_RENDERER_INIT_PARAM);
146         if (defaultRendererParam == null) {
147             defaultRendererParam = DEFAULT_RENDERER_CLASS_NAME;
148         }
149         defaultRenderer = (AttributeRenderer) ClassUtil
150                 .instantiate(defaultRendererParam);
151         initializeRenderer(defaultRenderer);
152         String typeRenderersParam = parameters.get(TYPE_RENDERERS_INIT_PARAM);
153         Map<String, String> completeParams = new HashMap<String, String>(
154                 DEFAULT_TYPE_2_RENDERER);
155         if (typeRenderersParam != null) {
156             String[] pairs = typeRenderersParam.split("//s*;//s*");
157             for (int i = 0; i < pairs.length; i++) {
158                 String[] pair = pairs[i].split("//s*,//s*");
159                 if (pair == null || pair.length != 2) {
160                     throw new RendererException("The string '" + pairs[i]
161                             + "' is not a valid type-renderer pair");
162                 }
163                 completeParams.put(pair[0], pair[1]);
164             }
165         }
166 
167         for (Map.Entry<String, String> entry : completeParams.entrySet()) {
168             AttributeRenderer renderer = (AttributeRenderer) ClassUtil
169                     .instantiate(entry.getValue());
170             initializeRenderer(renderer);
171             renderers.put(entry.getKey(), renderer);
172         }
173     }
174 
175     /*** {@inheritDoc} */
176     public AttributeRenderer getRenderer(String name) {
177         AttributeRenderer retValue;
178         if (name != null) {
179             retValue = renderers.get(name);
180             if (retValue == null) {
181                 retValue = (AttributeRenderer) ClassUtil.instantiate(name);
182                 initializeRenderer(retValue);
183                 renderers.put(name, retValue);
184             }
185         } else {
186             retValue = defaultRenderer;
187         }
188 
189         return retValue;
190     }
191 
192     /***
193      * Sets the default renderer.
194      *
195      * @param renderer The default renderer.
196      * @since 2.1.0
197      */
198     public void setDefaultRenderer(AttributeRenderer renderer) {
199         this.defaultRenderer = renderer;
200     }
201 
202     /***
203      * Registers a renderer.
204      *
205      * @param name The name of the renderer.
206      * @param renderer The renderer to register.
207      * @since 2.1.0
208      */
209     public void registerRenderer(String name, AttributeRenderer renderer) {
210         renderers.put(name, renderer);
211     }
212 
213     /*** {@inheritDoc} */
214     public void setContainer(TilesContainer container) {
215         this.container = container;
216     }
217 
218     /*** {@inheritDoc} */
219     public void setAttributeEvaluatorFactory(AttributeEvaluatorFactory attributeEvaluatorFactory) {
220         this.attributeEvaluatorFactory = attributeEvaluatorFactory;
221     }
222 
223     /*** {@inheritDoc} */
224     public void setRequestContextFactory(
225             TilesRequestContextFactory contextFactory) {
226         this.contextFactory = contextFactory;
227     }
228 
229     /*** {@inheritDoc} */
230     public void setApplicationContext(TilesApplicationContext applicationContext) {
231         this.applicationContext = applicationContext;
232     }
233 
234     /***
235      * Initialize a renderer, by injecting dependencies.
236      *
237      * @param renderer The renderer to initialize.
238      * @since 2.1.0
239      */
240     protected void initializeRenderer(AttributeRenderer renderer) {
241         if (renderer instanceof TilesRequestContextFactoryAware) {
242             ((TilesRequestContextFactoryAware) renderer)
243                     .setRequestContextFactory(contextFactory);
244         }
245         if (renderer instanceof TilesApplicationContextAware) {
246             ((TilesApplicationContextAware) renderer)
247                     .setApplicationContext(applicationContext);
248         }
249         if (renderer instanceof TilesContainerAware) {
250             ((TilesContainerAware) renderer).setContainer(container);
251         }
252         if (renderer instanceof AttributeEvaluatorFactoryAware) {
253             ((AttributeEvaluatorFactoryAware) renderer)
254                     .setAttributeEvaluatorFactory(attributeEvaluatorFactory);
255         }
256     }
257 }