Wed Jan 8 2020 09:49:51

Asterisk developer's documentation


xml.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2008, Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16 
17 /*! \file
18  *
19  * \brief XML abstraction layer
20  *
21  * \author Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
22  */
23 
24 /*** MODULEINFO
25  <support_level>core</support_level>
26  ***/
27 
28 #include "asterisk.h"
29 #include "asterisk/xml.h"
30 #include "asterisk/logger.h"
31 
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369001 $")
33 
34 #if defined(HAVE_LIBXML2)
35 #include <libxml/parser.h>
36 #include <libxml/tree.h>
37 #include <libxml/xinclude.h>
38 /* libxml2 ast_xml implementation. */
39 
40 
41 int ast_xml_init(void)
42 {
43  LIBXML_TEST_VERSION
44 
45  return 0;
46 }
47 
48 int ast_xml_finish(void)
49 {
50  xmlCleanupParser();
51 
52  return 0;
53 }
54 
55 struct ast_xml_doc *ast_xml_open(char *filename)
56 {
57  xmlDoc *doc;
58 
59  if (!filename) {
60  return NULL;
61  }
62 
63  doc = xmlReadFile(filename, NULL, XML_PARSE_RECOVER);
64  if (doc) {
65  /* process xinclude elements. */
66  if (xmlXIncludeProcess(doc) < 0) {
67  xmlFreeDoc(doc);
68  return NULL;
69  }
70  }
71 
72  return (struct ast_xml_doc *) doc;
73 }
74 
75 struct ast_xml_doc *ast_xml_new(void)
76 {
77  xmlDoc *doc;
78 
79  doc = xmlNewDoc((const xmlChar *) "1.0");
80  return (struct ast_xml_doc *) doc;
81 }
82 
83 struct ast_xml_node *ast_xml_new_node(const char *name)
84 {
85  xmlNode *node;
86  if (!name) {
87  return NULL;
88  }
89 
90  node = xmlNewNode(NULL, (const xmlChar *) name);
91 
92  return (struct ast_xml_node *) node;
93 }
94 
95 struct ast_xml_node *ast_xml_new_child(struct ast_xml_node *parent, const char *child_name)
96 {
97  xmlNode *child;
98 
99  if (!parent || !child_name) {
100  return NULL;
101  }
102 
103  child = xmlNewChild((xmlNode *) parent, NULL, (const xmlChar *) child_name, NULL);
104  return (struct ast_xml_node *) child;
105 }
106 
107 struct ast_xml_node *ast_xml_add_child(struct ast_xml_node *parent, struct ast_xml_node *child)
108 {
109  if (!parent || !child) {
110  return NULL;
111  }
112  return (struct ast_xml_node *) xmlAddChild((xmlNode *) parent, (xmlNode *) child);
113 }
114 
115 struct ast_xml_doc *ast_xml_read_memory(char *buffer, size_t size)
116 {
117  xmlDoc *doc;
118 
119  if (!buffer) {
120  return NULL;
121  }
122 
123  if (!(doc = xmlParseMemory(buffer, (int) size))) {
124  /* process xinclude elements. */
125  if (xmlXIncludeProcess(doc) < 0) {
126  xmlFreeDoc(doc);
127  return NULL;
128  }
129  }
130 
131  return (struct ast_xml_doc *) doc;
132 }
133 
134 void ast_xml_close(struct ast_xml_doc *doc)
135 {
136  if (!doc) {
137  return;
138  }
139 
140  xmlFreeDoc((xmlDoc *) doc);
141  doc = NULL;
142 }
143 
144 void ast_xml_set_root(struct ast_xml_doc *doc, struct ast_xml_node *node)
145 {
146  if (!doc || !node) {
147  return;
148  }
149 
150  xmlDocSetRootElement((xmlDoc *) doc, (xmlNode *) node);
151 }
152 
153 struct ast_xml_node *ast_xml_get_root(struct ast_xml_doc *doc)
154 {
155  xmlNode *root_node;
156 
157  if (!doc) {
158  return NULL;
159  }
160 
161  root_node = xmlDocGetRootElement((xmlDoc *) doc);
162 
163  return (struct ast_xml_node *) root_node;
164 }
165 
166 void ast_xml_free_node(struct ast_xml_node *node)
167 {
168  if (!node) {
169  return;
170  }
171 
172  xmlFreeNode((xmlNode *) node);
173  node = NULL;
174 }
175 
176 void ast_xml_free_attr(const char *attribute)
177 {
178  if (attribute) {
179  xmlFree((char *) attribute);
180  }
181 }
182 
183 void ast_xml_free_text(const char *text)
184 {
185  if (text) {
186  xmlFree((char *) text);
187  }
188 }
189 
190 const char *ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname)
191 {
192  xmlChar *attrvalue;
193 
194  if (!node) {
195  return NULL;
196  }
197 
198  if (!attrname) {
199  return NULL;
200  }
201 
202  attrvalue = xmlGetProp((xmlNode *) node, (xmlChar *) attrname);
203 
204  return (const char *) attrvalue;
205 }
206 
207 int ast_xml_set_attribute(struct ast_xml_node *node, const char *name, const char *value)
208 {
209  if (!name || !value) {
210  return -1;
211  }
212 
213  if (!xmlSetProp((xmlNode *) node, (xmlChar *) name, (xmlChar *) value)) {
214  return -1;
215  }
216 
217  return 0;
218 }
219 
220 struct ast_xml_node *ast_xml_find_element(struct ast_xml_node *root_node, const char *name, const char *attrname, const char *attrvalue)
221 {
222  struct ast_xml_node *cur;
223  const char *attr;
224 
225  if (!root_node) {
226  return NULL;
227  }
228 
229  for (cur = root_node; cur; cur = ast_xml_node_get_next(cur)) {
230  /* Check if the name matchs */
231  if (strcmp(ast_xml_node_get_name(cur), name)) {
232  continue;
233  }
234  /* We need to check for a specific attribute name? */
235  if (!attrname || !attrvalue) {
236  return cur;
237  }
238  /* Get the attribute, we need to compare it. */
239  if ((attr = ast_xml_get_attribute(cur, attrname))) {
240  /* does attribute name/value matches? */
241  if (!strcmp(attr, attrvalue)) {
242  ast_xml_free_attr(attr);
243  return cur;
244  }
245  ast_xml_free_attr(attr);
246  }
247  }
248 
249  return NULL;
250 }
251 
252 struct ast_xml_doc *ast_xml_get_doc(struct ast_xml_node *node)
253 {
254  if (!node) {
255  return NULL;
256  }
257 
258  return (struct ast_xml_doc *) ((xmlNode *)node)->doc;
259 }
260 
261 struct ast_xml_ns *ast_xml_find_namespace(struct ast_xml_doc *doc, struct ast_xml_node *node, const char *ns_name) {
262  xmlNsPtr ns = xmlSearchNs((xmlDocPtr) doc, (xmlNodePtr) node, (xmlChar *) ns_name);
263  return (struct ast_xml_ns *) ns;
264 }
265 
266 const char *ast_xml_get_ns_href(struct ast_xml_ns *ns)
267 {
268  return (const char *) ((xmlNsPtr) ns)->href;
269 }
270 
271 const char *ast_xml_get_text(struct ast_xml_node *node)
272 {
273  if (!node) {
274  return NULL;
275  }
276 
277  return (const char *) xmlNodeGetContent((xmlNode *) node);
278 }
279 
280 void ast_xml_set_text(struct ast_xml_node *node, const char *content)
281 {
282  if (!node || !content) {
283  return;
284  }
285 
286  xmlNodeSetContent((xmlNode *) node, (const xmlChar *) content);
287 }
288 
289 int ast_xml_doc_dump_file(FILE *output, struct ast_xml_doc *doc)
290 {
291  return xmlDocDump(output, (xmlDocPtr)doc);
292 }
293 
294 const char *ast_xml_node_get_name(struct ast_xml_node *node)
295 {
296  return (const char *) ((xmlNode *) node)->name;
297 }
298 
299 struct ast_xml_node *ast_xml_node_get_children(struct ast_xml_node *node)
300 {
301  return (struct ast_xml_node *) ((xmlNode *) node)->children;
302 }
303 
304 struct ast_xml_node *ast_xml_node_get_next(struct ast_xml_node *node)
305 {
306  return (struct ast_xml_node *) ((xmlNode *) node)->next;
307 }
308 
309 struct ast_xml_node *ast_xml_node_get_prev(struct ast_xml_node *node)
310 {
311  return (struct ast_xml_node *) ((xmlNode *) node)->prev;
312 }
313 
314 struct ast_xml_node *ast_xml_node_get_parent(struct ast_xml_node *node)
315 {
316  return (struct ast_xml_node *) ((xmlNode *) node)->parent;
317 }
318 
319 #endif /* defined(HAVE_LIBXML2) */
320 
Asterisk main include file. File version handling, generic pbx functions.
int ast_xml_init(void)
Initialize the XML library implementation. This function is used to setup everything needed to start ...
Definition: xml.c:41
const char * ast_xml_get_ns_href(struct ast_xml_ns *ns)
Definition: xml.c:266
struct ast_xml_doc * ast_xml_open(char *filename)
Open an XML document.
Definition: xml.c:55
struct ast_xml_doc * ast_xml_new(void)
Create a XML document.
Definition: xml.c:75
void ast_xml_free_node(struct ast_xml_node *node)
Free node.
Definition: xml.c:166
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.
Definition: xml.c:107
int ast_xml_doc_dump_file(FILE *output, struct ast_xml_doc *doc)
Dump the specified document to a file.
Definition: xml.c:289
char * text
Definition: app_queue.c:1091
struct ast_xml_node * ast_xml_get_root(struct ast_xml_doc *doc)
Get the document root node.
Definition: xml.c:153
int value
Definition: syslog.c:39
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.
Definition: xml.c:95
struct ast_xml_doc * ast_xml_get_doc(struct ast_xml_node *node)
Get the document based on a node.
Definition: xml.c:252
const char * ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname)
Get a node attribute by name.
Definition: xml.c:190
void ast_xml_free_attr(const char *attribute)
Free an attribute returned by ast_xml_get_attribute()
Definition: xml.c:176
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.
Definition: xml.c:220
struct ast_xml_node * ast_xml_new_node(const char *name)
Create a XML node.
Definition: xml.c:83
Asterisk XML abstraction layer.
void ast_xml_set_root(struct ast_xml_doc *doc, struct ast_xml_node *node)
Specify the root node of a XML document.
Definition: xml.c:144
void ast_xml_set_text(struct ast_xml_node *node, const char *content)
Set an element content string.
Definition: xml.c:280
struct ast_xml_node * ast_xml_node_get_parent(struct ast_xml_node *node)
Get the parent of a specified node.
Definition: xml.c:314
static const char name[]
void ast_xml_close(struct ast_xml_doc *doc)
Close an already open document and free the used structure.
Definition: xml.c:134
Support for logging to various files, console and syslog Configuration in file logger.conf.
int ast_xml_set_attribute(struct ast_xml_node *node, const char *name, const char *value)
Set an attribute to a node.
Definition: xml.c:207
const char * ast_xml_get_text(struct ast_xml_node *node)
Get an element content string.
Definition: xml.c:271
struct ast_xml_node * ast_xml_node_get_prev(struct ast_xml_node *node)
Get the previous node in the same leve.
Definition: xml.c:309
struct ast_xml_doc * ast_xml_read_memory(char *buffer, size_t size)
Open an XML document that resides in memory.
Definition: xml.c:115
struct ast_xml_ns * ast_xml_find_namespace(struct ast_xml_doc *doc, struct ast_xml_node *node, const char *ns_name)
Definition: xml.c:261
struct ast_xml_node * ast_xml_node_get_next(struct ast_xml_node *node)
Get the next node in the same level.
Definition: xml.c:304
int ast_xml_finish(void)
Cleanup library allocated global data.
Definition: xml.c:48
struct ast_xml_node * ast_xml_node_get_children(struct ast_xml_node *node)
Get the node&#39;s children.
Definition: xml.c:299
const char * ast_xml_node_get_name(struct ast_xml_node *node)
Get the name of a node.
Definition: xml.c:294
void ast_xml_free_text(const char *text)
Free a content element that was returned by ast_xml_get_text()
Definition: xml.c:183
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180