Debian Codegraph @

net/sf/saxon/pattern/NodeTestPattern.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.expr.StaticProperty;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.expr.parser.ExpressionTool;
13 import net.sf.saxon.expr.parser.RebindingMap;
14 import net.sf.saxon.om.Item;
15 import net.sf.saxon.om.NodeInfo;
16 import net.sf.saxon.trace.ExpressionPresenter;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.type.AnyItemType;
19 import net.sf.saxon.type.SchemaDeclaration;
20 import net.sf.saxon.type.UType;
21 import net.sf.saxon.value.SequenceType;
22
23 /**
24 * A NodeTestPattern is a pattern that consists simply of a NodeTest
25 *
26 * @author Michael H. Kay
27 */
28
29 public class NodeTestPattern extends Pattern {
30
31 private NodeTest nodeTest;
32
33
34 /**
35 * Create an NodeTestPattern that matches all items of a given type
36 *
37 * @param test the type that the items must satisfy for the pattern to match
38 */
39
40 public NodeTestPattern(NodeTest test) {
41 nodeTest = test;
42 setPriority(test.getDefaultPriority());
43 }
44
45 /**
46 * Determine whether this Pattern matches the given Node. This is the main external interface
47 * for matching patterns: it sets current() to the node being tested
48 *
49 * @param item The item to be tested against the Pattern
50 * @param context The context in which the match is to take place.
51 * @return true if the item matches the Pattern, false otherwise
52 */
53
54 public boolean matches(Item<?> item, XPathContext context) {
55 return item instanceof NodeInfo && nodeTest.matchesNode((NodeInfo)item);
56 }
57
58 /**
59 * Get a NodeTest that all the nodes matching this pattern must satisfy
60 */
61
62 public NodeTest getItemType() {
63 return nodeTest;
64 }
65
66 /**
67 * Get a UType indicating which kinds of items this Pattern can match.
68 *
69 * @return a UType indicating all the primitive types of item that the pattern can match.
70 */
71 @Override
72 public UType getUType() {
73 return nodeTest.getUType();
74 }
75
76 /**
77 * Determine the name fingerprint of nodes to which this pattern applies. Used for
78 * optimisation.
79 *
80 * @return A fingerprint that the nodes must match, or -1 if it can match multiple fingerprints
81 */
82
83 public int getFingerprint() {
84 return nodeTest.getFingerprint();
85 }
86
87 /**
88 * Display the pattern for diagnostics
89 */
90
91 public String toString() {
92 return nodeTest.toString();
93 }
94
95 public String toShortString() {
96 return nodeTest.toShortString();
97 }
98
99 /**
100 * Determine whether this pattern is the same as another pattern
101 *
102 * @param other the other object
103 */
104
105 public boolean equals(/*@NotNull*/ Object other) {
106 return (other instanceof NodeTestPattern) &&
107 ((NodeTestPattern) other).nodeTest.equals(nodeTest);
108 }
109
110 /**
111 * Hashcode supporting equals()
112 */
113
114 public int computeHashCode() {
115 return 0x7aeffea8 ^ nodeTest.hashCode();
116 }
117
118 /**
119 * Convert the pattern to a typed pattern, in which an element name is treated as
120 * schema-element(N)
121 *
122 * @param val either "strict" or "lax" depending on the value of xsl:mode/@typed
123 * @return either the original pattern unchanged, or a new pattern as the result of the
124 * conversion
125 */
126 @Override
127 public Pattern convertToTypedPattern(String val) throws XPathException {
128 if (nodeTest instanceof NameTest && nodeTest.getUType() == UType.ELEMENT) {
129 SchemaDeclaration decl = getConfiguration().getElementDeclaration(nodeTest.getMatchingNodeName());
130 if (decl == null) {
131 if ("lax".equals(val)) {
132 return this;
133 } else {
134 // See spec bug 25517
135 throw new XPathException("The mode specifies typed='strict', " +
136 "but there is no schema element declaration named " + nodeTest, "XTSE3105");
137 }
138 } else {
139 NodeTest schemaNodeTest = decl.makeSchemaNodeTest();
140 return new NodeTestPattern(schemaNodeTest);
141 }
142 } else {
143 return this;
144 }
145 }
146
147 public void export(ExpressionPresenter presenter) throws XPathException {
148 presenter.startElement("p.nodeTest");
149 presenter.emitAttribute("test", nodeTest.toString());
150 ExpressionPresenter.ExportOptions options = (ExpressionPresenter.ExportOptions) presenter.getOptions();
151 if ("JS".equals(options.target)) {
152 int targetVersion = options.targetVersion;
153 try {
154 presenter.emitAttribute("jsTest", nodeTest.generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion));
155 } catch (XPathException e) {
156 e.maybeSetLocation(getLocation());
157 throw e;
158 }
159 }
160 if (options.addStaticType) {
161 SequenceType type = SequenceType.makeSequenceType(nodeTest, StaticProperty.ALLOWS_ONE);
162 presenter.emitAttribute("sType", type.toExportString2());
163 }
164 presenter.endElement();
165 }
166
167 /**
168 * Copy a pattern. This makes a deep copy.
169 *
170 * @return the copy of the original pattern
171 * @param rebindings variables that need to be re-bound
172 */
173
174 /*@NotNull*/
175 public Pattern copy(RebindingMap rebindings) {
176 NodeTestPattern n = new NodeTestPattern(nodeTest.copy());
177 n.setPriority(getDefaultPriority());
178 ExpressionTool.copyLocationInfo(this, n);
179 return n;
180 }
181
182 public NodeTest getNodeTest() {
183 return nodeTest;
184 }
185 }
186