nodetest.java

来自「java jdk 1.4的源码」· Java 代码 · 共 732 行 · 第 1/2 页

JAVA
732
字号
/* * 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.patterns;import java.util.Vector;import javax.xml.transform.TransformerException;import org.apache.xml.dtm.DTM;import org.apache.xml.dtm.DTMFilter;import org.apache.xml.dtm.ref.ExpandedNameTable;import org.apache.xpath.Expression;import org.apache.xpath.ExpressionOwner;import org.apache.xpath.XPath;import org.apache.xpath.XPathContext;import org.apache.xpath.XPathVisitor;import org.apache.xpath.objects.XNumber;import org.apache.xpath.objects.XObject;/** * <meta name="usage" content="advanced"/> * This is the basic node test class for both match patterns and location path * steps. */public class NodeTest extends Expression{  /**   * The namespace or local name for node tests with a wildcard.   *  @see <a href="http://www.w3.org/TR/xpath#NT-NameTest">the XPath NameTest production.</a>    */  public static final String WILD = "*";  /**   * The URL to pass to the Node#supports method, to see if the   * DOM has already been stripped of whitespace nodes.    */  public static final String SUPPORTS_PRE_STRIPPING =    "http://xml.apache.org/xpath/features/whitespace-pre-stripping";  /**   * This attribute determines which node types are accepted.   * @serial   */  protected int m_whatToShow;  /**   * Special bitmap for match patterns starting with a function.   * Make sure this does not conflict with {@link org.w3c.dom.traversal.NodeFilter}.   */  public static final int SHOW_BYFUNCTION = 0x00010000;  /**   * This attribute determines which node types are accepted.   * These constants are defined in the {@link org.w3c.dom.traversal.NodeFilter}   * interface.   *   * @return bitset mainly defined in {@link org.w3c.dom.traversal.NodeFilter}.   */  public int getWhatToShow()  {    return m_whatToShow;  }    /**   * This attribute determines which node types are accepted.   * These constants are defined in the {@link org.w3c.dom.traversal.NodeFilter}   * interface.   *   * @param what bitset mainly defined in {@link org.w3c.dom.traversal.NodeFilter}.   */  public void setWhatToShow(int what)  {    m_whatToShow = what;  }  /**   * The namespace to be tested for, which may be null.   *  @serial    */  String m_namespace;  /**   * Return the namespace to be tested.   *   * @return The namespace to be tested for, or {@link #WILD}, or null.   */  public String getNamespace()  {    return m_namespace;  }  /**   * Set the namespace to be tested.   *   * @param ns The namespace to be tested for, or {@link #WILD}, or null.   */  public void setNamespace(String ns)  {    m_namespace = ns;  }  /**   * The local name to be tested for.   *  @serial    */  protected String m_name;  /**   * Return the local name to be tested.   *   * @return the local name to be tested, or {@link #WILD}, or an empty string.   */  public String getLocalName()  {    return (null == m_name) ? "" : m_name;  }  /**   * Set the local name to be tested.   *   * @param name the local name to be tested, or {@link #WILD}, or an empty string.   */  public void setLocalName(String name)  {    m_name = name;  }  /**   * Statically calculated score for this test.  One of   *  {@link #SCORE_NODETEST},   *  {@link #SCORE_NONE},   *  {@link #SCORE_NSWILD},   *  {@link #SCORE_QNAME}, or   *  {@link #SCORE_OTHER}.   *  @serial   */  XNumber m_score;  /**   * The match score if the pattern consists of just a NodeTest.   *  @see <a href="http://www.w3.org/TR/xslt#conflict">XSLT Specification - 5.5 Conflict Resolution for Template Rules</a>    */  public static final XNumber SCORE_NODETEST =    new XNumber(XPath.MATCH_SCORE_NODETEST);  /**   * The match score if the pattern pattern has the form NCName:*.   *  @see <a href="http://www.w3.org/TR/xslt#conflict">XSLT Specification - 5.5 Conflict Resolution for Template Rules</a>    */  public static final XNumber SCORE_NSWILD =    new XNumber(XPath.MATCH_SCORE_NSWILD);  /**   * The match score if the pattern has the form   * of a QName optionally preceded by an @ character.   *  @see <a href="http://www.w3.org/TR/xslt#conflict">XSLT Specification - 5.5 Conflict Resolution for Template Rules</a>    */  public static final XNumber SCORE_QNAME =    new XNumber(XPath.MATCH_SCORE_QNAME);  /**   * The match score if the pattern consists of something   * other than just a NodeTest or just a qname.   *  @see <a href="http://www.w3.org/TR/xslt#conflict">XSLT Specification - 5.5 Conflict Resolution for Template Rules</a>    */  public static final XNumber SCORE_OTHER =    new XNumber(XPath.MATCH_SCORE_OTHER);  /**   * The match score if no match is made.   *  @see <a href="http://www.w3.org/TR/xslt#conflict">XSLT Specification - 5.5 Conflict Resolution for Template Rules</a>    */  public static final XNumber SCORE_NONE =    new XNumber(XPath.MATCH_SCORE_NONE);  /**   * Construct an NodeTest that tests for namespaces and node names.   *   *   * @param whatToShow Bit set defined mainly by {@link org.w3c.dom.traversal.NodeFilter}.   * @param namespace The namespace to be tested.   * @param name The local name to be tested.   */  public NodeTest(int whatToShow, String namespace, String name)  {    initNodeTest(whatToShow, namespace, name);  }  /**   * Construct an NodeTest that doesn't test for node names.   *   *   * @param whatToShow Bit set defined mainly by {@link org.w3c.dom.traversal.NodeFilter}.   */  public NodeTest(int whatToShow)  {    initNodeTest(whatToShow);  }    /**   * @see Expression#deepEquals(Expression)   */  public boolean deepEquals(Expression expr)  {  	if(!isSameClass(expr))  		return false;  		  	NodeTest nt = (NodeTest)expr;  	if(null != nt.m_name)  	{  		if(null == m_name)  			return false;  		else if(!nt.m_name.equals(m_name))  			return false;  	}  	else if(null != m_name)  		return false;  	if(null != nt.m_namespace)  	{  		if(null == m_namespace)  			return false;  		else if(!nt.m_namespace.equals(m_namespace))  			return false;  	}  	else if(null != m_namespace)  		return false;  		  		  	if(m_whatToShow != nt.m_whatToShow)  		return false;  		  	if(m_isTotallyWild != nt.m_isTotallyWild)  		return false;	return true;  }  /**   * Null argument constructor.   */  public NodeTest(){}  /**   * Initialize this node test by setting the whatToShow property, and   * calculating the score that this test will return if a test succeeds.   *   *   * @param whatToShow Bit set defined mainly by {@link org.w3c.dom.traversal.NodeFilter}.   */  public void initNodeTest(int whatToShow)  {    m_whatToShow = whatToShow;    calcScore();  }  /**   * Initialize this node test by setting the whatToShow property and the   * namespace and local name, and   * calculating the score that this test will return if a test succeeds.   *   *   * @param whatToShow Bit set defined mainly by {@link org.w3c.dom.traversal.NodeFilter}.   * @param namespace The namespace to be tested.   * @param name The local name to be tested.   */  public void initNodeTest(int whatToShow, String namespace, String name)  {    m_whatToShow = whatToShow;    m_namespace = namespace;    m_name = name;    calcScore();  }  /**   * True if this test has a null namespace and a local name of {@link #WILD}.   *  @serial    */  private boolean m_isTotallyWild;    /**   * Get the static score for this node test.   * @return Should be one of the SCORE_XXX constants.   */  public XNumber getStaticScore()  {    return m_score;  }    /**   * Set the static score for this node test.   * @param score Should be one of the SCORE_XXX constants.   */  public void setStaticScore(XNumber score)  {    m_score = score;  }  /**   * Static calc of match score.   */  protected void calcScore()  {    if ((m_namespace == null) && (m_name == null))      m_score = SCORE_NODETEST;    else if (((m_namespace == WILD) || (m_namespace == null))             && (m_name == WILD))

⌨️ 快捷键说明

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