forked from dom4j/dom4j
-
Notifications
You must be signed in to change notification settings - Fork 4
/
QName.java
396 lines (342 loc) · 13.4 KB
/
QName.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
*
* This software is open source.
* See the bottom of this file for the licence.
*/
package org.dom4j;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.regex.Pattern;
import org.dom4j.tree.QNameCache;
import org.dom4j.util.SingletonStrategy;
/**
* <p>
* <code>QName</code> represents a qualified name value of an XML element or
* attribute. It consists of a local name and a {@link Namespace}instance. This
* object is immutable.
* </p>
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
* @author Filip Jirsák
*/
public class QName implements Serializable {
/** The Singleton instance */
private static SingletonStrategy singleton = null;
/**
* {@code NameStartChar} without colon.
*
* <pre>NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]</pre>
*
* @see <a href="https://www.w3.org/TR/xml/#sec-common-syn">XML 1.0 – 2.3 Common Syntactic Constructs</a>
* @see <a href="https://www.w3.org/TR/2006/REC-xml11-20060816/#sec-common-syn">XML 1.1 – 2.3 Common Syntactic Constructs</a>
*/
private static final String NAME_START_CHAR = "_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD";
/**
* {@code NameChar} without colon.
*
* <pre>NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]</pre>
*
* @see <a href="https://www.w3.org/TR/xml/#sec-common-syn">XML 1.0 – 2.3 Common Syntactic Constructs</a>
* @see <a href="https://www.w3.org/TR/2006/REC-xml11-20060816/#sec-common-syn">XML 1.1 – 2.3 Common Syntactic Constructs</a>
*/
private static final String NAME_CHAR = NAME_START_CHAR + "-.0-9\u00B7\u0300-\u036F\u203F-\u2040";
/**
* {@code NCName}
*
* <pre>
* NCName ::= NCNameStartChar NCNameChar* (An XML Name, minus the ":")
* NCNameChar ::= NameChar -':'
* NCNameStartChar ::= NameStartChar -':'
* </pre>
*
* @see <a href="https://www.w3.org/TR/xml-names/#ns-qualnames">Namespaces in XML 1.0 – 4 Qualified Names</a>
* @see <a href="https://www.w3.org/TR/2006/REC-xml-names11-20060816/#ns-qualnames">Namespaces in XML 1.1 – 4 Qualified Names</a>
*/
private static final String NCNAME = "["+NAME_START_CHAR+"]["+NAME_CHAR+"]*";
/**
* Regular expression for {@code Name} (with colon).
*
* <pre>Name ::= NameStartChar (NameChar)*</pre>
*
* @see <a href="https://www.w3.org/TR/xml/#sec-common-syn">XML 1.0 – 2.3 Common Syntactic Constructs</a>
* @see <a href="https://www.w3.org/TR/2006/REC-xml11-20060816/#sec-common-syn">XML 1.1 – 2.3 Common Syntactic Constructs</a>
*/
private static final Pattern RE_NAME = Pattern.compile("[:"+NAME_START_CHAR+"][:"+NAME_CHAR+"]*");
/**
* Regular expression for {@code NCName}.
*
* <pre>
* NCName ::= NCNameStartChar NCNameChar* (An XML Name, minus the ":")
* NCNameChar ::= NameChar -':'
* NCNameStartChar ::= NameStartChar -':'
* </pre>
*
* @see <a href="https://www.w3.org/TR/xml-names/#ns-qualnames">Namespaces in XML 1.0 – 4 Qualified Names</a>
* @see <a href="https://www.w3.org/TR/2006/REC-xml-names11-20060816/#ns-qualnames">Namespaces in XML 1.1 – 4 Qualified Names</a>
*/
private static final Pattern RE_NCNAME = Pattern.compile(NCNAME);
/**
* Regular expression for {@code QName}.
*
* <pre>
* QName ::= PrefixedName | UnprefixedName
* PrefixedName ::= Prefix ':' LocalPart
* UnprefixedName ::= LocalPart
* Prefix ::= NCName
* LocalPart ::= NCName
* </pre>
*
* @see <a href="https://www.w3.org/TR/xml-names/#ns-qualnames">Namespaces in XML 1.0 – 4 Qualified Names</a>
* @see <a href="https://www.w3.org/TR/2006/REC-xml-names11-20060816/#ns-qualnames">Namespaces in XML 1.1 – 4 Qualified Names</a>
*/
private static final Pattern RE_QNAME = Pattern.compile("(?:"+NCNAME+":)?"+NCNAME);
static {
try {
String defaultSingletonClass = "org.dom4j.util.SimpleSingleton";
Class clazz = null;
try {
String singletonClass = defaultSingletonClass;
singletonClass = System.getProperty(
"org.dom4j.QName.singleton.strategy", singletonClass);
clazz = Class.forName(singletonClass);
} catch (Exception exc1) {
try {
String singletonClass = defaultSingletonClass;
clazz = Class.forName(singletonClass);
} catch (Exception exc2) {
}
}
singleton = (SingletonStrategy) clazz.newInstance();
singleton.setSingletonClassName(QNameCache.class.getName());
} catch (Exception exc3) {
}
}
/** The local name of the element or attribute */
private String name;
/** The qualified name of the element or attribute */
private String qualifiedName;
/** The Namespace of this element or attribute */
private transient Namespace namespace;
/** A cached version of the hashcode for efficiency */
private int hashCode;
/** The document factory used for this QName if specified or null */
private DocumentFactory documentFactory;
public QName(String name) {
this(name, Namespace.NO_NAMESPACE);
}
public QName(String name, Namespace namespace) {
this.name = (name == null) ? "" : name;
this.namespace = (namespace == null) ? Namespace.NO_NAMESPACE
: namespace;
if (this.namespace.equals(Namespace.NO_NAMESPACE)) {
validateName(this.name);
} else {
validateNCName(this.name);
}
}
public QName(String name, Namespace namespace, String qualifiedName) {
this.name = (name == null) ? "" : name;
this.qualifiedName = qualifiedName;
this.namespace = (namespace == null) ? Namespace.NO_NAMESPACE
: namespace;
validateNCName(this.name);
validateQName(this.qualifiedName);
}
public static QName get(String name) {
return getCache().get(name);
}
public static QName get(String name, Namespace namespace) {
return getCache().get(name, namespace);
}
public static QName get(String name, String prefix, String uri) {
if (((prefix == null) || (prefix.length() == 0)) && (uri == null)) {
return QName.get(name);
} else if ((prefix == null) || (prefix.length() == 0)) {
return getCache().get(name, Namespace.get(uri));
} else if (uri == null) {
return QName.get(name);
} else {
return getCache().get(name, Namespace.get(prefix, uri));
}
}
public static QName get(String qualifiedName, String uri) {
if (uri == null) {
return getCache().get(qualifiedName);
} else {
return getCache().get(qualifiedName, uri);
}
}
public static QName get(String localName, Namespace namespace,
String qualifiedName) {
return getCache().get(localName, namespace, qualifiedName);
}
/**
* DOCUMENT ME!
*
* @return the local name
*/
public String getName() {
return name;
}
/**
* DOCUMENT ME!
*
* @return the qualified name in the format <code>prefix:localName</code>
*/
public String getQualifiedName() {
if (qualifiedName == null) {
String prefix = getNamespacePrefix();
if ((prefix != null) && (prefix.length() > 0)) {
qualifiedName = prefix + ":" + name;
} else {
qualifiedName = name;
}
}
return qualifiedName;
}
/**
* DOCUMENT ME!
*
* @return the namespace of this QName
*/
public Namespace getNamespace() {
return namespace;
}
/**
* DOCUMENT ME!
*
* @return the namespace URI of this QName
*/
public String getNamespacePrefix() {
if (namespace == null) {
return "";
}
return namespace.getPrefix();
}
/**
* DOCUMENT ME!
*
* @return the namespace URI of this QName
*/
public String getNamespaceURI() {
if (namespace == null) {
return "";
}
return namespace.getURI();
}
/**
* DOCUMENT ME!
*
* @return the hash code based on the qualified name and the URI of the
* namespace.
*/
public int hashCode() {
if (hashCode == 0) {
hashCode = getName().hashCode() ^ getNamespaceURI().hashCode();
if (hashCode == 0) {
hashCode = 0xbabe;
}
}
return hashCode;
}
public boolean equals(Object object) {
if (this == object) {
return true;
} else if (object instanceof QName) {
QName that = (QName) object;
// we cache hash codes so this should be quick
if (hashCode() == that.hashCode()) {
return getName().equals(that.getName())
&& getNamespaceURI().equals(that.getNamespaceURI());
}
}
return false;
}
public String toString() {
return super.toString() + " [name: " + getName() + " namespace: \""
+ getNamespace() + "\"]";
}
/**
* DOCUMENT ME!
*
* @return the factory that should be used for Elements of this QName
*/
public DocumentFactory getDocumentFactory() {
return documentFactory;
}
public void setDocumentFactory(DocumentFactory documentFactory) {
this.documentFactory = documentFactory;
}
private void writeObject(ObjectOutputStream out) throws IOException {
// We use writeObject() and not writeUTF() to minimize space
// This allows for writing pointers to already written strings
out.writeObject(namespace.getPrefix());
out.writeObject(namespace.getURI());
out.defaultWriteObject();
}
private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException {
String prefix = (String) in.readObject();
String uri = (String) in.readObject();
in.defaultReadObject();
namespace = Namespace.get(prefix, uri);
}
private static QNameCache getCache() {
QNameCache cache = (QNameCache) singleton.instance();
return cache;
}
private static void validateName(String name) {
if (!RE_NAME.matcher(name).matches()) {
throw new IllegalArgumentException(String.format("Illegal character in name: '%s'.", name));
}
}
protected static void validateNCName(String ncname) {
if (!RE_NCNAME.matcher(ncname).matches()) {
throw new IllegalArgumentException(String.format("Illegal character in local name: '%s'.", ncname));
}
}
private static void validateQName(String qname) {
if (!RE_QNAME.matcher(qname).matches()) {
throw new IllegalArgumentException(String.format("Illegal character in qualified name: '%s'.", qname));
}
}
}
/*
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain copyright statements and
* notices. Redistributions must also contain a copy of this document.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name "DOM4J" must not be used to endorse or promote products derived
* from this Software without prior written permission of MetaStuff, Ltd. For
* written permission, please contact dom4j-info@metastuff.com.
*
* 4. Products derived from this Software may not be called "DOM4J" nor may
* "DOM4J" appear in their names without prior written permission of MetaStuff,
* Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
*
* 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
*
* THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
*/