KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > pattern > NameTest


1 package com.icl.saxon.pattern;
2 import com.icl.saxon.om.NodeInfo;
3 import com.icl.saxon.om.NamePool;
4 import com.icl.saxon.expr.XPathException;
5
6 /**
7   * NodeTest is an interface that enables a test of whether a node has a particular
8   * name and type. A NameTest matches the node type and the namespace URI and the local
9   * name.
10   *
11   * @author <A HREF="mailto:mhkay@iclway.co.uk>Michael H. Kay</A>
12   */

13
14 public class NameTest extends NodeTest {
15     
16     private short nodeType;
17     private int fingerprint;
18     
19     public NameTest(short nodeType, int nameCode) {
20         this.nodeType = nodeType;
21         this.fingerprint = nameCode & 0xfffff;
22         String JavaDoc s = " ";
23         // This last line is absurd, but it circumvents a bug in the Microsoft JVM
24
}
25
26     /**
27     * Create a NameTest for nodes of the same type and name as a given node
28     */

29     
30     public NameTest(NodeInfo node) {
31         this.nodeType = node.getNodeType();
32         this.fingerprint = node.getFingerprint();
33     }
34
35     /**
36     * Test whether this node test is satisfied by a given node
37     */

38
39     public final boolean matches(NodeInfo node) {
40         return fingerprint == node.getFingerprint() &&
41                nodeType == node.getNodeType();
42     }
43
44     /**
45     * Test whether this node test is satisfied by a given node
46     * @param nodeType The type of node to be matched
47     * @param fingerprint identifies the expanded name of the node to be matched
48     */

49
50     public boolean matches(short nodeType, int nameCode) {
51         // System.err.println("Matching node " + fingerprint + " against " + this.fingerprint);
52
// System.err.println(" " + (fingerprint == this.fingerprint && nodeType == this.nodeType));
53
return ((nameCode&0xfffff) == this.fingerprint && nodeType == this.nodeType);
54         // deliberately in this order for speed (first test usually fails)
55
}
56
57     /**
58     * Determine the default priority of this node test when used on its own as a Pattern
59     */

60
61     public final double getDefaultPriority() {
62         return 0.0;
63     }
64
65     /**
66     * Get the fingerprint required
67     */

68     
69     public int getFingerprint() {
70         return fingerprint;
71     }
72
73     /**
74     * Determine the types of nodes to which this pattern applies. Used for optimisation.
75     * For patterns that match nodes of several types, return NodeInfo.NODE
76     * @return the type of node matched by this pattern. e.g. NodeInfo.ELEMENT or NodeInfo.TEXT
77     */

78
79     public short getNodeType() {
80         return nodeType;
81     }
82
83 }
84
85 //
86
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
87
// you may not use this file except in compliance with the License. You may obtain a copy of the
88
// License at http://www.mozilla.org/MPL/
89
//
90
// Software distributed under the License is distributed on an "AS IS" basis,
91
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
92
// See the License for the specific language governing rights and limitations under the License.
93
//
94
// The Original Code is: all this file.
95
//
96
// The Initial Developer of the Original Code is
97
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
98
//
99
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
100
//
101
// Contributor(s): none.
102
//
103
Popular Tags