summaryrefslogtreecommitdiff
path: root/src/core/namednodemap.c
blob: 98fa5f98f57f74a7bbdf0e7d13eba9f8ae738643 (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
/*
 * 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/core/element.h>
#include <dom/core/node.h>
#include <dom/core/string.h>

#include "core/document.h"
#include "core/element.h"
#include "core/namednodemap.h"
#include "core/node.h"

#include "utils/utils.h"

/**
 * DOM named node map
 */
struct dom_namednodemap {
	struct dom_document *owner;	/**< Owning document */

	struct dom_node_internal *head;		/**< Start of item list */

	dom_node_type type;		/**< Type of items in map */

	uint32_t refcnt;		/**< Reference count */
};

/**
 * Create a namednodemap
 *
 * \param doc   The owning document
 * \param head  Start of list containing items in map
 * \param type  The type of items in the map
 * \param map   Pointer to location to receive created map
 * \return DOM_NO_ERR on success, DOM_NO_MEM_ERR on memory exhaustion
 *
 * ::head must be a node owned by ::doc and must be either an Element or
 * DocumentType node.
 *
 * If ::head is of type Element, ::type must be DOM_ATTRIBUTE_NODE
 * If ::head is of type DocumentType, ::type may be either
 * DOM_ENTITY_NODE or DOM_NOTATION_NODE.
 *
 * The returned map will already be referenced, so the client need not
 * explicitly reference it. The client must unref the map once it is
 * finished with it.
 */
dom_exception dom_namednodemap_create(struct dom_document *doc,
		struct dom_node_internal *head, dom_node_type type,
		struct dom_namednodemap **map)
{
	struct dom_namednodemap *m;

	m = dom_document_alloc(doc, NULL, sizeof(struct dom_namednodemap));
	if (m == NULL)
		return DOM_NO_MEM_ERR;

	dom_node_ref((struct dom_node *) doc);
	m->owner = doc;

	dom_node_ref(head);
	m->head = head;

	m->type = type;

	m->refcnt = 1;

	*map = m;

	return DOM_NO_ERR;
}

/**
 * Claim a reference on a DOM named node map
 *
 * \param map  The map to claim a reference on
 */
void dom_namednodemap_ref(struct dom_namednodemap *map)
{
	map->refcnt++;
}

/**
 * Release a reference on a DOM named node map
 *
 * \param map  The map to release the reference from
 *
 * If the reference count reaches zero, any memory claimed by the
 * map will be released
 */
void dom_namednodemap_unref(struct dom_namednodemap *map)
{
	if (--map->refcnt == 0) {
		struct dom_node_internal *owner = 
				(struct dom_node_internal *) map->owner;

		dom_node_unref(map->head);

		/* Remove map from document */
		dom_document_remove_namednodemap(map->owner, map);

		/* Destroy the map object */
		dom_document_alloc(map->owner, map, 0);

		/* And release our reference on the owning document
		 * This must be last as, otherwise, it's possible that
		 * the document is destroyed before we are */
		dom_node_unref(owner);
	}
}

/**
 * Retrieve the length of a named node map
 *
 * \param map     Map to retrieve length of
 * \param length  Pointer to location to receive length
 * \return DOM_NO_ERR.
 */
dom_exception dom_namednodemap_get_length(struct dom_namednodemap *map,
		unsigned long *length)
{
	struct dom_node_internal *cur;
	unsigned long len = 0;

	switch (map->type) {
	case DOM_ATTRIBUTE_NODE:
		cur = dom_element_get_first_attribute(
				(struct dom_element *) map->head);
		break;
	case DOM_NOTATION_NODE:
	case DOM_ENTITY_NODE:
		/** \todo handle notation and entity nodes */
	default:
		return DOM_NOT_SUPPORTED_ERR;
		break;
	}

	for (; cur != NULL; cur = cur->next) {
		len++;
	}

	*length = len;

	return DOM_NO_ERR;
}

/**
 * Retrieve an item by name from a named node map
 *
 * \param map   The map to retrieve the item from
 * \param name  The name of the item to retrieve
 * \param node  Pointer to location to receive item
 * \return DOM_NO_ERR.
 *
 * The returned node will have had its reference count increased. The client
 * should unref the node once it has finished with it.
 */
dom_exception dom_namednodemap_get_named_item(struct dom_namednodemap *map,
		struct dom_string *name, struct dom_node **node)
{
	struct dom_node_internal *cur;

	switch (map->type) {
	case DOM_ATTRIBUTE_NODE:
		cur = dom_element_get_first_attribute(
				(struct dom_element *) map->head);
		break;
	case DOM_NOTATION_NODE:
	case DOM_ENTITY_NODE:
		/** \todo handle notation and entity nodes */
	default:
		return DOM_NOT_SUPPORTED_ERR;
		break;
	}

	for (; cur != NULL; cur = cur->next) {
		if (dom_string_cmp(cur->name, name) == 0) {
			break;
		}
	}

	if (cur != NULL) {
		dom_node_ref(cur);
	}
	*node = (struct dom_node *) cur;

	return DOM_NO_ERR;
}

/**
 * Add a node to a named node map, replacing any matching existing node
 *
 * \param map   The map to add to
 * \param arg   The node to add
 * \param node  Pointer to location to receive replaced node
 * \return DOM_NO_ERR                      on success,
 *         DOM_WRONG_DOCUMENT_ERR          if ::arg was created from a
 *                                         different document than ::map,
 *         DOM_NO_MODIFICATION_ALLOWED_ERR if ::map is readonly,
 *         DOM_INUSE_ATTRIBUTE_ERR         if ::arg is an Attr that is
 *                                         already an attribute on another
 *                                         Element,
 *         DOM_HIERARCHY_REQUEST_ERR       if the type of ::arg is not
 *                                         permitted as a member of ::map.
 *
 * ::arg's nodeName attribute will be used to store it in ::map. It will
 * be accessible using the nodeName attribute as the key for lookup.
 *
 * Replacing a node by itself has no effect.
 *
 * The returned node will have had its reference count increased. The client
 * should unref the node once it has finished with it.
 */
dom_exception dom_namednodemap_set_named_item(struct dom_namednodemap *map,
		struct dom_node *arg, struct dom_node **node)
{
	dom_exception err;
	struct dom_node_internal *n = (struct dom_node_internal *) arg;

	/* Ensure arg and map belong to the same document */
	if (n->owner != map->owner)
		return DOM_WRONG_DOCUMENT_ERR;

	/* Ensure map is writable */
	if (_dom_node_readonly(map->head))
		return DOM_NO_MODIFICATION_ALLOWED_ERR;

	/* Ensure arg isn't attached to another element */
	if (n->type == DOM_ATTRIBUTE_NODE && n->parent != NULL && 
			n->parent != map->head)
		return DOM_INUSE_ATTRIBUTE_ERR;

	/* Ensure arg is permitted in the map */
	if (n->type != map->type)
		return DOM_HIERARCHY_REQUEST_ERR;

	/* Now delegate to the container-specific function. 
	 * NamedNodeMaps are live, so this is fine. */
	switch (map->type) {
	case DOM_ATTRIBUTE_NODE:
		err = dom_element_set_attribute_node(
				(struct dom_element *) map->head, 
				(struct dom_attr *) arg, 
				(struct dom_attr **) node);
		break;
	case DOM_NOTATION_NODE:
	case DOM_ENTITY_NODE:
		/** \todo handle notation and entity nodes */
	default:
		err = DOM_NOT_SUPPORTED_ERR;
		break;
	}

	/* Reference counting is handled by the container-specific call */

	return err;
}

/**
 * Remove an item by name from a named node map
 *
 * \param map   The map to remove from
 * \param name  The name of the item to remove
 * \param node  Pointer to location to receive removed item
 * \return DOM_NO_ERR                      on success,
 *         DOM_NOT_FOUND_ERR               if there is no node named ::name
 *                                         in ::map,
 *         DOM_NO_MODIFICATION_ALLOWED_ERR if ::map is readonly.
 *
 * The returned node will have had its reference count increased. The client
 * should unref the node once it has finished with it.
 */
dom_exception dom_namednodemap_remove_named_item(
		struct dom_namednodemap *map, struct dom_string *name,
		struct dom_node **node)
{
	dom_exception err;

	/* Ensure map is writable */
	if (_dom_node_readonly(map->head))
		return DOM_NO_MODIFICATION_ALLOWED_ERR;

	/* Now delegate to the container-specific function. 
	 * NamedNodeMaps are live, so this is fine. */
	switch (map->type) {
	case DOM_ATTRIBUTE_NODE:
	{
		struct dom_attr *attr;

		err = dom_element_get_attribute_node(
				(struct dom_element *) map->head,
				name, &attr);
		if (err == DOM_NO_ERR) {
			err = dom_element_remove_attribute_node(
				(struct dom_element *) map->head, 
				attr, (struct dom_attr **) node);
			if (err == DOM_NO_ERR) {
				/* No longer want attr */
				dom_node_unref((struct dom_node *) attr);
			}
		}
	}
		break;
	case DOM_NOTATION_NODE:
	case DOM_ENTITY_NODE:
		/** \todo handle notation and entity nodes */
	default:
		err = DOM_NOT_SUPPORTED_ERR;
		break;
	}

	/* Reference counting is handled by the container-specific call */

	return err;
}

/**
 * Retrieve an item from a named node map
 *
 * \param map    The map to retrieve the item from
 * \param index  The map index to retrieve
 * \param node   Pointer to location to receive item
 * \return DOM_NO_ERR.
 *
 * ::index is a zero-based index into ::map.
 * ::index lies in the range [0, length-1]
 *
 * The returned node will have had its reference count increased. The client
 * should unref the node once it has finished with it.
 */
dom_exception dom_namednodemap_item(struct dom_namednodemap *map,
		unsigned long index, struct dom_node **node)
{
	struct dom_node_internal *cur;
	unsigned long count = 0;

	switch (map->type) {
	case DOM_ATTRIBUTE_NODE:
		cur = dom_element_get_first_attribute(
				(struct dom_element *) map->head);
		break;
	case DOM_NOTATION_NODE:
	case DOM_ENTITY_NODE:
		/** \todo handle notation and entity nodes */
	default:
		return DOM_NOT_SUPPORTED_ERR;
		break;
	}

	for (; cur != NULL; cur = cur->next) {
		count++;

		if ((index + 1) == count) {
			break;
		}
	}

	if (cur != NULL) {
		dom_node_ref(cur);
	}
	*node = (struct dom_node *) cur;

	return DOM_NO_ERR;
}

/**
 * Retrieve an item by namespace/localname from a named node map
 *
 * \param map        The map to retrieve the item from
 * \param namespace  The namespace URI of the item to retrieve
 * \param localname  The local name of the node to retrieve
 * \param node       Pointer to location to receive item
 * \return DOM_NO_ERR            on success,
 *         DOM_NOT_SUPPORTED_ERR if the implementation does not support the
 *                               feature "XML" and the language exposed
 *                               through the Document does not support
 *                               Namespaces.
 *
 * The returned node will have had its reference count increased. The client
 * should unref the node once it has finished with it.
 */
dom_exception dom_namednodemap_get_named_item_ns(
		struct dom_namednodemap *map, struct dom_string *namespace,
		struct dom_string *localname, struct dom_node **node)
{
	struct dom_node_internal *cur;

	/** \todo ensure XML feature is supported */

	switch (map->type) {
	case DOM_ATTRIBUTE_NODE:
		cur = dom_element_get_first_attribute(
				(struct dom_element *) map->head);
		break;
	case DOM_NOTATION_NODE:
	case DOM_ENTITY_NODE:
		/** \todo handle notation and entity nodes */
	default:
		return DOM_NOT_SUPPORTED_ERR;
		break;
	}

	for (; cur != NULL; cur = cur->next) {
		if (((namespace == NULL && cur->namespace == NULL) || 
			(namespace != NULL && 
			dom_string_cmp(cur->namespace, namespace) == 0)) &&
				dom_string_cmp(cur->name, localname) == 0) {
			break;
		}
	}

	if (cur != NULL) {
		dom_node_ref(cur);
	}
	*node = (struct dom_node *) cur;

	return DOM_NO_ERR;
}

/**
 * Add a node to a named node map, replacing any matching existing node
 *
 * \param map   The map to add to
 * \param arg   The node to add
 * \param node  Pointer to location to receive replaced node
 * \return DOM_NO_ERR                      on success,
 *         DOM_WRONG_DOCUMENT_ERR          if ::arg was created from a
 *                                         different document than ::map,
 *         DOM_NO_MODIFICATION_ALLOWED_ERR if ::map is readonly,
 *         DOM_INUSE_ATTRIBUTE_ERR         if ::arg is an Attr that is
 *                                         already an attribute on another
 *                                         Element,
 *         DOM_HIERARCHY_REQUEST_ERR       if the type of ::arg is not
 *                                         permitted as a member of ::map.
 *         DOM_NOT_SUPPORTED_ERR if the implementation does not support the
 *                               feature "XML" and the language exposed
 *                               through the Document does not support
 *                               Namespaces.
 *
 * ::arg's namespaceURI and localName attributes will be used to store it in
 * ::map. It will be accessible using the namespaceURI and localName
 * attributes as the keys for lookup.
 *
 * Replacing a node by itself has no effect.
 *
 * The returned node will have had its reference count increased. The client
 * should unref the node once it has finished with it.
 */
dom_exception dom_namednodemap_set_named_item_ns(
		struct dom_namednodemap *map, struct dom_node *arg,
		struct dom_node **node)
{
	dom_exception err;
	struct dom_node_internal *n = (struct dom_node_internal *) arg;

	/** \todo ensure XML feature is supported */

	/* Ensure arg and map belong to the same document */
	if (n->owner != map->owner)
		return DOM_WRONG_DOCUMENT_ERR;

	/* Ensure map is writable */
	if (_dom_node_readonly(map->head))
		return DOM_NO_MODIFICATION_ALLOWED_ERR;

	/* Ensure arg isn't attached to another element */
	if (n->type == DOM_ATTRIBUTE_NODE && n->parent != NULL && 
			n->parent != map->head)
		return DOM_INUSE_ATTRIBUTE_ERR;

	/* Ensure arg is permitted in the map */
	if (n->type != map->type)
		return DOM_HIERARCHY_REQUEST_ERR;

	/* Now delegate to the container-specific function. 
	 * NamedNodeMaps are live, so this is fine. */
	switch (map->type) {
	case DOM_ATTRIBUTE_NODE:
		err = dom_element_set_attribute_node_ns(
				(struct dom_element *) map->head, 
				(struct dom_attr *) arg, 
				(struct dom_attr **) node);
		break;
	case DOM_NOTATION_NODE:
	case DOM_ENTITY_NODE:
		/** \todo handle notation and entity nodes */
	default:
		err = DOM_NOT_SUPPORTED_ERR;
		break;
	}

	/* Reference counting is handled by the container-specific call */

	return err;
}

/**
 * Remove an item by namespace/localname from a named node map
 *
 * \param map        The map to remove from
 * \param namespace  The namespace URI of the item to remove
 * \param localname  The local name of the item to remove
 * \param node       Pointer to location to receive removed item
 * \return DOM_NO_ERR                      on success,
 *         DOM_NOT_FOUND_ERR               if there is no node named ::name
 *                                         in ::map,
 *         DOM_NO_MODIFICATION_ALLOWED_ERR if ::map is readonly.
 *         DOM_NOT_SUPPORTED_ERR if the implementation does not support the
 *                               feature "XML" and the language exposed
 *                               through the Document does not support
 *                               Namespaces.
 *
 * The returned node will have had its reference count increased. The client
 * should unref the node once it has finished with it.
 */
dom_exception dom_namednodemap_remove_named_item_ns(
		struct dom_namednodemap *map, struct dom_string *namespace,
		struct dom_string *localname, struct dom_node **node)
{
	dom_exception err;

	/** \todo ensure XML feature is supported */

	/* Ensure map is writable */
	if (_dom_node_readonly(map->head))
		return DOM_NO_MODIFICATION_ALLOWED_ERR;

	/* Now delegate to the container-specific function. 
	 * NamedNodeMaps are live, so this is fine. */
	switch (map->type) {
	case DOM_ATTRIBUTE_NODE:
	{
		struct dom_attr *attr;

		err = dom_element_get_attribute_node_ns(
				(struct dom_element *) map->head,
				namespace, localname, &attr);
		if (err == DOM_NO_ERR) {
			err = dom_element_remove_attribute_node(
				(struct dom_element *) map->head, 
				attr, (struct dom_attr **) node);
			if (err == DOM_NO_ERR) {
				/* No longer want attr */
				dom_node_unref((struct dom_node *) attr);
			}
		}
	}
		break;
case DOM_NOTATION_NODE:
	case DOM_ENTITY_NODE:
		/** \todo handle notation and entity nodes */
	default:
		err = DOM_NOT_SUPPORTED_ERR;
		break;
	}

	/* Reference counting is handled by the container-specific call */

	return err;
}

/**
 * Match a namednodemap instance against a set of creation parameters
 *
 * \param map   The map to match
 * \param head  Start of list containing items in map
 * \param type  The type of items in the map
 * \return true if list matches, false otherwise
 */
bool dom_namednodemap_match(struct dom_namednodemap *map,
		struct dom_node_internal *head, dom_node_type type)
{
	if (map->head == head && map->type == type)
		return true;

	return false;
}