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