Debian Codegraph @

net/sf/saxon/om/NamespaceBinding.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.om;
9
10 import net.sf.saxon.tree.jiter.MonoIterator;
11 import net.sf.saxon.lib.NamespaceConstant;
12
13 import java.util.Iterator;
14
15 /**
16 * Represents the binding of a prefix to a URI. Also, in some contexts, represents an unbinding, by
17 * virtue of the URI being set to a zero length string.
18 *
19 * @since 9.4
20 */
21 public final class NamespaceBinding implements NamespaceBindingSet {
22
23 private String prefix;
24 private String uri;
25
26 public final static NamespaceBinding XML = new NamespaceBinding("xml", NamespaceConstant.XML);
27 public final static NamespaceBinding DEFAULT_UNDECLARATION = new NamespaceBinding("", "");
28
29 public final static NamespaceBinding[] EMPTY_ARRAY = new NamespaceBinding[0];
30
31 /**
32 * Create a binding of a prefix to a URI
33 *
34 * @param prefix the prefix: either an NCName, or a zero-length string to bind the default namespace.
35 * Must not be null.
36 * @param uri the namespace URI: either a URI, or a zero-length string to unbind the prefix. Must
37 * not be null.
38 */
39
40 public NamespaceBinding(String prefix, String uri) {
41 this.prefix = prefix;
42 this.uri = uri;
43 if (prefix == null || uri == null) {
44 throw new NullPointerException();
45 }
46 }
47
48 /**
49 * Create a binding of a prefix to a URI. Static factory method for the convenience of compiled bytecode;
50 * reuses standard NamespaceBinding objects where possible
51 *
52 * @param prefix the prefix: either an NCName, or a zero-length string to bind the default namespace.
53 * Must not be null.
54 * @param uri the namespace URI: either a URI, or a zero-length string to unbind the prefix. Must
55 * not be null.
56 * @return the namespace binding object
57 */
58
59 public static NamespaceBinding makeNamespaceBinding(CharSequence prefix, CharSequence uri) {
60 if (prefix.length()==0 && uri.length()==0) {
61 return DEFAULT_UNDECLARATION;
62 } else if (prefix.equals("xml") && uri.equals(NamespaceConstant.XML)) {
63 return XML;
64 } else {
65 return new NamespaceBinding(prefix.toString(), uri.toString());
66 }
67 }
68
69 /**
70 * Get the prefix part of the binding
71 *
72 * @return the prefix. Never null. The zero-length string indicates a binding for the default namespace.
73 */
74
75 public String getPrefix() {
76 return prefix;
77 }
78
79 /**
80 * Get the URI part of the binding
81 *
82 * @return the URI. Never null. The zero-length string indicates an unbinding of the prefix. For the
83 * default namespace (prefix="") this indicates that the prefix refers to names in no namespace; for other
84 * prefixes, it indicates that the prefix is not bound to any namespace and therefore cannot be used.
85 */
86
87 public String getURI() {
88 return uri;
89 }
90
91 /**
92 * Ask whether this is a binding for the XML namespace
93 *
94 * @return true if this is the binding of the prefix "xml" to the standard XML namespace.
95 */
96
97 public boolean isXmlNamespace() {
98 return prefix.equals("xml");
99 }
100
101 /**
102 * Ask whether this is an undeclaration of the default prefix, that is, a namespace binding
103 * corresponding to <code>xmlns=""</code>
104 *
105 * @return true if this corresponding to <code>xmlns=""</code>
106 */
107
108 public boolean isDefaultUndeclaration() {
109 return prefix.isEmpty() && uri.isEmpty();
110 }
111
112 /**
113 * Returns an iterator over this singleton set of namespace bindings.
114 *
115 * @return an Iterator.
116 */
117 @Override
118 public Iterator<NamespaceBinding> iterator() {
119 return new MonoIterator<NamespaceBinding>(this);
120 }
121
122 /**
123 * Test if this namespace binding is the same as another
124 *
125 * @param obj the comparand
126 * @return true if the comparand is a Namespace binding of the same prefix to the same URI
127 */
128 @Override
129 public boolean equals(Object obj) {
130 return obj instanceof NamespaceBinding &&
131 prefix.equals(((NamespaceBinding) obj).getPrefix()) &&
132 uri.equals(((NamespaceBinding) obj).getURI());
133 }
134
135 @Override
136 public int hashCode() {
137 return prefix.hashCode() ^ uri.hashCode();
138 }
139 }
140