Fri Aug 17 00:17:50 2018

Asterisk developer's documentation


xml.c File Reference

XML abstraction layer. More...

#include "asterisk.h"
#include "asterisk/xml.h"
#include "asterisk/logger.h"
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xinclude.h>

Go to the source code of this file.

Functions

struct ast_xml_node * ast_xml_add_child (struct ast_xml_node *parent, struct ast_xml_node *child)
 Add a child node, to a specified parent node.
void ast_xml_close (struct ast_xml_doc *doc)
 Close an already open document and free the used structure.
int ast_xml_doc_dump_file (FILE *output, struct ast_xml_doc *doc)
 Dump the specified document to a file.
struct ast_xml_node * ast_xml_find_element (struct ast_xml_node *root_node, const char *name, const char *attrname, const char *attrvalue)
 Find a node element by name.
struct ast_xml_ns * ast_xml_find_namespace (struct ast_xml_doc *doc, struct ast_xml_node *node, const char *ns_name)
int ast_xml_finish (void)
 Cleanup library allocated global data.
void ast_xml_free_attr (const char *attribute)
 Free an attribute returned by ast_xml_get_attribute().
void ast_xml_free_node (struct ast_xml_node *node)
 Free node.
void ast_xml_free_text (const char *text)
 Free a content element that was returned by ast_xml_get_text().
const char * ast_xml_get_attribute (struct ast_xml_node *node, const char *attrname)
 Get a node attribute by name.
struct ast_xml_doc * ast_xml_get_doc (struct ast_xml_node *node)
 Get the document based on a node.
const char * ast_xml_get_ns_href (struct ast_xml_ns *ns)
struct ast_xml_node * ast_xml_get_root (struct ast_xml_doc *doc)
 Get the document root node.
const char * ast_xml_get_text (struct ast_xml_node *node)
 Get an element content string.
int ast_xml_init (void)
 Initialize the XML library implementation. This function is used to setup everything needed to start working with the xml implementation.
struct ast_xml_doc * ast_xml_new (void)
 Create a XML document.
struct ast_xml_node * ast_xml_new_child (struct ast_xml_node *parent, const char *child_name)
 Add a child node inside a passed parent node.
struct ast_xml_node * ast_xml_new_node (const char *name)
 Create a XML node.
struct ast_xml_node * ast_xml_node_get_children (struct ast_xml_node *node)
 Get the node's children.
const char * ast_xml_node_get_name (struct ast_xml_node *node)
 Get the name of a node.
struct ast_xml_node * ast_xml_node_get_next (struct ast_xml_node *node)
 Get the next node in the same level.
struct ast_xml_node * ast_xml_node_get_parent (struct ast_xml_node *node)
 Get the parent of a specified node.
struct ast_xml_node * ast_xml_node_get_prev (struct ast_xml_node *node)
 Get the previous node in the same leve.
struct ast_xml_doc * ast_xml_open (char *filename)
 Open an XML document.
struct ast_xml_doc * ast_xml_read_memory (char *buffer, size_t size)
 Open an XML document that resides in memory.
int ast_xml_set_attribute (struct ast_xml_node *node, const char *name, const char *value)
 Set an attribute to a node.
void ast_xml_set_root (struct ast_xml_doc *doc, struct ast_xml_node *node)
 Specify the root node of a XML document.
void ast_xml_set_text (struct ast_xml_node *node, const char *content)
 Set an element content string.

Detailed Description

XML abstraction layer.

Author:
Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>

Definition in file xml.c.


Function Documentation

struct ast_xml_node* ast_xml_add_child ( struct ast_xml_node *  parent,
struct ast_xml_node *  child 
) [read]

Add a child node, to a specified parent node.

Parameters:
parent Where to add the child node.
child The child node to add.
Return values:
NULL on error.
non-NULL The add child node on success.

Definition at line 107 of file xml.c.

Referenced by data_get_xml_add_child().

00108 {
00109    if (!parent || !child) {
00110       return NULL;
00111    }
00112    return (struct ast_xml_node *) xmlAddChild((xmlNode *) parent, (xmlNode *) child);
00113 }

void ast_xml_close ( struct ast_xml_doc *  doc  ) 

Close an already open document and free the used structure.

Return values:
doc The document reference.

Definition at line 134 of file xml.c.

Referenced by ast_data_get_xml().

00135 {
00136    if (!doc) {
00137       return;
00138    }
00139 
00140    xmlFreeDoc((xmlDoc *) doc);
00141    doc = NULL;
00142 }

int ast_xml_doc_dump_file ( FILE *  output,
struct ast_xml_doc *  doc 
)

Dump the specified document to a file.

Definition at line 289 of file xml.c.

