KickJava   Java API By Example, From Geeks To Geeks.

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


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

14
15 public class NodeKindTest extends NodeTest {
16
17     public static final NodeKindTest DOCUMENT = new NodeKindTest(Type.DOCUMENT);
18     public static final NodeKindTest ELEMENT = new NodeKindTest(Type.ELEMENT);
19     public static final NodeKindTest ATTRIBUTE = new NodeKindTest(Type.ATTRIBUTE);
20     public static final NodeKindTest TEXT = new NodeKindTest(Type.TEXT);
21     public static final NodeKindTest COMMENT = new NodeKindTest(Type.COMMENT);
22     public static final NodeKindTest PROCESSING_INSTRUCTION = new NodeKindTest(Type.PROCESSING_INSTRUCTION);
23     public static final NodeKindTest NAMESPACE = new NodeKindTest(Type.NAMESPACE);
24
25
26     private int kind;
27
28     private NodeKindTest(int nodeKind) {
29         kind = nodeKind;
30     }
31
32     /**
33      * Make a test for a given kind of node
34      */

35
36     public static NodeTest makeNodeKindTest(int kind) {
37         switch (kind) {
38             case Type.DOCUMENT:
39                 return DOCUMENT;
40             case Type.ELEMENT:
41                 return ELEMENT;
42             case Type.ATTRIBUTE:
43                 return ATTRIBUTE;
44             case Type.COMMENT:
45                 return COMMENT;
46             case Type.TEXT:
47                 return TEXT;
48             case Type.PROCESSING_INSTRUCTION:
49                 return PROCESSING_INSTRUCTION;
50             case Type.NAMESPACE:
51                 return NAMESPACE;
52             case Type.NODE:
53                 return AnyNodeTest.getInstance();
54             default:
55                 throw new IllegalArgumentException JavaDoc("Unknown node kind in NodeKindTest");
56         }
57     }
58
59     /**
60     * Test whether this node test is satisfied by a given node
61     * @param nodeKind The type of node to be matched
62      * @param fingerprint identifies the expanded name of the node to be matched
63      */

64
65     public boolean matches(int nodeKind, int fingerprint, int annotation) {
66         return (kind == nodeKind);
67     }
68
69     /**
70      * Test whether this node test is satisfied by a given node on a TinyTree. The node
71      * must be a document, element, text, comment, or processing instruction node.
72      * This method is provided
73      * so that when navigating a TinyTree a node can be rejected without
74      * actually instantiating a NodeInfo object.
75      *
76      * @param tree the TinyTree containing the node
77      * @param nodeNr the number of the node within the TinyTree
78      * @return true if the node matches the NodeTest, otherwise false
79      */

80
81     public boolean matches(TinyTree tree, int nodeNr) {
82         return tree.getNodeKind(nodeNr) == kind;
83     }
84
85     /**
86      * Test whether this node test is satisfied by a given node. This alternative
87      * method is used in the case of nodes where calculating the fingerprint is expensive,
88      * for example DOM or JDOM nodes.
89      * @param node the node to be matched
90      */

91
92     public boolean matches(NodeInfo node) {
93         return node.getNodeKind() == kind;
94     }
95
96
97     /**
98     * Determine the default priority of this node test when used on its own as a Pattern
99     */

100
101     public final double getDefaultPriority() {
102         return -0.5;
103     }
104
105     /**
106     * Determine the types of nodes to which this pattern applies. Used for optimisation.
107     * @return the type of node matched by this pattern. e.g. Type.ELEMENT or Type.TEXT
108     */

109
110     public int getPrimitiveType() {
111         return kind;
112     }
113
114     /**
115      * Get a mask indicating which kinds of nodes this NodeTest can match. This is a combination
116      * of bits: 1<<Type.ELEMENT for element nodes, 1<<Type.TEXT for text nodes, and so on.
117      */

118
119     public int getNodeKindMask() {
120         return 1<<kind;
121     }
122
123     /**
124      * Get the content type allowed by this NodeTest (that is, the type annotation of the matched nodes).
125      * Return AnyType if there are no restrictions. The default implementation returns AnyType.
126      */

127
128     public SchemaType getContentType() {
129         if (kind == Type.ATTRIBUTE) {
130             return AnySimpleType.getInstance();
131         } else {
132             return super.getContentType();
133         }
134     }
135
136     public String JavaDoc toString() {
137         return toString(kind);
138     }
139
140     public static String JavaDoc toString(int kind) {
141             switch (kind) {
142                 case Type.DOCUMENT:
143                     return("document-node()" );
144                 case Type.ELEMENT:
145                     return( "element()" );
146                 case Type.ATTRIBUTE:
147                     return( "attribute()" );
148                 case Type.COMMENT:
149                     return( "comment()" );
150                 case Type.TEXT:
151                     return( "text()" );
152                 case Type.PROCESSING_INSTRUCTION:
153                     return( "processing-instruction()" );
154                 case Type.NAMESPACE:
155                     return( "namespace()" );
156                 default:
157                     return( "** error **");
158             }
159
160     }
161
162     /**
163       * Returns a hash code value for the object.
164       */

165
166      public int hashCode() {
167          return kind;
168      }
169
170 }
171
172 //
173
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
174
// you may not use this file except in compliance with the License. You may obtain a copy of the
175
// License at http://www.mozilla.org/MPL/
176
//
177
// Software distributed under the License is distributed on an "AS IS" basis,
178
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
179
// See the License for the specific language governing rights and limitations under the License.
180
//
181
// The Original Code is: all this file.
182
//
183
// The Initial Developer of the Original Code is Michael H. Kay.
184
//
185
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
186
//
187
// Contributor(s): none.
188
//
189
Popular Tags