[digester] delegate the parsing of some nodes to different parsers

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[digester] delegate the parsing of some nodes to different parsers

Valerio
Hello,
suppose you have something like:
<root>
  <foo>
     <bar>
     .....
     </bar>
  </foo>
</root>

I already have the digester code to istantiate a Bar object, if given as
parameter to BarParser.read(String s) i pass as
actual parameter everything between <bar> and </bar>.

Now i want to have a RootParser, so that if i pass it everything between
root tags, whenever it encounter any bar tags,
it delegates the parsing of that portion of the xml document to BarParser.

Is it possible ?

thanks,
valerio
--
To Iterate is Human, to Recurse, Divine
James O. Coplien, Bell Labs
(how good is to be human indeed)
Reply | Threaded
Open this post in threaded view
|

Re: [digester] delegate the parsing of some nodes to different parsers

Simon Kitching
On Wed, 2006-01-25 at 15:19 +0100, Valerio Schiavoni wrote:

> Hello,
> suppose you have something like:
> <root>
>   <foo>
>      <bar>
>      .....
>      </bar>
>   </foo>
> </root>
>
> I already have the digester code to istantiate a Bar object, if given as
> parameter to BarParser.read(String s) i pass as
> actual parameter everything between <bar> and </bar>.
>
> Now i want to have a RootParser, so that if i pass it everything between
> root tags, whenever it encounter any bar tags,
> it delegates the parsing of that portion of the xml document to BarParser.
>
> Is it possible ?

I guess it's possible though I don't know of anyone that has tried this
before.

You effectively need to get a reference to the xml parser that is
feeding events to the RootParser, and instead tell it to feed events to
the BarParser. In addition, I presume you'd need to grab the top object
off the RootParser's object stack and push it onto the BarParser's stack
so that the two "trees" of objects can be connected. At the end of the
bar element, you need to perform the reverse process. This sounds like
it is doable using a custom Rule class.

I don't know quite what you are trying to achieve with this. If the
intention is to be able to apply independent sets of rules for different
parts of the tree you might like to look at the "plugins" module of
Digester which implements that sort of functionality. Hopefully the
package javadoc describes its purpose adequately:
http://jakarta.apache.org/commons/digester/commons-digester-1.7/docs/api/org/apache/commons/digester/plugins/package-summary.html

Regards,

Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]