00290 {
00291    return xmlDocDump(output, (xmlDocPtr)doc);
00292 }

struct ast_xml_node* ast_xml_find_element ( struct ast_xml_node *  root_node,
const char *  name,
const char *  attrname,
const char *  attrvalue 
) [read]

Find a node element by name.

Parameters:
root_node This is the node starting point.
name Node name to find.
attrname attribute name to match (if NULL it won't be matched).
attrvalue attribute value to match (if NULL it won't be matched).
Return values:
NULL if not found.
The node on success.

Definition at line 220 of file xml.c.

References ast_xml_free_attr(), ast_xml_get_attribute(), ast_xml_node_get_name(), and ast_xml_node_get_next().

Referenced by xmldoc_build_field(), and xmldoc_get_node().

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 }

struct ast_xml_ns* ast_xml_find_namespace ( struct ast_xml_doc *  doc,
struct ast_xml_node *  node,
const char *  ns_name 
) [read]

Definition at line 261 of file xml.c.

00261                                                                                                                    {
00262    xmlNsPtr ns = xmlSearchNs((xmlDocPtr) doc, (xmlNodePtr) node, (xmlChar *) ns_name);
00263    return (struct ast_xml_ns *) ns;
00264 }

int ast_xml_finish ( void   ) 

Cleanup library allocated global data.

Return values:
0 On success.
1 On error.

Definition at line 48 of file xml.c.

00049 {
00050    xmlCleanupParser();
00051 
00052    return 0;
00053 }

void ast_xml_free_attr ( const char *  attribute  ) 

Free an attribute returned by ast_xml_get_attribute().

Parameters:
attribute pointer to be freed.

Definition at line 176 of file xml.c.

Referenced by ast_xml_find_element(), ast_xmldoc_build_seealso(), xmldoc_attribute_match(), xmldoc_get_syntax_cmd(), xmldoc_get_syntax_fun(), xmldoc_get_syntax_manager(), xmldoc_parse_argument(), xmldoc_parse_enumlist(), xmldoc_parse_optionlist(), xmldoc_parse_parameter(), xmldoc_parse_variable(), and xmldoc_parse_variablelist().

00177 {
00178    if (attribute) {
00179       xmlFree((char *) attribute);
00180    }
00181 }

void ast_xml_free_node ( struct ast_xml_node *  node  ) 

Free node.

Parameters:
node Node to be released.

Definition at line 166 of file xml.c.

00167 {
00168    if (!node) {
00169       return;
00170    }
00171 
00172    xmlFreeNode((xmlNode *) node);
00173    node = NULL;
00174 }

void ast_xml_free_text ( const char *  text  ) 

Free a content element that was returned by ast_xml_get_text().

Parameters:
text text to be freed.

Definition at line 183 of file xml.c.

Referenced by ast_xmldoc_build_seealso(), xmldoc_get_formatted(), xmldoc_parse_para(), and xmldoc_parse_variable().

00184 {
00185    if (text) {
00186       xmlFree((char *) text);
00187    }
00188 }

const char* ast_xml_get_attribute ( struct ast_xml_node *  node,
const char *  attrname 
)

Get a node attribute by name.

Parameters:
node Node where to search the attribute.
attrname Attribute name.
Return values:
NULL on error
The attribute value on success.

Definition at line 190 of file xml.c.

Referenced by ast_xml_find_element(), ast_xmldoc_build_seealso(), xmldoc_attribute_match(), xmldoc_get_syntax_cmd(), xmldoc_get_syntax_fun(), xmldoc_get_syntax_manager(), xmldoc_parse_argument(), xmldoc_parse_enumlist(), xmldoc_parse_optionlist(), xmldoc_parse_parameter(), xmldoc_parse_variable(), and xmldoc_parse_variablelist().

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 }

struct ast_xml_doc* ast_xml_get_doc ( struct ast_xml_node *  node  )  [read]

Get the document based on a node.

Parameters:
node A node that is part of the dom.
Returns:
The dom pointer where this node resides.

Definition at line 252 of file xml.c.

00253 {
00254    if (!node) {
00255       return NULL;
00256    }
00257 
00258    return (struct ast_xml_doc *) ((xmlNode *)node)->doc;
00259 }

const char* ast_xml_get_ns_href ( struct ast_xml_ns *  ns  ) 

Definition at line 266 of file xml.c.

00267 {
00268    return (const char *) ((xmlNsPtr) ns)->href;
00269 }

struct ast_xml_node* ast_xml_get_root ( struct ast_xml_doc *  doc  )  [read]

Get the document root node.

Parameters:
doc Document reference
Return values:
NULL on error
The root node on success.

Definition at line 153 of file xml.c.

Referenced by xmldoc_get_node().

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 }

const char* ast_xml_get_text ( struct ast_xml_node *  node  ) 

