Debian Codegraph @

net/sf/saxon/om/StructuredQName.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.trans.XPathException;
11 import net.sf.saxon.tree.util.FastStringBuffer;
12 import net.sf.saxon.value.Whitespace;
13
14 import javax.xml.namespace.QName;
15 import java.util.Arrays;
16
17 /**
18 * This class provides an economical representation of a QName triple (prefix, URI, and localname).
19 * The value is stored internally as a character array containing the concatenation of URI, localname,
20 * and prefix (in that order) with two integers giving the start positions of the localname and prefix.
21 * <p><i>Instances of this class are immutable.</i></p>
22 */
23
24 public class StructuredQName implements IdentityComparable {
25
26 private char[] content;
27 private int localNameStart;
28 private int prefixStart;
29 private int cachedHashCode = -1;
30
31 private StructuredQName(char[] content, int localNameStart, int prefixStart) {
32 this.content = content;
33 this.localNameStart = localNameStart;
34 this.prefixStart = prefixStart;
35 }
36
37 /**
38 * Construct a StructuredQName from a prefix, URI, and local name. This method performs no validation.
39 *
40 * @param prefix The prefix. Use an empty string to represent the null prefix.
41 * @param uri The namespace URI. Use an empty string or null to represent the no-namespace
42 * @param localName The local part of the name
43 */
44
45 public StructuredQName(String prefix, /*@Nullable*/ String uri, String localName) {
46 if (uri == null) {
47 uri = "";
48 }
49 int plen = prefix.length();
50 int ulen = uri.length();
51 int llen = localName.length();
52 localNameStart = ulen;
53 prefixStart = ulen + llen;
54 content = new char[ulen + llen + plen];
55 uri.getChars(0, ulen, content, 0);
56 localName.getChars(0, llen, content, ulen);
57 prefix.getChars(0, plen, content, ulen + llen);
58 }
59
60 /**
61 * Make a StructuredQName from a StructuredQName holding the local name and URI, plus a new prefix
62 */
63
64 public StructuredQName addPrefix(String prefix) {
65 char[] content2 = Arrays.copyOf(content, content.length + prefix.length());
66 prefix.getChars(0, prefix.length(), content2, content.length);
67 return new StructuredQName(content2, localNameStart, content.length);
68 }
69
70 /**
71 * Make a structuredQName from a Clark name
72 *
73 * @param expandedName the name in Clark notation "{uri}local" if in a namespace, or "local" otherwise.
74 * The format "{}local" is also accepted for a name in no namespace. The EQName syntax (Q{uri}local) is
75 * also accepted.
76 * @return the constructed StructuredQName
77 * @throws IllegalArgumentException if the Clark name is malformed
78 */
79
80 public static StructuredQName fromClarkName(String expandedName) {
81 String namespace;
82 String localName;
83 if (expandedName.startsWith("Q{")) {
84 expandedName = expandedName.substring(1);
85 }
86 if (expandedName.charAt(0) == '{') {
87 int closeBrace = expandedName.indexOf('}');
88 if (closeBrace < 0) {
89 throw new IllegalArgumentException("No closing '}' in Clark name");
90 }
91 namespace = expandedName.substring(1, closeBrace);
92 if (closeBrace == expandedName.length()) {
93 throw new IllegalArgumentException("Missing local part in Clark name");
94 }
95 localName = expandedName.substring(closeBrace + 1);
96 } else {
97 namespace = "";
98 localName = expandedName;
99 }
100 return new StructuredQName("", namespace, localName);
101 }
102
103 /**
104 * Make a structured QName from a lexical QName, using a supplied NamespaceResolver to
105 * resolve the prefix
106 *
107 *
108 * @param lexicalName the QName as a lexical name (prefix:local)
109 * @param useDefault set to true if an absent prefix implies use of the default namespace;
110 * set to false if an absent prefix implies no namespace
111 * @param allowEQName true if the EQName syntax Q{uri}local is acceptable
112 * @param resolver NamespaceResolver used to look up a URI for the prefix
113 * @return the StructuredQName object corresponding to this lexical QName
114 * @throws XPathException if the namespace prefix is not in scope or if the value is lexically
115 * invalid. Error code FONS0004 is set if the namespace prefix has not been declared; error
116 * code FOCA0002 is set if the name is lexically invalid. These may need to be
117 * changed on return depending on the caller's requirements.
118 */
119
120 public static StructuredQName fromLexicalQName(CharSequence lexicalName, boolean useDefault,
121 boolean allowEQName, NamespaceResolver resolver)
122 throws XPathException {
123 lexicalName = Whitespace.trimWhitespace(lexicalName);
124 if (allowEQName && lexicalName.length() >= 4 && lexicalName.charAt(0) == 'Q' && lexicalName.charAt(1) == '{') {
125 String name = lexicalName.toString();
126 int endBrace = name.indexOf('}');
127 if (endBrace < 0) {
128 throw new XPathException("Invalid EQName: closing brace not found");
129 } else if (endBrace == name.length() - 1) {
130 throw new XPathException("Invalid EQName: local part is missing");
131 }
132 String uri = name.substring(2, endBrace);
133 String local = name.substring(endBrace + 1);
134 if (!NameChecker.isValidNCName(local)) {
135 throw new XPathException("Invalid EQName: local part is not a valid NCName");
136 }
137 return new StructuredQName("", uri, local);
138 }
139 try {
140 String[] parts = NameChecker.getQNameParts(lexicalName);
141 String uri = resolver.getURIForPrefix(parts[0], useDefault);
142 if (uri == null) {
143 if (NameChecker.isValidNCName(parts[0])) {
144 XPathException de = new XPathException("Namespace prefix '" + parts[0] + "' has not been declared");
145 de.setErrorCode("FONS0004");
146 throw de;
147 } else {
148 XPathException de = new XPathException("Invalid namespace prefix '" + parts[0] + "'");
149 de.setErrorCode("FOCA0002");
150 throw de;
151 }
152 }
153 return new StructuredQName(parts[0], uri, parts[1]);
154 } catch (QNameException e) {
155 XPathException de = new XPathException(e.getMessage());
156 de.setErrorCode("FOCA0002");
157 throw de;
158 }
159 }
160
161 /**
162 * Make a structured QName from an EQName in {@code Q{uri}local} format.
163 *
164 * @param eqName the QName as an EQName ({@code Q{uri}local}), or an unqualified local name. The format
165 * of the local name is not checked.
166 * @return the StructuredQName object corresponding to this EQName
167 * @throws IllegalArgumentException if the eqName syntax is invalid (but the format of the
168 * URI and local name parts are not checked)
169 */
170
171 public static StructuredQName fromEQName(CharSequence eqName) {
172 eqName = Whitespace.trimWhitespace(eqName);
173 if (eqName.length() >= 4 && eqName.charAt(0) == 'Q' && eqName.charAt(1) == '{') {
174 String name = eqName.toString();
175 int endBrace = name.indexOf('}');
176 if (endBrace < 0) {
177 throw new IllegalArgumentException("Invalid EQName: closing brace not found");
178 } else if (endBrace == name.length() - 1) {
179 throw new IllegalArgumentException("Invalid EQName: local part is missing");
180 }
181 String uri = name.substring(2, endBrace);
182 String local = name.substring(endBrace + 1);
183 return new StructuredQName("", uri, local);
184 } else {
185 return new StructuredQName("", "", eqName.toString());
186 }
187 }
188
189
190 /**
191 * Get the prefix of the QName.
192 *
193 * @return the prefix. Returns the empty string if the name is unprefixed.
194 */
195
196 public String getPrefix() {
197 return new String(content, prefixStart, content.length - prefixStart);
198 }
199
200 /**
201 * Get the namespace URI of the QName.
202 *
203 * @return the URI. Returns the empty string to represent the no-namespace
204 */
205
206 public String getURI() {
207 if (localNameStart == 0) {
208 return "";
209 }
210 return new String(content, 0, localNameStart);
211 }
212
213 /**
214 * Test whether the URI is equal to some constant
215 * @param uri the namespace URI to be tested
216 * @return true if the namespace URI of this QName is equal to the supplied URI
217 */
218
219 public boolean hasURI(String uri) {
220 if (localNameStart != uri.length()) {
221 return false;
222 }
223 for (int i = localNameStart - 1; i >= 0; i--) {
224 // compare from the end of the URI to maximize chance of finding a difference quickly
225 if (content[i] != uri.charAt(i)) {
226 return false;
227 }
228 }
229 return true;
230 }
231
232 /**
233 * Get the local part of the QName
234 *
235 * @return the local part of the QName
236 */
237
238 public String getLocalPart() {
239 return new String(content, localNameStart, prefixStart - localNameStart);
240 }
241
242 /**
243 * Get the display name, that is the lexical QName in the form [prefix:]local-part
244 *
245 * @return the lexical QName
246 */
247
248 public String getDisplayName() {
249 if (prefixStart == content.length) {
250 return getLocalPart();
251 } else {
252 FastStringBuffer buff = new FastStringBuffer(content.length - localNameStart + 1);
253 buff.append(content, prefixStart, content.length - prefixStart);
254 buff.append(':');
255 buff.append(content, localNameStart, prefixStart - localNameStart);
256 return buff.toString();
257 }
258 }
259
260 /**
261 * Get the name as a StructuredQName (which it already is; but this satisfies the NodeName interface)
262 * @return the name as a StructuredQName
263 */
264
265 public StructuredQName getStructuredQName() {
266 return this;
267 }
268
269 /**
270 * Get the expanded QName in Clark format, that is "{uri}local" if it is in a namespace, or just "local"
271 * otherwise.
272 *
273 * @return the QName in Clark notation
274 */
275
276 public String getClarkName() {
277 FastStringBuffer buff = new FastStringBuffer(content.length - prefixStart + 2);
278 if (localNameStart > 0) {
279 buff.append('{');
280 buff.append(content, 0, localNameStart);
281 buff.append('}');
282 }
283 buff.append(content, localNameStart, prefixStart - localNameStart);
284 return buff.toString();
285 }
286
287 /**
288 * Get the expanded QName as an EQName, that is "Q{uri}local" for a name in a namespace,
289 * or "Q{}local" otherwise
290 *
291 * @return the QName in EQName notation
292 */
293
294 public String getEQName() {
295 FastStringBuffer buff = new FastStringBuffer(content.length - prefixStart + 2);
296 buff.append("Q{");
297 if (localNameStart > 0) {
298 buff.append(content, 0, localNameStart);
299 }
300 buff.append('}');
301 buff.append(content, localNameStart, prefixStart - localNameStart);
302 return buff.toString();
303 }
304
305 /**
306 * The toString() method displays the QName as a lexical QName, that is prefix:local
307 *
308 * @return the lexical QName
309 */
310
311 public String toString() {
312 return getDisplayName();
313 }
314
315 /**
316 * Compare two StructuredQName values for equality. This compares the URI and local name parts,
317 * excluding any prefix
318 */
319
320 public boolean equals(Object other) {
321 if (this == other) {
322 return true;
323 }
324 if (other instanceof StructuredQName) {
325 int c = ((StructuredQName) other).cachedHashCode;
326 if (c != -1 && c != hashCode()) {
327 return false;
328 }
329 StructuredQName sq2 = (StructuredQName) other;
330 if (localNameStart != sq2.localNameStart || prefixStart != sq2.prefixStart) {
331 return false;
332 }
333 for (int i = prefixStart - 1; i >= 0; i--) {
334 // compare from the end of the local name to maximize chance of finding a difference quickly
335 if (content[i] != sq2.content[i]) {
336 return false;
337 }
338 }
339 return true;
340 } else {
341 return false;
342 }
343 }
344
345 /**
346 * Get a hashcode to reflect the equals() method.
347 *
348 * <p>The hashcode is based on the URI and local part only, ignoring the prefix. In fact the URI plays little
349 * part in computing the hashcode, because the URI is often long, and largely redundant: QNames with the same
350 * local name will rarely have different URIs, and there are significant performance savings in the NamePool
351 * if a cheaper hashcode is used. So the only contribution from the URI is that we take its length into account.</p>
352 *
353 * @return a hashcode used to distinguish distinct QNames
354 */
355
356 public int hashCode() {
357 if (cachedHashCode == -1) {
358 int h = 0x8004a00b;
359 h ^= prefixStart;
360 h ^= localNameStart;
361 for (int i = localNameStart; i < prefixStart; i++) {
362 h ^= content[i] << (i & 0x1f);
363 }
364 return cachedHashCode = h;
365 } else {
366 return cachedHashCode;
367 }
368 }
369
370 /**
371 * Expose the hashCode algorithm so that other implementations of QNames can construct a compatible hashcode
372 *
373 * @param uri the namespace URI
374 * @param local the local name
375 * @return a hash code computed from the URI and local name
376 */
377
378 public static int computeHashCode(CharSequence uri, CharSequence local) {
379 int h = 0x8004a00b;
380 int localLen = local.length();
381 int uriLen = uri.length();
382 int totalLen = localLen + uriLen;
383 h ^= totalLen;
384 h ^= uriLen;
385 for (int i = 0, j = uriLen; i < localLen; i++, j++) {
386 h ^= local.charAt(i) << (j & 0x1f);
387 }
388 return h;
389 }
390
391 /**
392 * Test whether this name is in the same namespace as another name
393 *
394 * @param other the other name
395 * @return true if the two names are in the same namespace
396 */
397
398 public boolean isInSameNamespace(NodeName other) {
399 if (this == other) {
400 return true;
401 }
402 if (other instanceof StructuredQName) {
403 StructuredQName q2 = (StructuredQName) other;
404 if (localNameStart != q2.localNameStart) {
405 return false;
406 }
407 for (int i = localNameStart - 1; i >= 0; i--) {
408 // compare from the end of the URI to maximize chance of finding a difference quickly
409 if (content[i] != q2.content[i]) {
410 return false;
411 }
412 }
413 return true;
414 } else {
415 return getURI().equals(other.getURI());
416 }
417 }
418
419 /**
420 * Convert the StructuredQName to a javax.xml.namespace.QName
421 *
422 * @return an object of class javax.xml.namespace.QName representing this qualified name
423 */
424
425 public QName toJaxpQName() {
426 return new javax.xml.namespace.QName(getURI(), getLocalPart(), getPrefix());
427 }
428
429 /**
430 * Get the NamespaceBinding (prefix/uri pair) corresponding to this name
431 *
432 * @return a NamespaceBinding containing the prefix and URI present in this QName
433 */
434
435 public NamespaceBinding getNamespaceBinding() {
436 return NamespaceBinding.makeNamespaceBinding(getPrefix(), getURI());
437 }
438
439 /**
440 * Determine whether two IdentityComparable objects are identical. This is a stronger
441 * test than equality (even schema-equality); for example two dateTime values are not identical unless
442 * they are in the same timezone. In the case of a StructuredQName, the identity test compares
443 * prefixes as well as the namespace URI and local name.
444 *
445 * @param other the value to be compared with
446 * @return true if the two values are indentical, false otherwise
447 */
448 public boolean isIdentical(IdentityComparable other) {
449 return equals(other) && ((StructuredQName)other).getPrefix().equals(getPrefix());
450 }
451
452 /**
453 * Get a hashCode that offers the guarantee that if A.isIdentical(B), then A.identityHashCode() == B.identityHashCode()
454 *
455 * @return a hashCode suitable for use when testing for identity.
456 */
457 public int identityHashCode() {
458 return hashCode() ^ getPrefix().hashCode();
459 }
460 }
461