1 7 package org.jboss.xml.binding; 8 9 11 import javax.xml.namespace.QName ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 16 22 public class NamespaceRegistry 23 { 24 public static final String PREFIX_XML_SCHEMA = "xsd"; 25 public static final String URI_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema"; 26 27 public static final String PREFIX_XML_SCHEMA_INSTANCE = "xsi"; 28 public static final String URI_XML_SCHEMA_INSTANCE = "http://www.w3.org/2000/10/XMLSchema-instance"; 29 30 private HashMap namespaceMap = new HashMap (); 32 private int namespaceIndex; 33 34 public NamespaceRegistry() 35 { 36 namespaceMap.put(URI_XML_SCHEMA, PREFIX_XML_SCHEMA); 37 namespaceMap.put(URI_XML_SCHEMA_INSTANCE, PREFIX_XML_SCHEMA_INSTANCE); 38 } 39 40 42 public QName registerQName(QName qname) 43 { 44 if (qname == null) 45 return null; 46 47 String prefix = registerNamespaceURI(qname.getNamespaceURI(), qname.getPrefix()); 48 qname = new QName (qname.getNamespaceURI(), qname.getLocalPart(), prefix); 49 return qname; 50 } 51 52 59 public String registerNamespaceURI(String namespaceURI, String prefix) 60 { 61 String regPrefix = (String )namespaceMap.get(namespaceURI); 62 63 if (regPrefix == null && prefix != null && prefix.length() > 0) 64 regPrefix = prefix; 65 66 if (regPrefix == null) 67 regPrefix = "ns" + (++namespaceIndex); 68 69 namespaceMap.put(namespaceURI, regPrefix); 70 return regPrefix; 71 } 72 73 public String getPrefix(String namespaceURI) 74 { 75 return (String )namespaceMap.get(namespaceURI); 76 } 77 78 public String getNamespaceURI(String prefix) 79 { 80 Iterator it = namespaceMap.entrySet().iterator(); 81 while (it.hasNext()) 82 { 83 Map.Entry entry = (Map.Entry )it.next(); 84 if (prefix.equals(entry.getValue())) 85 return (String )entry.getKey(); 86 } 87 return null; 88 } 89 90 public Iterator getRegisteredNamespaceURIs() 91 { 92 return namespaceMap.keySet().iterator(); 93 } 94 } 95 | Popular Tags |