KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xml > binding > NamespaceRegistry


1 /**
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.xml.binding;
8
9 // $Id: NamespaceRegistry.java,v 1.5.2.5 2005/06/26 04:42:04 starksm Exp $
10

11 import javax.xml.namespace.QName JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15
16 /**
17  * A simple namespace registry
18  *
19  * @author thomas.diesler@jboss.org
20  * @since 08-June-2004
21  */

22 public class NamespaceRegistry
23 {
24    public static final String JavaDoc PREFIX_XML_SCHEMA = "xsd";
25    public static final String JavaDoc URI_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
26    
27    public static final String JavaDoc PREFIX_XML_SCHEMA_INSTANCE = "xsi";
28    public static final String JavaDoc URI_XML_SCHEMA_INSTANCE = "http://www.w3.org/2000/10/XMLSchema-instance";
29
30    // Maps namespace uri to prefix
31
private HashMap JavaDoc namespaceMap = new HashMap JavaDoc();
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    /** Register a QName and return a QName that is guarantied to have a prefix
41     */

42    public QName JavaDoc registerQName(QName JavaDoc qname)
43    {
44       if (qname == null)
45          return null;
46
47       String JavaDoc prefix = registerNamespaceURI(qname.getNamespaceURI(), qname.getPrefix());
48       qname = new QName JavaDoc(qname.getNamespaceURI(), qname.getLocalPart(), prefix);
49       return qname;
50    }
51
52    /** Register the given namespaceURI/prefix combination.
53     * In case the prefix is null, it will be assigend.
54     *
55     * @param namespaceURI The namespaceURI
56     * @param prefix The corresponding prefix, maybe null
57     * @return A prefix, never null
58     */

59    public String JavaDoc registerNamespaceURI(String JavaDoc namespaceURI, String JavaDoc prefix)
60    {
61       String JavaDoc regPrefix = (String JavaDoc)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 JavaDoc getPrefix(String JavaDoc namespaceURI)
74    {
75       return (String JavaDoc)namespaceMap.get(namespaceURI);
76    }
77
78    public String JavaDoc getNamespaceURI(String JavaDoc prefix)
79    {
80       Iterator JavaDoc it = namespaceMap.entrySet().iterator();
81       while (it.hasNext())
82       {
83          Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
84          if (prefix.equals(entry.getValue()))
85             return (String JavaDoc)entry.getKey();
86       }
87       return null;
88    }
89
90    public Iterator JavaDoc getRegisteredNamespaceURIs()
91    {
92       return namespaceMap.keySet().iterator();
93    }
94 }
95
Popular Tags