KickJava   Java API By Example, From Geeks To Geeks.

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


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.tinytree.TinyTree;
5
6 /**
7   * NodeTest is an interface that enables a test of whether a node has a particular
8   * name and type. A LocalNameTest matches the node type and the local name,
9   * it represents an XPath 2.0 test of the form *:name.
10   *
11   * @author Michael H. Kay
12   */

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

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

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

62
63     public boolean matches(NodeInfo node) {
64         return localName.equals(node.getLocalPart());
65     }
66
67
68     /**
69     * Determine the default priority of this node test when used on its own as a Pattern
70     */

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

81
82     public int getPrimitiveType() {
83         return nodeKind;
84     }
85
86     /**
87      * Get a mask indicating which kinds of nodes this NodeTest can match. This is a combination
88      * of bits: 1<<Type.ELEMENT for element nodes, 1<<Type.TEXT for text nodes, and so on.
89      */

90
91     public int getNodeKindMask() {
92         return 1<<nodeKind;
93     }
94
95     public String JavaDoc toString() {
96         return "*:" + localName;
97     }
98
99     /**
100       * Returns a hash code value for the object.
101       */

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