/*
 * Copyright (C) 2008 TranceCode Software
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 *
 * $Id$
 */
package org.trancecode.xml.saxon;

import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import net.sf.saxon.s9api.Axis;
import net.sf.saxon.s9api.XdmItem;
import net.sf.saxon.s9api.XdmNode;
import org.trancecode.collection.TubularIterables;

/**
 * Utility methods related to {@link Iterable} and Saxon.
 * 
 * @author Herve Quiroz
 */
public final class SaxonIterables
{
    private SaxonIterables()
    {
        // No instantiation
    }

    public static Iterable<XdmItem> childXdmItems(final XdmNode node)
    {
        return Iterables.concat(TubularIterables.newIterable(SaxonSuppliers.axisIterator(node, Axis.ATTRIBUTE)),
                TubularIterables.newIterable(SaxonSuppliers.axisIterator(node, Axis.CHILD)));
    }

    public static Iterable<XdmNode> childNodes(final XdmNode node)
    {
        return Iterables.filter(childXdmItems(node), XdmNode.class);
    }

    public static XdmNode childElement(final XdmNode node)
    {
        return Iterables.getOnlyElement(childElements(node));
    }

    public static Iterable<XdmNode> childElements(final XdmNode node)
    {
        return Iterables.filter(childNodes(node), SaxonPredicates.isElement());
    }

    public static Iterable<XdmNode> childNodesNoAttributes(final XdmNode node)
    {
        return Iterables.filter(childNodes(node), Predicates.not(SaxonPredicates.isAttribute()));
    }

    public static Iterable<XdmNode> attributes(final XdmNode node)
    {
        return Iterables.filter(childNodes(node), SaxonPredicates.isAttribute());
    }

    public static Iterable<XdmNode> namespaces(final XdmNode node)
    {
        return Iterables.filter(TubularIterables.newIterable(SaxonSuppliers.axisIterator(node, Axis.NAMESPACE)),
                XdmNode.class);
    }
}

@@ -909,24 +909,53 @@
 .Iterables;%0A
+%0Aimport java.util.Iterator;%0A%0A
 import net.s
@@ -1047,59 +1047,8 @@
 ode;
-%0Aimport org.trancecode.collection.TubularIterables;
 %0A%0A/*
@@ -1281,28 +1281,19 @@
 dmItem%3E 
-childXdmItem
+axi
 s(final 
@@ -1296,32 +1296,49 @@
 nal XdmNode node
+, final Axis axis
 )%0A    %7B%0A        
@@ -1344,24 +1344,28 @@
  return 
+new 
 Iterable
 s.concat
@@ -1360,97 +1360,161 @@
 able
-s.concat(TubularIterables.newIterable(SaxonSuppliers.axisIterator(node, Axis.ATTRIBUTE)),
+%3CXdmItem%3E()%0A        %7B%0A            @Override%0A            public Iterator%3CXdmItem%3E iterator()%0A            %7B%0A                return node.axisIterator(axis);
 %0A   
@@ -1524,70 +1524,170 @@
     
+  %7D%0A
       
-TubularIterables.newIterable(SaxonSuppliers.axisIterator
+  %7D;%0A    %7D%0A%0A    public static Iterable%3CXdmItem%3E childXdmItems(final XdmNode node)%0A    %7B%0A        return Iterables.concat(axis(node, Axis.ATTRIBUTE), axis
 (nod
@@ -1701,17 +1701,16 @@
 .CHILD))
-)
 ;%0A    %7D%0A
@@ -2617,64 +2617,12 @@
 ter(
-TubularIterables.newIterable(SaxonSuppliers.axisIterator
+axis
 (nod
@@ -2643,26 +2643,9 @@
 ACE)
-),%0A               
+,
  Xdm

Add SaxonIterables.axis()