1 package org.exolab.castor.xml.parsing;
2
3 import java.util.Enumeration;
4 import java.util.HashMap;
5
6 import org.apache.commons.lang.StringUtils;
7 import org.exolab.castor.mapping.FieldHandler;
8 import org.exolab.castor.mapping.MapItem;
9 import org.exolab.castor.xml.*;
10 import org.xml.sax.SAXException;
11
12
13
14
15
16
17
18
19
20
21
22 public class NamespaceHandling {
23
24
25
26
27 private NamespacesStack namespacesStack = new NamespacesStack();
28
29
30
31
32 private HashMap<String, String> _namespaceToPackage = new HashMap<String, String>();
33
34
35
36
37 private boolean _createNamespaceScope = true;
38
39
40
41
42
43
44 private static final String XML_PREFIX = "xml";
45
46
47
48
49
50
51
52
53
54 public void addNamespaceToPackageMapping(String nsURI, String packageName) {
55 _namespaceToPackage.put(StringUtils.defaultString(nsURI), StringUtils
56 .defaultString(packageName));
57
58 }
59
60
61
62
63
64
65
66
67 public String getMappedPackage(final String namespace) {
68 return _namespaceToPackage.get(StringUtils.defaultString(namespace));
69 }
70
71
72
73
74
75
76
77
78
79 public void processNamespaces(XMLClassDescriptor classDesc, Object object) {
80
81 if (classDesc == null) {
82 return;
83 }
84
85
86 XMLFieldDescriptor nsDescriptor = classDesc.getFieldDescriptor(null,
87 null, NodeType.Namespace);
88
89 if (nsDescriptor != null) {
90 FieldHandler handler = nsDescriptor.getHandler();
91 if (handler != null) {
92 Enumeration<String> enumeration = namespacesStack
93 .getLocalNamespacePrefixes();
94 while (enumeration.hasMoreElements()) {
95 String nsPrefix = StringUtils.defaultString(enumeration
96 .nextElement());
97 String nsURI = StringUtils.defaultString(namespacesStack
98 .getNamespaceURI(nsPrefix));
99 MapItem mapItem = new MapItem(nsPrefix, nsURI);
100 handler.setValue(object, mapItem);
101 }
102 }
103 }
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117 public Object resolveNamespace(Object value) throws SAXException {
118
119 if ((value == null) || !(value instanceof String)) {
120 return value;
121 }
122
123 String result = (String) value;
124 int idx = result.indexOf(':');
125 String prefix = null;
126 if (idx > 0) {
127 prefix = result.substring(0, idx);
128 if (XML_PREFIX.equals(prefix)) {
129
130 return value;
131 }
132 result = result.substring(idx + 1);
133 }
134 String namespace = getNamespaceURI(prefix);
135 if (StringUtils.isNotEmpty(namespace)) {
136 result = '{' + namespace + '}' + result;
137 return result;
138 } else if ((namespace == null) && (prefix != null)) {
139 throw new SAXException(
140 "The namespace associated with the prefix: '" + prefix
141 + "' is null.");
142 } else {
143 return result;
144 }
145
146 }
147
148
149
150
151 public void removeCurrentNamespaceInstance() {
152 namespacesStack.removeNamespaceScope();
153 }
154
155
156
157
158
159
160
161 public void addDefaultNamespace(String namespaceURI) {
162 namespacesStack.addDefaultNamespace(namespaceURI);
163 }
164
165
166
167
168
169
170
171
172
173 public void addNamespace(String prefix, String namespaceURI) {
174 namespacesStack.addNamespace(prefix, namespaceURI);
175
176 }
177
178
179
180
181
182
183
184
185 public String getNamespacePrefix(String namespaceURI) {
186 return namespacesStack.getNamespacePrefix(namespaceURI);
187 }
188
189
190
191
192
193
194
195
196 public String getNamespaceURI(String prefix) {
197 return namespacesStack.getNamespaceURI(prefix);
198 }
199
200
201
202
203
204
205 public String getDefaultNamespaceURI() {
206 return namespacesStack.getDefaultNamespaceURI();
207 }
208
209
210
211
212 public void createNamespace() {
213 namespacesStack.addNewNamespaceScope();
214 }
215
216
217
218
219
220 public NamespacesStack getNamespaceStack() {
221 return namespacesStack;
222 }
223
224
225
226
227
228
229 public boolean isNewNamespaceScopeNecessary() {
230 if (_createNamespaceScope) {
231 return true;
232 }
233 return false;
234 }
235
236
237
238
239 public void startNamespaceScope() {
240 createNamespace();
241 _createNamespaceScope = true;
242 }
243
244
245
246
247 public void stopNamespaceScope() {
248 createNamespace();
249 _createNamespaceScope = false;
250 }
251
252 public void setNewNamespaceScopeNecessary(boolean value) {
253 _createNamespaceScope = value;
254
255 }
256 }