Source code: com/sun/facelets/tag/ui/CompositionHandler.java
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.ui;
16
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25
26 import javax.el.ELException;
27 import javax.el.VariableMapper;
28 import javax.faces.FacesException;
29 import javax.faces.component.UIComponent;
30
31 import com.sun.facelets.FaceletContext;
32 import com.sun.facelets.FaceletException;
33 import com.sun.facelets.FaceletHandler;
34 import com.sun.facelets.TemplateClient;
35 import com.sun.facelets.el.VariableMapperWrapper;
36 import com.sun.facelets.tag.TagAttribute;
37 import com.sun.facelets.tag.TagConfig;
38 import com.sun.facelets.tag.TagHandler;
39
40 /**
41 * @author Jacob Hookom
42 * @version $Id: CompositionHandler.java,v 1.10 2006/03/29 04:10:08 jhook Exp $
43 */
44 public final class CompositionHandler extends TagHandler implements
45 TemplateClient {
46
47 private final Logger log = Logger.getLogger("facelets.tag.ui.composition");
48
49 public final static String Name = "composition";
50
51 protected final TagAttribute template;
52
53 protected final Map handlers;
54
55 protected final ParamHandler[] params;
56
57 /**
58 * @param config
59 */
60 public CompositionHandler(TagConfig config) {
61 super(config);
62 this.template = this.getAttribute("template");
63 if (this.template != null) {
64 this.handlers = new HashMap();
65 Iterator itr = this.findNextByType(DefineHandler.class);
66 DefineHandler d = null;
67 while (itr.hasNext()) {
68 d = (DefineHandler) itr.next();
69 this.handlers.put(d.getName(), d);
70 if (log.isLoggable(Level.FINE)) {
71 log.fine(tag + " found Define[" + d.getName() + "]");
72 }
73 }
74 List paramC = new ArrayList();
75 itr = this.findNextByType(ParamHandler.class);
76 while (itr.hasNext()) {
77 paramC.add(itr.next());
78 }
79 if (paramC.size() > 0) {
80 this.params = new ParamHandler[paramC.size()];
81 for (int i = 0; i < this.params.length; i++) {
82 this.params[i] = (ParamHandler) paramC.get(i);
83 }
84 } else {
85 this.params = null;
86 }
87 } else {
88 this.params = null;
89 this.handlers = null;
90 }
91 }
92
93 /*
94 * (non-Javadoc)
95 *
96 * @see com.sun.facelets.FaceletHandler#apply(com.sun.facelets.FaceletContext,
97 * javax.faces.component.UIComponent)
98 */
99 public void apply(FaceletContext ctx, UIComponent parent)
100 throws IOException, FacesException, FaceletException, ELException {
101 if (this.template != null) {
102 VariableMapper orig = ctx.getVariableMapper();
103 if (this.params != null) {
104 VariableMapper vm = new VariableMapperWrapper(orig);
105 ctx.setVariableMapper(vm);
106 for (int i = 0; i < this.params.length; i++) {
107 this.params[i].apply(ctx, parent);
108 }
109 }
110
111 ctx.extendClient(this);
112 try {
113 ctx.includeFacelet(parent, this.template.getValue(ctx));
114 } finally {
115 ctx.popClient(this);
116 ctx.setVariableMapper(orig);
117 }
118 } else {
119 this.nextHandler.apply(ctx, parent);
120 }
121 }
122
123 public boolean apply(FaceletContext ctx, UIComponent parent, String name)
124 throws IOException, FacesException, FaceletException, ELException {
125 if (name != null) {
126 FaceletHandler handler = (FaceletHandler) this.handlers.get(name);
127 if (handler != null) {
128 handler.apply(ctx, parent);
129 return true;
130 } else {
131 return false;
132 }
133 } else {
134 this.nextHandler.apply(ctx, parent);
135 return true;
136 }
137 }
138
139 }