KickJava   Java API By Example, From Geeks To Geeks.

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


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: QNameCache.java,v 1.1 2003/11/02 18:10:03 per_nyfelt Exp $
8  */

9
10 package org.ozoneDB.xml.dom4j.o3impl;
11
12 import org.dom4j.DocumentFactory;
13 import org.dom4j.Namespace;
14 import org.dom4j.NodeFactory;
15 import org.dom4j.QName;
16
17 import java.util.*;
18
19 /** <p><code>QNameCache</code> caches instances of <code>QName</code>
20  * for reuse both across documents and within documents.</p>
21  *
22  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
23  * @version $Revision: 1.1 $
24  */

25 public class QNameCache {
26
27     /** Cache of {@link org.dom4j.tree.DefaultQName} instances with no namespace */
28     protected Map noNamespaceCache = Collections.synchronizedMap(new HashMap());
29
30     /** Cache of {@link Map} instances indexed by namespace which contain
31      * caches of {@link org.dom4j.tree.DefaultQName} for each name
32      */

33     protected Map namespaceCache = Collections.synchronizedMap(new HashMap());
34
35     /** The document factory associated with new QNames instances in this cache
36      * or null if no instances should be associated by default
37      */

38     private NodeFactory nodeFactory;
39
40
41     public QNameCache() {
42     }
43
44     /**
45      * deprecated Use QNameCache(NodeFactory) instead.
46      */

47     public QNameCache(DocumentFactory documentFactory) {
48         this.nodeFactory = documentFactory;
49     }
50
51     public QNameCache(NodeFactory nodeFactory) {
52         this.nodeFactory = nodeFactory;
53     }
54
55     /** Returns a list of all the QName instances currently used
56      */

57     public List getQNames() {
58         List answer = new ArrayList();
59         answer.addAll(noNamespaceCache.values());
60         for (Iterator iter = namespaceCache.values().iterator(); iter.hasNext();) {
61             Map map = (Map) iter.next();
62             answer.addAll(map.values());
63         }
64         return answer;
65     }
66
67     /** @return the QName for the given name and no namepsace
68      */

69     public QName get(String JavaDoc name) {
70         QName answer = null;
71         if (name != null) {
72             answer = (QName) noNamespaceCache.get(name);
73         } else {
74             name = "";
75         }
76         if (answer == null) {
77             answer = createQName(name);
78             answer.setNodeFactory(nodeFactory);
79             noNamespaceCache.put(name, answer);
80         }
81         return answer;
82     }
83
84     /** @return the QName for the given local name and namepsace
85      */

86     public QName get(String JavaDoc name, Namespace namespace) {
87         Map cache = getNamespaceCache(namespace);
88         QName answer = null;
89         if (name != null) {
90             answer = (QName) cache.get(name);
91         } else {
92             name = "";
93         }
94         if (answer == null) {
95             answer = createQName(name, namespace);
96             answer.setNodeFactory(nodeFactory);
97             cache.put(name, answer);
98         }
99         return answer;
100     }
101
102
103     /** @return the QName for the given local name, qualified name and namepsace
104      */

105     public QName get(String JavaDoc localName, Namespace namespace, String JavaDoc qualifiedName) {
106         Map cache = getNamespaceCache(namespace);
107         QName answer = null;
108         if (localName != null) {
109             answer = (QName) cache.get(localName);
110         } else {
111             localName = "";
112         }
113
114         if (answer == null) {
115             answer = createQName(localName, namespace, qualifiedName);
116             answer.setNodeFactory(nodeFactory);
117             cache.put(localName, answer);
118         }
119         return answer;
120     }
121
122
123     public QName get(String JavaDoc qualifiedName, String JavaDoc uri) {
124         int index = qualifiedName.indexOf(':');
125         if (index < 0) {
126             return get(qualifiedName, nodeFactory.getNamespace(uri));
127         } else {
128             String JavaDoc name = qualifiedName.substring(index + 1);
129             String JavaDoc prefix = qualifiedName.substring(0, index);
130             return get(name, nodeFactory.createNamespace(prefix, uri));
131         }
132     }
133
134
135     /** @return the cached QName instance if there is one or adds the given
136      * qname to the cache if not
137      */

138     public QName intern(QName qname) {
139         return get(qname.getName(), qname.getNamespace(), qname.getQualifiedName());
140     }
141
142     /** @return the cache for the given namespace. If one does not
143      * currently exist it is created.
144      */

145     protected Map getNamespaceCache(Namespace namespace) {
146         if (namespace.equals(nodeFactory.getNoNamespace())) {
147             return noNamespaceCache;
148         }
149         Map answer = null;
150         if (namespace != null) {
151             answer = (Map) namespaceCache.get(namespace);
152         }
153         if (answer == null) {
154             answer = createMap();
155             namespaceCache.put(namespace, answer);
156         }
157         return answer;
158     }
159
160     /** A factory method
161      * @return a newly created {@link Map} instance.
162      */

163     protected Map createMap() {
164         return Collections.synchronizedMap(new HashMap());
165     }
166
167     /** Factory method to create a new QName object
168      * which can be overloaded to create derived QName instances
169      */

170     protected QName createQName(String JavaDoc name) {
171         return new DefaultQName(name, nodeFactory);
172     }
173
174     /** Factory method to create a new QName object
175      * which can be overloaded to create derived QName instances
176      */

177     protected QName createQName(String JavaDoc name, Namespace namespace) {
178         return new DefaultQName(name, namespace, nodeFactory);
179     }
180
181     /** Factory method to create a new QName object
182      * which can be overloaded to create derived QName instances
183      */

184     protected QName createQName(String JavaDoc name, Namespace namespace, String JavaDoc qualifiedName) {
185         return new DefaultQName(name, namespace, qualifiedName, nodeFactory);
186     }
187 }
188
189
190 /*
191  * Redistribution and use of this software and associated documentation
192  * ("Software"), with or without modification, are permitted provided
193  * that the following conditions are met:
194  *
195  * 1. Redistributions of source code must retain copyright
196  * statements and notices. Redistributions must also contain a
197  * copy of this document.
198  *
199  * 2. Redistributions in binary form must reproduce the
200  * above copyright notice, this list of conditions and the
201  * following disclaimer in the documentation and/or other
202  * materials provided with the distribution.
203  *
204  * 3. The name "DOM4J" must not be used to endorse or promote
205  * products derived from this Software without prior written
206  * permission of MetaStuff, Ltd. For written permission,
207  * please contact dom4j-info@metastuff.com.
208  *
209  * 4. Products derived from this Software may not be called "DOM4J"
210  * nor may "DOM4J" appear in their names without prior written
211  * permission of MetaStuff, Ltd. DOM4J is a registered
212  * trademark of MetaStuff, Ltd.
213  *
214  * 5. Due credit should be given to the DOM4J Project
215  * (http://dom4j.org/).
216  *
217  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
218  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
219  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
220  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
221  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
222  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
223  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
224  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
225  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
226  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
227  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
228  * OF THE POSSIBILITY OF SUCH DAMAGE.
229  *
230  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
231  *
232  * $Id: QNameCache.java,v 1.1 2003/11/02 18:10:03 per_nyfelt Exp $
233  */

234
Popular Tags