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;
20
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.net.URL;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import javax.el.ELException;
29 import javax.el.ValueExpression;
30 import javax.el.VariableMapper;
31 import javax.faces.FacesException;
32 import javax.faces.component.UIComponent;
33 import javax.faces.view.facelets.FaceletContext;
34 import javax.faces.view.facelets.FaceletException;
35 import javax.faces.view.facelets.TagAttribute;
36 import javax.faces.view.facelets.TagConfig;
37 import javax.faces.view.facelets.TagException;
38 import javax.faces.view.facelets.TagHandler;
39
40 import org.apache.myfaces.view.facelets.AbstractFaceletContext;
41 import org.apache.myfaces.view.facelets.TemplateClient;
42 import org.apache.myfaces.view.facelets.TemplateContext;
43 import org.apache.myfaces.view.facelets.el.VariableMapperWrapper;
44 import org.apache.myfaces.view.facelets.impl.TemplateContextImpl;
45 import org.apache.myfaces.view.facelets.tag.ui.DefineHandler;
46
47
48
49
50
51
52
53
54 final class UserTagHandler extends TagHandler implements TemplateClient, ComponentContainerHandler
55 {
56
57 protected final TagAttribute[] _vars;
58
59 protected final URL _location;
60
61 protected final Map<String, DefineHandler> _handlers;
62
63
64
65
66 public UserTagHandler(TagConfig config, URL location)
67 {
68 super(config);
69 this._vars = this.tag.getAttributes().getAll();
70 this._location = location;
71
72 Collection<DefineHandler> defines = TagHandlerUtils.findNextByType(nextHandler, DefineHandler.class);
73 if (defines.isEmpty())
74 {
75 _handlers = null;
76 }
77 else
78 {
79 _handlers = new HashMap<String, DefineHandler>();
80 for (DefineHandler handler : defines)
81 {
82 _handlers.put(handler.getName(), handler);
83 }
84 }
85 }
86
87
88
89
90
91
92
93
94
95 public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
96 ELException
97 {
98
99
100
101
102
103
104
105
106
107
108
109
110
111 AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
112
113 try
114 {
115 String[] names = null;
116 ValueExpression[] values = null;
117 if (this._vars.length > 0)
118 {
119 names = new String[_vars.length];
120 values = new ValueExpression[_vars.length];
121 for (int i = 0; i < _vars.length; i++)
122 {
123 names[i] = _vars[i].getLocalName();
124 values[i] = _vars[i].getValueExpression(ctx, Object.class);
125 }
126 }
127 actx.pushTemplateContext(new TemplateContextImpl());
128 if (this._vars.length > 0)
129 {
130 for (int i = 0; i < this._vars.length; i++)
131 {
132 ((AbstractFaceletContext) ctx).getTemplateContext().setParameter(names[i], values[i]);
133 }
134 }
135
136 actx.getTemplateContext().setAllowCacheELExpressions(false);
137 actx.pushClient(this);
138 ctx.includeFacelet(parent, this._location);
139 }
140 catch (FileNotFoundException e)
141 {
142 throw new TagException(this.tag, e.getMessage());
143 }
144 finally
145 {
146
147
148 actx.popClient(this);
149 actx.popTemplateContext();
150
151 }
152 }
153
154 public boolean apply(FaceletContext ctx, UIComponent parent, String name) throws IOException, FacesException,
155 FaceletException, ELException
156 {
157 if (name != null)
158 {
159 if (this._handlers == null)
160 {
161 return false;
162 }
163 DefineHandler handler = (DefineHandler) this._handlers.get(name);
164 if (handler != null)
165 {
166 AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
167 TemplateContext itc = actx.popTemplateContext();
168 try
169 {
170 handler.applyDefinition(ctx, parent);
171 }
172 finally
173 {
174 actx.pushTemplateContext(itc);
175 }
176 return true;
177 }
178 else
179 {
180 return false;
181 }
182 }
183 else
184 {
185 AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
186 TemplateContext itc = actx.popTemplateContext();
187 try
188 {
189 this.nextHandler.apply(ctx, parent);
190 }
191 finally
192 {
193 actx.pushTemplateContext(itc);
194 }
195 return true;
196 }
197 }
198
199 }