KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > pattern > AnyNodeTest


1 package net.sf.saxon.pattern;
2
3 import net.sf.saxon.om.Item;
4 import net.sf.saxon.om.NodeInfo;
5 import net.sf.saxon.type.AnyItemType;
6 import net.sf.saxon.type.ItemType;
7 import net.sf.saxon.type.Type;
8 import net.sf.saxon.tinytree.TinyTree;
9
10 /**
11   * NodeTest is an interface that enables a test of whether a node has a particular
12   * name and type. An AnyNodeTest matches any node.
13   *
14   * @author Michael H. Kay
15   */

16
17 public final class AnyNodeTest extends NodeTest {
18
19     static AnyNodeTest instance = new AnyNodeTest();
20
21     /**
22     * Get an instance of AnyNodeTest
23     */

24
25     public static AnyNodeTest getInstance() {
26         return instance;
27     }
28
29     /**
30      * Test whether a given item conforms to this type
31      * @param item The item to be tested
32      * @return true if the item is an instance of this type; false otherwise
33     */

34
35     public boolean matchesItem(Item item) {
36         return (item instanceof NodeInfo);
37     }
38
39     public ItemType getSuperType() {
40         return AnyItemType.getInstance();
41     }
42
43     /**
44     * Test whether this node test is satisfied by a given node
45     * @param nodeType The type of node to be matched
46      * @param fingerprint identifies the expanded name of the node to be matched
47      */

48
49     public final boolean matches(int nodeType, int fingerprint, int annotation) {
50         return nodeType != Type.PARENT_POINTER;
51     }
52
53     /**
54      * Test whether this node test is satisfied by a given node on a TinyTree. The node
55      * must be a document, element, text, comment, or processing instruction node.
56      * This method is provided
57      * so that when navigating a TinyTree a node can be rejected without
58      * actually instantiating a NodeInfo object.
59      *
60      * @param tree the TinyTree containing the node
61      * @param nodeNr the number of the node within the TinyTree
62      * @return true if the node matches the NodeTest, otherwise false
63      */

64
65     public boolean matches(TinyTree tree, int nodeNr) {
66         return tree.nodeKind[nodeNr] != Type.PARENT_POINTER;
67     }
68
69     /**
70      * Test whether this node test is satisfied by a given node. This alternative
71      * method is used in the case of nodes where calculating the fingerprint is expensive,
72      * for example DOM or JDOM nodes.
73      * @param node the node to be matched
74      */

75
76     public boolean matches(NodeInfo node) {
77         return true;
78     }
79
80     /**
81     * Determine the default priority of this node test when used on its own as a Pattern
82     */

83
84     public final double getDefaultPriority() {
85         return -0.5;
86     }
87
88     /**
89      * Get a mask indicating which kinds of nodes this NodeTest can match. This is a combination
90      * of bits: 1<<Type.ELEMENT for element nodes, 1<<Type.TEXT for text nodes, and so on.
91      */

92
93     public int getNodeKindMask() {
94         return 1<<Type.ELEMENT | 1<<Type.TEXT | 1<<Type.COMMENT | 1<<Type.PROCESSING_INSTRUCTION |
95                 1<<Type.ATTRIBUTE | 1<<Type.NAMESPACE | 1<<Type.DOCUMENT;
96     }
97
98     public String JavaDoc toString() {
99         return "node()";
100     }
101
102     /**
103      * Returns a hash code value for the object.
104      */

105
106     public int hashCode() {
107         return "AnyNodeTest".hashCode();
108     }
109
110 }
111
112 //
113
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
114
// you may not use this file except in compliance with the License. You may obtain a copy of the
115
// License at http://www.mozilla.org/MPL/
116
//
117
// Software distributed under the License is distributed on an "AS IS" basis,
118
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
119
// See the License for the specific language governing rights and limitations under the License.
120
//
121
// The Original Code is: all this file.
122
//
123
// The Initial Developer of the Original Code is Michael H. Kay.
124
//
125
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
126
//
127
// Contributor(s): none.
128
//
129
Popular Tags