1
2
3
4
5
6
7
8
9
10 package org.dom4j.tree;
11
12 import java.lang.reflect.Constructor;
13 import java.util.Map;
14
15 import org.dom4j.Namespace;
16
17 /***
18 * <p><code>NamespaceCache</code> caches instances of <code>DefaultNamespace</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 * @author Maarten Coene
23 * @author Brett Finnell
24 * @version $Revision: 1.11 $
25 */
26 public class NamespaceCache {
27
28 /*** Cache of {@link Map} instances indexed by URI which contain
29 * caches of {@link Namespace} for each prefix
30 */
31 protected static Map cache;
32
33 /*** Cache of {@link Namespace} instances indexed by URI
34 * for default namespaces with no prefixes
35 */
36 protected static Map noPrefixCache;
37
38 static {
39
40 try {
41 Class clazz = Class.forName("java.util.concurrent.ConcurrentHashMap");
42 Constructor construct = clazz.getConstructor(new Class[] {Integer.TYPE, Float.TYPE, Integer.TYPE});
43 cache = (Map) construct.newInstance(new Object[] {new Integer(11), new Float(0.75), new Integer(1)});
44 noPrefixCache = (Map) construct.newInstance(new Object[] {new Integer(11), new Float(0.75), new Integer(1)});
45 } catch (Exception exc1) {
46
47 try {
48 Class clazz = Class.forName("EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap");
49 cache = (Map) clazz.newInstance();
50 noPrefixCache = (Map) clazz.newInstance();
51 } catch (Exception exc2) {
52
53 cache = new ConcurrentReaderHashMap();
54 noPrefixCache = new ConcurrentReaderHashMap();
55 }
56 }
57
58 }
59
60
61 /*** @return the name model for the given name and namepsace
62 */
63 public Namespace get(String prefix, String uri) {
64 Map cache = getURICache(uri);
65 Namespace answer = (Namespace) cache.get(prefix);
66 if (answer == null) {
67 synchronized (cache) {
68 answer = (Namespace) cache.get(prefix);
69 if (answer == null) {
70 answer = createNamespace(prefix, uri);
71 cache.put(prefix, answer);
72 }
73 }
74 }
75 return answer;
76 }
77
78
79 /*** @return the name model for the given name and namepsace
80 */
81 public Namespace get(String uri) {
82 Namespace answer = (Namespace) noPrefixCache.get(uri);
83 if (answer == null) {
84 synchronized (noPrefixCache) {
85 answer = (Namespace) noPrefixCache.get(uri);
86 if (answer == null) {
87 answer = createNamespace("", uri);
88 noPrefixCache.put(uri, answer);
89 }
90 }
91 }
92 return answer;
93 }
94
95
96 /*** @return the cache for the given namespace URI. If one does not
97 * currently exist it is created.
98 */
99 protected Map getURICache(String uri) {
100 Map answer = (Map) cache.get(uri);
101 if (answer == null) {
102 synchronized (cache) {
103 answer = (Map) cache.get(uri);
104 if (answer == null) {
105 answer = new ConcurrentReaderHashMap();
106 cache.put(uri, answer);
107 }
108 }
109 }
110 return answer;
111 }
112
113 /*** A factory method to create {@link Namespace} instance
114 * @return a newly created {@link Namespace} instance.
115 */
116 protected Namespace createNamespace(String prefix, String uri) {
117 return new Namespace(prefix, uri);
118 }
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167