Debian Codegraph @

net/sf/saxon/xpath/JAXPVariableReference.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.xpath;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.expr.*;
12 import net.sf.saxon.expr.parser.RebindingMap;
13 import net.sf.saxon.om.Sequence;
14 import net.sf.saxon.om.SequenceIterator;
15 import net.sf.saxon.om.StructuredQName;
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.ItemType;
20 import net.sf.saxon.value.EmptySequence;
21
22 import javax.xml.xpath.XPathVariableResolver;
23
24
25 /**
26 * This class represents a variable in an XPath expression compiled using the JAXP XPath API.
27 * Although the class name suggests otherwise, the expression is not in fact a VariableReference;
28 * it's a custom expression which, on evaluation, calls the JAXP XPathVariableResolver to get the
29 * value of the variable.
30 */
31
32 // See bug 2554 which motivated a redesign for Saxon 9.7.0.2
33
34 public class JAXPVariableReference extends Expression implements Callable {
35
36 private StructuredQName name;
37 private XPathVariableResolver resolver;
38
39
40 /**
41 * Create the expression
42 */
43
44 public JAXPVariableReference(StructuredQName name, XPathVariableResolver resolver) {
45 this.name = name;
46 this.resolver = resolver;
47 }
48
49 /**
50 * Get a name identifying the kind of expression, in terms meaningful to a user.
51 *
52 * @return a name identifying the kind of expression, in terms meaningful to a user.
53 * The name will always be in the form of a lexical XML QName, and should match the name used
54 * in explain() output displaying the expression.
55 */
56
57 public String getExpressionName() {
58 return "$" + name.getDisplayName();
59 }
60
61 /**
62 * Create a clone copy of this expression
63 *
64 * @return a copy of this expression
65 * @param rebindings
66 */
67
68 /*@NotNull*/
69 public Expression copy(RebindingMap rebindings) {
70 return new JAXPVariableReference(name, resolver);
71 }
72
73 /**
74 * Determine the item type
75 */
76
77 /*@NotNull*/
78 public ItemType getItemType() {
79 return AnyItemType.getInstance();
80 }
81
82 /**
83 * Get the static cardinality
84 */
85
86 public int computeCardinality() {
87 return StaticProperty.ALLOWS_ZERO_OR_MORE;
88 }
89
90 /**
91 * Determine the special properties of this expression
92 *
93 * @return the value {@link StaticProperty#NO_NODES_NEWLY_CREATED}
94 */
95
96 public int computeSpecialProperties() {
97 return StaticProperty.NO_NODES_NEWLY_CREATED;
98 }
99
100 @Override
101 public int getImplementationMethod() {
102 return ITERATE_METHOD;
103 }
104
105 /**
106 * Is this expression the same as another expression?
107 */
108
109 public boolean equals(Object other) {
110 return other instanceof JAXPVariableReference &&
111 ((JAXPVariableReference)other).name.equals(name) &&
112 ((JAXPVariableReference)other).resolver == resolver;
113 }
114
115 /**
116 * get HashCode for comparing two expressions
117 */
118
119 public int computeHashCode() {
120 return name.hashCode();
121 }
122
123 @Override
124 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
125 Configuration config = context.getConfiguration();
126 Object value = resolver.resolveVariable(name.toJaxpQName());
127 if (value == null) {
128 return EmptySequence.getInstance();
129 }
130 JPConverter converter = JPConverter.allocate(value.getClass(), null, config);
131 return converter.convert(value, context);
132 }
133
134 /**
135 * Iterate over the value of the expression
136 */
137
138 /*@NotNull*/
139 public SequenceIterator<?> iterate(XPathContext context) throws XPathException {
140 return call(context, null).iterate();
141 }
142
143 /**
144 * The toString() method for an expression attempts to give a representation of the expression
145 * in an XPath-like form, but there is no guarantee that the syntax will actually be true XPath.
146 * In the case of XSLT instructions, the toString() method gives an abstracted view of the syntax
147 */
148
149 public String toString() {
150 return getExpressionName();
151 }
152
153 /**
154 * Diagnostic print of expression structure. The abstract expression tree
155 * is written to the supplied output destination.
156 */
157
158 public void export(ExpressionPresenter destination) throws XPathException {
159 destination.startElement("jaxpVar", this);
160 destination.emitAttribute("name", name);
161 destination.endElement();
162 }
163
164 }
165