Debian Codegraph @

net/sf/saxon/tree/wrapper/AbstractVirtualNode.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.tree.wrapper;
9
10 import net.sf.saxon.expr.parser.Location;
11 import net.sf.saxon.om.*;
12 import net.sf.saxon.pattern.NodeTest;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.tree.iter.AxisIterator;
15 import net.sf.saxon.tree.util.FastStringBuffer;
16 import net.sf.saxon.tree.util.Navigator;
17 import net.sf.saxon.type.SchemaType;
18
19
20 /**
21 * AbstractVirtualNode is an abstract superclass for VirtualNode implementations in which
22 * the underlying node is itself a Saxon NodeInfo.
23 */
24
25 public abstract class AbstractVirtualNode implements VirtualNode {
26
27 protected NodeInfo node;
28 /*@Nullable*/ protected AbstractVirtualNode parent; // null means unknown
29 protected TreeInfo docWrapper;
30
31 /**
32 * Get information about the tree to which this NodeInfo belongs
33 *
34 * @return the TreeInfo
35 * @since 9.7
36 */
37 public TreeInfo getTreeInfo() {
38 return docWrapper;
39 }
40
41 /**
42 * Get the underlying node, to implement the VirtualNode interface
43 */
44
45 public NodeInfo getUnderlyingNode() {
46 return node;
47 }
48
49 /**
50 * Get the fingerprint of the node
51 *
52 * @return the node's fingerprint, or -1 for an unnamed node
53 * @throws UnsupportedOperationException if this method is called for a node where
54 * hasFingerprint() returns false;
55 */
56 @Override
57 public int getFingerprint() {
58 if (node.hasFingerprint()) {
59 return node.getFingerprint();
60 } else {
61 throw new UnsupportedOperationException();
62 }
63 }
64
65 /**
66 * Test whether a fingerprint is available for the node name
67 */
68 @Override
69 public boolean hasFingerprint() {
70 return node.hasFingerprint();
71 }
72
73 /**
74 * Get the node underlying this virtual node. If this is a VirtualNode the method
75 * will automatically drill down through several layers of wrapping.
76 *
77 * @return The underlying node.
78 */
79
80 public Object getRealNode() {
81 Object u = this;
82 do {
83 u = ((VirtualNode) u).getUnderlyingNode();
84 } while (u instanceof VirtualNode);
85 return u;
86 }
87
88 /**
89 * Return the type of node.
90 *
91 * @return one of the values Node.ELEMENT, Node.TEXT, Node.ATTRIBUTE, etc.
92 */
93
94 public int getNodeKind() {
95 return node.getNodeKind();
96 }
97
98 /**
99 * Get the typed value.
100 *
101 * @return the typed value. If requireSingleton is set to true, the result will always be an
102 * AtomicValue. In other cases it may be a Value representing a sequence whose items are atomic
103 * values.
104 * @since 8.5
105 */
106
107 public AtomicSequence atomize() throws XPathException {
108 // The default implementation atomizes the base node
109 return node.atomize();
110 }
111
112 /**
113 * Get the type annotation
114 *
115 * @return the type annotation of the base node
116 */
117
118 public SchemaType getSchemaType() {
119 return node.getSchemaType();
120 }
121
122 /**
123 * The equals() method compares nodes for identity. It is defined to give the same result
124 * as isSameNodeInfo().
125 *
126 * @param other the node to be compared with this node
127 * @return true if this NodeInfo object and the supplied NodeInfo object represent
128 * the same node in the tree.
129 * @since 8.7 Previously, the effect of the equals() method was not defined. Callers
130 * should therefore be aware that third party implementations of the NodeInfo interface may
131 * not implement the correct semantics. It is safer to use isSameNodeInfo() for this reason.
132 * The equals() method has been defined because it is useful in contexts such as a Java Set or HashMap.
133 */
134
135 public boolean equals(Object other) {
136 if (other instanceof AbstractVirtualNode) {
137 return node.equals(((AbstractVirtualNode) other).node);
138 } else {
139 return node.equals(other);
140 }
141 }
142
143 /**
144 * The hashCode() method obeys the contract for hashCode(): that is, if two objects are equal
145 * (represent the same node) then they must have the same hashCode()
146 *
147 * @since 8.7 Previously, the effect of the equals() and hashCode() methods was not defined. Callers
148 * should therefore be aware that third party implementations of the NodeInfo interface may
149 * not implement the correct semantics.
150 */
151
152 public int hashCode() {
153 return node.hashCode() ^ 0x3c3c3c3c;
154 }
155
156 /**
157 * Get the System ID for the node.
158 *
159 * @return the System Identifier of the entity in the source document containing the node,
160 * or null if not known. Note this is not the same as the base URI: the base URI can be
161 * modified by xml:base, but the system ID cannot.
162 */
163
164 /*@Nullable*/
165 public String getSystemId() {
166 return node.getSystemId();
167 }
168
169 public void setSystemId(String uri) {
170 node.setSystemId(uri);
171 }
172
173 /**
174 * Get the Base URI for the node, that is, the URI used for resolving a relative URI contained
175 * in the node. In the JDOM model, base URIs are held only an the document level. We don't
176 * currently take any account of xml:base attributes.
177 */
178
179 public String getBaseURI() {
180 return node.getBaseURI();
181 }
182
183 /**
184 * Get line number
185 *
186 * @return the line number of the node in its original source document; or -1 if not available
187 */
188
189 public int getLineNumber() {
190 return node.getLineNumber();
191 }
192
193 /**
194 * Get column number
195 *
196 * @return the column number of the node in its original source document; or -1 if not available
197 */
198
199 public int getColumnNumber() {
200 return node.getColumnNumber();
201 }
202
203 /**
204 * Get an immutable copy of this Location object. By default Location objects may be mutable, so they
205 * should not be saved for later use. The result of this operation holds the same location information,
206 * but in an immutable form.
207 */
208 public Location saveLocation() {
209 return this;
210 }
211
212 /**
213 * Determine the relative position of this node and another node, in document order.
214 * The other node will always be in the same document.
215 *
216 * @param other The other node, whose position is to be compared with this node
217 * @return -1 if this node precedes the other node, +1 if it follows the other
218 * node, or 0 if they are the same node. (In this case, isSameNode() will always
219 * return true, and the two nodes will produce the same result for generateId())
220 */
221
222 public int compareOrder(/*@NotNull*/ NodeInfo other) {
223 if (other instanceof AbstractVirtualNode) {
224 return node.compareOrder(((AbstractVirtualNode) other).node);
225 } else {
226 return node.compareOrder(other);
227 }
228 }
229
230 /**
231 * Return the string value of the node. The interpretation of this depends on the type
232 * of node. For an element it is the accumulated character content of the element,
233 * including descendant elements.
234 *
235 * @return the string value of the node
236 */
237
238 public final String getStringValue() {
239 return getStringValueCS().toString();
240 }
241
242 /**
243 * Get the value of the item as a CharSequence. This is in some cases more efficient than
244 * the version of the method that returns a String.
245 */
246
247 public CharSequence getStringValueCS() {
248 // default implementation returns the string value of the base node
249 return node.getStringValueCS();
250 }
251
252 /**
253 * Get the local part of the name of this node. This is the name after the ":" if any.
254 *
255 * @return the local part of the name. For an unnamed node, returns null, except for
256 * un unnamed namespace node, which returns "".
257 */
258
259 public String getLocalPart() {
260 return node.getLocalPart();
261 }
262
263 /**
264 * Get the URI part of the name of this node. This is the URI corresponding to the
265 * prefix, or the URI of the default namespace if appropriate.
266 *
267 * @return The URI of the namespace of this node. For an unnamed node, return null.
268 * For a node with an empty prefix, return an empty string.
269 */
270
271 public String getURI() {
272 return node.getURI();
273 }
274
275 /**
276 * Get the prefix of the name of the node. This is defined only for elements and attributes.
277 * If the node has no prefix, or for other kinds of node, return a zero-length string.
278 *
279 * @return The prefix of the name of the node.
280 */
281
282 public String getPrefix() {
283 return node.getPrefix();
284 }
285
286 /**
287 * Get the display name of this node. For elements and attributes this is [prefix:]localname.
288 * For unnamed nodes, it is an empty string.
289 *
290 * @return The display name of this node.
291 * For a node with no name, return an empty string.
292 */
293
294 public String getDisplayName() {
295 return node.getDisplayName();
296 }
297
298 /**
299 * Return an iteration over the nodes reached by the given axis from this node
300 *
301 * @param axisNumber the axis to be used
302 * @param nodeTest A pattern to be matched by the returned nodes
303 * @return a SequenceIterator that scans the nodes reached by the axis in turn.
304 */
305
306 /*@NotNull*/
307 public AxisIterator iterateAxis(byte axisNumber, NodeTest nodeTest) {
308 return new Navigator.AxisFilter(iterateAxis(axisNumber), nodeTest);
309 }
310
311 /**
312 * Get the string value of a given attribute of this node
313 *
314 * @param uri the namespace URI of the attribute name. Supply the empty string for an attribute
315 * that is in no namespace
316 * @param local the local part of the attribute name.
317 * @return the attribute value if it exists, or null if it does not exist. Always returns null
318 * if this node is not an element.
319 * @since 9.4
320 */
321 public String getAttributeValue(/*@NotNull*/ String uri, /*@NotNull*/ String local) {
322 return node.getAttributeValue(uri, local);
323 }
324
325 /**
326 * Get the root node
327 *
328 * @return the NodeInfo representing the root of the tree
329 */
330
331 public NodeInfo getRoot() {
332 NodeInfo p = this;
333 while (true) {
334 NodeInfo q = p.getParent();
335 if (q == null) {
336 return p;
337 }
338 p = q;
339 }
340 }
341
342 /**
343 * Determine whether the node has any children.
344 * <p>Note: the result is equivalent to
345 * <code>getEnumeration(Axis.CHILD, AnyNodeTest.getInstance()).hasNext()</code></p>
346 */
347
348 public boolean hasChildNodes() {
349 return node.hasChildNodes();
350 }
351
352 /**
353 * Get a character string that uniquely identifies this node.
354 * Note: a.isSameNode(b) if and only if generateId(a)==generateId(b)
355 *
356 * @param buffer a buffer, into which will be placed
357 * a string that uniquely identifies this node, within this
358 * document. The calling code prepends information to make the result
359 * unique across all documents.
360 */
361
362 public void generateId(FastStringBuffer buffer) {
363 // Note: giving the node the same ID as its underlying node is slightly questionable; depends on usage
364 node.generateId(buffer);
365 }
366
367 /**
368 * Get all namespace undeclarations and undeclarations defined on this element.
369 *
370 * @param buffer If this is non-null, and the result array fits in this buffer, then the result
371 * may overwrite the contents of this array, to avoid the cost of allocating a new array on the heap.
372 * @return An array of integers representing the namespace declarations and undeclarations present on
373 * this element. For a node other than an element, return null. Otherwise, the returned array is a
374 * sequence of namespace codes, whose meaning may be interpreted by reference to the name pool. The
375 * top half word of each namespace code represents the prefix, the bottom half represents the URI.
376 * If the bottom half is zero, then this is a namespace undeclaration rather than a declaration.
377 * The XML namespace is never included in the list. If the supplied array is larger than required,
378 * then the first unused entry will be set to -1.
379 * <p>For a node other than an element, the method returns null.</p>
380 */
381
382 public NamespaceBinding[] getDeclaredNamespaces(NamespaceBinding[] buffer) {
383 return node.getDeclaredNamespaces(buffer);
384 }
385
386
387 /**
388 * Determine whether this node has the is-id property
389 *
390 * @return true if the node is an ID
391 */
392
393 public boolean isId() {
394 return node.isId();
395 }
396
397 /**
398 * Determine whether this node has the is-idref property
399 *
400 * @return true if the node is an IDREF or IDREFS element or attribute
401 */
402
403 public boolean isIdref() {
404 return node.isIdref();
405 }
406
407 /**
408 * Determine whether the node has the is-nilled property
409 *
410 * @return true if the node has the is-nilled property
411 */
412
413 public boolean isNilled() {
414 return node.isNilled();
415 }
416 }
417