commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Hans Gilde <hansgi...@earthlink.net>
Subject [PATCH] Jelly UseBean minor improvement
Date Fri, 20 Aug 2004 01:32:46 GMT
It's sort of hard to extend UseBean because it will try to set all the tag
attributes as bean properties. You can remove an attribute from the map, but
that's annoying because then your subclasses don't see it.

 

I added a couple of methods that let a subclass tell UseBean to ignore
certain property names. So, those properties stay in the attribute map but
don't cause errors in setting the bean.

 

This is the first step to a patch to clean up the Swing ComponentTag so that
it fails properly when one misspells a bean property. As long as everyone
agrees to this method of ignoring attributes that aren't bean properties.

 

Hans

 

##########################

 

Index: UseBeanTag.java

===================================================================

RCS file:
/home/cvspublic/jakarta-commons/jelly/src/java/org/apache/commons/jelly/tags
/core/UseBeanTag.java,v

retrieving revision 1.14

diff -u -r1.14 UseBeanTag.java

--- UseBeanTag.java      24 Feb 2004 14:10:38 -0000       1.14

+++ UseBeanTag.java   20 Aug 2004 01:22:12 -0000

@@ -16,13 +16,15 @@

 package org.apache.commons.jelly.tags.core;

 

 import java.lang.reflect.InvocationTargetException;

+import java.util.HashMap;

+import java.util.HashSet;

 import java.util.Map;

+import java.util.Set;

 

 import org.apache.commons.beanutils.BeanUtils;

-

 import org.apache.commons.jelly.JellyTagException;

-import org.apache.commons.jelly.MissingAttributeException;

 import org.apache.commons.jelly.MapTagSupport;

+import org.apache.commons.jelly.MissingAttributeException;

 import org.apache.commons.jelly.XMLOutput;

 import org.apache.commons.jelly.impl.BeanSource;

 

@@ -50,6 +52,11 @@

     

     /** the default class to use if no Class is specified */

     private Class defaultClass;

+    

+    /**a Set of Strings of property names to ignore (remove from the 

+     * Map of attributes before passing to ConvertUtils)

+     */

+    private Set ignoreProperties;

 

     public UseBeanTag() {

     }

@@ -74,7 +81,8 @@

     public void doTag(XMLOutput output) throws JellyTagException {

         Map attributes = getAttributes();

         String var = (String) attributes.get( "var" );

-        Object classObject = attributes.remove( "class" );

+        Object classObject = attributes.get( "class" );

+        addIgnoreProperty("class");

         

         try {

             // this method could return null in derived classes

@@ -163,10 +171,15 @@

     /**

      * Sets the properties on the bean. Derived tags could implement some
custom 

      * type conversion etc.

+     * <p/>

+     * This method ignores all property names in the Set returned by {@link
#getIgnorePropertySet()}.

      */

     protected void setBeanProperties(Object bean, Map attributes) throws
JellyTagException {

+        Map attrsToUse = new HashMap(attributes);

+        attrsToUse.keySet().removeAll(getIgnorePropertySet());

+        

         try {

-            BeanUtils.populate(bean, attributes);

+            BeanUtils.populate(bean, attrsToUse);

         } catch (IllegalAccessException e) {

             throw new JellyTagException("could not set the properties of
the bean",e);

         } catch (InvocationTargetException e) {

@@ -196,5 +209,26 @@

      */

     protected Class getDefaultClass() {

         return defaultClass;

+    }

+    

+    /** Adds a name to the Set of property names that will be skipped when
setting

+     * bean properties. In other words, names added here won't be set into
the bean

+     * if they're present in the attribute Map.

+     * @param name

+     */

+    protected void addIgnoreProperty(String name) {

+        getIgnorePropertySet().add(name); 

+    }

+    

+    /** Gets the Set of property names that should be ignored when setting
the

+     * properties of the bean.

+     * @return

+     */

+    protected Set getIgnorePropertySet() {

+        if (ignoreProperties == null) {

+            ignoreProperties = new HashSet();

+        }

+        

+        return ignoreProperties;

     }

 }


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message