Path: blob/master/src/java.xml/share/classes/javax/xml/namespace/QName.java
40948 views
/*1* Copyright (c) 2003, 2018, 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 javax.xml.XMLConstants;29import jdk.xml.internal.SecuritySupport;3031/**32* <p><code>QName</code> represents a <strong>qualified name</strong>33* as defined in the XML specifications: <a34* href="http://www.w3.org/TR/xmlschema-2/#QName">XML Schema Part2:35* Datatypes specification</a>, <a36* href="http://www.w3.org/TR/REC-xml-names/#ns-qualnames">Namespaces37* in XML</a>.38*39* <p>The value of a <code>QName</code> contains a <strong>Namespace40* URI</strong>, <strong>local part</strong> and41* <strong>prefix</strong>.</p>42*43* <p>The prefix is included in <code>QName</code> to retain lexical44* information <strong><em>when present</em></strong> in an {@link45* javax.xml.transform.Source XML input source}. The prefix is46* <strong><em>NOT</em></strong> used in {@link #equals(Object)47* QName.equals(Object)} or to compute the {@link #hashCode()48* QName.hashCode()}. Equality and the hash code are defined using49* <strong><em>only</em></strong> the Namespace URI and local part.</p>50*51* <p>If not specified, the Namespace URI is set to {@link52* javax.xml.XMLConstants#NULL_NS_URI XMLConstants.NULL_NS_URI}.53* If not specified, the prefix is set to {@link54* javax.xml.XMLConstants#DEFAULT_NS_PREFIX55* XMLConstants.DEFAULT_NS_PREFIX}.</p>56*57* <p><code>QName</code> is immutable.</p>58*59* @author Jeff Suttor60* @see <a href="http://www.w3.org/TR/xmlschema-2/#QName">61* XML Schema Part2: Datatypes specification</a>62* @see <a href="http://www.w3.org/TR/REC-xml-names/#ns-qualnames">63* Namespaces in XML</a>64* @since 1.565*/6667public class QName implements Serializable {68// tests show that the ID is the same from JDK 1.5 through JDK 969private static final long serialVersionUID = -9120448754896609940L;7071/**72* <p>Namespace URI of this <code>QName</code>.</p>73*/74private final String namespaceURI;7576/**77* <p>local part of this <code>QName</code>.</p>78*/79private final String localPart;8081/**82* <p>prefix of this <code>QName</code>.</p>83*/84private final String prefix;8586/**87* <p><code>QName</code> constructor specifying the Namespace URI88* and local part.</p>89*90* <p>If the Namespace URI is <code>null</code>, it is set to91* {@link javax.xml.XMLConstants#NULL_NS_URI92* XMLConstants.NULL_NS_URI}. This value represents no93* explicitly defined Namespace as defined by the <a94* href="http://www.w3.org/TR/REC-xml-names/#ns-qualnames">Namespaces95* in XML</a> specification. This action preserves compatible96* behavior with QName 1.0. Explicitly providing the {@link97* javax.xml.XMLConstants#NULL_NS_URI98* XMLConstants.NULL_NS_URI} value is the preferred coding99* style.</p>100*101* <p>If the local part is <code>null</code> an102* <code>IllegalArgumentException</code> is thrown.103* A local part of "" is allowed to preserve104* compatible behavior with QName 1.0. </p>105*106* <p>When using this constructor, the prefix is set to {@link107* javax.xml.XMLConstants#DEFAULT_NS_PREFIX108* XMLConstants.DEFAULT_NS_PREFIX}.</p>109*110* <p>The Namespace URI is not validated as a111* <a href="http://www.ietf.org/rfc/rfc2396.txt">URI reference</a>.112* The local part is not validated as a113* <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>114* as specified in <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces115* in XML</a>.</p>116*117* @param namespaceURI Namespace URI of the <code>QName</code>118* @param localPart local part of the <code>QName</code>119*120* @throws IllegalArgumentException When <code>localPart</code> is121* <code>null</code>122*123* @see #QName(String namespaceURI, String localPart, String124* prefix) QName(String namespaceURI, String localPart, String125* prefix)126*/127public QName(final String namespaceURI, final String localPart) {128this(namespaceURI, localPart, XMLConstants.DEFAULT_NS_PREFIX);129}130131/**132* <p><code>QName</code> constructor specifying the Namespace URI,133* local part and prefix.</p>134*135* <p>If the Namespace URI is <code>null</code>, it is set to136* {@link javax.xml.XMLConstants#NULL_NS_URI137* XMLConstants.NULL_NS_URI}. This value represents no138* explicitly defined Namespace as defined by the <a139* href="http://www.w3.org/TR/REC-xml-names/#ns-qualnames">Namespaces140* in XML</a> specification. This action preserves compatible141* behavior with QName 1.0. Explicitly providing the {@link142* javax.xml.XMLConstants#NULL_NS_URI143* XMLConstants.NULL_NS_URI} value is the preferred coding144* style.</p>145*146* <p>If the local part is <code>null</code> an147* <code>IllegalArgumentException</code> is thrown.148* A local part of "" is allowed to preserve149* compatible behavior with QName 1.0. </p>150*151* <p>If the prefix is <code>null</code>, an152* <code>IllegalArgumentException</code> is thrown. Use {@link153* javax.xml.XMLConstants#DEFAULT_NS_PREFIX154* XMLConstants.DEFAULT_NS_PREFIX} to explicitly indicate that no155* prefix is present or the prefix is not relevant.</p>156*157* <p>The Namespace URI is not validated as a158* <a href="http://www.ietf.org/rfc/rfc2396.txt">URI reference</a>.159* The local part and prefix are not validated as a160* <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>161* as specified in <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces162* in XML</a>.</p>163*164* @param namespaceURI Namespace URI of the <code>QName</code>165* @param localPart local part of the <code>QName</code>166* @param prefix prefix of the <code>QName</code>167*168* @throws IllegalArgumentException When <code>localPart</code>169* or <code>prefix</code> is <code>null</code>170*/171public QName(String namespaceURI, String localPart, String prefix) {172173// map null Namespace URI to default174// to preserve compatibility with QName 1.0175if (namespaceURI == null) {176this.namespaceURI = XMLConstants.NULL_NS_URI;177} else {178this.namespaceURI = namespaceURI;179}180181// local part is required.182// "" is allowed to preserve compatibility with QName 1.0183if (localPart == null) {184throw new IllegalArgumentException(185"local part cannot be \"null\" when creating a QName");186}187this.localPart = localPart;188189// prefix is required190if (prefix == null) {191throw new IllegalArgumentException(192"prefix cannot be \"null\" when creating a QName");193}194this.prefix = prefix;195}196197/**198* <p><code>QName</code> constructor specifying the local part.</p>199*200* <p>If the local part is <code>null</code> an201* <code>IllegalArgumentException</code> is thrown.202* A local part of "" is allowed to preserve203* compatible behavior with QName 1.0. </p>204*205* <p>When using this constructor, the Namespace URI is set to206* {@link javax.xml.XMLConstants#NULL_NS_URI207* XMLConstants.NULL_NS_URI} and the prefix is set to {@link208* javax.xml.XMLConstants#DEFAULT_NS_PREFIX209* XMLConstants.DEFAULT_NS_PREFIX}.</p>210*211* <p><em>In an XML context, all Element and Attribute names exist212* in the context of a Namespace. Making this explicit during the213* construction of a <code>QName</code> helps prevent hard to214* diagnosis XML validity errors. The constructors {@link215* #QName(String namespaceURI, String localPart) QName(String216* namespaceURI, String localPart)} and217* {@link #QName(String namespaceURI, String localPart, String prefix)}218* are preferred.</em></p>219*220* <p>The local part is not validated as a221* <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>222* as specified in <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces223* in XML</a>.</p>224*225* @param localPart local part of the <code>QName</code>226*227* @throws IllegalArgumentException When <code>localPart</code> is228* <code>null</code>229*230* @see #QName(String namespaceURI, String localPart) QName(String231* namespaceURI, String localPart)232* @see #QName(String namespaceURI, String localPart, String233* prefix) QName(String namespaceURI, String localPart, String234* prefix)235*/236public QName(String localPart) {237this(238XMLConstants.NULL_NS_URI,239localPart,240XMLConstants.DEFAULT_NS_PREFIX);241}242243/**244* <p>Get the Namespace URI of this <code>QName</code>.</p>245*246* @return Namespace URI of this <code>QName</code>247*/248public String getNamespaceURI() {249return namespaceURI;250}251252/**253* <p>Get the local part of this <code>QName</code>.</p>254*255* @return local part of this <code>QName</code>256*/257public String getLocalPart() {258return localPart;259}260261/**262* <p>Get the prefix of this <code>QName</code>.</p>263*264* <p>The prefix assigned to a <code>QName</code> might265* <strong><em>NOT</em></strong> be valid in a different266* context. For example, a <code>QName</code> may be assigned a267* prefix in the context of parsing a document but that prefix may268* be invalid in the context of a different document.</p>269*270* @return prefix of this <code>QName</code>271*/272public String getPrefix() {273return prefix;274}275276/**277* <p>Test this <code>QName</code> for equality with another278* <code>Object</code>.</p>279*280* <p>If the <code>Object</code> to be tested is not a281* <code>QName</code> or is <code>null</code>, then this method282* returns <code>false</code>.</p>283*284* <p>Two <code>QName</code>s are considered equal if and only if285* both the Namespace URI and local part are equal. This method286* uses <code>String.equals()</code> to check equality of the287* Namespace URI and local part. The prefix is288* <strong><em>NOT</em></strong> used to determine equality.</p>289*290* <p>This method satisfies the general contract of {@link291* java.lang.Object#equals(Object) Object.equals(Object)}</p>292*293* @param objectToTest the <code>Object</code> to test for294* equality with this <code>QName</code>295* @return <code>true</code> if the given <code>Object</code> is296* equal to this <code>QName</code> else <code>false</code>297*/298public final boolean equals(Object objectToTest) {299if (objectToTest == this) {300return true;301}302303if (objectToTest == null || !(objectToTest instanceof QName)) {304return false;305}306307QName qName = (QName) objectToTest;308309return localPart.equals(qName.localPart)310&& namespaceURI.equals(qName.namespaceURI);311}312313/**314* <p>Generate the hash code for this <code>QName</code>.</p>315*316* <p>The hash code is calculated using both the Namespace URI and317* the local part of the <code>QName</code>. The prefix is318* <strong><em>NOT</em></strong> used to calculate the hash319* code.</p>320*321* <p>This method satisfies the general contract of {@link322* java.lang.Object#hashCode() Object.hashCode()}.</p>323*324* @return hash code for this <code>QName</code> <code>Object</code>325*/326public final int hashCode() {327return namespaceURI.hashCode() ^ localPart.hashCode();328}329330/**331* <p><code>String</code> representation of this332* <code>QName</code>.</p>333*334* <p>The commonly accepted way of representing a <code>QName</code>335* as a <code>String</code> was336* <a href="http://jclark.com/xml/xmlns.htm">defined</a>337* by James Clark. Although this is not a <em>standard</em>338* specification, it is in common use, e.g. {@link339* javax.xml.transform.Transformer#setParameter(String name, Object value)}.340* This implementation represents a <code>QName</code> as:341* "{" + Namespace URI + "}" + local part. If the Namespace URI342* <code>.equals(XMLConstants.NULL_NS_URI)</code>, only the343* local part is returned. An appropriate use of this method is344* for debugging or logging for human consumption.</p>345*346* <p>Note the prefix value is <strong><em>NOT</em></strong>347* returned as part of the <code>String</code> representation.</p>348*349* <p>This method satisfies the general contract of {@link350* java.lang.Object#toString() Object.toString()}.</p>351*352* @return <code>String</code> representation of this <code>QName</code>353*/354public String toString() {355if (namespaceURI.equals(XMLConstants.NULL_NS_URI)) {356return localPart;357} else {358return "{" + namespaceURI + "}" + localPart;359}360}361362/**363* <p><code>QName</code> derived from parsing the formatted364* <code>String</code>.</p>365*366* <p>If the <code>String</code> is <code>null</code> or does not conform to367* {@link #toString() QName.toString()} formatting, an368* <code>IllegalArgumentException</code> is thrown.</p>369*370* <p><em>The <code>String</code> <strong>MUST</strong> be in the371* form returned by {@link #toString() QName.toString()}.</em></p>372*373* <p>The commonly accepted way of representing a <code>QName</code>374* as a <code>String</code> was375* <a href="http://jclark.com/xml/xmlns.htm">defined</a>376* by James Clark. Although this is not a <em>standard</em>377* specification, it is in common use, e.g. {@link378* javax.xml.transform.Transformer#setParameter(String name, Object value)}.379* This implementation parses a <code>String</code> formatted380* as: "{" + Namespace URI + "}" + local part. If the Namespace381* URI <code>.equals(XMLConstants.NULL_NS_URI)</code>, only the382* local part should be provided.</p>383*384* <p>The prefix value <strong><em>CANNOT</em></strong> be385* represented in the <code>String</code> and will be set to386* {@link javax.xml.XMLConstants#DEFAULT_NS_PREFIX387* XMLConstants.DEFAULT_NS_PREFIX}.</p>388*389* <p>This method does not do full validation of the resulting390* <code>QName</code>.391* <p>The Namespace URI is not validated as a392* <a href="http://www.ietf.org/rfc/rfc2396.txt">URI reference</a>.393* The local part is not validated as a394* <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>395* as specified in396* <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces in XML</a>.</p>397*398* @param qNameAsString <code>String</code> representation399* of the <code>QName</code>400*401* @throws IllegalArgumentException When <code>qNameAsString</code> is402* <code>null</code> or malformed403*404* @return <code>QName</code> corresponding to the given <code>String</code>405* @see #toString() QName.toString()406*/407public static QName valueOf(String qNameAsString) {408409// null is not valid410if (qNameAsString == null) {411throw new IllegalArgumentException(412"cannot create QName from \"null\" or \"\" String");413}414415// "" local part is valid to preserve compatible behavior with QName 1.0416if (qNameAsString.length() == 0) {417return new QName(418XMLConstants.NULL_NS_URI,419qNameAsString,420XMLConstants.DEFAULT_NS_PREFIX);421}422423// local part only?424if (qNameAsString.charAt(0) != '{') {425return new QName(426XMLConstants.NULL_NS_URI,427qNameAsString,428XMLConstants.DEFAULT_NS_PREFIX);429}430431// Namespace URI improperly specified?432if (qNameAsString.startsWith("{" + XMLConstants.NULL_NS_URI + "}")) {433throw new IllegalArgumentException(434"Namespace URI .equals(XMLConstants.NULL_NS_URI), "435+ ".equals(\"" + XMLConstants.NULL_NS_URI + "\"), "436+ "only the local part, "437+ "\""438+ qNameAsString.substring(2 + XMLConstants.NULL_NS_URI.length())439+ "\", "440+ "should be provided.");441}442443// Namespace URI and local part specified444int endOfNamespaceURI = qNameAsString.indexOf('}');445if (endOfNamespaceURI == -1) {446throw new IllegalArgumentException(447"cannot create QName from \""448+ qNameAsString449+ "\", missing closing \"}\"");450}451return new QName(452qNameAsString.substring(1, endOfNamespaceURI),453qNameAsString.substring(endOfNamespaceURI + 1),454XMLConstants.DEFAULT_NS_PREFIX);455}456}457458459