jackrabbit-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jukka Zitting" <jukka.zitt...@gmail.com>
Subject Re: Finding out if node is a last child of a parent
Date Wed, 08 Aug 2007 09:42:24 GMT

On 8/8/07, Amir Mistric <amistric@nemours.org> wrote:
> - What is the *best* way to find out if a child node is the last child of its
> parent (based on certain node type)?
> - Is there an API that already does this or would I have to create my
> own method.
> - If I have to create my own method, what would you recommend?
> Do an Xpath query using last() or get a count of children and compare
> last child node with passed in one?

JCR doesn't have methods like Node.getNext() or Node.getPrevious()
that would allow you to do sideways traversal.

I guess the best way to achieve your goal would be:

    Node node = ...
    NodeIterator iterator = node.getParent().getNodes();
    while (iterator.hasNext()) {
        if (node.isSame(iterator.nextNode())) {
            if (iterator.hasNext()) {
                return /* this is not the last sibling */;
            } else {
                return /* this is the last sibling */;

This approach is obviously not too efficient, but can be used to
implement your use case as well as the "next"/"previous" links
discussed in another post a while ago.

Perhaps we should ask for JSR 283 to consider adding support for
sibling access methods?


Jukka Zitting

View raw message