KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > tag > UserTagHandler


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14
15 package com.sun.facelets.tag;
16
17 import java.io.FileNotFoundException JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.net.URL JavaDoc;
20
21 import javax.el.ELException;
22 import javax.el.VariableMapper;
23 import javax.faces.FacesException;
24 import javax.faces.component.UIComponent;
25
26 import com.sun.facelets.FaceletContext;
27 import com.sun.facelets.FaceletException;
28 import com.sun.facelets.FaceletHandler;
29 import com.sun.facelets.TemplateClient;
30 import com.sun.facelets.el.VariableMapperWrapper;
31
32 /**
33  * A Tag that is specified in a FaceletFile. Takes all attributes specified and
34  * sets them on the FaceletContext before including the targeted Facelet file.
35  *
36  * @author Jacob Hookom
37  * @version $Id: UserTagHandler.java,v 1.7 2006/03/29 04:10:13 jhook Exp $
38  */

39 final class UserTagHandler extends TagHandler implements TemplateClient {
40
41     protected final TagAttribute[] vars;
42
43     protected final URL JavaDoc location;
44
45     /**
46      * @param config
47      */

48     public UserTagHandler(TagConfig config, URL JavaDoc location) {
49         super(config);
50         this.vars = this.tag.getAttributes().getAll();
51         this.location = location;
52     }
53
54     /**
55      * Iterate over all TagAttributes and set them on the FaceletContext's
56      * VariableMapper, then include the target Facelet. Finally, replace the old
57      * VariableMapper.
58      *
59      * @see TagAttribute#getValueExpression(FaceletContext, Class)
60      * @see VariableMapper
61      * @see com.sun.facelets.FaceletHandler#apply(com.sun.facelets.FaceletContext,
62      * javax.faces.component.UIComponent)
63      */

64     public void apply(FaceletContext ctx, UIComponent parent)
65             throws IOException JavaDoc, FacesException, FaceletException, ELException {
66         VariableMapper orig = ctx.getVariableMapper();
67         
68         // setup a variable map
69
if (this.vars.length > 0) {
70             VariableMapper varMapper = new VariableMapperWrapper(orig);
71             for (int i = 0; i < this.vars.length; i++) {
72                 varMapper.setVariable(this.vars[i].getLocalName(), this.vars[i]
73                         .getValueExpression(ctx, Object JavaDoc.class));
74             }
75             ctx.setVariableMapper(varMapper);
76         }
77         
78         // eval include
79
try {
80             ctx.pushClient(this);
81             ctx.includeFacelet(parent, this.location);
82         } catch (FileNotFoundException JavaDoc e) {
83             throw new TagException(this.tag, e.getMessage());
84         } finally {
85             
86             // make sure we undo our changes
87
ctx.popClient(this);
88             ctx.setVariableMapper(orig);
89         }
90     }
91
92     public boolean apply(FaceletContext ctx, UIComponent parent, String JavaDoc name) throws IOException JavaDoc, FacesException, FaceletException, ELException {
93         if (name == null) {
94             this.nextHandler.apply(ctx, parent);
95             return true;
96         }
97         return false;
98     }
99
100 }
101
Popular Tags