1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.view.facelets.tag.ui;
21
22 import java.io.IOException;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
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 import org.apache.myfaces.view.facelets.AbstractFaceletContext;
38 import org.apache.myfaces.view.facelets.TemplateClient;
39 import org.apache.myfaces.view.facelets.el.VariableMapperWrapper;
40 import org.apache.myfaces.view.facelets.tag.TagHandlerUtils;
41
42
43
44
45
46
47
48
49
50
51
52 public final class LegacyCompositionHandler extends TagHandler implements TemplateClient
53 {
54
55 private static final Logger log = Logger.getLogger(CompositionHandler.class.getName());
56
57 public final static String NAME = "composition";
58
59
60
61
62
63
64
65
66
67 protected final TagAttribute _template;
68
69 protected final Map<String, DefineHandler> _handlers;
70
71 protected final LegacyParamHandler[] _params;
72
73
74
75
76 public LegacyCompositionHandler(TagConfig config)
77 {
78 super(config);
79 _template = getAttribute("template");
80 if (_template != null)
81 {
82 _handlers = new HashMap<String, DefineHandler>();
83 for (DefineHandler handler : TagHandlerUtils.findNextByType(nextHandler, DefineHandler.class))
84 {
85 _handlers.put(handler.getName(), handler);
86 if (log.isLoggable(Level.FINE))
87 {
88 log.fine(tag + " found Define[" + handler.getName() + "]");
89 }
90 }
91
92 Collection<LegacyParamHandler> params = TagHandlerUtils.findNextByType(nextHandler,
93 LegacyParamHandler.class);
94 if (!params.isEmpty())
95 {
96 int i = 0;
97 _params = new LegacyParamHandler[params.size()];
98 for (LegacyParamHandler 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 @Override
122 public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
123 ELException
124 {
125 if (_template != null)
126 {
127 VariableMapper orig = ctx.getVariableMapper();
128 AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
129 actx.extendClient(this);
130 if (_params != null)
131 {
132 VariableMapper vm = new VariableMapperWrapper(orig);
133 ctx.setVariableMapper(vm);
134 for (int i = 0; i < _params.length; i++)
135 {
136 _params[i].apply(ctx, parent);
137 }
138 }
139
140 try
141 {
142 ctx.includeFacelet(parent, _template.getValue(ctx));
143 }
144 finally
145 {
146 actx.popExtendedClient(this);
147 ctx.setVariableMapper(orig);
148 }
149 }
150 else
151 {
152 this.nextHandler.apply(ctx, parent);
153 }
154 }
155
156 @Override
157 public boolean apply(FaceletContext ctx, UIComponent parent, String name) throws IOException, FacesException,
158 FaceletException, ELException
159 {
160 if (name != null)
161 {
162 if (_handlers == null)
163 {
164 return false;
165 }
166
167 DefineHandler handler = _handlers.get(name);
168 if (handler != null)
169 {
170 handler.applyDefinition(ctx, parent);
171 return true;
172 }
173 else
174 {
175 return false;
176 }
177 }
178 else
179 {
180 this.nextHandler.apply(ctx, parent);
181 return true;
182 }
183 }
184
185 }