
javax.xml.namespace.QName Maven / Gradle / Ivy
/*
* Copyright 2003, 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package javax.xml.namespace;
import java.io.Serializable;
/** A QName
is a qualified name, as specified by
* XML Schema Part2: Datatypes specification, Namespaces in XML, Namespaces in XML Errata.
* A qualified name is made up of a namespace URI, a local part, and a prefix.
* The prefix is not really a part of the QName
and
* remains only to provide lexical information. It is not
* used in the {@link #equals(Object)} or {@link #hashCode()}
* methods.
* Namespace URI and prefix may be omitted, in which case the
* default value "" (empty string) is used.
* Instances of QName
are immutable. You may safely
* store references.
*/
public class QName implements Serializable {
private final String namespaceURI, localPart, prefix;
/** Creates a new QName
with the given
* pNamespaceURI
and pLocalPart
. The
* prefix is set to "" (empty string).
* @param pNamespaceURI The namespace URI; may be null, in which case
* the default value "" (empty string) is used.
* @param pLocalPart The local part.
* @throws IllegalArgumentException The local part was null.
*/
public QName(String pNamespaceURI, String pLocalPart) {
if (pLocalPart == null) {
throw new IllegalArgumentException("The local part must not be null");
}
namespaceURI = pNamespaceURI == null ? "" : pNamespaceURI;
localPart = pLocalPart;
prefix = "";
}
/** Creates a new QName
with the given
* pNamespaceURI
, pLocalPart
, and
* pPrefix
.
* @param pNamespaceURI The namespace URI; may be null, in which case
* the default value "" (empty string) is used.
* @param pLocalPart The local part.
* @param pPrefix The prefix. Must not be null. Use "" (empty string)
* to indicate that no namespace URI is present or the namespace
* URI is not relevant.
* @throws IllegalArgumentException The local part or the prefix was null.
*/
public QName(String pNamespaceURI, String pLocalPart, java.lang.String pPrefix) {
if (pLocalPart == null) {
throw new IllegalArgumentException("The local part must not be null");
}
if (pPrefix == null) {
throw new IllegalArgumentException("The prefix must not be null");
}
namespaceURI = pNamespaceURI == null ? "" : pNamespaceURI;
localPart = pLocalPart;
prefix = pPrefix;
}
/** Creates a new QName
with the given
* pLocalPart
, the namespace URI "" (empty string),
* and the prefix "" (empty string).
* @param pLocalPart The local part.
* @throws IllegalArgumentException The local part or the prefix was null.
*/
public QName(String pLocalPart) {
if (pLocalPart == null) {
throw new IllegalArgumentException("The local part must not be null");
}
namespaceURI = "";
localPart = pLocalPart;
prefix = "";
}
/** Returns the namespace URI.
* @return Namespace URI or "" (empty string) to indicate the absence
* of a namespace.
*/
public String getNamespaceURI() {
return namespaceURI;
}
/** Returns the local part of the QName
.
* @return The local part.
*/
public String getLocalPart() {
return localPart;
}
/** Returns the namespace prefix.
* @return The namespace prefix or "" (empty string) to indicate the
* default namespace
*/
public String getPrefix() {
return prefix;
}
/** Returns true, if
*
* pOther
instanceof QName
* - getNamespaceURI().equals(pOther.getNamespaceURI())
* - getLocalPart().equals(pOther.getLocalPart())
*
* Note: The prefix is ignored.
*/
public boolean equals(Object pOther) {
if (!(pOther instanceof QName)) {
return false;
}
QName other = (QName) pOther;
return namespaceURI.equals(other.namespaceURI) && localPart.equals(other.localPart);
}
/** Returns the QName
's hash code.
* The prefix is ignored when calculating the hash code.
*/
public int hashCode() {
return namespaceURI.hashCode() + localPart.hashCode();
}
/** Converts the QName into a string representation. The current
* implementation returns the local part, if the namespace URI is
* "" (empty string). Otherwise returns "{" + namespaceURI + "}" + localPart.
* The prefix is ignored.
* The representation is subject to changes, as there is currently no
* standard representation for a QName
. You should use this
* method for debugging or logging purposes only.
*/
public java.lang.String toString() {
return namespaceURI.length() == 0 ?
localPart : "{" + namespaceURI + "}" + localPart;
}
/** Parses the given string representation of a pQName
.
* The QName
is expected to have the same representation
* than returned by {@link #toString()}.
* It is not possible to specify a prefix. The returned
* QName
will always have the prefix "" (empty string).
* @param pQName String representation of a QName, as generated by
* {@link #toString()}.
* @return QName with the prefix "" (empty string)
* @throws IllegalArgumentException The given pQName
* was null or empty.
*/
public static QName valueOf(String pQName) {
if (pQName == null) {
throw new IllegalArgumentException("The string representation of a QName must not be null.");
}
if (pQName.charAt(0) == '{') {
int end = pQName.indexOf('}', 1);
if (end == -1) {
throw new IllegalArgumentException("Expected a terminator ('}') of the namespace URI.");
}
return new QName(pQName.substring(1, end), pQName.substring(end+1));
} else {
return new QName(pQName);
}
}
}