Debian Codegraph @

net/sf/saxon/dom/NodeOverNodeInfo.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.dom;
9
10 import net.sf.saxon.expr.XPathContext;
11 import net.sf.saxon.expr.sort.CodepointCollator;
12 import net.sf.saxon.expr.sort.GenericAtomicComparer;
13 import net.sf.saxon.functions.DeepEqual;
14 import net.sf.saxon.lib.NamespaceConstant;
15 import net.sf.saxon.om.AxisInfo;
16 import net.sf.saxon.om.NodeInfo;
17 import net.sf.saxon.om.SequenceIterator;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.tree.iter.AxisIterator;
20 import net.sf.saxon.tree.iter.SingletonIterator;
21 import net.sf.saxon.type.Type;
22 import org.w3c.dom.*;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27
28 /**
29 * This class implements the DOM Node interface as a wrapper around a Saxon NodeInfo object.
30 * <p>The class provides read-only access to the tree; methods that request updates all fail
31 * with an UnsupportedOperationException.</p>
32 */
33
34 public abstract class NodeOverNodeInfo implements Node {
35
36 protected NodeInfo node;
37
38 /**
39 * Get the Saxon NodeInfo object representing this node
40 *
41 * @return the Saxon NodeInfo object
42 */
43
44 /*@Nullable*/
45 public NodeInfo getUnderlyingNodeInfo() {
46 return node;
47 }
48
49 /**
50 * Factory method to construct a DOM node that wraps an underlying Saxon NodeInfo
51 *
52 * @param node the Saxon NodeInfo object
53 * @return the DOM wrapper node
54 */
55
56 public static NodeOverNodeInfo wrap(NodeInfo node) {
57 NodeOverNodeInfo n;
58 if (node == null) {
59 return null;
60 }
61 switch (node.getNodeKind()) {
62 case Type.DOCUMENT:
63 n = new DocumentOverNodeInfo();
64 break;
65 case Type.ELEMENT:
66 n = new ElementOverNodeInfo();
67 break;
68 case Type.ATTRIBUTE:
69 n = new AttrOverNodeInfo();
70 break;
71 case Type.TEXT:
72 case Type.COMMENT:
73 n = new TextOverNodeInfo();
74 break;
75 case Type.PROCESSING_INSTRUCTION:
76 n = new PIOverNodeInfo();
77 break;
78 case Type.NAMESPACE:
79 n = new AttrOverNodeInfo();
80 break;
81 default:
82 return null;
83 }
84 n.node = node;
85 return n;
86 }
87
88
89 /**
90 * Determine whether this is the same node as another node. DOM Level 3 method.
91 *
92 * @return true if this Node object and the supplied Node object represent the
93 * same node in the tree.
94 */
95
96 public final boolean isSameNode(Node other) {
97 return other instanceof NodeOverNodeInfo &&
98 node.equals(((NodeOverNodeInfo) other).node);
99 }
100
101 /**
102 * The equals() method returns true for two Node objects that represent the same
103 * conceptual DOM Node. This is a concession to the Xalan IdentityTransformer, which relies
104 * on equals() for DOM Nodes having this behaviour, even though it is not defined in the
105 * specification
106 *
107 * @param obj the object to be compared
108 * @return if this node and obj represent the same conceptual DOM node. That is, return
109 * true if isSameNode((Node)obj) returns true
110 */
111
112 @Override
113 public boolean equals(Object obj) {
114 return obj instanceof Node && isSameNode((Node) obj);
115 }
116
117 /**
118 * Return a hashCode
119 *
120 * @return a hashCode such that two wrappers over the same underlying node have the same hashCode.
121 */
122
123 @Override
124 public int hashCode() {
125 return node.hashCode();
126 }
127
128 /**
129 * Get the base URI for the node. Default implementation for child nodes gets
130 * the base URI of the parent node.
131 */
132
133 public String getBaseURI() {
134 return node.getBaseURI();
135 }
136
137 /**
138 * Get the name of this node, following the DOM rules
139 *
140 * @return The name of the node. For an element this is the element name, for an attribute
141 * it is the attribute name, as a lexical QName. Other node types return conventional names such
142 * as "#text" or "#comment"
143 */
144
145 public String getNodeName() {
146 switch (node.getNodeKind()) {
147 case Type.DOCUMENT:
148 return "#document";
149 case Type.ELEMENT:
150 return node.getDisplayName();
151 case Type.ATTRIBUTE:
152 return node.getDisplayName();
153 case Type.TEXT:
154 return "#text";
155 case Type.COMMENT:
156 return "#comment";
157 case Type.PROCESSING_INSTRUCTION:
158 return node.getLocalPart();
159 case Type.NAMESPACE:
160 if (node.getLocalPart().isEmpty()) {
161 return "xmlns";
162 } else {
163 return "xmlns:" + node.getLocalPart();
164 }
165 default:
166 return "#unknown";
167 }
168 }
169
170 /**
171 * Get the local name of this node, following the DOM rules
172 *
173 * @return The local name of the node. For an element this is the local part of the element name,
174 * for an attribute it is the local part of the attribute name. Other node types return null.
175 */
176
177 public String getLocalName() {
178 switch (node.getNodeKind()) {
179 case Type.ELEMENT:
180 case Type.ATTRIBUTE:
181 return node.getLocalPart();
182 case Type.DOCUMENT:
183 case Type.TEXT:
184 case Type.COMMENT:
185 case Type.PROCESSING_INSTRUCTION:
186 return null;
187 case Type.NAMESPACE:
188 if (node.getLocalPart().isEmpty()) {
189 return "xmlns";
190 } else {
191 return node.getLocalPart();
192 }
193 default:
194 return null;
195 }
196 }
197
198
199 /**
200 * Determine whether the node has any children.
201 *
202 * @return <code>true</code> if this node has any attributes,
203 * <code>false</code> otherwise.
204 */
205
206 public boolean hasChildNodes() {
207 return node.iterateAxis(AxisInfo.CHILD).next() != null;
208 }
209
210 /**
211 * Returns whether this node has any attributes. We treat the declaration of the XML namespace
212 * as being present on every element, and since namespace declarations are treated as attributes,
213 * every element has at least one attribute. This method therefore returns true.
214 *
215 * @return <code>true</code> if this node has any attributes,
216 * <code>false</code> otherwise.
217 * @since DOM Level 2
218 */
219
220 public boolean hasAttributes() {
221 return true;
222 }
223
224 /**
225 * Get the type of this node (node kind, in XPath terminology).
226 * Note, the numbers assigned to node kinds
227 * in Saxon (see {@link Type}) are the same as those assigned in the DOM
228 */
229
230 public short getNodeType() {
231 short kind = (short) node.getNodeKind();
232 if (kind == Type.NAMESPACE) {
233 return Type.ATTRIBUTE;
234 } else {
235 return kind;
236 }
237 }
238
239 /**
240 * Find the parent node of this node.
241 *
242 * @return The Node object describing the containing element or root node.
243 */
244
245 public Node getParentNode() {
246 return wrap(node.getParent());
247 }
248
249 /**
250 * Get the previous sibling of the node
251 *
252 * @return The previous sibling node. Returns null if the current node is the first
253 * child of its parent.
254 */
255
256 public Node getPreviousSibling() {
257 return wrap(node.iterateAxis(AxisInfo.PRECEDING_SIBLING).next());
258 }
259
260 /**
261 * Get next sibling node
262 *
263 * @return The next sibling node. Returns null if the current node is the last
264 * child of its parent.
265 */
266
267 public Node getNextSibling() {
268 return wrap(node.iterateAxis(AxisInfo.FOLLOWING_SIBLING).next());
269 }
270
271 /**
272 * Get first child
273 *
274 * @return the first child node of this node, or null if it has no children
275 */
276
277 public Node getFirstChild() {
278 return wrap(node.iterateAxis(AxisInfo.CHILD).next());
279 }
280
281 /**
282 * Get last child
283 *
284 * @return last child of this node, or null if it has no children
285 */
286
287 public Node getLastChild() {
288 AxisIterator children = node.iterateAxis(AxisInfo.CHILD);
289 NodeInfo last = null;
290 while (true) {
291 NodeInfo next = children.next();
292 if (next == null) {
293 return wrap(last);
294 } else {
295 last = next;
296 }
297 }
298 }
299
300 /**
301 * Get the node value (as defined in the DOM).
302 * This is not generally the same as the XPath string-value: in particular, the
303 * node value of an element node is null.
304 */
305
306 public String getNodeValue() {
307 switch (node.getNodeKind()) {
308 case Type.DOCUMENT:
309 case Type.ELEMENT:
310 return null;
311 case Type.ATTRIBUTE:
312 case Type.TEXT:
313 case Type.COMMENT:
314 case Type.PROCESSING_INSTRUCTION:
315 case Type.NAMESPACE:
316 return node.getStringValue();
317 default:
318 return null;
319 }
320 }
321
322 /**
323 * Set the node value. Always fails
324 */
325
326 public void setNodeValue(String nodeValue) throws DOMException {
327 disallowUpdate();
328 }
329
330 /**
331 * Return a <code>NodeList</code> that contains all children of this node. If
332 * there are no children, this is a <code>NodeList</code> containing no
333 * nodes.
334 */
335
336 public NodeList getChildNodes() {
337 try {
338 List<Node> nodes = new ArrayList<>(10);
339 SequenceIterator iter = node.iterateAxis(AxisInfo.CHILD);
340 NodeInfo node;
341 while ((node = (NodeInfo) iter.next()) != null) {
342 nodes.add(NodeOverNodeInfo.wrap(node));
343 }
344 return new DOMNodeList(nodes);
345 } catch (XPathException err) {
346 return null;
347 // can't happen
348 }
349 }
350
351 /**
352 * Return a <code>NamedNodeMap</code> containing the attributes of this node (if
353 * it is an <code>Element</code>) or <code>null</code> otherwise. Note that this
354 * implementation changed in Saxon 8.8 to treat namespace declarations as attributes.
355 */
356
357 public NamedNodeMap getAttributes() {
358 if (node.getNodeKind() == Type.ELEMENT) {
359 return new DOMAttributeMap(node);
360 } else {
361 return null;
362 }
363 }
364
365 /**
366 * Return the <code>Document</code> object associated with this node.
367 */
368
369 public Document getOwnerDocument() {
370 return (Document) wrap(node.getRoot());
371 }
372
373 /**
374 * Insert the node <code>newChild</code> before the existing child node
375 * <code>refChild</code>. Always fails.
376 *
377 * @param newChild The node to insert.
378 * @param refChild The reference node, i.e., the node before which the
379 * new node must be inserted.
380 * @return The node being inserted.
381 * @throws org.w3c.dom.DOMException NO_MODIFICATION_ALLOWED_ERR: Always raised.
382 */
383
384 public Node insertBefore(Node newChild,
385 Node refChild)
386 throws DOMException {
387 disallowUpdate();
388 return null;
389 }
390
391 /**
392 * Replace the child node <code>oldChild</code> with
393 * <code>newChild</code> in the list of children, and returns the
394 * <code>oldChild</code> node. Always fails.
395 *
396 * @param newChild The new node to put in the child list.
397 * @param oldChild The node being replaced in the list.
398 * @return The node replaced.
399 * @throws org.w3c.dom.DOMException NO_MODIFICATION_ALLOWED_ERR: Always raised.
400 */
401
402 public Node replaceChild(Node newChild,
403 Node oldChild)
404 throws DOMException {
405 disallowUpdate();
406 return null;
407 }
408
409 /**
410 * Remove the child node indicated by <code>oldChild</code> from the
411 * list of children, and returns it. Always fails.
412 *
413 * @param oldChild The node being removed.
414 * @return The node removed.
415 * @throws org.w3c.dom.DOMException NO_MODIFICATION_ALLOWED_ERR: Always raised.
416 */
417
418 public Node removeChild(Node oldChild) throws DOMException {
419 disallowUpdate();
420 return null;
421 }
422
423 /**
424 * Adds the node <code>newChild</code> to the end of the list of children
425 * of this node. Always fails.
426 *
427 * @param newChild The node to add.
428 * @return The node added.
429 * @throws org.w3c.dom.DOMException <br> NO_MODIFICATION_ALLOWED_ERR: Always raised.
430 */
431
432 public Node appendChild(Node newChild) throws DOMException {
433 disallowUpdate();
434 return null;
435 }
436
437 /**
438 * Returns a duplicate of this node, i.e., serves as a generic copy
439 * constructor for nodes. Always fails.
440 *
441 * @param deep If <code>true</code> , recursively clone the subtree under
442 * the specified node; if <code>false</code> , clone only the node
443 * itself (and its attributes, if it is an <code>Element</code> ).
444 * @return The duplicate node.
445 */
446
447 public Node cloneNode(boolean deep) {
448 disallowUpdate();
449 return null;
450 }
451
452 /**
453 * Puts all <code>Text</code> nodes in the full depth of the sub-tree
454 * underneath this <code>Node</code>, including attribute nodes, into a
455 * "normal" form where only structure (e.g., elements, comments,
456 * processing instructions, CDATA sections, and entity references)
457 * separates <code>Text</code> nodes, i.e., there are neither adjacent
458 * <code>Text</code> nodes nor empty <code>Text</code> nodes.
459 *
460 * @since DOM Level 2
461 */
462
463 public void normalize() {
464 // null operation; nodes are always normalized
465 }
466
467 /**
468 * Tests whether the DOM implementation implements a specific feature and
469 * that feature is supported by this node.
470 *
471 * @param feature The name of the feature to test. This is the same name
472 * which can be passed to the method <code>hasFeature</code> on
473 * <code>DOMImplementation</code> .
474 * @param version This is the version number of the feature to test. In
475 * Level 2, version 1, this is the string "2.0". If the version is not
476 * specified, supporting any version of the feature will cause the
477 * method to return <code>true</code> .
478 * @return Returns <code>true</code> if the specified feature is supported
479 * on this node, <code>false</code> otherwise.
480 * @since DOM Level 2
481 */
482
483 public boolean isSupported(String feature,
484 String version) {
485 return (feature.equalsIgnoreCase("XML") || feature.equalsIgnoreCase("Core")) &&
486 (version == null || version.isEmpty() ||
487 version.equals("3.0") || version.equals("2.0") || version.equals("1.0"));
488 }
489
490 /**
491 * The namespace URI of this node, or <code>null</code> if it is
492 * unspecified.
493 * <br> This is not a computed value that is the result of a namespace
494 * lookup based on an examination of the namespace declarations in scope.
495 * It is merely the namespace URI given at creation time.
496 * <br> For nodes of any type other than <code>ELEMENT_NODE</code> and
497 * <code>ATTRIBUTE_NODE</code> and nodes created with a DOM Level 1
498 * method, such as <code>createElement</code> from the
499 * <code>Document</code> interface, this is always <code>null</code> .
500 * Per the Namespaces in XML Specification an attribute does not
501 * inherit its namespace from the element it is attached to. If an
502 * attribute is not explicitly given a namespace, it simply has no
503 * namespace.
504 *
505 * @since DOM Level 2
506 */
507
508 public String getNamespaceURI() {
509 if (node.getNodeKind() == Type.NAMESPACE) {
510 return NamespaceConstant.XMLNS;
511 }
512 String uri = node.getURI();
513 return "".equals(uri) ? null : uri;
514 }
515
516 /**
517 * The namespace prefix of this node, or <code>null</code> if it is
518 * unspecified.
519 * <br>For nodes of any type other than <code>ELEMENT_NODE</code> and
520 * <code>ATTRIBUTE_NODE</code> and nodes created with a DOM Level 1
521 * method, such as <code>createElement</code> from the
522 * <code>Document</code> interface, this is always <code>null</code>.
523 *
524 * @since DOM Level 2
525 */
526
527 public String getPrefix() {
528 if (node.getNodeKind() == Type.NAMESPACE) {
529 if (node.getLocalPart().isEmpty()) {
530 return null;
531 } else {
532 return "xmlns";
533 }
534 }
535 String p = node.getPrefix();
536 return "".equals(p) ? null : p;
537 }
538
539 /**
540 * Set the namespace prefix of this node. Always fails.
541 */
542
543 public void setPrefix(String prefix)
544 throws DOMException {
545 disallowUpdate();
546 }
547
548 /**
549 * Compare the position of the (other) node in document order with the reference node (this node).
550 * DOM Level 3 method.
551 *
552 * @param other the other node.
553 * @return Returns how the node is positioned relatively to the reference
554 * node.
555 * @throws org.w3c.dom.DOMException if an error occurs
556 */
557
558 public short compareDocumentPosition(Node other) throws DOMException {
559 final short DOCUMENT_POSITION_DISCONNECTED = 0x01;
560 final short DOCUMENT_POSITION_PRECEDING = 0x02;
561 final short DOCUMENT_POSITION_FOLLOWING = 0x04;
562 final short DOCUMENT_POSITION_CONTAINS = 0x08;
563 final short DOCUMENT_POSITION_CONTAINED_BY = 0x10;
564 if (!(other instanceof NodeOverNodeInfo)) {
565 return DOCUMENT_POSITION_DISCONNECTED;
566 }
567 int c = node.compareOrder(((NodeOverNodeInfo) other).node);
568 if (c == 0) {
569 return (short) 0;
570 } else if (c == -1) {
571 short result = DOCUMENT_POSITION_FOLLOWING;
572 short d = compareDocumentPosition(other.getParentNode());
573 if (d == 0 || (d & DOCUMENT_POSITION_CONTAINED_BY) != 0) {
574 result |= DOCUMENT_POSITION_CONTAINED_BY;
575 }
576 return result;
577 } else if (c == +1) {
578 short result = DOCUMENT_POSITION_PRECEDING;
579 short d = getParentNode().compareDocumentPosition(other);
580 if (d == 0 || (d & DOCUMENT_POSITION_CONTAINS) != 0) {
581 result |= DOCUMENT_POSITION_CONTAINS;
582 }
583 return result;
584 } else {
585 throw new AssertionError();
586 }
587 }
588
589 /**
590 * Get the text content of a node. This is a DOM Level 3 method. The definition
591 * is the same as the definition of the string value of a node in XPath, except
592 * in the case of document nodes.
593 *
594 * @return the string value of the node, or null in the case of document nodes.
595 * @throws org.w3c.dom.DOMException if a dynamic error occurs
596 */
597
598 public String getTextContent() throws DOMException {
599 if (node.getNodeKind() == Type.DOCUMENT) {
600 return null;
601 } else {
602 return node.getStringValue();
603 }
604 }
605
606 /**
607 * Set the text content of a node. Always fails.
608 *
609 * @param textContent the new text content of the node
610 * @throws UnsupportedOperationException always
611 */
612
613 public void setTextContent(String textContent) throws UnsupportedOperationException {
614 disallowUpdate();
615 }
616
617 /**
618 * Get the (first) prefix assigned to a specified namespace URI, or null
619 * if the namespace is not in scope. DOM Level 3 method.
620 *
621 * @param namespaceURI the namespace whose prefix is required
622 * @return the corresponding prefix, if there is one, or null if not.
623 */
624
625 public String lookupPrefix(String namespaceURI) {
626 if (node.getNodeKind() == Type.DOCUMENT) {
627 return null;
628 } else if (node.getNodeKind() == Type.ELEMENT) {
629 AxisIterator iter = node.iterateAxis(AxisInfo.NAMESPACE);
630 NodeInfo ns;
631 while ((ns = iter.next()) != null) {
632 if (ns.getStringValue().equals(namespaceURI)) {
633 return ns.getLocalPart();
634 }
635 }
636 return null;
637 } else {
638 return getParentNode().lookupPrefix(namespaceURI);
639 }
640 }
641
642 /**
643 * Test whether a particular namespace is the default namespace.
644 * DOM Level 3 method.
645 *
646 * @param namespaceURI the namespace to be tested
647 * @return true if this is the default namespace
648 */
649
650 public boolean isDefaultNamespace(String namespaceURI) {
651 return namespaceURI.equals(lookupNamespaceURI(""));
652 }
653
654 /**
655 * Find the URI corresponding to a given in-scope prefix
656 *
657 * @param prefix The namespace prefix whose namespace URI is required.
658 * @return the corresponding namespace URI, or null if the prefix is
659 * not declared.
660 */
661
662 public String lookupNamespaceURI(String prefix) {
663 if (node.getNodeKind() == Type.DOCUMENT) {
664 return null;
665 } else if (node.getNodeKind() == Type.ELEMENT) {
666 AxisIterator iter = node.iterateAxis(AxisInfo.NAMESPACE);
667 NodeInfo ns;
668 while ((ns = iter.next()) != null) {
669 if (ns.getLocalPart().equals(prefix)) {
670 return ns.getStringValue();
671 }
672 }
673 return null;
674 } else {
675 return getParentNode().lookupNamespaceURI(prefix);
676 }
677 }
678
679 /**
680 * Compare whether two nodes have the same content. This is a DOM Level 3 method.
681 *
682 * @param arg The node to be compared. This must wrap a Saxon NodeInfo.
683 * @return true if the two nodes are deep-equal.
684 */
685
686 public boolean isEqualNode(Node arg) {
687 if (!(arg instanceof NodeOverNodeInfo)) {
688 throw new IllegalArgumentException("Other Node must wrap a Saxon NodeInfo");
689 }
690 try {
691 XPathContext context = node.getConfiguration().getConversionContext();
692 return DeepEqual.deepEqual(
693 SingletonIterator.makeIterator(node),
694 SingletonIterator.makeIterator(((NodeOverNodeInfo) arg).node),
695 new GenericAtomicComparer(CodepointCollator.getInstance(),
696 context),
697 context,
698 DeepEqual.INCLUDE_PREFIXES |
699 DeepEqual.INCLUDE_COMMENTS |
700 DeepEqual.COMPARE_STRING_VALUES |
701 DeepEqual.INCLUDE_PROCESSING_INSTRUCTIONS);
702 } catch (XPathException err) {
703 // can't happen
704 return false;
705 }
706 }
707
708 /**
709 * Get a feature of this node. DOM Level 3 method, always returns null.
710 *
711 * @param feature the required feature
712 * @param version the version of the required feature
713 * @return the value of the feature. Always null in this implementation
714 */
715
716 public Object getFeature(String feature, String version) {
717 return null;
718 }
719
720 /**
721 * Set user data. Always throws UnsupportedOperationException in this implementation
722 *
723 * @param key name of the user data
724 * @param data value of the user data
725 * @param handler handler for the user data
726 * @return This implementation always throws an exception
727 */
728
729 public Object setUserData(String key, Object data, UserDataHandler handler) {
730 disallowUpdate();
731 return null;
732 }
733
734 /**
735 * Get user data associated with this node. DOM Level 3 method, always returns
736 * null in this implementation
737 *
738 * @param key identifies the user data required
739 * @return always null in this implementation
740 */
741 public Object getUserData(String key) {
742 return null;
743 }
744
745 /**
746 * Internal method used to indicate that update operations are not allowed
747 *
748 * @throws org.w3c.dom.DOMException always, to indicate that update is not supported in this DOM implementation
749 */
750
751 protected static void disallowUpdate() throws DOMException {
752 throw new UnsupportedOperationException("The Saxon DOM implementation cannot be updated");
753 }
754
755 }
756