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 javax.xml.XMLConstants;
26  import javax.xml.namespace.NamespaceContext;
27  
28  import org.springframework.util.Assert;
29  
30  /**
31   * Simple <code>javax.xml.namespace.NamespaceContext</code> implementation. Follows the standard
32   * <code>NamespaceContext</code> contract, and is loadable via a <code>java.util.Map</code> or
33   * <code>java.util.Properties</code> object
34   *
35   * @author Arjen Poutsma
36   * @since 1.0.0
37   */
38  public class SimpleNamespaceContext implements NamespaceContext {
39  
40      private Map prefixToNamespaceUri = new HashMap();
41  
42      /** Maps a <code>String</code> namespaceUri to a <code>List</code> of prefixes */
43      private Map namespaceUriToPrefixes = new HashMap();
44  
45      private String defaultNamespaceUri = "";
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 (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
56              return defaultNamespaceUri;
57          }
58          else if (prefixToNamespaceUri.containsKey(prefix)) {
59              return (String) prefixToNamespaceUri.get(prefix);
60          }
61          return "";
62      }
63  
64      public String getPrefix(String namespaceUri) {
65          List prefixes = getPrefixesInternal(namespaceUri);
66          return prefixes.isEmpty() ? null : (String) prefixes.get(0);
67      }
68  
69      public Iterator getPrefixes(String namespaceUri) {
70          return getPrefixesInternal(namespaceUri).iterator();
71      }
72  
73      /**
74       * Sets the bindings for this namespace context. The supplied map must consist of string key value pairs.
75       *
76       * @param bindings the bindings
77       */
78      public void setBindings(Map bindings) {
79          for (Iterator iterator = bindings.entrySet().iterator(); iterator.hasNext();) {
80              Map.Entry entry = (Map.Entry) iterator.next();
81              bindNamespaceUri((String) entry.getKey(), (String) entry.getValue());
82          }
83      }
84  
85      /**
86       * Binds the given namespace as default namespace.
87       *
88       * @param namespaceUri the namespace uri
89       */
90      public void bindDefaultNamespaceUri(String namespaceUri) {
91          bindNamespaceUri(XMLConstants.DEFAULT_NS_PREFIX, namespaceUri);
92      }
93  
94      /**
95       * Binds the given prefix to the given namespace.
96       *
97       * @param prefix       the namespace prefix
98       * @param namespaceUri the namespace uri
99       */
100     public void bindNamespaceUri(String prefix, String namespaceUri) {
101         Assert.notNull(prefix, "No prefix given");
102         Assert.notNull(namespaceUri, "No namespaceUri given");
103         if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
104             defaultNamespaceUri = namespaceUri;
105         }
106         else {
107             prefixToNamespaceUri.put(prefix, namespaceUri);
108             getPrefixesInternal(namespaceUri).add(prefix);
109         }
110     }
111 
112     /** Removes all declared prefixes. */
113     public void clear() {
114         prefixToNamespaceUri.clear();
115     }
116 
117     /**
118      * Returns all declared prefixes.
119      *
120      * @return the declared prefixes
121      */
122     public Iterator getBoundPrefixes() {
123         return prefixToNamespaceUri.keySet().iterator();
124     }
125 
126     private List getPrefixesInternal(String namespaceUri) {
127         if (defaultNamespaceUri.equals(namespaceUri)) {
128             return Collections.singletonList(XMLConstants.DEFAULT_NS_PREFIX);
129         }
130         else if (XMLConstants.XML_NS_URI.equals(namespaceUri)) {
131             return Collections.singletonList(XMLConstants.XML_NS_PREFIX);
132         }
133         else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceUri)) {
134             return Collections.singletonList(XMLConstants.XMLNS_ATTRIBUTE);
135         }
136         else {
137             List list = (List) namespaceUriToPrefixes.get(namespaceUri);
138             if (list == null) {
139                 list = new ArrayList();
140                 namespaceUriToPrefixes.put(namespaceUri, list);
141             }
142             return list;
143         }
144     }
145 
146     /**
147      * Removes the given prefix from this context.
148      *
149      * @param prefix the prefix to be removed
150      */
151     public void removeBinding(String prefix) {
152         if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
153             defaultNamespaceUri = "";
154         }
155         else {
156             String namespaceUri = (String) namespaceUriToPrefixes.get(prefix);
157             List prefixes = getPrefixesInternal(namespaceUri);
158             prefixes.remove(prefix);
159             namespaceUriToPrefixes.remove(prefixes);
160         }
161     }
162 }