Question about TinyTreeWalker

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

Question about TinyTreeWalker

Pier Fumagalli-2
I am using the internal TinyTreeWalker part of Saxon-B. What I wanted  
to obtain is a simple reference to the node in the tree while I walk it:

For example, while walking the tree I see a specific element (in my  
case called "repeat"), which I want to remember about. For example  
given this document:

<document>
   <repeat number="5">
     Hello world
   </repeat>
</document>

My code would walk the tree, find out the "repeat" element, and  
somehow remember it.

At the end of the walk, I could get a hold of all the "repeat"  
elements I encountered and walk them again, doing whatever I need to  
do with them (five times in this case).

Problem I see with the walker is that it never "locates" itself: I  
can only walk the tree.

What I've done is that I've patched the source a little bit, and  
added this function in TinyTreeWalker:

     public int currentNode() {
         return currentNode;
     }

Basically, when I spot the element I want, I call "currentNode()" and  
remember the position.

When I'm done, I simply call TinyTree.getNode(xxx) (<- with the node  
number I've saved), create a new node, and walk it again (and again,  
and again, my five times).

This seems to be working perfectly, but I was wondering if there was  
a reason why such a method was not already inside Saxon (or what I  
did overlook, as I didn't find it).

The same can be said for TinyNodeImpl, I wasn't able to find a way to  
return the internal node number, something that I can store and then  
use again in TinyTree.getNode(int).

(Note, I prefer "int" to "TinyNodeImpl" because I don't have objects  
lying around and wasting memory for nothing)

Can anyone enlighten me please? Thanks for helping.

        Pier


smime.p7s (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

RE: Question about TinyTreeWalker

Michael Kay
The reason that TinyTreeWalker doesn't support the functionality you are
looking for is that it was designed solely as a "fast path" implementation
of the more general TreeWalker class, which is capable of walking any tree.
It was therefore designed to be used by clients who don't know anything
about the internals of the TinyTree and that have no use for methods that
work only for a TinyTree. The TreeWalker itself implements the PullProvider
interface which is modelled on Stax: that is, it's a pull-based streaming
API designed for use by applications that want serial access to the document
rather than direct access. If you want direct access I would suggest using
the iterateAxis() method to navigate the tree using the XPath axes.

The same applies to the absence of a method to get the node number from a
TinyNodeImpl: the TinyTree is an implementation of the tree model, and
clients are expected to be able to work with any tree model rather than
using access methods specific to the one implementation.

I think there are good software engineering reasons for encapsulating the
implementation details of the TinyTree, and you'll need to give me a very
convincing case if you want me to expose more of its internals.

Regards,

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of
> Pier Fumagalli
> Sent: 24 November 2005 18:22
> To: [hidden email]
> Subject: [saxon] Question about TinyTreeWalker
>
> I am using the internal TinyTreeWalker part of Saxon-B. What
> I wanted  
> to obtain is a simple reference to the node in the tree while
> I walk it:
>
> For example, while walking the tree I see a specific element (in my  
> case called "repeat"), which I want to remember about. For example  
> given this document:
>
> <document>
>    <repeat number="5">
>      Hello world
>    </repeat>
> </document>
>
> My code would walk the tree, find out the "repeat" element, and  
> somehow remember it.
>
> At the end of the walk, I could get a hold of all the "repeat"  
> elements I encountered and walk them again, doing whatever I need to  
> do with them (five times in this case).
>
> Problem I see with the walker is that it never "locates" itself: I  
> can only walk the tree.
>
> What I've done is that I've patched the source a little bit, and  
> added this function in TinyTreeWalker:
>
>      public int currentNode() {
>          return currentNode;
>      }
>
> Basically, when I spot the element I want, I call
> "currentNode()" and  
> remember the position.
>
> When I'm done, I simply call TinyTree.getNode(xxx) (<- with the node  
> number I've saved), create a new node, and walk it again (and again,  
> and again, my five times).
>
> This seems to be working perfectly, but I was wondering if there was  
> a reason why such a method was not already inside Saxon (or what I  
> did overlook, as I didn't find it).
>
> The same can be said for TinyNodeImpl, I wasn't able to find
> a way to  
> return the internal node number, something that I can store and then  
> use again in TinyTree.getNode(int).
>
> (Note, I prefer "int" to "TinyNodeImpl" because I don't have objects  
> lying around and wasting memory for nothing)
>
> Can anyone enlighten me please? Thanks for helping.
>
> Pier
>
>




-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
saxon-help mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help