View Javadoc
1   /*
2    * Copyright 2005-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.xml.namespace;
18  
19  import java.util.Collections;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.LinkedHashMap;
23  import java.util.LinkedHashSet;
24  import java.util.Map;
25  import java.util.Set;
26  import javax.xml.XMLConstants;
27  import javax.xml.namespace.NamespaceContext;
28  
29  import org.springframework.util.Assert;
30  
31  /**
32   * Simple <code>javax.xml.namespace.NamespaceContext</code> implementation. Follows the standard
33   * <code>NamespaceContext</code> contract, and is loadable via a <code>java.util.Map</code> or
34   * <code>java.util.Properties</code> object
35   *
36   * @author Arjen Poutsma
37   * @since 1.0.0
38   */
39  public class SimpleNamespaceContext implements NamespaceContext {
40  
41      private Map<String, String> prefixToNamespaceUri = new LinkedHashMap<String, String>();
42  
43      private Map<String, Set<String>> namespaceUriToPrefixes = new LinkedHashMap<String, Set<String>>();
44  
45      public String getNamespaceURI(String prefix) {
46          Assert.notNull(prefix, "prefix is null");
47          if (XMLConstants.XML_NS_PREFIX.equals(prefix)) {
48              return XMLConstants.XML_NS_URI;
49          }
50          else if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) {
51              return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
52          }
53          else if (prefixToNamespaceUri.containsKey(prefix)) {
54              return prefixToNamespaceUri.get(prefix);
55          }
56          return XMLConstants.NULL_NS_URI;
57      }
58  
59      public String getPrefix(String namespaceUri) {
60          Iterator<String> iterator = getPrefixes(namespaceUri);
61          return iterator.hasNext() ? iterator.next() : null;
62      }
63  
64      public Iterator<String> getPrefixes(String namespaceUri) {
65          Set<String> prefixes = getPrefixesInternal(namespaceUri);
66          prefixes = Collections.unmodifiableSet(prefixes);
67          return prefixes.iterator();
68      }
69  
70      /**
71       * Sets the bindings for this namespace context. The supplied map must consist of string key value pairs.
72       *
73       * @param bindings the bindings
74       */
75      public void setBindings(Map<String, String> bindings) {
76          for (Map.Entry<String, String> entry : bindings.entrySet()) {
77              bindNamespaceUri(entry.getKey(), entry.getValue());
78          }
79      }
80  
81      /**
82       * Binds the given namespace as default namespace.
83       *
84       * @param namespaceUri the namespace uri
85       */
86      public void bindDefaultNamespaceUri(String namespaceUri) {
87          bindNamespaceUri(XMLConstants.DEFAULT_NS_PREFIX, namespaceUri);
88      }
89  
90      /**
91       * Binds the given prefix to the given namespace.
92       *
93       * @param prefix       the namespace prefix
94       * @param namespaceUri the namespace uri
95       */
96      public void bindNamespaceUri(String prefix, String namespaceUri) {
97          Assert.notNull(prefix, "No prefix given");
98          Assert.notNull(namespaceUri, "No namespaceUri given");
99          if (XMLConstants.XML_NS_PREFIX.equals(prefix)) {
100             Assert.isTrue(XMLConstants.XML_NS_URI.equals(namespaceUri), "Prefix \"" + prefix +
101                     "\" bound to namespace \"" + namespaceUri + "\" (should be \"" + XMLConstants.XML_NS_URI + "\")");
102         } else if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) {
103             Assert.isTrue(XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceUri), "Prefix \"" + prefix +
104                     "\" bound to namespace \"" + namespaceUri + "\" (should be \"" +
105                     XMLConstants.XMLNS_ATTRIBUTE_NS_URI + "\")");
106         }
107         else {
108             prefixToNamespaceUri.put(prefix, namespaceUri);
109             getPrefixesInternal(namespaceUri).add(prefix);
110         }
111     }
112 
113     /** Removes all declared prefixes. */
114     public void clear() {
115         prefixToNamespaceUri.clear();
116         namespaceUriToPrefixes.clear();
117     }
118 
119     /**
120      * Returns all declared prefixes.
121      *
122      * @return the declared prefixes
123      */
124     public Iterator<String> getBoundPrefixes() {
125         Set<String> prefixes = new HashSet<String>(prefixToNamespaceUri.keySet());
126         prefixes.remove(XMLConstants.DEFAULT_NS_PREFIX);
127         prefixes = Collections.unmodifiableSet(prefixes);
128         return prefixes.iterator();
129     }
130 
131     private Set<String> getPrefixesInternal(String namespaceUri) {
132         if (XMLConstants.XML_NS_URI.equals(namespaceUri)) {
133             return Collections.singleton(XMLConstants.XML_NS_PREFIX);
134         }
135         else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceUri)) {
136             return Collections.singleton(XMLConstants.XMLNS_ATTRIBUTE);
137         }
138         else {
139             Set<String> set = namespaceUriToPrefixes.get(namespaceUri);
140             if (set == null) {
141                 set = new LinkedHashSet<String>();
142                 namespaceUriToPrefixes.put(namespaceUri, set);
143             }
144             return set;
145         }
146     }
147 
148     /**
149      * Removes the given prefix from this context.
150      *
151      * @param prefix the prefix to be removed
152      */
153     public void removeBinding(String prefix) {
154         String namespaceUri = prefixToNamespaceUri.remove(prefix);
155         if (namespaceUri != null) {
156             Set<String> prefixes = getPrefixesInternal(namespaceUri);
157             prefixes.remove(prefix);
158         }
159     }
160 
161     public boolean hasBinding(String prefix) {
162         return prefixToNamespaceUri.containsKey(prefix);
163     }
164 }