1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
34
35
36
37
38
39
40 public class SimpleNamespaceContext implements NamespaceContext {
41
42 private Map prefixToNamespaceUri = new HashMap();
43
44
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
72
73
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
84
85
86
87 public void bindDefaultNamespaceUri(String namespaceUri) {
88 bindNamespaceUri(XMLConstants.DEFAULT_NS_PREFIX, namespaceUri);
89 }
90
91
92
93
94
95
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
115 public void clear() {
116 prefixToNamespaceUri.clear();
117 namespaceUriToPrefixes.clear();
118 }
119
120
121
122
123
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
150
151
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 }