UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
document.cpp
Go to the documentation of this file.
1/*******************************************************************************
2 *
3 * Copyright (c) 2000-2003 Intel Corporation
4 * All rights reserved.
5 * Copyright (c) 2012 France Telecom All rights reserved.
6 * Copyright (C) 2022 GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
7 * Redistribution only with this Copyright remark. Last modified: 2025-05-29
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * - Neither name of Intel Corporation nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
29 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 ******************************************************************************/
38#include <ixml/ixmldebug.hpp>
39#include <ixml/ixmlparser.hpp>
40
41#include <cstring>
42
44 memset(doc, 0, sizeof(IXML_Document));
45}
46
48 if (doc != NULL) {
50 }
51}
52
61 IXML_Document* doc,
63 IXML_Node* nodeptr) {
64 if (nodeptr != NULL) {
65 nodeptr->ownerDocument = doc;
68 }
69}
70
71int ixmlDocument_importNode(IXML_Document* doc, IXML_Node* importNode, int deep,
72 IXML_Node** rtNode) {
73 unsigned short nodeType;
74 IXML_Node* newNode;
75
76 *rtNode = NULL;
77
78 if (doc == NULL || importNode == NULL) {
79 return IXML_INVALID_PARAMETER;
80 }
81
82 nodeType = ixmlNode_getNodeType(importNode);
83 if (nodeType == eDOCUMENT_NODE) {
84 return IXML_NOT_SUPPORTED_ERR;
85 }
86
87 newNode = ixmlNode_cloneNode(importNode, deep);
88 if (newNode == NULL) {
89 return IXML_FAILED;
90 }
91
93 *rtNode = newNode;
94
95 return IXML_SUCCESS;
96}
97
99 IXML_Element** rtElement) {
100 int errCode = IXML_SUCCESS;
101 IXML_Element* newElement = NULL;
102
103 if (doc == NULL || tagName == NULL) {
104 errCode = IXML_INVALID_PARAMETER;
105 goto ErrorHandler;
106 }
107
108 newElement = (IXML_Element*)malloc(sizeof(IXML_Element));
109 if (newElement == NULL) {
110 errCode = IXML_INSUFFICIENT_MEMORY;
111 goto ErrorHandler;
112 }
113
114 ixmlElement_init(newElement);
115 newElement->tagName = strdup(tagName);
116 if (newElement->tagName == NULL) {
117 ixmlElement_free(newElement);
118 newElement = NULL;
119 errCode = IXML_INSUFFICIENT_MEMORY;
120 goto ErrorHandler;
121 }
122 /* set the node fields */
123 newElement->n.nodeType = eELEMENT_NODE;
124 newElement->n.nodeName = strdup(tagName);
125 if (newElement->n.nodeName == NULL) {
126 free(newElement->tagName);
127 ixmlElement_free(newElement);
128 newElement = NULL;
129 errCode = IXML_INSUFFICIENT_MEMORY;
130 goto ErrorHandler;
131 }
132
133 newElement->n.ownerDocument = doc;
134
135ErrorHandler:
136 *rtElement = newElement;
137
138 return errCode;
139}
140
142 const DOMString tagName) {
143 IXML_Element* newElement = NULL;
144 int ret = IXML_SUCCESS;
145
146 ret = ixmlDocument_createElementEx(doc, tagName, &newElement);
147 if (ret != IXML_SUCCESS) {
148 IxmlPrintf(__FILE__, __LINE__, "ixmlDocument_createElement",
149 "Error %d\n", ret);
150 return NULL;
151 }
152 return newElement;
153}
154
156 IXML_Document* doc;
157 int errCode = IXML_SUCCESS;
158
159 doc = NULL;
160 doc = (IXML_Document*)malloc(sizeof(IXML_Document));
161 if (doc == NULL) {
162 errCode = IXML_INSUFFICIENT_MEMORY;
163 goto ErrorHandler;
164 }
165
167
168 doc->n.nodeName = strdup((const char*)DOCUMENTNODENAME);
169 if (doc->n.nodeName == NULL) {
171 doc = NULL;
172 errCode = IXML_INSUFFICIENT_MEMORY;
173 goto ErrorHandler;
174 }
175
176 doc->n.nodeType = eDOCUMENT_NODE;
177 doc->n.ownerDocument = doc;
178
179ErrorHandler:
180 *rtDoc = doc;
181 return errCode;
182}
183
185 IXML_Document* doc = NULL;
186
188
189 return doc;
190}
191
193 IXML_Node** textNode) {
194 IXML_Node* returnNode;
195 int rc = IXML_SUCCESS;
196
197 returnNode = NULL;
198 if (doc == NULL || data == NULL) {
199 rc = IXML_INVALID_PARAMETER;
200 goto ErrorHandler;
201 }
202
203 returnNode = (IXML_Node*)malloc(sizeof(IXML_Node));
204 if (returnNode == NULL) {
205 rc = IXML_INSUFFICIENT_MEMORY;
206 goto ErrorHandler;
207 }
208 /* initialize the node */
209 ixmlNode_init(returnNode);
210
211 returnNode->nodeName = strdup((const char*)TEXTNODENAME);
212 if (returnNode->nodeName == NULL) {
213 ixmlNode_free(returnNode);
214 returnNode = NULL;
215 rc = IXML_INSUFFICIENT_MEMORY;
216 goto ErrorHandler;
217 }
218 /* add in node value */
219 if (data != NULL) {
220 returnNode->nodeValue = strdup(data);
221 if (returnNode->nodeValue == NULL) {
222 ixmlNode_free(returnNode);
223 returnNode = NULL;
224 rc = IXML_INSUFFICIENT_MEMORY;
225 goto ErrorHandler;
226 }
227 }
228
229 returnNode->nodeType = eTEXT_NODE;
230 returnNode->ownerDocument = doc;
231
232ErrorHandler:
233 *textNode = returnNode;
234 return rc;
235}
236
238 const DOMString data) {
239 IXML_Node* returnNode = NULL;
240
241 ixmlDocument_createTextNodeEx(doc, data, &returnNode);
242
243 return returnNode;
244}
245
247 IXML_Attr** rtAttr) {
248 IXML_Attr* attrNode = NULL;
249 int errCode = IXML_SUCCESS;
250
251 attrNode = (IXML_Attr*)malloc(sizeof(IXML_Attr));
252 if (attrNode == NULL) {
253 errCode = IXML_INSUFFICIENT_MEMORY;
254 goto ErrorHandler;
255 }
256
257 if (doc == NULL || name == NULL) {
258 ixmlAttr_free(attrNode);
259 attrNode = NULL;
260 errCode = IXML_INVALID_PARAMETER;
261 goto ErrorHandler;
262 }
263
264 ixmlAttr_init(attrNode);
265 attrNode->n.nodeType = eATTRIBUTE_NODE;
266
267 /* set the node fields */
268 attrNode->n.nodeName = strdup(name);
269 if (attrNode->n.nodeName == NULL) {
270 ixmlAttr_free(attrNode);
271 attrNode = NULL;
272 errCode = IXML_INSUFFICIENT_MEMORY;
273 goto ErrorHandler;
274 }
275
276 attrNode->n.ownerDocument = doc;
277
278ErrorHandler:
279 *rtAttr = attrNode;
280 return errCode;
281}
282
284 const DOMString name) {
285 IXML_Attr* attrNode = NULL;
286
287 if (ixmlDocument_createAttributeEx(doc, name, &attrNode) != IXML_SUCCESS)
288 return NULL;
289
290 return attrNode;
291}
292
294 const DOMString namespaceURI,
295 const DOMString qualifiedName,
296 IXML_Attr** rtAttr) {
297 IXML_Attr* attrNode = NULL;
298 int errCode = IXML_SUCCESS;
299
300 if (doc == NULL || namespaceURI == NULL || qualifiedName == NULL) {
301 errCode = IXML_INVALID_PARAMETER;
302 goto ErrorHandler;
303 }
304
305 errCode = ixmlDocument_createAttributeEx(doc, qualifiedName, &attrNode);
306 if (errCode != IXML_SUCCESS) {
307 goto ErrorHandler;
308 }
309 /* set the namespaceURI field */
310 attrNode->n.namespaceURI = strdup(namespaceURI);
311 if (attrNode->n.namespaceURI == NULL) {
312 ixmlAttr_free(attrNode);
313 attrNode = NULL;
314 errCode = IXML_INSUFFICIENT_MEMORY;
315 goto ErrorHandler;
316 }
317 /* set the localName and prefix */
318 errCode = ixmlNode_setNodeName((IXML_Node*)attrNode, qualifiedName);
319 if (errCode != IXML_SUCCESS) {
320 ixmlAttr_free(attrNode);
321 attrNode = NULL;
322 goto ErrorHandler;
323 }
324
325ErrorHandler:
326 *rtAttr = attrNode;
327 return errCode;
328}
329
331 const DOMString namespaceURI,
332 const DOMString qualifiedName) {
333 IXML_Attr* attrNode = NULL;
334
335 ixmlDocument_createAttributeNSEx(doc, namespaceURI, qualifiedName,
336 &attrNode);
337
338 return attrNode;
339}
340
342 IXML_CDATASection** rtCD) {
343 int errCode = IXML_SUCCESS;
344 IXML_CDATASection* cDSectionNode = NULL;
345
346 if (doc == NULL || data == NULL) {
347 errCode = IXML_INVALID_PARAMETER;
348 goto ErrorHandler;
349 }
350
351 cDSectionNode = (IXML_CDATASection*)malloc(sizeof(IXML_CDATASection));
352 if (cDSectionNode == NULL) {
353 errCode = IXML_INSUFFICIENT_MEMORY;
354 goto ErrorHandler;
355 }
356
357 ixmlCDATASection_init(cDSectionNode);
358 cDSectionNode->n.nodeType = eCDATA_SECTION_NODE;
359 cDSectionNode->n.nodeName = strdup((const char*)CDATANODENAME);
360 if (cDSectionNode->n.nodeName == NULL) {
361 ixmlCDATASection_free(cDSectionNode);
362 cDSectionNode = NULL;
363 errCode = IXML_INSUFFICIENT_MEMORY;
364 goto ErrorHandler;
365 }
366
367 cDSectionNode->n.nodeValue = strdup(data);
368 if (cDSectionNode->n.nodeValue == NULL) {
369 ixmlCDATASection_free(cDSectionNode);
370 cDSectionNode = NULL;
371 errCode = IXML_INSUFFICIENT_MEMORY;
372 goto ErrorHandler;
373 }
374
375 cDSectionNode->n.ownerDocument = doc;
376
377ErrorHandler:
378 *rtCD = cDSectionNode;
379 return errCode;
380}
381
383 const DOMString data) {
384 IXML_CDATASection* cDSectionNode = NULL;
385
386 ixmlDocument_createCDATASectionEx(doc, data, &cDSectionNode);
387
388 return cDSectionNode;
389}
390
392 const DOMString namespaceURI,
393 const DOMString qualifiedName,
394 IXML_Element** rtElement) {
395 IXML_Element* newElement = NULL;
396 int ret = IXML_SUCCESS;
397 int line = 0;
398
399 if (doc == NULL || namespaceURI == NULL || qualifiedName == NULL) {
400 line = __LINE__;
401 ret = IXML_INVALID_PARAMETER;
402 goto ErrorHandler;
403 }
404
405 ret = ixmlDocument_createElementEx(doc, qualifiedName, &newElement);
406 if (ret != IXML_SUCCESS) {
407 line = __LINE__;
408 goto ErrorHandler;
409 }
410 /* set the namespaceURI field */
411 newElement->n.namespaceURI = strdup(namespaceURI);
412 if (newElement->n.namespaceURI == NULL) {
413 line = __LINE__;
414 ixmlElement_free(newElement);
415 newElement = NULL;
416 ret = IXML_INSUFFICIENT_MEMORY;
417 goto ErrorHandler;
418 }
419 /* set the localName and prefix */
420 ret = ixmlNode_setNodeName((IXML_Node*)newElement, qualifiedName);
421 if (ret != IXML_SUCCESS) {
422 line = __LINE__;
423 ixmlElement_free(newElement);
424 newElement = NULL;
425 ret = IXML_INSUFFICIENT_MEMORY;
426 goto ErrorHandler;
427 }
428
429 newElement->n.nodeValue = NULL;
430
431ErrorHandler:
432 *rtElement = newElement;
433 if (ret != IXML_SUCCESS) {
434 IxmlPrintf(__FILE__, line, "ixmlDocument_createElementNSEx",
435 "Error %d\n", ret);
436 }
437
438 return ret;
439}
440
442 const DOMString namespaceURI,
443 const DOMString qualifiedName) {
444 IXML_Element* newElement = NULL;
445
446 ixmlDocument_createElementNSEx(doc, namespaceURI, qualifiedName,
447 &newElement);
448
449 return newElement;
450}
451
453 const DOMString tagName) {
454 IXML_NodeList* returnNodeList = NULL;
455
456 if (doc == NULL || tagName == NULL) {
457 return NULL;
458 }
459
460 ixmlNode_getElementsByTagName((IXML_Node*)doc, tagName, &returnNodeList);
461
462 return returnNodeList;
463}
464
466 const DOMString namespaceURI,
467 const DOMString localName) {
468 IXML_NodeList* returnNodeList = NULL;
469
470 if (doc == NULL || namespaceURI == NULL || localName == NULL) {
471 return NULL;
472 }
473
474 ixmlNode_getElementsByTagNameNS((IXML_Node*)doc, namespaceURI, localName,
475 &returnNodeList);
476
477 return returnNodeList;
478}
479
481 const DOMString tagName) {
482 IXML_Element* rtElement = NULL;
483 IXML_Node* nodeptr = (IXML_Node*)doc;
484 const char* name;
485
486 if (nodeptr == NULL || tagName == NULL) {
487 return rtElement;
488 }
489
490 if (ixmlNode_getNodeType(nodeptr) == eELEMENT_NODE) {
491 name = ixmlNode_getNodeName(nodeptr);
492 if (name == NULL) {
493 return rtElement;
494 }
495
496 if (strcmp(tagName, name) == 0) {
497 rtElement = (IXML_Element*)nodeptr;
498 return rtElement;
499 } else {
500 rtElement = ixmlDocument_getElementById(
501 (IXML_Document*)ixmlNode_getFirstChild(nodeptr), tagName);
502 if (rtElement == NULL) {
503 rtElement = ixmlDocument_getElementById(
504 (IXML_Document*)ixmlNode_getNextSibling(nodeptr), tagName);
505 }
506 }
507 } else {
508 rtElement = ixmlDocument_getElementById(
509 (IXML_Document*)ixmlNode_getFirstChild(nodeptr), tagName);
510 if (rtElement == NULL) {
511 rtElement = ixmlDocument_getElementById(
512 (IXML_Document*)ixmlNode_getNextSibling(nodeptr), tagName);
513 }
514 }
515
516 return rtElement;
517}
static void ixmlDocument_setOwnerDocument(IXML_Document *doc, IXML_Node *nodeptr)
Definition document.cpp:59
Data structure representing a CDATA section node.
Definition ixml.hpp:162
Data structure common to all types of nodes.
Definition ixml.hpp:132
Data structure representing an Attribute node.
Definition ixml.hpp:177
Data structure representing a list of nodes.
Definition ixml.hpp:193
Data structure representing an Element node.
Definition ixml.hpp:169
Data structure representing the DOM Document.
Definition ixml.hpp:155
IXML_Element * ixmlDocument_getElementById(IXML_Document *doc, const DOMString tagName)
Returns the Element whose ID matches that given id.
Definition document.cpp:480
PUPNP_Api unsigned short ixmlNode_getNodeType(IXML_Node *nodeptr)
Retrieves the type of a Node. Note that not all possible return values are actually implemented.
Definition node.cpp:326
int ixmlDocument_createTextNodeEx(IXML_Document *doc, const DOMString data, IXML_Node **textNode)
Creates a new Text node with the given data.
Definition document.cpp:192
IXML_Element * ixmlDocument_createElementNS(IXML_Document *doc, const DOMString namespaceURI, const DOMString qualifiedName)
Creates a new Element node in the given qualified name and namespace URI.
Definition document.cpp:441
IXML_Element * ixmlDocument_createElement(IXML_Document *doc, const DOMString tagName)
Creates a new Element node with the given tag name.
Definition document.cpp:141
PUPNP_Api void ixmlAttr_free(IXML_Attr *attrNode)
Frees an Attr node.
Definition attr.cpp:45
IXML_CDATASection * ixmlDocument_createCDATASection(IXML_Document *doc, const DOMString data)
Creates a new CDATASection node with given data.
Definition document.cpp:382
int ixmlDocument_createAttributeNSEx(IXML_Document *doc, const DOMString namespaceURI, const DOMString qualifiedName, IXML_Attr **rtAttr)
Creates a new Attr node with the given qualified name and namespace URI.
Definition document.cpp:293
PUPNP_Api void ixmlCDATASection_init(IXML_CDATASection *nodeptr)
Initializes a CDATASection node.
Definition node.cpp:49
int ixmlDocument_createAttributeEx(IXML_Document *doc, const DOMString name, IXML_Attr **rtAttr)
Creates a new Attr node with the given name.
Definition document.cpp:246
#define DOMString
The type of DOM strings.
Definition ixml.hpp:47
IXML_NodeList * ixmlDocument_getElementsByTagNameNS(IXML_Document *doc, const DOMString namespaceURI, const DOMString localName)
Returns a NodeList of Elements that match the given local name and namespace URI in the order they ar...
Definition document.cpp:465
PUPNP_Api void ixmlNode_free(IXML_Node *nodeptr)
Frees a Node and all Nodes in its subtree.
Definition node.cpp:120
PUPNP_Api void ixmlCDATASection_free(IXML_CDATASection *nodeptr)
Frees a CDATASection node.
Definition node.cpp:53
IXML_Attr * ixmlDocument_createAttributeNS(IXML_Document *doc, const DOMString namespaceURI, const DOMString qualifiedName)
Creates a new Attribute node with the given qualified name and namespace URI.
Definition document.cpp:330
PUPNP_Api IXML_Node * ixmlNode_cloneNode(IXML_Node *nodeptr, int deep)
Clones a Node.
Definition node.cpp:1075
IXML_Node * ixmlDocument_createTextNode(IXML_Document *doc, const DOMString data)
Creates a new Text node with the given data.
Definition document.cpp:237
PUPNP_Api IXML_Node * ixmlNode_getNextSibling(IXML_Node *nodeptr)
Retrieves the sibling Node immediately following this Node.
Definition node.cpp:375
PUPNP_Api void ixmlElement_init(IXML_Element *element)
Initializes a IXML_Element node.
Definition element.cpp:43
IXML_NodeList * ixmlDocument_getElementsByTagName(IXML_Document *doc, const DOMString tagName)
Returns a NodeList of all Elements that match the given tag name in the order in which they were enco...
Definition document.cpp:452
PUPNP_Api const DOMString ixmlNode_getNodeName(IXML_Node *nodeptr)
Returns the name of the Node, depending on what type of Node it is, in a read-only string.
Definition node.cpp:179
void ixmlDocument_init(IXML_Document *doc)
Initializes a Document node.
Definition document.cpp:43
#define CDATANODENAME
The type of the DOM node.
Definition ixml.hpp:116
int ixmlDocument_createCDATASectionEx(IXML_Document *doc, const DOMString data, IXML_CDATASection **rtCD)
Creates a new CDATASection node with given data.
Definition document.cpp:341
#define TEXTNODENAME
The type of the DOM node.
Definition ixml.hpp:115
int ixmlDocument_createElementEx(IXML_Document *doc, const DOMString tagName, IXML_Element **rtElement)
Creates a new Element node with the given tag name.
Definition document.cpp:98
int ixmlDocument_importNode(IXML_Document *doc, IXML_Node *importNode, int deep, IXML_Node **rtNode)
Imports a Node from another Document into this Document.
Definition document.cpp:71
PUPNP_Api void ixmlElement_free(IXML_Element *element)
Frees the given Element and any subtree of the Element.
Definition element.cpp:654
IXML_Document * ixmlDocument_createDocument()
Creates a new empty Document node.
Definition document.cpp:184
#define DOCUMENTNODENAME
The type of the DOM node.
Definition ixml.hpp:114
IXML_Attr * ixmlDocument_createAttribute(IXML_Document *doc, const DOMString name)
Creates a new Attr node with the given name.
Definition document.cpp:283
PUPNP_Api IXML_Node * ixmlNode_getFirstChild(IXML_Node *nodeptr)
Retrieves the first child Node of a Node.
Definition node.cpp:342
int ixmlDocument_createDocumentEx(IXML_Document **rtDoc)
Creates a new empty Document node.
Definition document.cpp:155
int ixmlDocument_createElementNSEx(IXML_Document *doc, const DOMString namespaceURI, const DOMString qualifiedName, IXML_Element **rtElement)
Creates a new Element node in the given qualified name and namespace URI.
Definition document.cpp:391
void ixmlDocument_free(IXML_Document *doc)
Frees a Document object and all Nodes associated with it.
Definition document.cpp:47
Auxiliar routines to aid debugging.
void IxmlPrintf(const char *DbgFileName, int DbgLineNo, const char *FunctionName, const char *FmtStr,...)
Prints the debug statement either on the standard output or log file along with the information from ...
Definition ixmldebug.cpp:13
void ixmlNode_getElementsByTagName(IXML_Node *n, const char *tagname, IXML_NodeList **list)
Returns a nodeList of all descendant Elements with a given tagName, in the order in which they are en...
Definition node.cpp:1211
void ixmlNode_init(IXML_Node *nodeptr)
Intializes a node.
Definition node.cpp:43
int ixmlNode_setNodeName(IXML_Node *node, const DOMString qualifiedName)
Definition node.cpp:1284
void ixmlAttr_init(IXML_Attr *attrNode)
ixmlAttr_init
Definition attr.cpp:39
void ixmlNode_getElementsByTagNameNS(IXML_Node *n, const char *namespaceURI, const char *localName, IXML_NodeList **list)
Returns a nodeList of all the descendant Elements with a given local name and namespace URI in the or...
Definition node.cpp:1261