Issue Details (XML | Word | Printable)

Key: JELLY-120
Type: Improvement Improvement
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: dion gillard
Reporter: Hans Gilde
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Commons Jelly

UseBean tag improvement

Created: 21/Aug/04 05:32 PM   Updated: 31/Aug/04 05:01 AM
Return to search
Component/s: core / taglib.core
Affects Version/s: 1.0-beta-4
Fix Version/s: 1.0-beta-4

Time Tracking:
Not Specified

File Attachments:
  Size
Text File Licensed for inclusion in ASF works use-bean.txt 2004-08-30 12:32 AM Hans Gilde 5 kB
Zip Archive Licensed for inclusion in ASF works usebean-validate.zip 2004-08-31 04:15 AM Hans Gilde 2 kB

Resolution Date: 31/Aug/04 04:28 AM


 Description  « Hide
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;

}

}



 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
dion gillard added a comment - 30/Aug/04 12:05 AM
I think this should be done for beta4

dion gillard made changes - 30/Aug/04 12:05 AM
Field Original Value New Value
Fix Version/s 1.0-beta-4 [ 10215 ]
dion gillard made changes - 30/Aug/04 12:14 AM
Assignee dion gillard [ diongillard ]
dion gillard made changes - 30/Aug/04 12:30 AM
Status Open [ 1 ] In Progress [ 3 ]
Hans Gilde added a comment - 30/Aug/04 12:32 AM
cool, how about this one. It adds an option to ignore unknown properties. I figured that subclasses of UseBean might want it to ignore unknown properties.

The thing is that to make this work, I had to change from BeanUtils.populate(), to BeanUtils.copyProperties(). I think that this is a good change but it needs unit tests, which I'll do.


Hans Gilde made changes - 30/Aug/04 12:32 AM
Attachment use-bean.txt [ 15050 ]
dion gillard added a comment - 30/Aug/04 12:44 AM
I'm just in the process of adding test cases for the addIgnoredProperty code.

Repository Revision Date User Message
ASF #136056 Mon Aug 30 01:04:12 UTC 2004 dion Add test classes to work the code in JELLY-120
Files Changed
ADD /jakarta/commons/proper/jelly/trunk/src/test/org/apache/commons/jelly/core/extension
ADD /jakarta/commons/proper/jelly/trunk/src/test/org/apache/commons/jelly/core/extension/UseBeanExtendedTag.java
ADD /jakarta/commons/proper/jelly/trunk/src/test/org/apache/commons/jelly/core/extension/CoreExtensionTagLibrary.java

Repository Revision Date User Message
ASF #136057 Mon Aug 30 01:04:28 UTC 2004 dion JELLY-120.
Files Changed
MODIFY /jakarta/commons/proper/jelly/trunk/src/java/org/apache/commons/jelly/tags/core/UseBeanTag.java
MODIFY /jakarta/commons/proper/jelly/trunk/src/test/org/apache/commons/jelly/core/TestUseBeanTag.java

Repository Revision Date User Message
ASF #136058 Mon Aug 30 01:12:25 UTC 2004 dion JELLY-120.
Files Changed
MODIFY /jakarta/commons/proper/jelly/trunk/src/test/org/apache/commons/jelly/core/testUseBeanTag.jelly

dion gillard added a comment - 30/Aug/04 01:20 AM
Hans, could you re-do the patch for unknown properties off the latest code?

That'd be great.

It'd be even better if you added tests to the new test cases I've created.

Thanks


Hans Gilde added a comment - 31/Aug/04 04:15 AM
Let me know if you think of more test cases. There are none with arrays or collections...

Hans Gilde made changes - 31/Aug/04 04:15 AM
Attachment usebean-validate.zip [ 15060 ]
Repository Revision Date User Message
ASF #136060 Tue Aug 31 04:27:04 UTC 2004 dion JELLY-120. Allow subclasses to ignore bad properties
Files Changed
MODIFY /jakarta/commons/proper/jelly/trunk/src/test/org/apache/commons/jelly/core/testUseBeanTag.jelly
ADD /jakarta/commons/proper/jelly/trunk/src/test/org/apache/commons/jelly/core/extension/UseBeanIgnoreBadProps.java
MODIFY /jakarta/commons/proper/jelly/trunk/src/test/org/apache/commons/jelly/core/extension/CoreExtensionTagLibrary.java
MODIFY /jakarta/commons/proper/jelly/trunk/src/java/org/apache/commons/jelly/tags/core/UseBeanTag.java
MODIFY /jakarta/commons/proper/jelly/trunk/src/test/org/apache/commons/jelly/core/TestUseBeanTag.java

dion gillard added a comment - 31/Aug/04 04:28 AM
I made minor changes to the patch:
  • use Properties instead of Props
  • remove use of Jelly junit taglib, and put asserts back in the code
  • added license for new file

dion gillard made changes - 31/Aug/04 04:28 AM
Status In Progress [ 3 ] Closed [ 6 ]
Resolution Fixed [ 1 ]
Repository Revision Date User Message
ASF #136061 Tue Aug 31 04:47:19 UTC 2004 dion Doc JELLY-120
Files Changed
MODIFY /jakarta/commons/proper/jelly/trunk/xdocs/changes.xml

Hans Gilde added a comment - 31/Aug/04 05:01 AM
Thanks. I meant to add this comment to validateBeanProperties.

/** If {@link #isIgnoreUnknownProps()} returns true, make sure that

  • every non-ignored ({@see #addIgnoreProperty(String)}) property
  • matches a writable property on the target bean.
  • @param bean
  • @param attributes
  • @throws JellyTagException
    */