1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .util .xml ;
18
18
19
- import java .util .ArrayList ;
20
19
import java .util .Collections ;
21
20
import java .util .HashMap ;
22
21
import java .util .Iterator ;
23
- import java .util .List ;
22
+ import java .util .LinkedHashSet ;
24
23
import java .util .Map ;
24
+ import java .util .Set ;
25
25
import javax .xml .XMLConstants ;
26
26
import javax .xml .namespace .NamespaceContext ;
27
27
28
28
import org .springframework .util .Assert ;
29
29
30
30
/**
31
- * Simple {@code javax.xml.namespace.NamespaceContext} implementation. Follows the standard
32
- * {@code NamespaceContext} contract, and is loadable via a {@code java.util.Map} or
33
- * {@code java.util.Properties} object
31
+ * Simple {@code javax.xml.namespace.NamespaceContext} implementation.
32
+ * Follows the standard {@code NamespaceContext} contract, and is loadable
33
+ * via a {@code java.util.Map} or {@code java.util.Properties} object
34
34
*
35
35
* @author Arjen Poutsma
36
+ * @author Juergen Hoeller
36
37
* @since 3.0
37
38
*/
38
39
public class SimpleNamespaceContext implements NamespaceContext {
39
40
40
- private Map <String , String > prefixToNamespaceUri = new HashMap <String , String >();
41
+ private final Map <String , String > prefixToNamespaceUri = new HashMap <String , String >();
41
42
42
- private Map <String , List <String >> namespaceUriToPrefixes = new HashMap <String , List <String >>();
43
+ private final Map <String , Set <String >> namespaceUriToPrefixes = new HashMap <String , Set <String >>();
43
44
44
45
private String defaultNamespaceUri = "" ;
45
46
47
+
46
48
@ Override
47
49
public String getNamespaceURI (String prefix ) {
48
- Assert .notNull (prefix , "prefix is null " );
50
+ Assert .notNull (prefix , "No prefix given " );
49
51
if (XMLConstants .XML_NS_PREFIX .equals (prefix )) {
50
52
return XMLConstants .XML_NS_URI ;
51
53
}
52
54
else if (XMLConstants .XMLNS_ATTRIBUTE .equals (prefix )) {
53
55
return XMLConstants .XMLNS_ATTRIBUTE_NS_URI ;
54
56
}
55
57
else if (XMLConstants .DEFAULT_NS_PREFIX .equals (prefix )) {
56
- return defaultNamespaceUri ;
58
+ return this . defaultNamespaceUri ;
57
59
}
58
- else if (prefixToNamespaceUri .containsKey (prefix )) {
59
- return prefixToNamespaceUri .get (prefix );
60
+ else if (this . prefixToNamespaceUri .containsKey (prefix )) {
61
+ return this . prefixToNamespaceUri .get (prefix );
60
62
}
61
63
return "" ;
62
64
}
63
65
64
66
@ Override
65
67
public String getPrefix (String namespaceUri ) {
66
- List <? > prefixes = getPrefixesInternal (namespaceUri );
67
- return prefixes .isEmpty () ? null : ( String ) prefixes . get ( 0 );
68
+ Set < String > prefixes = getPrefixesSet (namespaceUri );
69
+ return (! prefixes .isEmpty () ? prefixes . iterator (). next () : null );
68
70
}
69
71
70
72
@ Override
71
73
public Iterator <String > getPrefixes (String namespaceUri ) {
72
- return getPrefixesInternal (namespaceUri ).iterator ();
74
+ return getPrefixesSet (namespaceUri ).iterator ();
73
75
}
74
76
77
+ private Set <String > getPrefixesSet (String namespaceUri ) {
78
+ Assert .notNull (namespaceUri , "No namespaceUri given" );
79
+ if (this .defaultNamespaceUri .equals (namespaceUri )) {
80
+ return Collections .singleton (XMLConstants .DEFAULT_NS_PREFIX );
81
+ }
82
+ else if (XMLConstants .XML_NS_URI .equals (namespaceUri )) {
83
+ return Collections .singleton (XMLConstants .XML_NS_PREFIX );
84
+ }
85
+ else if (XMLConstants .XMLNS_ATTRIBUTE_NS_URI .equals (namespaceUri )) {
86
+ return Collections .singleton (XMLConstants .XMLNS_ATTRIBUTE );
87
+ }
88
+ else {
89
+ Set <String > prefixes = this .namespaceUriToPrefixes .get (namespaceUri );
90
+ return (prefixes != null ? Collections .unmodifiableSet (prefixes ) : Collections .<String >emptySet ());
91
+ }
92
+ }
93
+
94
+
75
95
/**
76
- * Sets the bindings for this namespace context. The supplied map must consist of string key value pairs.
77
- *
78
- * @param bindings the bindings
96
+ * Set the bindings for this namespace context.
97
+ * The supplied map must consist of string key value pairs.
79
98
*/
80
99
public void setBindings (Map <String , String > bindings ) {
81
100
for (Map .Entry <String , String > entry : bindings .entrySet ()) {
@@ -84,79 +103,70 @@ public void setBindings(Map<String, String> bindings) {
84
103
}
85
104
86
105
/**
87
- * Binds the given namespace as default namespace.
88
- *
106
+ * Bind the given namespace as default namespace.
89
107
* @param namespaceUri the namespace uri
90
108
*/
91
109
public void bindDefaultNamespaceUri (String namespaceUri ) {
92
110
bindNamespaceUri (XMLConstants .DEFAULT_NS_PREFIX , namespaceUri );
93
111
}
94
112
95
113
/**
96
- * Binds the given prefix to the given namespace.
97
- *
98
- * @param prefix the namespace prefix
114
+ * Bind the given prefix to the given namespace.
115
+ * @param prefix the namespace prefix
99
116
* @param namespaceUri the namespace uri
100
117
*/
101
118
public void bindNamespaceUri (String prefix , String namespaceUri ) {
102
119
Assert .notNull (prefix , "No prefix given" );
103
120
Assert .notNull (namespaceUri , "No namespaceUri given" );
104
121
if (XMLConstants .DEFAULT_NS_PREFIX .equals (prefix )) {
105
- defaultNamespaceUri = namespaceUri ;
122
+ this . defaultNamespaceUri = namespaceUri ;
106
123
}
107
124
else {
108
- prefixToNamespaceUri .put (prefix , namespaceUri );
109
- getPrefixesInternal (namespaceUri ).add (prefix );
125
+ this .prefixToNamespaceUri .put (prefix , namespaceUri );
126
+ Set <String > prefixes = this .namespaceUriToPrefixes .get (namespaceUri );
127
+ if (prefixes == null ) {
128
+ prefixes = new LinkedHashSet <String >();
129
+ this .namespaceUriToPrefixes .put (namespaceUri , prefixes );
130
+ }
131
+ prefixes .add (prefix );
110
132
}
111
133
}
112
134
113
- /** Removes all declared prefixes. */
114
- public void clear () {
115
- prefixToNamespaceUri .clear ();
116
- }
117
-
118
135
/**
119
- * Returns all declared prefixes.
120
- *
121
- * @return the declared prefixes
136
+ * Remove the given prefix from this context.
137
+ * @param prefix the prefix to be removed
122
138
*/
123
- public Iterator <String > getBoundPrefixes () {
124
- return prefixToNamespaceUri .keySet ().iterator ();
125
- }
126
-
127
- private List <String > getPrefixesInternal (String namespaceUri ) {
128
- if (defaultNamespaceUri .equals (namespaceUri )) {
129
- return Collections .singletonList (XMLConstants .DEFAULT_NS_PREFIX );
130
- }
131
- else if (XMLConstants .XML_NS_URI .equals (namespaceUri )) {
132
- return Collections .singletonList (XMLConstants .XML_NS_PREFIX );
133
- }
134
- else if (XMLConstants .XMLNS_ATTRIBUTE_NS_URI .equals (namespaceUri )) {
135
- return Collections .singletonList (XMLConstants .XMLNS_ATTRIBUTE );
139
+ public void removeBinding (String prefix ) {
140
+ if (XMLConstants .DEFAULT_NS_PREFIX .equals (prefix )) {
141
+ this .defaultNamespaceUri = "" ;
136
142
}
137
- else {
138
- List <String > list = namespaceUriToPrefixes .get (namespaceUri );
139
- if (list == null ) {
140
- list = new ArrayList <String >();
141
- namespaceUriToPrefixes .put (namespaceUri , list );
143
+ else if (prefix != null ) {
144
+ String namespaceUri = this .prefixToNamespaceUri .remove (prefix );
145
+ if (namespaceUri != null ) {
146
+ Set <String > prefixes = this .namespaceUriToPrefixes .get (namespaceUri );
147
+ if (prefixes != null ) {
148
+ prefixes .remove (prefix );
149
+ if (prefixes .isEmpty ()) {
150
+ this .namespaceUriToPrefixes .remove (namespaceUri );
151
+ }
152
+ }
142
153
}
143
- return list ;
144
154
}
145
155
}
146
156
147
157
/**
148
- * Removes the given prefix from this context.
149
- *
150
- * @param prefix the prefix to be removed
158
+ * Remove all declared prefixes.
151
159
*/
152
- public void removeBinding (String prefix ) {
153
- if (XMLConstants .DEFAULT_NS_PREFIX .equals (prefix )) {
154
- defaultNamespaceUri = "" ;
155
- }
156
- else {
157
- String namespaceUri = prefixToNamespaceUri .remove (prefix );
158
- List <String > prefixes = getPrefixesInternal (namespaceUri );
159
- prefixes .remove (prefix );
160
- }
160
+ public void clear () {
161
+ this .prefixToNamespaceUri .clear ();
162
+ this .namespaceUriToPrefixes .clear ();
163
+ }
164
+
165
+ /**
166
+ * Return all declared prefixes.
167
+ */
168
+ public Iterator <String > getBoundPrefixes () {
169
+ return this .prefixToNamespaceUri .keySet ().iterator ();
161
170
}
171
+
162
172
}
0 commit comments