Debian Codegraph @

net/sf/saxon/tree/NamespaceNode.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;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.event.Receiver;
12 import net.sf.saxon.event.ReceiverOptions;
13 import net.sf.saxon.expr.parser.Location;
14 import net.sf.saxon.om.*;
15 import net.sf.saxon.pattern.NodeTest;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.tree.iter.*;
18 import net.sf.saxon.tree.util.FastStringBuffer;
19 import net.sf.saxon.tree.util.NamespaceIterator;
20 import net.sf.saxon.tree.util.Navigator;
21 import net.sf.saxon.type.BuiltInAtomicType;
22 import net.sf.saxon.type.SchemaType;
23 import net.sf.saxon.type.Type;
24 import net.sf.saxon.value.StringValue;
25
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29
30 /**
31 * This class represents a namespace node; it is used in several tree models.
32 */
33
34 public class NamespaceNode implements NodeInfo {
35
36 NodeInfo element;
37 NamespaceBinding nsBinding;
38 int position;
39 int fingerprint;
40
41 /**
42 * Create a namespace node
43 *
44 * @param element the parent element of the namespace node
45 * @param nscode the namespace code, representing the prefix and URI of the namespace binding
46 * @param position maintains document order among namespace nodes for the same element
47 */
48
49 public NamespaceNode(NodeInfo element, NamespaceBinding nscode, int position) {
50 this.element = element;
51 this.nsBinding = nscode;
52 this.position = position;
53 fingerprint = -1; // evaluated lazily to avoid NamePool access
54 }
55
56 /**
57 * Get information about the tree to which this NodeInfo belongs
58 *
59 * @return the TreeInfo
60 * @since 9.7
61 */
62 public TreeInfo getTreeInfo() {
63 return element.getTreeInfo();
64 }
65
66 /**
67 * To implement {@link Sequence}, this method returns the item itself
68 *
69 * @return this item
70 */
71
72 public NodeInfo head() {
73 return this;
74 }
75
76 /**
77 * To implement {@link Sequence}, this method returns a singleton iterator
78 * that delivers this item in the form of a sequence
79 *
80 * @return a singleton iterator that returns this item
81 */
82
83 public UnfailingIterator<NodeInfo> iterate() {
84 return SingletonIterator.makeIterator(this);
85 }
86
87 /**
88 * Get the kind of node. This will be a value such as Type.ELEMENT or Type.ATTRIBUTE
89 *
90 * @return an integer identifying the kind of node. These integer values are the
91 * same as those used in the DOM
92 * @see net.sf.saxon.type.Type
93 */
94
95 public int getNodeKind() {
96 return Type.NAMESPACE;
97 }
98
99 /**
100 * Determine whether this is the same node as another node.
101 * Note: a.equals(b) if and only if generateId(a)==generateId(b).
102 * This method has the same semantics as isSameNode() in DOM Level 3, but
103 * works on Saxon NodeInfo objects rather than DOM Node objects.
104 *
105 * @param other the node to be compared with this node
106 * @return true if this NodeInfo object and the supplied NodeInfo object represent
107 * the same node in the tree.
108 */
109
110 public boolean equals(Object other) {
111 return other instanceof NamespaceNode &&
112 element.equals(((NamespaceNode) other).element) &&
113 nsBinding.equals(((NamespaceNode) other).nsBinding);
114
115 }
116
117 /**
118 * The hashCode() method obeys the contract for hashCode(): that is, if two objects are equal
119 * (represent the same node) then they must have the same hashCode()
120 *
121 * @since 8.7 Previously, the effect of the equals() and hashCode() methods was not defined. Callers
122 * should therefore be aware that third party implementations of the NodeInfo interface may
123 * not implement the correct semantics.
124 */
125
126 public int hashCode() {
127 return element.hashCode() ^ (position << 13);
128 }
129
130 /**
131 * Get the System ID for the node.
132 *
133 * @return the System Identifier of the entity in the source document
134 * containing the node, or null if not known. Note this is not the
135 * same as the base URI: the base URI can be modified by xml:base, but
136 * the system ID cannot.
137 */
138
139 /*@Nullable*/
140 public String getSystemId() {
141 return element.getSystemId();
142 }
143
144 /**
145 * Get the Public ID of the entity containing the node.
146 *
147 * @return the Public Identifier of the entity in the source document
148 * containing the node, or null if not known or not applicable
149 * @since 9.7
150 */
151 public String getPublicId() {
152 return element.getPublicId();
153 }
154
155 /**
156 * Get the Base URI for the node, that is, the URI used for resolving a relative URI contained
157 * in the node. This will be the same as the System ID unless xml:base has been used.
158 *
159 * @return the base URI of the node
160 */
161
162 /*@Nullable*/
163 public String getBaseURI() {
164 return null; // the base URI of a namespace node is the empty sequence
165 }
166
167 /**
168 * Get line number
169 *
170 * @return the line number of the node in its original source document; or
171 * -1 if not available
172 */
173
174 public int getLineNumber() {
175 return element.getLineNumber();
176 }
177
178 /**
179 * Get column number
180 *
181 * @return the column number of the node in its original source document; or
182 * -1 if not available
183 */
184
185 public int getColumnNumber() {
186 return element.getColumnNumber();
187 }
188
189 /**
190 * Get an immutable copy of this Location object. By default Location objects may be mutable, so they
191 * should not be saved for later use. The result of this operation holds the same location information,
192 * but in an immutable form.
193 */
194 public Location saveLocation() {
195 return this;
196 }
197
198 /**
199 * Determine the relative position of this node and another node, in document order.
200 * The other node will always be in the same document.
201 *
202 * @param other The other node, whose position is to be compared with this
203 * node
204 * @return -1 if this node precedes the other node, +1 if it follows the
205 * other node, or 0 if they are the same node. (In this case,
206 * isSameNode() will always return true, and the two nodes will
207 * produce the same result for generateId())
208 */
209
210 public int compareOrder(/*@NotNull*/ NodeInfo other) {
211 if (other instanceof NamespaceNode && element.equals(((NamespaceNode) other).element)) {
212 int c = position - ((NamespaceNode) other).position;
213 return Integer.compare(c, 0);
214 } else if (element.equals(other)) {
215 return +1;
216 } else {
217 return element.compareOrder(other);
218 }
219 }
220
221 /**
222 * Return the string value of the node. The interpretation of this depends on the type
223 * of node. For a namespace node, it is the namespace URI.
224 *
225 * @return the string value of the node
226 */
227
228 public String getStringValue() {
229 return nsBinding.getURI();
230 }
231
232 /**
233 * Get the value of the item as a CharSequence. This is in some cases more efficient than
234 * the version of the method that returns a String.
235 */
236
237 public CharSequence getStringValueCS() {
238 return getStringValue();
239 }
240
241 /**
242 * Ask whether this NodeInfo implementation holds a fingerprint identifying the name of the
243 * node in the NamePool. If the answer is true, then the {@link #getFingerprint} method must
244 * return the fingerprint of the node. If the answer is false, then the {@link #getFingerprint}
245 * method should throw an {@code UnsupportedOperationException}. In the case of unnamed nodes
246 * such as text nodes, the result can be either true (in which case getFingerprint() should
247 * return -1) or false (in which case getFingerprint may throw an exception).
248 *
249 * @return true if the implementation of this node provides fingerprints.
250 * @since 9.8; previously Saxon relied on using <code>FingerprintedNode</code> as a marker interface.
251 */
252 @Override
253 public boolean hasFingerprint() {
254 return true;
255 }
256
257 /**
258 * Get fingerprint. The fingerprint is a coded form of the expanded name
259 * of the node: two nodes
260 * with the same name code have the same namespace URI and the same local name.
261 * A fingerprint of -1 should be returned for a node with no name.
262 *
263 * @return an integer fingerprint; two nodes with the same fingerprint have
264 * the same expanded QName
265 */
266
267 public int getFingerprint() {
268 if (fingerprint == -1) {
269 if (nsBinding.getPrefix().isEmpty()) {
270 return -1;
271 } else {
272 fingerprint = element.getConfiguration().getNamePool().allocateFingerprint("", nsBinding.getPrefix());
273 }
274 }
275 return fingerprint;
276 }
277
278 /**
279 * Get the local part of the name of this node. This is the name after the ":" if any.
280 *
281 * @return the local part of the name. For an unnamed node, returns "". Unlike the DOM
282 * interface, this returns the full name in the case of a non-namespaced name.
283 */
284
285 public String getLocalPart() {
286 return nsBinding.getPrefix();
287 }
288
289 /**
290 * Get the URI part of the name of this node. This is the URI corresponding to the
291 * prefix, or the URI of the default namespace if appropriate.
292 *
293 * @return The URI of the namespace of this node. Since the name of a namespace
294 * node is always an NCName (the namespace prefix), this method always returns "".
295 */
296
297 /*@NotNull*/
298 public String getURI() {
299 return "";
300 }
301
302 /**
303 * Get the display name of this node. For elements and attributes this is [prefix:]localname.
304 * For unnamed nodes, it is an empty string.
305 *
306 * @return The display name of this node. For a node with no name, return
307 * an empty string.
308 */
309
310 public String getDisplayName() {
311 return getLocalPart();
312 }
313
314 /**
315 * Get the prefix of the name of the node. This is defined only for elements and attributes.
316 * If the node has no prefix, or for other kinds of node, return a zero-length string.
317 *
318 * @return The prefix of the name of the node.
319 */
320
321 /*@NotNull*/
322 public String getPrefix() {
323 return "";
324 }
325
326 /**
327 * Get the configuration
328 */
329
330 public Configuration getConfiguration() {
331 return element.getConfiguration();
332 }
333
334 /**
335 * Get the NamePool that holds the namecode for this node
336 *
337 * @return the namepool
338 */
339
340 public NamePool getNamePool() {
341 return getConfiguration().getNamePool();
342 }
343
344 /**
345 * Get the type annotation of this node, if any. The type annotation is represented as
346 * SchemaType object.
347 * <p>Types derived from a DTD are not reflected in the result of this method.</p>
348 *
349 * @return For element and attribute nodes: the type annotation derived from schema
350 * validation (defaulting to xs:untyped and xs:untypedAtomic in the absence of schema
351 * validation). For comments, text nodes, processing instructions, and namespaces: null.
352 * For document nodes, either xs:untyped if the document has not been validated, or
353 * xs:anyType if it has.
354 * @since 9.4
355 */
356 public SchemaType getSchemaType() {
357 return BuiltInAtomicType.STRING;
358 }
359
360 /**
361 * Get the NodeInfo object representing the parent of this node
362 *
363 * @return the parent of this node; null if this node has no parent
364 */
365
366 public NodeInfo getParent() {
367 return element;
368 }
369
370 /**
371 * Return an iteration over all the nodes reached by the given axis from this node
372 * that match a given NodeTest
373 *
374 * @param axisNumber an integer identifying the axis; one of the constants
375 * defined in class net.sf.saxon.om.Axis
376 * @param nodeTest A pattern to be matched by the returned nodes; nodes
377 * that do not match this pattern are not included in the result
378 * @return a NodeEnumeration that scans the nodes reached by the axis in
379 * turn.
380 * @throws UnsupportedOperationException if the namespace axis is
381 * requested and this axis is not supported for this implementation.
382 * @see net.sf.saxon.om.AxisInfo
383 */
384
385 public AxisIterator iterateAxis(byte axisNumber, /*@NotNull*/ NodeTest nodeTest) {
386 switch (axisNumber) {
387 case AxisInfo.ANCESTOR:
388 return element.iterateAxis(AxisInfo.ANCESTOR_OR_SELF, nodeTest);
389
390 case AxisInfo.ANCESTOR_OR_SELF:
391 if (nodeTest.matchesNode(this)) {
392 return new PrependAxisIterator(this, element.iterateAxis(AxisInfo.ANCESTOR_OR_SELF, nodeTest));
393 } else {
394 return element.iterateAxis(AxisInfo.ANCESTOR_OR_SELF, nodeTest);
395 }
396
397 case AxisInfo.ATTRIBUTE:
398 case AxisInfo.CHILD:
399 case AxisInfo.DESCENDANT:
400 case AxisInfo.DESCENDANT_OR_SELF:
401 case AxisInfo.FOLLOWING_SIBLING:
402 case AxisInfo.NAMESPACE:
403 case AxisInfo.PRECEDING_SIBLING:
404 return EmptyIterator.OfNodes.THE_INSTANCE;
405
406 case AxisInfo.FOLLOWING:
407 return new Navigator.AxisFilter(
408 new Navigator.FollowingEnumeration(this),
409 nodeTest);
410
411 case AxisInfo.PARENT:
412 return Navigator.filteredSingleton(element, nodeTest);
413
414 case AxisInfo.PRECEDING:
415 return new Navigator.AxisFilter(
416 new Navigator.PrecedingEnumeration(this, false),
417 nodeTest);
418
419 case AxisInfo.SELF:
420 return Navigator.filteredSingleton(this, nodeTest);
421
422 case AxisInfo.PRECEDING_OR_ANCESTOR:
423 return new Navigator.AxisFilter(
424 new Navigator.PrecedingEnumeration(this, true),
425 nodeTest);
426 default:
427 throw new IllegalArgumentException("Unknown axis number " + axisNumber);
428 }
429 }
430
431 /**
432 * Get the string value of a given attribute of this node
433 *
434 * @param uri the namespace URI of the attribute name. Supply the empty string for an attribute
435 * that is in no namespace
436 * @param local the local part of the attribute name.
437 * @return the attribute value if it exists, or null if it does not exist. Always returns null
438 * if this node is not an element.
439 * @since 9.4
440 */
441 public String getAttributeValue(/*@NotNull*/ String uri, /*@NotNull*/ String local) {
442 return null;
443 }
444
445 /**
446 * Get the root node of the tree containing this node
447 *
448 * @return the NodeInfo representing the top-level ancestor of this node.
449 * This will not necessarily be a document node
450 */
451
452 public NodeInfo getRoot() {
453 return element.getRoot();
454 }
455
456 /**
457 * Determine whether the node has any children.
458 * <p>Note: the result is equivalent to
459 * <code>getEnumeration(Axis.CHILD, AnyNodeTest.getInstance()).hasNext()</code></p>
460 *
461 * @return True if the node has one or more children
462 */
463
464 public boolean hasChildNodes() {
465 return false;
466 }
467
468 /**
469 * Get a character string that uniquely identifies this node.
470 * Note: a.isSameNode(b) if and only if generateId(a)==generateId(b)
471 *
472 * @param buffer buffer to hold a string that uniquely identifies this node, across all
473 * documents.
474 */
475
476 public void generateId(/*@NotNull*/ FastStringBuffer buffer) {
477 element.generateId(buffer);
478 buffer.append("n");
479 buffer.append(Integer.toString(position));
480 }
481
482 /**
483 * Copy this node to a given outputter
484 * @param out the Receiver to which the node should be copied
485 * @param copyOptions a selection of the options defined in {@link CopyOptions}
486 * @param locationId If non-zero, identifies the location of the instruction
487 * that requested this copy. If zero, indicates that the location information
488 */
489
490 public void copy(/*@NotNull*/ Receiver out, int copyOptions, Location locationId) throws XPathException {
491 out.namespace(nsBinding, ReceiverOptions.REJECT_DUPLICATES);
492 }
493
494 /**
495 * Get all namespace undeclarations and undeclarations defined on this element.
496 *
497 * @param buffer If this is non-null, and the result array fits in this buffer, then the result
498 * may overwrite the contents of this array, to avoid the cost of allocating a new array on the heap.
499 * @return An array of integers representing the namespace declarations and undeclarations present on
500 * this element. For a node other than an element, return null. Otherwise, the returned array is a
501 * sequence of namespace codes, whose meaning may be interpreted by reference to the name pool. The
502 * top half word of each namespace code represents the prefix, the bottom half represents the URI.
503 * If the bottom half is zero, then this is a namespace undeclaration rather than a declaration.
504 * The XML namespace is never included in the list. If the supplied array is larger than required,
505 * then the first unused entry will be set to -1.
506 * <p>For a node other than an element, the method returns null.</p>
507 */
508
509 /*@Nullable*/
510 public NamespaceBinding[] getDeclaredNamespaces(NamespaceBinding[] buffer) {
511 return null;
512 }
513
514 /**
515 * Set the system identifier for this Source.
516 * <p>The system identifier is optional if the source does not
517 * get its data from a URL, but it may still be useful to provide one.
518 * The application can use a system identifier, for example, to resolve
519 * relative URIs and to include in error messages and warnings.</p>
520 *
521 * @param systemId The system identifier as a URL string.
522 */
523 public void setSystemId(String systemId) {
524 // no action: namespace nodes have the same base URI as their parent
525 }
526
527 /**
528 * Get the typed value.
529 *
530 * @return the typed value.
531 * @since 8.5
532 */
533
534 /*@NotNull*/
535 public AtomicSequence atomize() throws XPathException {
536 return new StringValue(getStringValueCS());
537 }
538
539 @Override
540 public boolean isStreamed() {
541 return element.isStreamed();
542 }
543
544 /**
545 * Factory method to create an iterator over the in-scope namespace nodes of an element
546 *
547 * @param element the node whose namespaces are required
548 * @param test used to filter the returned nodes
549 * @return an iterator over the namespace nodes that satisfy the test
550 */
551
552 /*@NotNull*/
553 public static AxisIterator makeIterator(final NodeInfo element, /*@NotNull*/ NodeTest test) {
554 List<NodeInfo> nodes = new ArrayList<>();
555 Iterator<NamespaceBinding> bindings = NamespaceIterator.iterateNamespaces(element);
556 int position = 0;
557 while (bindings.hasNext()) {
558 NamespaceNode node = new NamespaceNode(element, bindings.next(), position++);
559 if (test.matchesNode(node)) {
560 nodes.add(node);
561 }
562 }
563 NamespaceNode node = new NamespaceNode(element, NamespaceBinding.XML, position);
564 if (test.matchesNode(node)) {
565 nodes.add(node);
566 }
567 return new ListIterator.OfNodes(nodes);
568 }
569 }
570