summaryrefslogtreecommitdiff
path: root/bindings/xml/xmlbinding.c
blob: fbdc4c904bf3c2556b29811672f45bed20807bce (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
/*
 * This file is part of libdom.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2007 John-Mark Bell <jmb@netsurf-browser.org>
 */

#include <dom/bootstrap/implpriv.h>
#include <dom/bootstrap/implregistry.h>
#include <dom/dom.h>

#include "functypes.h"
#include "xmlbinding.h"
#include "utils.h"

static dom_exception xml_dom_get_dom_implementation(
		struct dom_string *features,
		struct dom_implementation **impl, dom_alloc alloc, void *pw);
static dom_exception xml_dom_get_dom_implementation_list(
		struct dom_string *features,
		struct dom_implementation_list **list,
		dom_alloc alloc, void *pw);

static dom_exception xml_dom_implementation_has_feature(
		struct dom_implementation *impl,
		struct dom_string *feature,
		struct dom_string *version,
		bool *result);
static dom_exception xml_dom_implementation_create_document_type(
		struct dom_implementation *impl,
		struct dom_string *qname,
		struct dom_string *public_id,
		struct dom_string *system_id,
		struct dom_document_type **doctype,
		dom_alloc alloc, void *pw);
static dom_exception xml_dom_implementation_create_document(
		struct dom_implementation *impl,
		struct dom_string *namespace,
		struct dom_string *qname,
		struct dom_document_type *doctype,
		struct dom_document **doc,
		dom_alloc alloc, void *pw);
static dom_exception xml_dom_implementation_get_feature(
		struct dom_implementation *impl,
		struct dom_string *feature,
		struct dom_string *version,
		void **object,
		dom_alloc alloc, void *pw);
static void xml_dom_implementation_destroy(struct dom_implementation *impl);


/**
 * DOM implementation source for XML documents
 */
static struct dom_implementation_source xml_dom_impl_src = {
	xml_dom_get_dom_implementation,
	xml_dom_get_dom_implementation_list
};

/**
 * DOM implementation for XML documents
 */
static struct dom_implementation xml_dom_impl = {
	xml_dom_implementation_has_feature,
	xml_dom_implementation_create_document_type,
	xml_dom_implementation_create_document,
	xml_dom_implementation_get_feature,
	xml_dom_implementation_destroy,
	0
};

/**
 * Get a DOM implementation that supports the requested features
 *
 * \param features  String containing required features
 * \param impl      Pointer to location to receive implementation
 * \param alloc     Function to (de)allocate memory
 * \param pw        Pointer to client-specific private data
 * \return DOM_NO_ERR on success, DOM_NO_MEM_ERR on memory exhaustion
 *
 * Any memory allocated by this call should be allocated using
 * the provided memory (de)allocation function. The implementation's
 * destroy() method will be called once it is no longer used.
 *
 * The implementation will be referenced, so the client need not
 * do this explicitly. The client must unref the implementation
 * once it has finished with it.
 */
dom_exception xml_dom_get_dom_implementation(
		struct dom_string *features,
		struct dom_implementation **impl, dom_alloc alloc, void *pw)
{
	UNUSED(features);
	UNUSED(alloc);
	UNUSED(pw);

	xml_dom_impl.refcnt++;

	*impl = &xml_dom_impl;

	return DOM_NO_ERR;
}

/**
 * Get a list of DOM implementations that support the requested
 * features
 *
 * \param features  String containing required features
 * \param list      Pointer to location to receive list
 * \param alloc     Function to (de)allocate memory
 * \param pw        Pointer to client-specific private data
 * \return DOM_NO_ERR on success, DOM_NO_MEM_ERR on memory exhaustion
 *
 * Any memory allocated by this call should be allocated using
 * the provided memory (de)allocation function. The ::alloc/::pw
 * pair must be stored on the list object, such that the list
 * and its contents may be freed once they are no longer needed.
 *
 * List nodes reference the implementation objects they point to.
 *
 * The list will be referenced, so the client need not do this
 * explicitly. The client must unref the list once it has finished
 * with it.
 */
dom_exception xml_dom_get_dom_implementation_list(
		struct dom_string *features,
		struct dom_implementation_list **list,
		dom_alloc alloc, void *pw)
{
	struct dom_implementation_list *l;
	struct dom_implementation_list_item *i;

	UNUSED(features);

	l = alloc(NULL, sizeof(struct dom_implementation_list), pw);
	if (l == NULL)
		return DOM_NO_MEM_ERR;

	i = alloc(NULL, sizeof(struct dom_implementation_list_item), pw);
	if (i == NULL) {
		alloc(l, 0, pw);
		return DOM_NO_MEM_ERR;
	}

	i->impl = &xml_dom_impl;
	i->next = NULL;
	i->prev = NULL;

	l->head = i;
	l->alloc = alloc;
	l->pw = pw;

	l->refcnt = 1;

	*list = l;

	return DOM_NO_ERR;
}

/**
 * Test whether a DOM implementation implements a specific feature
 * and version
 *
 * \param impl     The DOM implementation to query
 * \param feature  The feature to test for
 * \param version  The version number of the feature to test for
 * \param result   Pointer to location to receive result
 * \return DOM_NO_ERR.
 */
dom_exception xml_dom_implementation_has_feature(
		struct dom_implementation *impl,
		struct dom_string *feature,
		struct dom_string *version,
		bool *result)
{
	UNUSED(impl);
	UNUSED(feature);
	UNUSED(version);
	UNUSED(result);

	return DOM_NOT_SUPPORTED_ERR;
}

/**
 * Create a document type node
 *
 * \param impl       The implementation to create the node
 * \param qname      The qualified name of the document type
 * \param public_id  The external subset public identifier
 * \param system_id  The external subset system identifier
 * \param doctype    Pointer to location to receive result
 * \param alloc      Memory (de)allocation function
 * \param pw         Pointer to client-specific private data
 * \return DOM_NO_ERR on success,
 *         DOM_INVALID_CHARACTER_ERR if ::qname is invalid,
 *         DOM_NAMESPACE_ERR         if ::qname is malformed.
 *
 * Any memory allocated by this call should be allocated using
 * the provided memory (de)allocation function.
 *
 * The doctype will be referenced, so the client need not do this
 * explicitly. The client must unref the doctype once it has
 * finished with it.
 */
dom_exception xml_dom_implementation_create_document_type(
		struct dom_implementation *impl,
		struct dom_string *qname,
		struct dom_string *public_id,
		struct dom_string *system_id,
		struct dom_document_type **doctype,
		dom_alloc alloc, void *pw)
{
	struct dom_document_type *d;
	dom_exception err;

	/* We have no use for the impl -- we only have one */
	UNUSED(impl);

	/** \todo validate qname */

	/* Create the doctype */
	err = dom_document_type_create(qname, public_id, system_id,
			alloc, pw, &d);
	if (err != DOM_NO_ERR)
		return err;

	*doctype = d;

	return DOM_NO_ERR;
}

/**
 * Create a document node
 *
 * \param impl       The implementation to create the node
 * \param namespace  The namespace URI of the document element
 * \param qname      The qualified name of the document element
 * \param doctype    The type of document to create
 * \param doc        Pointer to location to receive result
 * \param alloc      Memory (de)allocation function
 * \param pw         Pointer to client-specific private data
 * \return DOM_NO_ERR on success,
 *         DOM_INVALID_CHARACTER_ERR if ::qname is invalid,
 *         DOM_NAMESPACE_ERR         if ::qname is malformed, or if
 *                                   ::qname has a prefix and
 *                                   ::namespace is NULL, or if
 *                                   ::qname is NULL and ::namespace
 *                                   is non-NULL, or if ::qname has
 *                                   a prefix "xml" and ::namespace
 *                                   is not
 *                                   "http://www.w3.org/XML/1998/namespace",
 *                                   or if ::impl does not support
 *                                   the "XML" feature and
 *                                   ::namespace is non-NULL,
 *         DOM_WRONG_DOCUMENT_ERR    if ::doctype is already being
 *                                   used by a document, or if it
 *                                   was not created by ::impl,
 *         DOM_NOT_SUPPORTED_ERR     if ::impl does not support the
 *                                   feature "XML" and the language
 *                                   exposed through Document does
 *                                   not support XML namespaces.
 *
 * Any memory allocated by this call should be allocated using
 * the provided memory (de)allocation function.
 *
 * The document will be referenced, so the client need not do this
 * explicitly. The client must unref the document once it has
 * finished with it.
 */
dom_exception xml_dom_implementation_create_document(
		struct dom_implementation *impl,
		struct dom_string *namespace,
		struct dom_string *qname,
		struct dom_document_type *doctype,
		struct dom_document **doc,
		dom_alloc alloc, void *pw)
{
	struct dom_document *d;
	dom_exception err;

	/* Create document object */
	err = dom_document_create(impl, alloc, pw, &d);
	if (err != DOM_NO_ERR)
		return err;

	/* Set its doctype, if necessary */
	if (doctype != NULL) {
		struct dom_node *ins_doctype = NULL;

		err = dom_node_append_child((struct dom_node *) d, 
				(struct dom_node *) doctype, &ins_doctype);
		if (err != DOM_NO_ERR) {
			dom_node_unref((struct dom_node *) d);
			return err;
		}

		/* Not interested in inserted doctype */
		if (ins_doctype != NULL)
			dom_node_unref(ins_doctype);
	}

	/* Create root element and attach it to document */
	if (qname != NULL) {
		struct dom_element *e;
		struct dom_node *inserted;

		err = dom_document_create_element_ns(d, namespace, qname, &e);
		if (err != DOM_NO_ERR) {
			dom_node_unref((struct dom_node *) d);
			return err;
		}

		err = dom_node_append_child((struct dom_node *) d,
				(struct dom_node *) e, &inserted);
		if (err != DOM_NO_ERR) {
			dom_node_unref((struct dom_node *) e);
			dom_node_unref((struct dom_node *) d);
			return err;
		}

		/* No longer interested in inserted node */
		dom_node_unref(inserted);

		/* Done with element */
		dom_node_unref((struct dom_node *) e);
	}

	*doc = d;

	return DOM_NO_ERR;
}

/**
 * Retrieve a specialized object which implements the specified
 * feature and version
 *
 * \param impl     The implementation to create the object
 * \param feature  The requested feature
 * \param version  The version number of the feature
 * \param object   Pointer to location to receive object
 * \param alloc    Memory (de)allocation function
 * \param pw       Pointer to client-specific private data
 * \return DOM_NO_ERR.
 *
 * Any memory allocated by this call should be allocated using
 * the provided memory (de)allocation function.
 */
dom_exception xml_dom_implementation_get_feature(
		struct dom_implementation *impl,
		struct dom_string *feature,
		struct dom_string *version,
		void **object,
		dom_alloc alloc, void *pw)
{
	UNUSED(impl);
	UNUSED(feature);
	UNUSED(version);
	UNUSED(object);
	UNUSED(alloc);
	UNUSED(pw);

	return DOM_NOT_SUPPORTED_ERR;
}

/**
 * Destroy a DOM implementation instance
 *
 * \param impl  The instance to destroy
 */
void xml_dom_implementation_destroy(struct dom_implementation *impl)
{
	UNUSED(impl);

	/* Nothing to do -- we're statically allocated */
}

/**
 * Initialise the XML DOM binding
 *
 * \param alloc  Pointer to memory (de)allocation function
 * \param pw     Pointer to client-specific private data
 * \return XML_OK on success, XML_NOMEM on memory exhaustion
 */
xml_error xml_dom_binding_initialise(xml_alloc alloc, void *pw)
{
	dom_exception err;

	err = dom_register_source(&xml_dom_impl_src, (dom_alloc) alloc, pw);
	if (err != DOM_NO_ERR)
		return XML_NOMEM;

	return XML_OK;
}

/**
 * Finalise the XML DOM binding
 * 
 * \return XML_OK on success.
 */
xml_error xml_dom_binding_finalise(void)
{
	return XML_OK;
}