locpathiterator.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,082 行 · 第 1/3 页

JAVA
1,082
字号
/* * 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.xpath.axes;// Java library importsimport java.io.IOException;import java.io.ObjectInputStream;import java.io.Serializable;import javax.xml.transform.TransformerException;import org.apache.xml.dtm.DTM;import org.apache.xml.dtm.DTMFilter;import org.apache.xml.dtm.DTMIterator;import org.apache.xml.dtm.DTMManager;import org.apache.xml.utils.PrefixResolver;import org.apache.xpath.ExpressionOwner;import org.apache.xpath.XPathContext;import org.apache.xpath.XPathVisitor;import org.apache.xpath.compiler.Compiler;import org.apache.xpath.objects.XNodeSet;import org.apache.xpath.objects.XObject;import org.apache.xpath.res.XPATHErrorResources;import org.apache.xalan.res.XSLMessages;import org.xml.sax.ContentHandler;import org.xml.sax.SAXException;/** * <meta name="usage" content="advanced"/> * This class extends NodeSetDTM, which implements NodeIterator, * and fetches nodes one at a time in document order based on a XPath * <a href="http://www.w3.org/TR/xpath#NT-LocationPath>LocationPath</a>. * * <p>If setShouldCacheNodes(true) is called, * as each node is iterated via nextNode(), the node is also stored * in the NodeVector, so that previousNode() can easily be done, except in * the case where the LocPathIterator is "owned" by a UnionPathIterator, * in which case the UnionPathIterator will cache the nodes.</p> */public abstract class LocPathIterator extends PredicatedNodeTest        implements Cloneable, DTMIterator, java.io.Serializable, PathComponent{	  /**   * Create a LocPathIterator object.   *   * @param nscontext The namespace context for this iterator,   * should be OK if null.   */  protected LocPathIterator()  {  }  /**   * Create a LocPathIterator object.   *   * @param nscontext The namespace context for this iterator,   * should be OK if null.   */  protected LocPathIterator(PrefixResolver nscontext)  {    setLocPathIterator(this);    m_prefixResolver = nscontext;  }  /**   * Create a LocPathIterator object, including creation   * of step walkers from the opcode list, and call back   * into the Compiler to create predicate expressions.   *   * @param compiler The Compiler which is creating   * this expression.   * @param opPos The position of this iterator in the   * opcode list from the compiler.   *   * @throws javax.xml.transform.TransformerException   */  protected LocPathIterator(Compiler compiler, int opPos, int analysis)          throws javax.xml.transform.TransformerException  {    this(compiler, opPos, analysis, true);  }  /**   * Create a LocPathIterator object, including creation   * of step walkers from the opcode list, and call back   * into the Compiler to create predicate expressions.   *   * @param compiler The Compiler which is creating   * this expression.   * @param opPos The position of this iterator in the   * opcode list from the compiler.   * @param shouldLoadWalkers True if walkers should be   * loaded, or false if this is a derived iterator and   * it doesn't wish to load child walkers.   *   * @throws javax.xml.transform.TransformerException   */  protected LocPathIterator(          Compiler compiler, int opPos, int analysis, boolean shouldLoadWalkers)            throws javax.xml.transform.TransformerException  {    setLocPathIterator(this);  }    /**    * Get the analysis bits for this walker, as defined in the WalkerFactory.   * @return One of WalkerFactory#BIT_DESCENDANT, etc.   */  public int getAnalysisBits()  {  	int axis = getAxis();  	int bit = WalkerFactory.getAnalysisBitFromAxes(axis);  	return bit;  }    /**   * Read the object from a serialization stream.   *   * @param stream Input stream to read from   *   * @throws java.io.IOException   * @throws javax.xml.transform.TransformerException   */  private void readObject(java.io.ObjectInputStream stream)          throws java.io.IOException, javax.xml.transform.TransformerException  {    try    {      stream.defaultReadObject();      m_clones =  new IteratorPool(this);    }    catch (ClassNotFoundException cnfe)    {      throw new javax.xml.transform.TransformerException(cnfe);    }  }    /**   * Set the environment in which this iterator operates, which should provide:   * a node (the context node... same value as "root" defined below)    * a pair of non-zero positive integers (the context position and the context size)    * a set of variable bindings    * a function library    * the set of namespace declarations in scope for the expression.   *    * <p>At this time the exact implementation of this environment is application    * dependent.  Probably a proper interface will be created fairly soon.</p>   *    * @param environment The environment object.   */  public void setEnvironment(Object environment)  {    // no-op for now.  }    /**   * Get an instance of a DTM that "owns" a node handle.  Since a node    * iterator may be passed without a DTMManager, this allows the    * caller to easily get the DTM using just the iterator.   *   * @param nodeHandle the nodeHandle.   *   * @return a non-null DTM reference.   */  public DTM getDTM(int nodeHandle)  {    // %OPT%    return m_execContext.getDTM(nodeHandle);  }    /**   * Get an instance of the DTMManager.  Since a node    * iterator may be passed without a DTMManager, this allows the    * caller to easily get the DTMManager using just the iterator.   *   * @return a non-null DTMManager reference.   */  public DTMManager getDTMManager()  {    return m_execContext.getDTMManager();  }    /**   * Execute this iterator, meaning create a clone that can   * store state, and initialize it for fast execution from   * the current runtime state.  When this is called, no actual   * query from the current context node is performed.   *   * @param xctxt The XPath execution context.   *   * @return An XNodeSet reference that holds this iterator.   *   * @throws javax.xml.transform.TransformerException   */  public XObject execute(XPathContext xctxt)          throws javax.xml.transform.TransformerException  {    XNodeSet iter = new XNodeSet((LocPathIterator)m_clones.getInstance());    iter.setRoot(xctxt.getCurrentNode(), xctxt);    return iter;  }      /**   * Execute an expression in the XPath runtime context, and return the   * result of the expression.   *   *   * @param xctxt The XPath runtime context.   * @param handler The target content handler.   *   * @return The result of the expression in the form of a <code>XObject</code>.   *   * @throws javax.xml.transform.TransformerException if a runtime exception   *         occurs.   * @throws org.xml.sax.SAXException   */  public void executeCharsToContentHandler(          XPathContext xctxt, org.xml.sax.ContentHandler handler)            throws javax.xml.transform.TransformerException,                   org.xml.sax.SAXException  {    LocPathIterator clone = (LocPathIterator)m_clones.getInstance();    int current = xctxt.getCurrentNode();    clone.setRoot(current, xctxt);        int node = clone.nextNode();    DTM dtm = clone.getDTM(node);    clone.detach();	    if(node != DTM.NULL)    {      dtm.dispatchCharactersEvents(node, handler, false);    }  }    /**   * <meta name="usage" content="experimental"/>   * Given an select expression and a context, evaluate the XPath   * and return the resulting iterator.   *    * @param xctxt The execution context.   * @param contextNode The node that "." expresses.   * @param namespaceContext The context in which namespaces in the   * XPath are supposed to be expanded.   *    * @throws TransformerException thrown if the active ProblemListener decides   * the error condition is severe enough to halt processing.   *   * @throws javax.xml.transform.TransformerException   */  public DTMIterator asIterator(          XPathContext xctxt, int contextNode)            throws javax.xml.transform.TransformerException  {    XNodeSet iter = new XNodeSet((LocPathIterator)m_clones.getInstance());    iter.setRoot(contextNode, xctxt);    return iter;  }    /**   * Tell if the expression is a nodeset expression.  In other words, tell    * if you can execute {@link asNode() asNode} without an exception.   * @return true if the expression can be represented as a nodeset.   */  public boolean isNodesetExpr()  {    return true;  }    /**   * Return the first node out of the nodeset, if this expression is    * a nodeset expression.  This is the default implementation for    * nodesets.  Derived classes should try and override this and return a    * value without having to do a clone operation.   * @param xctxt The XPath runtime context.   * @return the first node out of the nodeset, or DTM.NULL.   */  public int asNode(XPathContext xctxt)    throws javax.xml.transform.TransformerException  {    DTMIterator iter = (DTMIterator)m_clones.getInstance();        int current = xctxt.getCurrentNode();        iter.setRoot(current, xctxt);    int next = iter.nextNode();    // m_clones.freeInstance(iter);    iter.detach();    return next;  }    /**   * Evaluate this operation directly to a boolean.   *   * @param xctxt The runtime execution context.   *   * @return The result of the operation as a boolean.

⌨️ 快捷键说明

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