001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.wsdl.ui.common;
043:
044: /**
045: * Implements a fully-qualified name model.
046: *
047: * @author Enrico Lelina
048: * @version
049: */
050: public class QName {
051:
052: /** The XML namespace URI. */
053: public static final String XMLNS = "http://www.w3.org/XML/1998/namespace";
054:
055: /** The local name. */
056: private String mLocalName = null;
057:
058: /** The namespace URI. */
059: private String mNamespaceURI = null;
060:
061: /** The namespace prefix. */
062: private String mPrefix = null;
063:
064: /**
065: * Constructs an empty QName.
066: */
067: public QName() {
068: // nothing to do
069: }
070:
071: /**
072: * Constructs a QName given QName string
073: * @param localName the local name
074: */
075: public QName(String qNameStr) {
076: QName qName = getQNameFromString(qNameStr);
077: this .mNamespaceURI = qName.getNamespaceURI();
078: this .mPrefix = qName.getPrefix();
079: this .mLocalName = qName.getLocalName();
080: }
081:
082: /**
083: * Constructs a QName with the specified namespace URI and local name.
084: * @param namespaceURI the namespace URI
085: * @param name Either the local name or qualified name.
086: */
087: public QName(String namespaceURI, String name) {
088: this (namespaceURI, null, name);
089: }
090:
091: /**
092: * Constructs a QName with the specified namespace URI, namespace prefix
093: * and local name.
094: * @param namespaceURI the namespace URI
095: * @param prefix the namespace prefix
096: * @param name Either the local name or qualified name (in which case, prefix must be <code>null</code>).
097: */
098: public QName(String namespaceURI, String prefix, String name) {
099: mNamespaceURI = namespaceURI;
100: int colon;
101: if ((null == prefix)
102: && ((name != null) && ((colon = name.indexOf(':')) != -1))) {
103: mPrefix = name.substring(0, colon);
104: mLocalName = name.substring(colon + 1);
105: } else {
106: mPrefix = prefix;
107: mLocalName = name;
108: }
109: }
110:
111: /**
112: * Constructs a QName from the given QName string, e.g., "tns:foo", "foo".
113: * @param qNameString the QName string
114: * @return a new QName
115: */
116: public static QName getQNameFromString(String qNameString) {
117: QName qName = new QName(QName.getNamespaceURI(qNameString),
118: QName.getPrefix(qNameString), QName
119: .getLocalName(qNameString));
120:
121: if (qName.getLocalName() == null) {
122: return null;
123: }
124:
125: return qName;
126: }
127:
128: /**
129: * Gets the prefix from the given QName string.
130: * @param qName the QName string
131: * @return the prefix or null if there is no prefix
132: */
133: public static String getPrefix(String qName) {
134: if (qName == null || qName.trim().equals("")) {
135: return null;
136: }
137:
138: int index = qName.indexOf('{');
139: //if { then we have namespace
140: if (index != -1) {
141: return null;
142: }
143:
144: index = qName.lastIndexOf(':');
145:
146: return ((index > 0) ? qName.substring(0, index) : null);
147: }
148:
149: /**
150: * Gets the local name from the given QName string.
151: * @param qName the QName string
152: * @return the local name
153: */
154: public static String getLocalName(String qName) {
155: if (qName == null || qName.trim().equals("")) {
156: return null;
157: }
158:
159: //first check if qName is {namespace}localName
160: int index = qName.lastIndexOf('}');
161:
162: if (index == -1) {
163: index = qName.lastIndexOf(':');
164: }
165:
166: return ((index < 0) ? qName : qName.substring(index + 1));
167: }
168:
169: public static String getNamespaceURI(String qName) {
170: if (qName == null || qName.trim().equals("")) {
171: return null;
172: }
173:
174: String namespace = null;
175: int sIndex = qName.indexOf('{');
176: int eIndex = qName.lastIndexOf('}');
177:
178: if (sIndex != -1 && eIndex != -1) {
179: namespace = qName.substring(sIndex + 1, eIndex);
180: }
181:
182: return namespace;
183: }
184:
185: /**
186: * Gets the local name.
187: * @return the local name
188: */
189: public String getLocalName() {
190: return mLocalName;
191: }
192:
193: /**
194: * Sets the local name.
195: * @param localName the new local name
196: */
197: public void setLocalName(String localName) {
198: mLocalName = localName;
199: }
200:
201: /**
202: * Gets the namespace URI.
203: * @return the namespace URI
204: */
205: public String getNamespaceURI() {
206: return mNamespaceURI;
207: }
208:
209: /**
210: * Sets the namespace URI.
211: * @param namespaceURI the new namespace URI
212: */
213: public void setNamespaceURI(String namespaceURI) {
214: mNamespaceURI = namespaceURI;
215: }
216:
217: /**
218: * Gets the prefix.
219: * @return the prefix
220: */
221: public String getPrefix() {
222: return mPrefix;
223: }
224:
225: /**
226: * Sets the prefix.
227: * @param prefix the new prefix
228: */
229: public void setPrefix(String prefix) {
230: mPrefix = prefix;
231: }
232:
233: /**
234: * Returns the string representation of the QName. If the prefix is
235: * available, then it will be [prefix]:[localName], for example,
236: * tns:foo. If the prefix is not available and there is a namespace URI,
237: * then it will be {[namespaceURI]}[localName], for example,
238: * {http://schemas.xmlsoap.org/wsdl/}message. If neither the prefix not
239: * the namespace URI are present, then it will just be the local name.
240: * @return the QName's string representation
241: */
242: public String toString() {
243: String qName = (null == getLocalName()) ? "" : getLocalName();
244:
245: if (getPrefix() != null) {
246: return getPrefix() + ':' + qName;
247: } else if (getNamespaceURI() != null) {
248: return '{' + getNamespaceURI() + '}' + qName;
249: } else {
250: return qName;
251: }
252: }
253:
254: public boolean equals(Object src) {
255: if (!(src instanceof QName)) {
256: return false;
257: }
258:
259: QName srcQName = (QName) src;
260: return this .toString().equals(srcQName.toString());
261:
262: }
263:
264: public int hashCode() {
265: return this.toString().hashCode();
266: }
267: }
|