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