KickJava   Java API By Example, From Geeks To Geeks.

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


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: NamespaceCache.java,v 1.4 2003/12/06 21:34:04 per_nyfelt Exp $
8  */

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

24 public class NamespaceCache {
25
26     /** Cache of {@link Map} instances indexed by URI which contain
27      * caches of {@link org.dom4j.tree.AbstractNamespace} for each prefix
28      */

29     protected static Map JavaDoc cache;
30
31     /** Cache of {@link org.dom4j.tree.AbstractNamespace} instances indexed by URI
32      * for default namespaces with no prefixes
33      */

34     protected static Map JavaDoc noPrefixCache;
35
36
37     /** @return the name model for the given name and namepsace
38      */

39     public synchronized Namespace get(String JavaDoc prefix, String JavaDoc uri) {
40         Map JavaDoc cache = getURICache(uri);
41         Namespace answer = (Namespace) cache.get(prefix);
42         if (answer == null) {
43             answer = createNamespace(prefix, uri);
44             cache.put(prefix, answer);
45         }
46         return answer;
47     }
48
49
50     /** @return the name model for the given name and namepsace
51      */

52     public synchronized Namespace get(String JavaDoc uri) {
53         if (noPrefixCache == null) {
54             noPrefixCache = createURIMap();
55         }
56         Namespace answer = (Namespace) noPrefixCache.get(uri);
57         if (answer == null) {
58             answer = createNamespace("", uri);
59             noPrefixCache.put(uri, answer);
60         }
61         return answer;
62     }
63
64
65     /** @return the cache for the given namespace URI. If one does not
66      * currently exist it is created.
67      */

68     protected synchronized Map JavaDoc getURICache(String JavaDoc uri) {
69         if (cache == null) {
70             cache = createURIMap();
71         }
72         Map JavaDoc answer = (Map JavaDoc) cache.get(uri);
73         if (answer == null) {
74             answer = createPrefixMap();
75             cache.put(uri, answer);
76         }
77         return answer;
78     }
79
80     /** A factory method to create {@link AbstractNamespace} instance
81      * @return a newly created {@link AbstractNamespace} instance.
82      */

83     protected Namespace createNamespace(String JavaDoc prefix, String JavaDoc uri) {
84         throw new RuntimeException JavaDoc("FIXME - should NamespaceCache become a real OzoneObject or whould db be passed?");
85         //return O3Namespace.create(db, prefix, uri);
86
}
87
88     /** A factory method to create prefix caches
89      * @return a newly created {@link Map} instance.
90      */

91     protected Map JavaDoc createPrefixMap() {
92         return Collections.synchronizedMap(new HashMap JavaDoc());
93     }
94
95     /** A factory method to create URI caches
96      * @return a newly created {@link Map} instance.
97      */

98     protected Map JavaDoc createURIMap() {
99         return Collections.synchronizedMap(new HashMap JavaDoc());
100     }
101 }
102
103
104 /*
105  * Redistribution and use of this software and associated documentation
106  * ("Software"), with or without modification, are permitted provided
107  * that the following conditions are met:
108  *
109  * 1. Redistributions of source code must retain copyright
110  * statements and notices. Redistributions must also contain a
111  * copy of this document.
112  *
113  * 2. Redistributions in binary form must reproduce the
114  * above copyright notice, this list of conditions and the
115  * following disclaimer in the documentation and/or other
116  * materials provided with the distribution.
117  *
118  * 3. The name "DOM4J" must not be used to endorse or promote
119  * products derived from this Software without prior written
120  * permission of MetaStuff, Ltd. For written permission,
121  * please contact dom4j-info@metastuff.com.
122  *
123  * 4. Products derived from this Software may not be called "DOM4J"
124  * nor may "DOM4J" appear in their names without prior written
125  * permission of MetaStuff, Ltd. DOM4J is a registered
126  * trademark of MetaStuff, Ltd.
127  *
128  * 5. Due credit should be given to the DOM4J Project
129  * (http://dom4j.org/).
130  *
131  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
132  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
133  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
134  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
135  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
136  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
137  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
138  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
139  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
140  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
141  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
142  * OF THE POSSIBILITY OF SUCH DAMAGE.
143  *
144  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
145  *
146  * $Id: NamespaceCache.java,v 1.4 2003/12/06 21:34:04 per_nyfelt Exp $
147  */

148
Popular Tags