2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
43
package org.netbeans.modules.xml.wsdl.ui.common;
46
* Implements a fully-qualified name model.
48
* @author Enrico Lelina
53
/** The XML namespace URI. */
54
public static final String XMLNS = "http://www.w3.org/XML/1998/namespace";
56
/** The local name. */
57
private String mLocalName = null;
59
/** The namespace URI. */
60
private String mNamespaceURI = null;
62
/** The namespace prefix. */
63
private String mPrefix = null;
66
* Constructs an empty QName.
73
* Constructs a QName given QName string
74
* @param localName the local name
76
public QName(String qNameStr) {
77
QName qName = getQNameFromString(qNameStr);
78
this.mNamespaceURI = qName.getNamespaceURI();
79
this.mPrefix = qName.getPrefix();
80
this.mLocalName = qName.getLocalName();
84
* Constructs a QName with the specified namespace URI and local name.
85
* @param namespaceURI the namespace URI
86
* @param name Either the local name or qualified name.
88
public QName(String namespaceURI, String name) {
89
this(namespaceURI, null, name);
93
* Constructs a QName with the specified namespace URI, namespace prefix
95
* @param namespaceURI the namespace URI
96
* @param prefix the namespace prefix
97
* @param name Either the local name or qualified name (in which case, prefix must be <code>null</code>).
99
public QName(String namespaceURI, String prefix, String name) {
100
mNamespaceURI = namespaceURI;
103
&& ((name != null) && ((colon = name.indexOf(':')) != -1))) {
104
mPrefix = name.substring(0, colon);
105
mLocalName = name.substring(colon + 1);
113
* Constructs a QName from the given QName string, e.g., "tns:foo", "foo".
114
* @param qNameString the QName string
115
* @return a new QName
117
public static QName getQNameFromString(String qNameString) {
118
QName qName = new QName(QName.getNamespaceURI(qNameString),
119
QName.getPrefix(qNameString),
120
QName.getLocalName(qNameString));
122
if(qName.getLocalName() == null) {
130
* Gets the prefix from the given QName string.
131
* @param qName the QName string
132
* @return the prefix or null if there is no prefix
134
public static String getPrefix(String qName) {
135
if(qName == null || qName.trim().equals("")) {
139
int index = qName.indexOf('{');
140
//if { then we have namespace
145
index = qName.lastIndexOf(':');
147
return ((index > 0) ? qName.substring(0, index) : null);
151
* Gets the local name from the given QName string.
152
* @param qName the QName string
153
* @return the local name
155
public static String getLocalName(String qName) {
156
if(qName == null || qName.trim().equals("")) {
160
//first check if qName is {namespace}localName
161
int index = qName.lastIndexOf('}');
164
index = qName.lastIndexOf(':');
167
return ((index < 0) ? qName : qName.substring(index + 1));
170
public static String getNamespaceURI(String qName) {
171
if(qName == null || qName.trim().equals("")) {
175
String namespace = null;
176
int sIndex = qName.indexOf('{');
177
int eIndex = qName.lastIndexOf('}');
181
namespace = qName.substring(sIndex+1, eIndex);
188
* Gets the local name.
189
* @return the local name
191
public String getLocalName() {
196
* Sets the local name.
197
* @param localName the new local name
199
public void setLocalName(String localName) {
200
mLocalName = localName;
204
* Gets the namespace URI.
205
* @return the namespace URI
207
public String getNamespaceURI() {
208
return mNamespaceURI;
212
* Sets the namespace URI.
213
* @param namespaceURI the new namespace URI
215
public void setNamespaceURI(String namespaceURI) {
216
mNamespaceURI = namespaceURI;
223
public String getPrefix() {
229
* @param prefix the new prefix
231
public void setPrefix(String prefix) {
236
* Returns the string representation of the QName. If the prefix is
237
* available, then it will be [prefix]:[localName], for example,
238
* tns:foo. If the prefix is not available and there is a namespace URI,
239
* then it will be {[namespaceURI]}[localName], for example,
240
* {http://schemas.xmlsoap.org/wsdl/}message. If neither the prefix not
241
* the namespace URI are present, then it will just be the local name.
242
* @return the QName's string representation
244
public String toString() {
245
String qName = (null == getLocalName()) ? "" : getLocalName();
247
if (getPrefix() != null) {
248
return getPrefix() + ':' + qName;
249
} else if (getNamespaceURI() != null) {
250
return '{' + getNamespaceURI() + '}' + qName;
256
public boolean equals(Object src) {
257
if(!(src instanceof QName)) {
261
QName srcQName = (QName) src;
262
return this.toString().equals(srcQName.toString());
266
public int hashCode() {
267
return this.toString().hashCode();