void chardata_fn(void *userData, const XML_Char *s, int len) { struct stack *stk = (struct stack *) userData; DOM_String *str; DOM_Text *tex; DOM_Node *parent; if (stk == NULL || s == NULL || len == 0) { DOM_Exception = DOM_NULL_POINTER_ERR; return; } parent = (DOM_Node *) stack_peek(stk); if (parent == NULL) { DOM_Exception = DOM_SYSTEM_ERR; return; } if ((str = (DOM_String *) malloc(len + 1)) == NULL) { DOM_Exception = DOM_NO_MEMORY_ERR; return; } memcpy(str, s, len); str[len] = '\0'; tex = DOM_Document_createTextNode(parent->ownerDocument, str); free(str); if (tex == NULL) { return; } DOM_Node_appendChild(parent, tex); if (DOM_Exception) { DOM_Document_destroyNode(parent->ownerDocument, tex); } }
void start_fn(void *userData, const XML_Char *name, const XML_Char **atts) { struct stack *stk = (struct stack *) userData; DOM_Node *parent, *child; int i; if (stk == NULL || name == NULL || atts == NULL) { DOM_Exception = DOM_NULL_POINTER_ERR; return; } parent = (DOM_Node *) stack_peek(stk); if (parent == NULL) { DOM_Exception = DOM_SYSTEM_ERR; return; } child = DOM_Document_createElement(parent->ownerDocument, name); if (child == NULL) { return; } for (i = 0; atts[i]; i += 2) { DOM_Element_setAttribute(child, atts[i], atts[i + 1]); if (DOM_Exception) { return; } } if (DOM_Node_appendChild(parent, child) == NULL) { return; } if (stack_push(stk, child) == 0) { DOM_Exception = DOM_SYSTEM_ERR; } }
void processing_fn(void *userData, const XML_Char *target, const XML_Char *data) { struct stack *stk = (struct stack *) userData; DOM_ProcessingInstruction *pi; DOM_Node *parent; parent = (DOM_Node *) stack_peek(stk); if (parent == NULL) { DOM_Exception = DOM_SYSTEM_ERR; return; } if ((pi = DOM_Document_createProcessingInstruction(parent->ownerDocument, target, data))) { DOM_Node_appendChild(parent, pi); if (DOM_Exception) { DOM_Document_destroyNode(parent->ownerDocument, pi); } } }
void comment_fn(void *userData, const XML_Char *s) { struct stack *stk = (struct stack *) userData; DOM_Comment *com; DOM_Node *parent; parent = (DOM_Node *) stack_peek(stk); if (parent == NULL) { DOM_Exception = DOM_SYSTEM_ERR; return; } if ((com = DOM_Document_createComment(parent->ownerDocument, s))) { DOM_Node_appendChild(parent, com); if (DOM_Exception) { DOM_Document_destroyNode(parent->ownerDocument, com); } } }
DOM_Node * DOM_Node_cloneNode(DOM_Node *node, int deep) { DOM_Node *clone = NULL; DOM_Node *ntmp, *ctmp; NodeEntry *e; if (node == NULL) { DOM_Exception = DOM_NULL_POINTER_ERR; return NULL; } switch(node->nodeType) { case DOM_ELEMENT_NODE: clone = DOM_Document_createElement(node->ownerDocument, node->nodeName); if (clone) { for (e = node->attributes->first; e != NULL; e = e->next) { if ((ctmp = DOM_Node_cloneNode(e->node, deep)) == NULL || NodeList_append(clone->attributes, ctmp) == NULL) { DOM_Document_destroyNode(clone->ownerDocument, ctmp); DOM_Document_destroyNode(clone->ownerDocument, clone); return NULL; } } } break; case DOM_ATTRIBUTE_NODE: if ((clone = DOM_Document_createAttribute(node->ownerDocument, node->nodeName))) { clone->u.Attr.specified = node->u.Attr.specified; free(clone->nodeValue); clone->u.Attr.value = clone->nodeValue = DOM_String_dup(node->nodeValue); if (clone->u.Attr.value == NULL) { DOM_Exception = DOM_NO_MEMORY_ERR; return NULL; } } break; case DOM_COMMENT_NODE: clone = DOM_Document_createComment(node->ownerDocument, node->nodeValue); break; case DOM_TEXT_NODE: clone = DOM_Document_createTextNode(node->ownerDocument, node->nodeValue); break; case DOM_CDATA_SECTION_NODE: clone = DOM_Document_createCDATASection(node->ownerDocument, node->nodeValue); break; case DOM_DOCUMENT_NODE: clone = DOM_Implementation_createDocument(NULL, NULL, NULL); break; case DOM_DOCUMENT_FRAGMENT_NODE: clone = DOM_Document_createDocumentFragment(node->ownerDocument); break; case DOM_PROCESSING_INSTRUCTION_NODE: clone = DOM_Document_createProcessingInstruction(node->ownerDocument, node->u.ProcessingInstruction.target, node->u.ProcessingInstruction.data); break; case DOM_ENTITY_REFERENCE_NODE: case DOM_ENTITY_NODE: case DOM_DOCUMENT_TYPE_NODE: case DOM_NOTATION_NODE: DOM_Exception = DOM_NOT_SUPPORTED_ERR; return NULL; } if (clone && node->childNodes) { for (ntmp = node->firstChild; ntmp != NULL; ntmp = ntmp->nextSibling) { ctmp = DOM_Node_cloneNode(ntmp, deep); if (ctmp == NULL || DOM_Node_appendChild(clone, ctmp) == NULL) { DOM_Document_destroyNode(clone->ownerDocument, ctmp); DOM_Document_destroyNode(clone->ownerDocument, clone); return NULL; } } } //DBL: erase next line if (DOM_Node_hasChildNodes(node)) {} return clone; }
DOM_Node * DOM_Node_appendChild(DOM_Node *node, DOM_Node *newChild) { int addingDocumentElement; if (node == NULL || newChild == NULL) { DOM_Exception = DOM_NULL_POINTER_ERR; return NULL; } if (newChild->ownerDocument != node->ownerDocument) { DOM_Exception = DOM_WRONG_DOCUMENT_ERR; return NULL; } addingDocumentElement = node->nodeType == DOM_DOCUMENT_NODE && newChild->nodeType == DOM_ELEMENT_NODE; if (CANNOT_HAVE_CHILDREN(node->nodeType) || Node_isAncestor(newChild, node) || (addingDocumentElement && node->u.Document.documentElement)) { DOM_Exception = DOM_HIERARCHY_REQUEST_ERR; return NULL; } if (newChild->nodeType == DOM_DOCUMENT_FRAGMENT_NODE) { DOM_Node *n, *nxt; for (n = newChild->firstChild; n != NULL; n = nxt) { nxt = n->nextSibling; if (DOM_Node_removeChild(newChild, n) == NULL) { return NULL; } if (DOM_Node_appendChild(node, n) == NULL) { DOM_Document_destroyNode(n->ownerDocument, n); return NULL; } } return newChild; } DOM_Node_removeChild(node, newChild); if (NodeList_append(node->childNodes, newChild) == NULL) { return NULL; } if (node->firstChild == NULL) { node->firstChild = node->lastChild = newChild; newChild->previousSibling = NULL; newChild->nextSibling = NULL; } else { node->lastChild->nextSibling = newChild; newChild->previousSibling = node->lastChild; node->lastChild = newChild; } newChild->nextSibling = NULL; newChild->parentNode = node; if (addingDocumentElement) { node->u.Document.documentElement = newChild; } return newChild; }