| 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.NodeInfo; |
| 11 | import net.sf.saxon.om.NodeName; |
| 12 | import net.sf.saxon.om.StructuredQName; |
| 13 | import net.sf.saxon.tree.tiny.NodeVectorTree; |
| 14 | import net.sf.saxon.type.ItemType; |
| 15 | import net.sf.saxon.type.SchemaType; |
| 16 | import net.sf.saxon.type.Type; |
| 17 | import net.sf.saxon.type.UType; |
| 18 | |
| 19 | import java.util.function.IntPredicate; |
| 20 | |
| 21 | /** |
| 22 | * NodeTest is an interface that enables a test of whether a node has a particular |
| 23 | * name and type. An AnyNodeTest matches any node. |
| 24 | * |
| 25 | * @author Michael H. Kay |
| 26 | */ |
| 27 | |
| 28 | public final class AnyNodeTest extends NodeTest implements QNameTest { |
| 29 | |
| 30 | private static AnyNodeTest THE_INSTANCE = new AnyNodeTest(); |
| 31 | |
| 32 | /** |
| 33 | * Get an instance of AnyNodeTest |
| 34 | * @return the singleton instance of this class |
| 35 | */ |
| 36 | |
| 37 | public static AnyNodeTest getInstance() { |
| 38 | return THE_INSTANCE; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Private constructor |
| 43 | */ |
| 44 | |
| 45 | private AnyNodeTest() { |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get the corresponding {@link net.sf.saxon.type.UType}. A UType is a union of primitive item |
| 50 | * types. |
| 51 | * |
| 52 | * @return the smallest UType that subsumes this item type |
| 53 | */ |
| 54 | public UType getUType() { |
| 55 | return UType.ANY_NODE; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Test whether this node test is satisfied by a given node. This method is only |
| 60 | * fully supported for a subset of NodeTests, because it doesn't provide all the information |
| 61 | * needed to evaluate all node tests. In particular (a) it can't be used to evaluate a node |
| 62 | * test of the form element(N,T) or schema-element(E) where it is necessary to know whether the |
| 63 | * node is nilled, and (b) it can't be used to evaluate a node test of the form |
| 64 | * document-node(element(X)). This in practice means that it is used (a) to evaluate the |
| 65 | * simple node tests found in the XPath 1.0 subset used in XML Schema, and (b) to evaluate |
| 66 | * node tests where the node kind is known to be an attribute. |
| 67 | * |
| 68 | * @param nodeKind The kind of node to be matched |
| 69 | * @param name identifies the expanded name of the node to be matched. |
| 70 | * The value should be null for a node with no name. |
| 71 | * @param annotation The actual content type of the node |
| 72 | */ |
| 73 | @Override |
| 74 | public boolean matches(int nodeKind, NodeName name, SchemaType annotation) { |
| 75 | return nodeKind != Type.PARENT_POINTER; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public IntPredicate getMatcher(NodeVectorTree tree) { |
| 80 | final byte[] nodeKindArray = tree.getNodeKindArray(); |
| 81 | return nodeNr -> nodeKindArray[nodeNr] != Type.PARENT_POINTER; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Test whether this node test is satisfied by a given node. This alternative |
| 86 | * method is used in the case of nodes where calculating the fingerprint is expensive, |
| 87 | * for example DOM or JDOM nodes. |
| 88 | * |
| 89 | * @param node the node to be matched |
| 90 | */ |
| 91 | |
| 92 | public boolean matchesNode(NodeInfo node) { |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Test whether this QNameTest matches a given QName |
| 98 | * |
| 99 | * @param qname the QName to be matched |
| 100 | * @return true if the name matches, false if not |
| 101 | */ |
| 102 | |
| 103 | public boolean matches(StructuredQName qname) { |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Determine the default priority of this node test when used on its own as a Pattern |
| 109 | */ |
| 110 | |
| 111 | public final double getDefaultPriority() { |
| 112 | return -0.5; |
| 113 | } |
| 114 | |
| 115 | /*@NotNull*/ |
| 116 | public String toString() { |
| 117 | return "node()"; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Generate Javascript code to test if a name matches the test. |
| 122 | * |
| 123 | * @return JS code as a string. The generated code will be used |
| 124 | * as the body of a JS function in which the argument name "q" is an |
| 125 | * XdmQName object holding the name. The XdmQName object has properties |
| 126 | * uri and local. |
| 127 | * @param targetVersion The version of Saxon-JS being targeted |
| 128 | */ |
| 129 | public String generateJavaScriptNameTest(int targetVersion) { |
| 130 | return "true"; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Generate Javascript code to test whether an item conforms to this item type |
| 135 | * |
| 136 | * @return a Javascript instruction or sequence of instructions, which can be used as the body |
| 137 | * of a Javascript function, and which returns a boolean indication whether the value of the |
| 138 | * variable "item" is an instance of this item type. |
| 139 | * @param knownToBe An item type that the supplied item is known to conform to; the generated code |
| 140 | * can assume that the item is an instance of this type. |
| 141 | * @param targetVersion The version of Saxon-JS for which code is being generated. Currently either 1 or 2. |
| 142 | |
| 143 | */ |
| 144 | @Override |
| 145 | public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) { |
| 146 | return "return SaxonJS.U.isNode(item);"; |
| 147 | } |
| 148 | |
| 149 | |
| 150 | } |
| 151 |