Thu Sep 7 01:03:05 2017

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 /*** MODULEINFO
00025    <support_level>core</support_level>
00026  ***/
00027 
00028 #include "asterisk.h"
00029 #include "asterisk/xml.h"
00030 #include "asterisk/logger.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369001 $")
00033 
00034 #if defined(HAVE_LIBXML2)
00035 #include <libxml/parser.h>
00036 #include <libxml/tree.h>
00037 #include <libxml/xinclude.h>
00038 /* libxml2 ast_xml implementation. */
00039 
00040 
00041 int ast_xml_init(void)
00042 {
00043    LIBXML_TEST_VERSION
00044 
00045    return 0;
00046 }
00047 
00048 int ast_xml_finish(void)
00049 {
00050    xmlCleanupParser();
00051 
00052    return 0;
00053 }
00054 
00055 struct ast_xml_doc *ast_xml_open(char *filename)
00056 {
00057    xmlDoc *doc;
00058 
00059    if (!filename) {
00060       return NULL;
00061    }
00062 
00063    doc = xmlReadFile(filename, NULL, XML_PARSE_RECOVER);
00064    if (doc) {
00065       /* process xinclude elements. */
00066       if (xmlXIncludeProcess(doc) < 0) {
00067          xmlFreeDoc(doc);
00068          return NULL;
00069       }
00070    }
00071 
00072    return (struct ast_xml_doc *) doc;
00073 }
00074 
00075 struct ast_xml_doc *ast_xml_new(void)
00076 {
00077    xmlDoc *doc;
00078 
00079    doc = xmlNewDoc((const xmlChar *) "1.0");
00080    return (struct ast_xml_doc *) doc;
00081 }
00082 
00083 struct ast_xml_node *ast_xml_new_node(const char *name)
00084 {
00085    xmlNode *node;
00086    if (!name) {
00087       return NULL;
00088    }
00089 
00090    node = xmlNewNode(NULL, (const xmlChar *) name);
00091 
00092    return (struct ast_xml_node *) node;
00093 }
00094 
00095 struct ast_xml_node *ast_xml_new_child(struct ast_xml_node *parent, const char *child_name)
00096 {
00097    xmlNode *child;
00098 
00099    if (!parent || !child_name) {
00100       return NULL;
00101    }
00102 
00103    child = xmlNewChild((xmlNode *) parent, NULL, (const xmlChar *) child_name, NULL);
00104    return (struct ast_xml_node *) child;
00105 }
00106 
00107 struct ast_xml_node *ast_xml_add_child(struct ast_xml_node *parent, struct ast_xml_node *child)
00108 {
00109    if (!parent || !child) {
00110       return NULL;
00111    }
00112    return (struct ast_xml_node *) xmlAddChild((xmlNode *) parent, (xmlNode *) child);
00113 }
00114 
00115 struct ast_xml_doc *ast_xml_read_memory(char *buffer, size_t size)
00116 {
00117    xmlDoc *doc;
00118 
00119    if (!buffer) {
00120       return NULL;
00121    }
00122 
00123    if (!(doc = xmlParseMemory(buffer, (int) size))) {
00124       /* process xinclude elements. */
00125       if (xmlXIncludeProcess(doc) < 0) {
00126          xmlFreeDoc(doc);
00127          return NULL;
00128       }
00129    }
00130 
00131    return (struct ast_xml_doc *) doc;
00132 }
00133 
00134 void ast_xml_close(struct ast_xml_doc *doc)
00135 {
00136    if (!doc) {
00137       return;
00138    }
00139 
00140    xmlFreeDoc((xmlDoc *) doc);
00141    doc = NULL;
00142 }
00143 
00144 void ast_xml_set_root(struct ast_xml_doc *doc, struct ast_xml_node *node)
00145 {
00146    if (!doc || !node) {
00147       return;
00148    }
00149 
00150    xmlDocSetRootElement((xmlDoc *) doc, (xmlNode *) node);
00151 }
00152 
00153 struct ast_xml_node *ast_xml_get_root(struct ast_xml_doc *doc)
00154 {
00155    xmlNode *root_node;
00156 
00157    if (!doc) {
00158       return NULL;
00159    }
00160 
00161    root_node = xmlDocGetRootElement((xmlDoc *) doc);
00162 
00163    return (struct ast_xml_node *) root_node;
00164 }
00165 
00166 void ast_xml_free_node(struct ast_xml_node *node)
00167 {
00168    if (!node) {
00169       return;
00170    }
00171 
00172    xmlFreeNode((xmlNode *) node);
00173    node = NULL;
00174 }
00175 
00176 void ast_xml_free_attr(const char *attribute)
00177 {
00178    if (attribute) {
00179       xmlFree((char *) attribute);
00180    }
00181 }
00182 
00183 void ast_xml_free_text(const char *text)
00184 {
00185    if (text) {
00186       xmlFree((char *) text);
00187    }
00188 }
00189 
00190 const char *ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname)
00191 {
00192    xmlChar *attrvalue;
00193 
00194    if (!node) {
00195       return NULL;
00196    }
00197 
00198    if (!attrname) {
00199       return NULL;
00200    }
00201 
00202    attrvalue = xmlGetProp((xmlNode *) node, (xmlChar *) attrname);
00203 
00204    return (const char *) attrvalue;
00205 }
00206 
00207 int ast_xml_set_attribute(struct ast_xml_node *node, const char *name, const char *value)
00208 {
00209    if (!name || !value) {
00210       return -1;
00211    }
00212 
00213    if (!xmlSetProp((xmlNode *) node, (xmlChar *) name, (xmlChar *) value)) {
00214       return -1;
00215    }
00216 
00217    return 0;
00218 }
00219 
00220 struct ast_xml_node *ast_xml_find_element(struct ast_xml_node *root_node, const char *name, const char *attrname, const char *attrvalue)
00221 {
00222    struct ast_xml_node *cur;
00223    const char *attr;
00224 
00225    if (!root_node) {
00226       return NULL;
00227    }
00228 
00229    for (cur = root_node; cur; cur = ast_xml_node_get_next(cur)) {
00230       /* Check if the name matchs */
00231       if (strcmp(ast_xml_node_get_name(cur), name)) {
00232          continue;
00233       }
00234       /* We need to check for a specific attribute name? */
00235       if (!attrname || !attrvalue) {
00236          return cur;
00237       }
00238       /* Get the attribute, we need to compare it. */
00239       if ((attr = ast_xml_get_attribute(cur, attrname))) {
00240          /* does attribute name/value matches? */
00241          if (!strcmp(attr, attrvalue)) {
00242             ast_xml_free_attr(attr);
00243             return cur;
00244          }
00245          ast_xml_free_attr(attr);
00246       }
00247    }
00248 
00249    return NULL;
00250 }
00251 
00252 struct ast_xml_doc *ast_xml_get_doc(struct ast_xml_node *node)
00253 {
00254    if (!node) {
00255       return NULL;
00256    }
00257 
00258    return (struct ast_xml_doc *) ((xmlNode *)node)->doc;
00259 }
00260 
00261 struct ast_xml_ns *ast_xml_find_namespace(struct ast_xml_doc *doc, struct ast_xml_node *node, const char *ns_name) {
00262    xmlNsPtr ns = xmlSearchNs((xmlDocPtr) doc, (xmlNodePtr) node, (xmlChar *) ns_name);
00263    return (struct ast_xml_ns *) ns;
00264 }
00265 
00266 const char *ast_xml_get_ns_href(struct ast_xml_ns *ns)
00267 {
00268    return (const char *) ((xmlNsPtr) ns)->href;
00269 }
00270 
00271 const char *ast_xml_get_text(struct ast_xml_node *node)
00272 {
00273    if (!node) {
00274       return NULL;
00275    }
00276 
00277    return (const char *) xmlNodeGetContent((xmlNode *) node);
00278 }
00279 
00280 void ast_xml_set_text(struct ast_xml_node *node, const char *content)
00281 {
00282    if (!node || !content) {
00283       return;
00284    }
00285 
00286    xmlNodeSetContent((xmlNode *) node, (const xmlChar *) content);
00287 }
00288 
00289 int ast_xml_doc_dump_file(FILE *output, struct ast_xml_doc *doc)
00290 {
00291    return xmlDocDump(output, (xmlDocPtr)doc);
00292 }
00293 
00294 const char *ast_xml_node_get_name(struct ast_xml_node *node)
00295 {
00296    return (const char *) ((xmlNode *) node)->name;
00297 }
00298 
00299 struct ast_xml_node *ast_xml_node_get_children(struct ast_xml_node *node)
00300 {
00301    return (struct ast_xml_node *) ((xmlNode *) node)->children;
00302 }
00303 
00304 struct ast_xml_node *ast_xml_node_get_next(struct ast_xml_node *node)
00305 {
00306    return (struct ast_xml_node *) ((xmlNode *) node)->next;
00307 }
00308 
00309 struct ast_xml_node *ast_xml_node_get_prev(struct ast_xml_node *node)
00310 {
00311    return (struct ast_xml_node *) ((xmlNode *) node)->prev;
00312 }
00313 
00314 struct ast_xml_node *ast_xml_node_get_parent(struct ast_xml_node *node)
00315 {
00316    return (struct ast_xml_node *) ((xmlNode *) node)->parent;
00317 }
00318 
00319 #endif /* defined(HAVE_LIBXML2) */
00320 

Generated on 7 Sep 2017 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1