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: GenericValueHolderRule.java 28456 2008-01-03 12:01:11Z sfermigier $ 020 */ 021 022package org.nuxeo.ecm.platform.ui.web.tag.jsf; 023 024import javax.el.MethodExpression; 025import javax.el.ValueExpression; 026import javax.faces.component.UIComponent; 027import javax.faces.component.ValueHolder; 028import javax.faces.convert.Converter; 029import javax.faces.view.facelets.FaceletContext; 030import javax.faces.view.facelets.MetaRule; 031import javax.faces.view.facelets.Metadata; 032import javax.faces.view.facelets.MetadataTarget; 033import javax.faces.view.facelets.TagAttribute; 034 035import org.nuxeo.ecm.platform.ui.web.binding.MethodValueExpression; 036import org.nuxeo.ecm.platform.ui.web.util.ComponentTagUtils; 037 038import com.sun.faces.facelets.el.LegacyValueBinding; 039 040/** 041 * Generic value rule, used to evaluate an expression as a regular value expression, or invoking a method expression. 042 * <p> 043 * The method can have parameters and the expression must use parentheses even if no parameters are needed. 044 * 045 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 046 * @deprecated since 5.7.3: resolving method expressions for value expressions is now supported by the default EL, this 047 * is now useless 048 */ 049@Deprecated 050public class GenericValueHolderRule extends MetaRule { 051 052 static final class LiteralConverterMetadata extends Metadata { 053 054 private final String converterId; 055 056 LiteralConverterMetadata(String converterId) { 057 this.converterId = converterId; 058 } 059 060 @Override 061 public void applyMetadata(FaceletContext ctx, Object instance) { 062 ((ValueHolder) instance).setConverter(ctx.getFacesContext().getApplication().createConverter(converterId)); 063 } 064 } 065 066 static final class DynamicConverterMetadata2 extends Metadata { 067 068 private final TagAttribute attr; 069 070 DynamicConverterMetadata2(TagAttribute attr) { 071 this.attr = attr; 072 } 073 074 @Override 075 public void applyMetadata(FaceletContext ctx, Object instance) { 076 ((UIComponent) instance).setValueExpression("converter", attr.getValueExpression(ctx, Converter.class)); 077 } 078 } 079 080 static final class LiteralValueMetadata extends Metadata { 081 082 private final String value; 083 084 LiteralValueMetadata(String value) { 085 this.value = value; 086 } 087 088 @Override 089 public void applyMetadata(FaceletContext ctx, Object instance) { 090 ((ValueHolder) instance).setValue(value); 091 } 092 } 093 094 static final class DynamicValueExpressionMetadata extends Metadata { 095 096 private final TagAttribute attr; 097 098 DynamicValueExpressionMetadata(TagAttribute attr) { 099 this.attr = attr; 100 } 101 102 @Override 103 public void applyMetadata(FaceletContext ctx, Object instance) { 104 ((UIComponent) instance).setValueExpression("value", attr.getValueExpression(ctx, Object.class)); 105 } 106 } 107 108 static final class MethodValueBindingMetadata extends Metadata { 109 110 private final TagAttribute attr; 111 112 MethodValueBindingMetadata(TagAttribute attr) { 113 this.attr = attr; 114 } 115 116 @Override 117 public void applyMetadata(FaceletContext ctx, Object instance) { 118 Class[] paramTypesClasses = new Class[0]; 119 Class returnType = Object.class; 120 MethodExpression meth = attr.getMethodExpression(ctx, returnType, paramTypesClasses); 121 ValueExpression ve = new MethodValueExpression(ctx.getFunctionMapper(), ctx.getVariableMapper(), meth, 122 paramTypesClasses); 123 ((UIComponent) instance).setValueBinding("value", new LegacyValueBinding(ve)); 124 } 125 } 126 127 public static final GenericValueHolderRule Instance = new GenericValueHolderRule(); 128 129 @Override 130 public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta) { 131 if (meta.isTargetInstanceOf(ValueHolder.class)) { 132 133 if ("converter".equals(name)) { 134 if (attribute.isLiteral()) { 135 return new LiteralConverterMetadata(attribute.getValue()); 136 } else { 137 return new DynamicConverterMetadata2(attribute); 138 } 139 } 140 141 if ("genericValue".equals(name)) { 142 if (attribute.isLiteral()) { 143 return new LiteralValueMetadata(attribute.getValue()); 144 } else { 145 String value = attribute.getValue(); 146 if (ComponentTagUtils.isMethodReference(value)) { 147 // value expression resolved invoking a method 148 // expression 149 return new MethodValueBindingMetadata(attribute); 150 } else { 151 // regular value expression 152 return new DynamicValueExpressionMetadata(attribute); 153 } 154 } 155 } 156 157 } 158 return null; 159 } 160 161}