Debian Codegraph @

net/sf/saxon/s9api/QName.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.s9api;
9
10 import net.sf.saxon.lib.NamespaceConstant;
11 import net.sf.saxon.om.InscopeNamespaceResolver;
12 import net.sf.saxon.om.NameChecker;
13 import net.sf.saxon.om.NodeInfo;
14 import net.sf.saxon.om.StructuredQName;
15 import net.sf.saxon.trans.XPathException;
16
17 /**
18 * The QName class represents an instance of xs:QName, as defined in the XPath 2.0 data model.
19 * Internally, it has three components, a namespace URI, a local name, and a prefix. The prefix
20 * is intended to be used only when converting the value back to a string.
21 * <p>This class also defines a number of QName-valued constants relating to built-in types in
22 * XML Schema</p>
23 * <p>A QName is immutable.</p>
24 * <p>Note that a QName is not itself an {@link XdmItem} in this model; however it can be wrapped in an
25 * XdmItem.</p>
26 */
27 public class QName {
28
29 private StructuredQName sqName;
30
31 /**
32 * Construct a QName using a namespace prefix, a namespace URI, and a local name (in that order).
33 * <p>This constructor does not check that the components of the QName are lexically valid.</p>
34 *
35 * @param prefix The prefix of the name. Use either the string "" or null for names that have
36 * no prefix (that is, they are in the default namespace)
37 * @param uri The namespace URI. Use either the string "" or null for names that are not in any namespace.
38 * @param localName The local part of the name
39 */
40
41 public QName(String prefix, String uri, String localName) {
42 sqName = new StructuredQName(prefix, uri, localName);
43 }
44
45 /**
46 * Construct a QName using a namespace URI and a lexical representation. The lexical representation
47 * may be a local name on its own, or it may be in the form prefix:local-name.
48 * <p>This constructor does not check that the components of the QName are lexically valid.</p>
49 *
50 * @param uri The namespace URI. Use either the string "" or null for names that are not in any namespace.
51 * @param lexical Either the local part of the name, or the prefix and local part in the format prefix:local
52 */
53
54 public QName(String uri, String lexical) {
55 uri = (uri == null ? "" : uri);
56 int colon = lexical.indexOf(':');
57 if (colon < 0) {
58 sqName = new StructuredQName("", uri, lexical);
59 } else {
60 String prefix = lexical.substring(0, colon);
61 String local = lexical.substring(colon + 1);
62 sqName = new StructuredQName(prefix, uri, local);
63 }
64 }
65
66 /**
67 * Construct a QName from a localName alone. The localName must not contain a colon.
68 * The resulting QName represents a name in no namespace (which therefore has no prefix)
69 *
70 * @param localName The local name. This must be a valid NCName, in particular it must contain no colon
71 */
72
73 public QName(String localName) {
74 int colon = localName.indexOf(':');
75 if (colon < 0) {
76 sqName = new StructuredQName("", "", localName);
77 } else {
78 throw new IllegalArgumentException("Local name contains a colon");
79 }
80 }
81
82 /**
83 * Construct a QName from a lexical QName, supplying an element node whose
84 * in-scope namespaces are to be used to resolve any prefix contained in the QName.
85 * <p>This constructor checks that the components of the QName are
86 * lexically valid.</p>
87 * <p>If the lexical QName has no prefix, the name is considered to be in the
88 * default namespace, as defined by <code>xmlns="..."</code>.</p>
89 * <p>If the prefix of the lexical QName is not in scope, returns null.</p>
90 *
91 * @param lexicalQName The lexical QName, in the form <code>prefix:local</code>
92 * or simply <code>local</code>. The EQName syntax "Q{uri}local"
93 * and the ClarkName syntax "{uri}local" are also accepted.
94 * @param element The element node whose in-scope namespaces are to be used
95 * to resolve the prefix part of the lexical QName.
96 * @throws IllegalArgumentException If the prefix of the lexical QName is not in scope
97 * or if the lexical QName is invalid (for example, if it contains invalid characters)
98 */
99
100 public QName(String lexicalQName, XdmNode element) {
101 if (lexicalQName.startsWith("{")) {
102 lexicalQName = "Q" + lexicalQName;
103 }
104 try {
105 NodeInfo node = (NodeInfo) element.getUnderlyingValue();
106 sqName = StructuredQName.fromLexicalQName(lexicalQName, true,
107 true, new InscopeNamespaceResolver(node));
108
109 } catch (XPathException err) {
110 throw new IllegalArgumentException(err);
111 }
112 }
113
114 /**
115 * Construct a QName from a JAXP QName object
116 *
117 * @param qName the JAXP QName object
118 */
119
120 public QName(javax.xml.namespace.QName qName) {
121 sqName = new StructuredQName(qName.getPrefix(), qName.getNamespaceURI(), qName.getLocalPart());
122 }
123
124 /**
125 * Construct a QName from a StructuredQName
126 *
127 * @param sqName the StructuredQName
128 */
129
130 public QName(StructuredQName sqName) {
131 this.sqName = sqName;
132 }
133
134 /**
135 * Factory method to construct a QName from a string containing the expanded
136 * QName in Clark notation, that is, <code>{uri}local</code>
137 * <p>The prefix part of the <code>QName</code> will be set to an empty string.</p>
138 *
139 * @param expandedName The URI in Clark notation: <code>{uri}local</code> if the
140 * name is in a namespace, or simply <code>local</code> if not.
141 * @return the QName corresponding to the supplied name in Clark notation. This will always
142 * have an empty prefix.
143 * @throws IllegalArgumentException if the format of the supplied name is incorrect
144 */
145
146 public static QName fromClarkName(String expandedName) throws IllegalArgumentException {
147 String namespaceURI;
148 String localName;
149 if (expandedName == null || expandedName.isEmpty()) {
150 throw new IllegalArgumentException("Supplied Clark name is null or empty");
151 }
152 if (expandedName.charAt(0) == '{') {
153 int closeBrace = expandedName.indexOf('}');
154 if (closeBrace < 0) {
155 throw new IllegalArgumentException("No closing '}' in Clark name");
156 }
157 namespaceURI = expandedName.substring(1, closeBrace);
158 if (closeBrace == expandedName.length()) {
159 throw new IllegalArgumentException("Missing local part in Clark name");
160 }
161 localName = expandedName.substring(closeBrace + 1);
162 } else {
163 namespaceURI = "";
164 localName = expandedName;
165 }
166
167 return new QName("", namespaceURI, localName);
168 }
169
170 /**
171 * Factory method to construct a QName from a string containing the expanded
172 * QName in EQName notation, that is, <code>Q{uri}local</code>
173 * <p>The prefix part of the <code>QName</code> will be set to an empty string.</p>
174 *
175 * @param expandedName The URI in EQName notation: <code>Q{uri}local</code> if the
176 * name is in a namespace. For a name in no namespace, either of the
177 * forms <code>Q{}local</code> or simply <code>local</code> are accepted.
178 * @return the QName corresponding to the supplied name in EQName notation. This will always
179 * have an empty prefix.
180 */
181
182 public static QName fromEQName(String expandedName) {
183 String namespaceURI;
184 String localName;
185 if (expandedName.charAt(0) == 'Q' && expandedName.charAt(1) == '{') {
186 int closeBrace = expandedName.indexOf('}');
187 if (closeBrace < 0) {
188 throw new IllegalArgumentException("No closing '}' in EQName");
189 }
190 namespaceURI = expandedName.substring(2, closeBrace);
191 if (closeBrace == expandedName.length()) {
192 throw new IllegalArgumentException("Missing local part in EQName");
193 }
194 localName = expandedName.substring(closeBrace + 1);
195 } else {
196 namespaceURI = "";
197 localName = expandedName;
198 }
199
200 return new QName("", namespaceURI, localName);
201 }
202
203
204 /**
205 * Validate the QName against the XML 1.0 or XML 1.1 rules for valid names.
206 *
207 * @param processor The Processor in which the name is to be validated.
208 * This was at one time used to determine whether the
209 * XML 1.0 or XML 1.1 rules for forming names are used. However,
210 * the XML 1.0 and 1.1 rules are now aligned, so this argument
211 * is no longer used.
212 * @return true if the name is valid, false if not
213 */
214
215 public boolean isValid(Processor processor) {
216 String prefix = getPrefix();
217 if (prefix.length() > 0) {
218 if (!NameChecker.isValidNCName(prefix)) {
219 return false;
220 }
221 }
222 return NameChecker.isValidNCName(getLocalName());
223 }
224
225 /**
226 * Get the prefix of the QName. This plays no role in operations such as comparison
227 * of QNames for equality, but is retained (as specified in XPath) so that a string representation
228 * can be reconstructed.
229 * <p>Returns the zero-length string in the case of a QName that has no prefix.</p>
230 *
231 * @return the prefix part of the QName, or "" if the name has no known prefix
232 */
233
234 public String getPrefix() {
235 return sqName.getPrefix();
236 }
237
238 /**
239 * The namespace URI of the QName. Returns "" (the zero-length string) if the
240 * QName is not in a namespace.
241 *
242 * @return the namespace part of the QName, or "" for a name in no namespace
243 */
244
245 public String getNamespaceURI() {
246 return sqName.getURI();
247 }
248
249 /**
250 * The local part of the QName
251 *
252 * @return the local part of the QName
253 */
254
255 public String getLocalName() {
256 return sqName.getLocalPart();
257 }
258
259 /**
260 * The expanded name, as a string using the notation devised by James Clark.
261 * If the name is in a namespace, the resulting string takes the form <code>{uri}local</code>.
262 * Otherwise, the value is the local part of the name.
263 *
264 * @return the name in Clark notation. If the name is not in a namespace, returns the
265 * local part of the name. Otherwise returns the concatenation of "{", the namespace part
266 * of the QName, "}", and the local part of the QName.
267 */
268
269 public String getClarkName() {
270 String uri = getNamespaceURI();
271 if (uri.isEmpty()) {
272 return getLocalName();
273 } else {
274 return "{" + uri + "}" + getLocalName();
275 }
276 }
277
278
279 /*
280 * The expanded name, as a string using the notation defined by the EQName production in XPath 3.0.
281 * If the name is in a namespace, the resulting string takes the form <code>Q{uri}local</code>.
282 * Otherwise, the value is the local part of the name.
283 *
284 */
285 public String getEQName() {
286 String uri = getNamespaceURI();
287 if (uri.length() == 0) {
288 return getLocalName();
289 } else {
290 return "Q{" + uri + "}" + getLocalName();
291 }
292 }
293
294
295 /**
296 * Convert the value to a string. The resulting string is the lexical form of the QName,
297 * using the original prefix if there was one.
298 */
299
300 public String toString() {
301 return sqName.getDisplayName();
302 }
303
304 /**
305 * Get a hash code for the QName, to support equality matching. This supports the
306 * semantics of equality, which considers only the namespace URI and local name, and
307 * not the prefix.
308 *
309 * @return a hashCode for the QName
310 */
311
312 public int hashCode() {
313 return sqName.hashCode();
314 }
315
316 /**
317 * Test whether two QNames are equal. This supports the
318 * semantics of equality, which considers only the namespace URI and local name, and
319 * not the prefix.
320 *
321 * @return true if the namespace URIs are equal and the local parts are equal, when
322 * compared character-by-character.
323 */
324
325 public boolean equals(Object other) {
326 return other instanceof QName && sqName.equals(((QName) other).sqName);
327 }
328
329 /**
330 * Get the underlying StructuredQName
331 *
332 * @return the underlying StructuredQName
333 */
334
335 public StructuredQName getStructuredQName() {
336 return sqName;
337 }
338
339 /**
340 * QName denoting the schema type xs:string *
341 */
342 public static final QName XS_STRING =
343 new QName("xs", NamespaceConstant.SCHEMA, "string");
344 /**
345 * QName denoting the schema type xs:boolean *
346 */
347 public static final QName XS_BOOLEAN =
348 new QName("xs", NamespaceConstant.SCHEMA, "boolean");
349 /**
350 * QName denoting the schema type xs:decimal *
351 */
352 public static final QName XS_DECIMAL =
353 new QName("xs", NamespaceConstant.SCHEMA, "decimal");
354 /**
355 * QName denoting the schema type xs:float *
356 */
357 public static final QName XS_FLOAT =
358 new QName("xs", NamespaceConstant.SCHEMA, "float");
359 /**
360 * QName denoting the schema type xs:double *
361 */
362 public static final QName XS_DOUBLE =
363 new QName("xs", NamespaceConstant.SCHEMA, "double");
364 /**
365 * QName denoting the schema type xs:duration *
366 */
367 public static final QName XS_DURATION =
368 new QName("xs", NamespaceConstant.SCHEMA, "duration");
369 /**
370 * QName denoting the schema type xs:dateTime *
371 */
372 public static final QName XS_DATE_TIME =
373 new QName("xs", NamespaceConstant.SCHEMA, "dateTime");
374 /**
375 * QName denoting the schema type xs:time *
376 */
377 public static final QName XS_TIME =
378 new QName("xs", NamespaceConstant.SCHEMA, "time");
379 /**
380 * QName denoting the schema type xs:date *
381 */
382 public static final QName XS_DATE =
383 new QName("xs", NamespaceConstant.SCHEMA, "date");
384 /**
385 * QName denoting the schema type xs:gYearMonth *
386 */
387 public static final QName XS_G_YEAR_MONTH =
388 new QName("xs", NamespaceConstant.SCHEMA, "gYearMonth");
389 /**
390 * QName denoting the schema type xs:gYear *
391 */
392 public static final QName XS_G_YEAR =
393 new QName("xs", NamespaceConstant.SCHEMA, "gYear");
394 /**
395 * QName denoting the schema type xs:gMonthDay *
396 */
397 public static final QName XS_G_MONTH_DAY =
398 new QName("xs", NamespaceConstant.SCHEMA, "gMonthDay");
399 /**
400 * QName denoting the schema type xs:gDay *
401 */
402 public static final QName XS_G_DAY =
403 new QName("xs", NamespaceConstant.SCHEMA, "gDay");
404 /**
405 * QName denoting the schema type xs:gMonth *
406 */
407 public static final QName XS_G_MONTH =
408 new QName("xs", NamespaceConstant.SCHEMA, "gMonth");
409 /**
410 * QName denoting the schema type xs:hexBinary *
411 */
412 public static final QName XS_HEX_BINARY =
413 new QName("xs", NamespaceConstant.SCHEMA, "hexBinary");
414 /**
415 * QName denoting the schema type xs:base64Binary *
416 */
417 public static final QName XS_BASE64_BINARY =
418 new QName("xs", NamespaceConstant.SCHEMA, "base64Binary");
419 /**
420 * QName denoting the schema type xs:anyURI *
421 */
422 public static final QName XS_ANY_URI =
423 new QName("xs", NamespaceConstant.SCHEMA, "anyURI");
424 /**
425 * QName denoting the schema type xs:QName *
426 */
427 public static final QName XS_QNAME =
428 new QName("xs", NamespaceConstant.SCHEMA, "QName");
429 /**
430 * QName denoting the schema type xs:NOTATION *
431 */
432 public static final QName XS_NOTATION =
433 new QName("xs", NamespaceConstant.SCHEMA, "NOTATION");
434 /**
435 * QName denoting the schema type xs:integer *
436 */
437 public static final QName XS_INTEGER =
438 new QName("xs", NamespaceConstant.SCHEMA, "integer");
439 /**
440 * QName denoting the schema type xs:nonPositiveInteger *
441 */
442 public static final QName XS_NON_POSITIVE_INTEGER =
443 new QName("xs", NamespaceConstant.SCHEMA, "nonPositiveInteger");
444 /**
445 * QName denoting the schema type xs:negativeInteger *
446 */
447 public static final QName XS_NEGATIVE_INTEGER =
448 new QName("xs", NamespaceConstant.SCHEMA, "negativeInteger");
449 /**
450 * QName denoting the schema type xs:long *
451 */
452 public static final QName XS_LONG =
453 new QName("xs", NamespaceConstant.SCHEMA, "long");
454 /**
455 * QName denoting the schema type xs:int *
456 */
457 public static final QName XS_INT =
458 new QName("xs", NamespaceConstant.SCHEMA, "int");
459 /**
460 * QName denoting the schema type xs:short *
461 */
462 public static final QName XS_SHORT =
463 new QName("xs", NamespaceConstant.SCHEMA, "short");
464 /**
465 * QName denoting the schema type xs:byte *
466 */
467 public static final QName XS_BYTE =
468 new QName("xs", NamespaceConstant.SCHEMA, "byte");
469 /**
470 * QName denoting the schema type xs:nonNegativeInteger *
471 */
472 public static final QName XS_NON_NEGATIVE_INTEGER =
473 new QName("xs", NamespaceConstant.SCHEMA, "nonNegativeInteger");
474 /**
475 * QName denoting the schema type xs:positiveInteger *
476 */
477 public static final QName XS_POSITIVE_INTEGER =
478 new QName("xs", NamespaceConstant.SCHEMA, "positiveInteger");
479 /**
480 * QName denoting the schema type xs:unsignedLong *
481 */
482 public static final QName XS_UNSIGNED_LONG =
483 new QName("xs", NamespaceConstant.SCHEMA, "unsignedLong");
484 /**
485 * QName denoting the schema type xs:unsignedInt *
486 */
487 public static final QName XS_UNSIGNED_INT =
488 new QName("xs", NamespaceConstant.SCHEMA, "unsignedInt");
489 /**
490 * QName denoting the schema type xs:unsignedShort *
491 */
492 public static final QName XS_UNSIGNED_SHORT =
493 new QName("xs", NamespaceConstant.SCHEMA, "unsignedShort");
494 /**
495 * QName denoting the schema type xs:unsignedByte *
496 */
497 public static final QName XS_UNSIGNED_BYTE =
498 new QName("xs", NamespaceConstant.SCHEMA, "unsignedByte");
499 /**
500 * QName denoting the schema type xs:normalizedString *
501 */
502 public static final QName XS_NORMALIZED_STRING =
503 new QName("xs", NamespaceConstant.SCHEMA, "normalizedString");
504 /**
505 * QName denoting the schema type xs:token *
506 */
507 public static final QName XS_TOKEN =
508 new QName("xs", NamespaceConstant.SCHEMA, "token");
509 /**
510 * QName denoting the schema type xs:language *
511 */
512 public static final QName XS_LANGUAGE =
513 new QName("xs", NamespaceConstant.SCHEMA, "language");
514 /**
515 * QName denoting the schema type xs:NMTOKEN *
516 */
517 public static final QName XS_NMTOKEN =
518 new QName("xs", NamespaceConstant.SCHEMA, "NMTOKEN");
519 /**
520 * QName denoting the schema type xs:NMTOKENS *
521 */
522 public static final QName XS_NMTOKENS =
523 new QName("xs", NamespaceConstant.SCHEMA, "NMTOKENS");
524 /**
525 * QName denoting the schema type xs:Name *
526 */
527 public static final QName XS_NAME =
528 new QName("xs", NamespaceConstant.SCHEMA, "Name");
529 /**
530 * QName denoting the schema type xs:NCName *
531 */
532 public static final QName XS_NCNAME =
533 new QName("xs", NamespaceConstant.SCHEMA, "NCName");
534 /**
535 * QName denoting the schema type xs:ID *
536 */
537 public static final QName XS_ID =
538 new QName("xs", NamespaceConstant.SCHEMA, "ID");
539 /**
540 * QName denoting the schema type xs:IDREF *
541 */
542 public static final QName XS_IDREF =
543 new QName("xs", NamespaceConstant.SCHEMA, "IDREF");
544 /**
545 * QName denoting the schema type xs:IDREFS *
546 */
547 public static final QName XS_IDREFS =
548 new QName("xs", NamespaceConstant.SCHEMA, "IDREFS");
549 /**
550 * QName denoting the schema type xs:ENTITY *
551 */
552 public static final QName XS_ENTITY =
553 new QName("xs", NamespaceConstant.SCHEMA, "ENTITY");
554 /**
555 * QName denoting the schema type xs:ENTITIES *
556 */
557 public static final QName XS_ENTITIES =
558 new QName("xs", NamespaceConstant.SCHEMA, "ENTITIES");
559 /**
560 * QName denoting the schema type xs:untyped *
561 */
562 public static final QName XS_UNTYPED =
563 new QName("xs", NamespaceConstant.SCHEMA, "untyped");
564 /**
565 * QName denoting the schema type xs:untypedAtomic *
566 */
567 public static final QName XS_UNTYPED_ATOMIC =
568 new QName("xs", NamespaceConstant.SCHEMA, "untypedAtomic");
569 /**
570 * QName denoting the schema type xs:anyAtomicType *
571 */
572 public static final QName XS_ANY_ATOMIC_TYPE =
573 new QName("xs", NamespaceConstant.SCHEMA, "anyAtomicType");
574 /**
575 * QName denoting the schema type xs:yearMonthDuration *
576 */
577 public static final QName XS_YEAR_MONTH_DURATION =
578 new QName("xs", NamespaceConstant.SCHEMA, "yearMonthDuration");
579 /**
580 * QName denoting the schema type xs:dayTimeDuration *
581 */
582 public static final QName XS_DAY_TIME_DURATION =
583 new QName("xs", NamespaceConstant.SCHEMA, "dayTimeDuration");
584 /**
585 * QName denoting the schema type xs:dateTimeStamp *
586 */
587 public static final QName XS_DATE_TIME_STAMP =
588 new QName("xs", NamespaceConstant.SCHEMA, "dateTimeStamp");
589
590 }
591