001: /**
002: * Licensed under the Common Development and Distribution License,
003: * you may not use this file except in compliance with the License.
004: * You may obtain a copy of the License at
005: *
006: * http://www.sun.com/cddl/
007: *
008: * Unless required by applicable law or agreed to in writing, software
009: * distributed under the License is distributed on an "AS IS" BASIS,
010: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * permissions and limitations under the License.
013: */package com.sun.facelets.tag.ui;
014:
015: import java.io.IOException;
016: import java.util.ArrayList;
017: import java.util.HashMap;
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.Map;
021: import java.util.logging.Level;
022: import java.util.logging.Logger;
023:
024: import javax.el.ELException;
025: import javax.el.VariableMapper;
026: import javax.faces.FacesException;
027: import javax.faces.component.UIComponent;
028:
029: import com.sun.facelets.FaceletContext;
030: import com.sun.facelets.FaceletException;
031: import com.sun.facelets.FaceletHandler;
032: import com.sun.facelets.TemplateClient;
033: import com.sun.facelets.el.VariableMapperWrapper;
034: import com.sun.facelets.tag.TagAttribute;
035: import com.sun.facelets.tag.TagConfig;
036: import com.sun.facelets.tag.TagHandler;
037:
038: /**
039: * @author Jacob Hookom
040: * @version $Id: CompositionHandler.java,v 1.12 2007/08/09 16:54:44 rlubke Exp $
041: */
042: public final class CompositionHandler extends TagHandler implements
043: TemplateClient {
044:
045: private final Logger log = Logger
046: .getLogger("facelets.tag.ui.composition");
047:
048: public final static String Name = "composition";
049:
050: protected final TagAttribute template;
051:
052: protected final Map handlers;
053:
054: protected final ParamHandler[] params;
055:
056: /**
057: * @param config
058: */
059: public CompositionHandler(TagConfig config) {
060: super (config);
061: this .template = this .getAttribute("template");
062: if (this .template != null) {
063: this .handlers = new HashMap();
064: Iterator itr = this .findNextByType(DefineHandler.class);
065: DefineHandler d = null;
066: while (itr.hasNext()) {
067: d = (DefineHandler) itr.next();
068: this .handlers.put(d.getName(), d);
069: if (log.isLoggable(Level.FINE)) {
070: log
071: .fine(tag + " found Define[" + d.getName()
072: + "]");
073: }
074: }
075: List paramC = new ArrayList();
076: itr = this .findNextByType(ParamHandler.class);
077: while (itr.hasNext()) {
078: paramC.add(itr.next());
079: }
080: if (paramC.size() > 0) {
081: this .params = new ParamHandler[paramC.size()];
082: for (int i = 0; i < this .params.length; i++) {
083: this .params[i] = (ParamHandler) paramC.get(i);
084: }
085: } else {
086: this .params = null;
087: }
088: } else {
089: this .params = null;
090: this .handlers = null;
091: }
092: }
093:
094: /*
095: * (non-Javadoc)
096: *
097: * @see com.sun.facelets.FaceletHandler#apply(com.sun.facelets.FaceletContext,
098: * javax.faces.component.UIComponent)
099: */
100: public void apply(FaceletContext ctx, UIComponent parent)
101: throws IOException, FacesException, FaceletException,
102: ELException {
103: if (this .template != null) {
104: VariableMapper orig = ctx.getVariableMapper();
105: if (this .params != null) {
106: VariableMapper vm = new VariableMapperWrapper(orig);
107: ctx.setVariableMapper(vm);
108: for (int i = 0; i < this .params.length; i++) {
109: this .params[i].apply(ctx, parent);
110: }
111: }
112:
113: ctx.extendClient(this );
114: try {
115: ctx.includeFacelet(parent, this .template.getValue(ctx));
116: } finally {
117: ctx.popClient(this );
118: ctx.setVariableMapper(orig);
119: }
120: } else {
121: this .nextHandler.apply(ctx, parent);
122: }
123: }
124:
125: public boolean apply(FaceletContext ctx, UIComponent parent,
126: String name) throws IOException, FacesException,
127: FaceletException, ELException {
128: if (name != null) {
129: if (this .handlers == null) {
130: return false;
131: }
132: DefineHandler handler = (DefineHandler) this .handlers
133: .get(name);
134: if (handler != null) {
135: handler.applyDefinition(ctx, parent);
136: return true;
137: } else {
138: return false;
139: }
140: } else {
141: this .nextHandler.apply(ctx, parent);
142: return true;
143: }
144: }
145:
146: }
|