| 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.handler; |
|---|
| 21 |
|
|---|
| 22 |
import java.io.IOException; |
|---|
| 23 |
|
|---|
| 24 |
import javax.el.MethodExpression; |
|---|
| 25 |
import javax.el.ValueExpression; |
|---|
| 26 |
import javax.faces.component.UIComponent; |
|---|
| 27 |
|
|---|
| 28 |
import org.nuxeo.ecm.platform.ui.web.binding.MethodValueExpression; |
|---|
| 29 |
|
|---|
| 30 |
import com.sun.facelets.FaceletContext; |
|---|
| 31 |
import com.sun.facelets.tag.MetaTagHandler; |
|---|
| 32 |
import com.sun.facelets.tag.TagAttribute; |
|---|
| 33 |
import com.sun.facelets.tag.TagConfig; |
|---|
| 34 |
|
|---|
| 35 |
/** |
|---|
| 36 |
* Tag handler that exposes the result of a method binding in the variable map. |
|---|
| 37 |
* |
|---|
| 38 |
* @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> |
|---|
| 39 |
* |
|---|
| 40 |
*/ |
|---|
| 41 |
public class MethodResultTagHandler extends MetaTagHandler { |
|---|
| 42 |
|
|---|
| 43 |
private static final Class[] DEFAULT_PARAM_TYPES_CLASSES = new Class[0]; |
|---|
| 44 |
|
|---|
| 45 |
private final TagAttribute name; |
|---|
| 46 |
|
|---|
| 47 |
private final TagAttribute value; |
|---|
| 48 |
|
|---|
| 49 |
private final TagAttribute immediate; |
|---|
| 50 |
|
|---|
| 51 |
private final TagAttribute paramTypes; |
|---|
| 52 |
|
|---|
| 53 |
public MethodResultTagHandler(TagConfig config) { |
|---|
| 54 |
super(config); |
|---|
| 55 |
name = getRequiredAttribute("name"); |
|---|
| 56 |
value = getRequiredAttribute("value"); |
|---|
| 57 |
immediate = getAttribute("immediate"); |
|---|
| 58 |
paramTypes = getAttribute("paramTypes"); |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
private Class[] resolveParamTypes(FaceletContext ctx) { |
|---|
| 62 |
// TODO: implement string parsing ? |
|---|
| 63 |
return DEFAULT_PARAM_TYPES_CLASSES; |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
public void apply(FaceletContext ctx, UIComponent parent) |
|---|
| 67 |
throws IOException { |
|---|
| 68 |
String nameStr = name.getValue(ctx); |
|---|
| 69 |
// resolve given value as a method binding, paramtypes ignored for now |
|---|
| 70 |
Class[] paramTypesClasses = resolveParamTypes(ctx); |
|---|
| 71 |
MethodExpression meth = value.getMethodExpression(ctx, Object.class, |
|---|
| 72 |
paramTypesClasses); |
|---|
| 73 |
ValueExpression ve = null; |
|---|
| 74 |
Boolean invokeNow = false; |
|---|
| 75 |
if (immediate != null) { |
|---|
| 76 |
invokeNow = immediate.getBoolean(ctx); |
|---|
| 77 |
} |
|---|
| 78 |
if (invokeNow) { |
|---|
| 79 |
Object res = meth.invoke(ctx, paramTypesClasses); |
|---|
| 80 |
ve = ctx.getExpressionFactory().createValueExpression(res, |
|---|
| 81 |
Object.class); |
|---|
| 82 |
} else { |
|---|
| 83 |
ve = new MethodValueExpression(meth, paramTypesClasses); |
|---|
| 84 |
} |
|---|
| 85 |
ctx.getVariableMapper().setVariable(nameStr, ve); |
|---|
| 86 |
this.nextHandler.apply(ctx, parent); |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
} |
|---|