summaryrefslogtreecommitdiff
path: root/src/libedataserver/e-xml-document.c
blob: 9bb54e7da5a2e70b9ebed0fd23dd609ae4590dd2 (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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2017 Red Hat, Inc. (www.redhat.com)
 *
 * This library is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * SECTION: e-xml-document
 * @include: libedataserver/libedataserver.h
 * @short_description: An XML document wrapper
 *
 * The #EXmlDocument class wraps creation of XML documents.
 **/

#include "evolution-data-server-config.h"

#include <string.h>

#include "e-xml-document.h"

struct _EXmlDocumentPrivate {
	xmlDocPtr doc;
	xmlNodePtr root;
	xmlNodePtr current_element;

	GHashTable *namespaces_by_href; /* gchar *ns_href ~> xmlNsPtr */
};

G_DEFINE_TYPE (EXmlDocument, e_xml_document, G_TYPE_OBJECT)

static void
e_xml_document_finalize (GObject *object)
{
	EXmlDocument *xml = E_XML_DOCUMENT (object);

	if (xml->priv->doc) {
		xmlFreeDoc (xml->priv->doc);
		xml->priv->doc = NULL;
	}

	xml->priv->root = NULL;
	xml->priv->current_element = NULL;

	if (xml->priv->namespaces_by_href) {
		g_hash_table_destroy (xml->priv->namespaces_by_href);
		xml->priv->namespaces_by_href = NULL;
	}

	/* Chain up to parent's method. */
	G_OBJECT_CLASS (e_xml_document_parent_class)->finalize (object);
}

static void
e_xml_document_class_init (EXmlDocumentClass *klass)
{
	GObjectClass *object_class;

	g_type_class_add_private (klass, sizeof (EXmlDocumentPrivate));

	object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = e_xml_document_finalize;
}

static void
e_xml_document_init (EXmlDocument *xml)
{
	xml->priv = G_TYPE_INSTANCE_GET_PRIVATE (xml, E_TYPE_XML_DOCUMENT, EXmlDocumentPrivate);

	xml->priv->doc = xmlNewDoc ((const xmlChar *) "1.0");
	g_return_if_fail (xml->priv->doc != NULL);

	xml->priv->doc->encoding = xmlCharStrdup ("UTF-8");

	xml->priv->namespaces_by_href = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
}

/**
 * e_xml_document_new:
 * @ns_href: (nullable): default namespace href to use, or %NULL
 * @root_element: root element name
 *
 * Creates a new #EXmlDocument with root element @root_element and optionally
 * also with set default namespace @ns_href.
 *
 * Returns: (transfer full): a new #EXmlDocument; free it with g_object_unref(),
 *    when no longer needed.
 *
 * Since: 3.26
 **/
EXmlDocument *
e_xml_document_new (const gchar *ns_href,
		    const gchar *root_element)
{
	EXmlDocument *xml;

	g_return_val_if_fail (root_element != NULL, NULL);
	g_return_val_if_fail (*root_element, NULL);

	xml = g_object_new (E_TYPE_XML_DOCUMENT, NULL);

	xml->priv->root = xmlNewDocNode (xml->priv->doc, NULL, (const xmlChar *) root_element, NULL);
	if (ns_href) {
		xmlNsPtr ns;

		ns = xmlNewNs (xml->priv->root, (const xmlChar *) ns_href, NULL);
		g_warn_if_fail (ns != NULL);

		xmlSetNs (xml->priv->root, ns);

		if (ns)
			g_hash_table_insert (xml->priv->namespaces_by_href, g_strdup (ns_href), ns);
	}

	xmlDocSetRootElement (xml->priv->doc, xml->priv->root);

	xml->priv->current_element = xml->priv->root;

	return xml;
}

/**
 * e_xml_document_get_xmldoc:
 * @xml: an #EXmlDocument
 *
 * Returns: (transfer none): Underlying #xmlDocPtr.
 *
 * Since: 3.26
 **/
xmlDoc *
e_xml_document_get_xmldoc (EXmlDocument *xml)
{
	g_return_val_if_fail (E_IS_XML_DOCUMENT (xml), NULL);

	return xml->priv->doc;
}

/**
 * e_xml_document_get_content:
 * @xml: an #EXmlDocument
 * @out_length: (out) (nullable): optional return location for length of the content, or %NULL
 *
 * Gets content of the @xml as string. The string is nul-terminated, but
 * if @out_length is also provided, then it doesn't contain this additional
 * nul character.
 *
 * Returns: (transfer full): Content of the @xml as newly allocated string.
 *    Free it with g_free(), when no longer needed.
 *
 * Since: 3.26
 **/
gchar *
e_xml_document_get_content (const EXmlDocument *xml,
			    gsize *out_length)
{
	xmlOutputBufferPtr xmlbuffer;
	gsize length;
	gchar *text;

	g_return_val_if_fail (E_IS_XML_DOCUMENT (xml), NULL);

	xmlbuffer = xmlAllocOutputBuffer (NULL);
	xmlNodeDumpOutput (xmlbuffer, xml->priv->doc, xml->priv->root, 0, 1, NULL);
	xmlOutputBufferFlush (xmlbuffer);

#ifdef LIBXML2_NEW_BUFFER
	length = xmlOutputBufferGetSize (xmlbuffer);
	text = g_strndup ((const gchar *) xmlOutputBufferGetContent (xmlbuffer), length);
#else
	length = xmlbuffer->buffer->use;
	text = g_strndup ((const gchar *) xmlbuffer->buffer->content, length);
#endif

	xmlOutputBufferClose (xmlbuffer);

	if (out_length)
		*out_length = length;

	return text;
}

/**
 * e_xml_document_add_namespaces:
 * @xml: an #EXmlDocument
 * @ns_prefix: namespace prefix to use for this namespace
 * @ns_href: namespace href
 * @...: %NULL-terminated pairs of (ns_prefix, ns_href)
 *
 * Adds one or more namespaces to @xml, which can be referenced
 * later by @ns_href. The caller should take care that neither
 * used @ns_prefix, nor @ns_href, is already used by @xml.
 *
 * Since: 3.26
 **/
void
e_xml_document_add_namespaces (EXmlDocument *xml,
			       const gchar *ns_prefix,
			       const gchar *ns_href,
			       ...)
{
	xmlNsPtr ns;
	va_list va;

	g_return_if_fail (E_IS_XML_DOCUMENT (xml));
	g_return_if_fail (ns_prefix != NULL);
	g_return_if_fail (xml->priv->root != NULL);

	if (!ns_href)
		ns_href = "";

	if (!g_hash_table_contains (xml->priv->namespaces_by_href, ns_href)) {
		ns = xmlNewNs (xml->priv->root, (const xmlChar *) ns_href, (const xmlChar *) ns_prefix);
		g_return_if_fail (ns != NULL);

		g_hash_table_insert (xml->priv->namespaces_by_href, g_strdup (ns_href), ns);
	}

	va_start (va, ns_href);

	while (ns_prefix = va_arg (va, const gchar *), ns_prefix) {
		ns_href = va_arg (va, const gchar *);
		if (!ns_href)
			ns_href = "";

		if (!g_hash_table_contains (xml->priv->namespaces_by_href, ns_href)) {
			ns = xmlNewNs (xml->priv->root, (const xmlChar *) ns_href, (const xmlChar *) ns_prefix);
			g_warn_if_fail (ns != NULL);

			if (ns)
				g_hash_table_insert (xml->priv->namespaces_by_href, g_strdup (ns_href), ns);
		}
	}

	va_end (va);
}

static gchar *
e_xml_document_number_to_alpha (gint number)
{
	GString *alpha;

	g_return_val_if_fail (number >= 0, NULL);

	alpha = g_string_new ("");
	g_string_append_c (alpha, 'A' + (number % 26));

	while (number = number / 26, number > 0) {
		g_string_prepend_c (alpha, 'A' + (number % 26));
	}

	return g_string_free (alpha, FALSE);
}

static gchar *
e_xml_document_gen_ns_prefix (EXmlDocument *xml,
			      const gchar *ns_href)
{
	GHashTable *prefixes;
	GHashTableIter iter;
	gpointer value;
	gchar *new_prefix = NULL;
	const gchar *ptr;
	gint counter = 0, n_prefixes;

	g_return_val_if_fail (E_IS_XML_DOCUMENT (xml), NULL);
	g_return_val_if_fail (ns_href && *ns_href, NULL);

	prefixes = g_hash_table_new (g_str_hash, g_str_equal);

	g_hash_table_iter_init (&iter, xml->priv->namespaces_by_href);
	while (g_hash_table_iter_next (&iter, NULL, &value)) {
		xmlNsPtr ns = value;

		if (ns && ns->prefix)
			g_hash_table_insert (prefixes, (gpointer) ns->prefix, NULL);
	}

	ptr = strrchr (ns_href, ':');

	/* the ns_href ends with ':' */
	if (ptr && !ptr[1] && g_ascii_isalpha (ns_href[0])) {
		new_prefix = g_strndup (ns_href, 1);
	} else if (ptr && strchr (ns_href, ':') < ptr && g_ascii_isalpha (ptr[1])) {
		new_prefix = g_strndup (ptr + 1, 1);
	} else if (g_str_has_prefix (ns_href, "http://") &&
		   g_ascii_isalpha (ns_href[7])) {
		new_prefix = g_strndup (ns_href + 7, 1);
	}

	n_prefixes = g_hash_table_size (prefixes);

	while (!new_prefix || g_hash_table_contains (prefixes, new_prefix)) {
		g_free (new_prefix);

		if (counter > n_prefixes + 2) {
			new_prefix = NULL;
			break;
		}

		new_prefix = e_xml_document_number_to_alpha (counter);
		counter++;
	}

	g_hash_table_destroy (prefixes);

	return new_prefix;
}

static xmlNsPtr
e_xml_document_ensure_namespace (EXmlDocument *xml,
				 const gchar *ns_href)
{
	xmlNsPtr ns;
	gchar *ns_prefix;

	g_return_val_if_fail (E_IS_XML_DOCUMENT (xml), NULL);

	if (!ns_href)
		return NULL;

	ns = g_hash_table_lookup (xml->priv->namespaces_by_href, ns_href);
	if (ns || !*ns_href)
		return ns;

	ns_prefix = e_xml_document_gen_ns_prefix (xml, ns_href);

	e_xml_document_add_namespaces (xml, ns_prefix, ns_href, NULL);

	g_free (ns_prefix);

	return g_hash_table_lookup (xml->priv->namespaces_by_href, ns_href);
}

/**
 * e_xml_document_start_element:
 * @xml: an #EXmlDocument
 * @ns_href: (nullable): optional namespace href for the new element, or %NULL
 * @name: name of the new element
 *
 * Starts a new non-text element as a child of the current element.
 * Each such call should be ended with corresponding e_xml_document_end_element().
 * Use %NULL @ns_href, to use the default namespace, otherwise either previously
 * added namespace with the same href from e_xml_document_add_namespaces() is picked,
 * or a new namespace with generated prefix is added.
 *
 * To start a text node use e_xml_document_start_text_element().
 *
 * Since: 3.26
 **/
void
e_xml_document_start_element (EXmlDocument *xml,
			      const gchar *ns_href,
			      const gchar *name)
{
	g_return_if_fail (E_IS_XML_DOCUMENT (xml));
	g_return_if_fail (name != NULL);
	g_return_if_fail (*name);
	g_return_if_fail (xml->priv->current_element != NULL);

	xml->priv->current_element = xmlNewChild (xml->priv->current_element,
		e_xml_document_ensure_namespace (xml, ns_href), (const xmlChar *) name, NULL);
}

/**
 * e_xml_document_start_text_element:
 * @xml: an #EXmlDocument
 * @ns_href: (nullable): optional namespace href for the new element, or %NULL
 * @name: name of the new element
 *
 * Starts a new text element as a child of the current element.
 * Each such call should be ended with corresponding e_xml_document_end_element().
 * Use %NULL @ns_href, to use the default namespace, otherwise either previously
 * added namespace with the same href from e_xml_document_add_namespaces() is picked,
 * or a new namespace with generated prefix is added.
 *
 * To start a non-text node use e_xml_document_start_element().
 *
 * Since: 3.26
 **/
void
e_xml_document_start_text_element (EXmlDocument *xml,
				   const gchar *ns_href,
				   const gchar *name)
{
	g_return_if_fail (E_IS_XML_DOCUMENT (xml));
	g_return_if_fail (name != NULL);
	g_return_if_fail (*name);
	g_return_if_fail (xml->priv->current_element != NULL);

	xml->priv->current_element = xmlNewTextChild (xml->priv->current_element,
		e_xml_document_ensure_namespace (xml, ns_href), (const xmlChar *) name, NULL);
}

/**
 * e_xml_document_end_element:
 * @xml: an #EXmlDocument
 *
 * This is a pair function for e_xml_document_start_element() and
 * e_xml_document_start_text_element(), which changes current
 * element to the parent of that element.
 *
 * Since: 3.26
 **/
void
e_xml_document_end_element (EXmlDocument *xml)
{
	g_return_if_fail (E_IS_XML_DOCUMENT (xml));
	g_return_if_fail (xml->priv->current_element != NULL);
	g_return_if_fail (xml->priv->current_element != xml->priv->root);

	xml->priv->current_element = xml->priv->current_element->parent;
}

/**
 * e_xml_document_add_empty_element:
 * @xml: an #EXmlDocument
 * @ns_href: (nullable): optional namespace href for the new element, or %NULL
 * @name: name of the new element
 *
 * Adds an empty element, which is an element with no attribute and no value.
 *
 * It's the same as calling e_xml_document_start_element() immediately
 * followed by e_xml_document_end_element().
 *
 * Since: 3.26
 **/
void
e_xml_document_add_empty_element (EXmlDocument *xml,
				  const gchar *ns_href,
				  const gchar *name)
{
	g_return_if_fail (E_IS_XML_DOCUMENT (xml));
	g_return_if_fail (name != NULL);
	g_return_if_fail (*name);
	g_return_if_fail (xml->priv->current_element != NULL);

	e_xml_document_start_element (xml, ns_href, name);
	e_xml_document_end_element (xml);
}

/**
 * e_xml_document_add_attribute:
 * @xml: an #EXmlDocument
 * @ns_href: (nullable): optional namespace href for the new attribute, or %NULL
 * @name: name of the attribute
 * @value: value of the attribute
 *
 * Adds a new attribute to the current element.
 * Use %NULL @ns_href, to use the default namespace, otherwise either previously
 * added namespace with the same href from e_xml_document_add_namespaces() is picked,
 * or a new namespace with generated prefix is added.
 *
 * Since: 3.26
 **/
void
e_xml_document_add_attribute (EXmlDocument *xml,
			      const gchar *ns_href,
			      const gchar *name,
			      const gchar *value)
{
	g_return_if_fail (E_IS_XML_DOCUMENT (xml));
	g_return_if_fail (xml->priv->current_element != NULL);
	g_return_if_fail (name != NULL);
	g_return_if_fail (value != NULL);

	xmlNewNsProp (
		xml->priv->current_element,
		e_xml_document_ensure_namespace (xml, ns_href),
		(const xmlChar *) name,
		(const xmlChar *) value);
}

/**
 * e_xml_document_add_attribute_int:
 * @xml: an #EXmlDocument
 * @ns_href: (nullable): optional namespace href for the new attribute, or %NULL
 * @name: name of the attribute
 * @value: integer value of the attribute
 *
 * Adds a new attribute with an integer value to the current element.
 * Use %NULL @ns_href, to use the default namespace, otherwise either previously
 * added namespace with the same href from e_xml_document_add_namespaces() is picked,
 * or a new namespace with generated prefix is added.
 *
 * Since: 3.26
 **/
void
e_xml_document_add_attribute_int (EXmlDocument *xml,
				  const gchar *ns_href,
				  const gchar *name,
				  gint64 value)
{
	gchar *strvalue;

	g_return_if_fail (E_IS_XML_DOCUMENT (xml));
	g_return_if_fail (xml->priv->current_element != NULL);
	g_return_if_fail (name != NULL);

	strvalue = g_strdup_printf ("%" G_GINT64_FORMAT, value);
	e_xml_document_add_attribute (xml, ns_href, name, strvalue);
	g_free (strvalue);
}

/**
 * e_xml_document_add_attribute_double:
 * @xml: an #EXmlDocument
 * @ns_href: (nullable): optional namespace href for the new attribute, or %NULL
 * @name: name of the attribute
 * @value: double value of the attribute
 *
 * Adds a new attribute with a double value to the current element.
 * Use %NULL @ns_href, to use the default namespace, otherwise either previously
 * added namespace with the same href from e_xml_document_add_namespaces() is picked,
 * or a new namespace with generated prefix is added.
 *
 * Since: 3.26
 **/
void
e_xml_document_add_attribute_double (EXmlDocument *xml,
				     const gchar *ns_href,
				     const gchar *name,
				     gdouble value)
{
	gchar *strvalue;

	g_return_if_fail (E_IS_XML_DOCUMENT (xml));
	g_return_if_fail (xml->priv->current_element != NULL);
	g_return_if_fail (name != NULL);

	strvalue = g_strdup_printf ("%f", value);
	e_xml_document_add_attribute (xml, ns_href, name, strvalue);
	g_free (strvalue);
}

/**
 * e_xml_document_add_attribute_time:
 * @xml: an #EXmlDocument
 * @ns_href: (nullable): optional namespace href for the new attribute, or %NULL
 * @name: name of the attribute
 * @value: time_t value of the attribute
 *
 * Adds a new attribute with a time_t value in ISO 8601 format to the current element.
 * The format is "YYYY-MM-DDTHH:MM:SSZ".
 * Use %NULL @ns_href, to use the default namespace, otherwise either previously
 * added namespace with the same href from e_xml_document_add_namespaces() is picked,
 * or a new namespace with generated prefix is added.
 *
 * Since: 3.26
 **/
void
e_xml_document_add_attribute_time (EXmlDocument *xml,
				   const gchar *ns_href,
				   const gchar *name,
				   time_t value)
{
	GTimeVal tv;
	gchar *strvalue;

	g_return_if_fail (E_IS_XML_DOCUMENT (xml));
	g_return_if_fail (xml->priv->current_element != NULL);
	g_return_if_fail (name != NULL);

	tv.tv_usec = 0;
	tv.tv_sec = value;

	strvalue = g_time_val_to_iso8601 (&tv);
	e_xml_document_add_attribute (xml, ns_href, name, strvalue);
	g_free (strvalue);
}

/**
 * e_xml_document_add_attribute_time_ical:
 * @xml: an #EXmlDocument
 * @ns_href: (nullable): optional namespace href for the new attribute, or %NULL
 * @name: name of the attribute
 * @value: time_t value of the attribute
 *
 * Adds a new attribute with a time_t value in iCalendar format to the current element.
 * The format is "YYYYMMDDTHHMMSSZ".
 * Use %NULL @ns_href, to use the default namespace, otherwise either previously
 * added namespace with the same href from e_xml_document_add_namespaces() is picked,
 * or a new namespace with generated prefix is added.
 *
 * Since: 3.32
 **/
void
e_xml_document_add_attribute_time_ical (EXmlDocument *xml,
					const gchar *ns_href,
					const gchar *name,
					time_t value)
{
	GDateTime *dt;
	gchar *strvalue;

	g_return_if_fail (E_IS_XML_DOCUMENT (xml));
	g_return_if_fail (xml->priv->current_element != NULL);
	g_return_if_fail (name != NULL);

	dt = g_date_time_new_from_unix_utc ((gint64) value);
	g_return_if_fail (dt != NULL);

	strvalue = g_date_time_format (dt, "%Y%m%dT%H%M%SZ");

	g_date_time_unref (dt);

	e_xml_document_add_attribute (xml, ns_href, name, strvalue);
	g_free (strvalue);
}

/**
 * e_xml_document_write_int:
 * @xml: an #EXmlDocument
 * @value: value to write as the content
 *
 * Writes @value as content of the current element.
 *
 * Since: 3.26
 **/
void
e_xml_document_write_int (EXmlDocument *xml,
			  gint64 value)
{
	gchar *strvalue;

	g_return_if_fail (E_IS_XML_DOCUMENT (xml));
	g_return_if_fail (xml->priv->current_element != NULL);

	strvalue = g_strdup_printf ("%" G_GINT64_FORMAT, value);
	e_xml_document_write_string (xml, strvalue);
	g_free (strvalue);
}

/**
 * e_xml_document_write_double:
 * @xml: an #EXmlDocument
 * @value: value to write as the content
 *
 * Writes @value as content of the current element.
 *
 * Since: 3.26
 **/
void
e_xml_document_write_double (EXmlDocument *xml,
			     gdouble value)
{
	gchar *strvalue;

	g_return_if_fail (E_IS_XML_DOCUMENT (xml));
	g_return_if_fail (xml->priv->current_element != NULL);

	strvalue = g_strdup_printf ("%f", value);
	e_xml_document_write_string (xml, strvalue);
	g_free (strvalue);
}

/**
 * e_xml_document_write_base64:
 * @xml: an #EXmlDocument
 * @value: value to write as the content
 * @len: length of @value
 *
 * Writes @value of length @len, encoded to base64, as content of the current element.
 *
 * Since: 3.26
 **/
void
e_xml_document_write_base64 (EXmlDocument *xml,
			     const gchar *value,
			     gint len)
{
	gchar *strvalue;

	g_return_if_fail (E_IS_XML_DOCUMENT (xml));
	g_return_if_fail (xml->priv->current_element != NULL);
	g_return_if_fail (value != NULL);

	strvalue = g_base64_encode ((const guchar *) value, len);
	e_xml_document_write_string (xml, strvalue);
	g_free (strvalue);
}

/**
 * e_xml_document_write_time:
 * @xml: an #EXmlDocument
 * @value: value to write as the content
 *
 * Writes @value in ISO 8601 format as content of the current element.
 * The format is "YYYY-MM-DDTHH:MM:SSZ".
 *
 * Since: 3.26
 **/
void
e_xml_document_write_time (EXmlDocument *xml,
			   time_t value)
{
	GTimeVal tv;
	gchar *strvalue;

	g_return_if_fail (E_IS_XML_DOCUMENT (xml));
	g_return_if_fail (xml->priv->current_element != NULL);

	tv.tv_usec = 0;
	tv.tv_sec = value;

	strvalue = g_time_val_to_iso8601 (&tv);
	e_xml_document_write_string (xml, strvalue);
	g_free (strvalue);
}

/**
 * e_xml_document_write_string:
 * @xml: an #EXmlDocument
 * @value: value to write as the content
 *
 * Writes @value as content of the current element.
 *
 * Since: 3.26
 **/
void
e_xml_document_write_string (EXmlDocument *xml,
			     const gchar *value)
{
	g_return_if_fail (E_IS_XML_DOCUMENT (xml));
	g_return_if_fail (xml->priv->current_element != NULL);
	g_return_if_fail (value != NULL);

	xmlNodeAddContent (
		xml->priv->current_element,
		(const xmlChar *) value);
}

/**
 * e_xml_document_write_buffer:
 * @xml: an #EXmlDocument
 * @value: value to write as the content
 * @len: length of @value
 *
 * Writes @value of length @len as content of the current element.
 *
 * Since: 3.26
 **/
void
e_xml_document_write_buffer (EXmlDocument *xml,
			     const gchar *value,
			     gint len)
{
	g_return_if_fail (E_IS_XML_DOCUMENT (xml));
	g_return_if_fail (xml->priv->current_element != NULL);
	g_return_if_fail (value != NULL);

	xmlNodeAddContentLen (
		xml->priv->current_element,
		(const xmlChar *) value, len);
}