001/*
002 * (C) Copyright 2014 Nuxeo SA (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-2.1.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 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.ui.web.binding.alias;
018
019import java.io.IOException;
020
021import javax.el.ELException;
022import javax.el.ExpressionFactory;
023import javax.el.ValueExpression;
024import javax.el.VariableMapper;
025import javax.faces.FacesException;
026import javax.faces.component.UIComponent;
027import javax.faces.view.facelets.ComponentConfig;
028import javax.faces.view.facelets.FaceletContext;
029
030import org.nuxeo.ecm.platform.ui.web.tag.handler.GenericHtmlComponentHandler;
031
032import com.sun.faces.facelets.el.VariableMapperWrapper;
033
034/**
035 * Generic HTML component handler ensuring that sub handlers extending {@link AliasTagHandler} will anchor their
036 * components in the tree.
037 * <p>
038 * This is helpful when exposing variables depending on a variable that is only available at render time (like a list or
039 * dataTable component), to ensure accurate resolution of variables at render time.
040 *
041 * @since 6.0
042 * @see AliasTagHandler
043 */
044public class AliasAnchorComponentHandler extends GenericHtmlComponentHandler {
045
046    public AliasAnchorComponentHandler(ComponentConfig config) {
047        super(config);
048    }
049
050    @Override
051    public void applyNextHandler(FaceletContext ctx, UIComponent c) throws IOException, FacesException, ELException {
052        VariableMapper orig = ctx.getVariableMapper();
053        VariableMapper vm = new VariableMapperWrapper(orig);
054        ExpressionFactory eFactory = ctx.getExpressionFactory();
055        ValueExpression ve = eFactory.createValueExpression(Boolean.TRUE, Boolean.class);
056        vm.setVariable(AliasTagHandler.ANCHOR_ENABLED_VARIABLE, ve);
057        ctx.setVariableMapper(vm);
058        try {
059            super.applyNextHandler(ctx, c);
060        } finally {
061            ctx.setVariableMapper(orig);
062        }
063    }
064
065}