aboutsummaryrefslogtreecommitdiff
path: root/libxsde/xsde/cxx/serializer/context.cxx
blob: f340e7ab0e8d4bea26845341f5771d4f393d40a6 (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
// file      : xsde/cxx/serializer/context.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <xsde/cxx/config.hxx>
#include <xsde/cxx/ro-string.hxx>

#ifdef XSDE_EXCEPTIONS
#  include <new> // std::bad_alloc
#  include <xsde/cxx/serializer/exceptions.hxx>
#endif

#include <xsde/cxx/serializer/context.hxx>

namespace xsde
{
  namespace cxx
  {
    namespace serializer
    {
#ifdef XSDE_POLYMORPHIC
#ifdef XSDE_EXCEPTIONS
      void context::
#else
      bool context::
#endif
      set_type (const char* type)
      {
        ro_string t (type);
        const char* prefix = 0;

        size_t pos = t.find (' ');

        if (pos != ro_string::npos)
        {
          prefix = lookup_namespace_prefix (type + pos + 1);

          //@@ TODO: what if prefix is not found?
          //
#ifndef XSDE_EXCEPTIONS
          if (prefix == 0)
            return false;
#endif
          if (*prefix == '\0')
            prefix = 0;
        }

#ifdef XSDE_EXCEPTIONS
        start_attribute ("http://www.w3.org/2001/XMLSchema-instance", "type");

        if (prefix)
        {
          characters (prefix);
          characters (":");
        }

        characters (type, pos != ro_string::npos ? pos : t.size ());
        end_attribute ();
#else
        if (!start_attribute (
              "http://www.w3.org/2001/XMLSchema-instance", "type"))
          return false;

        if (prefix)
        {
          if (!characters (prefix) || !characters (":"))
            return false;
        }

        if (!characters (type, pos != ro_string::npos ? pos : t.size ()) ||
            !end_attribute ())
          return false;

        return true;
#endif
      }
#endif // XSDE_POLYMORPHIC

#ifdef XSDE_EXCEPTIONS
      void context::
      throw_xml_error (genx::xml_error e)
      {
        switch (e)
        {
        case GENX_ALLOC_FAILED:
          {
            throw std::bad_alloc ();
          }
        case GENX_IO_ERROR:
          {
            // This should never happen with consistent exception
            // handling usage since the write/flush functions
            // throw exceptions to indicate write failures.
            //
            throw xml (e);
          }
        default:
          {
            throw xml (e);
          }
        }
      }
#endif
    }
  }
}