1 17 package org.apache.servicemix.client; 18 19 import javax.xml.xpath.*; 20 import javax.xml.namespace.NamespaceContext ; 21 22 import java.util.*; 23 24 30 public class DefaultNamespaceContext implements NamespaceContext { 31 32 private final Map map; 33 private final NamespaceContext parent; 34 35 public DefaultNamespaceContext() { 36 this.map = new HashMap(); 37 XPathFactory factory = XPathFactory.newInstance(); 38 this.parent = factory.newXPath().getNamespaceContext(); 39 } 40 41 public DefaultNamespaceContext(NamespaceContext parent, Map map) { 42 this.parent = parent; 43 this.map = map; 44 } 45 46 49 public DefaultNamespaceContext add(String prefix, String uri) { 50 map.put(prefix, uri); 51 return this; 52 } 53 54 public String getNamespaceURI(String prefix) { 55 String answer = (String ) map.get(prefix); 56 if (answer == null && parent != null) { 57 return parent.getNamespaceURI(prefix); 58 } 59 return answer; 60 } 61 62 public String getPrefix(String namespaceURI) { 63 for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) { 64 Map.Entry entry = (Map.Entry) iter.next(); 65 if (namespaceURI.equals(entry.getValue())) { 66 return (String ) entry.getKey(); 67 } 68 } 69 if (parent != null) { 70 return parent.getPrefix(namespaceURI); 71 } 72 return null; 73 } 74 75 public Iterator getPrefixes(String namespaceURI) { 76 Set set = new HashSet(); 77 for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) { 78 Map.Entry entry = (Map.Entry) iter.next(); 79 if (namespaceURI.equals(entry.getValue())) { 80 set.add(entry.getKey()); 81 } 82 } 83 if (parent != null) { 84 Iterator iter = parent.getPrefixes(namespaceURI); 85 while (iter.hasNext()) { 86 set.add(iter.next()); 87 } 88 } 89 return set.iterator(); 90 } 91 } 92 | Popular Tags |