/*
* (C) Copyright 2007 Nuxeo SA (http://nuxeo.com/) and others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* Nuxeo - initial API and implementation
*
* $Id: MetaValueHolderRule.java 28460 2008-01-03 15:34:05Z sfermigier $
*/
package org.nuxeo.ecm.platform.ui.web.tag.jsf;
import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
import javax.faces.component.ValueHolder;
import javax.faces.view.facelets.FaceletContext;
import javax.faces.view.facelets.MetaRule;
import javax.faces.view.facelets.Metadata;
import javax.faces.view.facelets.MetadataTarget;
import javax.faces.view.facelets.TagAttribute;
import org.nuxeo.ecm.platform.ui.web.binding.MetaValueExpression;
/**
* Meta value rule, used to evaluate an expression as a regular value expression, or invoking it again as another value
* expression or method expression.
* <p>
* The method can have parameters and the expression must use parentheses even if no parameters are needed.
*
* @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
*/
public class MetaValueHolderRule extends MetaRule {
static final class LiteralValueMetadata extends Metadata {
private final String value;
LiteralValueMetadata(String value) {
this.value = value;
}
@Override
public void applyMetadata(FaceletContext ctx, Object instance) {
((ValueHolder) instance).setValue(value);
}
}
static final class MetaValueExpressionMetadata extends Metadata {
private final TagAttribute attr;
MetaValueExpressionMetadata(TagAttribute attr) {
this.attr = attr;
}
@Override
public void applyMetadata(FaceletContext ctx, Object instance) {
ValueExpression originalExpression = attr.getValueExpression(ctx, Object.class);
((UIComponent) instance).setValueExpression("value",
new MetaValueExpression(originalExpression, ctx.getFunctionMapper(), ctx.getVariableMapper()));
}
}
public static final MetaValueHolderRule Instance = new MetaValueHolderRule();
@Override
public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta) {
if (meta.isTargetInstanceOf(ValueHolder.class)) {
if ("value".equals(name)) {
if (attribute.isLiteral()) {
return new LiteralValueMetadata(attribute.getValue());
} else {
return new MetaValueExpressionMetadata(attribute);
}
}
}
return null;
}
}