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

#ifndef XSDE_CXX_SERIALIZER_CONTEXT_HXX
#define XSDE_CXX_SERIALIZER_CONTEXT_HXX

#include <xsde/cxx/config.hxx>

#include <stddef.h> // size_t

#include <xsde/c/genx/genx.h>
#include <xsde/cxx/string.hxx>

#ifndef XSDE_EXCEPTIONS
#  include <xsde/cxx/sys-error.hxx>
#endif

#include <xsde/cxx/serializer/genx/xml-error.hxx>

#ifdef XSDE_SERIALIZER_VALIDATION
#  include <xsde/cxx/schema-error.hxx>
#endif

namespace xsde
{
  namespace cxx
  {
    namespace serializer
    {
      class context
      {
      public:
        context (genxWriter xml_serializer);

      private:
        context (const context&);
        context& operator= (const context&);

      public:
        genxWriter
        xml_serializer ();

#ifdef XSDE_POLYMORPHIC
      public:
        // Set/get the dynamic serializer type id (as opaque const void*)
        // in case of polymorphic serialization. If type id is not set,
        // static type is assumed.
        //
        void
        type_id (const void*);

        const void*
        type_id ();
#endif

      public:
#ifdef XSDE_EXCEPTIONS
        void
        start_element (const char* name);

        void
        start_element (const char* ns, const char* name);

        void
        end_element ();

        void
        start_attribute (const char* name);

        void
        start_attribute (const char* ns, const char* name);

        void
        end_attribute ();

        void
        attribute (const char* name, const char* value);

        void
        attribute (const char* ns, const char* name, const char* value);

        void
        characters (const char*);

        void
        characters (const char*, size_t);

        void
        declare_namespace (const char* ns, const char* prefix);

        void
        declare_default_namespace (const char* ns);

        void
        clear_default_namespace ();

        const char*
        lookup_namespace_prefix (const char* ns);

#ifdef XSDE_POLYMORPHIC
        void
        set_type (const char* type);
#endif
#else
        bool
        start_element (const char* name);

        bool
        start_element (const char* ns, const char* name);

        bool
        end_element ();

        bool
        start_attribute (const char* name);

        bool
        start_attribute (const char* ns, const char* name);

        bool
        end_attribute ();

        bool
        attribute (const char* name, const char* value);

        bool
        attribute (const char* ns, const char* name, const char* value);

        bool
        characters (const char*);

        bool
        characters (const char*, size_t);

        bool
        declare_namespace (const char* ns, const char* prefix);

        bool
        declare_default_namespace (const char* ns);

        bool
        clear_default_namespace ();

        const char*
        lookup_namespace_prefix (const char* ns);

#ifdef XSDE_POLYMORPHIC
        bool
        set_type (const char* type);
#endif
#endif

        // Error handling via exceptions.
        //

#ifdef XSDE_EXCEPTIONS
      public:
        void
        throw_xml_error (genx::xml_error);
#endif

        // Error handling via codes.
        //

        // Application error.
        //
#ifndef XSDE_EXCEPTIONS
      public:
        int
        app_error () const;

        void
        app_error (int);
#endif

        // Schema error.
        //
#ifdef XSDE_SERIALIZER_VALIDATION
      public:
        typedef cxx::schema_error::value schema_error_t;

        schema_error_t
        schema_error () const;

        void
        schema_error (schema_error_t);
#endif

        // XML error.
        //
#ifndef XSDE_EXCEPTIONS
      public:
        typedef genx::xml_error xml_error_t;

        xml_error_t
        xml_error () const;

        void
        xml_error (xml_error_t);
#endif

        // System error.
        //
#ifndef XSDE_EXCEPTIONS
      public:
        typedef cxx::sys_error::value sys_error_t;

        sys_error_t
        sys_error () const;

        void
        sys_error (sys_error_t);
#endif

        // Implementation details.
        //
#if defined(XSDE_SERIALIZER_VALIDATION) || !defined(XSDE_EXCEPTIONS)
      public:
        enum error_type_t
        {
          error_none = 0,
          error_app,
          error_schema,
          error_xml,
          error_sys
        };

        error_type_t
        error_type () const;

      protected:
        error_type_t error_type_;

        union
        {
#ifndef XSDE_EXCEPTIONS
          int app;
          xml_error_t xml;
#endif
#ifdef XSDE_SERIALIZER_VALIDATION
          schema_error_t schema;
#endif
#ifndef XSDE_EXCEPTIONS
          sys_error_t sys;
#endif
        } error_code_;

#endif // XSDE_SERIALIZER_VALIDATION || !XSDE_EXCEPTIONS

      protected:
        genxWriter xml_serializer_;

#ifdef XSDE_POLYMORPHIC
        const void* type_id_;
#endif

        // Support for ISO-8859-1 conversion.
        //
#ifdef XSDE_ENCODING_ISO8859_1
      protected:
        const char*
        conv_data (const char* iso_s, size_t utf_n, string& var);

        const char*
        conv_data (const char* iso_s, size_t iso_n, size_t utf_n, string& var);

        const char*
        conv_name (const char* iso_s, size_t utf_n, char* fix, string& var);

        char data_buf_[256];
        char name_buf1_[128];
        char name_buf2_[128]; // Keep buf1 and buf2 sizes the same.
#endif
      };
    }
  }
}

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

#endif  // XSDE_CXX_SERIALIZER_CONTEXT_HXX