Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.xml/share/classes/javax/xml/namespace/QName.java
40948 views
1
/*
2
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.xml.namespace;
27
28
import java.io.Serializable;
29
import javax.xml.XMLConstants;
30
import jdk.xml.internal.SecuritySupport;
31
32
/**
33
* <p><code>QName</code> represents a <strong>qualified name</strong>
34
* as defined in the XML specifications: <a
35
* href="http://www.w3.org/TR/xmlschema-2/#QName">XML Schema Part2:
36
* Datatypes specification</a>, <a
37
* href="http://www.w3.org/TR/REC-xml-names/#ns-qualnames">Namespaces
38
* in XML</a>.
39
*
40
* <p>The value of a <code>QName</code> contains a <strong>Namespace
41
* URI</strong>, <strong>local part</strong> and
42
* <strong>prefix</strong>.</p>
43
*
44
* <p>The prefix is included in <code>QName</code> to retain lexical
45
* information <strong><em>when present</em></strong> in an {@link
46
* javax.xml.transform.Source XML input source}. The prefix is
47
* <strong><em>NOT</em></strong> used in {@link #equals(Object)
48
* QName.equals(Object)} or to compute the {@link #hashCode()
49
* QName.hashCode()}. Equality and the hash code are defined using
50
* <strong><em>only</em></strong> the Namespace URI and local part.</p>
51
*
52
* <p>If not specified, the Namespace URI is set to {@link
53
* javax.xml.XMLConstants#NULL_NS_URI XMLConstants.NULL_NS_URI}.
54
* If not specified, the prefix is set to {@link
55
* javax.xml.XMLConstants#DEFAULT_NS_PREFIX
56
* XMLConstants.DEFAULT_NS_PREFIX}.</p>
57
*
58
* <p><code>QName</code> is immutable.</p>
59
*
60
* @author Jeff Suttor
61
* @see <a href="http://www.w3.org/TR/xmlschema-2/#QName">
62
* XML Schema Part2: Datatypes specification</a>
63
* @see <a href="http://www.w3.org/TR/REC-xml-names/#ns-qualnames">
64
* Namespaces in XML</a>
65
* @since 1.5
66
*/
67
68
public class QName implements Serializable {
69
// tests show that the ID is the same from JDK 1.5 through JDK 9
70
private static final long serialVersionUID = -9120448754896609940L;
71
72
/**
73
* <p>Namespace URI of this <code>QName</code>.</p>
74
*/
75
private final String namespaceURI;
76
77
/**
78
* <p>local part of this <code>QName</code>.</p>
79
*/
80
private final String localPart;
81
82
/**
83
* <p>prefix of this <code>QName</code>.</p>
84
*/
85
private final String prefix;
86
87
/**
88
* <p><code>QName</code> constructor specifying the Namespace URI
89
* and local part.</p>
90
*
91
* <p>If the Namespace URI is <code>null</code>, it is set to
92
* {@link javax.xml.XMLConstants#NULL_NS_URI
93
* XMLConstants.NULL_NS_URI}. This value represents no
94
* explicitly defined Namespace as defined by the <a
95
* href="http://www.w3.org/TR/REC-xml-names/#ns-qualnames">Namespaces
96
* in XML</a> specification. This action preserves compatible
97
* behavior with QName 1.0. Explicitly providing the {@link
98
* javax.xml.XMLConstants#NULL_NS_URI
99
* XMLConstants.NULL_NS_URI} value is the preferred coding
100
* style.</p>
101
*
102
* <p>If the local part is <code>null</code> an
103
* <code>IllegalArgumentException</code> is thrown.
104
* A local part of "" is allowed to preserve
105
* compatible behavior with QName 1.0. </p>
106
*
107
* <p>When using this constructor, the prefix is set to {@link
108
* javax.xml.XMLConstants#DEFAULT_NS_PREFIX
109
* XMLConstants.DEFAULT_NS_PREFIX}.</p>
110
*
111
* <p>The Namespace URI is not validated as a
112
* <a href="http://www.ietf.org/rfc/rfc2396.txt">URI reference</a>.
113
* The local part is not validated as a
114
* <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>
115
* as specified in <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces
116
* in XML</a>.</p>
117
*
118
* @param namespaceURI Namespace URI of the <code>QName</code>
119
* @param localPart local part of the <code>QName</code>
120
*
121
* @throws IllegalArgumentException When <code>localPart</code> is
122
* <code>null</code>
123
*
124
* @see #QName(String namespaceURI, String localPart, String
125
* prefix) QName(String namespaceURI, String localPart, String
126
* prefix)
127
*/
128
public QName(final String namespaceURI, final String localPart) {
129
this(namespaceURI, localPart, XMLConstants.DEFAULT_NS_PREFIX);
130
}
131
132
/**
133
* <p><code>QName</code> constructor specifying the Namespace URI,
134
* local part and prefix.</p>
135
*
136
* <p>If the Namespace URI is <code>null</code>, it is set to
137
* {@link javax.xml.XMLConstants#NULL_NS_URI
138
* XMLConstants.NULL_NS_URI}. This value represents no
139
* explicitly defined Namespace as defined by the <a
140
* href="http://www.w3.org/TR/REC-xml-names/#ns-qualnames">Namespaces
141
* in XML</a> specification. This action preserves compatible
142
* behavior with QName 1.0. Explicitly providing the {@link
143
* javax.xml.XMLConstants#NULL_NS_URI
144
* XMLConstants.NULL_NS_URI} value is the preferred coding
145
* style.</p>
146
*
147
* <p>If the local part is <code>null</code> an
148
* <code>IllegalArgumentException</code> is thrown.
149
* A local part of "" is allowed to preserve
150
* compatible behavior with QName 1.0. </p>
151
*
152
* <p>If the prefix is <code>null</code>, an
153
* <code>IllegalArgumentException</code> is thrown. Use {@link
154
* javax.xml.XMLConstants#DEFAULT_NS_PREFIX
155
* XMLConstants.DEFAULT_NS_PREFIX} to explicitly indicate that no
156
* prefix is present or the prefix is not relevant.</p>
157
*
158
* <p>The Namespace URI is not validated as a
159
* <a href="http://www.ietf.org/rfc/rfc2396.txt">URI reference</a>.
160
* The local part and prefix are not validated as a
161
* <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>
162
* as specified in <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces
163
* in XML</a>.</p>
164
*
165
* @param namespaceURI Namespace URI of the <code>QName</code>
166
* @param localPart local part of the <code>QName</code>
167
* @param prefix prefix of the <code>QName</code>
168
*
169
* @throws IllegalArgumentException When <code>localPart</code>
170
* or <code>prefix</code> is <code>null</code>
171
*/
172
public QName(String namespaceURI, String localPart, String prefix) {
173
174
// map null Namespace URI to default
175
// to preserve compatibility with QName 1.0
176
if (namespaceURI == null) {
177
this.namespaceURI = XMLConstants.NULL_NS_URI;
178
} else {
179
this.namespaceURI = namespaceURI;
180
}
181
182
// local part is required.
183
// "" is allowed to preserve compatibility with QName 1.0
184
if (localPart == null) {
185
throw new IllegalArgumentException(
186
"local part cannot be \"null\" when creating a QName");
187
}
188
this.localPart = localPart;
189
190
// prefix is required
191
if (prefix == null) {
192
throw new IllegalArgumentException(
193
"prefix cannot be \"null\" when creating a QName");
194
}
195
this.prefix = prefix;
196
}
197
198
/**
199
* <p><code>QName</code> constructor specifying the local part.</p>
200
*
201
* <p>If the local part is <code>null</code> an
202
* <code>IllegalArgumentException</code> is thrown.
203
* A local part of "" is allowed to preserve
204
* compatible behavior with QName 1.0. </p>
205
*
206
* <p>When using this constructor, the Namespace URI is set to
207
* {@link javax.xml.XMLConstants#NULL_NS_URI
208
* XMLConstants.NULL_NS_URI} and the prefix is set to {@link
209
* javax.xml.XMLConstants#DEFAULT_NS_PREFIX
210
* XMLConstants.DEFAULT_NS_PREFIX}.</p>
211
*
212
* <p><em>In an XML context, all Element and Attribute names exist
213
* in the context of a Namespace. Making this explicit during the
214
* construction of a <code>QName</code> helps prevent hard to
215
* diagnosis XML validity errors. The constructors {@link
216
* #QName(String namespaceURI, String localPart) QName(String
217
* namespaceURI, String localPart)} and
218
* {@link #QName(String namespaceURI, String localPart, String prefix)}
219
* are preferred.</em></p>
220
*
221
* <p>The local part is not validated as a
222
* <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>
223
* as specified in <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces
224
* in XML</a>.</p>
225
*
226
* @param localPart local part of the <code>QName</code>
227
*
228
* @throws IllegalArgumentException When <code>localPart</code> is
229
* <code>null</code>
230
*
231
* @see #QName(String namespaceURI, String localPart) QName(String
232
* namespaceURI, String localPart)
233
* @see #QName(String namespaceURI, String localPart, String
234
* prefix) QName(String namespaceURI, String localPart, String
235
* prefix)
236
*/
237
public QName(String localPart) {
238
this(
239
XMLConstants.NULL_NS_URI,
240
localPart,
241
XMLConstants.DEFAULT_NS_PREFIX);
242
}
243
244
/**
245
* <p>Get the Namespace URI of this <code>QName</code>.</p>
246
*
247
* @return Namespace URI of this <code>QName</code>
248
*/
249
public String getNamespaceURI() {
250
return namespaceURI;
251
}
252
253
/**
254
* <p>Get the local part of this <code>QName</code>.</p>
255
*
256
* @return local part of this <code>QName</code>
257
*/
258
public String getLocalPart() {
259
return localPart;
260
}
261
262
/**
263
* <p>Get the prefix of this <code>QName</code>.</p>
264
*
265
* <p>The prefix assigned to a <code>QName</code> might
266
* <strong><em>NOT</em></strong> be valid in a different
267
* context. For example, a <code>QName</code> may be assigned a
268
* prefix in the context of parsing a document but that prefix may
269
* be invalid in the context of a different document.</p>
270
*
271
* @return prefix of this <code>QName</code>
272
*/
273
public String getPrefix() {
274
return prefix;
275
}
276
277
/**
278
* <p>Test this <code>QName</code> for equality with another
279
* <code>Object</code>.</p>
280
*
281
* <p>If the <code>Object</code> to be tested is not a
282
* <code>QName</code> or is <code>null</code>, then this method
283
* returns <code>false</code>.</p>
284
*
285
* <p>Two <code>QName</code>s are considered equal if and only if
286
* both the Namespace URI and local part are equal. This method
287
* uses <code>String.equals()</code> to check equality of the
288
* Namespace URI and local part. The prefix is
289
* <strong><em>NOT</em></strong> used to determine equality.</p>
290
*
291
* <p>This method satisfies the general contract of {@link
292
* java.lang.Object#equals(Object) Object.equals(Object)}</p>
293
*
294
* @param objectToTest the <code>Object</code> to test for
295
* equality with this <code>QName</code>
296
* @return <code>true</code> if the given <code>Object</code> is
297
* equal to this <code>QName</code> else <code>false</code>
298
*/
299
public final boolean equals(Object objectToTest) {
300
if (objectToTest == this) {
301
return true;
302
}
303
304
if (objectToTest == null || !(objectToTest instanceof QName)) {
305
return false;
306
}
307
308
QName qName = (QName) objectToTest;
309
310
return localPart.equals(qName.localPart)
311
&& namespaceURI.equals(qName.namespaceURI);
312
}
313
314
/**
315
* <p>Generate the hash code for this <code>QName</code>.</p>
316
*
317
* <p>The hash code is calculated using both the Namespace URI and
318
* the local part of the <code>QName</code>. The prefix is
319
* <strong><em>NOT</em></strong> used to calculate the hash
320
* code.</p>
321
*
322
* <p>This method satisfies the general contract of {@link
323
* java.lang.Object#hashCode() Object.hashCode()}.</p>
324
*
325
* @return hash code for this <code>QName</code> <code>Object</code>
326
*/
327
public final int hashCode() {
328
return namespaceURI.hashCode() ^ localPart.hashCode();
329
}
330
331
/**
332
* <p><code>String</code> representation of this
333
* <code>QName</code>.</p>
334
*
335
* <p>The commonly accepted way of representing a <code>QName</code>
336
* as a <code>String</code> was
337
* <a href="http://jclark.com/xml/xmlns.htm">defined</a>
338
* by James Clark. Although this is not a <em>standard</em>
339
* specification, it is in common use, e.g. {@link
340
* javax.xml.transform.Transformer#setParameter(String name, Object value)}.
341
* This implementation represents a <code>QName</code> as:
342
* "{" + Namespace URI + "}" + local part. If the Namespace URI
343
* <code>.equals(XMLConstants.NULL_NS_URI)</code>, only the
344
* local part is returned. An appropriate use of this method is
345
* for debugging or logging for human consumption.</p>
346
*
347
* <p>Note the prefix value is <strong><em>NOT</em></strong>
348
* returned as part of the <code>String</code> representation.</p>
349
*
350
* <p>This method satisfies the general contract of {@link
351
* java.lang.Object#toString() Object.toString()}.</p>
352
*
353
* @return <code>String</code> representation of this <code>QName</code>
354
*/
355
public String toString() {
356
if (namespaceURI.equals(XMLConstants.NULL_NS_URI)) {
357
return localPart;
358
} else {
359
return "{" + namespaceURI + "}" + localPart;
360
}
361
}
362
363
/**
364
* <p><code>QName</code> derived from parsing the formatted
365
* <code>String</code>.</p>
366
*
367
* <p>If the <code>String</code> is <code>null</code> or does not conform to
368
* {@link #toString() QName.toString()} formatting, an
369
* <code>IllegalArgumentException</code> is thrown.</p>
370
*
371
* <p><em>The <code>String</code> <strong>MUST</strong> be in the
372
* form returned by {@link #toString() QName.toString()}.</em></p>
373
*
374
* <p>The commonly accepted way of representing a <code>QName</code>
375
* as a <code>String</code> was
376
* <a href="http://jclark.com/xml/xmlns.htm">defined</a>
377
* by James Clark. Although this is not a <em>standard</em>
378
* specification, it is in common use, e.g. {@link
379
* javax.xml.transform.Transformer#setParameter(String name, Object value)}.
380
* This implementation parses a <code>String</code> formatted
381
* as: "{" + Namespace URI + "}" + local part. If the Namespace
382
* URI <code>.equals(XMLConstants.NULL_NS_URI)</code>, only the
383
* local part should be provided.</p>
384
*
385
* <p>The prefix value <strong><em>CANNOT</em></strong> be
386
* represented in the <code>String</code> and will be set to
387
* {@link javax.xml.XMLConstants#DEFAULT_NS_PREFIX
388
* XMLConstants.DEFAULT_NS_PREFIX}.</p>
389
*
390
* <p>This method does not do full validation of the resulting
391
* <code>QName</code>.
392
* <p>The Namespace URI is not validated as a
393
* <a href="http://www.ietf.org/rfc/rfc2396.txt">URI reference</a>.
394
* The local part is not validated as a
395
* <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>
396
* as specified in
397
* <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces in XML</a>.</p>
398
*
399
* @param qNameAsString <code>String</code> representation
400
* of the <code>QName</code>
401
*
402
* @throws IllegalArgumentException When <code>qNameAsString</code> is
403
* <code>null</code> or malformed
404
*
405
* @return <code>QName</code> corresponding to the given <code>String</code>
406
* @see #toString() QName.toString()
407
*/
408
public static QName valueOf(String qNameAsString) {
409
410
// null is not valid
411
if (qNameAsString == null) {
412
throw new IllegalArgumentException(
413
"cannot create QName from \"null\" or \"\" String");
414
}
415
416
// "" local part is valid to preserve compatible behavior with QName 1.0
417
if (qNameAsString.length() == 0) {
418
return new QName(
419
XMLConstants.NULL_NS_URI,
420
qNameAsString,
421
XMLConstants.DEFAULT_NS_PREFIX);
422
}
423
424
// local part only?
425
if (qNameAsString.charAt(0) != '{') {
426
return new QName(
427
XMLConstants.NULL_NS_URI,
428
qNameAsString,
429
XMLConstants.DEFAULT_NS_PREFIX);
430
}
431
432
// Namespace URI improperly specified?
433
if (qNameAsString.startsWith("{" + XMLConstants.NULL_NS_URI + "}")) {
434
throw new IllegalArgumentException(
435
"Namespace URI .equals(XMLConstants.NULL_NS_URI), "
436
+ ".equals(\"" + XMLConstants.NULL_NS_URI + "\"), "
437
+ "only the local part, "
438
+ "\""
439
+ qNameAsString.substring(2 + XMLConstants.NULL_NS_URI.length())
440
+ "\", "
441
+ "should be provided.");
442
}
443
444
// Namespace URI and local part specified
445
int endOfNamespaceURI = qNameAsString.indexOf('}');
446
if (endOfNamespaceURI == -1) {
447
throw new IllegalArgumentException(
448
"cannot create QName from \""
449
+ qNameAsString
450
+ "\", missing closing \"}\"");
451
}
452
return new QName(
453
qNameAsString.substring(1, endOfNamespaceURI),
454
qNameAsString.substring(endOfNamespaceURI + 1),
455
XMLConstants.DEFAULT_NS_PREFIX);
456
}
457
}
458
459