You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by William Fuller <wi...@sbcglobal.net> on 2005/01/19 09:49:33 UTC
non-String Bean Digester problem
Hello, I'm new to Digester, Tomcat, Servlet, Velocity, and even Java...
Yup, one of those.
I am running into a spot of bother while using Digester to parse my XML file
that I will ultimately pluck from Derby. The file starts out like this:
<Screen>
<ScreenID>3220D03D-59F7-49ca-86AE-E97B7427BA7B</ScreenID>
<Lifetime_ms>20000</Lifetime_ms>
<BugItem>
<ImageURL>cnn001.jpg</ImageURL>
...
It is a rather large file with many Vectors and I parse it just fine so long
as all the setMethods are of type String. I am using XML Rules and my file
looks like this:
<?xml version="1.0"?>
<digester-rules>
<pattern value="Screen">
<object-create-rule classname="com.synergy.screendoc.ScreenDocument"
/>
<call-method-rule pattern="ScreenID" methodname="setScreenID"
paramcount="0" />
<call-method-rule pattern="Lifetime_ms" methodname="setLifetime_ms"
paramcount="0" />
...
My Java bean for the first level looks like this:
public class ScreenDocument
{
public String ScreenID;
public String Lifetime_ms;
public Vector BugItem;
public Vector StaticTextItem;
public Vector DynamicTextItem;
public ScreenDocument()
{
BugItem = new Vector();
StaticTextItem = new Vector();
DynamicTextItem = new Vector();
}
public String getScreenID() { return ScreenID; }
public void setScreenID( String rhs ) { this.ScreenID = rhs; }
public String getLifetime_ms() { return Lifetime_ms; }
public void setLifetime_ms( String rhs ) { this.Lifetime_ms = rhs; }
...
The problem that I am encountering is that if I try to use
bean-property-setter-rule like this:
<?xml version="1.0"?>
<digester-rules>
<pattern value="Screen">
<object-create-rule classname="com.synergy.screendoc.ScreenDocument"
/>
<set-properties-rule/>
<bean-property-setter-rule pattern="ScreenID"/>
<bean-property-setter-rule pattern="Lifetime_ms"/>
I get the Digester exception that the bean has no property ScreenID.
Having a method that works I would normally just blow this off, but I don't
really want Lifetime_ms to be a String, I want it to be an "int". I would
be happy with it being a java.lang.Integer, but when I do something like
this:
<?xml version="1.0"?>
<digester-rules>
<pattern value="Screen">
<object-create-rule classname="com.synergy.screendoc.ScreenDocument"
/>
<call-method-rule pattern="ScreenID" methodname="setScreenID"
paramcount="0" />
<pattern value="Lifetime_ms">
<call-method-rule methodname="setLifetime_ms" paramcount="1"/>
<object-param-rule paramnumber='0' type="java.lang.Integer" />
</pattern>
...
And change the bean to have Lifetime_ms defined, set, and returned as type
Integer, Digester throws and exception and complains that setLifetime_ms is
not defined in my class.
I am at a loss for understanding why I am having this problem and suspect I
am doing something really dumb.
Can anyone please set me staight?
Thank you in advance.
Will
Re: non-String Bean Digester problem
Posted by Simon Kitching <sk...@apache.org>.
Hi William,
On Wed, 2005-01-19 at 02:49 -0600, William Fuller wrote:
> Hello, I'm new to Digester, Tomcat, Servlet, Velocity, and even Java...
> Yup, one of those.
No worries, we've all been there..
>
> I am running into a spot of bother while using Digester to parse my XML file
> that I will ultimately pluck from Derby. The file starts out like this:
>
>
> <Screen>
> <ScreenID>3220D03D-59F7-49ca-86AE-E97B7427BA7B</ScreenID>
> <Lifetime_ms>20000</Lifetime_ms>
> <BugItem>
> <ImageURL>cnn001.jpg</ImageURL>
> ...
>
> It is a rather large file with many Vectors and I parse it just fine so long
> as all the setMethods are of type String. I am using XML Rules and my file
> looks like this:
>
> <?xml version="1.0"?>
>
> <digester-rules>
> <pattern value="Screen">
> <object-create-rule classname="com.synergy.screendoc.ScreenDocument"
> />
> <call-method-rule pattern="ScreenID" methodname="setScreenID"
> paramcount="0" />
> <call-method-rule pattern="Lifetime_ms" methodname="setLifetime_ms"
> paramcount="0" />
> ...
>
> My Java bean for the first level looks like this:
>
> public class ScreenDocument
> {
> public String ScreenID;
> public String Lifetime_ms;
> public Vector BugItem;
> public Vector StaticTextItem;
> public Vector DynamicTextItem;
>
> public ScreenDocument()
> {
> BugItem = new Vector();
> StaticTextItem = new Vector();
> DynamicTextItem = new Vector();
> }
>
> public String getScreenID() { return ScreenID; }
> public void setScreenID( String rhs ) { this.ScreenID = rhs; }
> public String getLifetime_ms() { return Lifetime_ms; }
> public void setLifetime_ms( String rhs ) { this.Lifetime_ms = rhs; }
> ...
If I understand your problem, your issue is that this works when
property Lifetime_ms is a String, but you want it to be an int or
Integer?
Well, this should just work. Digester uses BeanUtils to access
properties of a Java object, and this automatically does type
conversions. From inspection of the target ScreenDocument class,
BeanUtils should see that there is a setScreenID(int rhs) method, and so
automatically convert String->int before invoking that method. If the
getter/setter deal in Integer rather than int, then that should work
too.
A few suggestions:
(a) Did you change the prototype of the getScreenID method to return the
same datatype as the setScreenID takes as a parameter? This is required
by the JavaBean standard...
(b) If you're still having problems then you can try enabling debugging
in Digester. See http://wiki.apache.org/jakarta-commons/Digester/FAQ
section 1.4.
>
> The problem that I am encountering is that if I try to use
> bean-property-setter-rule like this:
>
> <?xml version="1.0"?>
>
> <digester-rules>
> <pattern value="Screen">
> <object-create-rule classname="com.synergy.screendoc.ScreenDocument"
> />
> <set-properties-rule/>
> <bean-property-setter-rule pattern="ScreenID"/>
> <bean-property-setter-rule pattern="Lifetime_ms"/>
>
> I get the Digester exception that the bean has no property ScreenID.
>
> Having a method that works I would normally just blow this off, but I don't
> really want Lifetime_ms to be a String, I want it to be an "int". I would
> be happy with it being a java.lang.Integer, but when I do something like
> this:
>
> <?xml version="1.0"?>
>
> <digester-rules>
> <pattern value="Screen">
> <object-create-rule classname="com.synergy.screendoc.ScreenDocument"
> />
> <call-method-rule pattern="ScreenID" methodname="setScreenID"
> paramcount="0" />
> <pattern value="Lifetime_ms">
> <call-method-rule methodname="setLifetime_ms" paramcount="1"/>
> <object-param-rule paramnumber='0' type="java.lang.Integer" />
> </pattern>
> ...
>
> And change the bean to have Lifetime_ms defined, set, and returned as type
> Integer, Digester throws and exception and complains that setLifetime_ms is
> not defined in my class.
>
> I am at a loss for understanding why I am having this problem and suspect I
> am doing something really dumb.
>
> Can anyone please set me staight?
I'm puzzled as well. It should all work. The only thing that I can think
of is that you failed to change the getter method [as described in (a)
above].
If this isn't the case, then please enable debugging and have a look at
the output. If this still doesn't help, then post the debug output to
this list and maybe I (or someone else) can help further.
Regards,
Simon
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org