1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.view.facelets.tag.ui;
20
21 import java.io.IOException;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27
28 import javax.el.ELException;
29 import javax.el.VariableMapper;
30 import javax.faces.FacesException;
31 import javax.faces.component.UIComponent;
32 import javax.faces.view.facelets.FaceletContext;
33 import javax.faces.view.facelets.FaceletException;
34 import javax.faces.view.facelets.TagAttribute;
35 import javax.faces.view.facelets.TagConfig;
36 import javax.faces.view.facelets.TagHandler;
37
38 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute;
39 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
40 import org.apache.myfaces.view.facelets.AbstractFaceletContext;
41 import org.apache.myfaces.view.facelets.TemplateClient;
42 import org.apache.myfaces.view.facelets.el.VariableMapperWrapper;
43 import org.apache.myfaces.view.facelets.tag.TagHandlerUtils;
44
45
46
47
48
49
50
51 @JSFFaceletTag(name="ui:composition")
52 public final class CompositionHandler extends TagHandler implements TemplateClient
53 {
54
55
56 private static final Logger log = Logger.getLogger(CompositionHandler.class.getName());
57
58 public final static String Name = "composition";
59
60
61
62
63
64 @JSFFaceletAttribute(
65 name="template",
66 className="javax.el.ValueExpression",
67 deferredValueType="java.lang.String")
68 protected final TagAttribute _template;
69
70 protected final Map<String, DefineHandler> _handlers;
71
72 protected final ParamHandler[] _params;
73
74
75
76
77 public CompositionHandler(TagConfig config)
78 {
79 super(config);
80 _template = getAttribute("template");
81 if (_template != null)
82 {
83 _handlers = new HashMap<String, DefineHandler>();
84 for (DefineHandler handler : TagHandlerUtils.findNextByType(nextHandler, DefineHandler.class))
85 {
86 _handlers.put(handler.getName(), handler);
87 if (log.isLoggable(Level.FINE))
88 {
89 log.fine(tag + " found Define[" + handler.getName() + "]");
90 }
91 }
92
93 Collection<ParamHandler> params = TagHandlerUtils.findNextByType(nextHandler, ParamHandler.class);
94 if (!params.isEmpty())
95 {
96 int i = 0;
97 _params = new ParamHandler[params.size()];
98 for (ParamHandler handler : params)
99 {
100 _params[i++] = handler;
101 }
102 }
103 else
104 {
105 _params = null;
106 }
107 }
108 else
109 {
110 _params = null;
111 _handlers = null;
112 }
113 }
114
115
116
117
118
119
120
121 public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
122 ELException
123 {
124 if (_template != null)
125 {
126
127
128
129
130
131
132
133
134
135
136 AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
137 actx.extendClient(this);
138 if (_params != null)
139 {
140
141
142 for (int i = 0; i < _params.length; i++)
143 {
144 _params[i].apply(ctx, parent);
145 }
146 }
147
148 try
149 {
150 ctx.includeFacelet(parent, _template.getValue(ctx));
151 }
152 finally
153 {
154 actx.popExtendedClient(this);
155
156 }
157 }
158 else
159 {
160 this.nextHandler.apply(ctx, parent);
161 }
162 }
163
164 public boolean apply(FaceletContext ctx, UIComponent parent, String name) throws IOException, FacesException,
165 FaceletException, ELException
166 {
167 if (name != null)
168 {
169 if (_handlers == null)
170 {
171 return false;
172 }
173
174 DefineHandler handler = _handlers.get(name);
175 if (handler != null)
176 {
177 handler.applyDefinition(ctx, parent);
178 return true;
179 }
180 else
181 {
182 return false;
183 }
184 }
185 else
186 {
187 this.nextHandler.apply(ctx, parent);
188 return true;
189 }
190 }
191
192 }