//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Copyright (c) 2013 Saxonica Limited. // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// package net.sf.saxon.option.xom; import net.sf.saxon.Configuration; import net.sf.saxon.event.Receiver; import net.sf.saxon.om.AtomicSequence; import net.sf.saxon.om.DocumentInfo; import net.sf.saxon.om.NamePool; import net.sf.saxon.om.NamespaceBinding; import net.sf.saxon.om.NodeInfo; import net.sf.saxon.om.Sequence; import net.sf.saxon.om.SequenceIterator; import net.sf.saxon.pattern.AnyNodeTest; import net.sf.saxon.pattern.NameTest; import net.sf.saxon.pattern.NodeKindTest; import net.sf.saxon.pattern.NodeTest; import net.sf.saxon.trans.XPathException; import net.sf.saxon.tree.iter.AxisIterator; import net.sf.saxon.tree.iter.EmptyAxisIterator; import net.sf.saxon.tree.iter.SingletonIterator; import net.sf.saxon.tree.util.FastStringBuffer; import net.sf.saxon.tree.util.Navigator; import net.sf.saxon.tree.util.SteppingNavigator; import net.sf.saxon.tree.util.SteppingNode; import net.sf.saxon.tree.wrapper.AbstractNodeWrapper; import net.sf.saxon.tree.wrapper.SiblingCountingNode; import net.sf.saxon.tree.wrapper.VirtualNode; import net.sf.saxon.type.BuiltInAtomicType; import net.sf.saxon.type.SchemaType; import net.sf.saxon.type.Type; import net.sf.saxon.type.Untyped; import net.sf.saxon.value.StringValue; import net.sf.saxon.value.UntypedAtomicValue; import nu.xom.Attribute; import nu.xom.Comment; import nu.xom.DocType; import nu.xom.Document; import nu.xom.Element; import nu.xom.Node; import nu.xom.ParentNode; import nu.xom.ProcessingInstruction; import nu.xom.Text; /** * A node in the XML parse tree representing an XML element, character content, * or attribute. *
* This is the implementation of the NodeInfo interface used as a wrapper for * XOM nodes. * * @author Michael H. Kay * @author Wolfgang Hoschek (ported net.sf.saxon.jdom to XOM) */ public class XOMNodeWrapper extends AbstractNodeWrapper implements VirtualNode, SiblingCountingNode, SteppingNode { protected Node node; protected short nodeKind; private XOMNodeWrapper parent; // null means unknown protected XOMDocumentWrapper docWrapper; //represents the index position in it's parent child nodes protected int index; // -1 means unknown /** * This constructor is protected: nodes should be created using the wrap * factory method on the XOMDocumentWrapper class * * @param node The XOM node to be wrapped * @param parent The XOMNodeWrapper that wraps the parent of this node * @param index Position of this node among its siblings */ protected XOMNodeWrapper(Node node, XOMNodeWrapper parent, int index) { short kind; if (node instanceof Element) { kind = Type.ELEMENT; } else if (node instanceof Text) { kind = Type.TEXT; } else if (node instanceof Attribute) { kind = Type.ATTRIBUTE; } else if (node instanceof Comment) { kind = Type.COMMENT; } else if (node instanceof ProcessingInstruction) { kind = Type.PROCESSING_INSTRUCTION; } else if (node instanceof Document) { kind = Type.DOCUMENT; } else { throwIllegalNode(node); // moved out of fast path to enable better inlining return; // keep compiler happy } nodeKind = kind; this.node = node; this.parent = parent; this.index = index; } /** * Factory method to wrap a XOM node with a wrapper that implements the * Saxon NodeInfo interface. * * @param node The XOM node * @param docWrapper The wrapper for the Document containing this node * @return The new wrapper for the supplied node */ protected final XOMNodeWrapper makeWrapper(Node node, XOMDocumentWrapper docWrapper) { return makeWrapper(node, docWrapper, null, -1); } /** * Factory method to wrap a XOM node with a wrapper that implements the * Saxon NodeInfo interface. * * @param node The XOM node * @param docWrapper The wrapper for the Document containing this node * @param parent The wrapper for the parent of the XOM node * @param index The position of this node relative to its siblings * @return The new wrapper for the supplied node */ protected final XOMNodeWrapper makeWrapper(Node node, XOMDocumentWrapper docWrapper, XOMNodeWrapper parent, int index) { if (node == docWrapper.node) return docWrapper; XOMNodeWrapper wrapper = new XOMNodeWrapper(node, parent, index); wrapper.docWrapper = docWrapper; return wrapper; } private static void throwIllegalNode(/*@Nullable*/ Node node) { String str = node == null ? "NULL" : node.getClass() + " instance " + node.toString(); throw new IllegalArgumentException("Bad node type in XOM! " + str); } /** * To implement {@link Sequence}, this method returns a singleton iterator * that delivers this item in the form of a sequence * * @return a singleton iterator that returns this item */ public SequenceIterator iterate() { return SingletonIterator.makeIterator(this); } /** * Get the configuration */ public Configuration getConfiguration() { return docWrapper.getConfiguration(); } /** * Get the underlying XOM node, to implement the VirtualNode interface */ public Object getUnderlyingNode() { return node; } /** * Get the name pool for this node * * @return the NamePool */ public NamePool getNamePool() { return docWrapper.getNamePool(); } /** * Return the type of node. * * @return one of the values Node.ELEMENT, Node.TEXT, Node.ATTRIBUTE, etc. */ public int getNodeKind() { return nodeKind; } /** * Get the typed value. * * @return the typed value. If requireSingleton is set to true, the result * will always be an AtomicValue. In other cases it may be a Value * representing a sequence whose items are atomic values. * @since 8.5 */ public AtomicSequence atomize() { switch (getNodeKind()) { case Type.COMMENT: case Type.PROCESSING_INSTRUCTION: return new StringValue(getStringValueCS()); default: return new UntypedAtomicValue(getStringValueCS()); } } /** * Get the type annotation */ public int getTypeAnnotation() { SchemaType st = getSchemaType(); return (st == null ? -1 : st.getFingerprint()); } /** * Get the type annotation of this node, if any. The type annotation is represented as * SchemaType object. * *Types derived from a DTD are not reflected in the result of this method.
* * @return For element and attribute nodes: the type annotation derived from schema * validation (defaulting to xs:untyped and xs:untypedAtomic in the absence of schema * validation). For comments, text nodes, processing instructions, and namespaces: null. * For document nodes, either xs:untyped if the document has not been validated, or * xs:anyType if it has. * @since 9.4 */ public SchemaType getSchemaType() { if (getNodeKind() == Type.ATTRIBUTE) { return BuiltInAtomicType.UNTYPED_ATOMIC; } else { return Untyped.getInstance(); } } /** * Determine whether this is the same node as another node.For a node other than an element, the method returns null.
*/ public NamespaceBinding[] getDeclaredNamespaces(NamespaceBinding[] buffer) { if (node instanceof Element) { Element elem = (Element) node; int size = elem.getNamespaceDeclarationCount(); if (size == 0) { return NamespaceBinding.EMPTY_ARRAY; } NamespaceBinding[] result = (buffer == null || size > buffer.length ? new NamespaceBinding[size] : buffer); for (int i = 0; i < size; i++) { String prefix = elem.getNamespacePrefix(i); String uri = elem.getNamespaceURI(prefix); result[i] = new NamespaceBinding(prefix, uri); } if (size < result.length) { result[size] = null; } return result; } else { return null; } } /** * Determine whether this node has the is-id property * * @return true if the node is an ID */ public boolean isId() { return getNodeKind() == Type.ATTRIBUTE && ((Attribute) node).getType() == Attribute.Type.ID; } /** * Determine whether this node has the is-idref property * * @return true if the node is an IDREF or IDREFS element or attribute */ public boolean isIdref() { return getNodeKind() == Type.ATTRIBUTE && ( ((Attribute) node).getType() == Attribute.Type.IDREF || ((Attribute) node).getType() == Attribute.Type.IDREFS); } /** * Determine whether the node has the is-nilled property * * @return true if the node has the is-nilled property */ public boolean isNilled() { return false; } /////////////////////////////////////////////////////////////////////////////// // Methods to support update access /////////////////////////////////////////////////////////////////////////////// /** * Delete this node (that is, detach it from its parent) */ public void delete() throws XPathException { if (parent != null) { if (nodeKind == Type.ATTRIBUTE) { ((Element) parent.node).removeAttribute((Attribute) node); } else { ((ParentNode) parent.node).removeChild(node); } } } /** * Insert a sequence of nodes as the first children of the target node * @param content the nodes to be inserted. */ // public void insertAsFirst(SequenceIterator content) throws XPathException { // if (!(node instanceof ParentNode)) { // throw new XPathException("Cannot insert children unless parent is an element or document node"); // } // int i = 0; // while (true) { // NodeInfo next = (NodeInfo)content.next(); // if (next == null) { // break; // } // if (next instanceof XOMNodeWrapper) { // Node nextNode = ((XOMNodeWrapper)next).node; // ParentNode existingParent = nextNode.getParent(); // if (existingParent != null) { // existingParent.removeChild(nextNode); // } // ((ParentNode)node).insertChild(nextNode, i++); // } else { // throw new XPathException("Cannot insert non-XOM node"); // } // } // } // // /** // * Insert a sequence of nodes as the last children of the target node // * @param content the nodes to be inserted. // */ // // public void insertAsLast(SequenceIterator content) throws XPathException { // if (!(node instanceof ParentNode)) { // throw new XPathException("Cannot insert children unless parent is an element or document node"); // } // while (true) { // NodeInfo next = (NodeInfo)content.next(); // if (next == null) { // break; // } // if (next instanceof XOMNodeWrapper) { // Node nextNode = ((XOMNodeWrapper)next).node; // ParentNode existingParent = nextNode.getParent(); // if (existingParent != null) { // existingParent.removeChild(nextNode); // } // ((ParentNode)node).appendChild(nextNode); // } else { // throw new XPathException("Cannot insert non-XOM node"); // } // } // } // // // /** // * Add attributes to this node // * // * @param content the attributes to be added // */ // // public void insertAttributes(SequenceIterator content) throws XPathException { // if (nodeKind == Type.ELEMENT) { // while (true) { // NodeInfo next = (NodeInfo)content.next(); // if (next == null) { // break; // } // if (next.getNodeKind() != Type.ATTRIBUTE) { // throw new XPathException("Node to be inserted is not an attribute"); // } // if (next instanceof XOMNodeWrapper) { // Node node = ((XOMNodeWrapper)next).node; // if (node.getParent() != null) { // node = node.copy(); // } // ((Element)node).addAttribute((Attribute)node); // } else { // throw new XPathException("Cannot insert non-XOM node"); // } // } // } else { // throw new XPathException("Cannot insert attributes unless parent is an element node"); // } // } // // /** // * Rename this node // * // * @param newName the new name // */ // // public void rename(StructuredQName newName) throws XPathException { // if (node instanceof Element) { // ((Element)node).setNamespaceURI(newName.getNamespaceURI()); // ((Element)node).setLocalName(newName.getLocalName()); // ((Element)node).setNamespacePrefix(newName.getPrefix()); // } else if (node instanceof Attribute) { // ((Attribute)node).setNamespace(newName.getPrefix(), newName.getNamespaceURI()); // ((Attribute)node).setLocalName(newName.getLocalName()); // } // } /** * Replace this node with a given sequence of nodes * * @param replacement the replacement nodes */ // public void replace(SequenceIterator replacement) throws XPathException { // XOMNodeWrapper parentNode = ((XOMNodeWrapper)) // if (getPar) { // throw new XPathException("Cannot replace node unless parent is an element or document node"); // } // while (true) { // NodeInfo next = (NodeInfo)content.next(); // if (next == null) { // break; // } // if (next instanceof XOMNodeWrapper) { // Node nextNode = ((XOMNodeWrapper)next).node; // ParentNode existingParent = nextNode.getParent(); // if (existingParent != null) { // existingParent.removeChild(nextNode); // } // ((ParentNode)node).appendChild(nextNode); // } else { // throw new XPathException("Cannot insert non-XOM node"); // } // } // } /** * Replace the string-value of this node * * @param stringValue the new string value */ // public void replaceStringValue(CharSequence stringValue) throws XPathException { // switch (nodeKind) { // case Type.ATTRIBUTE: // ((Attribute)node).setValue(stringValue.toString()); // case Type. // } // } /////////////////////////////////////////////////////////////////////////////// // Axis enumeration classes /////////////////////////////////////////////////////////////////////////////// /** * Handles the ancestor axis in a rather direct manner. */ private final class AncestorAxisIterator implements AxisIterator { private XOMNodeWrapper start; private boolean includeSelf; private NodeInfo current; private NodeTest nodeTest; private int position; public AncestorAxisIterator(XOMNodeWrapper start, boolean includeSelf, NodeTest test) { // use lazy instead of eager materialization (performance) this.start = start; if (test == AnyNodeTest.getInstance()) test = null; nodeTest = test; if (!includeSelf) { current = start; } this.includeSelf = includeSelf; position = 0; } /** * Move to the next node, without returning it. Returns true if there is * a next node, false if the end of the sequence has been reached. After * calling this method, the current node may be retrieved using the * current() function. */ public boolean moveNext() { return (next() != null); } public NodeInfo next() { NodeInfo curr; do { // until we find a match curr = advance(); } while (curr != null && nodeTest != null && (!nodeTest.matches(curr))); if (curr != null) position++; current = curr; return curr; } private NodeInfo advance() { if (current == null) current = start; else current = current.getParent(); return current; } public NodeInfo current() { return current; } public int position() { return position; } public void close() { } /** * Return an iterator over an axis, starting at the current node. * * @param axis the axis to iterate over, using a constant such as * {@link net.sf.saxon.om.AxisInfo#CHILD} * @param test a predicate to apply to the nodes before returning them. * @throws NullPointerException if there is no current node */ public AxisIterator iterateAxis(byte axis, NodeTest test) { return current.iterateAxis(axis, test); } /** * Return the atomized value of the current node. * * @return the atomized value. * @throws NullPointerException if there is no current node */ public Sequence atomize() throws XPathException { return current.atomize(); } /** * Return the string value of the current node. * * @return the string value, as an instance of CharSequence. * @throws NullPointerException if there is no current node */ public CharSequence getStringValue() { return current.getStringValue(); } /*@NotNull*/ public AxisIterator getAnother() { return new AncestorAxisIterator(start, includeSelf, nodeTest); } public int getProperties() { return 0; } } // end of class AncestorAxisIterator /** * Handles the attribute axis in a rather direct manner. */ private final class AttributeAxisIterator implements AxisIterator { private XOMNodeWrapper start; private NodeInfo current; private int cursor; private NodeTest nodeTest; private int position; public AttributeAxisIterator(XOMNodeWrapper start, NodeTest test) { // use lazy instead of eager materialization (performance) this.start = start; if (test == AnyNodeTest.getInstance()) test = null; nodeTest = test; position = 0; cursor = 0; } /** * Move to the next node, without returning it. Returns true if there is * a next node, false if the end of the sequence has been reached. After * calling this method, the current node may be retrieved using the * current() function. */ public boolean moveNext() { return (next() != null); } public NodeInfo next() { NodeInfo curr; do { // until we find a match curr = advance(); } while (curr != null && nodeTest != null && (!nodeTest.matches(curr))); if (curr != null) position++; current = curr; return curr; } private NodeInfo advance() { Element elem = (Element) start.node; if (cursor == elem.getAttributeCount()) return null; NodeInfo curr = makeWrapper(elem.getAttribute(cursor), docWrapper, start, cursor); cursor++; return curr; } public NodeInfo current() { return current; } public int position() { return position; } public void close() { } /** * Return an iterator over an axis, starting at the current node. * * @param axis the axis to iterate over, using a constant such as * {@link net.sf.saxon.om.AxisInfo#CHILD} * @param test a predicate to apply to the nodes before returning them. * @throws NullPointerException if there is no current node */ public AxisIterator iterateAxis(byte axis, NodeTest test) { return current.iterateAxis(axis, test); } /** * Return the atomized value of the current node. * * @return the atomized value. * @throws NullPointerException if there is no current node */ public Sequence atomize() throws XPathException { return current.atomize(); } /** * Return the string value of the current node. * * @return the string value, as an instance of CharSequence. * @throws NullPointerException if there is no current node */ public CharSequence getStringValue() { return current.getStringValue(); } /*@NotNull*/ public AxisIterator getAnother() { return new AttributeAxisIterator(start, nodeTest); } public int getProperties() { return 0; } } // end of class AttributeAxisIterator /** * The class ChildAxisIterator handles not only the child axis, but also the * following-sibling and preceding-sibling axes. It can also iterate the * children of the start node in reverse order, something that is needed to * support the preceding and preceding-or-ancestor axes (the latter being * used by xsl:number) */ private final class ChildAxisIterator implements AxisIterator { private XOMNodeWrapper start; private XOMNodeWrapper commonParent; private int ix; private boolean downwards; // iterate children of start node (not siblings) private boolean forwards; // iterate in document order (not reverse order) private NodeInfo current; private ParentNode par; private int cursor; private NodeTest nodeTest; private int position; private ChildAxisIterator(XOMNodeWrapper start, boolean downwards, boolean forwards, NodeTest test) { this.start = start; this.downwards = downwards; this.forwards = forwards; if (test == AnyNodeTest.getInstance()) test = null; nodeTest = test; position = 0; commonParent = downwards ? start : (XOMNodeWrapper) start.getParent(); par = (ParentNode) commonParent.node; if (downwards) { ix = (forwards ? 0 : par.getChildCount()); } else { // find the start node among the list of siblings // ix = start.getSiblingPosition(); ix = par.indexOf(start.node); if (forwards) ix++; } cursor = ix; if (!downwards && !forwards) ix--; } /** * Move to the next node, without returning it. Returns true if there is * a next node, false if the end of the sequence has been reached. After * calling this method, the current node may be retrieved using the * current() function. */ public boolean moveNext() { return (next() != null); } public NodeInfo next() { NodeInfo curr; do { // until we find a match curr = advance(); } while (curr != null && nodeTest != null && (!nodeTest.matches(curr))); if (curr != null) position++; current = curr; return curr; } private NodeInfo advance() { Node nextChild; do { if (forwards) { if (cursor == par.getChildCount()) return null; nextChild = par.getChild(cursor++); } else { // backwards if (cursor == 0) return null; nextChild = par.getChild(--cursor); } } while (nextChild instanceof DocType); // DocType is not an XPath node; can occur for /child::node() NodeInfo curr = makeWrapper(nextChild, docWrapper, commonParent, ix); ix += (forwards ? 1 : -1); return curr; } public NodeInfo current() { return current; } public int position() { return position; } public void close() { } /** * Return an iterator over an axis, starting at the current node. * * @param axis the axis to iterate over, using a constant such as * {@link net.sf.saxon.om.AxisInfo#CHILD} * @param test a predicate to apply to the nodes before returning them. * @throws NullPointerException if there is no current node */ public AxisIterator iterateAxis(byte axis, NodeTest test) { return current.iterateAxis(axis, test); } /** * Return the atomized value of the current node. * * @return the atomized value. * @throws NullPointerException if there is no current node */ public Sequence atomize() throws XPathException { return current.atomize(); } /** * Return the string value of the current node. * * @return the string value, as an instance of CharSequence. * @throws NullPointerException if there is no current node */ public CharSequence getStringValue() { return current.getStringValue(); } /*@NotNull*/ public AxisIterator getAnother() { return new ChildAxisIterator(start, downwards, forwards, nodeTest); } public int getProperties() { return 0; } } /* *//** * A bit of a misnomer; efficiently takes care of descendants, * descentants-or-self as well as "following" axis. * "includeSelf" must be false for the following axis. * Uses simple and effective O(1) backtracking via indexOf(). *//* private final class DescendantAxisIterator implements AxisIterator { private XOMNodeWrapper start; private boolean includeSelf; private boolean following; private Node anchor; // so we know where to stop the scan private Node currNode; private boolean moveToNextSibling; private NodeInfo current; private NodeTest nodeTest; private int position; private String testLocalName; private String testURI; public DescendantAxisIterator(XOMNodeWrapper start, boolean includeSelf, boolean following, NodeTest test) { this.start = start; this.includeSelf = includeSelf; this.following = following; moveToNextSibling = following; if (!following) anchor = start.node; if (!includeSelf) currNode = start.node; if (test == AnyNodeTest.getInstance()) { // performance hack test = null; // mark as AnyNodeTest } else if (test instanceof NameTest) { NameTest nt = (NameTest) test; if (nt.getPrimitiveType() == Type.ELEMENT) { // performance hack // mark as element name test testLocalName = nt.getLocalPart(); testURI = nt.getNamespaceURI(); } } else if (test instanceof NodeKindTest) { if (test.getPrimitiveType() == Type.ELEMENT) { // performance hack // mark as element type test testLocalName = ""; testURI = null; } } nodeTest = test; position = 0; } *//** * Move to the next node, without returning it. Returns true if there is * a next node, false if the end of the sequence has been reached. After * calling this method, the current node may be retrieved using the * current() function. *//* public boolean moveNext() { return (next() != null); } public NodeInfo next() { NodeInfo curr; do { // until we find a match curr = advance(); } while (curr != null && nodeTest != null && (! nodeTest.matches(curr))); if (curr != null) position++; current = curr; return curr; } // might look expensive at first glance - but it's not private NodeInfo advance() { if (currNode == null) { // if includeSelf currNode = start.node; return start; } int i; do { i = 0; Node p = currNode; if (p.getChildCount() == 0 || moveToNextSibling) { // move to next sibling moveToNextSibling = false; // do it just once while (true) { // if we've reached the root we're done scanning p = currNode.getParent(); if (p == null) return null; // Note: correct even if currNode is an attribute. // Performance is particularly good with the O(1) patch // for XOM's ParentNode.indexOf() i = currNode.getParent().indexOf(currNode) + 1; if (i < p.getChildCount()) { break; // break out of while(true) loop; move to next sibling } else { // reached last sibling; move up currNode = p; // if we've come all the way back to the start anchor we're done if (p == anchor) return null; } } } currNode = p.getChild(i); } while (!conforms(currNode)); // note the null here: makeNodeWrapper(parent, ...) is fast, so it // doesn't really matter that we don't keep a link to it. // In fact, it makes objects more short lived, easing pressure on // the VM allocator and collector for tenured heaps. return makeWrapper(currNode, docWrapper, null, i); } // avoids XOMNodeWrapper allocation when there's clearly a mismatch (common case) private boolean conforms(Node node) { if (testLocalName != null) { // element test? if (!(node instanceof Element)) return false; if (testURI == null) return true; // pure element type test // element name test Element elem = (Element) node; return testLocalName.equals(elem.getLocalName()) && testURI.equals(elem.getNamespaceURI()); } else { // DocType is not an XPath node; can occur for /descendants::node() return !(node instanceof DocType); } } public NodeInfo current() { return current; } public int position() { return position; } public void close() { } *//** * Return an iterator over an axis, starting at the current node. * * @param axis the axis to iterate over, using a constant such as * {@link net.sf.saxon.om.AxisInfo#CHILD} * @param test a predicate to apply to the nodes before returning them. * @throws NullPointerException if there is no current node *//* public AxisIterator iterateAxis(byte axis, NodeTest test) { return current.iterateAxis(axis, test); } *//** * Return the atomized value of the current node. * * @return the atomized value. * @throws NullPointerException if there is no current node *//* public Sequence atomize() throws XPathException { return current.atomize(); } *//** * Return the string value of the current node. * * @return the string value, as an instance of CharSequence. * @throws NullPointerException if there is no current node *//* public CharSequence getStringValue() { return current.getStringValue(); } *//*@NotNull*//* public AxisIterator getAnother() { return new DescendantAxisIterator(start, includeSelf, following, nodeTest); } public int getProperties() { return 0; } }*/ /** * Efficiently takes care of preceding axis and Saxon internal preceding-or-ancestor axis. * Uses simple and effective O(1) backtracking via indexOf(). * Implemented along similar lines as DescendantAxisIterator. */ private final class PrecedingAxisIterator implements AxisIterator { private XOMNodeWrapper start; private boolean includeAncestors; private Node currNode; private ParentNode nextAncestor; // next ancestors to skip if !includeAncestors private NodeInfo current; private NodeTest nodeTest; private int position; private String testLocalName; private String testURI; public PrecedingAxisIterator(XOMNodeWrapper start, boolean includeAncestors, NodeTest test) { this.start = start; this.includeAncestors = includeAncestors; currNode = start.node; nextAncestor = includeAncestors ? null : start.node.getParent(); if (test == AnyNodeTest.getInstance()) { // performance hack test = null; // mark as AnyNodeTest } else if (test instanceof NameTest) { NameTest nt = (NameTest) test; if (nt.getPrimitiveType() == Type.ELEMENT) { // performance hack // mark as element name test NamePool pool = getNamePool(); testLocalName = pool.getLocalName(nt.getFingerprint()); testURI = pool.getURI(nt.getFingerprint()); } } else if (test instanceof NodeKindTest) { if (test.getPrimitiveType() == Type.ELEMENT) { // performance hack // mark as element type test testLocalName = ""; testURI = null; } } nodeTest = test; position = 0; } /** * Move to the next node, without returning it. Returns true if there is * a next node, false if the end of the sequence has been reached. After * calling this method, the current node may be retrieved using the * current() function. */ public boolean moveNext() { return (next() != null); } public NodeInfo next() { NodeInfo curr; do { // until we find a match curr = advance(); } while (curr != null && nodeTest != null && (!nodeTest.matches(curr))); if (curr != null) position++; current = curr; return curr; } // might look expensive at first glance - but it's not private NodeInfo advance() { int i; do { Node p; while (true) { // if we've reached the root we're done scanning // System.out.println("p="+p); p = currNode.getParent(); if (p == null) return null; // Note: correct even if currNode is an attribute. // Performance is particularly good with the O(1) patch // for XOM's ParentNode.indexOf() i = currNode.getParent().indexOf(currNode) - 1; if (i >= 0) { // move to next sibling's last descendant node p = p.getChild(i); // move to next sibling int j; while ((j = p.getChildCount() - 1) >= 0) { // move to last descendant node p = p.getChild(j); i = j; } break; // break out of while(true) loop } else { // there are no more siblings; move up // if !includeAncestors skip the ancestors of the start node // assert p != null if (p != nextAncestor) break; // break out of while(true) loop nextAncestor = nextAncestor.getParent(); currNode = p; } } currNode = p; } while (!conforms(currNode)); // note the null here: makeNodeWrapper(parent, ...) is fast, so it // doesn't really matter that we don't keep a link to it. // In fact, it makes objects more short lived, easing pressure on // the VM allocator and collector for tenured heaps. return makeWrapper(currNode, docWrapper, null, i); } // avoids XOMNodeWrapper allocation when there's clearly a mismatch (common case) // same as for DescendantAxisIterator private boolean conforms(Node node) { if (testLocalName != null) { // element test? if (!(node instanceof Element)) { return false; } if (testURI == null) { return true; // pure element type test } // element name test Element elem = (Element) node; return testLocalName.equals(elem.getLocalName()) && testURI.equals(elem.getNamespaceURI()); } else { // DocType is not an XPath node return !(node instanceof DocType); } } public NodeInfo current() { return current; } public int position() { return position; } public void close() { } /** * Return an iterator over an axis, starting at the current node. * * @param axis the axis to iterate over, using a constant such as * {@link net.sf.saxon.om.AxisInfo#CHILD} * @param test a predicate to apply to the nodes before returning them. * @throws NullPointerException if there is no current node */ public AxisIterator iterateAxis(byte axis, NodeTest test) { return current.iterateAxis(axis, test); } /** * Return the atomized value of the current node. * * @return the atomized value. * @throws NullPointerException if there is no current node */ public Sequence atomize() throws XPathException { return current.atomize(); } /** * Return the string value of the current node. * * @return the string value, as an instance of CharSequence. * @throws NullPointerException if there is no current node */ public CharSequence getStringValue() { return current.getStringValue(); } /*@NotNull*/ public AxisIterator getAnother() { return new PrecedingAxisIterator(start, includeAncestors, nodeTest); } public int getProperties() { return 0; } } }