| 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.expr.instruct; |
| 9 | |
| 10 | import net.sf.saxon.lib.NamespaceConstant; |
| 11 | import net.sf.saxon.om.NamespaceBinding; |
| 12 | import net.sf.saxon.om.NamespaceResolver; |
| 13 | |
| 14 | import java.util.*; |
| 15 | import java.util.function.Predicate; |
| 16 | |
| 17 | /** |
| 18 | * An object representing a list of Namespaces. Used when the namespace |
| 19 | * controller in the stylesheet needs to be kept for use at run-time. The list of namespaces |
| 20 | * is maintained in the form of numeric prefix/uri codes, which are only meaningful |
| 21 | * in the context of a name pool |
| 22 | */ |
| 23 | |
| 24 | public final class SavedNamespaceContext implements NamespaceResolver { |
| 25 | |
| 26 | private Map<String, String> bindings = new HashMap<>(); |
| 27 | |
| 28 | /** |
| 29 | * Create a NamespaceContext object |
| 30 | * |
| 31 | * @param nsBindings an array of namespace bindings. Each namespace code is an integer |
| 32 | * in which the first 16 bits represent the prefix (zero if it's the default namespace) |
| 33 | * and the next 16 bits represent the uri. These are codes held in the NamePool. The |
| 34 | * list will be searched from the "high" end. |
| 35 | */ |
| 36 | |
| 37 | public SavedNamespaceContext(/*@NotNull*/ Iterable<NamespaceBinding> nsBindings) { |
| 38 | this(nsBindings.iterator()); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Create a NamespaceContext object |
| 43 | * |
| 44 | * @param nsBindings an array of namespace bindings. Each namespace code is an integer |
| 45 | * in which the first 16 bits represent the prefix (zero if it's the default namespace) |
| 46 | * and the next 16 bits represent the uri. These are codes held in the NamePool. The |
| 47 | * list will be searched from the "high" end. |
| 48 | */ |
| 49 | |
| 50 | public SavedNamespaceContext(Iterator<NamespaceBinding> nsBindings) { |
| 51 | while (nsBindings.hasNext()) { |
| 52 | NamespaceBinding next = nsBindings.next(); |
| 53 | bindings.put(next.getPrefix(), next.getURI()); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Create a NamespaceContext object, excluding some namespaces |
| 59 | * |
| 60 | * @param nsBindings an array of namespace bindings. Each namespace code is an integer |
| 61 | * in which the first 16 bits represent the prefix (zero if it's the default namespace) |
| 62 | * and the next 16 bits represent the uri. These are codes held in the NamePool. The |
| 63 | * list will be searched from the "high" end. |
| 64 | */ |
| 65 | |
| 66 | public SavedNamespaceContext(Iterator<NamespaceBinding> nsBindings, Predicate<NamespaceBinding> excluded) { |
| 67 | while (nsBindings.hasNext()) { |
| 68 | NamespaceBinding next = nsBindings.next(); |
| 69 | if (!excluded.test(next)) { |
| 70 | bindings.put(next.getPrefix(), next.getURI()); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Create a SavedNamespaceContext that captures all the information in a given NamespaceResolver |
| 77 | * |
| 78 | * @param resolver the NamespaceResolver |
| 79 | */ |
| 80 | |
| 81 | public SavedNamespaceContext(/*@NotNull*/ NamespaceResolver resolver) { |
| 82 | Iterator iter = resolver.iteratePrefixes(); |
| 83 | while (iter.hasNext()) { |
| 84 | String prefix = (String) iter.next(); |
| 85 | String uri = resolver.getURIForPrefix(prefix, true); |
| 86 | bindings.put(prefix, uri); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get the namespace URI corresponding to a given prefix. Return null |
| 92 | * if the prefix is not in scope. |
| 93 | * |
| 94 | * @param prefix the namespace prefix |
| 95 | * @param useDefault true if the default namespace is to be used when the |
| 96 | * prefix is "" |
| 97 | * @return the uri for the namespace, or null if the prefix is not in scope |
| 98 | */ |
| 99 | |
| 100 | /*@Nullable*/ |
| 101 | public String getURIForPrefix(/*@NotNull*/ String prefix, boolean useDefault) { |
| 102 | |
| 103 | if (prefix.isEmpty() && !useDefault) { |
| 104 | return NamespaceConstant.NULL; |
| 105 | } |
| 106 | |
| 107 | if (prefix.equals("xml")) { |
| 108 | return NamespaceConstant.XML; |
| 109 | } |
| 110 | |
| 111 | String uri = bindings.get(prefix); |
| 112 | if (uri == null) { |
| 113 | return prefix.isEmpty() ? NamespaceConstant.NULL : null; |
| 114 | } else { |
| 115 | return uri; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get an iterator over all the prefixes declared in this namespace context. This will include |
| 121 | * the default namespace (prefix="") and the XML namespace where appropriate |
| 122 | */ |
| 123 | |
| 124 | public Iterator<String> iteratePrefixes() { |
| 125 | List<String> prefixes = new ArrayList<>(bindings.size() + 1); |
| 126 | prefixes.addAll(bindings.keySet()); |
| 127 | prefixes.add("xml"); |
| 128 | return prefixes.iterator(); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Compare this saved namespace context to another (so that they can be shared) |
| 133 | * @param obj the other namespace context |
| 134 | */ |
| 135 | |
| 136 | @Override |
| 137 | public boolean equals(Object obj) { |
| 138 | return obj instanceof SavedNamespaceContext && bindings.equals(((SavedNamespaceContext) obj).bindings); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public int hashCode() { |
| 143 | return bindings.hashCode(); |
| 144 | } |
| 145 | } |
| 146 |