Author: gvanmatre Date: Tue Jan 17 20:35:49 2006 New Revision: 370045 URL: http://svn.apache.org/viewcvs?rev=370045&view=rev Log: Refactored removing a dependency with commons-beanutils by using the new ProperyHelper and ConverterHelper classes in the shale core library.
Removed: struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/utils/PropUtils.java struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/utils/PropUtilsTestCase.java Modified: struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/component/chain/PropertyValueCommand.java Modified: struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/component/chain/PropertyValueCommand.java URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/component/chain/PropertyValueCommand.java?rev=370045&r1=370044&r2=370045&view=diff ============================================================================== --- struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/component/chain/PropertyValueCommand.java (original) +++ struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/component/chain/PropertyValueCommand.java Tue Jan 17 20:35:49 2006 @@ -20,6 +20,7 @@ import javax.faces.component.UIComponentBase; import javax.faces.context.FacesContext; +import javax.faces.el.PropertyNotFoundException; import javax.faces.el.ValueBinding; import org.apache.commons.chain.Command; @@ -28,8 +29,9 @@ import org.apache.commons.logging.LogFactory; import org.apache.shale.clay.config.beans.AttributeBean; import org.apache.shale.clay.config.beans.ComponentBean; -import org.apache.shale.clay.utils.PropUtils; import org.apache.shale.faces.ShaleConstants; +import org.apache.shale.util.ConverterHelper; +import org.apache.shale.util.PropertyHelper; import org.apache.shale.util.Tags; /** @@ -48,6 +50,54 @@ } /** + * <p>Helper class to provide access to the properties of JavaBeans.</p> + */ + private PropertyHelper propertyHelper = new PropertyHelper(); + + /** + * <p>Helper class to provide access to by-type JavaServer Faces + * <code>Converter</code> capabilities.</p> + */ + private ConverterHelper converterHelper = new ConverterHelper(); + + /** + * <p>Sets a property value on the target component. If the data + * type of the target bean property is not a String and the property + * value is a String, the data type is converted to the target type + * using the <code>ConverterHelper</code>. The value is applied to + * the component using <code>PropertyHelper</code>.</p> + */ + private void setProperty(FacesContext context, Object target, String propertyName, Object propertyValue) { + Class classz = null; + StringBuffer actualPropertyName = new StringBuffer(propertyName); + + try { + classz = propertyHelper.getType(target, actualPropertyName.toString()); + } catch (PropertyNotFoundException e) { + if (propertyName.length() > 3 && propertyName.startsWith("is") ) { + actualPropertyName.delete(0, 2); + actualPropertyName.setCharAt(0, Character.toLowerCase(actualPropertyName.charAt(0))); + + classz = propertyHelper.getType(target, actualPropertyName.toString()); + } else { + throw e; + } + + } + + if (classz != Object.class && classz != propertyValue.getClass() + && propertyValue.getClass() == String.class) { + + Object targetValue = converterHelper.asObject(context, classz, (String) propertyValue); + propertyHelper.setValue(target, actualPropertyName.toString(), targetValue); + } else { + propertyHelper.setValue(target, actualPropertyName.toString(), propertyValue); + } + + } + + + /** * <p> * Looks at the [EMAIL PROTECTED] AttributeBean} on the [EMAIL PROTECTED] ClayContext} to see * if the value is a binding EL. If it is not it just updates the component @@ -119,8 +169,8 @@ ValueBinding vb = facesContext.getApplication().createValueBinding(expr); Object value = vb.getValue(facesContext); try { - PropUtils.setProperty(child, attributeBean.getName(), value); - } catch (Exception e) { + setProperty(facesContext ,child, attributeBean.getName(), value); + } catch (Exception e) { if (child instanceof UIComponentBase) ((UIComponentBase) child).getAttributes().put(attributeBean.getName(), expr); else @@ -128,7 +178,7 @@ } } else { try { - PropUtils.setProperty(child, attributeBean.getName(), expr); + setProperty(facesContext, child, attributeBean.getName(), expr); } catch (Exception e) { if (child instanceof UIComponentBase) ((UIComponentBase) child).getAttributes().put(attributeBean.getName(), expr); --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]