KickJava   Java API By Example, From Geeks To Geeks.

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


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.Type;
5 import net.sf.saxon.tinytree.TinyTree;
6
7 import java.util.Set JavaDoc;
8
9 /**
10   * NodeTest is an interface that enables a test of whether a node has a particular
11   * name and type. A SubstitutionGroupTest matches element nodes whose name is one of
12   * a given set of names: it is used for KindTests of the form schema-element(N) where all
13   * elements in a substitution group are to be matched.
14   *
15   * @author Michael H. Kay
16   */

17
18 public class SubstitutionGroupTest extends NodeTest {
19
20     private int fingerprint;
21     private Set JavaDoc group;
22
23     /**
24      * Constructor
25      * @param group A HashSet containing Integer values representing the fingerprints
26      * of element names included in the substitution group
27      */

28
29     public SubstitutionGroupTest(int fingerprint, Set JavaDoc group) {
30         this.group = group;
31         this.fingerprint = fingerprint;
32     }
33
34    /**
35     * Test whether this node test is satisfied by a given node
36     * @param nodeKind The type of node to be matched
37     * @param nameCode identifies the expanded name of the node to be matched
38     */

39
40     public boolean matches(int nodeKind, int nameCode, int annotation) {
41         return nodeKind == Type.ELEMENT &&
42                 group.contains(new Integer JavaDoc(nameCode & NamePool.FP_MASK));
43     }
44
45     /**
46      * Test whether this node test is satisfied by a given node on a TinyTree. The node
47      * must be a document, element, text, comment, or processing instruction node.
48      * This method is provided
49      * so that when navigating a TinyTree a node can be rejected without
50      * actually instantiating a NodeInfo object.
51      *
52      * @param tree the TinyTree containing the node
53      * @param nodeNr the number of the node within the TinyTree
54      * @return true if the node matches the NodeTest, otherwise false
55      */

56
57     public boolean matches(TinyTree tree, int nodeNr) {
58         return tree.getNodeKind(nodeNr) == Type.ELEMENT &&
59                 group.contains(new Integer JavaDoc(tree.getNameCode(nodeNr) & NamePool.FP_MASK));
60     }
61
62     /**
63      * Test whether this node test is satisfied by a given node. This alternative
64      * method is used in the case of nodes where calculating the fingerprint is expensive,
65      * for example DOM or JDOM nodes.
66      * @param node the node to be matched
67      */

68
69     public boolean matches(NodeInfo node) {
70         return node.getNodeKind() == Type.ELEMENT &&
71                 group.contains(new Integer JavaDoc(node.getFingerprint()));
72     }
73     /**
74     * Determine the default priority of this node test when used on its own as a Pattern
75     */

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

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

95
96     public int getNodeKindMask() {
97         return 1<<Type.ELEMENT;
98     }
99
100     /**
101      * Get the set of node names allowed by this NodeTest. This is returned as a set of Integer fingerprints.
102      * A null value indicates that all names are permitted (i.e. that there are no constraints on the node name.
103      * The default implementation returns null.
104      */

105
106     public Set JavaDoc getRequiredNodeNames() {
107         return group;
108     }
109
110     /**
111      * Get the fingerprint of the head of the substitution group
112      * @return the fingerprint of the head of the substitution group
113      */

114
115     public int getHeadFingerprint() {
116         return fingerprint;
117     }
118
119     public String JavaDoc toString(NamePool pool) {
120         return "schema-element(" + pool.getDisplayName(fingerprint) + ')';
121     }
122
123     /**
124       * Returns a hash code value for the object.
125       */

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