1
0

libxml2_api.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /** @file libxml2_api.c
  2. */
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <libxml/parser.h>
  6. #include <libxml/tree.h>
  7. #include <faux/faux.h>
  8. #include <faux/str.h>
  9. #include <klish/kxml.h>
  10. bool_t kxml_doc_start(void)
  11. {
  12. return BOOL_TRUE;
  13. }
  14. bool_t kxml_doc_stop(void)
  15. {
  16. xmlCleanupParser();
  17. return BOOL_TRUE;
  18. }
  19. kxml_doc_t *kxml_doc_read(const char *filename)
  20. {
  21. xmlDoc *doc = NULL;
  22. if (faux_str_is_empty(filename))
  23. return NULL;
  24. doc = xmlReadFile(filename, NULL, 0);
  25. return (kxml_doc_t *)doc;
  26. }
  27. void kxml_doc_release(kxml_doc_t *doc)
  28. {
  29. if (doc)
  30. xmlFreeDoc((xmlDoc *)doc);
  31. }
  32. bool_t kxml_doc_is_valid(const kxml_doc_t *doc)
  33. {
  34. return (bool_t)(doc != NULL);
  35. }
  36. /*
  37. int kxml_doc_error_caps(kxml_doc_t *doc)
  38. {
  39. doc = doc; // happy compiler
  40. return kxmlERR_NOCAPS;
  41. }
  42. int kxml_doc_err_line(kxml_doc_t *doc)
  43. {
  44. doc = doc; // happy compiler
  45. return -1;
  46. }
  47. int kxml_doc_err_col(kxml_doc_t *doc)
  48. {
  49. doc = doc; // happy compiler
  50. return -1;
  51. }
  52. const char *kxml_doc_err_msg(kxml_doc_t *doc)
  53. {
  54. doc = doc; // happy compiler
  55. return "";
  56. }
  57. */
  58. kxml_nodetype_e kxml_node_type(const kxml_node_t *node)
  59. {
  60. if (!node)
  61. return KXML_NODE_UNKNOWN;
  62. switch (((xmlNode *)node)->type) {
  63. case XML_ELEMENT_NODE:
  64. return KXML_NODE_ELM;
  65. case XML_TEXT_NODE:
  66. return KXML_NODE_TEXT;
  67. case XML_COMMENT_NODE:
  68. return KXML_NODE_COMMENT;
  69. case XML_PI_NODE:
  70. return KXML_NODE_PI;
  71. case XML_ATTRIBUTE_NODE:
  72. return KXML_NODE_ATTR;
  73. default:
  74. break;
  75. }
  76. return KXML_NODE_UNKNOWN;
  77. }
  78. kxml_node_t *kxml_doc_root(const kxml_doc_t *doc)
  79. {
  80. if (!doc)
  81. return NULL;
  82. return (kxml_node_t *)xmlDocGetRootElement((xmlDoc *)doc);
  83. }
  84. kxml_node_t *kxml_node_parent(const kxml_node_t *node)
  85. {
  86. xmlNode *root = NULL;
  87. xmlNode *n = (xmlNode *)node;
  88. if (!n)
  89. return NULL;
  90. root = xmlDocGetRootElement(n->doc);
  91. if (n != root)
  92. return (kxml_node_t *)n->parent;
  93. return NULL;
  94. }
  95. const kxml_node_t *kxml_node_next_child(const kxml_node_t *parent,
  96. const kxml_node_t *curchild)
  97. {
  98. xmlNode *child = NULL;
  99. if (!parent)
  100. return NULL;
  101. if (curchild) {
  102. child = ((xmlNode *)curchild)->next;
  103. } else {
  104. child = ((xmlNode *)parent)->children;
  105. }
  106. return (kxml_node_t *)child;
  107. }
  108. char *kxml_node_attr(const kxml_node_t *node, const char *attrname)
  109. {
  110. xmlNode *n = (xmlNode *)node;
  111. xmlAttr *a = NULL;
  112. if (!node || !attrname)
  113. return NULL;
  114. if (n->type != XML_ELEMENT_NODE)
  115. return NULL;
  116. a = n->properties;
  117. while (a) {
  118. if (faux_str_casecmp((char *)a->name, attrname) == 0) {
  119. if (a->children && a->children->content)
  120. return (char *)a->children->content;
  121. else
  122. return NULL;
  123. }
  124. a = a->next;
  125. }
  126. return NULL;
  127. }
  128. void kxml_node_attr_free(char *str)
  129. {
  130. str = str; // Happy compiler
  131. // kxml_node_attr() doesn't allocate any memory
  132. // so we don't need to free()
  133. }
  134. char *kxml_node_content(const kxml_node_t *node)
  135. {
  136. xmlNode *n = (xmlNode *)node;
  137. xmlNode *c = NULL;
  138. char *content = NULL;
  139. if (!n)
  140. return NULL;
  141. c = n->children;
  142. while (c) {
  143. if ((c->type == XML_TEXT_NODE || c->type == XML_CDATA_SECTION_NODE)
  144. && !xmlIsBlankNode(c)) {
  145. faux_str_cat(&content, (char *)c->content);
  146. }
  147. c = c->next;
  148. }
  149. return content;
  150. }
  151. void kxml_node_content_free(char *str)
  152. {
  153. if (!str)
  154. return;
  155. faux_str_free(str);
  156. }
  157. char *kxml_node_name(const kxml_node_t *node)
  158. {
  159. xmlNode *n = (xmlNode *)node;
  160. if (!node)
  161. return NULL;
  162. return (char *)n->name;
  163. }
  164. void kxml_node_name_free(char *str)
  165. {
  166. str = str; // Happy compiler
  167. // kxml_node_name() doesn't allocate any memory
  168. // so we don't need to free()
  169. }