On Wed, 2003-01-15 at 06:15, Craig R. McClanahan wrote:
> On Tue, 14 Jan 2003, Justin Ashworth wrote:
>
> > Date: Tue, 14 Jan 2003 08:50:14 -0500
> > From: Justin Ashworth <[EMAIL PROTECTED]>
> > Reply-To: Jakarta Commons Users List <[EMAIL PROTECTED]>
> > To: [EMAIL PROTECTED]
> > Subject: [Digester] Passing arguments to constructors
> >
> > Hi,
> >
> > I just started using Digester yesterday and it has already saved me a lot of
> > time. However, there is one thing which I haven't been able to figure out
> > how to do that is making up for the time I saved. I have a condition under
> > which a certain object should not be created from the XML. I am reading in
> > menu contents from an XML file and I want to make the menu dynamic based on
> > the user's permissions, known only at runtime. I could easily do this if I
> > could just pass the permissions into the constructor when a new object is
> > created or, alternatively, when setNext is called. That way I can determine
> > at runtime whether or not the object should be saved or ignored, based on
> > whether or not the user has appropriate permissions. Right now what I have
> > to do is create the entire object then parse through it and prune the
> > branches that shouldn't exist, which as you could imagine takes a lot of
> > time. Are there any provisions for doing what I'm trying to do with
> > Digester? If not, does anybody else think this would be worthwhile enough
> > that I could implement the feature in Digester?
> >
>
> Check out the ObjectCreationFactory rule. Instead of specifying the class
> of the object you're trying to create, you specify the class of a Factory
> object -- which can do anything necessary to create the instance of the
> actual object (including calling a constructor with parameters, or calling
> some other object factory, or ...).
>
> > Thanks much,
> >
> > Justin
>
> Craig
>
Hi Justin,
Just thought it mught be helpful to add a little pseudocode to Craig's
response. In your code which sets up your digester parsing rules, do:
XYZFactory factory = new XYZFactory(param1, param2);
digester.addFactoryCreate("/foo/bar", factory);
Your factory class constructor stores the parameters away for use later.
When node /foo/bar is encountered in the input xml, the factory OBJECT's
public Object createObject(org.xml.sax.Attributes attributes)
method is called. This method should create an instance of your class
passing any required constructor parameters, and return it.
Regards,
Simon
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>