KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.pattern;
2 import net.sf.saxon.om.NamePool;
3 import net.sf.saxon.om.NodeInfo;
4 import net.sf.saxon.type.ItemType;
5 import net.sf.saxon.tinytree.TinyTree;
6
7 /**
8   * NodeTest is an interface that enables a test of whether a node has a particular
9   * name and type. A NamespaceTest matches the node type and the namespace URI.
10   *
11   * @author Michael H. Kay
12   */

13
14 public final class NamespaceTest extends NodeTest {
15
16     private NamePool namePool;
17     private int nodeKind;
18     private short uriCode;
19     private String JavaDoc uri;
20
21     public NamespaceTest(NamePool pool, int nodeKind, String JavaDoc uri) {
22         namePool = pool;
23         this.nodeKind = nodeKind;
24         this.uri = uri;
25         this.uriCode = pool.allocateCodeForURI(uri);
26     }
27
28     /**
29     * Test whether this node test is satisfied by a given node
30     * @param nodeType The type of node to be matched
31      * @param fingerprint identifies the expanded name of the node to be matched
32      */

33
34     public boolean matches(int nodeType, int fingerprint, int annotation) {
35         if (fingerprint == -1) return false;
36         if (nodeType != nodeKind) return false;
37         return uriCode == namePool.getURICode(fingerprint);
38     }
39
40     /**
41      * Test whether this node test is satisfied by a given node on a TinyTree. The node
42      * must be a document, element, text, comment, or processing instruction node.
43      * This method is provided
44      * so that when navigating a TinyTree a node can be rejected without
45      * actually instantiating a NodeInfo object.
46      *
47      * @param tree the TinyTree containing the node
48      * @param nodeNr the number of the node within the TinyTree
49      */

50
51     public boolean matches(TinyTree tree, int nodeNr) {
52         int fingerprint = tree.getNameCode(nodeNr) & NamePool.FP_MASK;
53         if (fingerprint == -1) return false;
54         if (tree.getNodeKind(nodeNr) != nodeKind) return false;
55         return uriCode == namePool.getURICode(fingerprint);
56     }
57
58     /**
59      * Test whether this node test is satisfied by a given node. This alternative
60      * method is used in the case of nodes where calculating the fingerprint is expensive,
61      * for example DOM or JDOM nodes.
62      * @param node the node to be matched
63      */

64
65     public boolean matches(NodeInfo node) {
66         return node.getNodeKind()==nodeKind && node.getURI().equals(uri);
67     }
68
69     /**
70     * Determine the default priority of this node test when used on its own as a Pattern
71     */

72
73     public final double getDefaultPriority() {
74         return -0.25;
75     }
76
77     /**
78     * Determine the types of nodes to which this pattern applies. Used for optimisation.
79     * For patterns that match nodes of several types, return Type.NODE
80     * @return the type of node matched by this pattern. e.g. Type.ELEMENT or Type.TEXT
81     */

82
83     public int getPrimitiveType() {
84         return nodeKind;
85     }
86
87     /**
88      * Get the type from which this item type is derived by restriction. This
89      * is the supertype in the XPath type heirarchy, as distinct from the Schema
90      * base type: this means that the supertype of xs:boolean is xdt:anyAtomicType,
91      * whose supertype is item() (rather than xs:anySimpleType).
92      * <p>
93      * In fact the concept of "supertype" is not really well-defined, because the types
94      * form a lattice rather than a hierarchy. The only real requirement on this function
95      * is that it returns a type that strictly subsumes this type, ideally as narrowly
96      * as possible.
97      * @return the supertype, or null if this type is item()
98      */

99
100     public ItemType getSuperType() {
101         return NodeKindTest.makeNodeKindTest(nodeKind);
102     }
103
104     /**
105      * Get a mask indicating which kinds of nodes this NodeTest can match. This is a combination
106      * of bits: 1<<Type.ELEMENT for element nodes, 1<<Type.TEXT for text nodes, and so on.
107      */

108
109     public int getNodeKindMask() {
110         return 1<<nodeKind;
111     }
112
113     public String JavaDoc toString() {
114         return '{' + namePool.getURIFromURICode(uriCode) + "}:*";
115     }
116
117     /**
118      * Returns a hash code value for the object.
119      */

120
121     public int hashCode() {
122         return uriCode << 5 + nodeKind;
123     }
124
125 }
126
127 //
128
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
129
// you may not use this file except in compliance with the License. You may obtain a copy of the
130
// License at http://www.mozilla.org/MPL/
131
//
132
// Software distributed under the License is distributed on an "AS IS" basis,
133
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
134
// See the License for the specific language governing rights and limitations under the License.
135
//
136
// The Original Code is: all this file.
137
//
138
// The Initial Developer of the Original Code is Michael H. Kay.
139
//
140
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
141
//
142
// Contributor(s): none.
143
//
144
Popular Tags