KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > schema2beans > 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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.schema2beans;
21
22 /**
23  * @author cliffwd
24  * A QName class for dealing with XML Qualified Names; basically,
25  * a namespace and a localpart.
26  *
27  * This class is intended solely for those who for some reason can't
28  * use javax.xml.namespace.QName. See that class for documentation.
29  * Remember that prefix is not part of equals or hashCode.
30  */

31 public class QName {
32     private String JavaDoc namespaceURI;
33     private String JavaDoc localPart;
34     private String JavaDoc prefix;
35
36     public QName(String JavaDoc localPart) {
37         this("", localPart, "");
38     }
39
40     public QName(String JavaDoc namespaceURI, String JavaDoc localPart) {
41         this(namespaceURI, localPart, "");
42     }
43
44     public QName(String JavaDoc namespaceURI, String JavaDoc localPart, String JavaDoc prefix) {
45         if (namespaceURI == null)
46             namespaceURI = "";
47         this.namespaceURI = namespaceURI;
48         if (localPart == null)
49             throw new IllegalArgumentException JavaDoc("localPart == null");
50         this.localPart = localPart;
51         this.prefix = prefix;
52     }
53
54     public String JavaDoc getNamespaceURI() {
55         return namespaceURI;
56     }
57
58     public String JavaDoc getLocalPart() {
59         return localPart;
60     }
61
62     public String JavaDoc getPrefix() {
63         return prefix;
64     }
65
66     public boolean equals(Object JavaDoc o) {
67         if (o == this)
68             return true;
69         if (!(o instanceof QName))
70             return false;
71         QName q = (QName) o;
72         if (!namespaceURI.equals(q.namespaceURI))
73             return false;
74         if (!localPart.equals(q.localPart))
75             return false;
76         return true;
77     }
78
79     public int hashCode() {
80         int result = 17;
81         result = 37*result + namespaceURI.hashCode();
82         result = 37*result + localPart.hashCode();
83         return result;
84     }
85
86     public String JavaDoc toString() {
87         if ("".equals(namespaceURI))
88             return localPart;
89         else
90             return "{"+namespaceURI+"}"+localPart;
91     }
92
93     public static QName valueOf(String JavaDoc asString) {
94         int pos = asString.indexOf('}');
95         if (pos < 0) {
96             return new QName(asString);
97         } else {
98             String JavaDoc ns = asString.substring(1, pos-1);
99             String JavaDoc localPart = asString.substring(pos+1, asString.length());
100             return new QName(ns, localPart);
101         }
102     }
103 }
104
Popular Tags