Debian Codegraph @

net/sf/saxon/om/FingerprintedQName.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 /**
11 * A QName triple (prefix, URI, local) with the additional ability to hold an integer fingerprint.
12 * The integer fingerprint provides a fast way of checking equality. A FingerprintedQName makes sense
13 * only in the context of a known NamePool, and instances must be compared only if they relate to the
14 * same NamePool. The fingerprint is optional, and is used only if present.
15 */
16 public class FingerprintedQName implements NodeName {
17
18 private StructuredQName qName;
19 private int fingerprint = -1;
20
21 public FingerprintedQName(String prefix, String uri, String localName) {
22 qName = new StructuredQName(prefix, uri, localName);
23 }
24
25 public FingerprintedQName(String prefix, String uri, String localName, int fingerprint) {
26 qName = new StructuredQName(prefix, uri, localName);
27 this.fingerprint = fingerprint;
28 }
29
30 public FingerprintedQName(String prefix, String uri, String localName, NamePool pool) {
31 qName = new StructuredQName(prefix, uri, localName);
32 this.fingerprint = pool.allocateFingerprint(uri, localName);
33 }
34
35 public FingerprintedQName(StructuredQName qName, int fingerprint) {
36 this.qName = qName;
37 this.fingerprint = fingerprint;
38 }
39
40 public FingerprintedQName(StructuredQName qName, NamePool pool) {
41 this.qName = qName;
42 this.fingerprint = pool.allocateFingerprint(qName.getURI(), qName.getLocalPart());
43 }
44
45 /**
46 * Make a FingerprintedQName from a Clark name
47 *
48 * @param expandedName the name in Clark notation "{uri}local" if in a namespace, or "local" otherwise.
49 * The format "{}local" is also accepted for a name in no namespace.
50 * @return the constructed FingerprintedQName
51 * @throws IllegalArgumentException if the Clark name is malformed
52 */
53
54 public static FingerprintedQName fromClarkName(String expandedName) {
55 String namespace;
56 String localName;
57 if (expandedName.charAt(0) == '{') {
58 int closeBrace = expandedName.indexOf('}');
59 if (closeBrace < 0) {
60 throw new IllegalArgumentException("No closing '}' in Clark name");
61 }
62 namespace = expandedName.substring(1, closeBrace);
63 if (closeBrace == expandedName.length()) {
64 throw new IllegalArgumentException("Missing local part in Clark name");
65 }
66 localName = expandedName.substring(closeBrace + 1);
67 } else {
68 namespace = "";
69 localName = expandedName;
70 }
71 return new FingerprintedQName("", namespace, localName);
72 }
73
74 /**
75 * Ask whether this node name representation has a known namecode and fingerprint
76 *
77 * @return true if the methods getFingerprint() and getNameCode() will
78 * return a result other than -1
79 */
80 public boolean hasFingerprint() {
81 return fingerprint != -1;
82 }
83
84 /**
85 * Get the fingerprint of this name if known. This method should not to any work to allocate
86 * a fingerprint if none is already available
87 *
88 * @return the fingerprint if known; otherwise -1
89 */
90 public int getFingerprint() {
91 return fingerprint;
92 }
93
94 /**
95 * Get the fingerprint of this name, allocating a new code from the namepool if necessary
96 *
97 * @param pool the NamePool used to allocate the name
98 * @return a fingerprint for this name, newly allocated if necessary
99 */
100
101 public int obtainFingerprint(NamePool pool) {
102 if (fingerprint == -1) {
103 fingerprint = pool.allocateFingerprint(getURI(), getLocalPart());
104 }
105 return fingerprint;
106 }
107
108 /**
109 * Get the display name, that is the lexical QName in the form [prefix:]local-part
110 *
111 * @return the lexical QName
112 */
113 public String getDisplayName() {
114 return qName.getDisplayName();
115 }
116
117 /**
118 * Get the prefix of the QName.
119 *
120 * @return the prefix. Returns the empty string if the name is unprefixed.
121 */
122 public String getPrefix() {
123 return qName.getPrefix();
124 }
125
126 /**
127 * Get the namespace URI of the QName.
128 *
129 * @return the URI. Returns the empty string to represent the no-namespace
130 */
131 public String getURI() {
132 return qName.getURI();
133 }
134
135 /**
136 * Get the local part of the QName
137 *
138 * @return the local part of the QName
139 */
140 public String getLocalPart() {
141 return qName.getLocalPart();
142 }
143
144 /**
145 * Get the name in the form of a StructuredQName
146 *
147 * @return the name in the form of a StructuredQName
148 */
149 public StructuredQName getStructuredQName() {
150 return qName;
151 }
152
153 /**
154 * Test whether this name is in the same namespace as another name
155 *
156 * @param other the other name
157 * @return true if the two names are in the same namespace
158 */
159 public boolean isInSameNamespace(NodeName other) {
160 return qName.isInSameNamespace(other);
161 }
162
163 /**
164 * Test whether this name is in a given namespace
165 *
166 * @param ns the namespace to be tested against
167 * @return true if the name is in the specified namespace
168 */
169 public boolean hasURI(String ns) {
170 return qName.hasURI(ns);
171 }
172
173 /**
174 * Get a {@link NamespaceBinding} whose (prefix, uri) pair are the prefix and URI of this
175 * node name
176 *
177 * @return the corresponding NamespaceBinding
178 */
179 public NamespaceBinding getNamespaceBinding() {
180 return qName.getNamespaceBinding();
181 }
182
183 /**
184 * Get a hashCode that offers the guarantee that if A.isIdentical(B), then A.identityHashCode() == B.identityHashCode()
185 *
186 * @return a hashCode suitable for use when testing for identity.
187 */
188 public int identityHashCode() {
189 return 0;
190 }
191
192 /*
193 * Compare two names for equality
194 */
195
196 public boolean equals(/*@NotNull*/ Object other) {
197 if (other instanceof NodeName) {
198 if (fingerprint != -1 && ((NodeName) other).hasFingerprint()) {
199 return getFingerprint() == ((NodeName) other).getFingerprint();
200 } else {
201 return getLocalPart().equals(((NodeName) other).getLocalPart()) &&
202 isInSameNamespace((NodeName) other);
203 }
204 } else {
205 return false;
206 }
207 }
208
209 /**
210 * Returns a hash code value for the object.
211 */
212 @Override
213 public int hashCode() {
214 return qName.hashCode();
215 }
216
217 /**
218 * Determine whether two IdentityComparable objects are identical. This is a stronger
219 * test than equality (even schema-equality); for example two dateTime values are not identical unless
220 * they are in the same timezone.
221 *
222 * @param other the other value
223 * @return true if the two values are indentical, false otherwise
224 */
225 public boolean isIdentical(IdentityComparable other) {
226 return other instanceof NodeName &&
227 this.equals(other) &&
228 this.getPrefix().equals(((NodeName) other).getPrefix());
229 }
230
231 @Override
232 public String toString() {
233 return qName.getDisplayName();
234 }
235 }