KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.pattern;
2 import net.sf.saxon.om.FingerprintedNode;
3 import net.sf.saxon.om.NamePool;
4 import net.sf.saxon.om.NodeInfo;
5 import net.sf.saxon.type.Type;
6 import net.sf.saxon.type.ItemType;
7 import net.sf.saxon.tinytree.TinyTree;
8
9 import java.util.HashSet JavaDoc;
10 import java.util.Set JavaDoc;
11
12 /**
13   * NodeTest is an interface that enables a test of whether a node has a particular
14   * name and type. A NameTest matches the node kind and the namespace URI and the local
15   * name.
16   *
17   * @author Michael H. Kay
18   */

19
20 public class NameTest extends NodeTest {
21
22     private int nodeKind;
23     private int fingerprint;
24     private NamePool namePool;
25     private String JavaDoc uri = null;
26     private String JavaDoc localName = null;
27
28     public NameTest(int nodeType, int nameCode, NamePool namePool) {
29         this.nodeKind = nodeType;
30         this.fingerprint = nameCode & 0xfffff;
31         this.namePool = namePool;
32     }
33
34     /**
35     * Create a NameTest for nodes of the same type and name as a given node
36     */

37
38     public NameTest(NodeInfo node) {
39         this.nodeKind = node.getNodeKind();
40         this.fingerprint = node.getFingerprint();
41         this.namePool = node.getNamePool();
42     }
43
44     /**
45     * Test whether this node test is satisfied by a given node
46     * @param nodeKind The type of node to be matched
47      * @param nameCode identifies the expanded name of the node to be matched
48      */

49
50     public boolean matches(int nodeKind, int nameCode, int annotation) {
51         // System.err.println("Matching node " + nameCode + " against " + this.fingerprint);
52
// System.err.println(" " + ((nameCode&0xfffff) == this.fingerprint && nodeType == this.nodeType));
53
return ((nameCode&0xfffff) == this.fingerprint && nodeKind == this.nodeKind);
54         // deliberately in this order for speed (first test usually fails)
55
}
56
57     /**
58      * Test whether this node test is satisfied by a given node on a TinyTree. The node
59      * must be a document, element, text, comment, or processing instruction node.
60      * This method is provided so that when navigating a TinyTree a node can be rejected without
61      * actually instantiating a NodeInfo object.
62      *
63      * @param tree the TinyTree containing the node
64      * @param nodeNr the number of the node within the TinyTree
65      * @return true if the node matches the NodeTest, otherwise false
66      */

67
68     public boolean matches(TinyTree tree, int nodeNr) {
69         return ((tree.getNameCode(nodeNr)&0xfffff) == this.fingerprint && tree.getNodeKind(nodeNr) == this.nodeKind);
70     }
71
72     /**
73      * Test whether this node test is satisfied by a given node. This alternative
74      * method is used in the case of nodes where calculating the fingerprint is expensive,
75      * for example DOM or JDOM nodes.
76      * @param node the node to be matched
77      */

78
79     public boolean matches(NodeInfo node) {
80         if (node.getNodeKind() != nodeKind) {
81             return false;
82         }
83
84         // Two different algorithms are used for name matching. If the fingerprint of the node is readily
85
// available, we use it to do an integer comparison. Otherwise, we do string comparisons on the URI
86
// and local name. In practice, Saxon's native node implementations use fingerprint matching, while
87
// DOM and JDOM nodes use string comparison of names
88

89         if (node instanceof FingerprintedNode) {
90             return node.getFingerprint() == fingerprint;
91         } else {
92             if (uri == null) {
93                 uri = namePool.getURI(fingerprint);
94             }
95             if (localName == null) {
96                 localName = namePool.getLocalName(fingerprint);
97             }
98             return localName.equals(node.getLocalPart()) && uri.equals(node.getURI());
99         }
100     }
101
102     /**
103     * Determine the default priority of this node test when used on its own as a Pattern
104     */

105
106     public final double getDefaultPriority() {
107         return 0.0;
108     }
109
110     /**
111     * Get the fingerprint required
112     */

113
114     public int getFingerprint() {
115         return fingerprint;
116     }
117
118     /**
119     * Determine the types of nodes to which this pattern applies. Used for optimisation.
120     * For patterns that match nodes of several types, return Type.NODE
121     * @return the type of node matched by this pattern. e.g. Type.ELEMENT or Type.TEXT
122     */

123
124     public int getPrimitiveType() {
125         return nodeKind;
126     }
127
128     /**
129      * Get the type from which this item type is derived by restriction. This
130      * is the supertype in the XPath type heirarchy, as distinct from the Schema
131      * base type: this means that the supertype of xs:boolean is xdt:anyAtomicType,
132      * whose supertype is item() (rather than xs:anySimpleType).
133      * <p>
134      * In fact the concept of "supertype" is not really well-defined, because the types
135      * form a lattice rather than a hierarchy. The only real requirement on this function
136      * is that it returns a type that strictly subsumes this type, ideally as narrowly
137      * as possible.
138      * @return the supertype, or null if this type is item()
139      */

140
141     public ItemType getSuperType() {
142         return NodeKindTest.makeNodeKindTest(nodeKind);
143     }
144
145     /**
146      * Get a mask indicating which kinds of nodes this NodeTest can match. This is a combination
147      * of bits: 1<<Type.ELEMENT for element nodes, 1<<Type.TEXT for text nodes, and so on.
148      */

149
150     public int getNodeKindMask() {
151         return 1<<nodeKind;
152     }
153
154     /**
155      * Get the set of node names allowed by this NodeTest. This is returned as a set of Integer fingerprints.
156      * A null value indicates that all names are permitted (i.e. that there are no constraints on the node name.
157      * The default implementation returns null.
158      */

159
160     public Set JavaDoc getRequiredNodeNames() {
161         HashSet JavaDoc s = new HashSet JavaDoc(1);
162         s.add(new Integer JavaDoc(fingerprint));
163         return s;
164     }
165
166     public String JavaDoc toString() {
167         return toString(namePool);
168     }
169
170     public String JavaDoc toString(NamePool pool) {
171         switch (nodeKind) {
172             case Type.ELEMENT:
173                 return "element(" + pool.getDisplayName(fingerprint) + ')';
174             case Type.ATTRIBUTE:
175                 return "attribute(" + pool.getDisplayName(fingerprint) + ')';
176             case Type.PROCESSING_INSTRUCTION:
177                 return "processing-instruction(" + pool.getDisplayName(fingerprint) + ')';
178             case Type.NAMESPACE:
179                 return "namespace(" + pool.getDisplayName(fingerprint) + ')';
180         }
181         return pool.getDisplayName(fingerprint);
182     }
183
184     /**
185      * Returns a hash code value for the object.
186      */

187
188     public int hashCode() {
189         return nodeKind<<20 ^ fingerprint;
190     }
191
192
193 }
194
195 //
196
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
197
// you may not use this file except in compliance with the License. You may obtain a copy of the
198
// License at http://www.mozilla.org/MPL/
199
//
200
// Software distributed under the License is distributed on an "AS IS" basis,
201
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
202
// See the License for the specific language governing rights and limitations under the License.
203
//
204
// The Original Code is: all this file.
205
//
206
// The Initial Developer of the Original Code is Michael H. Kay.
207
//
208
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
209
//
210
// Contributor(s): none.
211
//
212
Popular Tags