KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.xb.binding;
23
24 import javax.xml.namespace.NamespaceContext JavaDoc;
25 import javax.xml.namespace.QName JavaDoc;
26
27 import java.io.Serializable JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.ArrayList JavaDoc;
34
35 /**
36  * A simple namespace registry.
37  *
38  * It assignes namespace prefixes of the form 'ns?' where ? is an incrementing integer.
39  * {@see registerURI(String,String)}
40  *
41  * [TODO] cleanup the api
42  *
43  * @author Thomas.Diesler@jboss.org
44  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
45  * @author <a HREF="mailto:anil.saldhana@jboss.org">Anil Saldhana</a>
46  * @since 08-June-2004
47  */

48 public class NamespaceRegistry implements NamespaceContext JavaDoc, Serializable JavaDoc
49 {
50    private static final long serialVersionUID = 8435680858785550261L;
51
52    // The index of the last assigned prefix
53
private int namespaceIndex;
54
55    private final Map JavaDoc prefixByUri = new HashMap JavaDoc();
56    private final Map JavaDoc uriByPrefix = new HashMap JavaDoc();
57
58    public NamespaceRegistry()
59    {
60    }
61
62    /** Register a QName and return a QName that is guarantied to have a prefix
63     */

64    public QName JavaDoc registerQName(QName JavaDoc qname)
65    {
66       if (qname == null)
67          return null;
68
69       String JavaDoc nsURI = qname.getNamespaceURI();
70       String JavaDoc 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 JavaDoc(nsURI, qname.getLocalPart(), prefix);
81       return qname;
82    }
83
84    /** Register the given nsURI/prefix combination.
85     * In case the prefix is null, it will be assigend.
86     *
87     * @param nsURI The nsURI
88     * @param prefix The corresponding prefix, maybe null
89     * @return A prefix, never null
90     */

91    public String JavaDoc registerURI(String JavaDoc nsURI, String JavaDoc prefix)
92    {
93       if (prefix == null)
94       {
95          prefix = "ns" + (++namespaceIndex);
96       }
97
98       addPrefixMapping(prefix, nsURI);
99       return prefix;
100    }
101
102    /**
103     * Adds prefix mapping.
104     *
105     * @param prefix prefix to map
106     * @param nsURI the URI to prefix to
107     */

108    public void addPrefixMapping(String JavaDoc prefix, String JavaDoc nsURI)
109    {
110       if (nsURI == null)
111          throw new IllegalArgumentException JavaDoc("Cannot add mapping for null namespace URI");
112
113       Object JavaDoc 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 JavaDoc)
121          {
122             List JavaDoc list = new ArrayList JavaDoc();
123             list.add(obj);
124             list.add(nsURI);
125             uriByPrefix.put(prefix, list);
126          }
127          else if (obj instanceof List JavaDoc)
128          {
129             ((List JavaDoc)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 JavaDoc)
142          {
143             List JavaDoc list = new ArrayList JavaDoc();
144             list.add(obj);
145             list.add(prefix);
146             prefixByUri.put(nsURI, list);
147          }
148          else if (obj instanceof List JavaDoc)
149          {
150             ((List JavaDoc)obj).add(prefix);
151          }
152          else
153          {
154             throwUnexpectedEntryException(obj);
155          }
156       }
157    }
158
159    /**
160     * Removes the last mapping for the given prefix.
161     *
162     * @param prefix the prefix to remove mapping for
163     */

