001: package net.sf.saxon.pattern;
002:
003: import net.sf.saxon.om.Item;
004: import net.sf.saxon.om.NodeInfo;
005: import net.sf.saxon.type.AnyItemType;
006: import net.sf.saxon.type.ItemType;
007: import net.sf.saxon.type.Type;
008: import net.sf.saxon.type.TypeHierarchy;
009: import net.sf.saxon.tinytree.TinyTree;
010: import net.sf.saxon.expr.XPathContext;
011:
012: /**
013: * NodeTest is an interface that enables a test of whether a node has a particular
014: * name and type. An AnyNodeTest matches any node.
015: *
016: * @author Michael H. Kay
017: */
018:
019: public final class AnyNodeTest extends NodeTest {
020:
021: static AnyNodeTest instance = new AnyNodeTest();
022:
023: /**
024: * Get an instance of AnyNodeTest
025: */
026:
027: public static AnyNodeTest getInstance() {
028: return instance;
029: }
030:
031: /**
032: * Test whether a given item conforms to this type
033: * @param item The item to be tested
034: * @param context
035: * @return true if the item is an instance of this type; false otherwise
036: */
037:
038: public boolean matchesItem(Item item, XPathContext context) {
039: return (item instanceof NodeInfo);
040: }
041:
042: public ItemType getSuperType(TypeHierarchy th) {
043: return AnyItemType.getInstance();
044: }
045:
046: /**
047: * Test whether this node test is satisfied by a given node
048: * @param nodeType The type of node to be matched
049: * @param fingerprint identifies the expanded name of the node to be matched
050: */
051:
052: public final boolean matches(int nodeType, int fingerprint,
053: int annotation) {
054: return nodeType != Type.PARENT_POINTER;
055: }
056:
057: /**
058: * Test whether this node test is satisfied by a given node on a TinyTree. The node
059: * must be a document, element, text, comment, or processing instruction node.
060: * This method is provided
061: * so that when navigating a TinyTree a node can be rejected without
062: * actually instantiating a NodeInfo object.
063: *
064: * @param tree the TinyTree containing the node
065: * @param nodeNr the number of the node within the TinyTree
066: * @return true if the node matches the NodeTest, otherwise false
067: */
068:
069: public boolean matches(TinyTree tree, int nodeNr) {
070: return tree.nodeKind[nodeNr] != Type.PARENT_POINTER;
071: }
072:
073: /**
074: * Test whether this node test is satisfied by a given node. This alternative
075: * method is used in the case of nodes where calculating the fingerprint is expensive,
076: * for example DOM or JDOM nodes.
077: * @param node the node to be matched
078: */
079:
080: public boolean matches(NodeInfo node) {
081: return true;
082: }
083:
084: /**
085: * Determine the default priority of this node test when used on its own as a Pattern
086: */
087:
088: public final double getDefaultPriority() {
089: return -0.5;
090: }
091:
092: /**
093: * Get a mask indicating which kinds of nodes this NodeTest can match. This is a combination
094: * of bits: 1<<Type.ELEMENT for element nodes, 1<<Type.TEXT for text nodes, and so on.
095: */
096:
097: public int getNodeKindMask() {
098: return 1 << Type.ELEMENT | 1 << Type.TEXT | 1 << Type.COMMENT
099: | 1 << Type.PROCESSING_INSTRUCTION
100: | 1 << Type.ATTRIBUTE | 1 << Type.NAMESPACE
101: | 1 << Type.DOCUMENT;
102: }
103:
104: public String toString() {
105: return "node()";
106: }
107:
108: /**
109: * Returns a hash code value for the object.
110: */
111:
112: public int hashCode() {
113: return "AnyNodeTest".hashCode();
114: }
115:
116: }
117:
118: //
119: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
120: // you may not use this file except in compliance with the License. You may obtain a copy of the
121: // License at http://www.mozilla.org/MPL/
122: //
123: // Software distributed under the License is distributed on an "AS IS" basis,
124: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
125: // See the License for the specific language governing rights and limitations under the License.
126: //
127: // The Original Code is: all this file.
128: //
129: // The Initial Developer of the Original Code is Michael H. Kay.
130: //
131: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
132: //
133: // Contributor(s): none.
134: //
|