1 /* DOMC Document Object Model library in C
2  * Copyright (c) 2001 Michael B. Allen <mba2000 ioplex.com>
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 /* namednodemap.c - the DOM_NamedNodeMap interface
26  */
27 
28 #include <string.h>
29 #include "defines.h"
30 #include <mba/msgno.h>
31 #include "domc.h"
32 #include "dom.h"
33 
34 /* NamedNodeMap
35  */
36 
37 void
DOM_Document_destroyNamedNodeMap(DOM_Document * doc,DOM_NamedNodeMap * map,int free_nodes)38 DOM_Document_destroyNamedNodeMap(DOM_Document *doc, DOM_NamedNodeMap *map, int free_nodes)
39 {
40     DOM_Document_destroyNodeList(doc, map, free_nodes);
41 }
42 DOM_NamedNodeMap *
Document_createNamedNodeMap(DOM_Document * doc)43 Document_createNamedNodeMap(DOM_Document *doc)
44 {
45     return Document_createNodeList(doc);
46 }
47 
48 DOM_Node *
DOM_NamedNodeMap_getNamedItem(const DOM_NamedNodeMap * map,const DOM_String * name)49 DOM_NamedNodeMap_getNamedItem(const DOM_NamedNodeMap *map, const DOM_String *name)
50 {
51 	NodeEntry *e;
52 	unsigned short nodeType;
53 
54 	if (map && name) {
55 		if (map->filter) {
56 			nodeType = map->filter;
57 			map = map->list;
58 			for (e = map->first; e != NULL; e = e->next) {
59 				if (e->node->nodeType == nodeType && strcoll(name, e->node->nodeName) == 0) {
60 					return e->node;
61 				}
62 			}
63 		} else {
64 			for (e = map->first; e != NULL; e = e->next) {
65 				if (strcoll(name, e->node->nodeName) == 0) {
66 					return e->node;
67 				}
68 			}
69 		}
70 	}
71 
72 	return NULL;
73 }
74 DOM_Node *
DOM_NamedNodeMap_setNamedItem(DOM_NamedNodeMap * map,DOM_Node * arg)75 DOM_NamedNodeMap_setNamedItem(DOM_NamedNodeMap *map, DOM_Node *arg)
76 {
77 	NodeEntry *e;
78 
79 	if (map && arg) {
80 		if (map->filter) {
81 			DOM_Exception = DOM_NO_MODIFICATION_ALLOWED_ERR;
82 			PMNO(DOM_Exception);
83 			return NULL;
84 		}
85 		if (map->_ownerDocument != arg->ownerDocument) {
86 			DOM_Exception = DOM_WRONG_DOCUMENT_ERR;
87 			PMNO(DOM_Exception);
88 			return NULL;
89 		}
90 		if (arg->nodeType == DOM_ATTRIBUTE_NODE && arg->u.Attr.ownerElement &&
91 						arg->u.Attr.ownerElement != map->_ownerElement) {
92 			DOM_Exception = DOM_INUSE_ATTRIBUTE_ERR;
93 			PMNO(DOM_Exception);
94 			return NULL;
95 		}
96 		for (e = map->first; e != NULL && strcoll(arg->nodeName, e->node->nodeName); e = e->next) {
97 			;
98 		}
99 		if (e) {
100 			DOM_Node *tmp = e->node;
101 			e->node = arg;
102 			if (arg->nodeType == DOM_ATTRIBUTE_NODE) {
103 				arg->u.Attr.ownerElement = map->_ownerElement;
104 				tmp->u.Attr.ownerElement = NULL;
105 			}
106 			return tmp;
107 		}
108 		NodeList_append(map, arg);
109 	}
110 
111 	return NULL;
112 }
113 DOM_Node *
DOM_NamedNodeMap_removeNamedItem(DOM_NamedNodeMap * map,const DOM_String * name)114 DOM_NamedNodeMap_removeNamedItem(DOM_NamedNodeMap *map, const DOM_String *name)
115 {
116 	NodeEntry *e;
117 	DOM_Node *r = NULL;
118 
119 	if (map && name) {
120 		if (map->filter) {
121 			DOM_Exception = DOM_NO_MODIFICATION_ALLOWED_ERR;
122 			PMNO(DOM_Exception);
123 			return NULL;
124 		}
125 		for (e = map->first; e != NULL; e = e->next) {
126 			if (strcoll(name, e->node->nodeName) == 0 &&
127 									NodeList_remove(map, e->node)) {
128 				r = e->node;
129 				free(e);
130 				if (r->nodeType == DOM_ATTRIBUTE_NODE) {
131 					r->u.Attr.ownerElement = NULL;
132 				}
133 				return r;
134 			}
135 		}
136 	}
137 
138 	DOM_Exception = DOM_NOT_FOUND_ERR;
139 	PMNO(DOM_Exception);
140 	return NULL;
141 }
142 DOM_Node *
DOM_NamedNodeMap_item(const DOM_NamedNodeMap * map,int index)143 DOM_NamedNodeMap_item(const DOM_NamedNodeMap *map, int index)
144 {
145 	return DOM_NodeList_item(map, index);
146 }
147 
148