dtmdefaultbasetraversers.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,740 行 · 第 1/4 页
JAVA
1,740 行
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xalan" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, Lotus * Development Corporation., http://www.lotus.com. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package org.apache.xml.dtm.ref;import org.apache.xml.dtm.*;import javax.xml.transform.Source;import org.apache.xml.utils.XMLStringFactory;import org.apache.xalan.res.XSLTErrorResources;import org.apache.xalan.res.XSLMessages;/** * This class implements the traversers for DTMDefaultBase. * * PLEASE NOTE that the public interface for all traversers should be * in terms of DTM Node Handles... but they may use the internal node * identity indices within their logic, for efficiency's sake. Be very * careful to avoid confusing these when maintaining this code. * */public abstract class DTMDefaultBaseTraversers extends DTMDefaultBase{ /** * Construct a DTMDefaultBaseTraversers object from a DOM node. * * @param mgr The DTMManager who owns this DTM. * @param domSource the DOM source that this DTM will wrap. * @param source The object that is used to specify the construction source. * @param dtmIdentity The DTM identity ID for this DTM. * @param whiteSpaceFilter The white space filter for this DTM, which may * be null. * @param xstringfactory The factory to use for creating XMLStrings. * @param doIndexing true if the caller considers it worth it to use * indexing schemes. */ public DTMDefaultBaseTraversers(DTMManager mgr, Source source, int dtmIdentity, DTMWSFilter whiteSpaceFilter, XMLStringFactory xstringfactory, boolean doIndexing) { super(mgr, source, dtmIdentity, whiteSpaceFilter, xstringfactory, doIndexing); } /** * This returns a stateless "traverser", that can navigate * over an XPath axis, though perhaps not in document order. * * @param axis One of Axes.ANCESTORORSELF, etc. * * @return A DTMAxisTraverser, or null if the given axis isn't supported. */ public DTMAxisTraverser getAxisTraverser(final int axis) { DTMAxisTraverser traverser; if (null == m_traversers) // Cache of stateless traversers for this DTM { m_traversers = new DTMAxisTraverser[Axis.names.length]; traverser = null; } else { traverser = m_traversers[axis]; // Share/reuse existing traverser if (traverser != null) return traverser; } switch (axis) // Generate new traverser { case Axis.ANCESTOR : traverser = new AncestorTraverser(); break; case Axis.ANCESTORORSELF : traverser = new AncestorOrSelfTraverser(); break; case Axis.ATTRIBUTE : traverser = new AttributeTraverser(); break; case Axis.CHILD : traverser = new ChildTraverser(); break; case Axis.DESCENDANT : traverser = new DescendantTraverser(); break; case Axis.DESCENDANTORSELF : traverser = new DescendantOrSelfTraverser(); break; case Axis.FOLLOWING : traverser = new FollowingTraverser(); break; case Axis.FOLLOWINGSIBLING : traverser = new FollowingSiblingTraverser(); break; case Axis.NAMESPACE : traverser = new NamespaceTraverser(); break; case Axis.NAMESPACEDECLS : traverser = new NamespaceDeclsTraverser(); break; case Axis.PARENT : traverser = new ParentTraverser(); break; case Axis.PRECEDING : traverser = new PrecedingTraverser(); break; case Axis.PRECEDINGSIBLING : traverser = new PrecedingSiblingTraverser(); break; case Axis.SELF : traverser = new SelfTraverser(); break; case Axis.ALL : traverser = new AllFromRootTraverser(); break; case Axis.ALLFROMNODE : traverser = new AllFromNodeTraverser(); break; case Axis.PRECEDINGANDANCESTOR : traverser = new PrecedingAndAncestorTraverser(); break; case Axis.DESCENDANTSFROMROOT : traverser = new DescendantFromRootTraverser(); break; case Axis.DESCENDANTSORSELFFROMROOT : traverser = new DescendantOrSelfFromRootTraverser(); break; case Axis.ROOT : traverser = new RootTraverser(); break; case Axis.FILTEREDLIST : return null; // Don't want to throw an exception for this one. default : throw new DTMException(XSLMessages.createMessage(XSLTErrorResources.ER_UNKNOWN_AXIS_TYPE, new Object[]{Integer.toString(axis)})); //"Unknown axis traversal type: "+axis); } if (null == traverser) throw new DTMException(XSLMessages.createMessage(XSLTErrorResources.ER_AXIS_TRAVERSER_NOT_SUPPORTED, new Object[]{Axis.names[axis]})); // "Axis traverser not supported: " // + Axis.names[axis]); m_traversers[axis] = traverser; return traverser; } /** * Implements traversal of the Ancestor access, in reverse document order. */ private class AncestorTraverser extends DTMAxisTraverser { /** * Traverse to the next node after the current node. * * @param context The context node if this iteration. * @param current The current node of the iteration. * * @return the next node in the iteration, or DTM.NULL. */ public int next(int context, int current) { return getParent(current); } /** * Traverse to the next node after the current node that is matched * by the expanded type ID. * * @param context The context node of this iteration. * @param current The current node of the iteration. * @param expandedTypeID The expanded type ID that must match. * * @return the next node in the iteration, or DTM.NULL. */ public int next(int context, int current, int expandedTypeID) { // Process using identities current = makeNodeIdentity(current); while (DTM.NULL != (current = m_parent.elementAt(current))) { if (m_exptype.elementAt(current) == expandedTypeID) return makeNodeHandle(current); } return NULL; } } /** * Implements traversal of the Ancestor access, in reverse document order. */ private class AncestorOrSelfTraverser extends AncestorTraverser { /** * By the nature of the stateless traversal, the context node can not be * returned or the iteration will go into an infinate loop. To see if * the self node should be processed, use this function. * * @param context The context node of this traversal. * * @return the first node in the traversal. */ public int first(int context) { return context; } /** * By the nature of the stateless traversal, the context node can not be * returned or the iteration will go into an infinate loop. To see if * the self node should be processed, use this function. If the context * node does not match the expanded type ID, this function will return * false. * * @param context The context node of this traversal. * @param expandedTypeID The expanded type ID that must match. * * @return the first node in the traversal. */ public int first(int context, int expandedTypeID) { return (getExpandedTypeID(context) == expandedTypeID) ? context : next(context, context, expandedTypeID); } } /** * Implements traversal of the Attribute access */ private class AttributeTraverser extends DTMAxisTraverser { /** * Traverse to the next node after the current node. * * @param context The context node of this iteration. * @param current The current node of the iteration. * * @return the next node in the iteration, or DTM.NULL. */ public int next(int context, int current) { return (context == current) ? getFirstAttribute(context) : getNextAttribute(current); } /** * Traverse to the next node after the current node that is matched * by the expanded type ID. * * @param context The context node of this iteration. * @param current The current node of the iteration. * @param expandedTypeID The expanded type ID that must match. * * @return the next node in the iteration, or DTM.NULL. */ public int next(int context, int current, int expandedTypeID) { current = (context == current) ? getFirstAttribute(context) : getNextAttribute(current); do { if (getExpandedTypeID(current) == expandedTypeID) return current; } while (DTM.NULL != (current = getNextAttribute(current))); return NULL; } } /** * Implements traversal of the Ancestor access, in reverse document order. */ private class ChildTraverser extends DTMAxisTraverser { /** * Get the next indexed node that matches the expanded type ID. Before * calling this function, one should first call * {@link #isIndexed(int) isIndexed} to make sure that the index can * contain nodes that match the given expanded type ID. * * @param axisRoot The root identity of the axis. * @param nextPotential The node found must match or occur after this node. * @param expandedTypeID The expanded type ID for the request. * * @return The node ID or NULL if not found. */ protected int getNextIndexed(int axisRoot, int nextPotential, int expandedTypeID) { int nsIndex = m_expandedNameTable.getNamespaceID(expandedTypeID); int lnIndex = m_expandedNameTable.getLocalNameID(expandedTypeID); for (; ; ) { int nextID = findElementFromIndex(nsIndex, lnIndex, nextPotential); if (NOTPROCESSED != nextID) { int parentID = m_parent.elementAt(nextID); // Is it a child? if(parentID == axisRoot) return nextID; // If the parent occured before the subtree root, then // we know it is past the child axis. if(parentID < axisRoot) return NULL; // Otherwise, it could be a descendant below the subtree root // children, or it could be after the subtree root. So we have // to climb up until the parent is less than the subtree root, in // which case we return NULL, or until it is equal to the subtree // root, in which case we continue to look. do { parentID = m_parent.elementAt(parentID); if(parentID < axisRoot) return NULL; } while(parentID > axisRoot); // System.out.println("Found node via index: "+first); nextPotential = nextID+1; continue; } nextNode(); if(!(m_nextsib.elementAt(axisRoot) == NOTPROCESSED)) break; } return DTM.NULL; } /** * By the nature of the stateless traversal, the context node can not be * returned or the iteration will go into an infinate loop. So to traverse * an axis, the first function must be used to get the first node. * * <p>This method needs to be overloaded only by those axis that process * the self node. <\p> * * @param context The context node of this traversal. This is the point * that the traversal starts from. * @return the first node in the traversal. */ public int first(int context) { return getFirstChild(context); } /** * By the nature of the stateless traversal, the context node can not be * returned or the iteration will go into an infinate loop. So to traverse * an axis, the first function must be used to get the first node. * * <p>This method needs to be overloaded only by those axis that process * the self node. <\p> * * @param context The context node of this traversal. This is the point * of origin for the traversal -- its "root node" or starting point. * @param expandedTypeID The expanded type ID that must match. * * @return the first node in the traversal.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?