| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class NamespaceIterator implements Iterator<NamespaceBinding> { |
| 30 |
|
| 31 |
private NodeInfo element; |
| 32 |
private int index; |
| 33 |
private NamespaceBinding next; |
| 34 |
private NamespaceBinding[] localDeclarations; |
| 35 |
HashSet<String> undeclaredPrefixes; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public static Iterator<NamespaceBinding> iterateNamespaces( 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 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public static void sendNamespaces( NodeInfo element, 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 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 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 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
private NamespaceIterator( 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 |
|
| 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 |
|
| 130 |
undeclaredPrefixes.add(prefix); |
| 131 |
} else { |
| 132 |
if (undeclaredPrefixes.add(prefix)) { |
| 133 |
|
| 134 |
next = nsCode; |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|