org.jdom2.Namespace Maven / Gradle / Ivy
Show all versions of jdom Show documentation
/*--
Copyright (C) 2000-2012 Jason Hunter & Brett McLaughlin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the disclaimer that follows
these conditions in the documentation and/or other materials
provided with the distribution.
3. The name "JDOM" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact .
4. Products derived from this software may not be called "JDOM", nor
may "JDOM" appear in their name, without prior written permission
from the JDOM Project Management .
In addition, we request (but do not require) that you include in the
end-user documentation provided with the redistribution and/or in the
software itself an acknowledgement equivalent to the following:
"This product includes software developed by the
JDOM Project (http://www.jdom.org/)."
Alternatively, the acknowledgment may be graphical using the logos
available at http://www.jdom.org/images/logos.
THIS SOFTWARE IS PROVIDED ``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 THE JDOM AUTHORS OR THE PROJECT
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.
This software consists of voluntary contributions made by many
individuals on behalf of the JDOM Project and was originally
created by Jason Hunter and
Brett McLaughlin . For more information
on the JDOM Project, please see .
*/
package org.jdom2;
import static org.jdom2.JDOMConstants.*;
import java.io.InvalidObjectException;
import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* An XML namespace representation, as well as a factory for creating XML
* namespace objects.
*
* See {@link NamespaceAware} for additional notes on how Namespaces are
* 'in-scope' in JDOM content, and how those in-scope Namespaces are accessed.
*
* @see NamespaceAware
* @author Brett McLaughlin
* @author Elliotte Rusty Harold
* @author Jason Hunter
* @author Wesley Biggs
* @author Rolf Lear
*/
public final class Namespace implements Serializable {
/**
* Factory list of namespaces.
* Keys are URI&prefix.
* Values are Namespace objects
*/
private static final ConcurrentMap>
namespacemap = new ConcurrentHashMap
>(512, 0.75f, 64);
/** Define a Namespace
for when not in a namespace */
public static final Namespace NO_NAMESPACE = new Namespace(NS_PREFIX_DEFAULT,
NS_URI_DEFAULT);
/** Define a Namespace
for the standard xml prefix. */
public static final Namespace XML_NAMESPACE = new Namespace(NS_PREFIX_XML,
NS_URI_XML);
static {
// pre-populate the map with the constant namespaces that would
// otherwise fail validation
final ConcurrentMap nmap =
new ConcurrentHashMap();
nmap.put(NO_NAMESPACE.getPrefix(), NO_NAMESPACE);
namespacemap.put(NO_NAMESPACE.getURI(), nmap);
final ConcurrentMap xmap =
new ConcurrentHashMap();
xmap.put(XML_NAMESPACE.getPrefix(), XML_NAMESPACE);
namespacemap.put(XML_NAMESPACE.getURI(), xmap);
}
/**
* This will retrieve (if in existence) or create (if not) a
* Namespace
for the supplied prefix and uri.
*
* @param prefix String
prefix to map to
* Namespace
.
* @param uri String
URI of new Namespace
.
* @return Namespace
- ready to use namespace.
* @throws IllegalNameException if the given prefix and uri make up
* an illegal namespace name.
* @see Verifier#checkNamespacePrefix(String)
* @see Verifier#checkNamespaceURI(String)
*/
public static Namespace getNamespace(final String prefix, final String uri) {
// This is a rewrite of the JDOM 1 getNamespace() to use
// java.util.concurrent. The motivation is:
// 1. avoid having to create a new NamespaceKey for each query.
// 2. avoid a 'big' synchronisation bottleneck in the Namespace class.
// 3. no-memory-lookup for pre-existing Namespaces... (avoid 'new' and
// most String methods that allocte memory (like trim())
if (uri == null) {
if (prefix == null || NS_PREFIX_DEFAULT.equals(prefix)) {
return NO_NAMESPACE;
}
// we have an attempt for some prefix
// (not "" or it would have found NO_NAMESPACE) on the null URI
throw new IllegalNameException("", "namespace",
"Namespace URIs must be non-null and non-empty Strings");
}
// must have checked for 'null' uri else namespacemap throws NPE
// do not 'trim' uri's any more see issue #50
ConcurrentMap urimap = namespacemap.get(uri);
if (urimap == null) {
// no Namespaces yet with that URI.
// Ensure proper naming
String reason;
if ((reason = Verifier.checkNamespaceURI(uri)) != null) {
throw new IllegalNameException(uri, "Namespace URI", reason);
}
urimap = new ConcurrentHashMap();
final ConcurrentMap xmap =
namespacemap.putIfAbsent(uri, urimap);
if (xmap != null) {
// some other thread registered this URI between when we
// first checked, and when we got a new map created.
// we must use the already-registered value.
urimap = xmap;
}
}
// OK, we have a container for the URI, let's search on the prefix.
Namespace ns = urimap.get(prefix == null ? NS_PREFIX_DEFAULT : prefix);
if (ns != null) {
// got one.
return ns;
}
// OK, no namespace yet for that uri/prefix
// validate the prefix (the uri is already validated).
if (NS_URI_DEFAULT.equals(uri)) {
// we have an attempt for some prefix
// (not "" or it would have found NO_NAMESPACE) on the "" URI
// note, we have already done this check for 'null' uri above.
throw new IllegalNameException("", "namespace",
"Namespace URIs must be non-null and non-empty Strings");
}
// The erratum to Namespaces in XML 1.0 that suggests this
// next check is controversial. Not everyone accepts it.
if (NS_URI_XML.equals(uri)) {
throw new IllegalNameException(uri, "Namespace URI",
"The " + NS_URI_XML + " must be bound to " +
"only the '" + NS_PREFIX_XML + "' prefix.");
}
// no namespace found, we validate the prefix
final String pfx = prefix == null ? NS_PREFIX_DEFAULT : prefix;
String reason;
if ((reason = Verifier.checkNamespacePrefix(pfx)) != null) {
throw new IllegalNameException(pfx, "Namespace prefix", reason);
}
// OK, good bet that we have a new Namespace.
ns = new Namespace(pfx, uri);
final Namespace prev = urimap.putIfAbsent(pfx, ns);
if (prev != null) {
// someone registered the same namespace as us while we were busy
// validating. Use their registered copy.
ns = prev;
}
return ns;
}
/** The prefix mapped to this namespace */
private final transient String prefix;
/** The URI for this namespace */
private final transient String uri;
/**
* This will retrieve (if in existence) or create (if not) a
* Namespace
for the supplied URI, and make it usable
* as a default namespace, as no prefix is supplied.
*
* @param uri String
URI of new Namespace
.
* @return Namespace
- ready to use namespace.
*/
public static Namespace getNamespace(final String uri) {
return getNamespace(NS_PREFIX_DEFAULT, uri);
}
/**
* This constructor handles creation of a Namespace
object
* with a prefix and URI; it is intentionally left private
* so that it cannot be invoked by external programs/code.
*
* @param prefix String
prefix to map to this namespace.
* @param uri String
URI for namespace.
*/
private Namespace(final String prefix, final String uri) {
this.prefix = prefix;
this.uri = uri;
}
/**
* This returns the prefix mapped to this Namespace
.
*
* @return String
- prefix for this Namespace
.
*/
public String getPrefix() {
return prefix;
}
/**
* This returns the namespace URI for this Namespace
.
*
* @return String
- URI for this Namespace
.
*/
public String getURI() {
return uri;
}
/**
* This tests for equality - Two Namespaces
* are equal if and only if their URIs are byte-for-byte equals.
*
* @param ob Object
to compare to this Namespace
.
* @return boolean
- whether the supplied object is equal to
* this Namespace
.
*/
@Override
public boolean equals(final Object ob) {
if (this == ob) {
return true;
}
if (ob instanceof Namespace) { // instanceof returns false if null
return uri.equals(((Namespace)ob).uri);
}
return false;
}
/**
* This returns a String
representation of this
* Namespace
, suitable for use in debugging.
*
* @return String
- information about this instance.
*/
@Override
public String toString() {
return "[Namespace: prefix \"" + prefix + "\" is mapped to URI \"" +
uri + "\"]";
}
/**
* This returns the hash code for the Namespace
that conforms
* to the 'equals()' contract.
*
* If two namespaces have the same URI, they are equal and have the same
* hash code, even if they have different prefixes.
*
* @return int
- hash code for this Namespace
.
*/
@Override
public int hashCode() {
return uri.hashCode();
}
/* *****************************************
* Serialization
* ***************************************** */
/**
* JDOM 2.0.0 Serialization version
*/
private static final long serialVersionUID = 200L;
private static final class NamespaceSerializationProxy
implements Serializable {
private static final long serialVersionUID = 200L;
private final String pprefix, puri;
public NamespaceSerializationProxy(String pprefix, String puri) {
this.pprefix = pprefix;
this.puri = puri;
}
private Object readResolve() {
return Namespace.getNamespace(pprefix, puri);
}
}
/**
* Serializes Namespace by using a proxy serialization instance.
* @serialData The proxy deals with the protocol.
* @return the Namespace proxy instance.
*/
private Object writeReplace() {
return new NamespaceSerializationProxy(prefix, uri);
}
/**
* Because Namespace is serialized by proxy, the reading of direct Namespace
* instances is illegal and prohibited.
* @return nothing.
* @throws InvalidObjectException always
*/
private Object readResolve() throws InvalidObjectException {
throw new InvalidObjectException(
"Namespace is serialized through a proxy");
}
}