Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/namespace/QName.java
48534 views
/*1* Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.xml.namespace;2627import java.io.Serializable;28import java.security.AccessController;29import java.security.PrivilegedAction;3031import javax.xml.XMLConstants;3233/**34* <p><code>QName</code> represents a <strong>qualified name</strong>35* as defined in the XML specifications: <a36* href="http://www.w3.org/TR/xmlschema-2/#QName">XML Schema Part2:37* Datatypes specification</a>, <a38* href="http://www.w3.org/TR/REC-xml-names/#ns-qualnames">Namespaces39* in XML</a>, <a40* href="http://www.w3.org/XML/xml-names-19990114-errata">Namespaces41* in XML Errata</a>.</p>42*43* <p>The value of a <code>QName</code> contains a <strong>Namespace44* URI</strong>, <strong>local part</strong> and45* <strong>prefix</strong>.</p>46*47* <p>The prefix is included in <code>QName</code> to retain lexical48* information <strong><em>when present</em></strong> in an {@link49* javax.xml.transform.Source XML input source}. The prefix is50* <strong><em>NOT</em></strong> used in {@link #equals(Object)51* QName.equals(Object)} or to compute the {@link #hashCode()52* QName.hashCode()}. Equality and the hash code are defined using53* <strong><em>only</em></strong> the Namespace URI and local part.</p>54*55* <p>If not specified, the Namespace URI is set to {@link56* javax.xml.XMLConstants#NULL_NS_URI XMLConstants.NULL_NS_URI}.57* If not specified, the prefix is set to {@link58* javax.xml.XMLConstants#DEFAULT_NS_PREFIX59* XMLConstants.DEFAULT_NS_PREFIX}.</p>60*61* <p><code>QName</code> is immutable.</p>62*63* @author <a href="mailto:[email protected]">Jeff Suttor</a>64* @version $Revision: 1.8 $, $Date: 2010/03/18 03:06:17 $65* @see <a href="http://www.w3.org/TR/xmlschema-2/#QName">66* XML Schema Part2: Datatypes specification</a>67* @see <a href="http://www.w3.org/TR/REC-xml-names/#ns-qualnames">68* Namespaces in XML</a>69* @see <a href="http://www.w3.org/XML/xml-names-19990114-errata">70* Namespaces in XML Errata</a>71* @since 1.572*/7374public class QName implements Serializable {7576/**77* <p>Stream Unique Identifier.</p>78*79* <p>Due to a historical defect, QName was released with multiple80* serialVersionUID values even though its serialization was the81* same.</p>82*83* <p>To workaround this issue, serialVersionUID is set with either84* a default value or a compatibility value. To use the85* compatiblity value, set the system property:</p>86*87* <code>com.sun.xml.namespace.QName.useCompatibleSerialVersionUID=1.0</code>88*89* <p>This workaround was inspired by classes in the javax.management90* package, e.g. ObjectName, etc.91* See CR6267224 for original defect report.</p>92*/93private static final long serialVersionUID;94/**95* <p>Default <code>serialVersionUID</code> value.</p>96*/97private static final long defaultSerialVersionUID = -9120448754896609940L;98/**99* <p>Compatibility <code>serialVersionUID</code> value.</p>100*/101private static final long compatibleSerialVersionUID = 4418622981026545151L;102/**103* <p>Flag to use default or campatible serialVersionUID.</p>104*/105private static boolean useDefaultSerialVersionUID = true;106static {107try {108// use a privileged block as reading a system property109String valueUseCompatibleSerialVersionUID = (String) AccessController.doPrivileged(110new PrivilegedAction() {111public Object run() {112return System.getProperty("com.sun.xml.namespace.QName.useCompatibleSerialVersionUID");113}114}115);116useDefaultSerialVersionUID = (valueUseCompatibleSerialVersionUID != null && valueUseCompatibleSerialVersionUID.equals("1.0")) ? false : true;117} catch (Exception exception) {118// use default if any Exceptions119useDefaultSerialVersionUID = true;120}121122// set serialVersionUID to desired value123if (useDefaultSerialVersionUID) {124serialVersionUID = defaultSerialVersionUID;125} else {126serialVersionUID = compatibleSerialVersionUID;127}128}129130/**131* <p>Namespace URI of this <code>QName</code>.</p>132*/133private final String namespaceURI;134135/**136* <p>local part of this <code>QName</code>.</p>137*/138private final String localPart;139140/**141* <p>prefix of this <code>QName</code>.</p>142*/143private final String prefix;144145/**146* <p><code>QName</code> constructor specifying the Namespace URI147* and local part.</p>148*149* <p>If the Namespace URI is <code>null</code>, it is set to150* {@link javax.xml.XMLConstants#NULL_NS_URI151* XMLConstants.NULL_NS_URI}. This value represents no152* explicitly defined Namespace as defined by the <a153* href="http://www.w3.org/TR/REC-xml-names/#ns-qualnames">Namespaces154* in XML</a> specification. This action preserves compatible155* behavior with QName 1.0. Explicitly providing the {@link156* javax.xml.XMLConstants#NULL_NS_URI157* XMLConstants.NULL_NS_URI} value is the preferred coding158* style.</p>159*160* <p>If the local part is <code>null</code> an161* <code>IllegalArgumentException</code> is thrown.162* A local part of "" is allowed to preserve163* compatible behavior with QName 1.0. </p>164*165* <p>When using this constructor, the prefix is set to {@link166* javax.xml.XMLConstants#DEFAULT_NS_PREFIX167* XMLConstants.DEFAULT_NS_PREFIX}.</p>168*169* <p>The Namespace URI is not validated as a170* <a href="http://www.ietf.org/rfc/rfc2396.txt">URI reference</a>.171* The local part is not validated as a172* <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>173* as specified in <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces174* in XML</a>.</p>175*176* @param namespaceURI Namespace URI of the <code>QName</code>177* @param localPart local part of the <code>QName</code>178*179* @throws IllegalArgumentException When <code>localPart</code> is180* <code>null</code>181*182* @see #QName(String namespaceURI, String localPart, String183* prefix) QName(String namespaceURI, String localPart, String184* prefix)185*/186public QName(final String namespaceURI, final String localPart) {187this(namespaceURI, localPart, XMLConstants.DEFAULT_NS_PREFIX);188}189190/**191* <p><code>QName</code> constructor specifying the Namespace URI,192* local part and prefix.</p>193*194* <p>If the Namespace URI is <code>null</code>, it is set to195* {@link javax.xml.XMLConstants#NULL_NS_URI196* XMLConstants.NULL_NS_URI}. This value represents no197* explicitly defined Namespace as defined by the <a198* href="http://www.w3.org/TR/REC-xml-names/#ns-qualnames">Namespaces199* in XML</a> specification. This action preserves compatible200* behavior with QName 1.0. Explicitly providing the {@link201* javax.xml.XMLConstants#NULL_NS_URI202* XMLConstants.NULL_NS_URI} value is the preferred coding203* style.</p>204*205* <p>If the local part is <code>null</code> an206* <code>IllegalArgumentException</code> is thrown.207* A local part of "" is allowed to preserve208* compatible behavior with QName 1.0. </p>209*210* <p>If the prefix is <code>null</code>, an211* <code>IllegalArgumentException</code> is thrown. Use {@link212* javax.xml.XMLConstants#DEFAULT_NS_PREFIX213* XMLConstants.DEFAULT_NS_PREFIX} to explicitly indicate that no214* prefix is present or the prefix is not relevant.</p>215*216* <p>The Namespace URI is not validated as a217* <a href="http://www.ietf.org/rfc/rfc2396.txt">URI reference</a>.218* The local part and prefix are not validated as a219* <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>220* as specified in <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces221* in XML</a>.</p>222*223* @param namespaceURI Namespace URI of the <code>QName</code>224* @param localPart local part of the <code>QName</code>225* @param prefix prefix of the <code>QName</code>226*227* @throws IllegalArgumentException When <code>localPart</code>228* or <code>prefix</code> is <code>null</code>229*/230public QName(String namespaceURI, String localPart, String prefix) {231232// map null Namespace URI to default233// to preserve compatibility with QName 1.0234if (namespaceURI == null) {235this.namespaceURI = XMLConstants.NULL_NS_URI;236} else {237this.namespaceURI = namespaceURI;238}239240// local part is required.241// "" is allowed to preserve compatibility with QName 1.0242if (localPart == null) {243throw new IllegalArgumentException(244"local part cannot be \"null\" when creating a QName");245}246this.localPart = localPart;247248// prefix is required249if (prefix == null) {250throw new IllegalArgumentException(251"prefix cannot be \"null\" when creating a QName");252}253this.prefix = prefix;254}255256/**257* <p><code>QName</code> constructor specifying the local part.</p>258*259* <p>If the local part is <code>null</code> an260* <code>IllegalArgumentException</code> is thrown.261* A local part of "" is allowed to preserve262* compatible behavior with QName 1.0. </p>263*264* <p>When using this constructor, the Namespace URI is set to265* {@link javax.xml.XMLConstants#NULL_NS_URI266* XMLConstants.NULL_NS_URI} and the prefix is set to {@link267* javax.xml.XMLConstants#DEFAULT_NS_PREFIX268* XMLConstants.DEFAULT_NS_PREFIX}.</p>269*270* <p><em>In an XML context, all Element and Attribute names exist271* in the context of a Namespace. Making this explicit during the272* construction of a <code>QName</code> helps prevent hard to273* diagnosis XML validity errors. The constructors {@link274* #QName(String namespaceURI, String localPart) QName(String275* namespaceURI, String localPart)} and276* {@link #QName(String namespaceURI, String localPart, String prefix)}277* are preferred.</em></p>278*279* <p>The local part is not validated as a280* <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>281* as specified in <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces282* in XML</a>.</p>283*284* @param localPart local part of the <code>QName</code>285*286* @throws IllegalArgumentException When <code>localPart</code> is287* <code>null</code>288*289* @see #QName(String namespaceURI, String localPart) QName(String290* namespaceURI, String localPart)291* @see #QName(String namespaceURI, String localPart, String292* prefix) QName(String namespaceURI, String localPart, String293* prefix)294*/295public QName(String localPart) {296this(297XMLConstants.NULL_NS_URI,298localPart,299XMLConstants.DEFAULT_NS_PREFIX);300}301302/**303* <p>Get the Namespace URI of this <code>QName</code>.</p>304*305* @return Namespace URI of this <code>QName</code>306*/307public String getNamespaceURI() {308return namespaceURI;309}310311/**312* <p>Get the local part of this <code>QName</code>.</p>313*314* @return local part of this <code>QName</code>315*/316public String getLocalPart() {317return localPart;318}319320/**321* <p>Get the prefix of this <code>QName</code>.</p>322*323* <p>The prefix assigned to a <code>QName</code> might324* <strong><em>NOT</em></strong> be valid in a different325* context. For example, a <code>QName</code> may be assigned a326* prefix in the context of parsing a document but that prefix may327* be invalid in the context of a different document.</p>328*329* @return prefix of this <code>QName</code>330*/331public String getPrefix() {332return prefix;333}334335/**336* <p>Test this <code>QName</code> for equality with another337* <code>Object</code>.</p>338*339* <p>If the <code>Object</code> to be tested is not a340* <code>QName</code> or is <code>null</code>, then this method341* returns <code>false</code>.</p>342*343* <p>Two <code>QName</code>s are considered equal if and only if344* both the Namespace URI and local part are equal. This method345* uses <code>String.equals()</code> to check equality of the346* Namespace URI and local part. The prefix is347* <strong><em>NOT</em></strong> used to determine equality.</p>348*349* <p>This method satisfies the general contract of {@link350* java.lang.Object#equals(Object) Object.equals(Object)}</p>351*352* @param objectToTest the <code>Object</code> to test for353* equality with this <code>QName</code>354* @return <code>true</code> if the given <code>Object</code> is355* equal to this <code>QName</code> else <code>false</code>356*/357public final boolean equals(Object objectToTest) {358if (objectToTest == this) {359return true;360}361362if (objectToTest == null || !(objectToTest instanceof QName)) {363return false;364}365366QName qName = (QName) objectToTest;367368return localPart.equals(qName.localPart)369&& namespaceURI.equals(qName.namespaceURI);370}371372/**373* <p>Generate the hash code for this <code>QName</code>.</p>374*375* <p>The hash code is calculated using both the Namespace URI and376* the local part of the <code>QName</code>. The prefix is377* <strong><em>NOT</em></strong> used to calculate the hash378* code.</p>379*380* <p>This method satisfies the general contract of {@link381* java.lang.Object#hashCode() Object.hashCode()}.</p>382*383* @return hash code for this <code>QName</code> <code>Object</code>384*/385public final int hashCode() {386return namespaceURI.hashCode() ^ localPart.hashCode();387}388389/**390* <p><code>String</code> representation of this391* <code>QName</code>.</p>392*393* <p>The commonly accepted way of representing a <code>QName</code>394* as a <code>String</code> was395* <a href="http://jclark.com/xml/xmlns.htm">defined</a>396* by James Clark. Although this is not a <em>standard</em>397* specification, it is in common use, e.g. {@link398* javax.xml.transform.Transformer#setParameter(String name, Object value)}.399* This implementation represents a <code>QName</code> as:400* "{" + Namespace URI + "}" + local part. If the Namespace URI401* <code>.equals(XMLConstants.NULL_NS_URI)</code>, only the402* local part is returned. An appropriate use of this method is403* for debugging or logging for human consumption.</p>404*405* <p>Note the prefix value is <strong><em>NOT</em></strong>406* returned as part of the <code>String</code> representation.</p>407*408* <p>This method satisfies the general contract of {@link409* java.lang.Object#toString() Object.toString()}.</p>410*411* @return <code>String</code> representation of this <code>QName</code>412*/413public String toString() {414if (namespaceURI.equals(XMLConstants.NULL_NS_URI)) {415return localPart;416} else {417return "{" + namespaceURI + "}" + localPart;418}419}420421/**422* <p><code>QName</code> derived from parsing the formatted423* <code>String</code>.</p>424*425* <p>If the <code>String</code> is <code>null</code> or does not conform to426* {@link #toString() QName.toString()} formatting, an427* <code>IllegalArgumentException</code> is thrown.</p>428*429* <p><em>The <code>String</code> <strong>MUST</strong> be in the430* form returned by {@link #toString() QName.toString()}.</em></p>431*432* <p>The commonly accepted way of representing a <code>QName</code>433* as a <code>String</code> was434* <a href="http://jclark.com/xml/xmlns.htm">defined</a>435* by James Clark. Although this is not a <em>standard</em>436* specification, it is in common use, e.g. {@link437* javax.xml.transform.Transformer#setParameter(String name, Object value)}.438* This implementation parses a <code>String</code> formatted439* as: "{" + Namespace URI + "}" + local part. If the Namespace440* URI <code>.equals(XMLConstants.NULL_NS_URI)</code>, only the441* local part should be provided.</p>442*443* <p>The prefix value <strong><em>CANNOT</em></strong> be444* represented in the <code>String</code> and will be set to445* {@link javax.xml.XMLConstants#DEFAULT_NS_PREFIX446* XMLConstants.DEFAULT_NS_PREFIX}.</p>447*448* <p>This method does not do full validation of the resulting449* <code>QName</code>.450* <p>The Namespace URI is not validated as a451* <a href="http://www.ietf.org/rfc/rfc2396.txt">URI reference</a>.452* The local part is not validated as a453* <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>454* as specified in455* <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces in XML</a>.</p>456*457* @param qNameAsString <code>String</code> representation458* of the <code>QName</code>459*460* @throws IllegalArgumentException When <code>qNameAsString</code> is461* <code>null</code> or malformed462*463* @return <code>QName</code> corresponding to the given <code>String</code>464* @see #toString() QName.toString()465*/466public static QName valueOf(String qNameAsString) {467468// null is not valid469if (qNameAsString == null) {470throw new IllegalArgumentException(471"cannot create QName from \"null\" or \"\" String");472}473474// "" local part is valid to preserve compatible behavior with QName 1.0475if (qNameAsString.length() == 0) {476return new QName(477XMLConstants.NULL_NS_URI,478qNameAsString,479XMLConstants.DEFAULT_NS_PREFIX);480}481482// local part only?483if (qNameAsString.charAt(0) != '{') {484return new QName(485XMLConstants.NULL_NS_URI,486qNameAsString,487XMLConstants.DEFAULT_NS_PREFIX);488}489490// Namespace URI improperly specified?491if (qNameAsString.startsWith("{" + XMLConstants.NULL_NS_URI + "}")) {492throw new IllegalArgumentException(493"Namespace URI .equals(XMLConstants.NULL_NS_URI), "494+ ".equals(\"" + XMLConstants.NULL_NS_URI + "\"), "495+ "only the local part, "496+ "\""497+ qNameAsString.substring(2 + XMLConstants.NULL_NS_URI.length())498+ "\", "499+ "should be provided.");500}501502// Namespace URI and local part specified503int endOfNamespaceURI = qNameAsString.indexOf('}');504if (endOfNamespaceURI == -1) {505throw new IllegalArgumentException(506"cannot create QName from \""507+ qNameAsString508+ "\", missing closing \"}\"");509}510return new QName(511qNameAsString.substring(1, endOfNamespaceURI),512qNameAsString.substring(endOfNamespaceURI + 1),513XMLConstants.DEFAULT_NS_PREFIX);514}515}516517518