| 1 |
/* |
|---|
| 2 |
* (C) Copyright 2007 Nuxeo SAS (http://nuxeo.com/) and contributors. |
|---|
| 3 |
* |
|---|
| 4 |
* All rights reserved. This program and the accompanying materials |
|---|
| 5 |
* are made available under the terms of the GNU Lesser General Public License |
|---|
| 6 |
* (LGPL) version 2.1 which accompanies this distribution, and is available at |
|---|
| 7 |
* http://www.gnu.org/licenses/lgpl.html |
|---|
| 8 |
* |
|---|
| 9 |
* This library is distributed in the hope that it will be useful, |
|---|
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 12 |
* Lesser General Public License for more details. |
|---|
| 13 |
* |
|---|
| 14 |
* Contributors: |
|---|
| 15 |
* Nuxeo - initial API and implementation |
|---|
| 16 |
* |
|---|
| 17 |
* $Id$ |
|---|
| 18 |
*/ |
|---|
| 19 |
|
|---|
| 20 |
package org.nuxeo.ecm.platform.ui.web.tag.jsf; |
|---|
| 21 |
|
|---|
| 22 |
import javax.el.MethodExpression; |
|---|
| 23 |
import javax.el.ValueExpression; |
|---|
| 24 |
import javax.faces.component.UIComponent; |
|---|
| 25 |
import javax.faces.component.ValueHolder; |
|---|
| 26 |
import javax.faces.convert.Converter; |
|---|
| 27 |
|
|---|
| 28 |
import org.apache.commons.logging.Log; |
|---|
| 29 |
import org.apache.commons.logging.LogFactory; |
|---|
| 30 |
import org.nuxeo.ecm.platform.ui.web.binding.MethodValueExpression; |
|---|
| 31 |
import org.nuxeo.ecm.platform.ui.web.util.ComponentTagUtils; |
|---|
| 32 |
|
|---|
| 33 |
import com.sun.facelets.FaceletContext; |
|---|
| 34 |
import com.sun.facelets.el.LegacyValueBinding; |
|---|
| 35 |
import com.sun.facelets.tag.MetaRule; |
|---|
| 36 |
import com.sun.facelets.tag.Metadata; |
|---|
| 37 |
import com.sun.facelets.tag.MetadataTarget; |
|---|
| 38 |
import com.sun.facelets.tag.TagAttribute; |
|---|
| 39 |
import com.sun.facelets.util.FacesAPI; |
|---|
| 40 |
|
|---|
| 41 |
/** |
|---|
| 42 |
* Generic value rule, used to evaluate an expression as a regular value |
|---|
| 43 |
* expression, or invoking a method expression. |
|---|
| 44 |
* <p> |
|---|
| 45 |
* The method can have parameters and the expression must use parentheses even |
|---|
| 46 |
* if no parameters are needed. |
|---|
| 47 |
* |
|---|
| 48 |
* @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> |
|---|
| 49 |
* |
|---|
| 50 |
*/ |
|---|
| 51 |
public class GenericValueHolderRule extends MetaRule { |
|---|
| 52 |
|
|---|
| 53 |
private static final Log log = LogFactory.getLog(GenericValueHolderRule.class); |
|---|
| 54 |
|
|---|
| 55 |
static final class LiteralConverterMetadata extends Metadata { |
|---|
| 56 |
|
|---|
| 57 |
private final String converterId; |
|---|
| 58 |
|
|---|
| 59 |
LiteralConverterMetadata(String converterId) { |
|---|
| 60 |
this.converterId = converterId; |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
@Override |
|---|
| 64 |
public void applyMetadata(FaceletContext ctx, Object instance) { |
|---|
| 65 |
((ValueHolder) instance).setConverter( |
|---|
| 66 |
ctx.getFacesContext().getApplication().createConverter( |
|---|
| 67 |
converterId)); |
|---|
| 68 |
} |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
static final class DynamicConverterMetadata extends Metadata { |
|---|
| 72 |
|
|---|
| 73 |
private final TagAttribute attr; |
|---|
| 74 |
|
|---|
| 75 |
DynamicConverterMetadata(TagAttribute attr) { |
|---|
| 76 |
this.attr = attr; |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
@Override |
|---|
| 80 |
public void applyMetadata(FaceletContext ctx, Object instance) { |
|---|
| 81 |
((UIComponent) instance).setValueBinding("converter", |
|---|
| 82 |
new LegacyValueBinding(attr.getValueExpression(ctx, |
|---|
| 83 |
Converter.class))); |
|---|
| 84 |
} |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
static final class DynamicConverterMetadata2 extends Metadata { |
|---|
| 88 |
|
|---|
| 89 |
private final TagAttribute attr; |
|---|
| 90 |
|
|---|
| 91 |
DynamicConverterMetadata2(TagAttribute attr) { |
|---|
| 92 |
this.attr = attr; |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
@Override |
|---|
| 96 |
public void applyMetadata(FaceletContext ctx, Object instance) { |
|---|
| 97 |
((UIComponent) instance).setValueExpression("converter", |
|---|
| 98 |
attr.getValueExpression(ctx, Converter.class)); |
|---|
| 99 |
} |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
static final class LiteralValueMetadata extends Metadata { |
|---|
| 103 |
|
|---|
| 104 |
private final String value; |
|---|
| 105 |
|
|---|
| 106 |
LiteralValueMetadata(String value) { |
|---|
| 107 |
this.value = value; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
@Override |
|---|
| 111 |
public void applyMetadata(FaceletContext ctx, Object instance) { |
|---|
| 112 |
((ValueHolder) instance).setValue(value); |
|---|
| 113 |
} |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
static final class DynamicValueExpressionMetadata extends Metadata { |
|---|
| 117 |
|
|---|
| 118 |
private final TagAttribute attr; |
|---|
| 119 |
|
|---|
| 120 |
DynamicValueExpressionMetadata(TagAttribute attr) { |
|---|
| 121 |
this.attr = attr; |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
@Override |
|---|
| 125 |
public void applyMetadata(FaceletContext ctx, Object instance) { |
|---|
| 126 |
((UIComponent) instance).setValueExpression("value", |
|---|
| 127 |
attr.getValueExpression(ctx, Object.class)); |
|---|
| 128 |
} |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
static final class DynamicValueBindingMetadata extends Metadata { |
|---|
| 132 |
|
|---|
| 133 |
private final TagAttribute attr; |
|---|
| 134 |
|
|---|
| 135 |
DynamicValueBindingMetadata(TagAttribute attr) { |
|---|
| 136 |
this.attr = attr; |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
@Override |
|---|
| 140 |
public void applyMetadata(FaceletContext ctx, Object instance) { |
|---|
| 141 |
((UIComponent) instance).setValueBinding("value", |
|---|
| 142 |
new LegacyValueBinding(attr.getValueExpression(ctx, |
|---|
| 143 |
Object.class))); |
|---|
| 144 |
} |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
static final class MethodValueBindingMetadata extends Metadata { |
|---|
| 148 |
|
|---|
| 149 |
private final TagAttribute attr; |
|---|
| 150 |
|
|---|
| 151 |
MethodValueBindingMetadata(TagAttribute attr) { |
|---|
| 152 |
this.attr = attr; |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
@Override |
|---|
| 156 |
public void applyMetadata(FaceletContext ctx, Object instance) { |
|---|
| 157 |
Class[] paramTypesClasses = new Class[0]; |
|---|
| 158 |
Class returnType = Object.class; |
|---|
| 159 |
MethodExpression meth = attr.getMethodExpression(ctx, returnType, |
|---|
| 160 |
paramTypesClasses); |
|---|
| 161 |
ValueExpression ve = new MethodValueExpression(meth, |
|---|
| 162 |
paramTypesClasses); |
|---|
| 163 |
((UIComponent) instance).setValueBinding("value", |
|---|
| 164 |
new LegacyValueBinding(ve)); |
|---|
| 165 |
} |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
public static final GenericValueHolderRule Instance = new GenericValueHolderRule(); |
|---|
| 169 |
|
|---|
| 170 |
@Override |
|---|
| 171 |
public Metadata applyRule(String name, TagAttribute attribute, |
|---|
| 172 |
MetadataTarget meta) { |
|---|
| 173 |
if (meta.isTargetInstanceOf(ValueHolder.class)) { |
|---|
| 174 |
|
|---|
| 175 |
if ("converter".equals(name)) { |
|---|
| 176 |
if (attribute.isLiteral()) { |
|---|
| 177 |
return new LiteralConverterMetadata(attribute.getValue()); |
|---|
| 178 |
} else { |
|---|
| 179 |
if (FacesAPI.getComponentVersion(meta.getTargetClass()) >= 12) { |
|---|
| 180 |
return new DynamicConverterMetadata2(attribute); |
|---|
| 181 |
} else { |
|---|
| 182 |
return new DynamicConverterMetadata(attribute); |
|---|
| 183 |
} |
|---|
| 184 |
} |
|---|
| 185 |
} |
|---|
| 186 |
|
|---|
| 187 |
if ("genericValue".equals(name)) { |
|---|
| 188 |
if (attribute.isLiteral()) { |
|---|
| 189 |
return new LiteralValueMetadata(attribute.getValue()); |
|---|
| 190 |
} else { |
|---|
| 191 |
String value = attribute.getValue(); |
|---|
| 192 |
if (ComponentTagUtils.isMethodReference(value)) { |
|---|
| 193 |
// value expression resolved invoking a method |
|---|
| 194 |
// expression |
|---|
| 195 |
return new MethodValueBindingMetadata(attribute); |
|---|
| 196 |
} else { |
|---|
| 197 |
// regular value expression |
|---|
| 198 |
if (FacesAPI.getComponentVersion(meta.getTargetClass()) >= 12) { |
|---|
| 199 |
return new DynamicValueExpressionMetadata(attribute); |
|---|
| 200 |
} else { |
|---|
| 201 |
return new DynamicValueBindingMetadata(attribute); |
|---|
| 202 |
} |
|---|
| 203 |
} |
|---|
| 204 |
} |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
} |
|---|
| 208 |
return null; |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
} |
|---|