Clover coverage report - dom4j - 1.6.1
Coverage timestamp: ma mei 16 2005 14:23:01 GMT+01:00

file stats: | LOC: | 310 |  | Methods: | 13
| NCLOC: | 117 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL

QNameCache.java

77,3%

78,6%

76,9%

78%

1 |  | /*
2 |  | * Copyright 2001-2005 (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 |  |
8 |  | package org.dom4j.tree;
9 |  |
10 |  | import java.util.ArrayList;
11 |  | import java.util.Collections;
12 |  | import java.util.HashMap;
13 |  | import java.util.Iterator;
14 |  | import java.util.List;
15 |  | import java.util.Map;
16 |  | import java.util.WeakHashMap;
17 |  |
18 |  | import org.dom4j.DocumentFactory;
19 |  | import org.dom4j.Namespace;
20 |  | import org.dom4j.QName;
21 |  |
22 |  | /**
23 |  | * <p>
24 |  | * <code>QNameCache</code> caches instances of <code>QName</code> for reuse
25 |  | * both across documents and within documents.
26 |  | * </p>< < < < < < < QNameCache.java
27 |  | *
28 |  | * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
29 |  | * @version $Revision: 1.16 $ =======
30 |  | *
31 |  | * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
32 |  | * @version $Revision: 1.16 $ >>>>>>> 1.15
33 |  | */
34 |  | public class QNameCache {
35 |  | /** Cache of {@link QName}instances with no namespace */
36 |  | protected Map noNamespaceCache = Collections
37 |  | .synchronizedMap(new WeakHashMap());
38 |  |
39 |  | /**
40 |  | * Cache of {@link Map}instances indexed by namespace which contain caches
41 |  | * of {@link QName}for each name
42 |  | */
43 |  | protected Map namespaceCache = Collections
44 |  | .synchronizedMap(new WeakHashMap());
45 |  |
46 |  | /**
47 |  | * The document factory associated with new QNames instances in this cache
48 |  | * or null if no instances should be associated by default
49 |  | */
50 |  | private DocumentFactory documentFactory;
51 |  |
52 | 90 | public QNameCache() {
53 |  | }
54 |  |
55 | 544 | public QNameCache(DocumentFactory documentFactory) {
56 | 544 | this.documentFactory = documentFactory;
57 |  | }
58 |  |
59 |  | /**
60 |  | * Returns a list of all the QName instances currently used
61 |  | *
62 |  | * @return DOCUMENT ME!
63 |  | */
64 | 1 | public List getQNames() {
65 | 1 | List answer = new ArrayList();
66 | 1 | answer.addAll(noNamespaceCache.values());
67 |  |
68 | 1 | for (Iterator it = namespaceCache.values().iterator(); it.hasNext();) {
69 | 10 | Map map = (Map) it.next();
70 | 10 | answer.addAll(map.values());
71 |  | }
72 |  |
73 | 1 | return answer;
74 |  | }
75 |  |
76 |  | /**
77 |  | * DOCUMENT ME!
78 |  | *
79 |  | * @param name
80 |  | * DOCUMENT ME!
81 |  | *
82 |  | * @return the QName for the given name and no namepsace
83 |  | */
84 | 26755 | public QName get(String name) {
85 | 26755 | QName answer = null;
86 |  |
87 | 26755 | if (name != null) {
88 | 26631 | answer = (QName) noNamespaceCache.get(name);
89 |  | } else {
90 | 124 | name = "";
91 |  | }
92 |  |
93 | 26755 | if (answer == null) {
94 | 1065 | answer = createQName(name);
95 | 1065 | answer.setDocumentFactory(documentFactory);
96 | 1065 | noNamespaceCache.put(name, answer);
97 |  | }
98 |  |
99 | 26755 | return answer;
100 |  | }
101 |  |
102 |  | /**
103 |  | * DOCUMENT ME!
104 |  | *
105 |  | * @param name
106 |  | * DOCUMENT ME!
107 |  | * @param namespace
108 |  | * DOCUMENT ME!
109 |  | *
110 |  | * @return the QName for the given local name and namepsace
111 |  | */
112 | 105078 | public QName get(String name, Namespace namespace) {
113 | 105078 | Map cache = getNamespaceCache(namespace);
114 | 105078 | QName answer = null;
115 |  |
116 | 105078 | if (name != null) {
117 | 105054 | answer = (QName) cache.get(name);
118 |  | } else {
119 | 24 | name = "";
120 |  | }
121 |  |
122 | 105078 | if (answer == null) {
123 | 1595 | answer = createQName(name, namespace);
124 | 1595 | answer.setDocumentFactory(documentFactory);
125 | 1595 | cache.put(name, answer);
126 |  | }
127 |  |
128 | 105078 | return answer;
129 |  | }
130 |  |
131 |  | /**
132 |  | * DOCUMENT ME!
133 |  | *
134 |  | * @param localName
135 |  | * DOCUMENT ME!
136 |  | * @param namespace
137 |  | * DOCUMENT ME!
138 |  | * @param qName
139 |  | * DOCUMENT ME!
140 |  | *
141 |  | * @return the QName for the given local name, qualified name and namepsace
142 |  | */
143 | 0 | public QName get(String localName, Namespace namespace, String qName) {
144 | 0 | Map cache = getNamespaceCache(namespace);
145 | 0 | QName answer = null;
146 |  |
147 | 0 | if (localName != null) {
148 | 0 | answer = (QName) cache.get(localName);
149 |  | } else {
150 | 0 | localName = "";
151 |  | }
152 |  |
153 | 0 | if (answer == null) {
154 | 0 | answer = createQName(localName, namespace, qName);
155 | 0 | answer.setDocumentFactory(documentFactory);
156 | 0 | cache.put(localName, answer);
157 |  | }
158 |  |
159 | 0 | return answer;
160 |  | }
161 |  |
162 | 30 | public QName get(String qualifiedName, String uri) {
163 | 30 | int index = qualifiedName.indexOf(':');
164 |  |
165 | 30 | if (index < 0) {
166 | 26 | return get(qualifiedName, Namespace.get(uri));
167 |  | } else {
168 | 4 | String name = qualifiedName.substring(index + 1);
169 | 4 | String prefix = qualifiedName.substring(0, index);
170 |  |
171 | 4 | return get(name, Namespace.get(prefix, uri));
172 |  | }
173 |  | }
174 |  |
175 |  | /**
176 |  | * DOCUMENT ME!
177 |  | *
178 |  | * @param qname
179 |  | * DOCUMENT ME!
180 |  | *
181 |  | * @return the cached QName instance if there is one or adds the given qname
182 |  | * to the cache if not
183 |  | */
184 | 0 | public QName intern(QName qname) {
185 | 0 | return get(qname.getName(), qname.getNamespace(), qname
186 |  | .getQualifiedName());
187 |  | }
188 |  |
189 |  | /**
190 |  | * DOCUMENT ME!
191 |  | *
192 |  | * @param namespace
193 |  | * DOCUMENT ME!
194 |  | *
195 |  | * @return the cache for the given namespace. If one does not currently
196 |  | * exist it is created.
197 |  | */
198 | 105078 | protected Map getNamespaceCache(Namespace namespace) {
199 | 105078 | if (namespace == Namespace.NO_NAMESPACE) {
200 | 84795 | return noNamespaceCache;
201 |  | }
202 |  |
203 | 20283 | Map answer = null;
204 |  |
205 | 20283 | if (namespace != null) {
206 | 20283 | answer = (Map) namespaceCache.get(namespace);
207 |  | }
208 |  |
209 | 20283 | if (answer == null) {
210 | 167 | answer = createMap();
211 | 167 | namespaceCache.put(namespace, answer);
212 |  | }
213 |  |
214 | 20283 | return answer;
215 |  | }
216 |  |
217 |  | /**
218 |  | * A factory method
219 |  | *
220 |  | * @return a newly created {@link Map}instance.
221 |  | */
222 | 167 | protected Map createMap() {
223 | 167 | return Collections.synchronizedMap(new HashMap());
224 |  | }
225 |  |
226 |  | /**
227 |  | * Factory method to create a new QName object which can be overloaded to
228 |  | * create derived QName instances
229 |  | *
230 |  | * @param name
231 |  | * DOCUMENT ME!
232 |  | *
233 |  | * @return DOCUMENT ME!
234 |  | */
235 | 1065 | protected QName createQName(String name) {
236 | 1065 | return new QName(name);
237 |  | }
238 |  |
239 |  | /**
240 |  | * Factory method to create a new QName object which can be overloaded to
241 |  | * create derived QName instances
242 |  | *
243 |  | * @param name
244 |  | * DOCUMENT ME!
245 |  | * @param namespace
246 |  | * DOCUMENT ME!
247 |  | *
248 |  | * @return DOCUMENT ME!
249 |  | */
250 | 1595 | protected QName createQName(String name, Namespace namespace) {
251 | 1595 | return new QName(name, namespace);
252 |  | }
253 |  |
254 |  | /**
255 |  | * Factory method to create a new QName object which can be overloaded to
256 |  | * create derived QName instances
257 |  | *
258 |  | * @param name
259 |  | * DOCUMENT ME!
260 |  | * @param namespace
261 |  | * DOCUMENT ME!
262 |  | * @param qualifiedName
263 |  | * DOCUMENT ME!
264 |  | *
265 |  | * @return DOCUMENT ME!
266 |  | */
267 | 0 | protected QName createQName(String name, Namespace namespace,
268 |  | String qualifiedName) {
269 | 0 | return new QName(name, namespace, qualifiedName);
270 |  | }
271 |  | }
272 |  |
273 |  |
274 |  |
275 |  | /*
276 |  | * Redistribution and use of this software and associated documentation
277 |  | * ("Software"), with or without modification, are permitted provided that the
278 |  | * following conditions are met:
279 |  | *
280 |  | * 1. Redistributions of source code must retain copyright statements and
281 |  | * notices. Redistributions must also contain a copy of this document.
282 |  | *
283 |  | * 2. Redistributions in binary form must reproduce the above copyright notice,
284 |  | * this list of conditions and the following disclaimer in the documentation
285 |  | * and/or other materials provided with the distribution.
286 |  | *
287 |  | * 3. The name "DOM4J" must not be used to endorse or promote products derived
288 |  | * from this Software without prior written permission of MetaStuff, Ltd. For
289 |  | * written permission, please contact dom4j-info@metastuff.com.
290 |  | *
291 |  | * 4. Products derived from this Software may not be called "DOM4J" nor may
292 |  | * "DOM4J" appear in their names without prior written permission of MetaStuff,
293 |  | * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
294 |  | *
295 |  | * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
296 |  | *
297 |  | * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
298 |  | * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
299 |  | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
300 |  | * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
301 |  | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
302 |  | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
303 |  | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
304 |  | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
305 |  | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
306 |  | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
307 |  | * POSSIBILITY OF SUCH DAMAGE.
308 |  | *
309 |  | * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
310 |  | */