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