KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.pattern;
2 import net.sf.saxon.om.Axis;
3 import net.sf.saxon.om.AxisIterator;
4 import net.sf.saxon.om.NodeInfo;
5 import net.sf.saxon.om.NamePool;
6 import net.sf.saxon.type.Type;
7 import net.sf.saxon.tinytree.TinyTree;
8
9 /**
10   * A DocumentNodeTest implements the test document-node(element(~,~))
11   */

12
13 // This is messy because the standard interface for a NodeTest does not allow
14
// any navigation from the node in question - it only tests for the node kind,
15
// node name, and type annotation of the node.
16

17 public class DocumentNodeTest extends NodeTest {
18
19
20     private NodeTest elementTest;
21
22     public DocumentNodeTest(NodeTest elementTest) {
23         this.elementTest = elementTest;
24     }
25
26     /**
27     * Test whether this node test is satisfied by a given node
28     * @param nodeKind 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 nodeKind, int fingerprint, int annotation) {
33         throw new UnsupportedOperationException JavaDoc("DocumentNodeTest doesn't support this method");
34     }
35
36     /**
37      * Test whether this node test is satisfied by a given node on a TinyTree. The node
38      * must be a document, element, text, comment, or processing instruction node.
39      * This method is provided
40      * so that when navigating a TinyTree a node can be rejected without
41      * actually instantiating a NodeInfo object.
42      *
43      * @param tree the TinyTree containing the node
44      * @param nodeNr the number of the node within the TinyTree
45      * @return true if the node matches the NodeTest, otherwise false
46      */

47
48     public boolean matches(TinyTree tree, int nodeNr) {
49         if (tree.getNodeKind(nodeNr) != Type.DOCUMENT) {
50             return false;
51         }
52         return matches(tree.getNode(nodeNr));
53     }
54
55     /**
56     * Determine whether this Pattern matches the given Node.
57     * @param node The NodeInfo representing the Element or other node to be tested against the Pattern
58     * uses variables, or contains calls on functions such as document() or key().
59     * @return true if the node matches the Pattern, false otherwise
60     */

61
62     public boolean matches(NodeInfo node) {
63         if (node.getNodeKind() != Type.DOCUMENT) {
64             return false;
65         }
66         AxisIterator iter = node.iterateAxis(Axis.CHILD);
67         // The match is true if there is exactly one element node child, no text node
68
// children, and the element node matches the element test.
69
boolean found = false;
70         while (true) {
71             NodeInfo n = (NodeInfo)iter.next();
72             if (n==null) {
73                 return found;
74             }
75             int kind = n.getNodeKind();
76             if (kind==Type.TEXT) {
77                 return false;
78             } else if (kind==Type.ELEMENT) {
79                 if (found) {
80                     return false;
81                 }
82                 if (elementTest.matchesItem(n)) {
83                     found = true;
84                 } else {
85                     return false;
86                 }
87             }
88         }
89     }
90
91     /**
92     * Determine the default priority of this node test when used on its own as a Pattern
93     */

94
95     public final double getDefaultPriority() {
96         return elementTest.getDefaultPriority();
97     }
98
99     /**
100     * Determine the types of nodes to which this pattern applies. Used for optimisation.
101     * @return the type of node matched by this pattern. e.g. Type.ELEMENT or Type.TEXT
102     */

103
104     public int getPrimitiveType() {
105         return Type.DOCUMENT;
106     }
107
108     /**
109      * Get a mask indicating which kinds of nodes this NodeTest can match. This is a combination
110      * of bits: 1<<Type.ELEMENT for element nodes, 1<<Type.TEXT for text nodes, and so on.
111      */

112
113     public int getNodeKindMask() {
114         return 1<<Type.DOCUMENT;
115     }
116
117     /**
118      * Get the element test contained within this document test
119      * @return the contained element test
120      */

121
122     public NodeTest getElementTest() {
123         return elementTest;
124     }
125
126     public String JavaDoc toString(NamePool pool) {
127         return "document-node(" + elementTest.toString(pool) + ')';
128     }
129
130     public String JavaDoc toString() {
131         return "document-node(" + elementTest.toString() + ')';
132     }
133
134     /**
135       * Returns a hash code value for the object.
136       */

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