dtmdefaultbasetraversers.java

来自「JAVA 所有包」· Java 代码 · 共 1,744 行 · 第 1/4 页

JAVA
1,744
字号
/* * Copyright 1999-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * $Id: DTMDefaultBaseTraversers.java,v 1.2.4.1 2005/09/15 08:15:00 suresh_emailid Exp $ */package com.sun.org.apache.xml.internal.dtm.ref;import com.sun.org.apache.xml.internal.dtm.*;import javax.xml.transform.Source;import com.sun.org.apache.xml.internal.utils.XMLStringFactory;import com.sun.org.apache.xml.internal.res.XMLErrorResources;import com.sun.org.apache.xml.internal.res.XMLMessages;import com.sun.org.apache.xalan.internal.xsltc.dom.NodeCounter;/** * 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 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);  }  /**   * Construct a DTMDefaultBaseTraversers object from a DOM node.   *   * @param mgr The DTMManager who owns this DTM.   * @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.   * @param blocksize The block size of the DTM.   * @param usePrevsib true if we want to build the previous sibling node array.   * @param newNameTable true if we want to use a new ExpandedNameTable for this DTM.   */  public DTMDefaultBaseTraversers(DTMManager mgr, Source source,                                  int dtmIdentity,                                  DTMWSFilter whiteSpaceFilter,                                  XMLStringFactory xstringfactory,                                  boolean doIndexing,                                  int blocksize,                                  boolean usePrevsib,                                  boolean newNameTable)  {    super(mgr, source, dtmIdentity, whiteSpaceFilter, xstringfactory,          doIndexing, blocksize, usePrevsib, newNameTable);  }  /**   * 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.getNamesLength()];      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(XMLMessages.createXMLMessage(XMLErrorResources.ER_UNKNOWN_AXIS_TYPE, new Object[]{Integer.toString(axis)})); //"Unknown axis traversal type: "+axis);    }    if (null == traverser)      throw new DTMException(XMLMessages.createXMLMessage(XMLErrorResources.ER_AXIS_TRAVERSER_NOT_SUPPORTED, new Object[]{Axis.getNames(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.     */    public int first(int context, int expandedTypeID)    {      if(true)      {        int identity = makeNodeIdentity(context);                int firstMatch = getNextIndexed(identity, _firstch(identity),                                 expandedTypeID);               return makeNodeHandle(firstMatch);      }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?