Debian Codegraph @

net/sf/saxon/pattern/DocumentNodeTest.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.pattern;
9
10 import net.sf.saxon.om.AxisInfo;
11 import net.sf.saxon.om.Item;
12 import net.sf.saxon.om.NodeInfo;
13 import net.sf.saxon.om.NodeName;
14 import net.sf.saxon.trans.Err;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.tree.iter.AxisIterator;
17 import net.sf.saxon.type.*;
18
19 import java.util.Optional;
20
21 /**
22 * A DocumentNodeTest implements the test document-node(element(~,~))
23 */
24
25 // This is messy because the standard interface for a NodeTest does not allow
26 // any navigation from the node in question - it only tests for the node kind,
27 // node name, and type annotation of the node.
28
29 public class DocumentNodeTest extends NodeTest {
30
31
32 private NodeTest elementTest;
33
34 public DocumentNodeTest(NodeTest elementTest) {
35 this.elementTest = elementTest;
36 }
37
38 /**
39 * Get the corresponding {@link net.sf.saxon.type.UType}. A UType is a union of primitive item
40 * types.
41 *
42 * @return the smallest UType that subsumes this item type
43 */
44 public UType getUType() {
45 return UType.DOCUMENT;
46 }
47
48 /**
49 * Test whether this node test is satisfied by a given node. This method is only
50 * fully supported for a subset of NodeTests, because it doesn't provide all the information
51 * needed to evaluate all node tests. In particular (a) it can't be used to evaluate a node
52 * test of the form element(N,T) or schema-element(E) where it is necessary to know whether the
53 * node is nilled, and (b) it can't be used to evaluate a node test of the form
54 * document-node(element(X)). This in practice means that it is used (a) to evaluate the
55 * simple node tests found in the XPath 1.0 subset used in XML Schema, and (b) to evaluate
56 * node tests where the node kind is known to be an attribute.
57 *
58 * @param nodeKind The kind of node to be matched
59 * @param name identifies the expanded name of the node to be matched.
60 * The value should be null for a node with no name.
61 * @param annotation The actual content type of the node
62 */
63 @Override
64 public boolean matches(int nodeKind, NodeName name, SchemaType annotation) {
65 if (nodeKind != Type.DOCUMENT) {
66 return false;
67 }
68 throw new UnsupportedOperationException("DocumentNodeTest doesn't support this method");
69 }
70
71 /**
72 * Determine whether this Pattern matches the given Node.
73 *
74 * @param node The NodeInfo representing the Element or other node to be tested against the Pattern
75 * uses variables, or contains calls on functions such as document() or key().
76 * @return true if the node matches the Pattern, false otherwise
77 */
78
79 public boolean matchesNode(NodeInfo node) {
80 if (node.getNodeKind() != Type.DOCUMENT) {
81 return false;
82 }
83 AxisIterator iter = node.iterateAxis(AxisInfo.CHILD);
84 // The match is true if there is exactly one element node child, no text node
85 // children, and the element node matches the element test.
86 boolean found = false;
87 NodeInfo n;
88 while ((n = iter.next()) != null) {
89 int kind = n.getNodeKind();
90 if (kind == Type.TEXT) {
91 return false;
92 } else if (kind == Type.ELEMENT) {
93 if (found) {
94 return false;
95 }
96 if (elementTest.matchesNode(n)) {
97 found = true;
98 } else {
99 return false;
100 }
101 }
102 }
103 return found;
104 }
105
106 /**
107 * Determine the default priority of this node test when used on its own as a Pattern
108 */
109
110 public final double getDefaultPriority() {
111 return elementTest.getDefaultPriority();
112 }
113
114 /**
115 * Determine the types of nodes to which this pattern applies. Used for optimisation.
116 *
117 * @return the type of node matched by this pattern. e.g. Type.ELEMENT or Type.TEXT
118 */
119
120 public int getPrimitiveType() {
121 return Type.DOCUMENT;
122 }
123
124 /**
125 * Get the element test contained within this document test
126 *
127 * @return the contained element test
128 */
129
130 public NodeTest getElementTest() {
131 return elementTest;
132 }
133
134 public String toString() {
135 return "document-node(" + elementTest + ')';
136 }
137
138 /**
139 * Returns a hash code value for the object.
140 */
141
142 public int hashCode() {
143 return elementTest.hashCode() ^ 12345;
144 }
145
146 public boolean equals(/*@NotNull*/ Object other) {
147 return other instanceof DocumentNodeTest &&
148 ((DocumentNodeTest) other).elementTest.equals(elementTest);
149 }
150
151 /**
152 * Generate Javascript code to test whether an item conforms to this item type
153 * @param knownToBe a type that the item is known to conform to, without further testing
154 * @param targetVersion the Saxon-JS version being targeted
155 * @return a Javascript instruction or sequence of instructions, which can be used as the body
156 * of a Javascript function, and which returns a boolean indication whether the value of the
157 * variable "item" is an instance of this item type.
158 *
159 */
160 @Override
161 public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) throws XPathException {
162 String elTest = "function e(item) {" + elementTest.generateJavaScriptItemTypeTest(NodeKindTest.ELEMENT, targetVersion) + "};";
163 if (targetVersion == 1) {
164 return elTest + "return SaxonJS.U.isNode(item) && (item.nodeType===9 || item.nodeType===11) && " +
165 "SaxonJS.U.Axis.child(item).filter(e).next();";
166 } else {
167 return elTest + "return SaxonJS.U.isConstrainedDocumentNode(item, e);";
168 }
169 }
170
171 /**
172 * Get extra diagnostic information about why a supplied item does not conform to this
173 * item type, if available. If extra information is returned, it should be in the form of a complete
174 * sentence, minus the closing full stop. No information should be returned for obvious cases.
175 *
176 * @param item the item that doesn't match this type
177 * @param th the type hierarchy cache
178 * @return optionally, a message explaining why the item does not match the type
179 */
180 @Override
181 public Optional<String> explainMismatch(Item item, TypeHierarchy th) {
182 Optional<String> explanation = super.explainMismatch(item, th);
183 if (explanation.isPresent()) {
184 return explanation;
185 }
186 NodeInfo node = (NodeInfo) item;
187 AxisIterator iter = node.iterateAxis(AxisInfo.CHILD);
188 // The match is true if there is exactly one element node child, no text node
189 // children, and the element node matches the element test.
190 boolean found = false;
191 NodeInfo n;
192 while ((n = iter.next()) != null) {
193 int kind = n.getNodeKind();
194 if (kind == Type.TEXT) {
195 return Optional.of("The supplied document node has text node children");
196 } else if (kind == Type.ELEMENT) {
197 if (found) {
198 return Optional.of("The supplied document node has more than one element child");
199 }
200 if (elementTest.matchesNode(n)) {
201 found = true;
202 } else {
203 String s = "The supplied document node has an element child (" + Err.depict(n) +
204 ") that does not satisfy the element test";
205 Optional<String> more = elementTest.explainMismatch(n, th);
206 if (more.isPresent()) {
207 s += ". " + more.get();
208 }
209 return Optional.of(s);
210 }
211 }
212 }
213 return Optional.empty();
214 }
215
216 }
217