Debian Codegraph @

net/sf/saxon/tree/util/NamespaceResolverWithDefault.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.om.NamespaceResolver;
11
12 import java.util.ArrayList;
13 import java.util.Iterator;
14
15 /**
16 * This class is a NamespaceResolver that modifies an underyling NamespaceResolver
17 * by changing the mapping of the null prefix to be a specified namespace, rather than
18 * the one used by the underlying namespace resolver.
19 */
20 public class NamespaceResolverWithDefault implements NamespaceResolver {
21
22 private NamespaceResolver baseResolver;
23 private String defaultNamespace;
24
25 public NamespaceResolverWithDefault(NamespaceResolver base, String defaultNamespace) {
26 this.baseResolver = base;
27 this.defaultNamespace = defaultNamespace;
28 }
29
30 /**
31 * Get the namespace URI corresponding to a given prefix. Return null
32 * if the prefix is not in scope.
33 *
34 * @param prefix the namespace prefix. May be the zero-length string, indicating
35 * that there is no prefix. This indicates either the default namespace or the
36 * null namespace, depending on the value of useDefault.
37 * @param useDefault true if the default namespace is to be used when the
38 * prefix is "". If false, the method returns "" when the prefix is "".
39 * @return the uri for the namespace, or null if the prefix is not in scope.
40 * The "null namespace" is represented by the pseudo-URI "".
41 */
42
43 /*@Nullable*/
44 public String getURIForPrefix(/*@NotNull*/ String prefix, boolean useDefault) {
45 if (useDefault && prefix.isEmpty()) {
46 return defaultNamespace;
47 } else {
48 return baseResolver.getURIForPrefix(prefix, useDefault);
49 }
50 }
51
52 /**
53 * Get an iterator over all the prefixes declared in this namespace context. This will include
54 * the default namespace (prefix="") and the XML namespace where appropriate
55 */
56
57 public Iterator<String> iteratePrefixes() {
58 ArrayList<String> list = new ArrayList<String>(10);
59 for (Iterator<String> it = baseResolver.iteratePrefixes(); it.hasNext(); ) {
60 String p = it.next();
61 if (p.length() != 0) {
62 list.add(p);
63 }
64 }
65 list.add("");
66 return list.iterator();
67 }
68 }
69