View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.renderkit.template;
20  
21  import java.io.IOException;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.context.FacesContext;
25  import javax.faces.render.Renderer;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  
31  public abstract class TemplateRenderer extends Renderer
32  {
33      private static final Log log = LogFactory.getLog(TemplateRenderer.class);
34  
35      private static final String TEMPLATE_ENCODER = "org.apache.myfaces.tomahawk.TemplateEncoder";
36      private static final String TEMPLATE_ENCODER_ENCODER_CLASS = "org.apache.myfaces.tomahawk.templateEncoder.ENCODER_CLASS";
37  
38      /**
39       * @see javax.faces.render.Renderer#encodeBegin(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
40       */
41      public void encodeBegin(FacesContext context, UIComponent component) throws IOException
42      {
43          if (!component.isRendered()) return;
44          encodeTemplate(context, component, getTemplateName(context, component) + "_begin.ftl");
45      }
46  
47      /**
48       * @see javax.faces.render.Renderer#encodeChildren(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
49       */
50      public void encodeChildren(FacesContext context, UIComponent component) throws IOException
51      {
52          if (!component.isRendered()) return;
53          if (!getRendersChildren()) return;
54          encodeTemplate(context, component, getTemplateName(context, component) + "_children.ftl");
55      }
56  
57      /**
58       * @see javax.faces.render.Renderer#encodeEnd(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
59       */
60      public void encodeEnd(FacesContext context, UIComponent component) throws IOException
61      {
62          if (!component.isRendered()) return;
63          encodeTemplate(context, component, getTemplateName(context, component) + "_end.ftl");
64      }
65  
66      protected void encodeTemplate(FacesContext context, UIComponent component, String template) throws IOException {
67          TemplateEncoder templateEncoder =
68                  (TemplateEncoder) context.getExternalContext().getApplicationMap().get(TEMPLATE_ENCODER);
69  
70          if(templateEncoder == null) {
71  
72              String className =
73                      context.getExternalContext().getInitParameter(TEMPLATE_ENCODER_ENCODER_CLASS);
74  
75              if(className == null) {
76                  className = "org.apache.myfaces.renderkit.template.DefaultTemplateEncoder";
77              }
78  
79              try {
80                  templateEncoder = (TemplateEncoder) Class.forName(className).newInstance();
81                  context.getExternalContext().getApplicationMap().put(TEMPLATE_ENCODER, templateEncoder);
82              } catch (ClassNotFoundException e) {
83                  log.error("Template encoder class : "+className+" not found. Alternative classed defined in web-xml parameter : "+TEMPLATE_ENCODER_ENCODER_CLASS);
84              } catch (IllegalAccessException e) {
85                  log.error("Constructor of template encoder class : " + className + " could not be accessed. Alternative classes may be defined in web-xml parameter : "+TEMPLATE_ENCODER_ENCODER_CLASS);
86              } catch (InstantiationException e) {
87                  log.error("Instance of template encoder class : " + className + " could not be instantiated. Alternative classes may be defined in web-xml parameter : "+TEMPLATE_ENCODER_ENCODER_CLASS);
88              }
89          }
90  
91          templateEncoder.encodeTemplate(context, component, this, template, getDatamodel(context, component));
92      }
93      
94      protected abstract Object getDatamodel(FacesContext context, UIComponent component);
95      protected abstract String getTemplateName(FacesContext context, UIComponent component);
96      
97      
98  }