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: DecorateHandler.java,v 1.14 2007/05/06 17:35:04 jhook Exp $
041: */
042: public final class DecorateHandler extends TagHandler implements
043: TemplateClient {
044:
045: private final Logger log = Logger
046: .getLogger("facelets.tag.ui.decorate");
047:
048: private final TagAttribute template;
049:
050: private final Map handlers;
051:
052: private final ParamHandler[] params;
053:
054: /**
055: * @param config
056: */
057: public DecorateHandler(TagConfig config) {
058: super (config);
059: this .template = this .getRequiredAttribute("template");
060: this .handlers = new HashMap();
061:
062: Iterator itr = this .findNextByType(DefineHandler.class);
063: DefineHandler d = null;
064: while (itr.hasNext()) {
065: d = (DefineHandler) itr.next();
066: this .handlers.put(d.getName(), d);
067: if (log.isLoggable(Level.FINE)) {
068: log.fine(tag + " found Define[" + d.getName() + "]");
069: }
070: }
071: List paramC = new ArrayList();
072: itr = this .findNextByType(ParamHandler.class);
073: while (itr.hasNext()) {
074: paramC.add(itr.next());
075: }
076: if (paramC.size() > 0) {
077: this .params = new ParamHandler[paramC.size()];
078: for (int i = 0; i < this .params.length; i++) {
079: this .params[i] = (ParamHandler) paramC.get(i);
080: }
081: } else {
082: this .params = null;
083: }
084: }
085:
086: /*
087: * (non-Javadoc)
088: *
089: * @see com.sun.facelets.FaceletHandler#apply(com.sun.facelets.FaceletContext,
090: * javax.faces.component.UIComponent)
091: */
092: public void apply(FaceletContext ctx, UIComponent parent)
093: throws IOException, FacesException, FaceletException,
094: ELException {
095: VariableMapper orig = ctx.getVariableMapper();
096: if (this .params != null) {
097: VariableMapper vm = new VariableMapperWrapper(orig);
098: ctx.setVariableMapper(vm);
099: for (int i = 0; i < this .params.length; i++) {
100: this .params[i].apply(ctx, parent);
101: }
102: }
103:
104: ctx.pushClient(this );
105: try {
106: ctx.includeFacelet(parent, this .template.getValue(ctx));
107: } finally {
108: ctx.setVariableMapper(orig);
109: ctx.popClient(this );
110: }
111: }
112:
113: public boolean apply(FaceletContext ctx, UIComponent parent,
114: String name) throws IOException, FacesException,
115: FaceletException, ELException {
116: if (name != null) {
117: DefineHandler handler = (DefineHandler) this .handlers
118: .get(name);
119: if (handler != null) {
120: handler.applyDefinition(ctx, parent);
121: return true;
122: } else {
123: return false;
124: }
125: } else {
126: this .nextHandler.apply(ctx, parent);
127: return true;
128: }
129: }
130: }
|