Get an element content string.

Parameters:
node Node from where to get the string.
Return values:
NULL on error.
The text content of node.

Definition at line 271 of file xml.c.

Referenced by ast_xmldoc_build_seealso(), xmldoc_get_formatted(), xmldoc_parse_para(), and xmldoc_parse_variable().

00272 {
00273    if (!node) {
00274       return NULL;
00275    }
00276 
00277    return (const char *) xmlNodeGetContent((xmlNode *) node);
00278 }

int ast_xml_init ( void   ) 

Initialize the XML library implementation. This function is used to setup everything needed to start working with the xml implementation.

Return values:
0 On success.
1 On error.

Definition at line 41 of file xml.c.

00042 {
00043    LIBXML_TEST_VERSION
00044 
00045    return 0;
00046 }

struct ast_xml_doc* ast_xml_new ( void   )  [read]

Create a XML document.

Return values:
NULL on error.
non-NULL The allocated document structure.

Definition at line 75 of file xml.c.

Referenced by ast_data_get_xml().

00076 {
00077    xmlDoc *doc;
00078 
00079    doc = xmlNewDoc((const xmlChar *) "1.0");
00080    return (struct ast_xml_doc *) doc;
00081 }

struct ast_xml_node* ast_xml_new_child ( struct ast_xml_node *  parent,
const char *  child_name 
) [read]

Add a child node inside a passed parent node.

Parameters:
parent The pointer of the parent node.
child_name The name of the child node to add.
Return values:
NULL on error.
non-NULL The created child node pointer.

Definition at line 95 of file xml.c.

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 }

struct ast_xml_node* ast_xml_new_node ( const char *  name  )  [read]

Create a XML node.

Parameters:
name The name of the node to be created.
Return values:
NULL on error.
non-NULL The allocated node structe.

Definition at line 83 of file xml.c.

Referenced by ast_data_get_xml(), and data_get_xml_add_child().

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 }

struct ast_xml_node* ast_xml_node_get_children ( struct ast_xml_node *  node  )  [read]
const char* ast_xml_node_get_name ( struct ast_xml_node *  node  ) 
struct ast_xml_node* ast_xml_node_get_next ( struct ast_xml_node *  node  )  [read]
struct ast_xml_node* ast_xml_node_get_parent ( struct ast_xml_node *  node  )  [read]

Get the parent of a specified node.

Definition at line 314 of file xml.c.

00315 {
00316    return (struct ast_xml_node *) ((xmlNode *) node)->parent;
00317 }

struct ast_xml_node* ast_xml_node_get_prev ( struct ast_xml_node *  node  )  [read]

Get the previous node in the same leve.

Definition at line 309 of file xml.c.

00310 {
00311    return (struct ast_xml_node *) ((xmlNode *) node)->prev;
00312 }

struct ast_xml_doc* ast_xml_open ( char *  filename  )  [read]

Open an XML document.

Parameters:
filename Document path.
Return values:
NULL on error.
The ast_xml_doc reference to the open document.

Definition at line 55 of file xml.c.

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 }

struct ast_xml_doc* ast_xml_read_memory ( char *  buffer,
size_t  size 
) [read]

Open an XML document that resides in memory.

Parameters:
buffer The address where the document is stored
size The number of bytes in the document
Return values:
NULL on error.
The ast_xml_doc reference to the open document.

Definition at line 115 of file xml.c.

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 }

int ast_xml_set_attribute ( struct ast_xml_node *  node,
const char *  name,
const char *  value 
)

Set an attribute to a node.

Parameters:
node In which node we want to insert the attribute.
name The attribute name.
value The attribute value.
Return values:
0 on success.
-1 on error.

Definition at line 207 of file xml.c.

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 }

void ast_xml_set_root ( struct ast_xml_doc *  doc,
struct ast_xml_node *  node 
)

Specify the root node of a XML document.

Parameters:
doc The document pointer.
node A pointer to the node we want to set as root node.

Definition at line 144 of file xml.c.

Referenced by ast_data_get_xml().

00145 {
00146    if (!doc || !node) {
00147       return;
00148    }
00149 
00150    xmlDocSetRootElement((xmlDoc *) doc, (xmlNode *) node);
00151 }

void ast_xml_set_text ( struct ast_xml_node *  node,
const char *  content 
)

Set an element content string.

Parameters:
node Node from where to set the content string.
content The text to insert in the node.

Definition at line 280 of file xml.c.

Referenced by data_get_xml_add_child().

00281 {
00282    if (!node || !content) {
00283       return;
00284    }
00285 
00286    xmlNodeSetContent((xmlNode *) node, (const xmlChar *) content);
00287 }


Generated on 17 Aug 2018 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1