164    public void removePrefixMapping(String JavaDoc prefix)
165    {
166       Object JavaDoc obj = uriByPrefix.get(prefix);
167       if (obj != null)
168       {
169          String JavaDoc uri = null;
170          if (obj instanceof String JavaDoc)
171          {
172             uri = (String JavaDoc)obj;
173             uriByPrefix.remove(prefix);
174          }
175          else if (obj instanceof List JavaDoc)
176          {
177             List JavaDoc list = (List JavaDoc)obj;
178             uri = (String JavaDoc)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 JavaDoc)
193             {
194                if (!prefix.equals(obj))
195                {
196                   throw new IllegalStateException JavaDoc("Inconsistent mapping: prefix=" + prefix + ", found=" + obj);
197                }
198                prefixByUri.remove(uri);
199             }
200             else if (obj instanceof List JavaDoc)
201             {
202                List JavaDoc list = (ArrayList JavaDoc)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    /**
218     * Unregisters all prefix mappings for the given URI, not just the last one added.
219     * todo what is this used for?
220     *
221     * @param nsURI the URI to unregister
222     */

223    public void unregisterURI(String JavaDoc nsURI)
224    {
225       Object JavaDoc obj = prefixByUri.get(nsURI);
226       if (obj != null)
227       {
228          String JavaDoc prefix = null;
229          if (obj instanceof String JavaDoc)
230          {
231             prefix = (String JavaDoc)obj;
232             prefixByUri.remove(nsURI);
233             removePrefixMappingOnly(prefix, nsURI);
234          }
235          else if (obj instanceof List JavaDoc)
236          {
237             List JavaDoc list = (List JavaDoc)obj;
238             for (int i = 0; i < list.size(); ++i)
239             {
240                removePrefixMappingOnly((String JavaDoc)list.get(i), nsURI);
241             }
242             prefixByUri.remove(nsURI);
243          }
244          else
245          {
246             throwUnexpectedEntryException(obj);
247          }
248       }
249    }
250
251    /** True if the given nsURI is registered.
252     */

253    public boolean isRegistered(String JavaDoc nsURI)
254    {
255       return prefixByUri.containsKey(nsURI);
256    }
257
258    /** Return an iterator over all registered nsURIs.
259     */

260    public Iterator JavaDoc getRegisteredURIs()
261    {
262       return prefixByUri.keySet().iterator();
263    }
264
265    /** Return an iterator over all registered nsURIs.
266     */

267    public Iterator JavaDoc getRegisteredPrefixes()
268    {
269       return uriByPrefix.keySet().iterator();
270    }
271
272    /**
273     * @return the number of registered URIs
274     */

275    public int size()
276    {
277       return prefixByUri.size();
278    }
279
280    // NamespaceContext implementation
281

282    /**
283     * Returns the last mapping for the prefix or null if the prefix was not mapped.
284     */

285    public String JavaDoc getPrefix(String JavaDoc nsURI)
286    {
287       Object JavaDoc obj = prefixByUri.get(nsURI);
288
289       String JavaDoc prefix = null;
290       if (obj != null)
291       {
292          if (obj instanceof String JavaDoc)
293          {
294             prefix = (String JavaDoc)obj;
295          }
296          else if (obj instanceof List JavaDoc)
297          {
298             List JavaDoc list = (List JavaDoc)obj;
299             prefix = (String JavaDoc)list.get(list.size() - 1);
300          }
301          else
302          {
303             throwUnexpectedEntryException(obj);
304          }
305       }
306
307       return prefix;
308    }
309
310    /**
311     * Returns all prefixes for the given URI.
312     *
313     * @param namespaceURI the URI to return prefixes for
314     * @return prefixes mapped to the URI
315     */

316    public Iterator JavaDoc getPrefixes(String JavaDoc namespaceURI)
317    {
318       Object JavaDoc obj = prefixByUri.get(namespaceURI);
319
320       Iterator JavaDoc result = null;
321       if (obj == null)
322       {
323          result = Collections.EMPTY_LIST.iterator();
324       }
325       else if (obj instanceof String JavaDoc)
326       {
327          result = Collections.singletonList(obj).iterator();
328       }
329       else if (obj instanceof List JavaDoc)
330       {
331          result = ((List JavaDoc)obj).iterator();
332       }
333       else
334       {
335          throwUnexpectedEntryException(obj);
336       }
337
338       return result;
339    }
340
341    /** Get the nsURI for a given prefix, maybe null.
342     */

343    public String JavaDoc getNamespaceURI(String JavaDoc prefix)
344    {
345       Object JavaDoc obj = uriByPrefix.get(prefix);
346
347       String JavaDoc uri = null;
348       if (obj != null)
349       {
350          if (obj instanceof String JavaDoc)
351          {
352             uri = (String JavaDoc)obj;
353          }
354          else if (obj instanceof List JavaDoc)
355          {
356             List JavaDoc list = (List JavaDoc)obj;
357             uri = (String JavaDoc)list.get(list.size() - 1);
358          }
359          else
360          {
361             throwUnexpectedEntryException(obj);
362          }
363       }
364
365       return uri;
366    }
367
368    // Private
369

370    private void removePrefixMappingOnly(String JavaDoc prefix, String JavaDoc nsURI)
371    {
372       Object JavaDoc obj = uriByPrefix.get(prefix);
373       if (obj instanceof String JavaDoc)
374       {
375          if (!obj.equals(nsURI))
376          {
377             throw new IllegalStateException JavaDoc("Inconsistent mapping: uri=" + nsURI + ", found=" + obj);
378          }
379          uriByPrefix.remove(prefix);
380       }
381       else if (obj instanceof List JavaDoc)
382       {
383          List JavaDoc list = (List JavaDoc)obj;
384          list.remove(prefix);
385          if (list.isEmpty())
386          {
387             uriByPrefix.remove(prefix);
388          }
389       }
390    }
391
392    private void throwUnexpectedEntryException(Object JavaDoc entry)
393    {
394       throw new IllegalStateException JavaDoc("Unexpected entry type: expected java.lang.String or java.util.List but got " + entry.getClass());
395    }
396 }
397
Popular Tags