xml.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. /* Optimize for XPath */
  86. xmlXPathOrderDocElems(doc);
  87. return (struct ast_xml_doc *) doc;
  88. }
  89. struct ast_xml_doc *ast_xml_new(void)
  90. {
  91. xmlDoc *doc;
  92. doc = xmlNewDoc((const xmlChar *) "1.0");
  93. return (struct ast_xml_doc *) doc;
  94. }
  95. struct ast_xml_node *ast_xml_new_node(const char *name)
  96. {
  97. xmlNode *node;
  98. if (!name) {
  99. return NULL;
  100. }
  101. node = xmlNewNode(NULL, (const xmlChar *) name);
  102. return (struct ast_xml_node *) node;
  103. }
  104. struct ast_xml_node *ast_xml_new_child(struct ast_xml_node *parent, const char *child_name)
  105. {
  106. xmlNode *child;
  107. if (!parent || !child_name) {
  108. return NULL;
  109. }
  110. child = xmlNewChild((xmlNode *) parent, NULL, (const xmlChar *) child_name, NULL);
  111. return (struct ast_xml_node *) child;
  112. }
  113. struct ast_xml_node *ast_xml_add_child(struct ast_xml_node *parent, struct ast_xml_node *child)
  114. {
  115. if (!parent || !child) {
  116. return NULL;
  117. }
  118. return (struct ast_xml_node *) xmlAddChild((xmlNode *) parent, (xmlNode *) child);
  119. }
  120. struct ast_xml_node *ast_xml_add_child_list(struct ast_xml_node *parent, struct ast_xml_node *child)
  121. {
  122. if (!parent || !child) {
  123. return NULL;
  124. }
  125. return (struct ast_xml_node *) xmlAddChildList((xmlNode *) parent, (xmlNode *) child);
  126. }
  127. struct ast_xml_node *ast_xml_copy_node_list(struct ast_xml_node *list)
  128. {
  129. if (!list) {
  130. return NULL;
  131. }
  132. return (struct ast_xml_node *) xmlCopyNodeList((xmlNode *) list);
  133. }
  134. struct ast_xml_doc *ast_xml_read_memory(char *buffer, size_t size)
  135. {
  136. xmlDoc *doc;
  137. if (!buffer) {
  138. return NULL;
  139. }
  140. if (!(doc = xmlParseMemory(buffer, (int) size))) {
  141. /* process xinclude elements. */
  142. if (xmlXIncludeProcess(doc) < 0) {
  143. xmlFreeDoc(doc);
  144. return NULL;
  145. }
  146. }
  147. return (struct ast_xml_doc *) doc;
  148. }
  149. void ast_xml_close(struct ast_xml_doc *doc)
  150. {
  151. if (!doc) {
  152. return;
  153. }
  154. xmlFreeDoc((xmlDoc *) doc);
  155. doc = NULL;
  156. }
  157. void ast_xml_set_root(struct ast_xml_doc *doc, struct ast_xml_node *node)
  158. {
  159. if (!doc || !node) {
  160. return;
  161. }
  162. xmlDocSetRootElement((xmlDoc *) doc, (xmlNode *) node);
  163. }
  164. struct ast_xml_node *ast_xml_get_root(struct ast_xml_doc *doc)
  165. {
  166. xmlNode *root_node;
  167. if (!doc) {
  168. return NULL;
  169. }
  170. root_node = xmlDocGetRootElement((xmlDoc *) doc);
  171. return (struct ast_xml_node *) root_node;
  172. }
  173. void ast_xml_free_node(struct ast_xml_node *node)
  174. {
  175. if (!node) {
  176. return;
  177. }
  178. xmlFreeNode((xmlNode *) node);
  179. node = NULL;
  180. }
  181. void ast_xml_free_attr(const char *attribute)
  182. {
  183. if (attribute) {
  184. xmlFree((char *) attribute);
  185. }
  186. }
  187. void ast_xml_free_text(const char *text)
  188. {
  189. if (text) {
  190. xmlFree((char *) text);
  191. }
  192. }
  193. const char *ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname)
  194. {
  195. xmlChar *attrvalue;
  196. if (!node) {
  197. return NULL;
  198. }
  199. if (!attrname) {
  200. return NULL;
  201. }
  202. attrvalue = xmlGetProp((xmlNode *) node, (xmlChar *) attrname);
  203. return (const char *) attrvalue;
  204. }
  205. int ast_xml_set_attribute(struct ast_xml_node *node, const char *name, const char *value)
  206. {
  207. if (!name || !value) {
  208. return -1;
  209. }
  210. if (!xmlSetProp((xmlNode *) node, (xmlChar *) name, (xmlChar *) value)) {
  211. return -1;
  212. }
  213. return 0;
  214. }
  215. struct ast_xml_node *ast_xml_find_element(struct ast_xml_node *root_node, const char *name, const char *attrname, const char *attrvalue)
  216. {
  217. struct ast_xml_node *cur;
  218. const char *attr;
  219. if (!root_node) {
  220. return NULL;
  221. }
  222. for (cur = root_node; cur; cur = ast_xml_node_get_next(cur)) {
  223. /* Check if the name matchs */
  224. if (strcmp(ast_xml_node_get_name(cur), name)) {
  225. continue;
  226. }
  227. /* We need to check for a specific attribute name? */
  228. if (!attrname || !attrvalue) {
  229. return cur;
  230. }
  231. /* Get the attribute, we need to compare it. */
  232. if ((attr = ast_xml_get_attribute(cur, attrname))) {
  233. /* does attribute name/value matches? */
  234. if (!strcmp(attr, attrvalue)) {
  235. ast_xml_free_attr(attr);
  236. return cur;
  237. }
  238. ast_xml_free_attr(attr);
  239. }
  240. }
  241. return NULL;
  242. }
  243. struct ast_xml_doc *ast_xml_get_doc(struct ast_xml_node *node)
  244. {
  245. if (!node) {
  246. return NULL;
  247. }
  248. return (struct ast_xml_doc *) ((xmlNode *)node)->doc;
  249. }
  250. struct ast_xml_ns *ast_xml_find_namespace(struct ast_xml_doc *doc, struct ast_xml_node *node, const char *ns_name) {
  251. xmlNsPtr ns = xmlSearchNs((xmlDocPtr) doc, (xmlNodePtr) node, (xmlChar *) ns_name);
  252. return (struct ast_xml_ns *) ns;
  253. }
  254. const char *ast_xml_get_ns_href(struct ast_xml_ns *ns)
  255. {
  256. return (const char *) ((xmlNsPtr) ns)->href;
  257. }
  258. const char *ast_xml_get_text(struct ast_xml_node *node)
  259. {
  260. if (!node) {
  261. return NULL;
  262. }
  263. return (const char *) xmlNodeGetContent((xmlNode *) node);
  264. }
  265. void ast_xml_set_text(struct ast_xml_node *node, const char *content)
  266. {
  267. if (!node || !content) {
  268. return;
  269. }
  270. xmlNodeSetContent((xmlNode *) node, (const xmlChar *) content);
  271. }
  272. int ast_xml_doc_dump_file(FILE *output, struct ast_xml_doc *doc)
  273. {
  274. return xmlDocDump(output, (xmlDocPtr)doc);
  275. }
  276. const char *ast_xml_node_get_name(struct ast_xml_node *node)
  277. {
  278. return (const char *) ((xmlNode *) node)->name;
  279. }
  280. struct ast_xml_node *ast_xml_node_get_children(struct ast_xml_node *node)
  281. {
  282. return (struct ast_xml_node *) ((xmlNode *) node)->children;
  283. }
  284. struct ast_xml_node *ast_xml_node_get_next(struct ast_xml_node *node)
  285. {
  286. return (struct ast_xml_node *) ((xmlNode *) node)->next;
  287. }
  288. struct ast_xml_node *ast_xml_node_get_prev(struct ast_xml_node *node)
  289. {
  290. return (struct ast_xml_node *) ((xmlNode *) node)->prev;
  291. }
  292. struct ast_xml_node *ast_xml_node_get_parent(struct ast_xml_node *node)
  293. {
  294. return (struct ast_xml_node *) ((xmlNode *) node)->parent;
  295. }
  296. struct ast_xml_node *ast_xml_xpath_get_first_result(struct ast_xml_xpath_results *results)
  297. {
  298. return (struct ast_xml_node *) ((xmlXPathObjectPtr) results)->nodesetval->nodeTab[0];
  299. }
  300. void ast_xml_xpath_results_free(struct ast_xml_xpath_results *results)
  301. {
  302. xmlXPathFreeObject((xmlXPathObjectPtr) results);
  303. }
  304. int ast_xml_xpath_num_results(struct ast_xml_xpath_results *results)
  305. {
  306. return ((xmlXPathObjectPtr) results)->nodesetval->nodeNr;
  307. }
  308. struct ast_xml_xpath_results *ast_xml_query(struct ast_xml_doc *doc, const char *xpath_str)
  309. {
  310. xmlXPathContextPtr context;
  311. xmlXPathObjectPtr result;
  312. if (!(context = xmlXPathNewContext((xmlDoc *) doc))) {
  313. ast_log(LOG_ERROR, "Could not create XPath context!\n");
  314. return NULL;
  315. }
  316. result = xmlXPathEvalExpression((xmlChar *) xpath_str, context);
  317. xmlXPathFreeContext(context);
  318. if (!result) {
  319. ast_log(LOG_WARNING, "Error for query: %s\n", xpath_str);
  320. return NULL;
  321. }
  322. if (xmlXPathNodeSetIsEmpty(result->nodesetval)) {
  323. xmlXPathFreeObject(result);
  324. ast_debug(5, "No results for query: %s\n", xpath_str);
  325. return NULL;
  326. }
  327. return (struct ast_xml_xpath_results *) result;
  328. }
  329. #endif /* defined(HAVE_LIBXML2) */