You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Chris Cheshire <ch...@gmail.com> on 2006/09/13 03:01:07 UTC

[digester] FactoryCreateRule help

I have to process an XML file that contains (in part) the following structure
<variables>
  <Variable>
    <ID>...</ID>
    <Name>...</Name>
    <Instance>...</Instance>
    <Value>...</Value>
  </Variable>
</variables>

I have a predefined list of variables based upon their ID, an abstract
Variable class, and then concrete subclasses of these for each valid
ID.

Variable contains a static method Variable getInstance(int ID) that
creates a new variable subclass based upon the ID (the id-subclass
mapping is based upon a properties file).

The FactoryCreateRule allows for creation of classes based via a
factory providing the identifiying parameter is an XML attribute. In
this case it is not. It is a sub-element, I have no control over that.

How can I use the FactoryCreateRule to call getInstance(int ID) in my
Variable class when I need to extract the ID from a nested element not
an attribute?

Thanks

Chris

PS I did explore other options for the Variable class where there is
just one class, but there is a need for polymorphism with regards to
the variables that have led me to implement it this way, so changing
Variable is also not an option.

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [digester] FactoryCreateRule help

Posted by Simon Kitching <sk...@apache.org>.
Hi Chris,

On Tue, 2006-09-12 at 18:01 -0700, Chris Cheshire wrote:
> I have to process an XML file that contains (in part) the following structure
> <variables>
>   <Variable>
>     <ID>...</ID>
>     <Name>...</Name>
>     <Instance>...</Instance>
>     <Value>...</Value>
>   </Variable>
> </variables>
> 
> I have a predefined list of variables based upon their ID, an abstract
> Variable class, and then concrete subclasses of these for each valid
> ID.
> 
> Variable contains a static method Variable getInstance(int ID) that
> creates a new variable subclass based upon the ID (the id-subclass
> mapping is based upon a properties file).
> 
> The FactoryCreateRule allows for creation of classes based via a
> factory providing the identifiying parameter is an XML attribute. In
> this case it is not. It is a sub-element, I have no control over that.
> 
> How can I use the FactoryCreateRule to call getInstance(int ID) in my
> Variable class when I need to extract the ID from a nested element not
> an attribute?

This is quite a common question, and really deserves an FAQ entry.

In short, you can't. Digester is a SAX-based system, and with SAX there
is no "look ahead" option.

One possible solution is to use an XSLT stylesheet to pre-process your
xml to move all the data needed to determine the object type into
attributes of the element that triggers the object creation. Then the
FactoryCreateRule can be used as normal.

It might be possible to create a variant of the FactoryCreateRule that
creates a hashmap or similar when the start element is found, store
child objects in that hashmap until the end element is found, then looks
into the hashmap to get the data to determine the type of object to
instantiate and copy the hashmap contents into properties of the created
object. It all seems rather clumsy/fragile though. If you do wish to try
this, and get it to work then please put that info in the digester wiki
as you're not the first person to ask about this.

Regards,

Simon



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org