KickJava   Java API By Example, From Geeks To Geeks.

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


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$
8  */

9
10 package org.ozoneDB.xml.dom4j.o3impl;
11
12 import org.dom4j.Namespace;
13 import org.ozoneDB.OzoneInterface;
14 import org.ozoneDB.OzoneObject;
15 import org.ozoneDB.UnexpectedException;
16 import org.ozoneDB.xml.dom4j.OzoneNamespaceCache;
17
18 import java.util.Collections JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 /** <p><code>NamespaceCacheImpl</code> caches instances of <code>AbstractNamespace</code>
23  * for reuse both across documents and within documents.</p>
24  *
25  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
26  * @author Per Nyfelt
27  * @version $Revision$
28  */

29 public class NamespaceCacheImpl extends OzoneObject implements OzoneNamespaceCache {
30
31     /** Cache of {@link Map} instances indexed by URI which contain caches
32      * of {@link org.dom4j.Namespace} impolementation instances for each prefix
33      */

34     protected static Map JavaDoc cache;
35
36     /** Cache of {@link org.dom4j.tree.AbstractNamespace} instances indexed by URI
37      * for default namespaces with no prefixes
38      */

39     protected static Map JavaDoc noPrefixCache;
40
41     public static Namespace XML_NAMESPACE;
42
43     public static Namespace NO_NAMESPACE;
44
45     /** A map of instances, one for each database */
46     private static transient Map JavaDoc instanceMap = new HashMap JavaDoc();
47
48     private static String JavaDoc OBJECT_NAME = NamespaceCacheImpl.class.getName();
49
50     public static synchronized OzoneNamespaceCache getInstance(OzoneInterface db) {
51         try {
52             OzoneNamespaceCache instance = (OzoneNamespaceCache) instanceMap.get(db);
53             if (instance == null) {
54                 System.out.println("looking for OzoneDocumentFactory instance (" + OBJECT_NAME + ") for database " + db);
55                 instance = (OzoneNamespaceCache) db.objectForName(OBJECT_NAME);
56                 if (instance == null) {
57                     System.out.println("creating new OzoneDocumentFactory");
58                     instance = NamespaceCacheImpl.create(db);
59                 }
60                 instanceMap.put(db, instance);
61             }
62             System.out.println("returning factory " + instance);
63             return instance;
64         } catch (Exception JavaDoc e) {
65             throw new UnexpectedException("failed to get NamespaceCacheImpl instance!", e);
66         }
67     }
68
69     private static OzoneNamespaceCache create(OzoneInterface db) {
70         return (OzoneNamespaceCache) db.createObject(NamespaceCacheImpl.class, OzoneInterface.Public, OBJECT_NAME);
71     }
72
73     public void onCreate() {
74         XML_NAMESPACE = get("xml", "http://www.w3.org/XML/1998/namespace");
75
76         /** No AbstractNamespace present */
77         NO_NAMESPACE = get("", "");
78
79     }
80
81     /** @return the name model for the given name and namepsace
82      */

83     public synchronized Namespace get(String JavaDoc prefix, String JavaDoc uri) {
84         Map JavaDoc cache = getURICache(uri);
85         Namespace answer = (Namespace) cache.get(prefix);
86         if (answer == null) {
87             answer = createNamespace(prefix, uri);
88             cache.put(prefix, answer);
89         }
90         return answer;
91     }
92
93
94     /** @return the name model for the given name and namepsace
95      */

96     public synchronized Namespace get(String JavaDoc uri) {
97         if (noPrefixCache == null) {
98             noPrefixCache = createURIMap();
99         }
100         Namespace answer = (Namespace) noPrefixCache.get(uri);
101         if (answer == null) {
102             answer = createNamespace("", uri);
103             noPrefixCache.put(uri, answer);
104         }
105         return answer;
106     }
107
108     public Namespace getXmlNamespace() {
109         return XML_NAMESPACE;
110     }
111
112     public Namespace getNoNamespace() {
113         return NO_NAMESPACE;
114     }
115
116
117     /** @return the cache for the given namespace URI. If one does not
118      * currently exist it is created.
119      */

120     protected synchronized Map JavaDoc getURICache(String JavaDoc uri) {
121         if (cache == null) {
122             cache = createURIMap();
123         }
124         Map JavaDoc answer = (Map JavaDoc) cache.get(uri);
125         if (answer == null) {
126             answer = createPrefixMap();
127             cache.put(uri, answer);
128         }
129         return answer;
130     }
131
132     /** A factory method to create {@link AbstractNamespace} instance
133      * @return a newly created {@link Namespace} instance.
134      */

135     protected Namespace createNamespace(String JavaDoc prefix, String JavaDoc uri) {
136         throw new RuntimeException JavaDoc("FIXME - should NamespaceCacheImpl become a real OzoneObject or whould db be passed?");
137         //return O3Namespace.create(db, prefix, uri);
138
}
139
140     /** A factory method to create prefix caches
141      * @return a newly created {@link Map} instance.
142      */

143     protected Map JavaDoc createPrefixMap() {
144         return Collections.synchronizedMap(new HashMap JavaDoc());
145     }
146
147     /** A factory method to create URI caches
148      * @return a newly created {@link Map} instance.
149      */

150     protected Map JavaDoc createURIMap() {
151         return Collections.synchronizedMap(new HashMap JavaDoc());
152     }
153 }
154
155
156 /*
157  * Redistribution and use of this software and associated documentation
158  * ("Software"), with or without modification, are permitted provided
159  * that the following conditions are met:
160  *
161  * 1. Redistributions of source code must retain copyright
162  * statements and notices. Redistributions must also contain a
163  * copy of this document.
164  *
165  * 2. Redistributions in binary form must reproduce the
166  * above copyright notice, this list of conditions and the
167  * following disclaimer in the documentation and/or other
168  * materials provided with the distribution.
169  *
170  * 3. The name "DOM4J" must not be used to endorse or promote
171  * products derived from this Software without prior written
172  * permission of MetaStuff, Ltd. For written permission,
173  * please contact dom4j-info@metastuff.com.
174  *
175  * 4. Products derived from this Software may not be called "DOM4J"
176  * nor may "DOM4J" appear in their names without prior written
177  * permission of MetaStuff, Ltd. DOM4J is a registered
178  * trademark of MetaStuff, Ltd.
179  *
180  * 5. Due credit should be given to the DOM4J Project
181  * (http://dom4j.org/).
182  *
183  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
184  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
185  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
186  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
187  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
188  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
189  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
190  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
191  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
192  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
193  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
194  * OF THE POSSIBILITY OF SUCH DAMAGE.
195  *
196  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
197  *
198  * $Id$
199  */

200
Popular Tags