Debian Codegraph @

net/sf/saxon/pull/NamespaceContextImpl.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.pull;
9
10 import net.sf.saxon.om.NamespaceResolver;
11
12 import javax.xml.namespace.NamespaceContext;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16
17 /**
18 * This class bridges between the JAXP 1.3 NamespaceContext interface and Saxon's
19 * equivalent NamespaceResolver interface. It allows any implementation of the Saxon
20 * NamespaceResolver to be wrapped as a JAXP NamespaceContext.
21 */
22
23 public class NamespaceContextImpl implements NamespaceContext, NamespaceResolver {
24
25 NamespaceResolver resolver;
26
27 /**
28 * Constructor: wrap a Saxon NamespaceResolver as a JAXP NamespaceContext
29 *
30 * @param resolver the Saxon NamespaceResolver
31 */
32
33 public NamespaceContextImpl(NamespaceResolver resolver) {
34 this.resolver = resolver;
35 }
36
37 /**
38 * Get the namespace URI corresponding to a given prefix. Return null
39 * if the prefix is not in scope.
40 *
41 * @param prefix the namespace prefix
42 * @param useDefault true if the default namespace is to be used when the
43 * prefix is ""
44 * @return the uri for the namespace, or null if the prefix is not in scope
45 */
46
47 /*@Nullable*/
48 public String getURIForPrefix(String prefix, boolean useDefault) {
49 return resolver.getURIForPrefix(prefix, useDefault);
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 return resolver.iteratePrefixes();
59 }
60
61 /**
62 * Implement the JAXP getNamespaceURI() method in terms of the Saxon-specific methods
63 *
64 * @param prefix a namespace prefix
65 * @return the corresponding URI, if the prefix is bound, or "" otherwise
66 */
67
68 public String getNamespaceURI(String prefix) {
69 if (prefix.equals("xmlns")) {
70 return "http://www.w3.org/2000/xmlns/";
71 }
72 return resolver.getURIForPrefix(prefix, true);
73 }
74
75 /**
76 * Get the prefix bound to a particular namespace URI, if there is one, or null if not (JAXP method)
77 *
78 * @param uri the namespace URI
79 * @return the prefix bound to the URI if there is one, or null if not
80 */
81
82 public String getPrefix(String uri) {
83 Iterator prefixes = iteratePrefixes();
84 while (prefixes.hasNext()) {
85 String p = (String) prefixes.next();
86 String u = resolver.getURIForPrefix(p, true);
87 if (u.equals(uri)) {
88 return p;
89 }
90 }
91 return null;
92 }
93
94 /**
95 * Get all the prefixes mapped to a given namespace URI (JAXP method)
96 *
97 * @param uri the namespace URI
98 * @return an iterator over all the prefixes bound to this namespace URI
99 */
100 public Iterator<String> getPrefixes(String uri) {
101 List<String> list = new ArrayList<>(4);
102 Iterator<String> prefixes = iteratePrefixes();
103 prefixes.forEachRemaining(p -> {
104 String u = resolver.getURIForPrefix(p, true);
105 if (u.equals(uri)) {
106 list.add(p);
107 }
108 });
109 return list.iterator();
110 }
111 }
112