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 package org.exolab.castor.xml.schema.reader;
37
38 import java.net.URI;
39
40 import org.exolab.castor.net.URIException;
41 import org.exolab.castor.net.URILocation;
42 import org.exolab.castor.net.URIResolver;
43 import org.exolab.castor.xml.AttributeSet;
44 import org.exolab.castor.xml.Namespaces;
45 import org.exolab.castor.xml.Unmarshaller;
46 import org.exolab.castor.xml.XMLException;
47 import org.exolab.castor.xml.schema.SchemaContext;
48 import org.exolab.castor.xml.schema.SchemaException;
49 import org.exolab.castor.xml.schema.Resolver;
50 import org.xml.sax.InputSource;
51 import org.xml.sax.Locator;
52 import org.xml.sax.Parser;
53
54
55
56
57
58
59
60 public abstract class ComponentReader {
61
62
63
64
65 private SchemaContext _schemaContext;
66
67 private Locator _documentLocator;
68
69
70
71
72 private URIResolver _uriResolver;
73
74 private ComponentReader() {
75 super();
76 }
77
78
79
80
81
82
83 protected ComponentReader(final SchemaContext schemaContext) {
84 this();
85 _schemaContext = schemaContext;
86 }
87
88
89
90
91
92
93 public abstract String elementName();
94
95
96
97
98
99
100 public abstract Object getObject();
101
102
103
104
105
106 public void finish() throws XMLException {}
107
108
109
110
111
112
113 public Resolver getResolver() {
114 return _schemaContext.getSchemaResolver();
115 }
116
117
118
119
120
121
122 public URIResolver getURIResolver() {
123 return _uriResolver;
124 }
125
126
127
128
129
130
131 public void setResolver(Resolver resolver) {
132 _schemaContext.setSchemaResolver(resolver);
133 }
134
135
136
137
138
139
140 public void setURIResolver(URIResolver uriResolver) {
141 _uriResolver = uriResolver;
142 }
143
144
145
146
147
148
149
150
151
152 public static boolean isWhiteSpace(char[] chars, int start, int length) {
153 int max = start + length;
154 for (int i = start; i < max; i++) {
155 char ch = chars[i];
156 switch (ch) {
157 case ' ':
158 case '\n':
159 case '\t':
160 case '\r':
161 break;
162 default:
163 return false;
164 }
165 }
166 return true;
167 }
168
169
170
171
172
173
174 public void error(String err) throws XMLException {
175 if (getDocumentLocator() != null) {
176 err += "\n line: " + getDocumentLocator().getLineNumber();
177 }
178
179 throw new XMLException(err);
180 }
181
182
183
184
185
186
187 public void error(Exception ex) throws XMLException {
188 if (getDocumentLocator() != null) {
189 String err = "An error occured at line: " + getDocumentLocator().getLineNumber();
190 throw new XMLException(err, ex);
191 }
192 throw new XMLException(ex);
193 }
194
195
196
197
198
199
200 public void illegalAttribute(String attName) throws XMLException {
201 String err = "Illegal attribute '" + attName + "' found on element <" + elementName() + ">.";
202
203 if (getDocumentLocator() != null) {
204 err += "\n line: " + getDocumentLocator().getLineNumber();
205 }
206
207 throw new XMLException(err);
208 }
209
210
211
212
213
214
215 public void illegalElement(String name) throws XMLException {
216 String err = "Illegal element '" + name + "' found as child of <" + elementName() + ">.";
217
218 if (getDocumentLocator() != null) {
219 err += "\n line: " + getDocumentLocator().getLineNumber();
220 }
221
222 throw new XMLException(err);
223 }
224
225
226
227
228
229
230 public void redefinedElement(String name) throws XMLException {
231 redefinedElement(name, null);
232 }
233
234
235
236
237
238
239 public void redefinedElement(String name, String xtraInfo) throws XMLException {
240 String err = "redefintion of element '" + name + "' within element <" + elementName() + ">.";
241
242 if (getDocumentLocator() != null) {
243 err += "\n line: " + getDocumentLocator().getLineNumber();
244 }
245
246 if (xtraInfo != null) {
247 err += "\n " + xtraInfo;
248 }
249
250 throw new XMLException(err + "\n");
251 }
252
253
254
255
256 public void outOfOrder(String name) throws XMLException {
257 String err = new StringBuilder("out of order element <").append(name).append("> found in <")
258 .append(elementName()).append(">.").toString();
259 throw new XMLException(err);
260 }
261
262
263
264
265
266
267
268
269 public static int toInt(String str) throws IllegalArgumentException {
270 try {
271 return Integer.parseInt(str);
272 } catch (NumberFormatException nfe) {
273 String err = str + " is not a valid integer. ";
274 throw new IllegalArgumentException(err);
275 }
276 }
277
278 public Locator getDocumentLocator() {
279 return _documentLocator;
280 }
281
282 public void setDocumentLocator(Locator documentLocator) {
283 _documentLocator = documentLocator;
284 }
285
286
287
288
289
290
291
292
293 public void characters(char[] chars, int start, int length) throws XMLException {
294
295 }
296
297
298
299
300
301
302
303
304 public void endElement(String name, String namespace) throws XMLException {
305
306 }
307
308
309
310
311
312
313
314
315
316
317
318 public void startElement(String name, String namespace, AttributeSet atts, Namespaces nsDecls)
319 throws XMLException {
320
321 }
322
323
324
325
326
327
328 public void setSchemaContext(final SchemaContext schemaContext) {
329 _schemaContext = schemaContext;
330 }
331
332
333
334
335
336
337 public SchemaContext getSchemaContext() {
338 return _schemaContext;
339 }
340
341 protected void parseSchema(Parser parser, SchemaUnmarshaller schemaUnmarshaller, URILocation uri,
342 String schemaLocation, String reason) throws SchemaException {
343 Sax2ComponentReader handler = new Sax2ComponentReader(schemaUnmarshaller);
344 parser.setDocumentHandler(handler);
345 parser.setErrorHandler(handler);
346
347 try {
348 InputSource source = new InputSource(uri.getReader());
349 source.setSystemId(uri.getAbsoluteURI());
350 parser.parse(source);
351 } catch (java.io.IOException ioe) {
352 throw new SchemaException("Error reading " + reason + " file '" + schemaLocation + "'");
353 } catch (org.xml.sax.SAXException sx) {
354 throw new SchemaException(sx);
355 }
356 }
357
358 protected Parser createParser(String reason) throws SchemaException {
359 Parser parser = null;
360 try {
361 parser = getSchemaContext().getParser();
362 } catch (RuntimeException rte) {
363 }
364 if (parser == null) {
365 throw new SchemaException("Error failed to create parser for " + reason);
366 }
367 return parser;
368 }
369
370 protected URILocation derive(Locator locator, String schemaLocation) throws XMLException {
371 try {
372 String documentBase = locator.getSystemId();
373 if (documentBase != null) {
374 if (!documentBase.endsWith("/"))
375 documentBase = documentBase.substring(0, documentBase.lastIndexOf("/") + 1);
376 }
377 return getURIResolver().resolve(schemaLocation, documentBase);
378 } catch (URIException ure) {
379 throw new XMLException(ure);
380 }
381
382 }
383
384 }