summaryrefslogtreecommitdiff
path: root/libxsd-frontend/xsd-frontend/xml.hxx
blob: d33f6bf342831177b510021330c8425a101ded0c (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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// file      : xsd-frontend/xml.hxx
// copyright : Copyright (c) 2005-2014 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#ifndef XSD_FRONTEND_XML_HXX
#define XSD_FRONTEND_XML_HXX

#include <vector>
#include <ostream>

#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/XMLString.hpp>

#include <xsd-frontend/version.hxx>            // Check Xerces-C++ version.
#include <xsd-frontend/types.hxx>
#include <xsd-frontend/schema-dom-parser.hxx>

namespace XSDFrontend
{
  namespace XML
  {
    namespace Xerces = xercesc;

    inline
    String
    transcode (XMLCh const* s, size_t length)
    {
      if (sizeof (wchar_t) == 4)
      {
        // UTF-32
        //
        XMLCh const* end (s + length);

        // Find what the resulting buffer size will be.
        //
        size_t rl (0);
        bool valid (true);

        for (XMLCh const* p (s); p < end; ++p)
        {
          rl++;

          if ((*p >= 0xD800) && (*p <= 0xDBFF))
          {
            // Make sure we have one more char and it has a valid
            // value for the second char in a surrogate pair.
            //
            if (++p == end || !((*p >= 0xDC00) && (*p <= 0xDFFF)))
            {
              valid = false;
              break;
            }
          }
        }

        if (!valid)
          return String ();

        String r;
        r.reserve (rl + 1);
        r.resize (rl);
        wchar_t* rs (const_cast<wchar_t*> (r.c_str ()));

        size_t i (0);

        for (XMLCh const* p (s); p < end; ++p)
        {
          XMLCh x (*p);

          if (x < 0xD800 || x > 0xDBFF)
            rs[i++] = wchar_t (x);
          else
            rs[i++] = ((x - 0xD800) << 10) + (*++p - 0xDC00) + 0x10000;
        }

        return r;
      }
      else if (sizeof (wchar_t) == 2)
      {
        // UTF-16
        //
        return String (reinterpret_cast<const wchar_t*> (s), length);
      }
      else
        return String ();
    }

    inline
    String
    transcode (XMLCh const* s)
    {
      return transcode (s,  Xerces::XMLString::stringLen (s));
    }

    inline
    NarrowString
    transcode_to_narrow (XMLCh const* xs)
    {
      char* s (Xerces::XMLString::transcode (xs));
      NarrowString r (s);
      Xerces::XMLString::release (&s);
      return r;
    }

    inline
    XMLCh*
    transcode (String const& str)
    {
      size_t l (str.size ());
      wchar_t const* s (str.c_str ());

      if (sizeof (wchar_t) == 4)
      {
        // Find what the resulting buffer size will be.
        //
        size_t rl (0);

        for (wchar_t const* p (s); p < s + l; ++p)
        {
          rl += (*p & 0xFFFF0000) ? 2 : 1;
        }

        XMLCh* r (new XMLCh[rl + 1]);
        XMLCh* ir (r);

        for (wchar_t const* p (s); p < s + l; ++p)
        {
          wchar_t w (*p);

          if (w & 0xFFFF0000)
          {
            // Surrogate pair.
            //
            *ir++ = static_cast<XMLCh> (((w - 0x10000) >> 10) + 0xD800);
            *ir++ = static_cast<XMLCh> ((w & 0x3FF) + 0xDC00);
          }
          else
            *ir++ = static_cast<XMLCh> (w);
        }

        *ir = XMLCh (0);

        return r;
      }
      else if (sizeof (wchar_t) == 2)
      {
        XMLCh* r (new XMLCh[l + 1]);
        XMLCh* ir (r);

        for (size_t i (0); i < l; ++ir, ++i)
          *ir = static_cast<XMLCh> (s[i]);

        *ir = XMLCh (0);

        return r;
      }
      else
        return 0;
    }

    class XMLChString
    {
    public :
      XMLChString (String const& s)
          : s_ (transcode (s))
      {
      }

      XMLChString (wchar_t const* s)
          : s_ (transcode (String (s)))
      {
      }

      ~XMLChString ()
      {
        delete[] s_;
      }

      XMLCh const*
      c_str () const
      {
        return s_;
      }

    private:
      XMLChString (XMLChString const&);

      XMLChString&
      operator= (XMLChString const&);

    private:
      XMLCh* s_;
    };


    class Element
    {
    public:
      Element (Xerces::DOMElement* e)
          : e_ (e),
            name_ (transcode (e->getLocalName ())),
            namespace__ (transcode (e->getNamespaceURI ()))
      {
      }

      String
      name () const
      {
        return name_;
      }

      String
      namespace_ () const
      {
        return namespace__;
      }

    public:
      unsigned long
      line () const
      {
        //@@ cache
        //
        return reinterpret_cast<unsigned long> (e_->getUserData (line_key));
      }

      unsigned long
      column () const
      {
        //@@ cache
        //
        return reinterpret_cast<unsigned long> (e_->getUserData (column_key));
      }

    public:
      Element
      parent () const
      {
        return dynamic_cast<Xerces::DOMElement*>(e_->getParentNode ());
      }

    public:
      // Attribute identified by a name.
      //
      bool
      attribute_p (String const& name) const
      {
        return attribute_p ("", name);
      }

      String
      attribute (String const& name) const
      {
        return attribute ("", name);
      }

      String
      operator[] (String const& name) const
      {
        return attribute (name);
      }

      // Attribute identified by namespace and name.
      //

      bool
      attribute_p (String const& namespace_, String const& name) const
      {
        Xerces::DOMAttr* a (
          e_->getAttributeNodeNS (
            XMLChString (namespace_).c_str (),
            XMLChString (name).c_str ()));

        return a != 0;
      }

      String
      attribute (String const& namespace_, String const& name) const
      {
        XMLCh const* value (
          e_->getAttributeNS (
            XMLChString (namespace_).c_str (),
            XMLChString (name).c_str ()));

        return transcode (value);
      }

    public:
      Xerces::DOMElement*
      dom_element () const
      {
        return e_;
      }

    private:
      Xerces::DOMElement* e_;

      String name_;
      String namespace__;
    };

    inline String
    prefix (String const& n)
    {
      size_t i (0);
      while (i < n.length () && n[i] != L':') ++i;

      //std::wcerr << "prefix " << n << " "
      //           << String (n, i == n.length () ? i : 0, i) << std::endl;

      return String (n, i == n.length () ? i : 0, i);
    }

    inline String
    uq_name (String const& n)
    {
      size_t i (0);
      while (i < n.length () && n[i] != L':') ++i;

      return String (n.c_str () + (i == n.length () ? 0 : i + 1));
    }

    struct NoMapping
    {
      NoMapping (String const& prefix)
          : prefix_ (prefix)
      {
      }

      String const&
      prefix () const
      {
        return prefix_;
      }

    private:
      String prefix_;
    };

    // Throws NoMapping if there is no prefix-namespace association.
    //
    inline String
    ns_name (Xerces::DOMElement const* e, String const& prefix)
    {
      // 'xml' prefix requires special handling and Xerces folks refuse
      // to handle this in DOM so I have to do it myself.
      //
      if (prefix == L"xml")
        return L"http://www.w3.org/XML/1998/namespace";

      // 0 means "no prefix" to Xerces.
      //
      XMLCh const* xns (
        e->lookupNamespaceURI (
          prefix.empty () ? 0 : XMLChString (prefix).c_str ()));

      if (xns == 0)
        throw NoMapping (prefix);

      return transcode (xns);
    }

    class NoPrefix {};

    inline String
    ns_prefix (Element const& e, String const& wns)
    {
      XMLChString ns (wns);
      XMLCh const* p (e.dom_element ()->lookupPrefix (ns.c_str ()));

      if (p == 0)
      {
        bool r (e.dom_element ()->isDefaultNamespace (ns.c_str ()));

        if (r)
          return L"";
        else
        {
          // 'xml' prefix requires special handling and Xerces folks refuse
          // to handle this in DOM so I have to do it myself.
          //
          if (wns == L"http://www.w3.org/XML/1998/namespace")
            return L"xml";

          throw NoPrefix ();
        }
      }

      return transcode (p);
    }

    inline String
    fq_name (Element const& e, String const& n)
    {
      String un (uq_name (n));

      try
      {
        String ns (ns_name (e.dom_element (), prefix (n)));
        return ns + L'#' + un;
      }
      catch (XML::NoMapping const&)
      {
        return un;
      }
    }


    // Simple auto_ptr version that calls release() instead of delete.
    //

    template <typename X>
    struct AutoPtrRef
    {
      X* x_;

      explicit
      AutoPtrRef (X* x)
          : x_ (x)
      {
      }
    };

    template <typename X>
    struct AutoPtr
    {
      ~AutoPtr ()
      {
        reset ();
      }

      explicit
      AutoPtr (X* x = 0)
          : x_ (x)
      {
      }

      AutoPtr (AutoPtr& y)
          : x_ (y.release ())
      {
      }

      AutoPtr (AutoPtrRef<X> r)
          : x_ (r.x_)
      {
      }

      AutoPtr&
      operator= (AutoPtr& y)
      {
        if (this != &y)
        {
          reset (y.release ());
        }

        return *this;
      }

      AutoPtr&
      operator= (AutoPtrRef<X> r)
      {
        if (r.x_ != x_)
        {
          reset (r.x_);
        }

        return *this;
      }

      operator AutoPtrRef<X> ()
      {
        return AutoPtrRef<X> (release ());
      }

    public:
      X&
      operator* () const
      {
        return *x_;
      }

      X*
      operator-> () const
      {
        return x_;
      }

      X*
      get () const
      {
        return x_;
      }

      X*
      release ()
      {
        X* x (x_);
        x_ = 0;
        return x;
      }

      void
      reset (X* x = 0)
      {
        if (x_)
          x_->release ();

        x_ = x;
      }

      // Conversion to bool.
      //
      typedef X* (AutoPtr::*boolConvertible)() const;

      operator boolConvertible () const throw ()
      {
        return x_ ? &AutoPtr<X>::operator-> : 0;
      }

    private:
      X* x_;
    };

    template <typename X>
    struct PtrVector: std::vector<X*>
    {
      typedef std::vector<X*> Base;

      ~PtrVector ()
      {
        for (typename Base::iterator i (this->begin ()), e (this->end ());
             i != e; ++i)
        {
          if (*i)
            (*i)->release ();
        }
      }

      void
      push_back (AutoPtr<X>& x)
      {
        Base::push_back (0);
        this->back () = x.release ();
      }
    };
  }
}

// Xerces DOoM.
//
//
inline
std::wostream&
operator<< (std::wostream& o, XMLCh const* s)
{
  return o << XSDFrontend::XML::transcode (s);
}

#endif  // XSD_FRONTEND_XML_HXX