KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > common > QName


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.xml.wsdl.ui.common;
22
23 /**
24  * Implements a fully-qualified name model.
25  *
26  * @author Enrico Lelina
27  * @version $Revision: 1.3 $
28  */

29 public class QName {
30
31     /** The XML namespace URI. */
32     public static final String JavaDoc XMLNS = "http://www.w3.org/XML/1998/namespace";
33
34     /** The local name. */
35     private String JavaDoc mLocalName = null;
36
37     /** The namespace URI. */
38     private String JavaDoc mNamespaceURI = null;
39
40     /** The namespace prefix. */
41     private String JavaDoc mPrefix = null;
42
43     /**
44      * Constructs an empty QName.
45      */

46     public QName() {
47         // nothing to do
48
}
49     
50     /**
51      * Constructs a QName given QName string
52      * @param localName the local name
53      */

54     public QName(String JavaDoc qNameStr) {
55         QName qName = getQNameFromString(qNameStr);
56         this.mNamespaceURI = qName.getNamespaceURI();
57         this.mPrefix = qName.getPrefix();
58         this.mLocalName = qName.getLocalName();
59     }
60     
61     /**
62      * Constructs a QName with the specified namespace URI and local name.
63      * @param namespaceURI the namespace URI
64      * @param name Either the local name or qualified name.
65      */

66     public QName(String JavaDoc namespaceURI, String JavaDoc name) {
67         this(namespaceURI, null, name);
68     }
69     
70     /**
71      * Constructs a QName with the specified namespace URI, namespace prefix
72      * and local name.
73      * @param namespaceURI the namespace URI
74      * @param prefix the namespace prefix
75      * @param name Either the local name or qualified name (in which case, prefix must be <code>null</code>).
76      */

77     public QName(String JavaDoc namespaceURI, String JavaDoc prefix, String JavaDoc name) {
78         mNamespaceURI = namespaceURI;
79         int colon;
80         if ((null == prefix)
81                 && ((name != null) && ((colon = name.indexOf(':')) != -1))) {
82             mPrefix = name.substring(0, colon);
83             mLocalName = name.substring(colon + 1);
84         } else {
85             mPrefix = prefix;
86             mLocalName = name;
87         }
88     }
89     
90     /**
91      * Constructs a QName from the given QName string, e.g., "tns:foo", "foo".
92      * @param qNameString the QName string
93      * @return a new QName
94      */

95     public static QName getQNameFromString(String JavaDoc qNameString) {
96         QName qName = new QName(QName.getNamespaceURI(qNameString),
97                                 QName.getPrefix(qNameString),
98                                 QName.getLocalName(qNameString));
99                                
100         if(qName.getLocalName() == null) {
101             return null;
102         }
103         
104         return qName;
105     }
106     
107     /**
108      * Gets the prefix from the given QName string.
109      * @param qName the QName string
110      * @return the prefix or null if there is no prefix
111      */

112     public static String JavaDoc getPrefix(String JavaDoc qName) {
113         if(qName == null || qName.trim().equals("")) {
114             return null;
115         }
116         
117         int index = qName.indexOf('{');
118         //if { then we have namespace
119
if(index != -1) {
120             return null;
121         }
122         
123         index = qName.lastIndexOf(':');
124         
125         return ((index > 0) ? qName.substring(0, index) : null);
126     }
127     
128     /**
129      * Gets the local name from the given QName string.
130      * @param qName the QName string
131      * @return the local name
132      */

133     public static String JavaDoc getLocalName(String JavaDoc qName) {
134         if(qName == null || qName.trim().equals("")) {
135             return null;
136         }
137         
138         //first check if qName is {namespace}localName
139
int index = qName.lastIndexOf('}');
140         
141         if(index == -1) {
142             index = qName.lastIndexOf(':');
143         }
144         
145         return ((index < 0) ? qName : qName.substring(index + 1));
146     }
147     
148     public static String JavaDoc getNamespaceURI(String JavaDoc qName) {
149         if(qName == null || qName.trim().equals("")) {
150             return null;
151         }
152         
153         String JavaDoc namespace = null;
154         int sIndex = qName.indexOf('{');
155         int eIndex = qName.lastIndexOf('}');
156         
157         if(sIndex != -1
158            && eIndex != -1) {
159             namespace = qName.substring(sIndex+1, eIndex);
160         }
161         
162         return namespace;
163     }
164     
165     /**
166      * Gets the local name.
167      * @return the local name
168      */

169     public String JavaDoc getLocalName() {
170         return mLocalName;
171     }
172     
173     /**
174      * Sets the local name.
175      * @param localName the new local name
176      */

177     public void setLocalName(String JavaDoc localName) {
178         mLocalName = localName;
179     }
180     
181     /**
182      * Gets the namespace URI.
183      * @return the namespace URI
184      */

185     public String JavaDoc getNamespaceURI() {
186         return mNamespaceURI;
187     }
188     
189     /**
190      * Sets the namespace URI.
191      * @param namespaceURI the new namespace URI
192      */

193     public void setNamespaceURI(String JavaDoc namespaceURI) {
194         mNamespaceURI = namespaceURI;
195     }
196     
197     /**
198      * Gets the prefix.
199      * @return the prefix
200      */

201     public String JavaDoc getPrefix() {
202         return mPrefix;
203     }
204     
205     /**
206      * Sets the prefix.
207      * @param prefix the new prefix
208      */

209     public void setPrefix(String JavaDoc prefix) {
210         mPrefix = prefix;
211     }
212     
213     /**
214      * Returns the string representation of the QName. If the prefix is
215      * available, then it will be [prefix]:[localName], for example,
216      * tns:foo. If the prefix is not available and there is a namespace URI,
217      * then it will be {[namespaceURI]}[localName], for example,
218      * {http://schemas.xmlsoap.org/wsdl/}message. If neither the prefix not
219      * the namespace URI are present, then it will just be the local name.
220      * @return the QName's string representation
221      */

222     public String JavaDoc toString() {
223         String JavaDoc qName = (null == getLocalName()) ? "" : getLocalName();
224         
225         if (getPrefix() != null) {
226             return getPrefix() + ':' + qName;
227         } else if (getNamespaceURI() != null) {
228             return '{' + getNamespaceURI() + '}' + qName;
229         } else {
230             return qName;
231         }
232     }
233     
234     public boolean equals(Object JavaDoc src) {
235         if(!(src instanceof QName)) {
236             return false;
237         }
238         
239         QName srcQName = (QName) src;
240         return this.toString().equals(srcQName.toString());
241         
242     }
243     
244     public int hashCode() {
245         return this.toString().hashCode();
246     }
247 }
248
Popular Tags