1 33 34 package com.icesoft.faces.webapp.parser; 35 36 import org.apache.commons.beanutils.BeanUtils; 37 import org.apache.commons.digester.Rule; 38 import org.xml.sax.Attributes ; 39 40 import javax.el.MethodExpression; 41 import javax.el.ValueExpression; 42 import javax.faces.context.FacesContext; 43 import javax.faces.webapp.UIComponentTag; 44 import javax.faces.webapp.UIComponentELTag; 45 import javax.faces.event.ActionEvent; 46 import java.util.HashMap ; 47 48 49 public class ELSetPropertiesRule extends Rule { 50 static Class elTagClass = UIComponentELTag.class; 52 53 public void begin(Attributes attributes) throws Exception { 54 FacesContext facesContext = FacesContext.getCurrentInstance(); 55 HashMap values = new HashMap (); 56 Object top = digester.peek(); 57 58 for (int i = 0; i < attributes.getLength(); i++) { 59 String name = attributes.getLocalName(i); 60 if ("".equals(name)) { 61 name = attributes.getQName(i); 62 } 63 String value = attributes.getValue(i); 64 65 if (name != null) { 68 values.put(name, value); 69 if (("id".equals(name)) || 70 ("name".equals(name)) || 71 ("var".equals(name))) { 72 values.put(name, value); 73 } else if (top instanceof UIComponentTag) { 74 values.put(name, value); 76 } else if ("action".equals(name)) { 77 values.put(name, 78 getMethodExpression( facesContext, name, value, 79 null )); 80 } else if ("actionListener".equals(name)) { 81 values.put(name, 82 getMethodExpression(facesContext, name, value, 83 ActionEvent.class)); 84 } else { 85 values.put(name, 86 getValueExpression(facesContext, name, value)); 87 } 88 if (top instanceof javax.faces.webapp.UIComponentELTag) { 89 if ("name".equals(name)) { 93 values.put(name, 94 getValueExpression(facesContext, name, value)); 95 } 96 } 97 98 } 99 } 100 101 102 BeanUtils.populate(top, values); 103 } 104 105 private ValueExpression getValueExpression(FacesContext facesContext, 106 String name, String value) { 107 108 Class argType = Object .class; 109 try { 110 if ( value.equalsIgnoreCase("true") || 111 value.equalsIgnoreCase("false") ) { 112 argType = Boolean .class; 113 } else if (null != Integer.valueOf(value)) { 114 argType = Integer .class; 116 } 117 } catch (NumberFormatException e) { 118 } 119 120 121 ValueExpression valueExpression = 122 facesContext.getApplication().getExpressionFactory() 123 .createValueExpression( 124 facesContext.getELContext(), 125 value, argType ); 126 127 return valueExpression; 128 } 129 130 private MethodExpression getMethodExpression(FacesContext facesContext, 131 String name, String value, 132 Class argType) { 133 Class [] argTypes = new Class []{}; 134 if (null != argType) { 135 argTypes = new Class []{argType}; 136 } 137 138 MethodExpression methodExpression = 139 facesContext.getApplication().getExpressionFactory() 140 .createMethodExpression( 141 facesContext.getELContext(), 142 value, String .class, 143 argTypes); 144 return methodExpression; 145 } 146 147 } 148 | Popular Tags |