Debian Codegraph @

net/sf/saxon/om/NameOfNode.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.wrapper.AbstractVirtualNode;
11
12 /**
13 * An implementation of NodeName that gets the name of an existing NodeInfo object.
14 * Useful when nodes are copied. However, it's not safe to use when the node is mutable.
15 */
16 public class NameOfNode implements NodeName {
17
18 private NodeInfo node;
19
20 /**
21 * Constructor is private to protect against use with mutable nodes
22 * @param node the node whose name is required
23 */
24
25 private NameOfNode(NodeInfo node) {
26 this.node = node;
27 }
28
29 /**
30 * Make a NodeName object based on the name of a supplied node.
31 * @param node the supplied node
32 * @return a NameOfNode object unless the node is mutable, in which case an immutable name is returned.
33 */
34
35 public static NodeName makeName(NodeInfo node) {
36 if (node instanceof MutableNodeInfo) {
37 return new FingerprintedQName(node.getPrefix(), node.getURI(), node.getLocalPart());
38 } else if (node instanceof AbstractVirtualNode) {
39 return new NameOfNode(((AbstractVirtualNode)node).getUnderlyingNode());
40 } else {
41 return new NameOfNode(node);
42 }
43 }
44
45 /**
46 * Get the prefix of the QName.
47 *
48 * @return the prefix. Returns the empty string if the name is unprefixed.
49 */
50 public String getPrefix() {
51 return node.getPrefix();
52 }
53
54 /**
55 * Get the namespace URI of the QName.
56 *
57 * @return the URI. Returns the empty string to represent the no-namespace
58 */
59 public String getURI() {
60 return node.getURI();
61 }
62
63 /**
64 * Get the local part of the QName
65 *
66 * @return the local part of the QName
67 */
68 public String getLocalPart() {
69 return node.getLocalPart();
70 }
71
72 /**
73 * Get the display name, that is the lexical QName in the form [prefix:]local-part
74 *
75 * @return the lexical QName
76 */
77 public String getDisplayName() {
78 return node.getDisplayName();
79 }
80
81 /**
82 * Get the name in the form of a StructuredQName
83 *
84 * @return the name in the form of a StructuredQName
85 */
86 public StructuredQName getStructuredQName() {
87 return new StructuredQName(getPrefix(), getURI(), getLocalPart());
88 }
89
90 /**
91 * Test whether this name is in the same namespace as another name
92 *
93 * @return true if the two names are in the same namespace
94 */
95 public boolean isInSameNamespace(/*@NotNull*/ NodeName other) {
96 return node.getURI().equals(other.getURI());
97 }
98
99 /**
100 * Test whether this name is in a given namespace
101 *
102 * @param ns the namespace to be tested against
103 * @return true if the name is in the specified namespace
104 */
105 public boolean hasURI(String ns) {
106 return node.getURI().equals(ns);
107 }
108
109 /**
110 * Get a {@link net.sf.saxon.om.NamespaceBinding} whose (prefix, uri) pair are the prefix and URI of this
111 * node name
112 *
113 * @return the corresponding NamespaceBinding
114 */
115
116 public NamespaceBinding getNamespaceBinding() {
117 return NamespaceBinding.makeNamespaceBinding(getPrefix(), getURI());
118 }
119
120 /**
121 * Ask whether this node name representation has a known namecode and fingerprint
122 *
123 * @return true if the methods getFingerprint() and getNameCode() will
124 * return a result other than -1
125 */
126 public boolean hasFingerprint() {
127 return node.hasFingerprint();
128 }
129
130 /**
131 * Get the fingerprint of this name if known. This method should not to any work to allocate
132 * a fingerprint if none is already available
133 *
134 * @return the fingerprint if known; otherwise -1
135 */
136 public int getFingerprint() {
137 if (hasFingerprint()) {
138 return node.getFingerprint();
139 } else {
140 return -1;
141 }
142 }
143
144 /**
145 * Get the nameCode of this name, allocating a new code from the namepool if necessary
146 *
147 * @param namePool the NamePool used to allocate the name
148 * @return a nameCode for this name, newly allocated if necessary
149 */
150 public int obtainFingerprint(NamePool namePool) {
151 if (node.hasFingerprint()) {
152 return node.getFingerprint();
153 } else {
154 return namePool.allocateFingerprint(node.getURI(), node.getLocalPart());
155 }
156 }
157
158 /**
159 * Returns a hash code value for the object.
160 */
161 @Override
162 public int hashCode() {
163 return StructuredQName.computeHashCode(getURI(), getLocalPart());
164 }
165
166 /**
167 * Indicates whether some other object is "equal to" this one.
168 */
169 @Override
170 public boolean equals(Object obj) {
171 if (obj instanceof NodeName) {
172 NodeName n = (NodeName) obj;
173 if (node.hasFingerprint() && n.hasFingerprint()) {
174 return node.getFingerprint() == n.getFingerprint();
175 } else {
176 return n.getLocalPart().equals(node.getLocalPart()) && n.hasURI(node.getURI());
177 }
178 } else {
179 return false;
180 }
181 }
182
183 /**
184 * Determine whether two IdentityComparable objects are identical. This is a stronger
185 * test than equality (even schema-equality); for example two dateTime values are not identical unless
186 * they are in the same timezone.
187 *
188 * @param other
189 * @return true if the two values are indentical, false otherwise
190 */
191 public boolean isIdentical(IdentityComparable other) {
192 return other instanceof NodeName &&
193 this.equals(other) && this.getPrefix().equals(((NodeName) other).getPrefix());
194 }
195
196 /**
197 * Get a hashCode that offers the guarantee that if A.isIdentical(B), then A.identityHashCode() == B.identityHashCode()
198 *
199 * @return a hashCode suitable for use when testing for identity.
200 */
201 public int identityHashCode() {
202 return hashCode() ^ getPrefix().hashCode();
203 }
204 }