xml.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. /*! \file
  17. *
  18. * \brief XML abstraction layer
  19. *
  20. * \author Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
  21. */
  22. /*** MODULEINFO
  23. <support_level>core</support_level>
  24. ***/
  25. #include "asterisk.h"
  26. #include "asterisk/xml.h"
  27. #include "asterisk/logger.h"
  28. #include "asterisk/utils.h"
  29. #include "asterisk/autoconfig.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #if defined(HAVE_LIBXML2)
  32. #include <libxml/parser.h>
  33. #include <libxml/tree.h>
  34. #include <libxml/xinclude.h>
  35. #include <libxml/xpath.h>
  36. /* libxml2 ast_xml implementation. */
  37. #ifdef HAVE_LIBXSLT
  38. #include <libxslt/xsltInternals.h>
  39. #include <libxslt/transform.h>
  40. #endif /* HAVE_LIBXSLT */
  41. int ast_xml_init(void)
  42. {
  43. LIBXML_TEST_VERSION
  44. return 0;
  45. }
  46. int ast_xml_finish(void)
  47. {
  48. xmlCleanupParser();
  49. #ifdef HAVE_LIBXSLT_CLEANUP
  50. xsltCleanupGlobals();
  51. #endif
  52. return 0;
  53. }
  54. struct ast_xml_doc *ast_xml_open(char *filename)
  55. {
  56. xmlDoc *doc;
  57. if (!filename) {
  58. return NULL;
  59. }
  60. doc = xmlReadFile(filename, NULL, XML_PARSE_RECOVER);
  61. if (!doc) {
  62. return NULL;
  63. }
  64. /* process xinclude elements. */
  65. if (xmlXIncludeProcess(doc) < 0) {
  66. xmlFreeDoc(doc);
  67. return NULL;
  68. }
  69. #ifdef HAVE_LIBXSLT
  70. {
  71. xsltStylesheetPtr xslt = xsltLoadStylesheetPI(doc);
  72. if (xslt) {
  73. xmlDocPtr tmpdoc = xsltApplyStylesheet(xslt, doc, NULL);
  74. xsltFreeStylesheet(xslt);
  75. xmlFreeDoc(doc);
  76. if (!tmpdoc) {
  77. return NULL;
  78. }
  79. doc = tmpdoc;
  80. }
  81. }
  82. #else /* no HAVE_LIBXSLT */
  83. ast_log(LOG_NOTICE, "XSLT support not found. XML documentation may be incomplete.\n");
  84. #endif /* HAVE_LIBXSLT */
  85. return (struct ast_xml_doc *) doc;
  86. }
  87. struct ast_xml_doc *ast_xml_new(void)
  88. {
  89. xmlDoc *doc;
  90. doc = xmlNewDoc((const xmlChar *) "1.0");
  91. return (struct ast_xml_doc *) doc;
  92. }
  93. struct ast_xml_node *ast_xml_new_node(const char *name)
  94. {
  95. xmlNode *node;
  96. if (!name) {
  97. return NULL;
  98. }
  99. node = xmlNewNode(NULL, (const xmlChar *) name);
  100. return (struct ast_xml_node *) node;
  101. }
  102. struct ast_xml_node *ast_xml_new_child(struct ast_xml_node *parent, const char *child_name)
  103. {
  104. xmlNode *child;
  105. if (!parent || !child_name) {
  106. return NULL;
  107. }
  108. child = xmlNewChild((xmlNode *) parent, NULL, (const xmlChar *) child_name, NULL);
  109. return (struct ast_xml_node *) child;
  110. }
  111. struct ast_xml_node *ast_xml_add_child(struct ast_xml_node *parent, struct ast_xml_node *child)
  112. {
  113. if (!parent || !child) {
  114. return NULL;
  115. }
  116. return (struct ast_xml_node *) xmlAddChild((xmlNode *) parent, (xmlNode *) child);
  117. }
  118. struct ast_xml_doc *ast_xml_read_memory(char *buffer, size_t size)
  119. {
  120. xmlDoc *doc;
  121. if (!buffer) {
  122. return NULL;
  123. }
  124. if (!(doc = xmlParseMemory(buffer, (int) size))) {
  125. /* process xinclude elements. */
  126. if (xmlXIncludeProcess(doc) < 0) {
  127. xmlFreeDoc(doc);
  128. return NULL;
  129. }
  130. }
  131. return (struct ast_xml_doc *) doc;
  132. }
  133. void ast_xml_close(struct ast_xml_doc *doc)
  134. {
  135. if (!doc) {
  136. return;
  137. }
  138. xmlFreeDoc((xmlDoc *) doc);
  139. doc = NULL;
  140. }
  141. void ast_xml_set_root(struct ast_xml_doc *doc, struct ast_xml_node *node)
  142. {
  143. if (!doc || !node) {
  144. return;
  145. }
  146. xmlDocSetRootElement((xmlDoc *) doc, (xmlNode *) node);
  147. }
  148. struct ast_xml_node *ast_xml_get_root(struct ast_xml_doc *doc)
  149. {
  150. xmlNode *root_node;
  151. if (!doc) {
  152. return NULL;
  153. }
  154. root_node = xmlDocGetRootElement((xmlDoc *) doc);
  155. return (struct ast_xml_node *) root_node;
  156. }
  157. void ast_xml_free_node(struct ast_xml_node *node)
  158. {
  159. if (!node) {
  160. return;
  161. }
  162. xmlFreeNode((xmlNode *) node);
  163. node = NULL;
  164. }
  165. void ast_xml_free_attr(const char *attribute)
  166. {
  167. if (attribute) {
  168. xmlFree((char *) attribute);
  169. }
  170. }
  171. void ast_xml_free_text(const char *text)
  172. {
  173. if (text) {
  174. xmlFree((char *) text);
  175. }
  176. }
  177. const char *ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname)
  178. {
  179. xmlChar *attrvalue;
  180. if (!node) {
  181. return NULL;
  182. }
  183. if (!attrname) {
  184. return NULL;
  185. }
  186. attrvalue = xmlGetProp((xmlNode *) node, (xmlChar *) attrname);
  187. return (const char *) attrvalue;
  188. }
  189. int ast_xml_set_attribute(struct ast_xml_node *node, const char *name, const char *value)
  190. {
  191. if (!name || !value) {
  192. return -1;
  193. }
  194. if (!xmlSetProp((xmlNode *) node, (xmlChar *) name, (xmlChar *) value)) {
  195. return -1;
  196. }
  197. return 0;
  198. }
  199. struct ast_xml_node *ast_xml_find_element(struct ast_xml_node *root_node, const char *name, const char *attrname, const char *attrvalue)
  200. {
  201. struct ast_xml_node *cur;
  202. const char *attr;
  203. if (!root_node) {
  204. return NULL;
  205. }
  206. for (cur = root_node; cur; cur = ast_xml_node_get_next(cur)) {
  207. /* Check if the name matchs */
  208. if (strcmp(ast_xml_node_get_name(cur), name)) {
  209. continue;
  210. }
  211. /* We need to check for a specific attribute name? */
  212. if (!attrname || !attrvalue) {
  213. return cur;
  214. }
  215. /* Get the attribute, we need to compare it. */
  216. if ((attr = ast_xml_get_attribute(cur, attrname))) {
  217. /* does attribute name/value matches? */
  218. if (!strcmp(attr, attrvalue)) {
  219. ast_xml_free_attr(attr);
  220. return cur;
  221. }
  222. ast_xml_free_attr(attr);
  223. }
  224. }
  225. return NULL;
  226. }
  227. struct ast_xml_doc *ast_xml_get_doc(struct ast_xml_node *node)
  228. {
  229. if (!node) {
  230. return NULL;
  231. }
  232. return (struct ast_xml_doc *) ((xmlNode *)node)->doc;
  233. }
  234. struct ast_xml_ns *ast_xml_find_namespace(struct ast_xml_doc *doc, struct ast_xml_node *node, const char *ns_name) {
  235. xmlNsPtr ns = xmlSearchNs((xmlDocPtr) doc, (xmlNodePtr) node, (xmlChar *) ns_name);
  236. return (struct ast_xml_ns *) ns;
  237. }
  238. const char *ast_xml_get_ns_href(struct ast_xml_ns *ns)
  239. {
  240. return (const char *) ((xmlNsPtr) ns)->href;
  241. }
  242. const char *ast_xml_get_text(struct ast_xml_node *node)
  243. {
  244. if (!node) {
  245. return NULL;
  246. }
  247. return (const char *) xmlNodeGetContent((xmlNode *) node);
  248. }
  249. void ast_xml_set_text(struct ast_xml_node *node, const char *content)
  250. {
  251. if (!node || !content) {
  252. return;
  253. }
  254. xmlNodeSetContent((xmlNode *) node, (const xmlChar *) content);
  255. }
  256. int ast_xml_doc_dump_file(FILE *output, struct ast_xml_doc *doc)
  257. {
  258. return xmlDocDump(output, (xmlDocPtr)doc);
  259. }
  260. const char *ast_xml_node_get_name(struct ast_xml_node *node)
  261. {
  262. return (const char *) ((xmlNode *) node)->name;
  263. }
  264. struct ast_xml_node *ast_xml_node_get_children(struct ast_xml_node *node)
  265. {
  266. return (struct ast_xml_node *) ((xmlNode *) node)->children;
  267. }
  268. struct ast_xml_node *ast_xml_node_get_next(struct ast_xml_node *node)
  269. {
  270. return (struct ast_xml_node *) ((xmlNode *) node)->next;
  271. }
  272. struct ast_xml_node *ast_xml_node_get_prev(struct ast_xml_node *node)
  273. {
  274. return (struct ast_xml_node *) ((xmlNode *) node)->prev;
  275. }
  276. struct ast_xml_node *ast_xml_node_get_parent(struct ast_xml_node *node)
  277. {
  278. return (struct ast_xml_node *) ((xmlNode *) node)->parent;
  279. }
  280. struct ast_xml_node *ast_xml_xpath_get_first_result(struct ast_xml_xpath_results *results)
  281. {
  282. return (struct ast_xml_node *) ((xmlXPathObjectPtr) results)->nodesetval->nodeTab[0];
  283. }
  284. void ast_xml_xpath_results_free(struct ast_xml_xpath_results *results)
  285. {
  286. xmlXPathFreeObject((xmlXPathObjectPtr) results);
  287. }
  288. int ast_xml_xpath_num_results(struct ast_xml_xpath_results *results)
  289. {
  290. return ((xmlXPathObjectPtr) results)->nodesetval->nodeNr;
  291. }
  292. struct ast_xml_xpath_results *ast_xml_query(struct ast_xml_doc *doc, const char *xpath_str)
  293. {
  294. xmlXPathContextPtr context;
  295. xmlXPathObjectPtr result;
  296. if (!(context = xmlXPathNewContext((xmlDoc *) doc))) {
  297. ast_log(LOG_ERROR, "Could not create XPath context!\n");
  298. return NULL;
  299. }
  300. result = xmlXPathEvalExpression((xmlChar *) xpath_str, context);
  301. xmlXPathFreeContext(context);
  302. if (!result) {
  303. ast_log(LOG_WARNING, "Error for query: %s\n", xpath_str);
  304. return NULL;
  305. }
  306. if (xmlXPathNodeSetIsEmpty(result->nodesetval)) {
  307. xmlXPathFreeObject(result);
  308. ast_debug(5, "No results for query: %s\n", xpath_str);
  309. return NULL;
  310. }
  311. return (struct ast_xml_xpath_results *) result;
  312. }
  313. #endif /* defined(HAVE_LIBXML2) */