1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
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 }