1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 package org.exolab.castor.builder.binding;
46
47
48 import java.io.IOException;
49 import java.net.MalformedURLException;
50 import java.net.URL;
51 import java.util.Enumeration;
52
53 import org.exolab.castor.builder.binding.xml.AutomaticNamingType;
54 import org.exolab.castor.builder.binding.xml.Binding;
55 import org.exolab.castor.builder.binding.xml.ComponentBindingType;
56 import org.exolab.castor.builder.binding.xml.IncludeType;
57 import org.exolab.castor.builder.binding.xml.NamingXMLType;
58 import org.exolab.castor.builder.binding.xml.PackageType;
59 import org.exolab.castor.xml.MarshalException;
60 import org.exolab.castor.xml.Unmarshaller;
61 import org.exolab.castor.xml.ValidationException;
62 import org.xml.sax.EntityResolver;
63 import org.xml.sax.InputSource;
64 import org.xml.sax.SAXException;
65
66
67
68
69
70
71
72
73 public final class BindingLoader {
74
75
76
77
78
79 private ExtendedBinding _binding;
80
81
82
83
84 private BindingResolver _resolver = new BindingResolver();
85
86
87
88
89 public BindingLoader() {
90
91 }
92
93
94
95
96
97
98
99 public void loadBinding(final String url) throws BindingException {
100 InputSource source;
101 try {
102 source = _resolver.resolveEntity(null, url);
103 if (source == null) {
104 source = new InputSource(url);
105 }
106 if (source.getSystemId() == null) {
107 source.setSystemId(url);
108 }
109 loadBinding(source);
110 } catch (SAXException ex) {
111 throw new BindingException(ex);
112 } catch (IOException ioe) {
113 throw new BindingException(ioe);
114 }
115 }
116
117
118
119
120
121
122
123
124
125
126
127 @SuppressWarnings("unchecked")
128 public void loadBinding(final InputSource source) throws BindingException {
129 Binding loaded = null;
130 if (_binding == null) {
131 _binding = new ExtendedBinding();
132 }
133
134
135 Unmarshaller unmarshaller = new Unmarshaller(Binding.class);
136 unmarshaller.setValidation(true);
137
138 try {
139 loaded = (Binding) unmarshaller.unmarshal(source);
140
141
142 _binding.setDefaultBindingType(loaded.getDefaultBindingType());
143
144
145 Enumeration<PackageType> packages = loaded.enumeratePackage();
146 while (packages.hasMoreElements()) {
147 PackageType tempPackage = packages.nextElement();
148 _binding.addPackage(tempPackage);
149 }
150
151
152 NamingXMLType naming = loaded.getNamingXML();
153 if (naming != null) {
154 _binding.setNamingXML(naming);
155 }
156
157
158 AutomaticNamingType automaticNaming = loaded.getAutomaticNaming();
159 if (automaticNaming != null) {
160 _binding.setAutomaticNaming(automaticNaming);
161 _binding.handleAutomaticNaming(automaticNaming);
162 }
163
164
165 Enumeration<ComponentBindingType> elements = loaded.enumerateElementBinding();
166 while (elements.hasMoreElements()) {
167 ComponentBindingType tempComp = elements.nextElement();
168 _binding.addElementBinding(tempComp);
169 }
170
171
172 Enumeration<ComponentBindingType> attributes = loaded.enumerateAttributeBinding();
173 while (attributes.hasMoreElements()) {
174 ComponentBindingType tempComp = attributes.nextElement();
175 _binding.addAttributeBinding(tempComp);
176 }
177
178
179 Enumeration<ComponentBindingType> complexTypes = loaded.enumerateComplexTypeBinding();
180 while (complexTypes.hasMoreElements()) {
181 ComponentBindingType tempComp = complexTypes.nextElement();
182 _binding.addComplexTypeBinding(tempComp);
183 }
184
185
186 Enumeration<ComponentBindingType> sts = loaded.enumerateSimpleTypeBinding();
187 while (sts.hasMoreElements()) {
188 ComponentBindingType tempComp = sts.nextElement();
189 _binding.addSimpleTypeBinding(tempComp);
190 }
191
192
193 Enumeration<ComponentBindingType> groups = loaded.enumerateGroupBinding();
194 while (groups.hasMoreElements()) {
195 ComponentBindingType tempComp = groups.nextElement();
196 _binding.addGroupBinding(tempComp);
197 }
198
199
200 Enumeration<ComponentBindingType> enums = loaded.enumerateEnumBinding();
201 while (enums.hasMoreElements()) {
202 ComponentBindingType tempEnum = enums.nextElement();
203 _binding.addEnumBinding(tempEnum);
204 }
205
206
207 Enumeration<IncludeType> includes = loaded.enumerateInclude();
208 while (includes.hasMoreElements()) {
209 IncludeType tempInclude = includes.nextElement();
210 try {
211 loadBinding(tempInclude.getURI());
212 } catch (Exception except) {
213 throw new BindingException(except);
214 }
215 }
216 } catch (MarshalException e) {
217 throw new BindingException(e);
218 } catch (ValidationException e) {
219 throw new BindingException(e);
220 }
221 }
222
223
224
225
226
227
228
229 public ExtendedBinding getBinding() {
230 return _binding;
231 }
232
233
234
235
236
237
238
239
240
241 public void setBaseURL(final String url) {
242 try {
243 _resolver.setBaseURL(new URL(url));
244 } catch (MalformedURLException except) {
245 throw new IllegalArgumentException(except.getMessage());
246 }
247 }
248
249
250
251
252
253
254
255
256
257
258
259
260 public static ExtendedBinding createBinding(final InputSource source) throws BindingException {
261 BindingLoader loader = new BindingLoader();
262 loader.loadBinding(source);
263 return loader.getBinding();
264 }
265
266
267
268
269
270
271
272
273 public static ExtendedBinding createBinding(final String fileName) throws BindingException {
274 BindingLoader loader = new BindingLoader();
275 InputSource source = new InputSource(fileName);
276 loader.loadBinding(source);
277 return loader.getBinding();
278 }
279
280
281
282
283
284
285 class BindingResolver implements EntityResolver {
286
287
288
289
290 private static final String BINDING_PUBLICID =
291 "-//EXOLAB/Castor Binding Schema Version 1.0//EN";
292
293
294
295 private static final String BINDING_SYSTEMID =
296 "http://exolab.castor.org/binding.xsd";
297
298
299
300
301 private static final String BINDING_RESOURCE =
302 "/org/exolab/castor/builder/binding/binding.xsd";
303
304
305
306
307 private URL _baseUrl;
308
309
310
311
312
313 public void setBaseURL(final URL baseUrl) {
314 _baseUrl = baseUrl;
315 }
316
317
318
319
320
321 public URL getBaseURL() {
322 return _baseUrl;
323 }
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345 public InputSource resolveEntity(final String publicId, final String systemId)
346 throws IOException, SAXException {
347 InputSource source = null;
348
349
350 if (publicId != null && publicId.equals(BINDING_PUBLICID)) {
351 source = new InputSource(getClass().getResourceAsStream(BINDING_RESOURCE));
352 source.setPublicId(publicId);
353 return source;
354 }
355
356 if (systemId != null && systemId.equals(BINDING_SYSTEMID)) {
357 source = new InputSource(getClass().getResourceAsStream(BINDING_RESOURCE));
358 source.setSystemId(systemId);
359 return source;
360 }
361
362
363
364 if (systemId != null && _baseUrl != null) {
365 URL url;
366 try {
367 url = new URL(systemId);
368 source = new InputSource(url.openStream());
369 source.setSystemId(systemId);
370 return source;
371 } catch (MalformedURLException except) {
372 try {
373 url = new URL(_baseUrl, systemId);
374 source = new InputSource(url.openStream());
375 source.setSystemId(systemId);
376 return source;
377 } catch (MalformedURLException ex2) {
378 throw new SAXException(ex2);
379 }
380 }
381 }
382
383 return null;
384 }
385
386 }
387
388 }