Asterisk - The Open Source Telephony Project  18.5.0
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) <[email protected]>
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) <[email protected]>
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 #include "asterisk/utils.h"
32 #include "asterisk/autoconfig.h"
33 
34 #if defined(HAVE_LIBXML2)
35 #include <libxml/parser.h>
36 #include <libxml/tree.h>
37 #include <libxml/xinclude.h>
38 #include <libxml/xpath.h>
39 /* libxml2 ast_xml implementation. */
40 #ifdef HAVE_LIBXSLT
41  #include <libxslt/xsltInternals.h>
42  #include <libxslt/transform.h>
43 #endif /* HAVE_LIBXSLT */
44 
45 
46 int ast_xml_init(void)
47 {
48  LIBXML_TEST_VERSION
49 
50  return 0;
51 }
52 
53 int ast_xml_finish(void)
54 {
55  xmlCleanupParser();
56 #ifdef HAVE_LIBXSLT_CLEANUP
57  xsltCleanupGlobals();
58 #endif
59 
60  return 0;
61 }
62 
63 struct ast_xml_doc *ast_xml_open(char *filename)
64 {
65  xmlDoc *doc;
66 
67  if (!filename) {
68  return NULL;
69  }
70 
71  doc = xmlReadFile(filename, NULL, XML_PARSE_RECOVER);
72  if (!doc) {
73  return NULL;
74  }
75 
76  /* process xinclude elements. */
77  if (xmlXIncludeProcess(doc) < 0) {
78  xmlFreeDoc(doc);
79  return NULL;
80  }
81 
82 #ifdef HAVE_LIBXSLT
83  {
84  xsltStylesheetPtr xslt = xsltLoadStylesheetPI(doc);
85  if (xslt) {
86  xmlDocPtr tmpdoc = xsltApplyStylesheet(xslt, doc, NULL);
87  xsltFreeStylesheet(xslt);
88  xmlFreeDoc(doc);
89  if (!tmpdoc) {
90  return NULL;
91  }
92  doc = tmpdoc;
93  }
94  }
95 #else /* no HAVE_LIBXSLT */
96  ast_log(LOG_NOTICE, "XSLT support not found. XML documentation may be incomplete.\n");
97 #endif /* HAVE_LIBXSLT */
98 
99  /* Optimize for XPath */
100  xmlXPathOrderDocElems(doc);
101 
102  return (struct ast_xml_doc *) doc;
103 }
104 
105 struct ast_xml_doc *ast_xml_new(void)
106 {
107  xmlDoc *doc;
108 
109  doc = xmlNewDoc((const xmlChar *) "1.0");
110  return (struct ast_xml_doc *) doc;
111 }
112 
113 struct ast_xml_node *ast_xml_new_node(const char *name)
114 {
115  xmlNode *node;
116  if (!name) {
117  return NULL;
118  }
119 
120  node = xmlNewNode(NULL, (const xmlChar *) name);
121 
122  return (struct ast_xml_node *) node;
123 }
124 
125 struct ast_xml_node *ast_xml_new_child(struct ast_xml_node *parent, const char *child_name)
126 {
127  xmlNode *child;
128 
129  if (!parent || !child_name) {
130  return NULL;
131  }
132 
133  child = xmlNewChild((xmlNode *) parent, NULL, (const xmlChar *) child_name, NULL);
134  return (struct ast_xml_node *) child;
135 }
136 
137 struct ast_xml_node *ast_xml_add_child(struct ast_xml_node *parent, struct ast_xml_node *child)
138 {
139  if (!parent || !child) {
140  return NULL;
141  }
142  return (struct ast_xml_node *) xmlAddChild((xmlNode *) parent, (xmlNode *) child);
143 }
144 
145 struct ast_xml_node *ast_xml_add_child_list(struct ast_xml_node *parent, struct ast_xml_node *child)
146 {
147  if (!parent || !child) {
148  return NULL;
149  }
150  return (struct ast_xml_node *) xmlAddChildList((xmlNode *) parent, (xmlNode *) child);
151 }
152 
153 struct ast_xml_node *ast_xml_copy_node_list(struct ast_xml_node *list)
154 {
155  if (!list) {
156  return NULL;
157  }
158  return (struct ast_xml_node *) xmlCopyNodeList((xmlNode *) list);
159 }
160 
161 struct ast_xml_doc *ast_xml_read_memory(char *buffer, size_t size)
162 {
163  xmlDoc *doc;
164 
165  if (!buffer) {
166  return NULL;
167  }
168 
169  if (!(doc = xmlParseMemory(buffer, (int) size))) {
170  /* process xinclude elements. */
171  if (xmlXIncludeProcess(doc) < 0) {
172  xmlFreeDoc(doc);
173  return NULL;
174  }
175  }
176 
177  return (struct ast_xml_doc *) doc;
178 }
179 
180 void ast_xml_close(struct ast_xml_doc *doc)
181 {
182  if (!doc) {
183  return;
184  }
185 
186  xmlFreeDoc((xmlDoc *) doc);
187  doc = NULL;
188 }
189 
190 void ast_xml_set_root(struct ast_xml_doc *doc, struct ast_xml_node *node)
191 {
192  if (!doc || !node) {
193  return;
194  }
195 
196  xmlDocSetRootElement((xmlDoc *) doc, (xmlNode *) node);
197 }
198 
199 struct ast_xml_node *ast_xml_get_root(struct ast_xml_doc *doc)
200 {
201  xmlNode *root_node;
202 
203  if (!doc) {
204  return NULL;
205  }
206 
207  root_node = xmlDocGetRootElement((xmlDoc *) doc);
208 
209  return (struct ast_xml_node *) root_node;
210 }
211 
212 void ast_xml_free_node(struct ast_xml_node *node)
213 {
214  if (!node) {
215  return;
216  }
217 
218  xmlFreeNode((xmlNode *) node);
219  node = NULL;
220 }
221 
222 void ast_xml_free_attr(const char *attribute)
223 {
224  if (attribute) {
225  xmlFree((char *) attribute);
226  }
227 }
228 
229 void ast_xml_free_text(const char *text)
230 {
231  if (text) {
232  xmlFree((char *) text);
233  }
234 }
235 
236 const char *ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname)
237 {
238  xmlChar *attrvalue;
239 
240  if (!node) {
241  return NULL;
242  }
243 
244  if (!attrname) {
245  return NULL;
246  }
247 
248  attrvalue = xmlGetProp((xmlNode *) node, (xmlChar *) attrname);
249 
250  return (const char *) attrvalue;
251 }
252 
253 int ast_xml_set_attribute(struct ast_xml_node *node, const char *name, const char *value)
254 {
255  if (!name || !value) {
256  return -1;
257  }
258 
259  if (!xmlSetProp((xmlNode *) node, (xmlChar *) name, (xmlChar *) value)) {
260  return -1;
261  }
262 
263  return 0;
264 }
265 
266 struct ast_xml_node *ast_xml_find_element(struct ast_xml_node *root_node, const char *name, const char *attrname, const char *attrvalue)
267 {
268  struct ast_xml_node *cur;
269  const char *attr;
270 
271  if (!root_node) {
272  return NULL;
273  }
274 
275  for (cur = root_node; cur; cur = ast_xml_node_get_next(cur)) {
276  /* Check if the name matchs */
277  if (strcmp(ast_xml_node_get_name(cur), name)) {
278  continue;
279  }
280  /* We need to check for a specific attribute name? */
281  if (!attrname || !attrvalue) {
282  return cur;
283  }
284  /* Get the attribute, we need to compare it. */
285  if ((attr = ast_xml_get_attribute(cur, attrname))) {
286  /* does attribute name/value matches? */
287  if (!strcmp(attr, attrvalue)) {
288  ast_xml_free_attr(attr);
289  return cur;
290  }
291  ast_xml_free_attr(attr);
292  }
293  }
294 
295  return NULL;
296 }
297 
298 struct ast_xml_doc *ast_xml_get_doc(struct ast_xml_node *node)
299 {
300  if (!node) {
301  return NULL;
302  }
303 
304  return (struct ast_xml_doc *) ((xmlNode *)node)->doc;
305 }
306 
307 struct ast_xml_ns *ast_xml_find_namespace(struct ast_xml_doc *doc, struct ast_xml_node *node, const char *ns_name) {
308  xmlNsPtr ns = xmlSearchNs((xmlDocPtr) doc, (xmlNodePtr) node, (xmlChar *) ns_name);
309  return (struct ast_xml_ns *) ns;
310 }
311 
312 const char *ast_xml_get_ns_href(struct ast_xml_ns *ns)
313 {
314  return (const char *) ((xmlNsPtr) ns)->href;
315 }
316 
317 const char *ast_xml_get_text(struct ast_xml_node *node)
318 {
319  if (!node) {
320  return NULL;
321  }
322 
323  return (const char *) xmlNodeGetContent((xmlNode *) node);
324 }
325 
326 void ast_xml_set_text(struct ast_xml_node *node, const char *content)
327 {
328  if (!node || !content) {
329  return;
330  }
331 
332  xmlNodeSetContent((xmlNode *) node, (const xmlChar *) content);
333 }
334 
335 int ast_xml_doc_dump_file(FILE *output, struct ast_xml_doc *doc)
336 {
337  return xmlDocDump(output, (xmlDocPtr)doc);
338 }
339 
340 const char *ast_xml_node_get_name(struct ast_xml_node *node)
341 {
342  return (const char *) ((xmlNode *) node)->name;
343 }
344 
345 struct ast_xml_node *ast_xml_node_get_children(struct ast_xml_node *node)
346 {
347  return (struct ast_xml_node *) ((xmlNode *) node)->children;
348 }
349 
350 struct ast_xml_node *ast_xml_node_get_next(struct ast_xml_node *node)
351 {
352  return (struct ast_xml_node *) ((xmlNode *) node)->next;
353 }
354 
355 struct ast_xml_node *ast_xml_node_get_prev(struct ast_xml_node *node)
356 {
357  return (struct ast_xml_node *) ((xmlNode *) node)->prev;
358 }
359 
360 struct ast_xml_node *ast_xml_node_get_parent(struct ast_xml_node *node)
361 {
362  return (struct ast_xml_node *) ((xmlNode *) node)->parent;
363 }
364 
365 struct ast_xml_node *ast_xml_xpath_get_first_result(struct ast_xml_xpath_results *results)
366 {
367  return (struct ast_xml_node *) ((xmlXPathObjectPtr) results)->nodesetval->nodeTab[0];
368 }
369 
370 void ast_xml_xpath_results_free(struct ast_xml_xpath_results *results)
371 {
372  xmlXPathFreeObject((xmlXPathObjectPtr) results);
373 }
374 
375 int ast_xml_xpath_num_results(struct ast_xml_xpath_results *results)
376 {
377  return ((xmlXPathObjectPtr) results)->nodesetval->nodeNr;
378 }
379 
380 struct ast_xml_xpath_results *ast_xml_query(struct ast_xml_doc *doc, const char *xpath_str)
381 {
382  xmlXPathContextPtr context;
383  xmlXPathObjectPtr result;
384  if (!(context = xmlXPathNewContext((xmlDoc *) doc))) {
385  ast_log(LOG_ERROR, "Could not create XPath context!\n");
386  return NULL;
387  }
388  result = xmlXPathEvalExpression((xmlChar *) xpath_str, context);
389  xmlXPathFreeContext(context);
390  if (!result) {
391  ast_log(LOG_WARNING, "Error for query: %s\n", xpath_str);
392  return NULL;
393  }
394  if (xmlXPathNodeSetIsEmpty(result->nodesetval)) {
395  xmlXPathFreeObject(result);
396  ast_debug(5, "No results for query: %s\n", xpath_str);
397  return NULL;
398  }
399  return (struct ast_xml_xpath_results *) result;
400 }
401 
402 #endif /* defined(HAVE_LIBXML2) */
Definition: test_heap.c:38
const char * ast_xml_get_text(struct ast_xml_node *node)
Get an element content string.
Definition: xml.c:317
Asterisk main include file. File version handling, generic pbx functions.
void ast_xml_free_text(const char *text)
Free a content element that was returned by ast_xml_get_text()
Definition: xml.c:229
struct ast_xml_doc * ast_xml_new(void)
Create a XML document.
Definition: xml.c:105
struct ast_xml_xpath_results * ast_xml_query(struct ast_xml_doc *doc, const char *xpath_str)
Execute an XPath query on an XML document.
Definition: xml.c:380
#define LOG_WARNING
Definition: logger.h:274
void ast_xml_xpath_results_free(struct ast_xml_xpath_results *results)
Free the XPath results.
Definition: xml.c:370
void ast_xml_set_text(struct ast_xml_node *node, const char *content)
Set an element content string.
Definition: xml.c:326
int ast_xml_init(void)
Initialize the XML library implementation. This function is used to setup everything needed to start ...
Definition: xml.c:46
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:125
int ast_xml_xpath_num_results(struct ast_xml_xpath_results *results)
Return the number of results from an XPath query.
Definition: xml.c:375
char * text
Definition: app_queue.c:1508
struct ast_xml_node * ast_xml_copy_node_list(struct ast_xml_node *list)
Create a copy of a n ode list.
Definition: xml.c:153
const char * ast_xml_get_ns_href(struct ast_xml_ns *ns)
Definition: xml.c:312
#define NULL
Definition: resample.c:96
void ast_xml_close(struct ast_xml_doc *doc)
Close an already open document and free the used structure.
Definition: xml.c:180
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:307
int value
Definition: syslog.c:37
void ast_xml_free_attr(const char *attribute)
Free an attribute returned by ast_xml_get_attribute()
Definition: xml.c:222
Utility functions.
int ast_xml_doc_dump_file(FILE *output, struct ast_xml_doc *doc)
Dump the specified document to a file.
Definition: xml.c:335
const char * ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname)
Get a node attribute by name.
Definition: xml.c:236
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
const char * ast_xml_node_get_name(struct ast_xml_node *node)
Get the name of a node.
Definition: xml.c:340
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:137
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:350
struct ast_xml_node * ast_xml_node_get_children(struct ast_xml_node *node)
Get the node&#39;s children.
Definition: xml.c:345
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:190
#define LOG_ERROR
Definition: logger.h:285
struct ast_xml_doc * ast_xml_get_doc(struct ast_xml_node *node)
Get the document based on a node.
Definition: xml.c:298
#define LOG_NOTICE
Definition: logger.h:263
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:355
struct ast_xml_node * ast_xml_xpath_get_first_result(struct ast_xml_xpath_results *results)
Return the first result node of an XPath query.
Definition: xml.c:365
static const char name[]
Definition: cdr_mysql.c:74
Support for logging to various files, console and syslog Configuration in file logger.conf.
struct ast_xml_node * ast_xml_new_node(const char *name)
Create a XML node.
Definition: xml.c:113
struct ast_xml_doc * ast_xml_read_memory(char *buffer, size_t size)
Open an XML document that resides in memory.
Definition: xml.c:161
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:253
static PGresult * result
Definition: cel_pgsql.c:88
struct ast_xml_node * ast_xml_node_get_parent(struct ast_xml_node *node)
Get the parent of a specified node.
Definition: xml.c:360
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116
struct ast_xml_node * ast_xml_add_child_list(struct ast_xml_node *parent, struct ast_xml_node *child)
Add a list of child nodes, to a specified parent node.
Definition: xml.c:145
struct ast_xml_node * ast_xml_get_root(struct ast_xml_doc *doc)
Get the document root node.
Definition: xml.c:199
struct ast_xml_doc * ast_xml_open(char *filename)
Open an XML document.
Definition: xml.c:63
void ast_xml_free_node(struct ast_xml_node *node)
Free node.
Definition: xml.c:212
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:266
int ast_xml_finish(void)
Cleanup library allocated global data.
Definition: xml.c:53