KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > xni > QName


1 /*
2  * Copyright 2000-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.xni;
18
19 /**
20  * A structure that holds the components of an XML Namespaces qualified
21  * name.
22  * <p>
23  * To be used correctly, the strings must be identical references for
24  * equal strings. Within the parser, these values are considered symbols
25  * and should always be retrieved from the <code>SymbolTable</code>.
26  *
27  * @see <a HREF="../../../../../xerces2/org/apache/xerces/util/SymbolTable.html">org.apache.xerces.util.SymbolTable</a>
28  *
29  * @author Andy Clark, IBM
30  *
31  * @version $Id: QName.java,v 1.8 2004/10/03 21:53:25 mrglavas Exp $
32  */

33 public class QName
34     implements Cloneable JavaDoc {
35
36     //
37
// Data
38
//
39

40     /**
41      * The qname prefix. For example, the prefix for the qname "a:foo"
42      * is "a".
43      */

44     public String JavaDoc prefix;
45
46     /**
47      * The qname localpart. For example, the localpart for the qname "a:foo"
48      * is "foo".
49      */

50     public String JavaDoc localpart;
51
52     /**
53      * The qname rawname. For example, the rawname for the qname "a:foo"
54      * is "a:foo".
55      */

56     public String JavaDoc rawname;
57
58     /**
59      * The URI to which the qname prefix is bound. This binding must be
60      * performed by a XML Namespaces aware processor.
61      */

62     public String JavaDoc uri;
63
64     //
65
// Constructors
66
//
67

68     /** Default constructor. */
69     public QName() {
70         clear();
71     } // <init>()
72

73     /** Constructs a QName with the specified values. */
74     public QName(String JavaDoc prefix, String JavaDoc localpart, String JavaDoc rawname, String JavaDoc uri) {
75         setValues(prefix, localpart, rawname, uri);
76     } // <init>(String,String,String,String)
77

78     /** Constructs a copy of the specified QName. */
79     public QName(QName qname) {
80         setValues(qname);
81     } // <init>(QName)
82

83     //
84
// Public methods
85
//
86

87     /**
88      * Convenience method to set the values of the qname components.
89      *
90      * @param qname The qualified name to be copied.
91      */

92     public void setValues(QName qname) {
93         prefix = qname.prefix;
94         localpart = qname.localpart;
95         rawname = qname.rawname;
96         uri = qname.uri;
97     } // setValues(QName)
98

99     /**
100      * Convenience method to set the values of the qname components.
101      *
102      * @param prefix The qname prefix. (e.g. "a")
103      * @param localpart The qname localpart. (e.g. "foo")
104      * @param rawname The qname rawname. (e.g. "a:foo")
105      * @param uri The URI binding. (e.g. "http://foo.com/mybinding")
106      */

107     public void setValues(String JavaDoc prefix, String JavaDoc localpart, String JavaDoc rawname,
108                           String JavaDoc uri) {
109         this.prefix = prefix;
110         this.localpart = localpart;
111         this.rawname = rawname;
112         this.uri = uri;
113     } // setValues(String,String,String,String)
114

115     /** Clears the values of the qname components. */
116     public void clear() {
117         prefix = null;
118         localpart = null;
119         rawname = null;
120         uri = null;
121     } // clear()
122

123     //
124
// Cloneable methods
125
//
126

127     /** Returns a clone of this object. */
128     public Object JavaDoc clone() {
129         return new QName(this);
130     } // clone():Object
131

132     //
133
// Object methods
134
//
135

136     /** Returns the hashcode for this object. */
137     public int hashCode() {
138         if (uri != null) {
139             return uri.hashCode() +
140                 ((localpart != null) ? localpart.hashCode() : 0);
141         }
142         return (rawname != null) ? rawname.hashCode() : 0;
143     } // hashCode():int
144

145     /** Returns true if the two objects are equal. */
146     public boolean equals(Object JavaDoc object) {
147         if (object instanceof QName) {
148             QName qname = (QName)object;
149             if (qname.uri != null) {
150                 return uri == qname.uri && localpart == qname.localpart;
151             }
152             else if (uri == null) {
153                 return rawname == qname.rawname;
154             }
155             // fall through and return not equal
156
}
157         return false;
158     } // equals(Object):boolean
159

160     /** Returns a string representation of this object. */
161     public String JavaDoc toString() {
162
163         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
164         boolean comma = false;
165         if (prefix != null) {
166             str.append("prefix=\""+prefix+'"');
167             comma = true;
168         }
169         if (localpart != null) {
170             if (comma) {
171                 str.append(',');
172             }
173             str.append("localpart=\""+localpart+'"');
174             comma = true;
175         }
176         if (rawname != null) {
177             if (comma) {
178                 str.append(',');
179             }
180             str.append("rawname=\""+rawname+'"');
181             comma = true;
182         }
183         if (uri != null) {
184             if (comma) {
185                 str.append(',');
186             }
187             str.append("uri=\""+uri+'"');
188         }
189         return str.toString();
190
191     } // toString():String
192

193 } // class QName
194
Popular Tags