Thu Apr 28 2011 17:15:26

Asterisk developer's documentation


xml.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2008, Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*! \file
00018  *
00019  * \brief XML abstraction layer
00020  *
00021  * \author Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
00022  */
00023 
00024 #include "asterisk.h"
00025 #include "asterisk/xml.h"
00026 
00027 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 197375 $")
00028 
00029 #if defined(HAVE_LIBXML2)
00030 #ifndef _POSIX_C_SOURCE /* Needed on Mac OS X */
00031 #define _POSIX_C_SOURCE 200112L
00032 #endif
00033 #include <libxml/parser.h>
00034 #include <libxml/tree.h>
00035 #include <libxml/xinclude.h>
00036 /* libxml2 ast_xml implementation. */
00037 
00038 
00039 int ast_xml_init(void)
00040 {
00041    LIBXML_TEST_VERSION
00042 
00043    return 0;
00044 }
00045 
00046 int ast_xml_finish(void)
00047 {
00048    xmlCleanupParser();
00049 
00050    return 0;
00051 }
00052 
00053 struct ast_xml_doc *ast_xml_open(char *filename)
00054 {
00055    xmlDoc *doc;
00056 
00057    if (!filename) {
00058       return NULL;
00059    }
00060 
00061    doc = xmlReadFile(filename, NULL, XML_PARSE_RECOVER);
00062    if (doc) {
00063       /* process xinclude elements. */
00064       if (xmlXIncludeProcess(doc) < 0) {
00065          xmlFreeDoc(doc);
00066          return NULL;
00067       }
00068    }
00069 
00070    return (struct ast_xml_doc *) doc;
00071 }
00072 
00073 void ast_xml_close(struct ast_xml_doc *doc)
00074 {
00075    if (!doc) {
00076       return;
00077    }
00078 
00079    xmlFreeDoc((xmlDoc *) doc);
00080    doc = NULL;
00081 }
00082 
00083 
00084 struct ast_xml_node *ast_xml_get_root(struct ast_xml_doc *doc)
00085 {
00086    xmlNode *root_node;
00087 
00088    if (!doc) {
00089       return NULL;
00090    }
00091 
00092    root_node = xmlDocGetRootElement((xmlDoc *) doc);
00093 
00094    return (struct ast_xml_node *) root_node;
00095 }
00096 
00097 void ast_xml_free_node(struct ast_xml_node *node)
00098 {
00099    if (!node) {
00100       return;
00101    }
00102 
00103    xmlFreeNode((xmlNode *) node);
00104    node = NULL;
00105 }
00106 
00107 void ast_xml_free_attr(const char *attribute)
00108 {
00109    if (attribute) {
00110       xmlFree((char *) attribute);
00111    }
00112 }
00113 
00114 void ast_xml_free_text(const char *text)
00115 {
00116    if (text) {
00117       xmlFree((char *) text);
00118    }
00119 }
00120 
00121 const char *ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname)
00122 {
00123    xmlChar *attrvalue;
00124 
00125    if (!node) {
00126       return NULL;
00127    }
00128 
00129    if (!attrname) {
00130       return NULL;
00131    }
00132 
00133    attrvalue = xmlGetProp((xmlNode *) node, (xmlChar *) attrname);
00134 
00135    return (const char *) attrvalue;
00136 }
00137 
00138 struct ast_xml_node *ast_xml_find_element(struct ast_xml_node *root_node, const char *name, const char *attrname, const char *attrvalue)
00139 {
00140    struct ast_xml_node *cur;
00141    const char *attr;
00142 
00143    if (!root_node) {
00144       return NULL;
00145    }
00146 
00147    for (cur = root_node; cur; cur = ast_xml_node_get_next(cur)) {
00148       /* Check if the name matchs */
00149       if (strcmp(ast_xml_node_get_name(cur), name)) {
00150          continue;
00151       }
00152       /* We need to check for a specific attribute name? */
00153       if (!attrname || !attrvalue) {
00154          return cur;
00155       }
00156       /* Get the attribute, we need to compare it. */
00157       if ((attr = ast_xml_get_attribute(cur, attrname))) {
00158          /* does attribute name/value matches? */
00159          if (!strcmp(attr, attrvalue)) {
00160             ast_xml_free_attr(attr);
00161             return cur;
00162          }
00163          ast_xml_free_attr(attr);
00164       }
00165    }
00166 
00167    return NULL;
00168 }
00169 
00170 const char *ast_xml_get_text(struct ast_xml_node *node)
00171 {
00172    if (!node) {
00173       return NULL;
00174    }
00175 
00176    return (const char *) xmlNodeGetContent((xmlNode *) node);
00177 }
00178 
00179 const char *ast_xml_node_get_name(struct ast_xml_node *node)
00180 {
00181    return (const char *) ((xmlNode *) node)->name;
00182 }
00183 
00184 struct ast_xml_node *ast_xml_node_get_children(struct ast_xml_node *node)
00185 {
00186    return (struct ast_xml_node *) ((xmlNode *) node)->children;
00187 }
00188 
00189 struct ast_xml_node *ast_xml_node_get_next(struct ast_xml_node *node)
00190 {
00191    return (struct ast_xml_node *) ((xmlNode *) node)->next;
00192 }
00193 
00194 struct ast_xml_node *ast_xml_node_get_prev(struct ast_xml_node *node)
00195 {
00196    return (struct ast_xml_node *) ((xmlNode *) node)->prev;
00197 }
00198 
00199 struct ast_xml_node *ast_xml_node_get_parent(struct ast_xml_node *node)
00200 {
00201    return (struct ast_xml_node *) ((xmlNode *) node)->parent;
00202 }
00203 
00204 #endif /* defined(HAVE_LIBXML2) */
00205