View Javadoc

1   /* The contents of this file are subject to the license and copyright terms
2    * detailed in the license directory at the root of the source tree (also 
3    * available online at http://www.fedora.info/license/).
4    */
5   package org.fedoracommons.funapi.utilities;
6   
7   import java.util.ArrayList;
8   import java.util.Collections;
9   import java.util.Iterator;
10  import java.util.List;
11  import java.util.Map;
12  import java.util.concurrent.ConcurrentHashMap;
13  
14  import javax.xml.XMLConstants;
15  import javax.xml.namespace.NamespaceContext;
16  
17  
18  /**
19   * An implementation of {@link javax.xml.namespace.NamespaceContext NamespaceContext} that provides
20   * an addNamespace method.
21   * 
22   * @author Edwin Shin
23   * @version $Id: NamespaceContextImpl.java 33 2008-10-25 19:31:36Z pangloss $
24   */
25  public class NamespaceContextImpl
26          implements NamespaceContext {
27      
28      private Map<String, String> prefix2ns = new ConcurrentHashMap<String, String>();
29      
30      public NamespaceContextImpl() {
31          prefix2ns.put(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
32          prefix2ns.put(XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
33      }
34      
35      public NamespaceContextImpl(String prefix, String namespaceURI) {
36          this();
37          this.addNamespace(prefix, namespaceURI);
38      }
39      
40      /**
41       * Constructor that takes a Map of prefix to namespaces.
42       * 
43       * @param prefix2ns a mapping of prefixes to namespaces.
44       * @throws IllegalArgumentException if prefix2ns contains 
45       * {@value javax.xml.XMLConstants#XML_NS_URI} or 
46       * {@value javax.xml.XMLConstants#XMLNS_ATTRIBUTE_NS_URI}
47       */
48      public NamespaceContextImpl(Map<String, String> prefix2ns) {
49          this();
50          for (String prefix : prefix2ns.keySet()) {
51              addNamespace(prefix, prefix2ns.get(prefix));
52          }
53      }
54      
55      /**
56       * {@inheritDoc}
57       */
58      public String getNamespaceURI(String prefix) {
59          if (prefix == null) {
60              throw new IllegalArgumentException("null prefix not allowed.");
61          }
62          if (prefix2ns.containsKey(prefix)) {
63              return prefix2ns.get(prefix);
64          }
65          return XMLConstants.NULL_NS_URI;
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      public String getPrefix(String namespaceURI) {
72          if (namespaceURI == null) {
73              throw new IllegalArgumentException("null namespaceURI not allowed.");
74          }
75          for (String prefix : prefix2ns.keySet()) {
76              if (prefix2ns.get(prefix).equals(namespaceURI)) {
77                  return prefix;
78              }
79          }
80          return null;
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      public Iterator<String> getPrefixes(String namespaceURI) {
87          List<String> prefixes = new ArrayList<String>();
88          for (String prefix : prefix2ns.keySet()) {
89              if (prefix2ns.containsKey(prefix) && 
90                      prefix2ns.get(prefix).equals(namespaceURI)) {
91                  prefixes.add(prefix);
92              }
93          }
94          return Collections.unmodifiableList(prefixes).iterator();
95      }
96      
97      /**
98       * Add a prefix to namespace mapping.
99       * 
100      * @param prefix
101      * @param namespaceURI
102      * @throws IllegalArgumentException if namespaceURI is one of 
103      * {@value javax.xml.XMLConstants#XML_NS_URI} or 
104      * {@value javax.xml.XMLConstants#XMLNS_ATTRIBUTE_NS_URI}
105      */
106     public void addNamespace(String prefix, String namespaceURI) {
107         if (prefix == null || namespaceURI == null) {
108             throw new IllegalArgumentException("null arguments not allowed.");
109         }
110         if (namespaceURI.equals(XMLConstants.XML_NS_URI)) {
111             throw new IllegalArgumentException("Adding a new namespace for " +
112                 XMLConstants.XML_NS_URI + "not allowed.");
113         } else if (namespaceURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
114             throw new IllegalArgumentException("Adding a new namespace for " +
115                 XMLConstants.XMLNS_ATTRIBUTE_NS_URI + "not allowed.");
116         }
117         prefix2ns.put(prefix, namespaceURI);
118     }
119 }