001/*
002 * (C) Copyright 2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id: MethodResultTagHandler.java 19474 2007-05-27 10:18:21Z sfermigier $
018 */
019
020package org.nuxeo.ecm.platform.ui.web.tag.handler;
021
022import java.io.IOException;
023
024import javax.el.MethodExpression;
025import javax.el.ValueExpression;
026import javax.el.VariableMapper;
027import javax.faces.component.UIComponent;
028import javax.faces.view.facelets.FaceletContext;
029import javax.faces.view.facelets.MetaRuleset;
030import javax.faces.view.facelets.MetaTagHandler;
031import javax.faces.view.facelets.TagAttribute;
032import javax.faces.view.facelets.TagConfig;
033
034import org.nuxeo.ecm.platform.ui.web.binding.MethodValueExpression;
035
036import com.sun.faces.facelets.el.VariableMapperWrapper;
037
038/**
039 * Tag handler that exposes the result of a method binding in the variable map.
040 *
041 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
042 * @deprecated since Seam 2.0 handles method results
043 * @see SetTagHandler for caching features using parameter immediate
044 */
045@Deprecated
046public class MethodResultTagHandler extends MetaTagHandler {
047
048    private static final Class[] DEFAULT_PARAM_TYPES_CLASSES = new Class[0];
049
050    private final TagAttribute name;
051
052    private final TagAttribute value;
053
054    private final TagAttribute immediate;
055
056    private final TagAttribute paramTypes;
057
058    public MethodResultTagHandler(TagConfig config) {
059        super(config);
060        name = getRequiredAttribute("name");
061        value = getRequiredAttribute("value");
062        immediate = getAttribute("immediate");
063        paramTypes = getAttribute("paramTypes");
064    }
065
066    private Class[] resolveParamTypes(FaceletContext ctx) {
067        // TODO: implement string parsing ?
068        return DEFAULT_PARAM_TYPES_CLASSES;
069    }
070
071    @Override
072    public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
073        String nameStr = name.getValue(ctx);
074        // resolve given value as a method binding, paramtypes ignored for now
075        Class[] paramTypesClasses = resolveParamTypes(ctx);
076        MethodExpression meth = value.getMethodExpression(ctx, Object.class, paramTypesClasses);
077        Boolean invokeNow = false;
078        if (immediate != null) {
079            invokeNow = immediate.getBoolean(ctx);
080        }
081        ValueExpression ve;
082        if (invokeNow) {
083            Object res = meth.invoke(ctx, paramTypesClasses);
084            ve = ctx.getExpressionFactory().createValueExpression(res, Object.class);
085        } else {
086            ve = new MethodValueExpression(ctx.getFunctionMapper(), ctx.getVariableMapper(), meth, paramTypesClasses);
087        }
088        VariableMapper orig = ctx.getVariableMapper();
089        VariableMapper vm = new VariableMapperWrapper(orig);
090        ctx.setVariableMapper(vm);
091        vm.setVariable(nameStr, ve);
092        try {
093            nextHandler.apply(ctx, parent);
094        } finally {
095            ctx.setVariableMapper(orig);
096        }
097    }
098
099    @Override
100    @SuppressWarnings("rawtypes")
101    protected MetaRuleset createMetaRuleset(Class type) {
102        return null;
103    }
104
105}