aboutsummaryrefslogtreecommitdiff
path: root/libxsd/xsd/cxx/xml/dom/parsing-source.txx
blob: 545eb822a7b2cb323c0f9d88c772917e9d1c1325 (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// file      : xsd/cxx/xml/dom/parsing-source.txx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#if _XERCES_VERSION >= 30000
#  include <xercesc/dom/DOMLSParser.hpp>
#  include <xercesc/dom/DOMLSException.hpp>
#else
#  include <xercesc/dom/DOMBuilder.hpp>
#endif
#include <xercesc/dom/DOMNamedNodeMap.hpp>
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMImplementationRegistry.hpp>

#include <xercesc/util/XMLUni.hpp>     // xercesc::fg*
#include <xercesc/util/XMLUniDefs.hpp> // chLatin_L, etc

#include <xercesc/framework/Wrapper4InputSource.hpp>

#include <xsd/cxx/xml/string.hxx>
#include <xsd/cxx/xml/dom/bits/error-handler-proxy.hxx>

namespace xsd
{
  namespace cxx
  {
    namespace xml
    {
      namespace dom
      {
        // parser
        //
        template <typename C>
        parser<C>::
        parser (const xercesc::DOMElement& e, bool ep, bool ap)
            : element_ (e),
              next_element_ (0),
              a_ (0),
              ai_ (0)
        {
          using xercesc::DOMNode;

          if (ep)
          {
            for (next_element_ = e.getFirstChild ();
                 next_element_ != 0 &&
                   next_element_->getNodeType () != DOMNode::ELEMENT_NODE;
                 next_element_ = next_element_->getNextSibling ()) /*noop*/;
          }

          if (ap)
          {
            a_ = e.getAttributes ();
            as_ = a_->getLength ();
          }
        }

        template <typename C>
        void parser<C>::
        next_element ()
        {
          using xercesc::DOMNode;

          for (next_element_ = next_element_->getNextSibling ();
               next_element_ != 0 &&
                 next_element_->getNodeType () != DOMNode::ELEMENT_NODE;
               next_element_ = next_element_->getNextSibling ())/*noop*/;
        }

        // parse()
        //
        template <typename C>
        xml::dom::auto_ptr<xercesc::DOMDocument>
        parse (xercesc::InputSource& is,
               error_handler<C>& eh,
               const properties<C>& prop,
               unsigned long flags)
        {
          bits::error_handler_proxy<C> ehp (eh);
          return xml::dom::parse (is, ehp, prop, flags);
        }

        template <typename C>
        auto_ptr<xercesc::DOMDocument>
        parse (xercesc::InputSource& is,
               xercesc::DOMErrorHandler& eh,
               const properties<C>& prop,
               unsigned long flags)
        {
          // HP aCC cannot handle using namespace xercesc;
          //
          using xercesc::DOMImplementationRegistry;
          using xercesc::DOMImplementationLS;
          using xercesc::DOMImplementation;
          using xercesc::DOMDocument;
#if _XERCES_VERSION >= 30000
          using xercesc::DOMLSParser;
          using xercesc::DOMConfiguration;
#else
          using xercesc::DOMBuilder;
#endif

          using xercesc::Wrapper4InputSource;
          using xercesc::XMLUni;


          // Instantiate the DOM parser.
          //
          const XMLCh ls_id[] = {xercesc::chLatin_L,
                                 xercesc::chLatin_S,
                                 xercesc::chNull};

          // Get an implementation of the Load-Store (LS) interface.
          //
          DOMImplementation* impl (
            DOMImplementationRegistry::getDOMImplementation (ls_id));

#if _XERCES_VERSION >= 30000
          auto_ptr<DOMLSParser> parser (
            impl->createLSParser (DOMImplementationLS::MODE_SYNCHRONOUS, 0));

          DOMConfiguration* conf (parser->getDomConfig ());

          // Discard comment nodes in the document.
          //
          conf->setParameter (XMLUni::fgDOMComments, false);

          // Enable datatype normalization.
          //
          conf->setParameter (XMLUni::fgDOMDatatypeNormalization, true);

          // Do not create EntityReference nodes in the DOM tree. No
          // EntityReference nodes will be created, only the nodes
          // corresponding to their fully expanded substitution text
          // will be created.
          //
          conf->setParameter (XMLUni::fgDOMEntities, false);

          // Perform namespace processing.
          //
          conf->setParameter (XMLUni::fgDOMNamespaces, true);

          // Do not include ignorable whitespace in the DOM tree.
          //
          conf->setParameter (XMLUni::fgDOMElementContentWhitespace, false);

          if (flags & dont_validate)
          {
            conf->setParameter (XMLUni::fgDOMValidate, false);
            conf->setParameter (XMLUni::fgXercesSchema, false);
            conf->setParameter (XMLUni::fgXercesSchemaFullChecking, false);
          }
          else
          {
            conf->setParameter (XMLUni::fgDOMValidate, true);
            conf->setParameter (XMLUni::fgXercesSchema, true);

            // Xerces-C++ 3.1.0 is the first version with working multi import
            // support.
            //
#if _XERCES_VERSION >= 30100
            if (!(flags & no_muliple_imports))
              conf->setParameter (XMLUni::fgXercesHandleMultipleImports, true);
#endif

            // This feature checks the schema grammar for additional
            // errors. We most likely do not need it when validating
            // instances (assuming the schema is valid).
            //
            conf->setParameter (XMLUni::fgXercesSchemaFullChecking, false);
          }

          // We will release DOM ourselves.
          //
          conf->setParameter (XMLUni::fgXercesUserAdoptsDOMDocument, true);


          // Transfer properies if any.
          //

          if (!prop.schema_location ().empty ())
          {
            xml::string sl (prop.schema_location ());
            const void* v (sl.c_str ());

            conf->setParameter (
              XMLUni::fgXercesSchemaExternalSchemaLocation,
              const_cast<void*> (v));
          }

          if (!prop.no_namespace_schema_location ().empty ())
          {
            xml::string sl (prop.no_namespace_schema_location ());
            const void* v (sl.c_str ());

            conf->setParameter (
              XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
              const_cast<void*> (v));
          }

          // If external schema location was specified, disable loading
          // schemas via the schema location attributes in the document.
          //
#if _XERCES_VERSION >= 30100
          if (!prop.schema_location ().empty () ||
              !prop.no_namespace_schema_location ().empty ())
          {
            conf->setParameter (XMLUni::fgXercesLoadSchema, false);
          }
#endif
          // Set error handler.
          //
          bits::error_handler_proxy<C> ehp (eh);
          conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp);

#else // _XERCES_VERSION >= 30000

          // Same as above but for Xerces-C++ 2 series.
          //
          auto_ptr<DOMBuilder> parser (
            impl->createDOMBuilder (DOMImplementationLS::MODE_SYNCHRONOUS, 0));

          parser->setFeature (XMLUni::fgDOMComments, false);
          parser->setFeature (XMLUni::fgDOMDatatypeNormalization, true);
          parser->setFeature (XMLUni::fgDOMEntities, false);
          parser->setFeature (XMLUni::fgDOMNamespaces, true);
          parser->setFeature (XMLUni::fgDOMWhitespaceInElementContent, false);

          if (flags & dont_validate)
          {
            parser->setFeature (XMLUni::fgDOMValidation, false);
            parser->setFeature (XMLUni::fgXercesSchema, false);
            parser->setFeature (XMLUni::fgXercesSchemaFullChecking, false);
          }
          else
          {
            parser->setFeature (XMLUni::fgDOMValidation, true);
            parser->setFeature (XMLUni::fgXercesSchema, true);
            parser->setFeature (XMLUni::fgXercesSchemaFullChecking, false);
          }

          parser->setFeature (XMLUni::fgXercesUserAdoptsDOMDocument, true);

          if (!prop.schema_location ().empty ())
          {
            xml::string sl (prop.schema_location ());
            const void* v (sl.c_str ());

            parser->setProperty (
              XMLUni::fgXercesSchemaExternalSchemaLocation,
              const_cast<void*> (v));
          }

          if (!prop.no_namespace_schema_location ().empty ())
          {
            xml::string sl (prop.no_namespace_schema_location ());
            const void* v (sl.c_str ());

            parser->setProperty (
              XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
              const_cast<void*> (v));
          }

          bits::error_handler_proxy<C> ehp (eh);
          parser->setErrorHandler (&ehp);

#endif // _XERCES_VERSION >= 30000

          xercesc::Wrapper4InputSource wrap (&is, false);

#if _XERCES_VERSION >= 30000
          auto_ptr<DOMDocument> doc;

          try
          {
            doc.reset (parser->parse (&wrap));
          }
          catch (const xercesc::DOMLSException&)
          {
          }
#else
          auto_ptr<DOMDocument> doc (parser->parse (wrap));
#endif
          if (ehp.failed ())
            doc.reset ();

          return doc;
        }

        template <typename C>
        xml::dom::auto_ptr<xercesc::DOMDocument>
        parse (const std::basic_string<C>& uri,
               error_handler<C>& eh,
               const properties<C>& prop,
               unsigned long flags)
        {
          bits::error_handler_proxy<C> ehp (eh);
          return xml::dom::parse (uri, ehp, prop, flags);
        }

        template <typename C>
        auto_ptr<xercesc::DOMDocument>
        parse (const std::basic_string<C>& uri,
               xercesc::DOMErrorHandler& eh,
               const properties<C>& prop,
               unsigned long flags)
        {
          // HP aCC cannot handle using namespace xercesc;
          //
          using xercesc::DOMImplementationRegistry;
          using xercesc::DOMImplementationLS;
          using xercesc::DOMImplementation;
          using xercesc::DOMDocument;
#if _XERCES_VERSION >= 30000
          using xercesc::DOMLSParser;
          using xercesc::DOMConfiguration;
#else
          using xercesc::DOMBuilder;
#endif
          using xercesc::XMLUni;


          // Instantiate the DOM parser.
          //
          const XMLCh ls_id[] = {xercesc::chLatin_L,
                                 xercesc::chLatin_S,
                                 xercesc::chNull};

          // Get an implementation of the Load-Store (LS) interface.
          //
          DOMImplementation* impl (
            DOMImplementationRegistry::getDOMImplementation (ls_id));

#if _XERCES_VERSION >= 30000
          auto_ptr<DOMLSParser> parser (
            impl->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0));

          DOMConfiguration* conf (parser->getDomConfig ());

          // Discard comment nodes in the document.
          //
          conf->setParameter (XMLUni::fgDOMComments, false);

          // Enable datatype normalization.
          //
          conf->setParameter (XMLUni::fgDOMDatatypeNormalization, true);

          // Do not create EntityReference nodes in the DOM tree. No
          // EntityReference nodes will be created, only the nodes
          // corresponding to their fully expanded substitution text
          // will be created.
          //
          conf->setParameter (XMLUni::fgDOMEntities, false);

          // Perform namespace processing.
          //
          conf->setParameter (XMLUni::fgDOMNamespaces, true);

          // Do not include ignorable whitespace in the DOM tree.
          //
          conf->setParameter (XMLUni::fgDOMElementContentWhitespace, false);

          if (flags & dont_validate)
          {
            conf->setParameter (XMLUni::fgDOMValidate, false);
            conf->setParameter (XMLUni::fgXercesSchema, false);
            conf->setParameter (XMLUni::fgXercesSchemaFullChecking, false);
          }
          else
          {
            conf->setParameter (XMLUni::fgDOMValidate, true);
            conf->setParameter (XMLUni::fgXercesSchema, true);

            // Xerces-C++ 3.1.0 is the first version with working multi import
            // support.
            //
#if _XERCES_VERSION >= 30100
            if (!(flags & no_muliple_imports))
              conf->setParameter (XMLUni::fgXercesHandleMultipleImports, true);
#endif

            // This feature checks the schema grammar for additional
            // errors. We most likely do not need it when validating
            // instances (assuming the schema is valid).
            //
            conf->setParameter (XMLUni::fgXercesSchemaFullChecking, false);
          }

          // We will release DOM ourselves.
          //
          conf->setParameter (XMLUni::fgXercesUserAdoptsDOMDocument, true);


          // Transfer properies if any.
          //

          if (!prop.schema_location ().empty ())
          {
            xml::string sl (prop.schema_location ());
            const void* v (sl.c_str ());

            conf->setParameter (
              XMLUni::fgXercesSchemaExternalSchemaLocation,
              const_cast<void*> (v));
          }

          if (!prop.no_namespace_schema_location ().empty ())
          {
            xml::string sl (prop.no_namespace_schema_location ());
            const void* v (sl.c_str ());

            conf->setParameter (
              XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
              const_cast<void*> (v));
          }

          // If external schema location was specified, disable loading
          // schemas via the schema location attributes in the document.
          //
#if _XERCES_VERSION >= 30100
          if (!prop.schema_location ().empty () ||
              !prop.no_namespace_schema_location ().empty ())
          {
            conf->setParameter (XMLUni::fgXercesLoadSchema, false);
          }
#endif
          // Set error handler.
          //
          bits::error_handler_proxy<C> ehp (eh);
          conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp);

#else // _XERCES_VERSION >= 30000

          // Same as above but for Xerces-C++ 2 series.
          //
          auto_ptr<DOMBuilder> parser (
            impl->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0));

          parser->setFeature (XMLUni::fgDOMComments, false);
          parser->setFeature (XMLUni::fgDOMDatatypeNormalization, true);
          parser->setFeature (XMLUni::fgDOMEntities, false);
          parser->setFeature (XMLUni::fgDOMNamespaces, true);
          parser->setFeature (XMLUni::fgDOMWhitespaceInElementContent, false);

          if (flags & dont_validate)
          {
            parser->setFeature (XMLUni::fgDOMValidation, false);
            parser->setFeature (XMLUni::fgXercesSchema, false);
            parser->setFeature (XMLUni::fgXercesSchemaFullChecking, false);
          }
          else
          {
            parser->setFeature (XMLUni::fgDOMValidation, true);
            parser->setFeature (XMLUni::fgXercesSchema, true);
            parser->setFeature (XMLUni::fgXercesSchemaFullChecking, false);
          }

          parser->setFeature (XMLUni::fgXercesUserAdoptsDOMDocument, true);

          if (!prop.schema_location ().empty ())
          {
            xml::string sl (prop.schema_location ());
            const void* v (sl.c_str ());

            parser->setProperty (
              XMLUni::fgXercesSchemaExternalSchemaLocation,
              const_cast<void*> (v));
          }

          if (!prop.no_namespace_schema_location ().empty ())
          {
            xml::string sl (prop.no_namespace_schema_location ());
            const void* v (sl.c_str ());

            parser->setProperty (
              XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
              const_cast<void*> (v));
          }

          bits::error_handler_proxy<C> ehp (eh);
          parser->setErrorHandler (&ehp);

#endif // _XERCES_VERSION >= 30000


#if _XERCES_VERSION >= 30000
          auto_ptr<DOMDocument> doc;

          try
          {
            doc.reset (parser->parseURI (string (uri).c_str ()));
          }
          catch (const xercesc::DOMLSException&)
          {
          }
#else
          auto_ptr<DOMDocument> doc (
            parser->parseURI (string (uri).c_str ()));
#endif

          if (ehp.failed ())
            doc.reset ();

          return doc;
        }
      }
    }
  }
}