KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > tree > 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:06:06 per_nyfelt Exp $
8  */

9
10 package org.dom4j.tree;
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 org.dom4j.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 JavaDoc name) {
52         return getCache().get(name);
53     }
54
55     public static QName get(String JavaDoc name, Namespace namespace) {
56         return getCache().get(name, namespace);
57     }
58
59     public static QName get(String JavaDoc name, String JavaDoc prefix, String JavaDoc uri) {
60         return getCache().get(name, AbstractNamespace.get( prefix, uri ));
61     }
62
63     public static QName get(String JavaDoc qualifiedName, String JavaDoc uri) {
64         return getCache().get(qualifiedName, uri);
65     }
66
67     public static QName get(String JavaDoc localName, Namespace namespace, String JavaDoc qualifiedName) {
68         return getCache().get(localName, namespace, qualifiedName);
69     }
70
71     public DefaultQName(String JavaDoc name) {
72         this( name, AbstractNamespace.NO_NAMESPACE );
73     }
74
75     public DefaultQName(String JavaDoc name, Namespace namespace) {
76         this.name = (name == null) ? "" : name;
77         this.namespace = (namespace == null) ? AbstractNamespace.NO_NAMESPACE : namespace;
78     }
79
80     public DefaultQName(String JavaDoc name, Namespace namespace, String JavaDoc qualifiedName) {
81         this.name = (name == null) ? "" : name;
82         this.qualifiedName = qualifiedName;
83         this.namespace = (namespace == null) ? AbstractNamespace.NO_NAMESPACE : namespace;
84     }
85
86
87     /** @return the local name
88       */

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

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

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

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

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

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

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

185     public void setDocumentFactory(DocumentFactory documentFactory) {
186         this.nodeFactory = documentFactory;
187     }
188
189     public void setNodeFactory(NodeFactory nodeFactory) {
190         this.nodeFactory = nodeFactory;
191     }
192
193     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
194
195         // We use writeObject() and not writeUTF() to minimize space
196
// This allows for writing pointers to already written strings
197
out.writeObject(namespace.getPrefix());
198         out.writeObject(namespace.getURI());
199
200         out.defaultWriteObject();
201     }
202
203     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
204
205         String JavaDoc prefix = (String JavaDoc) in.readObject();
206         String JavaDoc uri = (String JavaDoc) in.readObject();
207
208         in.defaultReadObject();
209
210         namespace = AbstractNamespace.get( prefix, uri );
211     }
212
213
214     private static QNameCache getCache() {
215       QNameCache cache = (QNameCache) cachePerThread.get();
216       if (cache==null) {
217         cache = new QNameCache();
218         cachePerThread.set(cache);
219       }
220       return cache;
221     }
222
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:06:06 per_nyfelt Exp $
271  */

272
Popular Tags