KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > dom4j > o3impl > DefaultQName


1 /*
2  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: DefaultQName.java,v 1.1 2003/11/02 18:10:03 per_nyfelt Exp $
8  */

9
10 package org.ozoneDB.xml.dom4j.o3impl;
11
12
13 import org.dom4j.DocumentFactory;
14 import org.dom4j.Namespace;
15 import org.dom4j.NodeFactory;
16 import org.dom4j.QName;
17
18 import java.io.IOException JavaDoc;
19 import java.io.ObjectInputStream JavaDoc;
20 import java.io.ObjectOutputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22
23 /** <p><code>QName</code> represents a qualified name value of an XML element
24  * or attribute. It consists of a local name and a {@link Namespace}
25  * instance. This object is immutable.</p>
26  *
27  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
28  * @version $Revision: 1.1 $
29  */

30 public class DefaultQName implements Serializable JavaDoc, QName {
31
32     //protected transient static QNameCache cache = new QNameCache();
33
protected transient static ThreadLocal JavaDoc cachePerThread = new ThreadLocal JavaDoc();
34
35     /** The local name of the element or attribute */
36     private String JavaDoc name;
37
38     /** The qualified name of the element or attribute */
39     private String JavaDoc qualifiedName;
40
41     /** The Namespace of this element or attribute */
42     private transient Namespace namespace;
43
44     /** A cached version of the hashcode for efficiency */
45     private int hashCode;
46
47     /** The document factory used for this QName if specified or null */
48     private NodeFactory nodeFactory;
49
50
51 // public static QName get(String name) {
52
// return getCache().get(name);
53
// }
54
//
55
// public static QName get(String name, Namespace namespace) {
56
// return getCache().get(name, namespace);
57
// }
58
//
59
// public static QName get(String name, String prefix, String uri) {
60
// return getCache().get(name, AbstractNamespace.get(prefix, uri));
61
// }
62
//
63
// public static QName get(String qualifiedName, String uri) {
64
// return getCache().get(qualifiedName, uri);
65
// }
66
//
67
// public static QName get(String localName, Namespace namespace, String qualifiedName) {
68
// return getCache().get(localName, namespace, qualifiedName);
69
// }
70

71     public DefaultQName(String JavaDoc name, NodeFactory factory) {
72         this(name, factory.getNoNamespace(), factory);
73         setNodeFactory(factory);
74     }
75
76     public DefaultQName(String JavaDoc name, Namespace namespace, NodeFactory nodeFactory) {
77         this.name = (name == null) ? "" : name;
78         this.namespace = (namespace == null) ? nodeFactory.getNoNamespace() : namespace;
79         setNodeFactory(nodeFactory);
80     }
81
82     public DefaultQName(String JavaDoc name, Namespace namespace, String JavaDoc qualifiedName, NodeFactory nodeFactory) {
83         this.name = (name == null) ? "" : name;
84         this.qualifiedName = qualifiedName;
85         this.namespace = (namespace == null) ? nodeFactory.getNoNamespace() : namespace;
86         setNodeFactory(nodeFactory);
87     }
88
89
90     /** @return the local name
91      */

92     public String JavaDoc getName() {
93         return name;
94     }
95
96     /** @return the qualified name in the format <code>prefix:localName</code>
97      */

98     public String JavaDoc getQualifiedName() {
99         if (qualifiedName == null) {
100             String JavaDoc prefix = getNamespacePrefix();
101             if (prefix != null && prefix.length() > 0) {
102                 qualifiedName = prefix + ":" + name;
103             } else {
104                 qualifiedName = name;
105             }
106         }
107         return qualifiedName;
108     }
109
110     /** @return the namespace of this QName
111      */

112     public Namespace getNamespace() {
113         return namespace;
114     }
115
116     /** @return the namespace URI of this QName
117      */

118     public String JavaDoc getNamespacePrefix() {
119         if (namespace == null) {
120             return "";
121         }
122         return namespace.getPrefix();
123     }
124
125     /** @return the namespace URI of this QName
126      */

127     public String JavaDoc getNamespaceURI() {
128         if (namespace == null) {
129             return "";
130         }
131         return namespace.getURI();
132     }
133
134
135     /** @return the hash code based on the qualified name and the URI of the
136      * namespace.
137      */

138     public int hashCode() {
139         if (hashCode == 0) {
140             hashCode = getName().hashCode()
141                     ^ getNamespaceURI().hashCode();
142             if (hashCode == 0) {
143                 hashCode = 0xbabe;
144             }
145         }
146         return hashCode;
147     }
148
149     public boolean equals(Object JavaDoc object) {
150         if (this == object) {
151             return true;
152         } else if (object instanceof QName) {
153             QName that = (QName) object;
154             // we cache hash codes so this should be quick
155
if (hashCode() == that.hashCode()) {
156                 return getName().equals(that.getName())
157                         && getNamespaceURI().equals(that.getNamespaceURI());
158             }
159         }
160         return false;
161     }
162
163     public String JavaDoc toString() {
164         return super.toString() + " [name: " + getName()
165                 + " namespace: \"" + getNamespace() + "\"]";
166     }
167
168     /** @return the factory that should be used for Elements of this QName
169      * @deprecated Use getNodeFactory() instead
170      */

171     public DocumentFactory getDocumentFactory() {
172         if (getNodeFactory() instanceof DocumentFactory) {
173             return (DocumentFactory) nodeFactory;
174         } else {
175             return null;
176         }
177     }
178
179     public NodeFactory getNodeFactory() {
180         return nodeFactory;
181     }
182
183     /**
184      * @deprecated Use setNodeFactory(NodeFactory) instead
185      */

186     public void setDocumentFactory(DocumentFactory documentFactory) {
187         this.nodeFactory = documentFactory;
188     }
189
190     public void setNodeFactory(NodeFactory nodeFactory) {
191         this.nodeFactory = nodeFactory;
192     }
193
194     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
195
196         // We use writeObject() and not writeUTF() to minimize space
197
// This allows for writing pointers to already written strings
198
out.writeObject(namespace.getPrefix());
199         out.writeObject(namespace.getURI());
200
201         out.defaultWriteObject();
202     }
203
204     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
205
206         String JavaDoc prefix = (String JavaDoc) in.readObject();
207         String JavaDoc uri = (String JavaDoc) in.readObject();
208
209         in.defaultReadObject();
210
211         // fixme: this will probably result in an NPE
212
namespace = nodeFactory.createNamespace(prefix, uri);
213     }
214
215
216     private static QNameCache getCache() {
217         QNameCache cache = (QNameCache) cachePerThread.get();
218         if (cache == null) {
219             cache = new QNameCache();
220             cachePerThread.set(cache);
221         }
222         return cache;
223     }
224
225 }
226
227
228 /*
229  * Redistribution and use of this software and associated documentation
230  * ("Software"), with or without modification, are permitted provided
231  * that the following conditions are met:
232  *
233  * 1. Redistributions of source code must retain copyright
234  * statements and notices. Redistributions must also contain a
235  * copy of this document.
236  *
237  * 2. Redistributions in binary form must reproduce the
238  * above copyright notice, this list of conditions and the
239  * following disclaimer in the documentation and/or other
240  * materials provided with the distribution.
241  *
242  * 3. The name "DOM4J" must not be used to endorse or promote
243  * products derived from this Software without prior written
244  * permission of MetaStuff, Ltd. For written permission,
245  * please contact dom4j-info@metastuff.com.
246  *
247  * 4. Products derived from this Software may not be called "DOM4J"
248  * nor may "DOM4J" appear in their names without prior written
249  * permission of MetaStuff, Ltd. DOM4J is a registered
250  * trademark of MetaStuff, Ltd.
251  *
252  * 5. Due credit should be given to the DOM4J Project
253  * (http://dom4j.org/).
254  *
255  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
256  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
257  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
258  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
259  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
260  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
261  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
262  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
263  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
264  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
265  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
266  * OF THE POSSIBILITY OF SUCH DAMAGE.
267  *
268  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
269  *
270  * $Id: DefaultQName.java,v 1.1 2003/11/02 18:10:03 per_nyfelt Exp $
271  */

272
Popular Tags