summaryrefslogtreecommitdiff
path: root/src/core/attr.c
blob: b2800576e4ea39d07646d5ac060f425cc4886a0a (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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
/*
 * 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>
 * Copyright 2009 Bo Yang <struggleyb.nku@gmail.com>
 */

#include <stddef.h>
#include <string.h>
#include <assert.h>

#include <dom/core/attr.h>
#include <dom/core/document.h>
#include <dom/core/node.h>
#include <dom/core/string.h>

#include "core/attr.h"
#include "core/document.h"
#include "core/entity_ref.h"
#include "core/node.h"
#include "core/element.h"
#include "utils/utils.h"

struct dom_element;

/**
 * DOM attribute node
 */
struct dom_attr {
	struct dom_node_internal base;	/**< Base node */

	bool specified;		/**< Whether the attribute is specified
				 * or default */

	struct dom_type_info *schema_type_info;	/**< Type information */

	bool is_id;	/**< Whether this attribute is a ID attribute */

	dom_attr_type type;	/**< The type of this attribute */
	
	union {
		unsigned long lvalue;
		unsigned short svalue;
		bool bvalue;
	} value;	/**< The special type value of this attribute */

	bool read_only;	/**< Whether this attribute is readonly */
};

/* The vtable for dom_attr node */
static struct dom_attr_vtable attr_vtable = {
	{
		DOM_NODE_VTABLE_ATTR
	},
	DOM_ATTR_VTABLE
};

/* The protected vtable for dom_attr */
static struct dom_node_protect_vtable attr_protect_vtable = {
	DOM_ATTR_PROTECT_VTABLE
};


/* -------------------------------------------------------------------- */

/* Constructor and destructor */

/**
 * Create an attribute node
 *
 * \param doc        The owning document
 * \param name       The (local) name of the node to create
 * \param namespace  The namespace URI of the attribute, or NULL
 * \param prefix     The namespace prefix of the attribute, or NULL
 * \param specified  Whether this attribute is specified
 * \param result     Pointer to location to receive created attribute
 * \return DOM_NO_ERR     on success,
 *         DOM_NO_MEM_ERR on memory exhaustion.
 *
 * ::doc and ::name will have their reference counts increased. The 
 * caller should make sure that ::name is a valid NCName here.
 *
 * The returned attribute will already be referenced.
 */
dom_exception _dom_attr_create(struct dom_document *doc,
		struct lwc_string_s *name, struct lwc_string_s *namespace,
		struct lwc_string_s *prefix, bool specified, 
		struct dom_attr **result)
{
	struct dom_attr *a;
	dom_exception err;

	/* Allocate the attribute node */
	a = _dom_document_alloc(doc, NULL, sizeof(struct dom_attr));
	if (a == NULL)
		return DOM_NO_MEM_ERR;

	/* Initialise the vtable */
	a->base.base.vtable = &attr_vtable;
	a->base.vtable = &attr_protect_vtable;

	/* Initialise the class */
	err = _dom_attr_initialise(a, doc, name, namespace, prefix, specified, 
			result);
	if (err != DOM_NO_ERR) {
		_dom_document_alloc(doc, a, 0);
		return err;
	}
	return DOM_NO_ERR;
}

/**
 * Initialise a dom_attr
 *
 * \param a          The dom_attr
 * \param doc        The document
 * \param name       The name of this attribute node
 * \param namespace  The namespace of this attribute
 * \param prefix     The prefix
 * \param specified  Whether this node is a specified one
 * \param result     The returned node
 * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
 */
dom_exception _dom_attr_initialise(dom_attr *a, 
		struct dom_document *doc,  struct lwc_string_s *name,
		struct lwc_string_s *namespace, struct lwc_string_s *prefix,
		bool specified, struct dom_attr **result)
{
	dom_exception err;

	err = _dom_node_initialise(&a->base, doc, DOM_ATTRIBUTE_NODE,
			name, NULL, namespace, prefix);
	if (err != DOM_NO_ERR) {
		return err;
	}

	a->specified = specified;
	a->schema_type_info = NULL;
	a->is_id = false;
	/* The attribute type is unset when it is created */
	a->type = DOM_ATTR_UNSET;
	a->read_only = false;

	*result = a;

	return DOM_NO_ERR;
}

/**
 * The destructor of dom_attr
 *
 * \param doc   The owner document
 * \param attr  The attribute
 */
void _dom_attr_finalise(dom_document *doc, dom_attr *attr)
{
	/* Now, clean up this node and destroy it */

	if (attr->schema_type_info != NULL) {
		/** \todo destroy schema type info */
	}

	_dom_node_finalise(doc, &attr->base);
}

/**
 * Destroy an attribute node
 *
 * \param doc   The owning document
 * \param attr  The attribute to destroy
 *
 * The contents of ::attr will be destroyed and ::attr will be freed
 */
void _dom_attr_destroy(struct dom_document *doc, struct dom_attr *attr)
{
	_dom_attr_finalise(doc, attr);

	_dom_document_alloc(doc, attr, 0);
}

/*-----------------------------------------------------------------------*/
/* Following are our implementation specific APIs */

/**
 * Get the Attr Node type
 *
 * \param a  The attribute node
 * \return the type
 */
dom_attr_type dom_attr_get_type(dom_attr *a)
{
	return a->type;
}

/**
 * Get the integer value of this attribute
 *
 * \param a      The attribute object
 * \param value  The returned value
 * \return DOM_NO_ERR on success,
 *         DOM_ATTR_WRONG_TYPE_ERR if the attribute node is not a integer
 *                                 attribute
 */
dom_exception dom_attr_get_integer(dom_attr *a, unsigned long *value)
{
	if (a->type != DOM_ATTR_INTEGER)
		return DOM_ATTR_WRONG_TYPE_ERR;
	
	*value = a->value.lvalue;

	return DOM_NO_ERR;
}

/**
 * Set the integer value of this attribute
 *
 * \param a      The attribute object
 * \param value  The new value
 * \return DOM_NO_ERR on success,
 *         DOM_ATTR_WRONG_TYPE_ERR if the attribute node is not a integer
 *                                 attribute
 */
dom_exception dom_attr_set_integer(dom_attr *a, unsigned long value)
{
	/* If this is the first set method, we should fix this attribute
	 * type */
	if (a->type == DOM_ATTR_UNSET)
		a->type = DOM_ATTR_INTEGER;

	if (a->type != DOM_ATTR_INTEGER)
		return DOM_ATTR_WRONG_TYPE_ERR;
	
	if (a->value.lvalue == value)
		return DOM_NO_ERR;
	
	a->value.lvalue = value;

	struct dom_document *doc = dom_node_get_owner(a);
	struct dom_node_internal *ele = dom_node_get_parent(a);
	bool success = true;
	dom_exception err;
	err = _dom_dispatch_attr_modified_event(doc, ele, NULL, NULL,
			(dom_event_target *) a, NULL,
			DOM_MUTATION_MODIFICATION, &success);
	if (err != DOM_NO_ERR)
		return err;
	
	success = true;
	err = _dom_dispatch_subtree_modified_event(doc,
			(dom_event_target *) a, &success);
	return err;
}

/**
 * Get the short value of this attribute
 *
 * \param a      The attribute object
 * \param value  The returned value
 * \return DOM_NO_ERR on success,
 *         DOM_ATTR_WRONG_TYPE_ERR if the attribute node is not a short
 *                                 attribute
 */
dom_exception dom_attr_get_short(dom_attr *a, unsigned short *value)
{
	if (a->type != DOM_ATTR_SHORT)
		return DOM_ATTR_WRONG_TYPE_ERR;
	
	*value = a->value.svalue;

	return DOM_NO_ERR;
}

/**
 * Set the short value of this attribute
 *
 * \param a      The attribute object
 * \param value  The new value
 * \return DOM_NO_ERR on success,
 *         DOM_ATTR_WRONG_TYPE_ERR if the attribute node is not a short
 *                                 attribute
 */
dom_exception dom_attr_set_short(dom_attr *a, unsigned short value)
{
	/* If this is the first set method, we should fix this attribute
	 * type */
	if (a->type == DOM_ATTR_UNSET)
		a->type = DOM_ATTR_SHORT;

	if (a->type != DOM_ATTR_SHORT)
		return DOM_ATTR_WRONG_TYPE_ERR;
	
	if (a->value.svalue == value)
		return DOM_NO_ERR;
	
	a->value.svalue = value;

	struct dom_document *doc = dom_node_get_owner(a);
	struct dom_node_internal *ele = dom_node_get_parent(a);
	bool success = true;
	dom_exception err;
	err = _dom_dispatch_attr_modified_event(doc, ele, NULL, NULL,
			(dom_event_target *) a, NULL,
			DOM_MUTATION_MODIFICATION, &success);
	if (err != DOM_NO_ERR)
		return err;
	
	success = true;
	err = _dom_dispatch_subtree_modified_event(doc,
			(dom_event_target *) a, &success);
	return err;
}

/**
 * Get the bool value of this attribute
 *
 * \param a      The attribute object
 * \param value  The returned value
 * \return DOM_NO_ERR on success,
 *         DOM_ATTR_WRONG_TYPE_ERR if the attribute node is not a bool
 *                                 attribute
 */
dom_exception dom_attr_get_bool(dom_attr *a, bool *value)
{
	if (a->type != DOM_ATTR_BOOL)
		return DOM_ATTR_WRONG_TYPE_ERR;
	
	*value = a->value.bvalue;

	return DOM_NO_ERR;
}

/**
 * Set the bool value of this attribute
 *
 * \param a      The attribute object
 * \param value  The new value
 * \return DOM_NO_ERR on success,
 *         DOM_ATTR_WRONG_TYPE_ERR if the attribute node is not a bool
 *                                 attribute
 */
dom_exception dom_attr_set_bool(dom_attr *a, bool value)
{
	/* If this is the first set method, we should fix this attribute
	 * type */
	if (a->type == DOM_ATTR_UNSET)
		a->type = DOM_ATTR_BOOL;

	if (a->type != DOM_ATTR_BOOL)
		return DOM_ATTR_WRONG_TYPE_ERR;
	
	if (a->value.bvalue == value)
		return DOM_NO_ERR;
	
	a->value.bvalue = value;

	struct dom_document *doc = dom_node_get_owner(a);
	struct dom_node_internal *ele = dom_node_get_parent(a);
	bool success = true;
	dom_exception err;
	err = _dom_dispatch_attr_modified_event(doc, ele, NULL, NULL,
			(dom_event_target *) a, NULL,
			DOM_MUTATION_MODIFICATION, &success);
	if (err != DOM_NO_ERR)
		return err;
	
	success = true;
	err = _dom_dispatch_subtree_modified_event(doc,
			(dom_event_target *) a, &success);
	return err;
}

/**
 * Set the node as a readonly attribute
 *
 * \param a  The attribute
 */
void dom_attr_mark_readonly(dom_attr *a)
{
	a->read_only = true;
}

/* -------------------------------------------------------------------- */

/* The public virtual functions */

/**
 * Retrieve an attribute's name
 *
 * \param attr    Attribute to retrieve name from
 * \param result  Pointer to location to receive result
 * \return DOM_NO_ERR on success, appropriate dom_exception on failure
 *
 * The returned string will have its reference count increased. It is
 * the responsibility of the caller to unref the string once it has
 * finished with it.
 */
dom_exception _dom_attr_get_name(struct dom_attr *attr,
		struct dom_string **result)
{
	/* This is the same as nodeName */
	return dom_node_get_node_name(attr, result);
}

/**
 * Determine if attribute was specified or default
 *
 * \param attr    Attribute to inspect
 * \param result  Pointer to location to receive result
 * \return DOM_NO_ERR.
 */
dom_exception _dom_attr_get_specified(struct dom_attr *attr, bool *result)
{
	*result = attr->specified;

	return DOM_NO_ERR;
}

/**
 * Retrieve an attribute's value
 *
 * \param attr    Attribute to retrieve value from
 * \param result  Pointer to location to receive result
 * \return DOM_NO_ERR on success, appropriate dom_exception on failure
 *
 * The returned string will have its reference count increased. It is
 * the responsibility of the caller to unref the string once it has
 * finished with it.
 */
dom_exception _dom_attr_get_value(struct dom_attr *attr,
		struct dom_string **result)
{
	struct dom_node_internal *a = (struct dom_node_internal *) attr;
	struct dom_node_internal *c;
	struct dom_string *value, *temp;
	dom_exception err;

	err = _dom_document_create_string(a->owner, 
			NULL, 0, &value);
	if (err != DOM_NO_ERR) {
		return err;
	}

	/* Force unknown types to strings, if necessary */
	if (attr->type == DOM_ATTR_UNSET && a->first_child != NULL) {
		attr->type = DOM_ATTR_STRING;
	}

	/* If this attribute node is not a string one, we just return an empty
	 * string */
	if (attr->type != DOM_ATTR_STRING) {
		*result = value;
		return DOM_NO_ERR;
	}

	/* Traverse children, building a string representation as we go */
	for (c = a->first_child; c != NULL; c = c->next) {
		if (c->type == DOM_TEXT_NODE && c->value != NULL) {
			/* Append to existing value */
			err = dom_string_concat(value, c->value, &temp);
			if (err != DOM_NO_ERR) {
				dom_string_unref(value);
				return err;
			}

			/* Finished with previous value */
			dom_string_unref(value);

			/* Claim new value */
			value = temp;
		} else if (c->type == DOM_ENTITY_REFERENCE_NODE) {
			struct dom_string *tr;

			/* Get textual representation of entity */
			err = _dom_entity_reference_get_textual_representation(
					(struct dom_entity_reference *) c,
					&tr);
			if (err != DOM_NO_ERR) {
				dom_string_unref(value);
				return err;
			}

			/* Append to existing value */
			err = dom_string_concat(value, tr, &temp);
			if (err != DOM_NO_ERR) {
				dom_string_unref(tr);
				dom_string_unref(value);
				return err;
			}

			/* No longer need textual representation */
			dom_string_unref(tr);

			/* Finished with previous value */
			dom_string_unref(value);

			/* Claim new value */
			value = temp;
		}
	}

	*result = value;

	return DOM_NO_ERR;
}

/**
 * Set an attribute's value
 *
 * \param attr   Attribute to retrieve value from
 * \param value  New value for attribute
 * \return DOM_NO_ERR                      on success,
 *         DOM_NO_MODIFICATION_ALLOWED_ERR if attribute is readonly.
 */
dom_exception _dom_attr_set_value(struct dom_attr *attr,
		struct dom_string *value)
{
	struct dom_node_internal *a = (struct dom_node_internal *) attr;
	struct dom_node_internal *c, *d;
	struct dom_text *text;
	dom_exception err;

	/* Ensure attribute is writable */
	if (_dom_node_readonly(a))
		return DOM_NO_MODIFICATION_ALLOWED_ERR;

	/* If this is the first set method, we should fix this attribute
	 * type */
	if (attr->type == DOM_ATTR_UNSET)
		attr->type = DOM_ATTR_STRING;
	
	if (attr->type != DOM_ATTR_STRING)
		return DOM_ATTR_WRONG_TYPE_ERR;
	
	dom_string *name = NULL;

	err = _dom_attr_get_name(attr, &name);
	if (err != DOM_NO_ERR)
		return err;
	
	dom_string *parsed = NULL;
	err = dom_element_parse_attribute(a->parent, name, value, &parsed);
	if (err != DOM_NO_ERR) {
		dom_string_unref(name);
		return err;
	}

	/* Create text node containing new value */
	err = dom_document_create_text_node(a->owner, parsed, &text);
	if (err != DOM_NO_ERR)
		return err;
	
	dom_string_unref(parsed);

	/* Destroy children of this node */
	for (c = a->first_child; c != NULL; c = d) {
		d = c->next;

		/* Detach child */
		c->parent = NULL;

		/* Detach from sibling list */
		c->previous = NULL;
		c->next = NULL;

		dom_node_try_destroy(c);
	}

	/* And insert the text node as the value */
	((struct dom_node_internal *) text)->parent = a;
	a->first_child = a->last_child = (struct dom_node_internal *) text;
	dom_node_unref(text);
	dom_node_remove_pending(text);

	/* Now the attribute node is specified */
	attr->specified = true;

	return DOM_NO_ERR;
}

/**
 * Retrieve the owning element of an attribute
 *
 * \param attr    The attribute to extract owning element from
 * \param result  Pointer to location to receive result
 * \return DOM_NO_ERR.
 *
 * The returned node will have its reference count increased. The caller
 * should unref it once it has finished with it.
 */
dom_exception _dom_attr_get_owner(struct dom_attr *attr,
		struct dom_element **result)
{
	struct dom_node_internal *a = (struct dom_node_internal *) attr;

	/* If there is an owning element, increase its reference count */
	if (a->parent != NULL)
		dom_node_ref(a->parent);

	*result = (struct dom_element *) a->parent;

	return DOM_NO_ERR;
}

/**
 * Retrieve an attribute's type information
 *
 * \param attr    The attribute to extract type information from
 * \param result  Pointer to location to receive result
 * \return DOM_NOT_SUPPORTED_ERR, we don't support this API now.
 *
 * The returned type info will have its reference count increased. The caller
 * should unref it once it has finished with it.
 */
dom_exception _dom_attr_get_schema_type_info(struct dom_attr *attr,
		struct dom_type_info **result)
{
	UNUSED(attr);
	UNUSED(result);

	return DOM_NOT_SUPPORTED_ERR;
}

/**
 * Determine if an attribute if of type ID
 *
 * \param attr    The attribute to inspect
 * \param result  Pointer to location to receive result
 * \return DOM_NO_ERR.
 */
dom_exception _dom_attr_is_id(struct dom_attr *attr, bool *result)
{
	*result = attr->is_id;

	return DOM_NO_ERR;
}

/*------------- The overload virtual functions ------------------------*/

/* Overload function of Node, please refer node.c for the detail of this 
 * function. */
dom_exception _dom_attr_get_node_value(dom_node_internal *node,
		struct dom_string **result)
{
	dom_attr *attr = (dom_attr *) node;

	return _dom_attr_get_value(attr, result);
}

/* Overload function of Node, please refer node.c for the detail of this 
 * function. */
dom_exception _dom_attr_clone_node(dom_node_internal *node, bool deep,
		dom_node_internal **result)
{
	dom_exception err;
	dom_attr *attr;

	/* Discard the warnings */
	UNUSED(deep);

	/* Clone an Attr always clone all its children */
	err = _dom_node_clone_node(node, true, result);
	if (err != DOM_NO_ERR)
		return err;
	
	attr = (dom_attr *) *result;
	/* Clone an Attr always result a specified Attr, 
	 * see DOM Level 3 Node.cloneNode */
	attr->specified = true;

	return DOM_NO_ERR;
}

/* Overload function of Node, please refer node.c for the detail of this 
 * function. */
dom_exception _dom_attr_set_prefix(dom_node_internal *node,
		struct dom_string *prefix)
{
	/* Really I don't know whether there should something
	 * special to do here */
	return _dom_node_set_prefix(node, prefix);
}

/* Overload function of Node, please refer node.c for the detail of this 
 * function. */
dom_exception _dom_attr_lookup_prefix(dom_node_internal *node,
		struct dom_string *namespace, struct dom_string **result)
{
	struct dom_element *owner;
	dom_exception err;

	err = dom_attr_get_owner_element(node, &owner);
	if (err != DOM_NO_ERR)
		return err;
	
	if (owner == NULL) {
		*result = NULL;
		return DOM_NO_ERR;
	}

	return dom_node_lookup_prefix(owner, namespace, result);
}

/* Overload function of Node, please refer node.c for the detail of this 
 * function. */
dom_exception _dom_attr_is_default_namespace(dom_node_internal *node,
		struct dom_string *namespace, bool *result)
{
	struct dom_element *owner;
	dom_exception err;

	err = dom_attr_get_owner_element(node, &owner);
	if (err != DOM_NO_ERR)
		return err;
	
	if (owner == NULL) {
		*result = false;
		return DOM_NO_ERR;
	}

	return dom_node_is_default_namespace(owner, namespace, result);
}

/* Overload function of Node, please refer node.c for the detail of this 
 * function. */
dom_exception _dom_attr_lookup_namespace(dom_node_internal *node,
		struct dom_string *prefix, struct dom_string **result)
{
	struct dom_element *owner;
	dom_exception err;

	err = dom_attr_get_owner_element(node, &owner);
	if (err != DOM_NO_ERR)
		return err;
	
	if (owner == NULL) {
		*result = NULL;
		return DOM_NO_ERR;
	}

	return dom_node_lookup_namespace(owner, prefix, result);
}


/*----------------------------------------------------------------------*/

/* The protected virtual functions */

/* The virtual destroy function of this class */
void __dom_attr_destroy(dom_node_internal *node)
{
	dom_document *doc = node->owner;

	assert(doc != NULL);
	_dom_attr_destroy(doc, (dom_attr *) node);
}

/* The memory allocator of this class */
dom_exception _dom_attr_alloc(struct dom_document *doc, 
		struct dom_node_internal *n, struct dom_node_internal **ret)
{
	UNUSED(n);
	dom_attr *a;
	
	a = _dom_document_alloc(doc, NULL, sizeof(struct dom_attr));
	if (a == NULL)
		return DOM_NO_MEM_ERR;
	
	*ret = (dom_node_internal *) a;
	dom_node_set_owner(*ret, doc);

	return DOM_NO_ERR;
}

/* The copy constructor of this class  */
dom_exception _dom_attr_copy(struct dom_node_internal *new, 
		struct dom_node_internal *old)
{
	dom_attr *na = (dom_attr *) new;
	dom_attr *oa = (dom_attr *) old;

	na->specified = oa->specified;

	/* TODO: deal with dom_type_info, it get no definition ! */
	na->schema_type_info = NULL;

	na->is_id = oa->is_id;

	na->type = oa->type;

	na->value = oa->value;

	/* TODO: is this correct? */
	na->read_only = false;

	return _dom_node_copy(new, old);
}


/**
 * Set/Unset whether this attribute is a ID attribute 
 *
 * \param attr   The attribute
 * \param is_id  Whether it is a ID attribute
 */
void _dom_attr_set_isid(struct dom_attr *attr, bool is_id)
{
	attr->is_id = is_id;
}

/**
 * Set/Unset whether the attribute is a specified one.
 *
 * \param attr       The attribute node
 * \param specified  Whether this attribute is a specified one
 */
void _dom_attr_set_specified(struct dom_attr *attr, bool specified)
{
	attr->specified = specified;
}

/**
 * Whether this attribute node is readonly
 *
 * \param a  The node
 * \return true if this Attr is readonly, false otherwise
 */
bool _dom_attr_readonly(const dom_attr *a)
{
	return a->read_only;
}