1 7 8 package org.dom4j.tree; 9 10 import java.util.ArrayList ; 11 import java.util.HashMap ; 12 import java.util.Map ; 13 14 import org.dom4j.DocumentFactory; 15 import org.dom4j.Namespace; 16 import org.dom4j.QName; 17 18 26 public class NamespaceStack { 27 28 private DocumentFactory documentFactory; 29 30 31 private ArrayList namespaceStack = new ArrayList (); 32 33 34 private ArrayList namespaceCacheList = new ArrayList (); 35 36 40 private Map currentNamespaceCache; 41 42 46 private Map rootNamespaceCache = new HashMap (); 47 48 49 private Namespace defaultNamespace; 50 51 public NamespaceStack() { 52 this.documentFactory = DocumentFactory.getInstance(); 53 } 54 55 public NamespaceStack(DocumentFactory documentFactory) { 56 this.documentFactory = documentFactory; 57 } 58 59 66 public void push(Namespace namespace) { 67 namespaceStack.add(namespace); 68 namespaceCacheList.add(null); 69 currentNamespaceCache = null; 70 71 String prefix = namespace.getPrefix(); 72 73 if ((prefix == null) || (prefix.length() == 0)) { 74 defaultNamespace = namespace; 75 } 76 } 77 78 83 public Namespace pop() { 84 return remove(namespaceStack.size() - 1); 85 } 86 87 92 public int size() { 93 return namespaceStack.size(); 94 } 95 96 99 public void clear() { 100 namespaceStack.clear(); 101 namespaceCacheList.clear(); 102 rootNamespaceCache.clear(); 103 currentNamespaceCache = null; 104 } 105 106 114 public Namespace getNamespace(int index) { 115 return (Namespace) namespaceStack.get(index); 116 } 117 118 127 public Namespace getNamespaceForPrefix(String prefix) { 128 if (prefix == null) { 129 prefix = ""; 130 } 131 132 for (int i = namespaceStack.size() - 1; i >= 0; i--) { 133 Namespace namespace = (Namespace) namespaceStack.get(i); 134 135 if (prefix.equals(namespace.getPrefix())) { 136 return namespace; 137 } 138 } 139 140 return null; 141 } 142 143 151 public String getURI(String prefix) { 152 Namespace namespace = getNamespaceForPrefix(prefix); 153 154 return (namespace != null) ? namespace.getURI() : null; 155 } 156 157 165 public boolean contains(Namespace namespace) { 166 String prefix = namespace.getPrefix(); 167 Namespace current = null; 168 169 if ((prefix == null) || (prefix.length() == 0)) { 170 current = getDefaultNamespace(); 171 } else { 172 current = getNamespaceForPrefix(prefix); 173 } 174 175 if (current == null) { 176 return false; 177 } 178 179 if (current == namespace) { 180 return true; 181 } 182 183 return namespace.getURI().equals(current.getURI()); 184 } 185 186 public QName getQName(String namespaceURI, String localName, 187 String qualifiedName) { 188 if (localName == null) { 189 localName = qualifiedName; 190 } else if (qualifiedName == null) { 191 qualifiedName = localName; 192 } 193 194 if (namespaceURI == null) { 195 namespaceURI = ""; 196 } 197 198 String prefix = ""; 199 int index = qualifiedName.indexOf(":"); 200 201 if (index > 0) { 202 prefix = qualifiedName.substring(0, index); 203 204 if (localName.trim().length() == 0) { 205 localName = qualifiedName.substring(index + 1); 206 } 207 } else if (localName.trim().length() == 0) { 208 localName = qualifiedName; 209 } 210 211 Namespace namespace = createNamespace(prefix, namespaceURI); 212 213 return pushQName(localName, qualifiedName, namespace, prefix); 214 } 215 216 public QName getAttributeQName(String namespaceURI, String localName, 217 String qualifiedName) { 218 if (qualifiedName == null) { 219 qualifiedName = localName; 220 } 221 222 Map map = getNamespaceCache(); 223 QName answer = (QName) map.get(qualifiedName); 224 225 if (answer != null) { 226 return answer; 227 } 228 229 if (localName == null) { 230 localName = qualifiedName; 231 } 232 233 if (namespaceURI == null) { 234 namespaceURI = ""; 235 } 236 237 Namespace namespace = null; 238 String prefix = ""; 239 int index = qualifiedName.indexOf(":"); 240 241 if (index > 0) { 242 prefix = qualifiedName.substring(0, index); 243 namespace = createNamespace(prefix, namespaceURI); 244 245 if (localName.trim().length() == 0) { 246 localName = qualifiedName.substring(index + 1); 247 } 248 } else { 249 namespace = Namespace.NO_NAMESPACE; 251 252 if (localName.trim().length() == 0) { 253 localName = qualifiedName; 254 } 255 } 256 257 answer = pushQName(localName, qualifiedName, namespace, prefix); 258 map.put(qualifiedName, answer); 259 260 return answer; 261 } 262 263 271 public void push(String prefix, String uri) { 272 if (uri == null) { 273 uri = ""; 274 } 275 276 Namespace namespace = createNamespace(prefix, uri); 277 push(namespace); 278 } 279 280 290 public Namespace addNamespace(String prefix, String uri) { 291 Namespace namespace = createNamespace(prefix, uri); 292 push(namespace); 293 294 return namespace; 295 } 296 297 305 public Namespace pop(String prefix) { 306 if (prefix == null) { 307 prefix = ""; 308 } 309 310 Namespace namespace = null; 311 312 for (int i = namespaceStack.size() - 1; i >= 0; i--) { 313 Namespace ns = (Namespace) namespaceStack.get(i); 314 315 if (prefix.equals(ns.getPrefix())) { 316 remove(i); 317 namespace = ns; 318 319 break; 320 } 321 } 322 323 if (namespace == null) { 324 System.out.println("Warning: missing namespace prefix ignored: " 325 + prefix); 326 } 327 328 return namespace; 329 } 330 331 public String toString() { 332 return super.toString() + " Stack: " + namespaceStack.toString(); 333 } 334 335 public DocumentFactory getDocumentFactory() { 336 return documentFactory; 337 } 338 339 public void setDocumentFactory(DocumentFactory documentFactory) { 340 this.documentFactory = documentFactory; 341 } 342 343 public Namespace getDefaultNamespace() { 344 if (defaultNamespace == null) { 345 defaultNamespace = findDefaultNamespace(); 346 } 347 348 return defaultNamespace; 349 } 350 351 354 368 protected QName pushQName(String localName, String qualifiedName, 369 Namespace namespace, String prefix) { 370 if ((prefix == null) || (prefix.length() == 0)) { 371 this.defaultNamespace = null; 372 } 373 374 return createQName(localName, qualifiedName, namespace); 375 } 376 377 390 protected QName createQName(String localName, String qualifiedName, 391 Namespace namespace) { 392 return documentFactory.createQName(localName, namespace); 393 } 394 395 406 protected Namespace createNamespace(String prefix, String namespaceURI) { 407 return documentFactory.createNamespace(prefix, namespaceURI); 408 } 409 410 416 protected Namespace findDefaultNamespace() { 417 for (int i = namespaceStack.size() - 1; i >= 0; i--) { 418 Namespace namespace = (Namespace) namespaceStack.get(i); 419 420 if (namespace != null) { 421 String prefix = namespace.getPrefix(); 422 423 if ((prefix == null) || (namespace.getPrefix().length() == 0)) { 424 return namespace; 425 } 426 } 427 } 428 429 return null; 430 } 431 432 440 protected Namespace remove(int index) { 441 Namespace namespace = (Namespace) namespaceStack.remove(index); 442 namespaceCacheList.remove(index); 443 defaultNamespace = null; 444 currentNamespaceCache = null; 445 446 return namespace; 447 } 448 449 protected Map getNamespaceCache() { 450 if (currentNamespaceCache == null) { 451 int index = namespaceStack.size() - 1; 452 453 if (index < 0) { 454 currentNamespaceCache = rootNamespaceCache; 455 } else { 456 currentNamespaceCache = (Map ) namespaceCacheList.get(index); 457 458 if (currentNamespaceCache == null) { 459 currentNamespaceCache = new HashMap (); 460 namespaceCacheList.set(index, currentNamespaceCache); 461 } 462 } 463 } 464 465 return currentNamespaceCache; 466 } 467 } 468 469 505 | Popular Tags |