1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements.  See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22package com.sun.org.apache.xerces.internal.xni;
23
24/**
25 * A structure that holds the components of an XML Namespaces qualified
26 * name.
27 * <p>
28 * To be used correctly, the strings must be identical references for
29 * equal strings. Within the parser, these values are considered symbols
30 * and should always be retrieved from the <code>SymbolTable</code>.
31 *
32 * @see <a href="../../../../../xerces2/com/sun/org/apache/xerces/internal/util/SymbolTable.html">com.sun.org.apache.xerces.internal.util.SymbolTable</a>
33 *
34 * @author Andy Clark, IBM
35 *
36 * Better performance patch for the equals method by Daniel Petersson: refer to jaxp issue 61;
37 * == were used to compare strings
38 * @author Joe Wang, Oracle
39 *
40 */
41public class QName
42implements Cloneable {
43
44
45    /**
46     * The qname prefix. For example, the prefix for the qname "a:foo"
47     * is "a".
48     */
49    public String prefix;
50
51    /**
52     * The qname localpart. For example, the localpart for the qname "a:foo"
53     * is "foo".
54     */
55    public String localpart;
56
57    /**
58     * The qname rawname. For example, the rawname for the qname "a:foo"
59     * is "a:foo".
60     */
61    public String rawname;
62
63    /**
64     * The URI to which the qname prefix is bound. This binding must be
65     * performed by a XML Namespaces aware processor.
66     */
67    public String uri;
68
69    //
70    // Constructors
71    //
72
73    /** Default constructor. */
74    public QName() {
75        clear();
76    } // <init>()
77
78    /** Constructs a QName with the specified values. */
79    public QName(String prefix, String localpart, String rawname, String uri) {
80        setValues(prefix, localpart, rawname, uri);
81    } // <init>(String,String,String,String)
82
83    /** Constructs a copy of the specified QName. */
84    public QName(QName qname) {
85        setValues(qname);
86    } // <init>(QName)
87
88    //
89    // Public methods
90    //
91
92    /**
93     * Convenience method to set the values of the qname components.
94     *
95     * @param QName The qualified name to be copied.
96     */
97    public void setValues(QName qname) {
98        prefix = qname.prefix;
99        localpart = qname.localpart;
100        rawname = qname.rawname;
101        uri = qname.uri;
102    } // setValues(QName)
103
104    /**
105     * Convenience method to set the values of the qname components.
106     *
107     * @param prefix    The qname prefix. (e.g. "a")
108     * @param localpart The qname localpart. (e.g. "foo")
109     * @param rawname   The qname rawname. (e.g. "a:foo")
110     * @param uri       The URI binding. (e.g. "http://foo.com/mybinding")
111     */
112    public void setValues(String prefix, String localpart, String rawname,
113    String uri) {
114        this.prefix = prefix;
115        this.localpart = localpart;
116        this.rawname = rawname;
117        this.uri = uri;
118    } // setValues(String,String,String,String)
119
120    /** Clears the values of the qname components. */
121    public void clear() {
122        prefix = null;
123        localpart = null;
124        rawname = null;
125        uri = null;
126    } // clear()
127
128    //
129    // Cloneable methods
130    //
131
132    /** Returns a clone of this object. */
133    public Object clone() {
134        return new QName(this);
135    } // clone():Object
136
137    //
138    // Object methods
139    //
140
141    /** Returns the hashcode for this object. */
142    public int hashCode() {
143        if (uri != null) {
144            return uri.hashCode() +
145                ((localpart != null) ? localpart.hashCode() : 0);
146        }
147        return (rawname != null) ? rawname.hashCode() : 0;
148    } // hashCode():int
149
150    /** Returns true if the two objects are equal. */
151    public boolean equals(Object object) {
152        if (object == this) {
153            return true;
154        }
155
156        if (object != null && object instanceof QName) {
157            QName qname = (QName)object;
158            if (qname.uri != null) {
159                    return qname.localpart.equals(localpart) && qname.uri.equals(uri);
160            }
161            else if (uri == null) {
162                return rawname.equals(qname.rawname);
163            }
164            // fall through and return not equal
165        }
166        return false;
167    } // equals(Object):boolean
168
169    /** Returns a string representation of this object. */
170    public String toString() {
171
172        StringBuffer str = new StringBuffer();
173        boolean comma = false;
174        if (prefix != null) {
175            str.append("prefix=\""+prefix+'"');
176            comma = true;
177        }
178        if (localpart != null) {
179            if (comma) {
180                str.append(',');
181            }
182            str.append("localpart=\""+localpart+'"');
183            comma = true;
184        }
185        if (rawname != null) {
186            if (comma) {
187                str.append(',');
188            }
189            str.append("rawname=\""+rawname+'"');
190            comma = true;
191        }
192        if (uri != null) {
193            if (comma) {
194                str.append(',');
195            }
196            str.append("uri=\""+uri+'"');
197        }
198        return str.toString();
199
200    } // toString():String
201
202} // class QName
203