~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to xml/wsdlui/src/org/netbeans/modules/xml/wsdl/ui/common/QName.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
 
 
43
package org.netbeans.modules.xml.wsdl.ui.common;
 
44
 
 
45
/**
 
46
 * Implements a fully-qualified name model.
 
47
 *
 
48
 * @author Enrico Lelina
 
49
 * @version 
 
50
 */
 
51
public class QName {
 
52
 
 
53
    /** The XML namespace URI. */
 
54
    public static final String XMLNS = "http://www.w3.org/XML/1998/namespace";
 
55
 
 
56
    /** The local name. */
 
57
    private String mLocalName = null;
 
58
 
 
59
    /** The namespace URI. */
 
60
    private String mNamespaceURI = null;
 
61
 
 
62
    /** The namespace prefix. */
 
63
    private String mPrefix = null;
 
64
 
 
65
    /**
 
66
     * Constructs an empty QName.
 
67
     */
 
68
    public QName() {
 
69
        // nothing to do
 
70
    }
 
71
    
 
72
    /**
 
73
     * Constructs a QName given QName string
 
74
     * @param localName the local name
 
75
     */
 
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();
 
81
    }
 
82
    
 
83
    /**
 
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.
 
87
     */
 
88
    public QName(String namespaceURI, String name) {
 
89
        this(namespaceURI, null, name);
 
90
    }
 
91
    
 
92
    /**
 
93
     * Constructs a QName with the specified namespace URI, namespace prefix
 
94
     * and local name.
 
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>).
 
98
     */
 
99
    public QName(String namespaceURI, String prefix, String name) {
 
100
        mNamespaceURI = namespaceURI;
 
101
        int colon;
 
102
        if ((null == prefix)
 
103
                && ((name != null) && ((colon = name.indexOf(':')) != -1))) {
 
104
            mPrefix = name.substring(0, colon);
 
105
            mLocalName = name.substring(colon + 1);
 
106
        } else {
 
107
            mPrefix = prefix;
 
108
            mLocalName = name;
 
109
        }
 
110
    }
 
111
    
 
112
    /**
 
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
 
116
     */
 
117
    public static QName getQNameFromString(String qNameString) {
 
118
        QName qName = new QName(QName.getNamespaceURI(qNameString),
 
119
                                QName.getPrefix(qNameString),
 
120
                                QName.getLocalName(qNameString));
 
121
                               
 
122
        if(qName.getLocalName() == null) {
 
123
            return null;
 
124
        }
 
125
        
 
126
        return qName;
 
127
    }
 
128
    
 
129
    /**
 
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
 
133
     */
 
134
    public static String getPrefix(String qName) {
 
135
        if(qName == null || qName.trim().equals("")) {
 
136
            return null;
 
137
        }
 
138
        
 
139
        int index = qName.indexOf('{');
 
140
        //if { then we have namespace
 
141
        if(index != -1) {
 
142
            return null;
 
143
        }
 
144
        
 
145
        index = qName.lastIndexOf(':');
 
146
        
 
147
        return ((index > 0) ? qName.substring(0, index) : null); 
 
148
    }
 
149
    
 
150
    /**
 
151
     * Gets the local name from the given QName string.
 
152
     * @param qName the QName string
 
153
     * @return the local name
 
154
     */
 
155
    public static String getLocalName(String qName) {
 
156
        if(qName == null || qName.trim().equals("")) {
 
157
            return null;
 
158
        }
 
159
        
 
160
        //first check if qName is {namespace}localName
 
161
        int    index = qName.lastIndexOf('}');
 
162
        
 
163
        if(index == -1) {
 
164
            index = qName.lastIndexOf(':');
 
165
        }
 
166
        
 
167
        return ((index < 0) ? qName : qName.substring(index + 1));
 
168
    }
 
169
    
 
170
    public static String getNamespaceURI(String qName) {
 
171
        if(qName == null || qName.trim().equals("")) {
 
172
            return null;
 
173
        }
 
174
        
 
175
        String namespace = null;
 
176
        int sIndex = qName.indexOf('{');
 
177
        int eIndex = qName.lastIndexOf('}');
 
178
        
 
179
        if(sIndex != -1 
 
180
           && eIndex != -1) {
 
181
            namespace = qName.substring(sIndex+1, eIndex);
 
182
        }
 
183
        
 
184
        return namespace;
 
185
    }
 
186
    
 
187
    /**
 
188
     * Gets the local name.
 
189
     * @return the local name
 
190
     */
 
191
    public String getLocalName() {
 
192
        return mLocalName;
 
193
    }
 
194
    
 
195
    /**
 
196
     * Sets the local name.
 
197
     * @param localName the new local name
 
198
     */
 
199
    public void setLocalName(String localName) {
 
200
        mLocalName = localName;
 
201
    }
 
202
    
 
203
    /**
 
204
     * Gets the namespace URI.
 
205
     * @return the namespace URI
 
206
     */
 
207
    public String getNamespaceURI() {
 
208
        return mNamespaceURI;
 
209
    }
 
210
    
 
211
    /**
 
212
     * Sets the namespace URI.
 
213
     * @param namespaceURI the new namespace URI
 
214
     */
 
215
    public void setNamespaceURI(String namespaceURI) {
 
216
        mNamespaceURI = namespaceURI;
 
217
    }
 
218
    
 
219
    /**
 
220
     * Gets the prefix.
 
221
     * @return the prefix
 
222
     */
 
223
    public String getPrefix() {
 
224
        return mPrefix;
 
225
    }
 
226
    
 
227
    /**
 
228
     * Sets the prefix.
 
229
     * @param prefix the new prefix
 
230
     */
 
231
    public void setPrefix(String prefix) {
 
232
        mPrefix = prefix;
 
233
    }
 
234
    
 
235
    /**
 
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
 
243
     */
 
244
    public String toString() {
 
245
        String qName = (null == getLocalName()) ? "" : getLocalName();
 
246
        
 
247
        if (getPrefix() != null) {
 
248
            return getPrefix() + ':' + qName;
 
249
        } else if (getNamespaceURI() != null) {
 
250
            return '{' + getNamespaceURI() + '}' + qName;
 
251
        } else {
 
252
            return qName;
 
253
        }
 
254
    }
 
255
    
 
256
    public boolean equals(Object src) {
 
257
        if(!(src instanceof QName)) {
 
258
            return false;
 
259
        }
 
260
        
 
261
        QName srcQName = (QName) src;
 
262
        return this.toString().equals(srcQName.toString());
 
263
        
 
264
    }
 
265
    
 
266
    public int hashCode() {
 
267
        return this.toString().hashCode();
 
268
    }
 
269
}