summaryrefslogtreecommitdiffstats
path: root/guththila/include/guththila_xml_writer.h
blob: afc5a26de2615117850eae7d1cb448f7a825ae43 (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
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef GUTHTHILA_XML_WRITER_H
#define GUTHTHILA_XML_WRITER_H

#include <guththila_token.h>
#include <guththila_defines.h>
#include <guththila_buffer.h>
#include <guththila.h>
#include <axutil_utils.h>

EXTERN_C_START()
#define GUTHTHILA_XML_WRITER_TOKEN

/*
Design notes:-
namesp member of guththila_xml_writer_s is populated with malloc created objects.
Providing a list for this seems expensive because most of the times only few
namespaces are present in a XML document.

element member of guththila_xml_writer_s must be povided the list capablity. This
is particularly important in very deep XML documents.
*/
typedef enum guththila_writer_type_s
{
    GUTHTHILA_WRITER_FILE = 1,
    GUTHTHILA_WRITER_MEMORY
} guththila_writer_type_t;

typedef struct guththila_writer_s
{
    short type;
    FILE *out_stream;
    guththila_buffer_t *buffer;
    int next;
}
guththila_writer_t;

typedef enum guththila_writer_status_s
{
    /*Started writing a non empty element */
    START = 1,
    /*Started writing a empty element */
    START_EMPTY,
    /*We are in a position to begin wrting an element */
    BEGINING
} guththila_writer_status_t;

/*Main structure which provides the writer capability*/
typedef struct guththila_xml_writer_s
{
    guththila_stack_t element;
    guththila_stack_t namesp;
    guththila_writer_t *writer;
#ifdef GUTHTHILA_XML_WRITER_TOKEN
    guththila_tok_list_t tok_list;
#endif
    /* Type of this writer. Can be file writer or memory writer */
    guththila_writer_type_t type;

    FILE *out_stream;
    guththila_buffer_t buffer;
    guththila_writer_status_t status;
    int next;
} guththila_xml_writer_t;

/*TODO: we need to came up with common implementation of followng two structures in writer and reader*/

/*
This is a private structure for keeping track of the elements. When we start to write an element this structure will be pop
*/
typedef struct guththila_xml_writer_element_s
{
#ifdef GUTHTHILA_XML_WRITER_TOKEN
    guththila_token_t *prefix;
    guththila_token_t *name;
#else
    guththila_char_t *prefix;
    guththila_char_t *name;
#endif
    /* contains the number of the stack which holds the namespaces
       for this element. When we close this element all the namespaces 
       that are below this should also must be closed */
    int name_sp_stack_no;
}
guththila_xml_writer_element_t;

typedef struct guththila_xml_writer_namesp_s
{
    /* These are double pointers because a single element may contain multple
       namespace declarations */
#ifdef GUTHTHILA_XML_WRITER_TOKEN
    guththila_token_t **name;
    guththila_token_t **uri;
#else
    guththila_char_t **name;
    guththila_char_t **uri;
#endif
    int no;             /*number of namespaces */
    int size;
}
guththila_xml_writer_namesp_t;

#define GUTHTHILA_XML_WRITER_NAMESP_DEF_SIZE 4

/*Writer functions*/

/* 
 * Create a writer which writes to a file.
 * @param file_name name of the file
 * @param env pointer to the environment
 */
GUTHTHILA_EXPORT guththila_xml_writer_t *GUTHTHILA_CALL
guththila_create_xml_stream_writer(
    char *file_name,
    const axutil_env_t * env);

/* 
 * Create a writer which writes to a memory buffer.
 * @param env pointer to the environment
 */
GUTHTHILA_EXPORT guththila_xml_writer_t *GUTHTHILA_CALL
guththila_create_xml_stream_writer_for_memory(
    const axutil_env_t * env);

/* 
 * Jus write what ever the content in the buffer. If the writer was in 
 * a start of a element it will close it.
 * @param wr pointer to the writer
 * @param buff buffer containing the data
 * @param size size of the buffer
 * @param env pointer to the environment
 */
GUTHTHILA_EXPORT int GUTHTHILA_CALL guththila_write_to_buffer(
    guththila_xml_writer_t * wr,
    char *buff,
    int size,
    const axutil_env_t * env);

/*
 * Write the name space with the given prifix and namespace.
 * @param wr pointer to the writer
 * @param prefix prefix of the namespace
 * @param uri uri of the namespace
 * @param env pointer to the environment 
 */
GUTHTHILA_EXPORT int GUTHTHILA_CALL guththila_write_namespace(
    guththila_xml_writer_t * wr,
    char *prefix,
    char *uri,
    const axutil_env_t * env);

/*
 * Write the name space with the given prifix and namespace.
 * @param wr pointer to the writer
 * @param prefix prefix of the namespace
 * @param uri uri of the namespace
 * @param local_name name of the attribute
 * @param value value of the attribute
 * @param env pointer to the environment 
 */
GUTHTHILA_EXPORT int GUTHTHILA_CALL
guththila_do_write_attribute_with_prefix_and_namespace(
    guththila_xml_writer_t * wr,
    char *prefix,
    char *uri,
    char *local_name,
    char *value,
    const axutil_env_t * env);


/*
 * Write the start document element with the xml version and encoding.
 * @param wr pointer to the writer
 * @param env pointer to the environment 
 * @param encoding encoding of the XML.
 * @param version xml version
 */
GUTHTHILA_EXPORT int GUTHTHILA_CALL guththila_write_start_document(
    guththila_xml_writer_t * wr,
    const axutil_env_t * env,
    char *encoding,
    char *version);

/*
 * Write the start element.
 * @param wr pointer to the writer
 * @param name name of the element
 * @param env pointer to the environment 
 */
GUTHTHILA_EXPORT int GUTHTHILA_CALL guththila_write_start_element(
    guththila_xml_writer_t * wr,
    char *name,
    const axutil_env_t * env);

/*
 * Write the end element. 
 * @param wr pointer to the writer
 * @param env pointer to the environment 
 */
GUTHTHILA_EXPORT int GUTHTHILA_CALL guththila_write_end_element(
    guththila_xml_writer_t * wr,
    const axutil_env_t * env);

/*
 * Not implemented.
 * @param wr pointer to the writer
 * @param env pointer to the environment 
 */
GUTHTHILA_EXPORT int GUTHTHILA_CALL guththila_close(
    guththila_xml_writer_t * wr,
    const axutil_env_t * env);

/*
 * Write the text content of a element. 
 * @param wr pointer to the writer
 * @param buff character string
 * @param env pointer to the environment 
 */
GUTHTHILA_EXPORT int GUTHTHILA_CALL guththila_write_characters(
    guththila_xml_writer_t * wr,
    char *buff,
    const axutil_env_t * env);

/*
 * Write comment with the given text data. 
 * @param wr pointer to the writer
 * @param buff character string
 * @param env pointer to the environment
 */ 
GUTHTHILA_EXPORT int GUTHTHILA_CALL guththila_write_comment(
    guththila_xml_writer_t * wr,
    char *buff,
    const axutil_env_t * env);

/*
 * Write scape character. 
 * @param wr pointer to the writer
 * @param buff character string
 * @param env pointer to the environment
 */ 
GUTHTHILA_EXPORT int GUTHTHILA_CALL guththila_write_escape_character(
    guththila_xml_writer_t * wr,
    char *buff,
    const axutil_env_t * env);

/*
 * Start to write an empty element with the given name. 
 * @param wr pointer to the writer
 * @param name name of the element
 * @param env pointer to the environment
 */ 
GUTHTHILA_EXPORT int GUTHTHILA_CALL guththila_write_empty_element(
    guththila_xml_writer_t * wr,
    char *name,
    const axutil_env_t * env);

/*
 * Write a defualt namespace. 
 * @param wr pointer to the writer
 * @param uri uri of the namespace
 * @param env pointer to the environment
 */ 
GUTHTHILA_EXPORT int GUTHTHILA_CALL guththila_write_default_namespace(
    guththila_xml_writer_t * wr,
    char *uri,
    const axutil_env_t * env);

/*
 * Write a attribute with the given name and value. 
 * @param wr pointer to the writer
 * @param localname name of the attribute
 * @param value value of the attribute
 * @param env pointer to the environment
 */ 
GUTHTHILA_EXPORT int GUTHTHILA_CALL guththila_write_attribute(
    guththila_xml_writer_t * wr,
    char *localname,
    char *value,
    const axutil_env_t * env);

/*
 * Write a attribute with the given name and value and namespace. 
 * @param wr pointer to the writer
 * @param prefix prefix of the attribute
 * @param namespace_uri uri of the namespace
 * @param localname name of the attribute
 * @param value value of the attribute
 * @param env pointer to the environment
 */ 
GUTHTHILA_EXPORT int GUTHTHILA_CALL
guththila_write_attribute_with_prefix_and_namespace(
    guththila_xml_writer_t * wr,
    char *prefix,
    char *namespace_uri,
    char *localname,
    char *value,
    const axutil_env_t * env);

/*
 * Write a attribute with the given name, value and prefix. If the prefix 
 * is not defined previously as a namespace this method will fail. 
 * @param wr pointer to the writer
 * @param prefix prefix of the attribute
 * @param localname name of the attribute
 * @param value value of the attribute
 * @param env pointer to the environment
 */ 
GUTHTHILA_EXPORT int GUTHTHILA_CALL guththila_write_attribute_with_prefix(
    guththila_xml_writer_t * wr,
    char *prefix,
    char *localname,
    char *value,
    const axutil_env_t * env);

/*
 * Write a attribute with the given name, value and namespace uri.  
 * If the namespace is not defined previously as a namespace this 
 * method will fail. The prefix corresponding to the namespace uri 
 * will be used. 
 * @param wr pointer to the writer
 * @param namesp namespace uri
 * @param localname name of the attribute
 * @param value value of the attribute
 * @param env pointer to the environment
 */ 
GUTHTHILA_EXPORT int GUTHTHILA_CALL guththila_write_attribute_with_namespace(
    guththila_xml_writer_t * wr,
    char *namesp,
    char *localname,
    char *value,
    const axutil_env_t * env);

/*
 * Write a start element with prefix and namespace. If the namespace is not 
 * defined previoully new namespace will be written. 
 * @param wr pointer to the writer
 * @param prefix prefix of the attribute
 * @param namespace_uri uri
 * @param localname name of the attribute
 * @param env pointer to the environment
 */ 
GUTHTHILA_EXPORT int GUTHTHILA_CALL
guththila_write_start_element_with_prefix_and_namespace(
    guththila_xml_writer_t * wr,
    char *prefix,
    char *namespace_uri,
    char *local_name,
    const axutil_env_t * env);

/*
 * Write a start element with the namespace. If the namespace is not 
 * defined previously method will fail. 
 * @param wr pointer to the writer
 * @param namespace_uri uri
 * @param localname name of the attribute
 * @param env pointer to the environment
 */ 
GUTHTHILA_EXPORT int GUTHTHILA_CALL
guththila_write_start_element_with_namespace(
    guththila_xml_writer_t * wr,
    char *namespace_uri,
    char *local_name,
    const axutil_env_t * env);

/*
 * Write a start element with the prefix. If the prefix is not 
 * defined previously method will fail. 
 * @param wr pointer to the writer
 * @param namespace_uri uri
 * @param localname name of the attribute
 * @param env pointer to the environment
 */ 
GUTHTHILA_EXPORT int GUTHTHILA_CALL
guththila_write_start_element_with_prefix(
    guththila_xml_writer_t * wr,
    char *prefix,
    char *local_name,
    const axutil_env_t * env);

/*
 * Write a empty element with prefix and namespace. If the namespace is not 
 * defined previoully new namespace will be written. 
 * @param wr pointer to the writer
 * @param prefix prefix of the attribute
 * @param namespace_uri uri
 * @param localname name of the attribute
 * @param env pointer to the environment
 */ 
GUTHTHILA_EXPORT int GUTHTHILA_CALL
guththila_write_empty_element_with_prefix_and_namespace(
    guththila_xml_writer_t * wr,
    char *prefix,
    char *namespace_uri,
    char *local_name,
    const axutil_env_t * env);

/*
 * Write a empty element with the namespace. If the namespace is not 
 * defined previously method will fail. 
 * @param wr pointer to the writer
 * @param namespace_uri uri
 * @param localname name of the attribute
 * @param env pointer to the environment
 */ 
GUTHTHILA_EXPORT int GUTHTHILA_CALL
guththila_write_empty_element_with_namespace(
    guththila_xml_writer_t * wr,
    char *namespace_uri,
    char *local_name,
    const axutil_env_t * env);

/*
 * Write a empty element with the prefix. If the prefix is not 
 * defined previously method will fail. 
 * @param wr pointer to the writer
 * @param namespace_uri uri
 * @param localname name of the attribute
 * @param env pointer to the environment
 */ 
GUTHTHILA_EXPORT int GUTHTHILA_CALL
guththila_write_empty_element_with_prefix(
    guththila_xml_writer_t * wr,
    char *prefix,
    char *local_name,
    const axutil_env_t * env);

/*
 * Close all the elements that were started by writing the end elements.
 * @param wr pointer to the writer
 * @param env pointer to the environment
 */ 
GUTHTHILA_EXPORT int GUTHTHILA_CALL guththila_write_end_document(
    guththila_xml_writer_t * wr,
    const axutil_env_t * env);
/* 
 * Write a new element with the name, then write the characters as text, 
 * then close the element and write a new line.
 * @param wr pointer to the writer
 * @element_name name of the element
 * @characters text of the new element
 * @param env pointer to the environment
 */
GUTHTHILA_EXPORT int GUTHTHILA_CALL guththila_write_line(
    guththila_xml_writer_t * wr,
    char *element_name,
    char *characters,
    const axutil_env_t * env);

/*
 * Get the memory buffer that is written.  
 * @param wr pointer to the writer
 * @param env pointer to the environment
 * @return memory buffer
 */
GUTHTHILA_EXPORT char *GUTHTHILA_CALL guththila_get_memory_buffer(
    guththila_xml_writer_t * wr,
    const axutil_env_t * env);

/*
 * Get the size of the memory buffer. 
 * @param wr pointer to the writer
 * @param env pointer to the environment
 * @return size of the buffer
 */
GUTHTHILA_EXPORT unsigned int GUTHTHILA_CALL
guththila_get_memory_buffer_size(
    guththila_xml_writer_t * wr,
    const axutil_env_t * env);

/*
 * Free the writer. 
 * @param wr pointer to the writer
 * @param env pointer to the environment
 */
GUTHTHILA_EXPORT void GUTHTHILA_CALL guththila_xml_writer_free(
    guththila_xml_writer_t * wr,
    const axutil_env_t * env);
/*
 * Get the prefix for the namespace.
 * @param wr pointer to the writer
 * @namespace namespace uri
 * @param env pointer to the environment
 * @return prefix for the namspace uri
 */
GUTHTHILA_EXPORT char *GUTHTHILA_CALL guththila_get_prefix_for_namespace(
    guththila_xml_writer_t * wr,
    char *namespace,
    const axutil_env_t * env);

EXTERN_C_END()
#endif