📄 nodetest.java
字号:
/* * 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: NodeTest.java,v 1.2.4.2 2005/09/15 00:21:14 jeffsuttor Exp $ */package com.sun.org.apache.xpath.internal.patterns;import com.sun.org.apache.xml.internal.dtm.DTM;import com.sun.org.apache.xml.internal.dtm.DTMFilter;import com.sun.org.apache.xpath.internal.Expression;import com.sun.org.apache.xpath.internal.ExpressionOwner;import com.sun.org.apache.xpath.internal.XPath;import com.sun.org.apache.xpath.internal.XPathContext;import com.sun.org.apache.xpath.internal.XPathVisitor;import com.sun.org.apache.xpath.internal.objects.XNumber;import com.sun.org.apache.xpath.internal.objects.XObject;/** * This is the basic node test class for both match patterns and location path * steps. * @xsl.usage advanced */public class NodeTest extends Expression{ static final long serialVersionUID = -5736721866747906182L; /** * 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)) m_score = SCORE_NODETEST; else if ((m_namespace != WILD) && (m_name == WILD)) m_score = SCORE_NSWILD; else m_score = SCORE_QNAME; m_isTotallyWild = (m_namespace == null && m_name == WILD); } /** * Get the score that this test will return if a test succeeds. * * * @return the score that this test will return if a test succeeds. */ public double getDefaultScore() { return m_score.num(); } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -