aspose file tools*
The moose likes Other Open Source Projects and the fly likes Digester Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Digester "aggressive mapping" Config Question" Watch "Digester "aggressive mapping" Config Question" New topic
Author

Digester "aggressive mapping" Config Question

Doug Meil
Greenhorn

Joined: May 11, 2005
Posts: 1
Hi there-

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)
==========================

<testclass
name="zone"
attr1="attr1value"
attr2="attr2value"
/>

==================
DIGESETER CODE...
=================

FileInputStream fis = new FileInputStream( file );

Digester digester = new Digester();
digester.addObjectCreate( "testclass", TestClass.class);
digester.addSetProperties( "testclass", "name", "name");
TestClass klass = (TestClass) digester.parse( fis );

System.out.println("testclass... " + klass );

======================
TestClass (for good measure)...
======================
public static class TestClass {
private String name;
private String attr1;
private String attr2;

public void setName(String n) {name = n;}
public String getName() {return name;}
public void setAttr1(String n) {attr1 = n;}
public String getAttr1() {return attr1;}
public void setAttr2(String n) {attr2 = n;}
public String getAttr2() {return attr2;}

public String toString() {
String s = "[TestClass] ";
s += "name=" + name;
s += ", attr1=" + attr1;
s += ", attr2=" + attr2;
return s;
}
}
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
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.String pattern) - 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:

digester.addSetProperties( "testclass", "name", "nom");

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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Digester "aggressive mapping" Config Question