001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.webapp.parser;
035:
036: import org.apache.commons.beanutils.BeanUtils;
037: import org.apache.commons.digester.Rule;
038: import org.xml.sax.Attributes;
039:
040: import javax.el.MethodExpression;
041: import javax.el.ValueExpression;
042: import javax.faces.context.FacesContext;
043: import javax.faces.webapp.UIComponentTag;
044: import javax.faces.webapp.UIComponentELTag;
045: import javax.faces.event.ActionEvent;
046: import java.util.HashMap;
047:
048: public class ELSetPropertiesRule extends Rule {
049:
050: public void begin(Attributes attributes) throws Exception {
051: FacesContext facesContext = FacesContext.getCurrentInstance();
052: HashMap values = new HashMap();
053: Object top = digester.peek();
054:
055: for (int i = 0; i < attributes.getLength(); i++) {
056: String name = attributes.getLocalName(i);
057: if ("".equals(name)) {
058: name = attributes.getQName(i);
059: }
060: String value = attributes.getValue(i);
061:
062: //take a guess at the types of the JSF 1.2 tag members
063: //we are probably better off doing this reflectively
064: if (name != null) {
065: values.put(name, value);
066: if (("id".equals(name)) || ("name".equals(name))
067: || ("var".equals(name))) {
068: values.put(name, value);
069: } else if (top instanceof UIComponentTag) {
070: //must be a JSF 1.1 tag
071: values.put(name, value);
072: } else if ("action".equals(name)) {
073: values.put(name, getMethodExpression(facesContext,
074: name, value, null));
075: } else if ("actionListener".equals(name)) {
076: values.put(name, getMethodExpression(facesContext,
077: name, value, ActionEvent.class));
078: } else {
079: values.put(name, getValueExpression(facesContext,
080: name, value));
081: }
082: if (top instanceof javax.faces.webapp.UIComponentELTag) {
083: //special case for
084: //com.sun.faces.taglib.jsf_core.ParameterTag
085: //and potentially others
086: if ("name".equals(name)) {
087: values.put(name, getValueExpression(
088: facesContext, name, value));
089: }
090: }
091:
092: }
093: }
094:
095: BeanUtils.populate(top, values);
096: }
097:
098: private ValueExpression getValueExpression(
099: FacesContext facesContext, String name, String value) {
100:
101: Class argType = Object.class;
102: try {
103: if (value.equalsIgnoreCase("true")
104: || value.equalsIgnoreCase("false")) {
105: argType = Boolean.class;
106: } else if (null != Integer.valueOf(value)) {
107: //attempt to coerce to Integer type for standard JSF components
108: argType = Integer.class;
109: }
110: } catch (NumberFormatException e) {
111: }
112:
113: ValueExpression valueExpression = facesContext.getApplication()
114: .getExpressionFactory().createValueExpression(
115: facesContext.getELContext(), value, argType);
116:
117: return valueExpression;
118: }
119:
120: private MethodExpression getMethodExpression(
121: FacesContext facesContext, String name, String value,
122: Class argType) {
123: Class[] argTypes = new Class[] {};
124: if (null != argType) {
125: argTypes = new Class[] { argType };
126: }
127:
128: MethodExpression methodExpression = facesContext
129: .getApplication().getExpressionFactory()
130: .createMethodExpression(facesContext.getELContext(),
131: value, String.class, argTypes);
132: return methodExpression;
133: }
134:
135: }
|