Debian Codegraph @

net/sf/saxon/tree/util/NamespaceIterator.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.util;
9
10 import net.sf.saxon.event.Receiver;
11 import net.sf.saxon.om.InScopeNamespaces;
12 import net.sf.saxon.om.NamespaceBinding;
13 import net.sf.saxon.om.NodeInfo;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.tree.tiny.TinyNodeImpl;
16 import net.sf.saxon.type.Type;
17
18 import java.util.Collections;
19 import java.util.HashSet;
20 import java.util.Iterator;
21 import java.util.List;
22
23 /**
24 * This class provides an iterator over the namespace codes representing the in-scope namespaces
25 * of any node. It relies on nodes to implement the method
26 * {@link net.sf.saxon.om.NodeInfo#getDeclaredNamespaces(net.sf.saxon.om.NamespaceBinding[])}.
27 * <p>The result does not include the XML namespace.</p>
28 */
29 public class NamespaceIterator implements Iterator<NamespaceBinding> {
30
31 /*@Nullable*/ private NodeInfo element;
32 private int index;
33 /*@Nullable*/ private NamespaceBinding next;
34 private NamespaceBinding[] localDeclarations;
35 HashSet<String> undeclaredPrefixes;
36
37 /**
38 * Factory method: create an iterator over the in-scope namespace codes for an element
39 *
40 * @param element the element (or other node) whose in-scope namespaces are required. If this
41 * is not an element, the result will be an empty iterator
42 * @return an iterator over the namespace codes. A namespace code is an integer that represents
43 * a prefix-uri binding; the prefix and URI can be obtained by reference to the name pool. This
44 * iterator will represent all the in-scope namespaces, without duplicates, and respecting namespace
45 * undeclarations. It does not include the XML namespace.
46 */
47
48 public static Iterator<NamespaceBinding> iterateNamespaces(/*@NotNull*/ NodeInfo element) {
49 if (element.getNodeKind() == Type.ELEMENT) {
50 return new NamespaceIterator(element);
51 } else {
52 List<NamespaceBinding> e = Collections.emptyList();
53 return e.iterator();
54 }
55 }
56
57 /**
58 * Send all the in-scope namespaces for a node (except the XML namespace) to a specified receiver
59 *
60 * @param element the element in question (the method does nothing if this is not an element)
61 * @param receiver the receiver to which the namespaces are notified
62 * @throws XPathException if a failure is reported by the downstream Receiver
63 */
64
65 public static void sendNamespaces(/*@NotNull*/ NodeInfo element, /*@NotNull*/ Receiver receiver) throws XPathException {
66 if (element instanceof TinyNodeImpl && !((TinyNodeImpl)element).getTree().isUsesNamespaces()) {
67 return;
68 }
69 while (element != null && element.getNodeKind() == Type.ELEMENT) {
70 // find the first ancestor-or-self element that has namespace declarations, and send all its in-scope
71 // namespaces to the receiver. The significance of this is that with shallow copy, the namespace set
72 // will usually be the same as the parent element's namespace set, and will then be quickly discarded
73 // by the ComplexContentOutputter.
74 NamespaceBinding[] localNamespaces = element.getDeclaredNamespaces(null);
75 if (localNamespaces.length > 0) {
76 receiver.namespace(new InScopeNamespaces(element), 0);
77 return;
78 }
79 element = element.getParent();
80 }
81 // for (Iterator<NamespaceBinding> iter = iterateNamespaces(element); iter.hasNext(); ) {
82 // NamespaceBinding nb = iter.next();
83 // receiver.namespace(nb, 0);
84 // }
85 }
86
87 private NamespaceIterator(/*@NotNull*/ NodeInfo element) {
88 this.element = element;
89 undeclaredPrefixes = new HashSet<>(8);
90 index = 0;
91 localDeclarations = element.getDeclaredNamespaces(null);
92 }
93
94 public boolean hasNext() {
95 if (element == null || (next == null && index != 0)) {
96 return false;
97 }
98 advance();
99 return next != null;
100 }
101
102 /*@Nullable*/
103 public NamespaceBinding next() {
104 return next;
105 }
106
107 private void advance() {
108 while (true) {
109 boolean ascend = index >= localDeclarations.length;
110 NamespaceBinding nsCode = null;
111 if (!ascend) {
112 nsCode = localDeclarations[index++];
113 ascend = nsCode == null;
114 }
115 if (ascend) {
116 element = element.getParent();
117 if (element != null && element.getNodeKind() == Type.ELEMENT) {
118 localDeclarations = element.getDeclaredNamespaces(localDeclarations);
119 index = 0;
120 continue;
121 } else {
122 next = null;
123 return;
124 }
125 }
126 String uri = nsCode.getURI();
127 String prefix = nsCode.getPrefix();
128 if (uri.isEmpty()) {
129 // this is an undeclaration
130 undeclaredPrefixes.add(prefix);
131 } else {
132 if (undeclaredPrefixes.add(prefix)) {
133 // it was added, so it's new, so return it
134 next = nsCode;
135 return;
136 }
137 // else it wasn't added, so we've already seen it
138 }
139 }
140 }
141 }
142