View Javadoc

1   /*
2    * Copyright 2006 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.ArrayList;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.HashSet;
27  import javax.xml.XMLConstants;
28  import javax.xml.namespace.NamespaceContext;
29  
30  import org.springframework.util.Assert;
31  
32  /**
33   * Simple <code>javax.xml.namespace.NamespaceContext</code> implementation. Follows the standard
34   * <code>NamespaceContext</code> contract, and is loadable via a <code>java.util.Map</code> or
35   * <code>java.util.Properties</code> object
36   *
37   * @author Arjen Poutsma
38   * @since 1.0.0
39   */
40  public class SimpleNamespaceContext implements NamespaceContext {
41  
42      private Map prefixToNamespaceUri = new HashMap();
43  
44      /** Maps a <code>String</code> namespaceUri to a <code>List</code> of prefixes */
45      private Map namespaceUriToPrefixes = new HashMap();
46  
47      public String getNamespaceURI(String prefix) {
48          Assert.notNull(prefix, "prefix is null");
49          if (XMLConstants.XML_NS_PREFIX.equals(prefix)) {
50              return XMLConstants.XML_NS_URI;
51          }
52          else if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) {
53              return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
54          }
55          else if (prefixToNamespaceUri.containsKey(prefix)) {
56              return (String) prefixToNamespaceUri.get(prefix);
57          }
58          return XMLConstants.NULL_NS_URI;
59      }
60  
61      public String getPrefix(String namespaceUri) {
62          List prefixes = getPrefixesInternal(namespaceUri);
63          return prefixes.isEmpty() ? null : (String) prefixes.get(0);
64      }
65  
66      public Iterator getPrefixes(String namespaceUri) {
67          return getPrefixesInternal(namespaceUri).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 bindings) {
76          for (Iterator iterator = bindings.entrySet().iterator(); iterator.hasNext();) {
77              Map.Entry entry = (Map.Entry) iterator.next();
78              bindNamespaceUri((String) entry.getKey(), (String) entry.getValue());
79          }
80      }
81  
82      /**
83       * Binds the given namespace as default namespace.
84       *
85       * @param namespaceUri the namespace uri
86       */
87      public void bindDefaultNamespaceUri(String namespaceUri) {
88          bindNamespaceUri(XMLConstants.DEFAULT_NS_PREFIX, namespaceUri);
89      }
90  
91      /**
92       * Binds the given prefix to the given namespace.
93       *
94       * @param prefix       the namespace prefix
95       * @param namespaceUri the namespace uri
96       */
97      public void bindNamespaceUri(String prefix, String namespaceUri) {
98          Assert.notNull(prefix, "No prefix given");
99          Assert.notNull(namespaceUri, "No namespaceUri given");
100         if (XMLConstants.XML_NS_PREFIX.equals(prefix)) {
101             Assert.isTrue(XMLConstants.XML_NS_URI.equals(namespaceUri), "Prefix \"" + prefix +
102                     "\" bound to namespace \"" + namespaceUri + "\" (should be \"" + XMLConstants.XML_NS_URI + "\")");
103         } else if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) {
104             Assert.isTrue(XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceUri), "Prefix \"" + prefix +
105                     "\" bound to namespace \"" + namespaceUri + "\" (should be \"" +
106                     XMLConstants.XMLNS_ATTRIBUTE_NS_URI + "\")");
107         }
108         else {
109             prefixToNamespaceUri.put(prefix, namespaceUri);
110             getPrefixesInternal(namespaceUri).add(prefix);
111         }
112     }
113 
114     /** Removes all declared prefixes. */
115     public void clear() {
116         prefixToNamespaceUri.clear();
117         namespaceUriToPrefixes.clear();
118     }
119 
120     /**
121      * Returns all declared prefixes.
122      *
123      * @return the declared prefixes
124      */
125     public Iterator getBoundPrefixes() {
126         Set prefixes = new HashSet(prefixToNamespaceUri.keySet());
127         prefixes.remove(XMLConstants.DEFAULT_NS_PREFIX);
128         return prefixes.iterator();
129     }
130 
131     private List getPrefixesInternal(String namespaceUri) {
132         if (XMLConstants.XML_NS_URI.equals(namespaceUri)) {
133             return Collections.singletonList(XMLConstants.XML_NS_PREFIX);
134         }
135         else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceUri)) {
136             return Collections.singletonList(XMLConstants.XMLNS_ATTRIBUTE);
137         }
138         else {
139             List list = (List) namespaceUriToPrefixes.get(namespaceUri);
140             if (list == null) {
141                 list = new ArrayList();
142                 namespaceUriToPrefixes.put(namespaceUri, list);
143             }
144             return list;
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 = (String) prefixToNamespaceUri.get(prefix);
155         List prefixes = getPrefixesInternal(namespaceUri);
156         prefixes.remove(prefix);
157     }
158 
159     public boolean hasBinding(String prefix) {
160         return prefixToNamespaceUri.containsKey(prefix);
161     }
162 }