1 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 34 public class QNameCache { 35 36 protected Map noNamespaceCache = Collections 37 .synchronizedMap(new WeakHashMap ()); 38 39 43 protected Map namespaceCache = Collections 44 .synchronizedMap(new WeakHashMap ()); 45 46 50 private DocumentFactory documentFactory; 51 52 public QNameCache() { 53 } 54 55 public QNameCache(DocumentFactory documentFactory) { 56 this.documentFactory = documentFactory; 57 } 58 59 64 public List getQNames() { 65 List answer = new ArrayList (); 66 answer.addAll(noNamespaceCache.values()); 67 68 for (Iterator it = namespaceCache.values().iterator(); it.hasNext();) { 69 Map map = (Map ) it.next(); 70 answer.addAll(map.values()); 71 } 72 73 return answer; 74 } 75 76 84 public QName get(String name) { 85 QName answer = null; 86 87 if (name != null) { 88 answer = (QName) noNamespaceCache.get(name); 89 } else { 90 name = ""; 91 } 92 93 if (answer == null) { 94 answer = createQName(name); 95 answer.setDocumentFactory(documentFactory); 96 noNamespaceCache.put(name, answer); 97 } 98 99 return answer; 100 } 101 102 112 public QName get(String name, Namespace namespace) { 113 Map cache = getNamespaceCache(namespace); 114 QName answer = null; 115 116 if (name != null) { 117 answer = (QName) cache.get(name); 118 } else { 119 name = ""; 120 } 121 122 if (answer == null) { 123 answer = createQName(name, namespace); 124 answer.setDocumentFactory(documentFactory); 125 cache.put(name, answer); 126 } 127 128 return answer; 129 } 130 131 143 public QName get(String localName, Namespace namespace, String qName) { 144 Map cache = getNamespaceCache(namespace); 145 QName answer = null; 146 147 if (localName != null) { 148 answer = (QName) cache.get(localName); 149 } else { 150 localName = ""; 151 } 152 153 if (answer == null) { 154 answer = createQName(localName, namespace, qName); 155 answer.setDocumentFactory(documentFactory); 156 cache.put(localName, answer); 157 } 158 159 return answer; 160 } 161 162 public QName get(String qualifiedName, String uri) { 163 int index = qualifiedName.indexOf(':'); 164 165 if (index < 0) { 166 return get(qualifiedName, Namespace.get(uri)); 167 } else { 168 String name = qualifiedName.substring(index + 1); 169 String prefix = qualifiedName.substring(0, index); 170 171 return get(name, Namespace.get(prefix, uri)); 172 } 173 } 174 175 184 public QName intern(QName qname) { 185 return get(qname.getName(), qname.getNamespace(), qname 186 .getQualifiedName()); 187 } 188 189 198 protected Map getNamespaceCache(Namespace namespace) { 199 if (namespace == Namespace.NO_NAMESPACE) { 200 return noNamespaceCache; 201 } 202 203 Map answer = null; 204 205 if (namespace != null) { 206 answer = (Map ) namespaceCache.get(namespace); 207 } 208 209 if (answer == null) { 210 answer = createMap(); 211 namespaceCache.put(namespace, answer); 212 } 213 214 return answer; 215 } 216 217 222 protected Map createMap() { 223 return Collections.synchronizedMap(new HashMap ()); 224 } 225 226 235 protected QName createQName(String name) { 236 return new QName(name); 237 } 238 239 250 protected QName createQName(String name, Namespace namespace) { 251 return new QName(name, namespace); 252 } 253 254 267 protected QName createQName(String name, Namespace namespace, 268 String qualifiedName) { 269 return new QName(name, namespace, qualifiedName); 270 } 271 } 272 273 274 275 | Popular Tags |