001/*
002 * (C) Copyright 2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id: MethodResultTagHandler.java 19474 2007-05-27 10:18:21Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.ui.web.tag.handler;
023
024import java.io.IOException;
025
026import javax.el.MethodExpression;
027import javax.el.ValueExpression;
028import javax.el.VariableMapper;
029import javax.faces.component.UIComponent;
030import javax.faces.view.facelets.FaceletContext;
031import javax.faces.view.facelets.MetaRuleset;
032import javax.faces.view.facelets.MetaTagHandler;
033import javax.faces.view.facelets.TagAttribute;
034import javax.faces.view.facelets.TagConfig;
035
036import org.nuxeo.ecm.platform.ui.web.binding.MethodValueExpression;
037
038import com.sun.faces.facelets.el.VariableMapperWrapper;
039
040/**
041 * Tag handler that exposes the result of a method binding in the variable map.
042 *
043 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
044 * @deprecated since Seam 2.0 handles method results
045 * @see SetTagHandler for caching features using parameter immediate
046 */
047@Deprecated
048public class MethodResultTagHandler extends MetaTagHandler {
049
050    private static final Class[] DEFAULT_PARAM_TYPES_CLASSES = new Class[0];
051
052    private final TagAttribute name;
053
054    private final TagAttribute value;
055
056    private final TagAttribute immediate;
057
058    private final TagAttribute paramTypes;
059
060    public MethodResultTagHandler(TagConfig config) {
061        super(config);
062        name = getRequiredAttribute("name");
063        value = getRequiredAttribute("value");
064        immediate = getAttribute("immediate");
065        paramTypes = getAttribute("paramTypes");
066    }
067
068    private Class[] resolveParamTypes(FaceletContext ctx) {
069        // TODO: implement string parsing ?
070        return DEFAULT_PARAM_TYPES_CLASSES;
071    }
072
073    @Override
074    public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
075        String nameStr = name.getValue(ctx);
076        // resolve given value as a method binding, paramtypes ignored for now
077        Class[] paramTypesClasses = resolveParamTypes(ctx);
078        MethodExpression meth = value.getMethodExpression(ctx, Object.class, paramTypesClasses);
079        Boolean invokeNow = false;
080        if (immediate != null) {
081            invokeNow = immediate.getBoolean(ctx);
082        }
083        ValueExpression ve;
084        if (invokeNow) {
085            Object res = meth.invoke(ctx, paramTypesClasses);
086            ve = ctx.getExpressionFactory().createValueExpression(res, Object.class);
087        } else {
088            ve = new MethodValueExpression(ctx.getFunctionMapper(), ctx.getVariableMapper(), meth, paramTypesClasses);
089        }
090        VariableMapper orig = ctx.getVariableMapper();
091        VariableMapper vm = new VariableMapperWrapper(orig);
092        ctx.setVariableMapper(vm);
093        vm.setVariable(nameStr, ve);
094        try {
095            nextHandler.apply(ctx, parent);
096        } finally {
097            ctx.setVariableMapper(orig);
098        }
099    }
100
101    @Override
102    @SuppressWarnings("rawtypes")
103    protected MetaRuleset createMetaRuleset(Class type) {
104        return null;
105    }
106
107}