On Wed, 2004-01-28 at 10:47, Samuel Cheung wrote:
> Thanks Robert.
>
> But if I have 50 static attributes in my Globals, do I need to create 1
> custom Rule per attribute?
Hmm.. I think there is a problem with Robert's suggestion (sorry, Robert
:-)
You are using a separate xml element for each value, with the body
holding the value to use (not an attribute), right?
eg:
<globals>
<x>1</attr_x>
<y>2</attr_y>
</globals>
I believe Robert assumed you were using attributes:
<globals>
<x value="1"/>
<y value="1"/>
</globals>
I think you can still use Robert's approach, but your custom rule needs
to override method "body" rather than "begin":
public void body(namespace, name, text) {
if name.equals("x") then Globals.x = text
else if name.equals("y") then Globals.y = text
etc
}
Or you could get more clever and use reflection to assign to the
corresponding attribute or method in Globals. This would automatically
"adapt" to changes in the Globals class, at the price of some additional
complexity.
Hope this helps,
Simon
>
>
> -----Original Message-----
> From: robert burrell donkin
> [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, January 27, 2004 3:43 PM
> To: Jakarta Commons Users List
> Subject: Re: [digester] Set attribute in a static class
>
>
> hi samual
>
> i'd recommend using a custom Rule implementation for something like
> this.
>
> for example, given a class like (say)
>
> public class Globals
> {
> public static String SOME_GLOBAL_SETTING;
> }
>
> a custom rule to set it from the 'some' attribute would be something
> like this:
>
> public SetGlobalsRule extends Rule {
>
> public void begin(String namespace, String name, Attributes
> attributes) {
>
> Globals.SOME_GLOBAL_SETTING = attribute.getValue("some");
> }
> }
>
> - robert
>
> On 27 Jan 2004, at 16:09, Samuel Cheung wrote:
>
> > Hi,
> >
> > I would like to use Digester to set attribute in my static class.
> > So I create my own ObjectCreationFactory (child class of
> > AbstractObjectCreationFactory) to return the static object.
> >
> > But when I set the attribute of the Static object by using
> > addCallParam(), I
> > got exception:
> > Jan 27, 2004 12:32:49 AM org.apache.commons.digester.Digester
> > peekParams
> > WARNING: Empty stack (returning null)
> > Jan 27, 2004 12:32:49 AM org.apache.commons.digester.Digester
> > endElement
> > SEVERE: End event threw exception
> > java.lang.NullPointerException
> > at
> > org.apache.commons.digester.CallParamRule.end(CallParamRule.java:288)
> > at org.apache.commons.digester.Digester.endElement(Digester.java:1058)
> > at org.apache.crimson.parser.Parser2.maybeElement(Unknown Source)
> > at org.apache.crimson.parser.Parser2.content(Unknown Source)
> > at org.apache.crimson.parser.Parser2.maybeElement(Unknown Source)
> > at org.apache.crimson.parser.Parser2.content(Unknown Source)
> > at org.apache.crimson.parser.Parser2.maybeElement(Unknown Source)
> > at org.apache.crimson.parser.Parser2.parseInternal(Unknown Source)
> >
> > Here is my code to load the config:
> >
> > public void loadConfig(File configFile) throws IOException,
> > SAXException
> > {
> > Digester digester = new Digester();
> > digester.addFactoryCreate("sysconfig",
> > sysconfigObjCreationFactory);
> >
> > digester.addFactoryCreate("sysconfig/comp1",
> > comp1configObjCreationFactory);
> >
> > digester.addCallParam( "sysconfig/comp1/parameter1", 0 );
> >
> > SysConfig sysConfig = (SysConfig) digester.parse(configFile);
> > }
> >
> > I appreciate if anyone can help me on this issue.
> >
> > Thanks in advance.
> >
> > Sam
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]