This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Jason suggested that I post this here. I've noticed that Digester is "aggressive" with mapping XML attributes to matching method names in target objects, and I cannot (at the moment) figure out which Rule to configure to turn it off. I would be most happy if someone else has an answer.
Here is some very short example code. Note that there is only ONE addSetProperties call to the digester instance for the 'name' property, but if you run this example ALL THREE attributes get mapped. I only want it to map the attributes that I tell it to (it's a long story...).
========================== XML FILE... (note: 3 attributes in XML file) ==========================
I'm not in a position to try this out right now, but I think this will work...
The Digester class has three addSetProperties() methods, each corresponding to the different constructors of SetPropertiesRule.
addSetProperties(java.lang.Stringpattern) - This will attempt to match all XML attributes for the specified patterh to properties in the Object on top of the stack. So in your case if you did digester.addSetProperties("testclass"), you would see the same behavior you are experiencing now since Digester would call setName(), setAttr1, and setAttr2() passing data from the appropriate attributes.
addSetProperties(java.lang.String pattern, java.lang.String attributeName, java.lang.String propertyName) - If you have one attribute in your XML element that doesn't match up with a property in your class that you want to set the attribute data to, you can use this method. Say for example instead of having a "name" property that your TestClass had a "nom" property. In order to get all the attributes set correctly in this case you might do the following:
addSetProperties(java.lang.String pattern, java.lang.String attributeNames, java.lang.String propertyNames) - I think this is the one you want. The previous method I talked about is really just a convenient alternative to this one. This allows you to explicitly match your attribute names to the properties you want to map them to. I won't bore you with the details since they explain it pretty well in the javadocs here. Suffice it to say that the following should work for this example:
The above example would set only the "name" property. If you wanted to set both "name" and "attr2" you would do the following:
Again, I'm not in a position right now to actually try this out, but I'm pretty sure that will do what you're looking for if I've understood your problem correctly. [ May 11, 2005: Message edited by: Jason Menard ]
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com