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