KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml > QName


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xml;
30
31 import com.caucho.util.L10N;
32
33 import org.w3c.dom.DOMException JavaDoc;
34
35 import java.io.Serializable JavaDoc;
36
37 public class QName implements Comparable JavaDoc, Serializable JavaDoc {
38   protected static L10N L = new L10N(QName.class);
39   
40   private String JavaDoc _prefix; // preferred prefix
41
private String JavaDoc _localName; // the real name
42
private String JavaDoc _namespace; // URL
43

44   private String JavaDoc _fullName; // foo:bar
45
private String JavaDoc _canonicalName; // http://www.w3.org?bar
46

47   public QName(String JavaDoc qName)
48   {
49     this(qName, "");
50   }
51   
52   public QName(String JavaDoc qName, String JavaDoc namespace)
53   {
54     _fullName = qName.intern();
55
56     if (namespace == null) {
57       _prefix = null;
58       _namespace = null;
59       _localName = null;
60     }
61     else if (namespace.equals("")) {
62       _prefix = null;
63       _namespace = "";
64       _localName = _fullName;
65     }
66     else {
67       _namespace = namespace.intern();
68       
69       int p = qName.indexOf(':');
70       if (p > 0) {
71     _prefix = qName.substring(0, p).intern();
72     _localName = qName.substring(p + 1).intern();
73       }
74       else {
75     _prefix = null;
76     _localName = _fullName;
77       }
78     }
79     
80     if (_namespace != null)
81       _canonicalName = ("{" + _namespace + "}" + _localName).intern();
82     else
83       _canonicalName = _fullName;
84   }
85     
86   public QName(String JavaDoc prefix, String JavaDoc localName, String JavaDoc namespace)
87   {
88     init(prefix, localName, namespace);
89   }
90     
91   public QName(String JavaDoc qName, String JavaDoc prefix, String JavaDoc localName, String JavaDoc namespace)
92   {
93     _fullName = qName.intern();
94     
95     if (prefix != null)
96       _prefix = prefix.intern();
97     
98     if (localName != null)
99       _localName = localName.intern();
100     
101     if (namespace != null)
102       _namespace = namespace.intern();
103     
104     if (_namespace != null)
105       _canonicalName = ("{" + _namespace + "}" + _localName).intern();
106     else
107       _canonicalName = _fullName;
108   }
109
110   private void init(String JavaDoc prefix, String JavaDoc localName, String JavaDoc namespace)
111   {
112     if (localName == null || localName.equals(""))
113       throw new QDOMException(DOMException.INVALID_CHARACTER_ERR, L.l("`{0}' is an invalid XML name because the local name is empty. XML names must be `prefix:name' or simply `name'.", prefix + ":"));
114
115     if (prefix == null || prefix.equals(""))
116       _prefix = null;
117     else
118       _prefix = prefix.intern();
119
120     _localName = localName.intern();
121
122     if (_prefix != null && _prefix != "")
123       _fullName = (_prefix + ":" + localName).intern();
124     else
125       _fullName = _localName;
126
127     if (namespace == null) {
128       _localName = null;
129     }
130     else if (namespace.equals("")) {
131       _namespace = "";
132       _localName = _fullName;
133     }
134     else
135       _namespace = namespace.intern();
136
137     if (_namespace != null)
138       _canonicalName = ("{" + _namespace + "}" + localName).intern();
139     else
140       _canonicalName = _fullName;
141   }
142
143   public String JavaDoc getName()
144   {
145     return _fullName;
146   }
147
148   public String JavaDoc getPrefix()
149   {
150     return _prefix;
151   }
152
153   public String JavaDoc getLocalName()
154   {
155     return _localName;
156   }
157
158   public String JavaDoc getCanonicalName()
159   {
160     return _canonicalName;
161   }
162
163   public String JavaDoc getNamespace()
164   {
165     return _namespace;
166   }
167
168   public String JavaDoc getNamespaceURI()
169   {
170     return _namespace;
171   }
172
173   /**
174    * Returns the hashcode of the qname.
175    */

176   public int hashCode()
177   {
178     return _canonicalName.hashCode();
179     /*
180     if (_namespace != null)
181       return _localName.hashCode() * 65521 + _namespace.hashCode();
182     else
183       return _localName.hashCode() * 65521;
184     */

185   }
186
187   /**
188    * Returns true if the two qnames are equivalent.
189    */

190   public boolean equals(Object JavaDoc b)
191   {
192     if (this == b)
193       return true;
194         
195     if (! (b instanceof QName))
196       return false;
197
198     QName name = (QName) b;
199
200     return _canonicalName == name._canonicalName;
201     /*
202     if (_localName != name._localName)
203       return false;
204     
205     if (_namespace != name._namespace)
206       return false;
207
208     return true;
209
210     if (_namespace != "")
211       return true;
212     else
213       return _prefix == bName._prefix;
214     */

215   }
216
217   public int compareTo(Object JavaDoc b)
218   {
219     if (this == b)
220       return 0;
221
222     else if (! (b instanceof QName))
223       return -1;
224
225     QName name = (QName) b;
226
227     return getCanonicalName().compareTo(name.getCanonicalName());
228     /*
229     int cmp = getName().compareTo(name.getName());
230
231     if (cmp != 0)
232       return cmp;
233     else if (_namespace == null)
234       return name._namespace == null ? 0 : -1;
235     else if (name._namespace == null)
236       return 1;
237     else
238       return _namespace.compareTo(name._namespace);
239     */

240   }
241
242   public String JavaDoc toString()
243   {
244     if (_prefix != null)
245       return "QName[" + _prefix + ":" + _canonicalName + "]";
246     else
247       return "QName[" + _canonicalName + "]";
248   }
249 }
250
Popular Tags