You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Greg McCreath <gr...@intellect.be> on 2002/03/21 17:16:21 UTC

FactoryCreateRule Example?

Can anyone point me to where I might might find one of the above.

I want to have an object created using Digester, but the object will have
two non no-argument constructors, and I want both of them to be recognised
from the XML. As an example of what I want  ...

< commsMessageFormatter class="MyClass" />

or 

< commsMessageFormatter class="MyClass" protocolType="ISOxxxx" />

or

< commsMessageFormatter class="MyClass" protocolType="ISOxxxx"
protocolVersion="1.0" />

All help appreciated ...

Greg.

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: FactoryCreateRule Example?

Posted by robert burrell donkin <ro...@mac.com>.
On Thursday, March 21, 2002, at 04:16 PM, Greg McCreath wrote:

hi greg

> Can anyone point me to where I might might find one of the above.

you can find what documentation there is in the 
org.apache.commons.digester package java doc comments.

(documentation patches will be gratefully received.)

> I want to have an object created using Digester, but the object will have
> two non no-argument constructors, and I want both of them to be recognised
> from the XML. As an example of what I want  ...
>
> < commsMessageFormatter class="MyClass" />
>
> or
>
> < commsMessageFormatter class="MyClass" protocolType="ISOxxxx" />
>
> or
>
> < commsMessageFormatter class="MyClass" protocolType="ISOxxxx"
> protocolVersion="1.0" />

(i'm going to assume that you're happy(/ier) with the pattern matching 
rules and talk about using FactoryCreateRule only.)

in order to use a FactoryCreateRule you need to make a 
ObjectCreationFactory implementation. you'll probably want to extend 
AbstractObjectCreationFactory and just override the createObject method.

createObject has the element's attributes passed in. use these to decide 
which constructor to call.

i quite often do is use an anonymous class so it'd probably go something 
like:

digester.addFactoryCreate(
     "/root/alpha",
     new AbstractObjectCreationFactory() {
          public Object createObject(Attributes attributes) {
              if (attributes.getLength() == 1) {
                  // return object created with single parameter constructor
                  ...
              }

              if (attributes.getLength() == 2) {
                  // return object created with double parameter constructor
                  ...
              }

              ...
          }
     }
}

hope that's enough to get you started.

- robert


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>