xml.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #if defined(HAVE_LIBXML2)
  30. #include <libxml/parser.h>
  31. #include <libxml/tree.h>
  32. #include <libxml/xinclude.h>
  33. /* libxml2 ast_xml implementation. */
  34. int ast_xml_init(void)
  35. {
  36. LIBXML_TEST_VERSION
  37. return 0;
  38. }
  39. int ast_xml_finish(void)
  40. {
  41. xmlCleanupParser();
  42. return 0;
  43. }
  44. struct ast_xml_doc *ast_xml_open(char *filename)
  45. {
  46. xmlDoc *doc;
  47. if (!filename) {
  48. return NULL;
  49. }
  50. doc = xmlReadFile(filename, NULL, XML_PARSE_RECOVER);
  51. if (doc) {
  52. /* process xinclude elements. */
  53. if (xmlXIncludeProcess(doc) < 0) {
  54. xmlFreeDoc(doc);
  55. return NULL;
  56. }
  57. }
  58. return (struct ast_xml_doc *) doc;
  59. }
  60. struct ast_xml_doc *ast_xml_new(void)
  61. {
  62. xmlDoc *doc;
  63. doc = xmlNewDoc((const xmlChar *) "1.0");
  64. return (struct ast_xml_doc *) doc;
  65. }
  66. struct ast_xml_node *ast_xml_new_node(const char *name)
  67. {
  68. xmlNode *node;
  69. if (!name) {
  70. return NULL;
  71. }
  72. node = xmlNewNode(NULL, (const xmlChar *) name);
  73. return (struct ast_xml_node *) node;
  74. }
  75. struct ast_xml_node *ast_xml_new_child(struct ast_xml_node *parent, const char *child_name)
  76. {
  77. xmlNode *child;
  78. if (!parent || !child_name) {
  79. return NULL;
  80. }
  81. child = xmlNewChild((xmlNode *) parent, NULL, (const xmlChar *) child_name, NULL);
  82. return (struct ast_xml_node *) child;
  83. }
  84. struct ast_xml_node *ast_xml_add_child(struct ast_xml_node *parent, struct ast_xml_node *child)
  85. {
  86. if (!parent || !child) {
  87. return NULL;
  88. }
  89. return (struct ast_xml_node *) xmlAddChild((xmlNode *) parent, (xmlNode *) child);
  90. }
  91. struct ast_xml_doc *ast_xml_read_memory(char *buffer, size_t size)
  92. {
  93. xmlDoc *doc;
  94. if (!buffer) {
  95. return NULL;
  96. }
  97. if (!(doc = xmlParseMemory(buffer, (int) size))) {
  98. /* process xinclude elements. */
  99. if (xmlXIncludeProcess(doc) < 0) {
  100. xmlFreeDoc(doc);
  101. return NULL;
  102. }
  103. }
  104. return (struct ast_xml_doc *) doc;
  105. }
  106. void ast_xml_close(struct ast_xml_doc *doc)
  107. {
  108. if (!doc) {
  109. return;
  110. }
  111. xmlFreeDoc((xmlDoc *) doc);
  112. doc = NULL;
  113. }
  114. void ast_xml_set_root(struct ast_xml_doc *doc, struct ast_xml_node *node)
  115. {
  116. if (!doc || !node) {
  117. return;
  118. }
  119. xmlDocSetRootElement((xmlDoc *) doc, (xmlNode *) node);
  120. }
  121. struct ast_xml_node *ast_xml_get_root(struct ast_xml_doc *doc)
  122. {
  123. xmlNode *root_node;
  124. if (!doc) {
  125. return NULL;
  126. }
  127. root_node = xmlDocGetRootElement((xmlDoc *) doc);
  128. return (struct ast_xml_node *) root_node;
  129. }
  130. void ast_xml_free_node(struct ast_xml_node *node)
  131. {
  132. if (!node) {
  133. return;
  134. }
  135. xmlFreeNode((xmlNode *) node);
  136. node = NULL;
  137. }
  138. void ast_xml_free_attr(const char *attribute)
  139. {
  140. if (attribute) {
  141. xmlFree((char *) attribute);
  142. }
  143. }
  144. void ast_xml_free_text(const char *text)
  145. {
  146. if (text) {
  147. xmlFree((char *) text);
  148. }
  149. }
  150. const char *ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname)
  151. {
  152. xmlChar *attrvalue;
  153. if (!node) {
  154. return NULL;
  155. }
  156. if (!attrname) {
  157. return NULL;
  158. }
  159. attrvalue = xmlGetProp((xmlNode *) node, (xmlChar *) attrname);
  160. return (const char *) attrvalue;
  161. }
  162. int ast_xml_set_attribute(struct ast_xml_node *node, const char *name, const char *value)
  163. {
  164. if (!name || !value) {
  165. return -1;
  166. }
  167. if (!xmlSetProp((xmlNode *) node, (xmlChar *) name, (xmlChar *) value)) {
  168. return -1;
  169. }
  170. return 0;
  171. }
  172. struct ast_xml_node *ast_xml_find_element(struct ast_xml_node *root_node, const char *name, const char *attrname, const char *attrvalue)
  173. {
  174. struct ast_xml_node *cur;
  175. const char *attr;
  176. if (!root_node) {
  177. return NULL;
  178. }
  179. for (cur = root_node; cur; cur = ast_xml_node_get_next(cur)) {
  180. /* Check if the name matchs */
  181. if (strcmp(ast_xml_node_get_name(cur), name)) {
  182. continue;
  183. }
  184. /* We need to check for a specific attribute name? */
  185. if (!attrname || !attrvalue) {
  186. return cur;
  187. }
  188. /* Get the attribute, we need to compare it. */
  189. if ((attr = ast_xml_get_attribute(cur, attrname))) {
  190. /* does attribute name/value matches? */
  191. if (!strcmp(attr, attrvalue)) {
  192. ast_xml_free_attr(attr);
  193. return cur;
  194. }
  195. ast_xml_free_attr(attr);
  196. }
  197. }
  198. return NULL;
  199. }
  200. struct ast_xml_doc *ast_xml_get_doc(struct ast_xml_node *node)
  201. {
  202. if (!node) {
  203. return NULL;
  204. }
  205. return (struct ast_xml_doc *) ((xmlNode *)node)->doc;
  206. }
  207. struct ast_xml_ns *ast_xml_find_namespace(struct ast_xml_doc *doc, struct ast_xml_node *node, const char *ns_name) {
  208. xmlNsPtr ns = xmlSearchNs((xmlDocPtr) doc, (xmlNodePtr) node, (xmlChar *) ns_name);
  209. return (struct ast_xml_ns *) ns;
  210. }
  211. const char *ast_xml_get_ns_href(struct ast_xml_ns *ns)
  212. {
  213. return (const char *) ((xmlNsPtr) ns)->href;
  214. }
  215. const char *ast_xml_get_text(struct ast_xml_node *node)
  216. {
  217. if (!node) {
  218. return NULL;
  219. }
  220. return (const char *) xmlNodeGetContent((xmlNode *) node);
  221. }
  222. void ast_xml_set_text(struct ast_xml_node *node, const char *content)
  223. {
  224. if (!node || !content) {
  225. return;
  226. }
  227. xmlNodeSetContent((xmlNode *) node, (const xmlChar *) content);
  228. }
  229. int ast_xml_doc_dump_file(FILE *output, struct ast_xml_doc *doc)
  230. {
  231. return xmlDocDump(output, (xmlDocPtr)doc);
  232. }
  233. const char *ast_xml_node_get_name(struct ast_xml_node *node)
  234. {
  235. return (const char *) ((xmlNode *) node)->name;
  236. }
  237. struct ast_xml_node *ast_xml_node_get_children(struct ast_xml_node *node)
  238. {
  239. return (struct ast_xml_node *) ((xmlNode *) node)->children;
  240. }
  241. struct ast_xml_node *ast_xml_node_get_next(struct ast_xml_node *node)
  242. {
  243. return (struct ast_xml_node *) ((xmlNode *) node)->next;
  244. }
  245. struct ast_xml_node *ast_xml_node_get_prev(struct ast_xml_node *node)
  246. {
  247. return (struct ast_xml_node *) ((xmlNode *) node)->prev;
  248. }
  249. struct ast_xml_node *ast_xml_node_get_parent(struct ast_xml_node *node)
  250. {
  251. return (struct ast_xml_node *) ((xmlNode *) node)->parent;
  252. }
  253. #endif /* defined(HAVE_LIBXML2) */