1 17 package org.apache.servicemix.eip.support; 18 19 import java.util.Collections ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.LinkedHashMap ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 import javax.xml.XMLConstants ; 27 import javax.xml.namespace.NamespaceContext ; 28 29 37 public class NamespaceContextImpl implements NamespaceContext { 38 39 44 private Map namespaces = new LinkedHashMap (); 45 46 50 public NamespaceContextImpl() { 51 } 52 53 59 public NamespaceContextImpl(Map namespaces) { 60 setNamespaces(namespaces); 61 } 62 63 67 public Map getNamespaces() { 68 return namespaces; 69 } 70 71 74 public void setNamespaces(Map namespaces) { 75 this.namespaces.clear(); 76 if (namespaces != null) { 77 this.namespaces.putAll(namespaces); 78 } 79 } 80 81 84 public String getNamespaceURI(String prefix) { 85 if (prefix == null) { 86 throw new IllegalArgumentException ("prefix argument was null"); 87 } else if (prefix.equals(XMLConstants.XML_NS_PREFIX)) { 88 return XMLConstants.XML_NS_URI; 89 } else if (prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) { 90 return XMLConstants.XMLNS_ATTRIBUTE_NS_URI; 91 } else if (namespaces.containsKey(prefix)) { 92 String uri = (String ) namespaces.get(prefix); 93 if (uri.length() == 0) { 94 return null; 95 } else { 96 return uri; 97 } 98 } else { 99 return null; 100 } 101 } 102 103 106 public String getPrefix(String nsURI) { 107 if (nsURI == null) { 108 throw new IllegalArgumentException ("nsURI was null"); 109 } else if (nsURI.length() == 0) { 110 throw new IllegalArgumentException ("nsURI was empty"); 111 } else if (nsURI.equals(XMLConstants.XML_NS_URI)) { 112 return XMLConstants.XML_NS_PREFIX; 113 } else if (nsURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) { 114 return XMLConstants.XMLNS_ATTRIBUTE; 115 } 116 Iterator iter = namespaces.entrySet().iterator(); 117 while (iter.hasNext()) { 118 Map.Entry entry = (Map.Entry ) iter.next(); 119 String uri = (String ) entry.getValue(); 120 if (uri.equals(nsURI)) { 121 return (String ) entry.getKey(); 122 } 123 } 124 if (nsURI.length() == 0) { 125 return ""; 126 } else { 127 return null; 128 } 129 } 130 131 134 public Iterator getPrefixes(String nsURI) { 135 if (nsURI == null) { 136 throw new IllegalArgumentException ("nsURI was null"); 137 } else if (nsURI.length() == 0) { 138 throw new IllegalArgumentException ("nsURI was empty"); 139 } else if (nsURI.equals(XMLConstants.XML_NS_URI)) { 140 return Collections.singleton(XMLConstants.XML_NS_PREFIX).iterator(); 141 } else if (nsURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) { 142 return Collections.singleton(XMLConstants.XMLNS_ATTRIBUTE).iterator(); 143 } 144 Set prefixes = null; 145 Iterator iter = namespaces.entrySet().iterator(); 146 while (iter.hasNext()) { 147 Map.Entry entry = (Map.Entry ) iter.next(); 148 String uri = (String ) entry.getValue(); 149 if (uri.equals(nsURI)) { 150 if (prefixes == null) { 151 prefixes = new HashSet (); 152 } 153 prefixes.add(entry.getKey()); 154 } 155 } 156 if (prefixes != null) { 157 return Collections.unmodifiableSet(prefixes).iterator(); 158 } else if (nsURI.length() == 0) { 159 return Collections.singleton("").iterator(); 160 } else { 161 return Collections.EMPTY_LIST.iterator(); 162 } 163 } 164 165 } 166 | Popular Tags |