jaxp/src/share/classes/javax/xml/namespace/QName.java
author ohair
Mon, 14 Apr 2008 14:52:27 -0700
changeset 311 3c14f21bf3f7
parent 6 7f561c08de6b
permissions -rw-r--r--
6484686: The next directory looks like it is no longer part of the build (deploy makefiles) Summary: Getting rid of the _OUTPUTDIR settings. Using BUILD_PARENT_DIRECTORY instead. This solves problems with the "/build/windows-i586*" paths getting mangled on Windows builds (fastdebug builds in particular). Reviewed-by: tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2006 Sun Microsystems, Inc.  All Rights Reserved.
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
 * have any questions.
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
package javax.xml.namespace;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import java.io.Serializable;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import java.security.AccessController;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import java.security.PrivilegedAction;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import javax.xml.XMLConstants;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 * <p><code>QName</code> represents a <strong>qualified name</strong>
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 * as defined in the XML specifications: <a
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 * href="http://www.w3.org/TR/xmlschema-2/#QName">XML Schema Part2:
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 * Datatypes specification</a>, <a
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 * href="http://www.w3.org/TR/REC-xml-names/#ns-qualnames">Namespaces
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 * in XML</a>, <a
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 * href="http://www.w3.org/XML/xml-names-19990114-errata">Namespaces
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 * in XML Errata</a>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 * <p>The value of a <code>QName</code> contains a <strong>Namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 * URI</strong>, <strong>local part</strong> and
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 * <strong>prefix</strong>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 * <p>The prefix is included in <code>QName</code> to retain lexical
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
 * information <strong><em>when present</em></strong> in an {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
 * javax.xml.transform.Source XML input source}. The prefix is
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 * <strong><em>NOT</em></strong> used in {@link #equals(Object)
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
 * QName.equals(Object)} or to compute the {@link #hashCode()
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
 * QName.hashCode()}.  Equality and the hash code are defined using
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
 * <strong><em>only</em></strong> the Namespace URI and local part.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
 * <p>If not specified, the Namespace URI is set to {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
 * javax.xml.XMLConstants#NULL_NS_URI XMLConstants.NULL_NS_URI}.
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
 * If not specified, the prefix is set to {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
 * javax.xml.XMLConstants#DEFAULT_NS_PREFIX
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
 * XMLConstants.DEFAULT_NS_PREFIX}.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
 * <p><code>QName</code> is immutable.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
 * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
 * @see <a href="http://www.w3.org/TR/xmlschema-2/#QName">
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
 *   XML Schema Part2: Datatypes specification</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
 * @see <a href="http://www.w3.org/TR/REC-xml-names/#ns-qualnames">
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
 *   Namespaces in XML</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
 * @see <a href="http://www.w3.org/XML/xml-names-19990114-errata">
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
 *   Namespaces in XML Errata</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
 * @since 1.5
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
public class QName implements Serializable {
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
     * <p>Stream Unique Identifier.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
     * <p>Due to a historical defect, QName was released with multiple
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
     * serialVersionUID values even though its serialization was the
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
     * same.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
     * <p>To workaround this issue, serialVersionUID is set with either
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
     * a default value or a compatibility value.  To use the
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
     * compatiblity value, set the system property:</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
     * <code>com.sun.xml.namespace.QName.useCompatibleSerialVersionUID=1.0</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
     * <p>This workaround was inspired by classes in the javax.management
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
     * package, e.g. ObjectName, etc.
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
     * See CR6267224 for original defect report.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    private static final long serialVersionUID;
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
     * <p>Default <code>serialVersionUID</code> value.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    private static final long defaultSerialVersionUID = -9120448754896609940L;
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
     * <p>Compatibility <code>serialVersionUID</code> value.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
    private static final long compatibleSerialVersionUID = 4418622981026545151L;
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
     * <p>Flag to use default or campatible serialVersionUID.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
    private static boolean useDefaultSerialVersionUID = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
    static {
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
            // use a privileged block as reading a system property
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
            String valueUseCompatibleSerialVersionUID = (String) AccessController.doPrivileged(
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
                    new PrivilegedAction() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
                        public Object run() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
                            return System.getProperty("com.sun.xml.namespace.QName.useCompatibleSerialVersionUID");
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
            );
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
            useDefaultSerialVersionUID = (valueUseCompatibleSerialVersionUID != null && valueUseCompatibleSerialVersionUID.equals("1.0")) ? false : true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
        } catch (Exception exception) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
            // use default if any Exceptions
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
            useDefaultSerialVersionUID = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
        // set serialVersionUID to desired value
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
        if (useDefaultSerialVersionUID) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
            serialVersionUID = defaultSerialVersionUID;
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
            serialVersionUID = compatibleSerialVersionUID;
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
     * <p>Namespace URI of this <code>QName</code>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
    private final String namespaceURI;
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
     * <p>local part of this <code>QName</code>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
    private final String localPart;
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
     * <p>prefix of this <code>QName</code>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
    private final String prefix;
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
     * <p><code>QName</code> constructor specifying the Namespace URI
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
     * and local part.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
     * <p>If the Namespace URI is <code>null</code>, it is set to
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
     * {@link javax.xml.XMLConstants#NULL_NS_URI
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
     * XMLConstants.NULL_NS_URI}.  This value represents no
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
     * explicitly defined Namespace as defined by the <a
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
     * href="http://www.w3.org/TR/REC-xml-names/#ns-qualnames">Namespaces
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
     * in XML</a> specification.  This action preserves compatible
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
     * behavior with QName 1.0.  Explicitly providing the {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
     * javax.xml.XMLConstants#NULL_NS_URI
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
     * XMLConstants.NULL_NS_URI} value is the preferred coding
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
     * style.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
     * <p>If the local part is <code>null</code> an
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
     * <code>IllegalArgumentException</code> is thrown.
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
     * A local part of "" is allowed to preserve
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
     * compatible behavior with QName 1.0. </p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
     * <p>When using this constructor, the prefix is set to {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
     * javax.xml.XMLConstants#DEFAULT_NS_PREFIX
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
     * XMLConstants.DEFAULT_NS_PREFIX}.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
     * <p>The Namespace URI is not validated as a
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
     * <a href="http://www.ietf.org/rfc/rfc2396.txt">URI reference</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
     * The local part is not validated as a
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
     * <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
     * as specified in <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
     * in XML</a>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
     * @param namespaceURI Namespace URI of the <code>QName</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
     * @param localPart    local part of the <code>QName</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
     * @throws IllegalArgumentException When <code>localPart</code> is
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
     *   <code>null</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
     * @see #QName(String namespaceURI, String localPart, String
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
     * prefix) QName(String namespaceURI, String localPart, String
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
     * prefix)
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
    public QName(final String namespaceURI, final String localPart) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
        this(namespaceURI, localPart, XMLConstants.DEFAULT_NS_PREFIX);
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
     * <p><code>QName</code> constructor specifying the Namespace URI,
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
     * local part and prefix.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
     * <p>If the Namespace URI is <code>null</code>, it is set to
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
     * {@link javax.xml.XMLConstants#NULL_NS_URI
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
     * XMLConstants.NULL_NS_URI}.  This value represents no
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
     * explicitly defined Namespace as defined by the <a
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
     * href="http://www.w3.org/TR/REC-xml-names/#ns-qualnames">Namespaces
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
     * in XML</a> specification.  This action preserves compatible
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
     * behavior with QName 1.0.  Explicitly providing the {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
     * javax.xml.XMLConstants#NULL_NS_URI
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
     * XMLConstants.NULL_NS_URI} value is the preferred coding
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
     * style.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
     * <p>If the local part is <code>null</code> an
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
     * <code>IllegalArgumentException</code> is thrown.
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
     * A local part of "" is allowed to preserve
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
     * compatible behavior with QName 1.0. </p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
     * <p>If the prefix is <code>null</code>, an
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
     * <code>IllegalArgumentException</code> is thrown.  Use {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
     * javax.xml.XMLConstants#DEFAULT_NS_PREFIX
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
     * XMLConstants.DEFAULT_NS_PREFIX} to explicitly indicate that no
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
     * prefix is present or the prefix is not relevant.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
     * <p>The Namespace URI is not validated as a
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
     * <a href="http://www.ietf.org/rfc/rfc2396.txt">URI reference</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
     * The local part and prefix are not validated as a
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
     * <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
     * as specified in <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
     * in XML</a>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
     * @param namespaceURI Namespace URI of the <code>QName</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
     * @param localPart    local part of the <code>QName</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
     * @param prefix       prefix of the <code>QName</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
     * @throws IllegalArgumentException When <code>localPart</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
     *   or <code>prefix</code> is <code>null</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
    public QName(String namespaceURI, String localPart, String prefix) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
        // map null Namespace URI to default
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
        // to preserve compatibility with QName 1.0
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
        if (namespaceURI == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
            this.namespaceURI = XMLConstants.NULL_NS_URI;
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
            this.namespaceURI = namespaceURI;
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
        // local part is required.
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
        // "" is allowed to preserve compatibility with QName 1.0
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
        if (localPart == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
            throw new IllegalArgumentException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
                    "local part cannot be \"null\" when creating a QName");
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
        this.localPart = localPart;
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
        // prefix is required
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
        if (prefix == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
            throw new IllegalArgumentException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
                    "prefix cannot be \"null\" when creating a QName");
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
        this.prefix = prefix;
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
     * <p><code>QName</code> constructor specifying the local part.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
     * <p>If the local part is <code>null</code> an
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
     * <code>IllegalArgumentException</code> is thrown.
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
     * A local part of "" is allowed to preserve
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
     * compatible behavior with QName 1.0. </p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
     * <p>When using this constructor, the Namespace URI is set to
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
     * {@link javax.xml.XMLConstants#NULL_NS_URI
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
     * XMLConstants.NULL_NS_URI} and the prefix is set to {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
     * javax.xml.XMLConstants#DEFAULT_NS_PREFIX
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
     * XMLConstants.DEFAULT_NS_PREFIX}.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
     * <p><em>In an XML context, all Element and Attribute names exist
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
     * in the context of a Namespace.  Making this explicit during the
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
     * construction of a <code>QName</code> helps prevent hard to
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
     * diagnosis XML validity errors.  The constructors {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
     * #QName(String namespaceURI, String localPart) QName(String
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
     * namespaceURI, String localPart)} and
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
     * {@link #QName(String namespaceURI, String localPart, String prefix)}
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
     * are preferred.</em></p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
     * <p>The local part is not validated as a
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
     * <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
     * as specified in <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
     * in XML</a>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
     * @param localPart local part of the <code>QName</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
     * @throws IllegalArgumentException When <code>localPart</code> is
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
     *   <code>null</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
     * @see #QName(String namespaceURI, String localPart) QName(String
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
     * namespaceURI, String localPart)
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
     * @see #QName(String namespaceURI, String localPart, String
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
     * prefix) QName(String namespaceURI, String localPart, String
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
     * prefix)
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
    public QName(String localPart) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
        this(
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
            XMLConstants.NULL_NS_URI,
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
            localPart,
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
            XMLConstants.DEFAULT_NS_PREFIX);
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
     * <p>Get the Namespace URI of this <code>QName</code>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
     * @return Namespace URI of this <code>QName</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
    public String getNamespaceURI() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
        return namespaceURI;
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
     * <p>Get the local part of this <code>QName</code>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
     *  @return local part of this <code>QName</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
    public String getLocalPart() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
        return localPart;
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
     * <p>Get the prefix of this <code>QName</code>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
     * <p>The prefix assigned to a <code>QName</code> might
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
     * <strong><em>NOT</em></strong> be valid in a different
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
     * context. For example, a <code>QName</code> may be assigned a
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
     * prefix in the context of parsing a document but that prefix may
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
     * be invalid in the context of a different document.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
     *  @return prefix of this <code>QName</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
    public String getPrefix() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
        return prefix;
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
     * <p>Test this <code>QName</code> for equality with another
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
     * <code>Object</code>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
     * <p>If the <code>Object</code> to be tested is not a
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
     * <code>QName</code> or is <code>null</code>, then this method
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
     * returns <code>false</code>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
     * <p>Two <code>QName</code>s are considered equal if and only if
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
     * both the Namespace URI and local part are equal. This method
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
     * uses <code>String.equals()</code> to check equality of the
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
     * Namespace URI and local part. The prefix is
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
     * <strong><em>NOT</em></strong> used to determine equality.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
     * <p>This method satisfies the general contract of {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
     * java.lang.Object#equals(Object) Object.equals(Object)}</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
     * @param objectToTest the <code>Object</code> to test for
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
     * equality with this <code>QName</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
     * @return <code>true</code> if the given <code>Object</code> is
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
     * equal to this <code>QName</code> else <code>false</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
    public final boolean equals(Object objectToTest) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
        if (objectToTest == null || !(objectToTest instanceof QName)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
            return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
        QName qName = (QName) objectToTest;
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
        return namespaceURI.equals(qName.namespaceURI)
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
            && localPart.equals(qName.localPart);
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
     * <p>Generate the hash code for this <code>QName</code>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
     * <p>The hash code is calculated using both the Namespace URI and
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
     * the local part of the <code>QName</code>.  The prefix is
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
     * <strong><em>NOT</em></strong> used to calculate the hash
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
     * code.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
     * <p>This method satisfies the general contract of {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
     * java.lang.Object#hashCode() Object.hashCode()}.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
     * @return hash code for this <code>QName</code> <code>Object</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
    public final int hashCode() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
        return namespaceURI.hashCode() ^ localPart.hashCode();
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
     * <p><code>String</code> representation of this
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
     * <code>QName</code>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
     * <p>The commonly accepted way of representing a <code>QName</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
     * as a <code>String</code> was
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
     * <a href="http://jclark.com/xml/xmlns.htm">defined</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
     * by James Clark.  Although this is not a <em>standard</em>
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
     * specification, it is in common use, e.g. {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
     * javax.xml.transform.Transformer#setParameter(String name, Object value)}.
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
     * This implementation represents a <code>QName</code> as:
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
     * "{" + Namespace URI + "}" + local part.  If the Namespace URI
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
     * <code>.equals(XMLConstants.NULL_NS_URI)</code>, only the
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
     * local part is returned.  An appropriate use of this method is
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
     * for debugging or logging for human consumption.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
     * <p>Note the prefix value is <strong><em>NOT</em></strong>
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
     * returned as part of the <code>String</code> representation.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
     * <p>This method satisfies the general contract of {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
     * java.lang.Object#toString() Object.toString()}.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
     *  @return <code>String</code> representation of this <code>QName</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
    public String toString() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
        if (namespaceURI.equals(XMLConstants.NULL_NS_URI)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
            return localPart;
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
            return "{" + namespaceURI + "}" + localPart;
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
     * <p><code>QName</code> derived from parsing the formatted
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
     * <code>String</code>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
     * <p>If the <code>String</code> is <code>null</code> or does not conform to
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
     * {@link #toString() QName.toString()} formatting, an
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
     * <code>IllegalArgumentException</code> is thrown.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
     * <p><em>The <code>String</code> <strong>MUST</strong> be in the
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
     * form returned by {@link #toString() QName.toString()}.</em></p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
     * <p>The commonly accepted way of representing a <code>QName</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
     * as a <code>String</code> was
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
     * <a href="http://jclark.com/xml/xmlns.htm">defined</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
     * by James Clark.  Although this is not a <em>standard</em>
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
     * specification, it is in common use, e.g. {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
     * javax.xml.transform.Transformer#setParameter(String name, Object value)}.
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
     * This implementation parses a <code>String</code> formatted
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
     * as: "{" + Namespace URI + "}" + local part.  If the Namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
     * URI <code>.equals(XMLConstants.NULL_NS_URI)</code>, only the
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
     * local part should be provided.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
     * <p>The prefix value <strong><em>CANNOT</em></strong> be
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
     * represented in the <code>String</code> and will be set to
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
     * {@link javax.xml.XMLConstants#DEFAULT_NS_PREFIX
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
     * XMLConstants.DEFAULT_NS_PREFIX}.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
     * <p>This method does not do full validation of the resulting
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
     * <code>QName</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
     * <p>The Namespace URI is not validated as a
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
     * <a href="http://www.ietf.org/rfc/rfc2396.txt">URI reference</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
     * The local part is not validated as a
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
     * <a href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
     * as specified in
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
     * <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces in XML</a>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
     * @param qNameAsString <code>String</code> representation
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
     * of the <code>QName</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
     * @throws IllegalArgumentException When <code>qNameAsString</code> is
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
     *   <code>null</code> or malformed
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
     * @return <code>QName</code> corresponding to the given <code>String</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
     * @see #toString() QName.toString()
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
    public static QName valueOf(String qNameAsString) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
        // null is not valid
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
        if (qNameAsString == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
            throw new IllegalArgumentException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
                    "cannot create QName from \"null\" or \"\" String");
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
        // "" local part is valid to preserve compatible behavior with QName 1.0
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
        if (qNameAsString.length() == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
            return new QName(
7f561c08de6b Initial load
duke
parents:
diff changeset
   473
                XMLConstants.NULL_NS_URI,
7f561c08de6b Initial load
duke
parents:
diff changeset
   474
                qNameAsString,
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
                XMLConstants.DEFAULT_NS_PREFIX);
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
        // local part only?
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
        if (qNameAsString.charAt(0) != '{') {
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
            return new QName(
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
                XMLConstants.NULL_NS_URI,
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
                qNameAsString,
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
                XMLConstants.DEFAULT_NS_PREFIX);
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
7f561c08de6b Initial load
duke
parents:
diff changeset
   486
        // Namespace URI improperly specified?
7f561c08de6b Initial load
duke
parents:
diff changeset
   487
        if (qNameAsString.startsWith("{" + XMLConstants.NULL_NS_URI + "}")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   488
            throw new IllegalArgumentException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   489
                "Namespace URI .equals(XMLConstants.NULL_NS_URI), "
7f561c08de6b Initial load
duke
parents:
diff changeset
   490
                + ".equals(\"" + XMLConstants.NULL_NS_URI + "\"), "
7f561c08de6b Initial load
duke
parents:
diff changeset
   491
                + "only the local part, "
7f561c08de6b Initial load
duke
parents:
diff changeset
   492
                + "\""
7f561c08de6b Initial load
duke
parents:
diff changeset
   493
                + qNameAsString.substring(2 + XMLConstants.NULL_NS_URI.length())
7f561c08de6b Initial load
duke
parents:
diff changeset
   494
                + "\", "
7f561c08de6b Initial load
duke
parents:
diff changeset
   495
                + "should be provided.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   496
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   497
7f561c08de6b Initial load
duke
parents:
diff changeset
   498
        // Namespace URI and local part specified
7f561c08de6b Initial load
duke
parents:
diff changeset
   499
        int endOfNamespaceURI = qNameAsString.indexOf('}');
7f561c08de6b Initial load
duke
parents:
diff changeset
   500
        if (endOfNamespaceURI == -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   501
            throw new IllegalArgumentException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   502
                "cannot create QName from \""
7f561c08de6b Initial load
duke
parents:
diff changeset
   503
                    + qNameAsString
7f561c08de6b Initial load
duke
parents:
diff changeset
   504
                    + "\", missing closing \"}\"");
7f561c08de6b Initial load
duke
parents:
diff changeset
   505
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   506
        return new QName(
7f561c08de6b Initial load
duke
parents:
diff changeset
   507
            qNameAsString.substring(1, endOfNamespaceURI),
7f561c08de6b Initial load
duke
parents:
diff changeset
   508
            qNameAsString.substring(endOfNamespaceURI + 1),
7f561c08de6b Initial load
duke
parents:
diff changeset
   509
            XMLConstants.DEFAULT_NS_PREFIX);
7f561c08de6b Initial load
duke
parents:
diff changeset
   510
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   511
}