1 16 package org.apache.commons.jxpath.ri; 17 18 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 23 import org.apache.commons.jxpath.Pointer; 24 import org.apache.commons.jxpath.ri.model.NodeIterator; 25 import org.apache.commons.jxpath.ri.model.NodePointer; 26 27 33 public class NamespaceResolver implements Cloneable { 34 35 protected HashMap namespaceMap = new HashMap (); 36 protected HashMap reverseMap; 37 protected NodePointer pointer; 38 private boolean sealed; 39 40 46 public void registerNamespace(String prefix, String namespaceURI) { 47 namespaceMap.put(prefix, namespaceURI); 48 reverseMap = null; 49 } 50 51 54 public void setNamespaceContextPointer(NodePointer pointer) { 55 this.pointer = pointer; 56 } 57 58 public Pointer getNamespaceContextPointer() { 59 return pointer; 60 } 61 62 72 public String getNamespaceURI(String prefix) { 73 String uri = (String ) namespaceMap.get(prefix); 74 if (uri == null && pointer != null) { 75 uri = pointer.getNamespaceURI(prefix); 76 } 77 return uri; 79 } 80 81 public String getPrefix(String namespaceURI) { 82 if (reverseMap == null) { 83 reverseMap = new HashMap (); 84 NodeIterator ni = pointer.namespaceIterator(); 85 if (ni != null) { 86 for (int position = 1; ni.setPosition(position); position++) { 87 NodePointer nsPointer = ni.getNodePointer(); 88 QName qname = nsPointer.getName(); 89 reverseMap.put(qname.getPrefix(), qname.getName()); 90 } 91 } 92 Iterator it = namespaceMap.entrySet().iterator(); 93 while (it.hasNext()) { 94 Map.Entry entry = (Map.Entry ) it.next(); 95 reverseMap.put(entry.getValue(), entry.getKey()); 96 } 97 } 98 String prefix = (String ) reverseMap.get(namespaceURI); 99 return prefix; 100 } 101 102 public boolean isSealed() { 103 return sealed; 104 } 105 106 public void seal() { 107 sealed = true; 108 } 109 110 public Object clone() { 111 try { 112 return super.clone(); 113 } 114 catch (CloneNotSupportedException e) { 115 e.printStackTrace(); 117 return null; 118 } 119 } 120 } | Popular Tags |