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: MetaValueHolderRule.java 28460 2008-01-03 15:34:05Z sfermigier $
018 */
019
020package org.nuxeo.ecm.platform.ui.web.tag.jsf;
021
022import javax.el.ValueExpression;
023import javax.faces.component.UIComponent;
024import javax.faces.component.ValueHolder;
025import javax.faces.view.facelets.FaceletContext;
026import javax.faces.view.facelets.MetaRule;
027import javax.faces.view.facelets.Metadata;
028import javax.faces.view.facelets.MetadataTarget;
029import javax.faces.view.facelets.TagAttribute;
030
031import org.nuxeo.ecm.platform.ui.web.binding.MetaValueExpression;
032
033/**
034 * Meta value rule, used to evaluate an expression as a regular value expression, or invoking it again as another value
035 * expression or method expression.
036 * <p>
037 * The method can have parameters and the expression must use parentheses even if no parameters are needed.
038 *
039 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
040 */
041public class MetaValueHolderRule extends MetaRule {
042
043    static final class LiteralValueMetadata extends Metadata {
044
045        private final String value;
046
047        LiteralValueMetadata(String value) {
048            this.value = value;
049        }
050
051        @Override
052        public void applyMetadata(FaceletContext ctx, Object instance) {
053            ((ValueHolder) instance).setValue(value);
054        }
055    }
056
057    static final class MetaValueExpressionMetadata extends Metadata {
058
059        private final TagAttribute attr;
060
061        MetaValueExpressionMetadata(TagAttribute attr) {
062            this.attr = attr;
063        }
064
065        @Override
066        public void applyMetadata(FaceletContext ctx, Object instance) {
067            ValueExpression originalExpression = attr.getValueExpression(ctx, Object.class);
068            ((UIComponent) instance).setValueExpression("value",
069                    new MetaValueExpression(originalExpression, ctx.getFunctionMapper(), ctx.getVariableMapper()));
070        }
071    }
072
073    public static final MetaValueHolderRule Instance = new MetaValueHolderRule();
074
075    @Override
076    public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta) {
077        if (meta.isTargetInstanceOf(ValueHolder.class)) {
078            if ("value".equals(name)) {
079                if (attribute.isLiteral()) {
080                    return new LiteralValueMetadata(attribute.getValue());
081                } else {
082                    return new MetaValueExpressionMetadata(attribute);
083                }
084            }
085
086        }
087        return null;
088    }
089
090}