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