1 | // file : xsd/cxx/parser/xerces/elements.txx |
---|
2 | // author : Boris Kolpackov <boris@codesynthesis.com> |
---|
3 | // copyright : Copyright (c) 2005-2008 Code Synthesis Tools CC |
---|
4 | // license : GNU GPL v2 + exceptions; see accompanying LICENSE file |
---|
5 | |
---|
6 | #include <istream> |
---|
7 | #include <cstddef> // std::size_t |
---|
8 | #include <cassert> |
---|
9 | |
---|
10 | #include <xercesc/sax/SAXParseException.hpp> |
---|
11 | #include <xercesc/sax2/Attributes.hpp> |
---|
12 | #include <xercesc/sax2/XMLReaderFactory.hpp> |
---|
13 | #include <xercesc/validators/schema/SchemaSymbols.hpp> |
---|
14 | |
---|
15 | #include <xsd/cxx/xml/string.hxx> |
---|
16 | #include <xsd/cxx/xml/sax/std-input-source.hxx> |
---|
17 | #include <xsd/cxx/xml/sax/bits/error-handler-proxy.hxx> |
---|
18 | #include <xsd/cxx/xml/bits/literals.hxx> // xml::bits::{xml_prefix, etc} |
---|
19 | |
---|
20 | #include <xsd/cxx/parser/error-handler.hxx> |
---|
21 | #include <xsd/cxx/parser/schema-exceptions.hxx> |
---|
22 | |
---|
23 | namespace xsd |
---|
24 | { |
---|
25 | namespace cxx |
---|
26 | { |
---|
27 | namespace parser |
---|
28 | { |
---|
29 | namespace xerces |
---|
30 | { |
---|
31 | |
---|
32 | // document |
---|
33 | // |
---|
34 | |
---|
35 | template <typename C> |
---|
36 | document<C>:: |
---|
37 | document (parser_base<C>& parser, |
---|
38 | const C* name, |
---|
39 | bool polymorphic) |
---|
40 | : cxx::parser::document<C> (parser, std::basic_string<C> (), name), |
---|
41 | polymorphic_ (polymorphic) |
---|
42 | { |
---|
43 | } |
---|
44 | |
---|
45 | template <typename C> |
---|
46 | document<C>:: |
---|
47 | document (parser_base<C>& parser, |
---|
48 | const std::basic_string<C>& name, |
---|
49 | bool polymorphic) |
---|
50 | : cxx::parser::document<C> (parser, std::basic_string<C> (), name), |
---|
51 | polymorphic_ (polymorphic) |
---|
52 | { |
---|
53 | } |
---|
54 | |
---|
55 | template <typename C> |
---|
56 | document<C>:: |
---|
57 | document (parser_base<C>& parser, |
---|
58 | const C* ns, |
---|
59 | const C* name, |
---|
60 | bool polymorphic) |
---|
61 | : cxx::parser::document<C> (parser, ns, name), |
---|
62 | polymorphic_ (polymorphic) |
---|
63 | { |
---|
64 | } |
---|
65 | |
---|
66 | template <typename C> |
---|
67 | document<C>:: |
---|
68 | document (parser_base<C>& parser, |
---|
69 | const std::basic_string<C>& ns, |
---|
70 | const std::basic_string<C>& name, |
---|
71 | bool polymorphic) |
---|
72 | : cxx::parser::document<C> (parser, ns, name), |
---|
73 | polymorphic_ (polymorphic) |
---|
74 | { |
---|
75 | } |
---|
76 | |
---|
77 | template <typename C> |
---|
78 | document<C>:: |
---|
79 | document (bool polymorphic) |
---|
80 | : polymorphic_ (polymorphic) |
---|
81 | { |
---|
82 | } |
---|
83 | |
---|
84 | // parse (uri) |
---|
85 | // |
---|
86 | template <typename C> |
---|
87 | void document<C>:: |
---|
88 | parse (const std::basic_string<C>& uri, |
---|
89 | flags f, |
---|
90 | const properties<C>& p) |
---|
91 | { |
---|
92 | xml::auto_initializer init ((f & flags::dont_initialize) == 0); |
---|
93 | |
---|
94 | error_handler<C> eh; |
---|
95 | xml::sax::bits::error_handler_proxy<C> eh_proxy (eh); |
---|
96 | std::auto_ptr<xercesc::SAX2XMLReader> sax (create_sax_ (f, p)); |
---|
97 | |
---|
98 | parse (uri, eh_proxy, *sax, f, p); |
---|
99 | |
---|
100 | eh.throw_if_failed (); |
---|
101 | } |
---|
102 | |
---|
103 | template <typename C> |
---|
104 | void document<C>:: |
---|
105 | parse (const C* uri, |
---|
106 | flags f, |
---|
107 | const properties<C>& p) |
---|
108 | { |
---|
109 | parse (std::basic_string<C> (uri), f, p); |
---|
110 | } |
---|
111 | |
---|
112 | // error_handler |
---|
113 | // |
---|
114 | |
---|
115 | template <typename C> |
---|
116 | void document<C>:: |
---|
117 | parse (const std::basic_string<C>& uri, |
---|
118 | xml::error_handler<C>& eh, |
---|
119 | flags f, |
---|
120 | const properties<C>& p) |
---|
121 | { |
---|
122 | xml::auto_initializer init ((f & flags::dont_initialize) == 0); |
---|
123 | |
---|
124 | xml::sax::bits::error_handler_proxy<C> eh_proxy (eh); |
---|
125 | std::auto_ptr<xercesc::SAX2XMLReader> sax (create_sax_ (f, p)); |
---|
126 | |
---|
127 | parse (uri, eh_proxy, *sax, f, p); |
---|
128 | |
---|
129 | if (eh_proxy.failed ()) |
---|
130 | throw parsing<C> (); |
---|
131 | } |
---|
132 | |
---|
133 | template <typename C> |
---|
134 | void document<C>:: |
---|
135 | parse (const C* uri, |
---|
136 | xml::error_handler<C>& eh, |
---|
137 | flags f, |
---|
138 | const properties<C>& p) |
---|
139 | { |
---|
140 | parse (std::basic_string<C> (uri), eh, f, p); |
---|
141 | } |
---|
142 | |
---|
143 | // ErrorHandler |
---|
144 | // |
---|
145 | |
---|
146 | template <typename C> |
---|
147 | void document<C>:: |
---|
148 | parse (const std::basic_string<C>& uri, |
---|
149 | xercesc::ErrorHandler& eh, |
---|
150 | flags f, |
---|
151 | const properties<C>& p) |
---|
152 | { |
---|
153 | xml::sax::bits::error_handler_proxy<C> eh_proxy (eh); |
---|
154 | std::auto_ptr<xercesc::SAX2XMLReader> sax (create_sax_ (f, p)); |
---|
155 | |
---|
156 | parse (uri, eh_proxy, *sax, f, p); |
---|
157 | |
---|
158 | if (eh_proxy.failed ()) |
---|
159 | throw parsing<C> (); |
---|
160 | } |
---|
161 | |
---|
162 | template <typename C> |
---|
163 | void document<C>:: |
---|
164 | parse (const C* uri, |
---|
165 | xercesc::ErrorHandler& eh, |
---|
166 | flags f, |
---|
167 | const properties<C>& p) |
---|
168 | { |
---|
169 | parse (std::basic_string<C> (uri), eh, f, p); |
---|
170 | } |
---|
171 | |
---|
172 | // SAX2XMLReader |
---|
173 | // |
---|
174 | |
---|
175 | template <typename C> |
---|
176 | void document<C>:: |
---|
177 | parse (const std::basic_string<C>& uri, |
---|
178 | xercesc::SAX2XMLReader& sax, |
---|
179 | flags f, |
---|
180 | const properties<C>& p) |
---|
181 | { |
---|
182 | // If there is no error handler, then fall back on the default |
---|
183 | // implementation. |
---|
184 | // |
---|
185 | xercesc::ErrorHandler* eh (sax.getErrorHandler ()); |
---|
186 | |
---|
187 | if (eh) |
---|
188 | { |
---|
189 | xml::sax::bits::error_handler_proxy<C> eh_proxy (*eh); |
---|
190 | |
---|
191 | parse (uri, eh_proxy, sax, f, p); |
---|
192 | |
---|
193 | if (eh_proxy.failed ()) |
---|
194 | throw parsing<C> (); |
---|
195 | } |
---|
196 | else |
---|
197 | { |
---|
198 | error_handler<C> fallback_eh; |
---|
199 | xml::sax::bits::error_handler_proxy<C> eh_proxy (fallback_eh); |
---|
200 | |
---|
201 | parse (uri, eh_proxy, sax, f, p); |
---|
202 | |
---|
203 | fallback_eh.throw_if_failed (); |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | template <typename C> |
---|
208 | void document<C>:: |
---|
209 | parse (const C* uri, |
---|
210 | xercesc::SAX2XMLReader& sax, |
---|
211 | flags f, |
---|
212 | const properties<C>& p) |
---|
213 | { |
---|
214 | parse (std::basic_string<C> (uri), sax, f, p); |
---|
215 | } |
---|
216 | |
---|
217 | // parse (istream) |
---|
218 | // |
---|
219 | |
---|
220 | template <typename C> |
---|
221 | void document<C>:: |
---|
222 | parse (std::istream& is, |
---|
223 | flags f, |
---|
224 | const properties<C>& p) |
---|
225 | { |
---|
226 | xml::auto_initializer init ((f & flags::dont_initialize) == 0); |
---|
227 | |
---|
228 | xml::sax::std_input_source isrc (is); |
---|
229 | |
---|
230 | parse (isrc, f, p); |
---|
231 | } |
---|
232 | |
---|
233 | template <typename C> |
---|
234 | void document<C>:: |
---|
235 | parse (std::istream& is, |
---|
236 | xml::error_handler<C>& eh, |
---|
237 | flags f, |
---|
238 | const properties<C>& p) |
---|
239 | { |
---|
240 | xml::auto_initializer init ((f & flags::dont_initialize) == 0); |
---|
241 | xml::sax::std_input_source isrc (is); |
---|
242 | parse (isrc, eh, f, p); |
---|
243 | } |
---|
244 | |
---|
245 | template <typename C> |
---|
246 | void document<C>:: |
---|
247 | parse (std::istream& is, |
---|
248 | xercesc::ErrorHandler& eh, |
---|
249 | flags f, |
---|
250 | const properties<C>& p) |
---|
251 | { |
---|
252 | xml::sax::std_input_source isrc (is); |
---|
253 | parse (isrc, eh, f, p); |
---|
254 | } |
---|
255 | |
---|
256 | template <typename C> |
---|
257 | void document<C>:: |
---|
258 | parse (std::istream& is, |
---|
259 | xercesc::SAX2XMLReader& sax, |
---|
260 | flags f, |
---|
261 | const properties<C>& p) |
---|
262 | { |
---|
263 | xml::sax::std_input_source isrc (is); |
---|
264 | parse (isrc, sax, f, p); |
---|
265 | } |
---|
266 | |
---|
267 | |
---|
268 | // parse (istream, system_id) |
---|
269 | // |
---|
270 | |
---|
271 | |
---|
272 | template <typename C> |
---|
273 | void document<C>:: |
---|
274 | parse (std::istream& is, |
---|
275 | const std::basic_string<C>& system_id, |
---|
276 | flags f, |
---|
277 | const properties<C>& p) |
---|
278 | { |
---|
279 | xml::auto_initializer init ((f & flags::dont_initialize) == 0); |
---|
280 | xml::sax::std_input_source isrc (is, system_id); |
---|
281 | parse (isrc, f, p); |
---|
282 | } |
---|
283 | |
---|
284 | template <typename C> |
---|
285 | void document<C>:: |
---|
286 | parse (std::istream& is, |
---|
287 | const std::basic_string<C>& system_id, |
---|
288 | xml::error_handler<C>& eh, |
---|
289 | flags f, |
---|
290 | const properties<C>& p) |
---|
291 | { |
---|
292 | xml::auto_initializer init ((f & flags::dont_initialize) == 0); |
---|
293 | xml::sax::std_input_source isrc (is, system_id); |
---|
294 | parse (isrc, eh, f, p); |
---|
295 | } |
---|
296 | |
---|
297 | template <typename C> |
---|
298 | void document<C>:: |
---|
299 | parse (std::istream& is, |
---|
300 | const std::basic_string<C>& system_id, |
---|
301 | xercesc::ErrorHandler& eh, |
---|
302 | flags f, |
---|
303 | const properties<C>& p) |
---|
304 | { |
---|
305 | xml::sax::std_input_source isrc (is, system_id); |
---|
306 | parse (isrc, eh, f, p); |
---|
307 | } |
---|
308 | |
---|
309 | template <typename C> |
---|
310 | void document<C>:: |
---|
311 | parse (std::istream& is, |
---|
312 | const std::basic_string<C>& system_id, |
---|
313 | xercesc::SAX2XMLReader& sax, |
---|
314 | flags f, |
---|
315 | const properties<C>& p) |
---|
316 | { |
---|
317 | xml::sax::std_input_source isrc (is, system_id); |
---|
318 | parse (isrc, sax, f, p); |
---|
319 | } |
---|
320 | |
---|
321 | |
---|
322 | // parse (istream, system_id, public_id) |
---|
323 | // |
---|
324 | |
---|
325 | template <typename C> |
---|
326 | void document<C>:: |
---|
327 | parse (std::istream& is, |
---|
328 | const std::basic_string<C>& system_id, |
---|
329 | const std::basic_string<C>& public_id, |
---|
330 | flags f, |
---|
331 | const properties<C>& p) |
---|
332 | { |
---|
333 | xml::auto_initializer init ((f & flags::dont_initialize) == 0); |
---|
334 | xml::sax::std_input_source isrc (is, system_id, public_id); |
---|
335 | parse (isrc, f, p); |
---|
336 | } |
---|
337 | |
---|
338 | template <typename C> |
---|
339 | void document<C>:: |
---|
340 | parse (std::istream& is, |
---|
341 | const std::basic_string<C>& system_id, |
---|
342 | const std::basic_string<C>& public_id, |
---|
343 | xml::error_handler<C>& eh, |
---|
344 | flags f, |
---|
345 | const properties<C>& p) |
---|
346 | { |
---|
347 | xml::auto_initializer init ((f & flags::dont_initialize) == 0); |
---|
348 | xml::sax::std_input_source isrc (is, system_id, public_id); |
---|
349 | parse (isrc, eh, f, p); |
---|
350 | } |
---|
351 | |
---|
352 | template <typename C> |
---|
353 | void document<C>:: |
---|
354 | parse (std::istream& is, |
---|
355 | const std::basic_string<C>& system_id, |
---|
356 | const std::basic_string<C>& public_id, |
---|
357 | xercesc::ErrorHandler& eh, |
---|
358 | flags f, |
---|
359 | const properties<C>& p) |
---|
360 | { |
---|
361 | xml::sax::std_input_source isrc (is, system_id, public_id); |
---|
362 | parse (isrc, eh, f, p); |
---|
363 | } |
---|
364 | |
---|
365 | template <typename C> |
---|
366 | void document<C>:: |
---|
367 | parse (std::istream& is, |
---|
368 | const std::basic_string<C>& system_id, |
---|
369 | const std::basic_string<C>& public_id, |
---|
370 | xercesc::SAX2XMLReader& sax, |
---|
371 | flags f, |
---|
372 | const properties<C>& p) |
---|
373 | { |
---|
374 | xml::sax::std_input_source isrc (is, system_id, public_id); |
---|
375 | parse (isrc, sax, f, p); |
---|
376 | } |
---|
377 | |
---|
378 | |
---|
379 | // parse (InputSource) |
---|
380 | // |
---|
381 | |
---|
382 | |
---|
383 | template <typename C> |
---|
384 | void document<C>:: |
---|
385 | parse (const xercesc::InputSource& is, |
---|
386 | flags f, |
---|
387 | const properties<C>& p) |
---|
388 | { |
---|
389 | error_handler<C> eh; |
---|
390 | xml::sax::bits::error_handler_proxy<C> eh_proxy (eh); |
---|
391 | std::auto_ptr<xercesc::SAX2XMLReader> sax (create_sax_ (f, p)); |
---|
392 | |
---|
393 | parse (is, eh_proxy, *sax, f, p); |
---|
394 | |
---|
395 | eh.throw_if_failed (); |
---|
396 | } |
---|
397 | |
---|
398 | template <typename C> |
---|
399 | void document<C>:: |
---|
400 | parse (const xercesc::InputSource& is, |
---|
401 | xml::error_handler<C>& eh, |
---|
402 | flags f, |
---|
403 | const properties<C>& p) |
---|
404 | { |
---|
405 | xml::sax::bits::error_handler_proxy<C> eh_proxy (eh); |
---|
406 | std::auto_ptr<xercesc::SAX2XMLReader> sax (create_sax_ (f, p)); |
---|
407 | |
---|
408 | parse (is, eh_proxy, *sax, f, p); |
---|
409 | |
---|
410 | if (eh_proxy.failed ()) |
---|
411 | throw parsing<C> (); |
---|
412 | } |
---|
413 | |
---|
414 | template <typename C> |
---|
415 | void document<C>:: |
---|
416 | parse (const xercesc::InputSource& is, |
---|
417 | xercesc::ErrorHandler& eh, |
---|
418 | flags f, |
---|
419 | const properties<C>& p) |
---|
420 | { |
---|
421 | xml::sax::bits::error_handler_proxy<C> eh_proxy (eh); |
---|
422 | std::auto_ptr<xercesc::SAX2XMLReader> sax (create_sax_ (f, p)); |
---|
423 | |
---|
424 | parse (is, eh_proxy, *sax, f, p); |
---|
425 | |
---|
426 | if (eh_proxy.failed ()) |
---|
427 | throw parsing<C> (); |
---|
428 | } |
---|
429 | |
---|
430 | |
---|
431 | template <typename C> |
---|
432 | void document<C>:: |
---|
433 | parse (const xercesc::InputSource& is, |
---|
434 | xercesc::SAX2XMLReader& sax, |
---|
435 | flags f, |
---|
436 | const properties<C>& p) |
---|
437 | { |
---|
438 | // If there is no error handler, then fall back on the default |
---|
439 | // implementation. |
---|
440 | // |
---|
441 | xercesc::ErrorHandler* eh (sax.getErrorHandler ()); |
---|
442 | |
---|
443 | if (eh) |
---|
444 | { |
---|
445 | xml::sax::bits::error_handler_proxy<C> eh_proxy (*eh); |
---|
446 | |
---|
447 | parse (is, eh_proxy, sax, f, p); |
---|
448 | |
---|
449 | if (eh_proxy.failed ()) |
---|
450 | throw parsing<C> (); |
---|
451 | } |
---|
452 | else |
---|
453 | { |
---|
454 | error_handler<C> fallback_eh; |
---|
455 | xml::sax::bits::error_handler_proxy<C> eh_proxy (fallback_eh); |
---|
456 | |
---|
457 | parse (is, eh_proxy, sax, f, p); |
---|
458 | |
---|
459 | fallback_eh.throw_if_failed (); |
---|
460 | } |
---|
461 | } |
---|
462 | |
---|
463 | namespace Bits |
---|
464 | { |
---|
465 | struct ErrorHandlingController |
---|
466 | { |
---|
467 | ErrorHandlingController (xercesc::SAX2XMLReader& sax, |
---|
468 | xercesc::ErrorHandler& eh) |
---|
469 | : sax_ (sax), eh_ (sax_.getErrorHandler ()) |
---|
470 | { |
---|
471 | sax_.setErrorHandler (&eh); |
---|
472 | } |
---|
473 | |
---|
474 | ~ErrorHandlingController () |
---|
475 | { |
---|
476 | sax_.setErrorHandler (eh_); |
---|
477 | } |
---|
478 | |
---|
479 | private: |
---|
480 | xercesc::SAX2XMLReader& sax_; |
---|
481 | xercesc::ErrorHandler* eh_; |
---|
482 | }; |
---|
483 | |
---|
484 | struct ContentHandlingController |
---|
485 | { |
---|
486 | ContentHandlingController (xercesc::SAX2XMLReader& sax, |
---|
487 | xercesc::ContentHandler& ch) |
---|
488 | : sax_ (sax), ch_ (sax_.getContentHandler ()) |
---|
489 | { |
---|
490 | sax_.setContentHandler (&ch); |
---|
491 | } |
---|
492 | |
---|
493 | ~ContentHandlingController () |
---|
494 | { |
---|
495 | sax_.setContentHandler (ch_); |
---|
496 | } |
---|
497 | |
---|
498 | private: |
---|
499 | xercesc::SAX2XMLReader& sax_; |
---|
500 | xercesc::ContentHandler* ch_; |
---|
501 | }; |
---|
502 | }; |
---|
503 | |
---|
504 | template <typename C> |
---|
505 | void document<C>:: |
---|
506 | parse (const std::basic_string<C>& uri, |
---|
507 | xercesc::ErrorHandler& eh, |
---|
508 | xercesc::SAX2XMLReader& sax, |
---|
509 | flags, |
---|
510 | const properties<C>&) |
---|
511 | { |
---|
512 | event_router<C> router (*this, polymorphic_); |
---|
513 | |
---|
514 | Bits::ErrorHandlingController ehc (sax, eh); |
---|
515 | Bits::ContentHandlingController chc (sax, router); |
---|
516 | |
---|
517 | try |
---|
518 | { |
---|
519 | sax.parse (xml::string (uri).c_str ()); |
---|
520 | } |
---|
521 | catch (const schema_exception<C>& e) |
---|
522 | { |
---|
523 | xml::string id (e.id ()); |
---|
524 | |
---|
525 | xercesc::SAXParseException se ( |
---|
526 | xml::string (e.message ()).c_str (), |
---|
527 | id.c_str (), |
---|
528 | id.c_str (), |
---|
529 | static_cast<XMLSSize_t> (e.line ()), |
---|
530 | static_cast<XMLSSize_t> (e.column ())); |
---|
531 | |
---|
532 | eh.fatalError (se); |
---|
533 | } |
---|
534 | } |
---|
535 | |
---|
536 | template <typename C> |
---|
537 | void document<C>:: |
---|
538 | parse (const xercesc::InputSource& is, |
---|
539 | xercesc::ErrorHandler& eh, |
---|
540 | xercesc::SAX2XMLReader& sax, |
---|
541 | flags, |
---|
542 | const properties<C>&) |
---|
543 | { |
---|
544 | event_router<C> router (*this, polymorphic_); |
---|
545 | |
---|
546 | Bits::ErrorHandlingController controller (sax, eh); |
---|
547 | Bits::ContentHandlingController chc (sax, router); |
---|
548 | |
---|
549 | try |
---|
550 | { |
---|
551 | sax.parse (is); |
---|
552 | } |
---|
553 | catch (const schema_exception<C>& e) |
---|
554 | { |
---|
555 | xml::string id (e.id ()); |
---|
556 | |
---|
557 | xercesc::SAXParseException se ( |
---|
558 | xml::string (e.message ()).c_str (), |
---|
559 | id.c_str (), |
---|
560 | id.c_str (), |
---|
561 | static_cast<XMLSSize_t> (e.line ()), |
---|
562 | static_cast<XMLSSize_t> (e.column ())); |
---|
563 | |
---|
564 | eh.fatalError (se); |
---|
565 | } |
---|
566 | } |
---|
567 | |
---|
568 | |
---|
569 | template <typename C> |
---|
570 | std::auto_ptr<xercesc::SAX2XMLReader> document<C>:: |
---|
571 | create_sax_ (flags f, const properties<C>& p) |
---|
572 | { |
---|
573 | // HP aCC cannot handle using namespace xercesc; |
---|
574 | // |
---|
575 | using xercesc::SAX2XMLReader; |
---|
576 | using xercesc::XMLReaderFactory; |
---|
577 | using xercesc::XMLUni; |
---|
578 | |
---|
579 | std::auto_ptr<SAX2XMLReader> sax ( |
---|
580 | XMLReaderFactory::createXMLReader ()); |
---|
581 | |
---|
582 | sax->setFeature (XMLUni::fgSAX2CoreNameSpaces, true); |
---|
583 | sax->setFeature (XMLUni::fgSAX2CoreNameSpacePrefixes, true); |
---|
584 | sax->setFeature (XMLUni::fgXercesValidationErrorAsFatal, true); |
---|
585 | |
---|
586 | if (f & flags::dont_validate) |
---|
587 | { |
---|
588 | sax->setFeature (XMLUni::fgSAX2CoreValidation, false); |
---|
589 | sax->setFeature (XMLUni::fgXercesSchema, false); |
---|
590 | sax->setFeature (XMLUni::fgXercesSchemaFullChecking, false); |
---|
591 | } |
---|
592 | else |
---|
593 | { |
---|
594 | sax->setFeature (XMLUni::fgSAX2CoreValidation, true); |
---|
595 | sax->setFeature (XMLUni::fgXercesSchema, true); |
---|
596 | |
---|
597 | // This feature checks the schema grammar for additional |
---|
598 | // errors. We most likely do not need it when validating |
---|
599 | // instances (assuming the schema is valid). |
---|
600 | // |
---|
601 | sax->setFeature (XMLUni::fgXercesSchemaFullChecking, false); |
---|
602 | } |
---|
603 | |
---|
604 | // Transfer properies if any. |
---|
605 | // |
---|
606 | |
---|
607 | if (!p.schema_location ().empty ()) |
---|
608 | { |
---|
609 | xml::string sl (p.schema_location ()); |
---|
610 | const void* v (sl.c_str ()); |
---|
611 | |
---|
612 | sax->setProperty ( |
---|
613 | XMLUni::fgXercesSchemaExternalSchemaLocation, |
---|
614 | const_cast<void*> (v)); |
---|
615 | } |
---|
616 | |
---|
617 | if (!p.no_namespace_schema_location ().empty ()) |
---|
618 | { |
---|
619 | xml::string sl (p.no_namespace_schema_location ()); |
---|
620 | const void* v (sl.c_str ()); |
---|
621 | |
---|
622 | sax->setProperty ( |
---|
623 | XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation, |
---|
624 | const_cast<void*> (v)); |
---|
625 | } |
---|
626 | |
---|
627 | return sax; |
---|
628 | } |
---|
629 | |
---|
630 | // event_router |
---|
631 | // |
---|
632 | template <typename C> |
---|
633 | event_router<C>:: |
---|
634 | event_router (cxx::parser::document<C>& consumer, bool polymorphic) |
---|
635 | : loc_ (0), consumer_ (consumer), polymorphic_ (polymorphic) |
---|
636 | { |
---|
637 | } |
---|
638 | |
---|
639 | template <typename C> |
---|
640 | void event_router<C>:: |
---|
641 | setDocumentLocator (const xercesc::Locator* const loc) |
---|
642 | { |
---|
643 | loc_ = loc; |
---|
644 | } |
---|
645 | |
---|
646 | template <typename C> |
---|
647 | void event_router<C>:: |
---|
648 | startElement(const XMLCh* const uri, |
---|
649 | const XMLCh* const lname, |
---|
650 | const XMLCh* const /*qname*/, |
---|
651 | const xercesc::Attributes& attributes) |
---|
652 | { |
---|
653 | typedef std::basic_string<C> string; |
---|
654 | |
---|
655 | { |
---|
656 | string ns (xml::transcode<C> (uri)); |
---|
657 | string name (xml::transcode<C> (lname)); |
---|
658 | |
---|
659 | // Without this explicit construction IBM XL C++ complains |
---|
660 | // about ro_string's copy ctor being private even though the |
---|
661 | // temporary has been eliminated. Note that we cannot |
---|
662 | // eliminate ns and name since ro_string does not make a copy. |
---|
663 | // |
---|
664 | ro_string<C> ro_ns (ns); |
---|
665 | ro_string<C> ro_name (name); |
---|
666 | |
---|
667 | try |
---|
668 | { |
---|
669 | if (!polymorphic_) |
---|
670 | consumer_.start_element (ro_ns, ro_name, 0); |
---|
671 | else |
---|
672 | { |
---|
673 | // Search for the xsi:type attribute. |
---|
674 | // |
---|
675 | int i (attributes.getIndex ( |
---|
676 | xercesc::SchemaSymbols::fgURI_XSI, |
---|
677 | xercesc::SchemaSymbols::fgXSI_TYPE)); |
---|
678 | |
---|
679 | if (i == -1) |
---|
680 | consumer_.start_element (ro_ns, ro_name, 0); |
---|
681 | else |
---|
682 | { |
---|
683 | // @@ Probably need proper QName validation. |
---|
684 | // |
---|
685 | // Get the qualified type name and try to resolve it. |
---|
686 | // |
---|
687 | string qn (xml::transcode<C> (attributes.getValue (i))); |
---|
688 | |
---|
689 | string tp, tn, tns; |
---|
690 | typename string::size_type pos (qn.find (C (':'))); |
---|
691 | |
---|
692 | if (pos != string::npos) |
---|
693 | { |
---|
694 | tp.assign (qn, 0, pos); |
---|
695 | tn.assign (qn, pos + 1, string::npos); |
---|
696 | |
---|
697 | if (tp.empty ()) |
---|
698 | throw dynamic_type<C> (qn); |
---|
699 | } |
---|
700 | else |
---|
701 | tn = qn; |
---|
702 | |
---|
703 | // Search our namespace declaration stack. Sun CC 5.7 |
---|
704 | // blows if we use const_reverse_iterator. |
---|
705 | // |
---|
706 | for (typename ns_decls::reverse_iterator |
---|
707 | it (ns_decls_.rbegin ()), e (ns_decls_.rend ()); |
---|
708 | it != e; ++it) |
---|
709 | { |
---|
710 | if (it->prefix == tp) |
---|
711 | { |
---|
712 | tns = it->ns; |
---|
713 | break; |
---|
714 | } |
---|
715 | } |
---|
716 | |
---|
717 | if (!tp.empty () && tns.empty ()) |
---|
718 | { |
---|
719 | // The 'xml' prefix requires special handling. |
---|
720 | // |
---|
721 | if (tp == xml::bits::xml_prefix<C> ()) |
---|
722 | tns = xml::bits::xml_namespace<C> (); |
---|
723 | else |
---|
724 | throw dynamic_type<C> (qn); |
---|
725 | } |
---|
726 | |
---|
727 | // Construct the compound name. |
---|
728 | // |
---|
729 | if (!tns.empty ()) |
---|
730 | { |
---|
731 | tn += C (' '); |
---|
732 | tn += tns; |
---|
733 | } |
---|
734 | |
---|
735 | ro_string<C> ro_tn (tn); |
---|
736 | |
---|
737 | consumer_.start_element (ro_ns, ro_name, &ro_tn); |
---|
738 | } |
---|
739 | } |
---|
740 | } |
---|
741 | catch (schema_exception<C>& e) |
---|
742 | { |
---|
743 | set_location (e); |
---|
744 | throw; |
---|
745 | } |
---|
746 | } |
---|
747 | |
---|
748 | // Xerces SAX uses unsigned int for indexing. |
---|
749 | // |
---|
750 | for (unsigned int i (0); i < attributes.getLength(); ++i) |
---|
751 | { |
---|
752 | string ns (xml::transcode<C> (attributes.getURI (i))); |
---|
753 | string name (xml::transcode<C> (attributes.getLocalName (i))); |
---|
754 | string value (xml::transcode<C> (attributes.getValue (i))); |
---|
755 | |
---|
756 | // Without this explicit construction IBM XL C++ complains |
---|
757 | // about ro_string's copy ctor being private even though the |
---|
758 | // temporary has been eliminated. Note that we cannot |
---|
759 | // eliminate ns, name and value since ro_string does not make |
---|
760 | // a copy. |
---|
761 | // |
---|
762 | ro_string<C> ro_ns (ns); |
---|
763 | ro_string<C> ro_name (name); |
---|
764 | ro_string<C> ro_value (value); |
---|
765 | |
---|
766 | try |
---|
767 | { |
---|
768 | consumer_.attribute (ro_ns, ro_name, ro_value); |
---|
769 | } |
---|
770 | catch (schema_exception<C>& e) |
---|
771 | { |
---|
772 | set_location (e); |
---|
773 | throw; |
---|
774 | } |
---|
775 | } |
---|
776 | } |
---|
777 | |
---|
778 | template <typename C> |
---|
779 | void event_router<C>:: |
---|
780 | endElement(const XMLCh* const uri, |
---|
781 | const XMLCh* const lname, |
---|
782 | const XMLCh* const /*qname*/) |
---|
783 | { |
---|
784 | typedef std::basic_string<C> string; |
---|
785 | |
---|
786 | string ns (xml::transcode<C> (uri)); |
---|
787 | string name (xml::transcode<C> (lname)); |
---|
788 | |
---|
789 | // Without this explicit construction IBM XL C++ complains |
---|
790 | // about ro_string's copy ctor being private even though the |
---|
791 | // temporary has been eliminated. Note that we cannot |
---|
792 | // eliminate ns and name since ro_string does not make a copy. |
---|
793 | // |
---|
794 | ro_string<C> ro_ns (ns); |
---|
795 | ro_string<C> ro_name (name); |
---|
796 | |
---|
797 | try |
---|
798 | { |
---|
799 | consumer_.end_element (ro_ns, ro_name); |
---|
800 | } |
---|
801 | catch (schema_exception<C>& e) |
---|
802 | { |
---|
803 | set_location (e); |
---|
804 | throw; |
---|
805 | } |
---|
806 | } |
---|
807 | |
---|
808 | template <typename C> |
---|
809 | void event_router<C>:: |
---|
810 | #if _XERCES_VERSION >= 30000 |
---|
811 | characters (const XMLCh* const s, const XMLSize_t n) |
---|
812 | #else |
---|
813 | characters (const XMLCh* const s, const unsigned int n) |
---|
814 | #endif |
---|
815 | { |
---|
816 | typedef std::basic_string<C> string; |
---|
817 | |
---|
818 | if (n != 0) |
---|
819 | { |
---|
820 | string str (xml::transcode<C> (s, n)); |
---|
821 | |
---|
822 | // Without this explicit construction IBM XL C++ complains |
---|
823 | // about ro_string's copy ctor being private even though the |
---|
824 | // temporary has been eliminated. Note that we cannot |
---|
825 | // eliminate str since ro_string does not make a copy. |
---|
826 | // |
---|
827 | ro_string<C> ro_str (str); |
---|
828 | |
---|
829 | try |
---|
830 | { |
---|
831 | consumer_.characters (ro_str); |
---|
832 | } |
---|
833 | catch (schema_exception<C>& e) |
---|
834 | { |
---|
835 | set_location (e); |
---|
836 | throw; |
---|
837 | } |
---|
838 | } |
---|
839 | } |
---|
840 | |
---|
841 | template <typename C> |
---|
842 | void event_router<C>:: |
---|
843 | startPrefixMapping (const XMLCh* const prefix, |
---|
844 | const XMLCh* const uri) |
---|
845 | { |
---|
846 | if (polymorphic_) |
---|
847 | { |
---|
848 | typedef std::basic_string<C> string; |
---|
849 | |
---|
850 | string p (xml::transcode<C> (prefix)); |
---|
851 | string ns (xml::transcode<C> (uri)); |
---|
852 | |
---|
853 | ns_decls_.push_back (ns_decl (p, ns)); |
---|
854 | } |
---|
855 | } |
---|
856 | |
---|
857 | template <typename C> |
---|
858 | void event_router<C>:: |
---|
859 | endPrefixMapping (const XMLCh* const prefix) |
---|
860 | { |
---|
861 | if (polymorphic_) |
---|
862 | { |
---|
863 | typedef std::basic_string<C> string; |
---|
864 | |
---|
865 | string p (xml::transcode<C> (prefix)); |
---|
866 | |
---|
867 | // Here we assume the prefixes are removed in the reverse |
---|
868 | // order of them being added. This appears to how every |
---|
869 | // sensible implementation works. |
---|
870 | // |
---|
871 | assert (ns_decls_.back ().prefix == p); |
---|
872 | |
---|
873 | ns_decls_.pop_back (); |
---|
874 | } |
---|
875 | } |
---|
876 | |
---|
877 | template <typename C> |
---|
878 | void event_router<C>:: |
---|
879 | set_location (schema_exception<C>& e) |
---|
880 | { |
---|
881 | if (loc_ != 0) |
---|
882 | { |
---|
883 | const XMLCh* id (loc_->getPublicId ()); |
---|
884 | |
---|
885 | if (id == 0) |
---|
886 | id = loc_->getSystemId (); |
---|
887 | |
---|
888 | if (id != 0) |
---|
889 | e.id (xml::transcode<C> (id)); |
---|
890 | |
---|
891 | XMLSSize_t l (loc_->getLineNumber ()); |
---|
892 | XMLSSize_t c (loc_->getColumnNumber ()); |
---|
893 | |
---|
894 | e.line (l == -1 ? 0 : static_cast<unsigned long> (l)); |
---|
895 | e.column (c == -1 ? 0: static_cast<unsigned long> (c)); |
---|
896 | } |
---|
897 | } |
---|
898 | } |
---|
899 | } |
---|
900 | } |
---|
901 | } |
---|