1 22 package org.jboss.xb.binding; 23 24 import javax.xml.namespace.NamespaceContext ; 25 import javax.xml.namespace.QName ; 26 27 import java.io.Serializable ; 28 import java.util.Collections ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 import java.util.List ; 33 import java.util.ArrayList ; 34 35 48 public class NamespaceRegistry implements NamespaceContext , Serializable 49 { 50 private static final long serialVersionUID = 8435680858785550261L; 51 52 private int namespaceIndex; 54 55 private final Map prefixByUri = new HashMap (); 56 private final Map uriByPrefix = new HashMap (); 57 58 public NamespaceRegistry() 59 { 60 } 61 62 64 public QName registerQName(QName qname) 65 { 66 if (qname == null) 67 return null; 68 69 String nsURI = qname.getNamespaceURI(); 70 String prefix = getPrefix(nsURI); 71 if (prefix == null) 72 { 73 prefix = qname.getPrefix(); 74 if (prefix.length() == 0) 75 prefix = registerURI(nsURI, null); 76 else 77 prefix = registerURI(nsURI, prefix); 78 } 79 80 qname = new QName (nsURI, qname.getLocalPart(), prefix); 81 return qname; 82 } 83 84 91 public String registerURI(String nsURI, String prefix) 92 { 93 if (prefix == null) 94 { 95 prefix = "ns" + (++namespaceIndex); 96 } 97 98 addPrefixMapping(prefix, nsURI); 99 return prefix; 100 } 101 102 108 public void addPrefixMapping(String prefix, String nsURI) 109 { 110 if (nsURI == null) 111 throw new IllegalArgumentException ("Cannot add mapping for null namespace URI"); 112 113 Object obj = uriByPrefix.get(prefix); 114 if (nsURI.equals(obj) == false) 115 { 116 if (obj == null) 117 { 118 uriByPrefix.put(prefix, nsURI); 119 } 120 else if (obj instanceof String ) 121 { 122 List list = new ArrayList (); 123 list.add(obj); 124 list.add(nsURI); 125 uriByPrefix.put(prefix, list); 126 } 127 else if (obj instanceof List ) 128 { 129 ((List )obj).add(nsURI); 130 } 131 else 132 { 133 throwUnexpectedEntryException(obj); 134 } 135 136 obj = prefixByUri.get(nsURI); 137 if (obj == null) 138 { 139 prefixByUri.put(nsURI, prefix); 140 } 141 else if (obj instanceof String ) 142 { 143 List list = new ArrayList (); 144 list.add(obj); 145 list.add(prefix); 146 prefixByUri.put(nsURI, list); 147 } 148 else if (obj instanceof List ) 149 { 150 ((List )obj).add(prefix); 151 } 152 else 153 { 154 throwUnexpectedEntryException(obj); 155 } 156 } 157 } 158 159 164 public void removePrefixMapping(String prefix) 165 { 166 Object obj = uriByPrefix.get(prefix); 167 if (obj != null) 168 { 169 String uri = null; 170 if (obj instanceof String ) 171 { 172 uri = (String )obj; 173 uriByPrefix.remove(prefix); 174 } 175 else if (obj instanceof List ) 176 { 177 List list = (List )obj; 178 uri = (String )list.remove(list.size() - 1); 179 if (list.isEmpty()) 180 { 181 uriByPrefix.remove(prefix); 182 } 183 } 184 else 185 { 186 throwUnexpectedEntryException(obj); 187 } 188 189 if (uri != null) 190 { 191 obj = prefixByUri.get(uri); 192 if (obj instanceof String ) 193 { 194 if (!prefix.equals(obj)) 195 { 196 throw new IllegalStateException ("Inconsistent mapping: prefix=" + prefix + ", found=" + obj); 197 } 198 prefixByUri.remove(uri); 199 } 200 else if (obj instanceof List ) 201 { 202 List list = (ArrayList )obj; 203 list.remove(prefix); 204 if (list.isEmpty()) 205 { 206 prefixByUri.remove(uri); 207 } 208 } 209 else 210 { 211 throwUnexpectedEntryException(obj); 212 } 213 } 214 } 215 } 216 217 223 public void unregisterURI(String nsURI) 224 { 225 Object obj = prefixByUri.get(nsURI); 226 if (obj != null) 227 { 228 String prefix = null; 229 if (obj instanceof String ) 230 { 231 prefix = (String )obj; 232 prefixByUri.remove(nsURI); 233 removePrefixMappingOnly(prefix, nsURI); 234 } 235 else if (obj instanceof List ) 236 { 237 List list = (List )obj; 238 for (int i = 0; i < list.size(); ++i) 239 { 240 removePrefixMappingOnly((String )list.get(i), nsURI); 241 } 242 prefixByUri.remove(nsURI); 243 } 244 else 245 { 246 throwUnexpectedEntryException(obj); 247 } 248 } 249 } 250 251 253 public boolean isRegistered(String nsURI) 254 { 255 return prefixByUri.containsKey(nsURI); 256 } 257 258 260 public Iterator getRegisteredURIs() 261 { 262 return prefixByUri.keySet().iterator(); 263 } 264 265 267 public Iterator getRegisteredPrefixes() 268 { 269 return uriByPrefix.keySet().iterator(); 270 } 271 272 275 public int size() 276 { 277 return prefixByUri.size(); 278 } 279 280 282 285 public String getPrefix(String nsURI) 286 { 287 Object obj = prefixByUri.get(nsURI); 288 289 String prefix = null; 290 if (obj != null) 291 { 292 if (obj instanceof String ) 293 { 294 prefix = (String )obj; 295 } 296 else if (obj instanceof List ) 297 { 298 List list = (List )obj; 299 prefix = (String )list.get(list.size() - 1); 300 } 301 else 302 { 303 throwUnexpectedEntryException(obj); 304 } 305 } 306 307 return prefix; 308 } 309 310 316 public Iterator getPrefixes(String namespaceURI) 317 { 318 Object obj = prefixByUri.get(namespaceURI); 319 320 Iterator result = null; 321 if (obj == null) 322 { 323 result = Collections.EMPTY_LIST.iterator(); 324 } 325 else if (obj instanceof String ) 326 { 327 result = Collections.singletonList(obj).iterator(); 328 } 329 else if (obj instanceof List ) 330 { 331 result = ((List )obj).iterator(); 332 } 333 else 334 { 335 throwUnexpectedEntryException(obj); 336 } 337 338 return result; 339 } 340 341 343 public String getNamespaceURI(String prefix) 344 { 345 Object obj = uriByPrefix.get(prefix); 346 347 String uri = null; 348 if (obj != null) 349 { 350 if (obj instanceof String ) 351 { 352 uri = (String )obj; 353 } 354 else if (obj instanceof List ) 355 { 356 List list = (List )obj; 357 uri = (String )list.get(list.size() - 1); 358 } 359 else 360 { 361 throwUnexpectedEntryException(obj); 362 } 363 } 364 365 return uri; 366 } 367 368 370 private void removePrefixMappingOnly(String prefix, String nsURI) 371 { 372 Object obj = uriByPrefix.get(prefix); 373 if (obj instanceof String ) 374 { 375 if (!obj.equals(nsURI)) 376 { 377 throw new IllegalStateException ("Inconsistent mapping: uri=" + nsURI + ", found=" + obj); 378 } 379 uriByPrefix.remove(prefix); 380 } 381 else if (obj instanceof List ) 382 { 383 List list = (List )obj; 384 list.remove(prefix); 385 if (list.isEmpty()) 386 { 387 uriByPrefix.remove(prefix); 388 } 389 } 390 } 391 392 private void throwUnexpectedEntryException(Object entry) 393 { 394 throw new IllegalStateException ("Unexpected entry type: expected java.lang.String or java.util.List but got " + entry.getClass()); 395 } 396 } 397 | Popular Tags |