Debian Codegraph @

main/xml.c

raw symbols SCIP
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 #define ASTMM_LIBC ASTMM_IGNORE
29 #include "asterisk.h"
30 #include "asterisk/xml.h"
31 #include "asterisk/logger.h"
32 #include "asterisk/utils.h"
33 #include "asterisk/autoconfig.h"
34
35 #if defined(HAVE_LIBXML2)
36 #include <libxml/parser.h>
37 #include <libxml/tree.h>
38 #include <libxml/xinclude.h>
39 #include <libxml/xpath.h>
40 #include <libxml/xpathInternals.h>
41 /* libxml2 ast_xml implementation. */
42 #ifdef HAVE_LIBXSLT
43 #include <libxslt/xsltInternals.h>
44 #include <libxslt/transform.h>
45 #include <libxslt/xsltutils.h>
46 #endif /* HAVE_LIBXSLT */
47
48
49 int ast_xml_init(void)
50 {
51 LIBXML_TEST_VERSION
52 #ifdef HAVE_LIBXSLT
53 xsltInit();
54 #endif
55 return 0;
56 }
57
58 int ast_xml_finish(void)
59 {
60 xmlCleanupParser();
61 #ifdef HAVE_LIBXSLT
62 #ifdef HAVE_LIBXSLT_CLEANUP
63 xsltCleanupGlobals();
64 #else
65 xsltUninit();
66 #endif
67 #endif
68
69 return 0;
70 }
71
72 /*!
73 * \internal
74 * \brief Process XML Inclusions (XInclude).
75 *
76 * XIncludes can result in new includes being inserted, so we need to reprocess
77 * until no changes are made or we encounter an error.
78 *
79 * \param doc the document to process
80 *
81 * \retval 0 if XInclude processing concluded successfully
82 * \retval -1 if an error occurred during XInclude processing
83 */
84 static int process_xincludes(xmlDoc *doc)
85 {
86 int res;
87
88 do {
89 res = xmlXIncludeProcess(doc);
90 } while (res > 0);
91
92 return res;
93 }
94
95 struct ast_xml_doc *ast_xml_open(char *filename)
96 {
97 xmlDoc *doc;
98
99 if (!filename) {
100 return NULL;
101 }
102
103 doc = xmlReadFile(filename, NULL, XML_PARSE_RECOVER | XML_PARSE_NONET);
104 if (!doc) {
105 return NULL;
106 }
107
108 /* process xinclude elements. */
109 if (process_xincludes(doc) < 0) {
110 xmlFreeDoc(doc);
111 return NULL;
112 }
113
114 #ifdef HAVE_LIBXSLT
115 {
116 xsltStylesheetPtr xslt = xsltLoadStylesheetPI(doc);
117 if (xslt) {
118 xmlDocPtr tmpdoc = xsltApplyStylesheet(xslt, doc, NULL);
119 xsltFreeStylesheet(xslt);
120 xmlFreeDoc(doc);
121 if (!tmpdoc) {
122 return NULL;
123 }
124 doc = tmpdoc;
125 }
126 }
127 #else /* no HAVE_LIBXSLT */
128 ast_log(LOG_NOTICE, "XSLT support not found. XML documentation may be incomplete.\n");
129 #endif /* HAVE_LIBXSLT */
130
131 /* Optimize for XPath */
132 xmlXPathOrderDocElems(doc);
133
134 return (struct ast_xml_doc *) doc;
135 }
136
137 struct ast_xml_doc *ast_xml_new(void)
138 {
139 xmlDoc *doc;
140
141 doc = xmlNewDoc((const xmlChar *) "1.0");
142 return (struct ast_xml_doc *) doc;
143 }
144
145 struct ast_xml_node *ast_xml_new_node(const char *name)
146 {
147 xmlNode *node;
148 if (!name) {
149 return NULL;
150 }
151
152 node = xmlNewNode(NULL, (const xmlChar *) name);
153
154 return (struct ast_xml_node *) node;
155 }
156
157 struct ast_xml_node *ast_xml_new_child(struct ast_xml_node *parent, const char *child_name)
158 {
159 xmlNode *child;
160
161 if (!parent || !child_name) {
162 return NULL;
163 }
164
165 child = xmlNewChild((xmlNode *) parent, NULL, (const xmlChar *) child_name, NULL);
166 return (struct ast_xml_node *) child;
167 }
168
169 struct ast_xml_node *ast_xml_add_child(struct ast_xml_node *parent, struct ast_xml_node *child)
170 {
171 if (!parent || !child) {
172 return NULL;
173 }
174 return (struct ast_xml_node *) xmlAddChild((xmlNode *) parent, (xmlNode *) child);
175 }
176
177 struct ast_xml_node *ast_xml_add_child_list(struct ast_xml_node *parent, struct ast_xml_node *child)
178 {
179 if (!parent || !child) {
180 return NULL;
181 }
182 return (struct ast_xml_node *) xmlAddChildList((xmlNode *) parent, (xmlNode *) child);
183 }
184
185 struct ast_xml_node *ast_xml_copy_node_list(struct ast_xml_node *list)
186 {
187 if (!list) {
188 return NULL;
189 }
190 return (struct ast_xml_node *) xmlCopyNodeList((xmlNode *) list);
191 }
192
193 struct ast_xml_doc *ast_xml_read_memory(char *buffer, size_t size)
194 {
195 xmlDoc *doc;
196
197 if (!buffer) {
198 return NULL;
199 }
200
201 if (!(doc = xmlParseMemory(buffer, (int) size))) {
202 /* process xinclude elements. */
203 if (process_xincludes(doc) < 0) {
204 xmlFreeDoc(doc);
205 return NULL;
206 }
207 }
208
209 return (struct ast_xml_doc *) doc;
210 }
211
212 void ast_xml_close(struct ast_xml_doc *doc)
213 {
214 if (!doc) {
215 return;
216 }
217
218 xmlFreeDoc((xmlDoc *) doc);
219 doc = NULL;
220 }
221
222 void ast_xml_set_root(struct ast_xml_doc *doc, struct ast_xml_node *node)
223 {
224 if (!doc || !node) {
225 return;
226 }
227
228 xmlDocSetRootElement((xmlDoc *) doc, (xmlNode *) node);
229 }
230
231 struct ast_xml_node *ast_xml_get_root(struct ast_xml_doc *doc)
232 {
233 xmlNode *root_node;
234
235 if (!doc) {
236 return NULL;
237 }
238
239 root_node = xmlDocGetRootElement((xmlDoc *) doc);
240
241 return (struct ast_xml_node *) root_node;
242 }
243
244 void ast_xml_free_node(struct ast_xml_node *node)
245 {
246 if (!node) {
247 return;
248 }
249
250 xmlFreeNode((xmlNode *) node);
251 node = NULL;
252 }
253
254 void ast_xml_free_attr(const char *attribute)
255 {
256 if (attribute) {
257 xmlFree((char *) attribute);
258 }
259 }
260
261 void ast_xml_free_text(const char *text)
262 {
263 if (text) {
264 xmlFree((char *) text);
265 }
266 }
267
268 const char *ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname)
269 {
270 xmlChar *attrvalue;
271
272 if (!node) {
273 return NULL;
274 }
275
276 if (!attrname) {
277 return NULL;
278 }
279
280 attrvalue = xmlGetProp((xmlNode *) node, (xmlChar *) attrname);
281
282 return (const char *) attrvalue;
283 }
284
285 int ast_xml_set_attribute(struct ast_xml_node *node, const char *name, const char *value)
286 {
287 if (!name || !value) {
288 return -1;
289 }
290
291 if (!xmlSetProp((xmlNode *) node, (xmlChar *) name, (xmlChar *) value)) {
292 return -1;
293 }
294
295 return 0;
296 }
297
298 struct ast_xml_node *ast_xml_find_element(struct ast_xml_node *root_node, const char *name, const char *attrname, const char *attrvalue)
299 {
300 struct ast_xml_node *cur;
301 const char *attr;
302
303 if (!root_node) {
304 return NULL;
305 }
306
307 for (cur = root_node; cur; cur = ast_xml_node_get_next(cur)) {
308 /* Check if the name matches */
309 if (strcmp(ast_xml_node_get_name(cur), name)) {
310 continue;
311 }
312 /* We need to check for a specific attribute name? */
313 if (!attrname || !attrvalue) {
314 return cur;
315 }
316 /* Get the attribute, we need to compare it. */
317 if ((attr = ast_xml_get_attribute(cur, attrname))) {
318 /* does attribute name/value matches? */
319 if (!strcmp(attr, attrvalue)) {
320 ast_xml_free_attr(attr);
321 return cur;
322 }
323 ast_xml_free_attr(attr);
324 }
325 }
326
327 return NULL;
328 }
329
330 struct ast_xml_doc *ast_xml_get_doc(struct ast_xml_node *node)
331 {
332 if (!node) {
333 return NULL;
334 }
335
336 return (struct ast_xml_doc *) ((xmlNode *)node)->doc;
337 }
338
339 struct ast_xml_ns *ast_xml_find_namespace(struct ast_xml_doc *doc, struct ast_xml_node *node, const char *ns_name) {
340 xmlNsPtr ns = xmlSearchNs((xmlDocPtr) doc, (xmlNodePtr) node, (xmlChar *) ns_name);
341 return (struct ast_xml_ns *) ns;
342 }
343
344 const char *ast_xml_get_ns_prefix(struct ast_xml_ns *ns)
345 {
346 return (const char *) ((xmlNsPtr) ns)->prefix;
347 }
348
349 const char *ast_xml_get_ns_href(struct ast_xml_ns *ns)
350 {
351 return (const char *) ((xmlNsPtr) ns)->href;
352 }
353
354 const char *ast_xml_get_text(struct ast_xml_node *node)
355 {
356 if (!node) {
357 return NULL;
358 }
359
360 return (const char *) xmlNodeGetContent((xmlNode *) node);
361 }
362
363 void ast_xml_set_text(struct ast_xml_node *node, const char *content)
364 {
365 if (!node || !content) {
366 return;
367 }
368
369 xmlNodeSetContent((xmlNode *) node, (const xmlChar *) content);
370 }
371
372 void ast_xml_set_name(struct ast_xml_node *node, const char *name)
373 {
374 if (!node || !name) {
375 return;
376 }
377
378 xmlNodeSetName((xmlNode *) node, (const xmlChar *) name);
379 }
380
381 int ast_xml_doc_dump_file(FILE *output, struct ast_xml_doc *doc)
382 {
383 return xmlDocDump(output, (xmlDocPtr)doc);
384 }
385
386 void ast_xml_doc_dump_memory(struct ast_xml_doc *doc, char **buffer, int *length)
387 {
388 xmlDocDumpFormatMemory((xmlDocPtr)doc, (xmlChar **)buffer, length, 1);
389 }
390
391 const char *ast_xml_node_get_name(struct ast_xml_node *node)
392 {
393 return (const char *) ((xmlNode *) node)->name;
394 }
395
396 struct ast_xml_node *ast_xml_node_get_children(struct ast_xml_node *node)
397 {
398 return (struct ast_xml_node *) ((xmlNode *) node)->children;
399 }
400
401 struct ast_xml_node *ast_xml_node_get_next(struct ast_xml_node *node)
402 {
403 return (struct ast_xml_node *) ((xmlNode *) node)->next;
404 }
405
406 struct ast_xml_node *ast_xml_node_get_prev(struct ast_xml_node *node)
407 {
408 return (struct ast_xml_node *) ((xmlNode *) node)->prev;
409 }
410
411 struct ast_xml_node *ast_xml_node_get_parent(struct ast_xml_node *node)
412 {
413 return (struct ast_xml_node *) ((xmlNode *) node)->parent;
414 }
415
416 struct ast_xml_node *ast_xml_xpath_get_first_result(struct ast_xml_xpath_results *results)
417 {
418 return (struct ast_xml_node *) ((xmlXPathObjectPtr) results)->nodesetval->nodeTab[0];
419 }
420
421 struct ast_xml_node *ast_xml_xpath_get_result(struct ast_xml_xpath_results *results, int i)
422 {
423 return (struct ast_xml_node *) ((xmlXPathObjectPtr) results)->nodesetval->nodeTab[i];
424 }
425
426 void ast_xml_xpath_results_free(struct ast_xml_xpath_results *results)
427 {
428 if (!results) {
429 return;
430 }
431 xmlXPathFreeObject((xmlXPathObjectPtr) results);
432 }
433
434 int ast_xml_xpath_num_results(struct ast_xml_xpath_results *results)
435 {
436 if (!results) {
437 return 0;
438 }
439 return ((xmlXPathObjectPtr) results)->nodesetval->nodeNr;
440 }
441
442 struct ast_xml_xpath_results *ast_xml_query(struct ast_xml_doc *doc, const char *xpath_str)
443 {
444 xmlXPathContextPtr context;
445 xmlXPathObjectPtr result;
446 if (!(context = xmlXPathNewContext((xmlDoc *) doc))) {
447 ast_log(LOG_ERROR, "Could not create XPath context!\n");
448 return NULL;
449 }
450 result = xmlXPathEvalExpression((xmlChar *) xpath_str, context);
451 xmlXPathFreeContext(context);
452 if (!result) {
453 ast_log(LOG_WARNING, "Error for query: %s\n", xpath_str);
454 return NULL;
455 }
456 if (xmlXPathNodeSetIsEmpty(result->nodesetval)) {
457 xmlXPathFreeObject(result);
458 ast_debug(5, "No results for query: %s\n", xpath_str);
459 return NULL;
460 }
461 return (struct ast_xml_xpath_results *) result;
462 }
463
464 struct ast_xml_xpath_results *ast_xml_query_with_namespaces(struct ast_xml_doc *doc, const char *xpath_str,
465 struct ast_xml_namespace_def_vector *namespaces)
466 {
467 xmlXPathContextPtr context;
468 xmlXPathObjectPtr result;
469 int i;
470
471 if (!(context = xmlXPathNewContext((xmlDoc *) doc))) {
472 ast_log(LOG_ERROR, "Could not create XPath context!\n");
473 return NULL;
474 }
475
476 for (i = 0; i < AST_VECTOR_SIZE(namespaces); i++) {
477 struct ast_xml_namespace_def ns = AST_VECTOR_GET(namespaces, i);
478 if (xmlXPathRegisterNs(context, (xmlChar *)ns.prefix,
479 (xmlChar *)ns.href) != 0) {
480 xmlXPathFreeContext(context);
481 ast_log(LOG_ERROR, "Could not register namespace %s:%s\n",
482 ns.prefix, ns.href);
483 return NULL;
484 }
485 }
486
487 result = xmlXPathEvalExpression((xmlChar *) xpath_str, context);
488 xmlXPathFreeContext(context);
489 if (!result) {
490 ast_log(LOG_WARNING, "Error for query: %s\n", xpath_str);
491 return NULL;
492 }
493 if (xmlXPathNodeSetIsEmpty(result->nodesetval)) {
494 xmlXPathFreeObject(result);
495 ast_debug(5, "No results for query: %s\n", xpath_str);
496 return NULL;
497 }
498 return (struct ast_xml_xpath_results *) result;
499 }
500
501 #ifdef HAVE_LIBXSLT
502 struct ast_xslt_doc *ast_xslt_open(char *filename)
503 {
504 xsltStylesheet *xslt;
505 xmlDoc *xml;
506
507 xml = xmlReadFile(filename, NULL, XML_PARSE_RECOVER | XML_PARSE_NONET);
508 if (!xml) {
509 return NULL;
510 }
511
512 if (process_xincludes(xml) < 0) {
513 xmlFreeDoc(xml);
514 return NULL;
515 }
516 xmlXPathOrderDocElems(xml);
517
518 if (!(xslt = xsltParseStylesheetDoc(xml))) {
519 xmlFreeDoc(xml);
520 return NULL;
521 }
522
523 return (struct ast_xslt_doc *) xslt;
524 }
525
526 struct ast_xslt_doc *ast_xslt_read_memory(char *buffer, size_t size)
527 {
528 xsltStylesheet *xslt;
529 xmlDoc *doc;
530
531 if (!buffer) {
532 return NULL;
533 }
534
535 doc = xmlReadMemory(buffer, (int) size, NULL, NULL, XML_PARSE_RECOVER | XML_PARSE_NONET);
536 if (!doc) {
537 return NULL;
538 }
539
540 if (process_xincludes(doc) < 0) {
541 xmlFreeDoc(doc);
542 return NULL;
543 }
544
545 if (!(xslt = xsltParseStylesheetDoc(doc))) {
546 xmlFreeDoc(doc);
547 return NULL;
548 }
549
550 return (struct ast_xslt_doc *) xslt;
551 }
552
553 void ast_xslt_close(struct ast_xslt_doc *axslt)
554 {
555 if (!axslt) {
556 return;
557 }
558
559 xsltFreeStylesheet((xsltStylesheet *) axslt);
560 }
561
562 struct ast_xml_doc *ast_xslt_apply(struct ast_xslt_doc *axslt, struct ast_xml_doc *axml, const char **params)
563 {
564 xsltStylesheet *xslt = (xsltStylesheet *)axslt;
565 xmlDoc *xml = (xmlDoc *)axml;
566 xmlDoc *res;
567 xsltTransformContextPtr ctxt;
568 xmlNs *ns;
569 int options = XSLT_PARSE_OPTIONS;
570
571 /*
572 * Normally we could just call xsltApplyStylesheet() without creating
573 * our own transform context but passing parameters to it that have
574 * namespace prefixes isn't supported. Instead we have to create a
575 * transform context, iterate over the namespace declarations in the
576 * stylesheet (not the incoming xml document), add them to the
577 * transform context's xpath context, and call xsltApplyStylesheetUser.
578 *
579 * Since this is a bit involved and libxslt apparently doesn't completely
580 * clean up after itself in this situation, we'll only do that dance
581 * if there are parameters passed in. Otherwise we just call the simpler
582 * xsltApplyStylesheet.
583 *
584 */
585
586 if (!params) {
587 res = xsltApplyStylesheet(xslt, xml, params);
588 return (struct ast_xml_doc *)res;
589 }
590
591 ctxt = xsltNewTransformContext(xslt, xml);
592 xsltSetCtxtParseOptions(ctxt, options);
593
594 for (ns = xslt->doc->children->nsDef; ns; ns = ns->next) {
595 if (xmlXPathRegisterNs(ctxt->xpathCtxt, ns->prefix, ns->href) != 0) {
596 xsltFreeTransformContext(ctxt);
597 return NULL;
598 }
599 }
600
601 res = xsltApplyStylesheetUser(xslt, xml, params, NULL, NULL, ctxt);
602 xmlXPathFreeContext(ctxt->xpathCtxt);
603 ctxt->xpathCtxt = NULL;
604 xsltFreeTransformContext(ctxt);
605
606 return (struct ast_xml_doc *)res;
607 }
608
609 int ast_xslt_save_result_to_string(char **buffer, int *length, struct ast_xml_doc *result,
610 struct ast_xslt_doc *axslt)
611 {
612 return xsltSaveResultToString((xmlChar **)buffer, length, (xmlDoc *)result, (xsltStylesheet *)axslt);
613 }
614
615 #endif /* defined(HAVE_LIBXSLT) */
616 #endif /* defined(HAVE_LIBXML2) */