/* * tree.c : implemetation of access function for an XML tree. */ #include #include #include #include #include "tree.h" /************************************************************************ * * * Allocation and deallocation of basic structures * * * ************************************************************************/ /* * Creation of a new DTD. */ xmlDtdPtr xmlNewDtd(xmlDocPtr doc, const char *href, const char *AS) { xmlDtdPtr cur; /* * Allocate a new DTD and fill the fields. */ cur = (xmlDtdPtr) malloc(sizeof(xmlDtd)); if (cur == NULL) { fprintf(stderr, "xmlNewDtd : malloc failed\n"); return(NULL); } cur->next = NULL; if (href != NULL) cur->href = strdup(href); else cur->href = NULL; if (AS != NULL) cur->AS = strdup(AS); else cur->AS = NULL; if (doc != NULL) { cur->next = doc->dtds; doc->dtds = cur; } return(cur); } /* * Freeing a DTD */ void xmlFreeDtd(xmlDtdPtr cur) { if (cur == NULL) { fprintf(stderr, "xmlFreeDtd : DTD == NULL\n"); return; } if (cur->href != NULL) free((char *) cur->href); if (cur->AS != NULL) free((char *) cur->AS); memset(cur, -1, sizeof(xmlDtd)); free(cur); } /* * Creation of a new document */ xmlDocPtr xmlNewDoc(const char *version) { xmlDocPtr cur; if (version == NULL) { fprintf(stderr, "xmlNewDoc : version == NULL\n"); return(NULL); } /* * Allocate a new document and fill the fields. */ cur = (xmlDocPtr) malloc(sizeof(xmlDoc)); if (cur == NULL) { fprintf(stderr, "xmlNewDoc : malloc failed\n"); return(NULL); } cur->version = strdup(version); cur->root = NULL; cur->dtds = NULL; return(cur); } /* * Freeing a document : all the tree is freed too. */ void xmlFreeDoc(xmlDocPtr cur) { if (cur == NULL) { fprintf(stderr, "xmlFreeDoc : document == NULL\n"); return; } free((char *) cur->version); if (cur->root != NULL) xmlFreeNode(cur->root); memset(cur, -1, sizeof(xmlDoc)); free(cur); } /* * Creation of a new property element in a given DTD. */ xmlPropPtr xmlNewProp(xmlNodePtr node, const char *name, char *value) { xmlPropPtr cur; if (name == NULL) { fprintf(stderr, "xmlNewProp : name == NULL\n"); return(NULL); } /* * Allocate a new property and fill the fields. */ cur = (xmlPropPtr) malloc(sizeof(xmlProp)); if (cur == NULL) { fprintf(stderr, "xmlNewProp : malloc failed\n"); return(NULL); } cur->node = node; cur->name = strdup(name); if (value != NULL) cur->value = strdup(value); else cur->value = NULL; if (node != NULL) { cur->next = node->properties; node->properties = cur; } else cur->next = NULL; return(cur); } /* * Freeing a property list : Free a property and all its siblings, * this is a recursive behaviour, all the childs * are freed too. */ void xmlFreePropList(xmlPropPtr cur) { xmlPropPtr next; if (cur == NULL) { fprintf(stderr, "xmlFreePropList : property == NULL\n"); return; } while (cur != NULL) { next = cur->next; xmlFreeProp(cur); cur = next; } } /* * Freeing a property. */ void xmlFreeProp(xmlPropPtr cur) { if (cur == NULL) { fprintf(stderr, "xmlFreeProp : property == NULL\n"); return; } if (cur->name != NULL) free((char *) cur->name); if (cur->value != NULL) free((char *) cur->value); memset(cur, -1, sizeof(xmlProp)); free(cur); } /* * Creation of a new node element in a given DTD. */ xmlNodePtr xmlNewNode(xmlDtdPtr dtd, const char *name, char *content) { xmlNodePtr cur; if (name == NULL) { fprintf(stderr, "xmlNewNode : name == NULL\n"); return(NULL); } /* * Allocate a new node and fill the fields. */ cur = (xmlNodePtr) malloc(sizeof(xmlNode)); if (cur == NULL) { fprintf(stderr, "xmlNewNode : malloc failed\n"); return(NULL); } cur->parent = NULL; cur->next = NULL; cur->childs = NULL; cur->properties = NULL; cur->type = 0; cur->name = name; cur->dtd = dtd; if (content != NULL) cur->content = strdup(content); else cur->content = NULL; return(cur); } /* * Creation of a new node contening text. */ xmlNodePtr xmlNewText(char *content) { xmlNodePtr cur; /* * Allocate a new node and fill the fields. */ cur = (xmlNodePtr) malloc(sizeof(xmlNode)); if (cur == NULL) { fprintf(stderr, "xmlNewNode : malloc failed\n"); return(NULL); } cur->parent = NULL; cur->next = NULL; cur->childs = NULL; cur->properties = NULL; cur->type = XML_TYPE_TEXT; cur->name = strdup("text");; cur->dtd = NULL; if (content != NULL) cur->content = strdup(content); else cur->content = NULL; return(cur); } /* * Creation of a new child element, added at the end. */ xmlNodePtr xmlNewChild(xmlNodePtr parent, xmlDtdPtr dtd, const char *name, char *content) { xmlNodePtr cur, prev; if (parent == NULL) { fprintf(stderr, "xmlNewChild : parent == NULL\n"); return(NULL); } if (name == NULL) { fprintf(stderr, "xmlNewChild : name == NULL\n"); return(NULL); } /* * Allocate a new node */ if (dtd == NULL) cur = xmlNewNode(parent->dtd, name, content); else cur = xmlNewNode(dtd, name, content); if (cur == NULL) return(NULL); /* * add the new element at the end of the childs list. */ cur->parent = parent; if (parent->childs == NULL) { parent->childs = cur; } else { prev = parent->childs; while (prev->next != NULL) prev = prev->next; prev->next = cur; } return(cur); } /* * Add a new child element, added at the end. */ xmlNodePtr xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { xmlNodePtr prev; if (parent == NULL) { fprintf(stderr, "xmladdChild : parent == NULL\n"); return(NULL); } if (cur == NULL) { fprintf(stderr, "xmladdChild : child == NULL\n"); return(NULL); } /* * add the new element at the end of the childs list. */ cur->parent = parent; if (parent->childs == NULL) { parent->childs = cur; } else { prev = parent->childs; while (prev->next != NULL) prev = prev->next; prev->next = cur; } return(cur); } /* * Freeing a node list : Free a node and all its siblings, * this is a recursive behaviour, all the childs * are freed too. */ void xmlFreeNodeList(xmlNodePtr cur) { xmlNodePtr next; if (cur == NULL) { fprintf(stderr, "xmlFreeNodeList : node == NULL\n"); return; } while (cur != NULL) { next = cur->next; xmlFreeNode(cur); cur = next; } } /* * Freeing a node : this is a recursive behaviour, all the childs * are freed too. */ void xmlFreeNode(xmlNodePtr cur) { if (cur == NULL) { fprintf(stderr, "xmlFreeNode : node == NULL\n"); return; } if (cur->properties != NULL) xmlFreePropList(cur->properties); if (cur->childs != NULL) xmlFreeNodeList(cur->childs); if (cur->content != NULL) free(cur->content); memset(cur, -1, sizeof(xmlNode)); free(cur); } /************************************************************************ * * * Content access functions * * * ************************************************************************/ /* * Changing the content of a node. */ void xmlNodeSetContent(xmlNodePtr cur, char *content) { if (cur == NULL) { fprintf(stderr, "xmlNodeSetContent : node == NULL\n"); return; } if (cur->content != NULL) free(cur->content); if (content != NULL) cur->content = strdup(content); else cur->content = NULL; } /* * Search a Dtd registered under a given name space for a document. */ xmlDtdPtr xmlSearchDtd(xmlDocPtr doc, char *nameSpace) { xmlDtdPtr cur; if ((doc == NULL) || (nameSpace == NULL)) return(NULL); cur = doc->dtds; while (cur != NULL) { if ((cur->AS != NULL) && (!strcmp(cur->AS, nameSpace))) return(cur); cur = cur->next; } return(NULL); } /************************************************************************ * * * Printing * * * ************************************************************************/ /* * Dump a DTD to the given FD */ void xmlDtdDump(FILE *f, xmlDtdPtr cur) { if (cur == NULL) { fprintf(stderr, "xmlDtdDump : DTD == NULL\n"); return; } fprintf(f, "href != NULL) fprintf(f, " href=\"%s\"", cur->href); if (cur->AS != NULL) fprintf(f, " AS=\"%s\"", cur->AS); fprintf(f, "?>\n"); } /* * Dump an XML node to the given FD */ void xmlNodeDump(FILE *f, xmlNodePtr cur, int level) { int i; if (cur == NULL) { fprintf(stderr, "xmlNodeDump : node == NULL\n"); return; } if (cur->type == XML_TYPE_TEXT) { if (cur->content != NULL) fprintf(f, "%s", cur->content); return; } for (i = 0;i < level;i++) fprintf(f, " "); if ((cur->dtd != NULL) && (cur->dtd->AS != NULL)) fprintf(f, "<%s:", cur->dtd->AS); else fprintf(f, "<"); fprintf(f, "%s", cur->name); if (cur->properties != NULL) xmlPropListDump(f, cur->properties); if ((cur->content == NULL) && (cur->childs == NULL)) { fprintf(f, "/>\n"); return; } fprintf(f, ">"); if (cur->content != NULL) fprintf(f, "%s", cur->content); if (cur->childs != NULL) { fprintf(f, "\n"); xmlNodeListDump(f, cur->childs, level + 1); for (i = 0;i < level;i++) fprintf(f, " "); } if ((cur->dtd != NULL) && (cur->dtd->AS != NULL)) fprintf(f, "dtd->AS); else fprintf(f, "\n", cur->name); } /* * Dump an XML property to the given FD */ void xmlPropDump(FILE *f, xmlPropPtr cur) { if (cur == NULL) { fprintf(stderr, "xmlPropDump : property == NULL\n"); return; } fprintf(f, " %s", cur->name); if (cur->value) { if (strchr(cur->value, '"') == NULL) fprintf(f, "=\"%s\"", cur->value); else fprintf(f, "='%s'", cur->value); } } /* * Dump an XML property list to the given FD */ void xmlPropListDump(FILE *f, xmlPropPtr cur) { if (cur == NULL) { fprintf(stderr, "xmlPropListDump : property == NULL\n"); return; } while (cur != NULL) { xmlPropDump(f, cur); cur = cur->next; } } /* * Dump an XML DTD list to the given FD */ void xmlDtdListDump(FILE *f, xmlDtdPtr cur) { if (cur == NULL) { fprintf(stderr, "xmlDtdListDump : DTD == NULL\n"); return; } while (cur != NULL) { xmlDtdDump(f, cur); cur = cur->next; } } /* * Dump an XML node list to the given FD */ void xmlNodeListDump(FILE *f, xmlNodePtr cur, int level) { if (cur == NULL) { fprintf(stderr, "xmlNodeListDump : node == NULL\n"); return; } while (cur != NULL) { xmlNodeDump(f, cur, level); cur = cur->next; } } /* * Dump an XML document to the given FD */ void xmlDocDump(FILE *f, xmlDocPtr cur) { if (cur == NULL) { fprintf(stderr, "xmlDocDump : document == NULL\n"); return; } fprintf(f, "\n", cur->version); if (cur->dtds != NULL) xmlDtdListDump(f, cur->dtds); if (cur->root != NULL) xmlNodeDump(f, cur->root, 0); } /************************************************************************ * * * Debug * * * ************************************************************************/ #ifdef DEBUG_TREE int main(void) { xmlDocPtr doc; xmlNodePtr tree, subtree; xmlDtdPtr dtd1; xmlDtdPtr dtd2; /* * build a fake XML document */ doc = xmlNewDoc("1.0"); dtd1 = xmlNewDtd(doc, "http://www.ietf.org/standards/dav/", "D"); dtd2 = xmlNewDtd(doc, "http://www.w3.com/standards/z39.50/", "Z"); doc->root = xmlNewNode(dtd1, "multistatus", NULL); tree = xmlNewChild(doc->root, NULL, "response", NULL); subtree = xmlNewChild(tree, NULL, "prop", NULL); xmlNewChild(subtree, dtd2, "Authors", NULL); subtree = xmlNewChild(tree, NULL, "status", "HTTP/1.1 420 Method Failure"); tree = xmlNewChild(doc->root, NULL, "response", NULL); subtree = xmlNewChild(tree, NULL, "prop", NULL); xmlNewChild(subtree, dtd2, "Copyright-Owner", NULL); subtree = xmlNewChild(tree, NULL, "status", "HTTP/1.1 409 Conflict"); tree = xmlNewChild(doc->root, NULL, "responsedescription", "Copyright Owner can not be deleted or altered"); /* * print it. */ xmlDocDump(stdout, doc); /* * free it. */ xmlFreeDoc(doc); return(0); } #endif