| 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.om; |
| 9 | |
| 10 | import net.sf.saxon.lib.NamespaceConstant; |
| 11 | import net.sf.saxon.tree.util.NamespaceIterator; |
| 12 | import net.sf.saxon.type.Type; |
| 13 | |
| 14 | import java.util.Iterator; |
| 15 | |
| 16 | /** |
| 17 | * A NamespaceResolver that resolves namespace prefixes by reference to a node in a document for which |
| 18 | * those namespaces are in-scope. |
| 19 | */ |
| 20 | public class InscopeNamespaceResolver implements NamespaceResolver { |
| 21 | |
| 22 | private NodeInfo node; |
| 23 | |
| 24 | /** |
| 25 | * Create a NamespaceResolver that resolves according to the in-scope namespaces |
| 26 | * of a given node |
| 27 | * |
| 28 | * @param node the given node. Must not be null. |
| 29 | * If this is an element node, the resolver uses the in-scope namespaces |
| 30 | * of that element. If it is a document node, the only in-scope namespace is the XML namespace. If it |
| 31 | * is any other kind of node, then if the node has a parent element, the resolver uses the in-scope |
| 32 | * namespaces of the parent element, otherwise ithe only in-scope namespace is the XML namespace. |
| 33 | */ |
| 34 | |
| 35 | public InscopeNamespaceResolver(NodeInfo node) { |
| 36 | int kind = node.getNodeKind(); |
| 37 | if (kind == Type.ELEMENT || kind == Type.DOCUMENT) { |
| 38 | this.node = node; |
| 39 | } else { |
| 40 | NodeInfo parent = node.getParent(); |
| 41 | this.node = parent == null ? node : parent; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get the namespace URI corresponding to a given prefix. Return null |
| 47 | * if the prefix is not in scope. |
| 48 | * |
| 49 | * @param prefix the namespace prefix. May be the zero-length string, indicating |
| 50 | * that there is no prefix. This indicates either the default namespace or the |
| 51 | * null namespace, depending on the value of useDefault. |
| 52 | * @param useDefault true if the default namespace is to be used when the |
| 53 | * prefix is "". If false, the method returns "" when the prefix is "". |
| 54 | * @return the uri for the namespace, or null if the prefix is not in scope. |
| 55 | * The "null namespace" is represented by the pseudo-URI "". |
| 56 | */ |
| 57 | |
| 58 | /*@Nullable*/ |
| 59 | public String getURIForPrefix(String prefix, boolean useDefault) { |
| 60 | if (prefix.isEmpty() && !useDefault) { |
| 61 | return NamespaceConstant.NULL; |
| 62 | } |
| 63 | if (prefix.equals("xml")) { |
| 64 | return NamespaceConstant.XML; |
| 65 | } |
| 66 | Iterator<NamespaceBinding> iter = NamespaceIterator.iterateNamespaces(node); |
| 67 | while (iter.hasNext()) { |
| 68 | NamespaceBinding nb = iter.next(); |
| 69 | if (nb.getPrefix().equals(prefix)) { |
| 70 | return nb.getURI(); |
| 71 | } |
| 72 | } |
| 73 | return prefix.isEmpty() ? "" : null; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get an iterator over all the prefixes declared in this namespace context. This will include |
| 78 | * the default namespace (prefix="") and the XML namespace where appropriate |
| 79 | */ |
| 80 | |
| 81 | public Iterator<String> iteratePrefixes() { |
| 82 | return new Iterator<String>() { |
| 83 | int phase = 0; |
| 84 | Iterator<NamespaceBinding> iter = NamespaceIterator.iterateNamespaces(node); |
| 85 | |
| 86 | public boolean hasNext() { |
| 87 | if (iter.hasNext()) { |
| 88 | return true; |
| 89 | } else if (phase == 0) { |
| 90 | phase = 1; |
| 91 | return true; |
| 92 | } else { |
| 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public String next() { |
| 98 | if (phase == 1) { |
| 99 | phase = 2; |
| 100 | return "xml"; |
| 101 | } else { |
| 102 | return iter.next().getPrefix(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | public void remove() { |
| 107 | throw new UnsupportedOperationException("remove"); |
| 108 | } |
| 109 | }; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Get the node on which this namespace resolver is based |
| 114 | * |
| 115 | * @return the node on which this namespace resolver is based |
| 116 | */ |
| 117 | |
| 118 | public NodeInfo getNode() { |
| 119 | return node; |
| 120 | } |
| 121 | } |
| 122 |