Sat Mar 10 01:54:28 2012

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

Generated on Sat Mar 10 01:54:28 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7