1
0

roxml_api.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * ------------------------------------------------------
  3. * shell_roxml.c
  4. *
  5. * This file implements the means to read an XML encoded file
  6. * and populate the CLI tree based on the contents. It implements
  7. * the kxml_ API using roxml
  8. * ------------------------------------------------------
  9. */
  10. #include <errno.h>
  11. #include <roxml.h>
  12. #include <string.h>
  13. #include <faux/faux.h>
  14. #include <faux/str.h>
  15. #include <klish/kxml.h>
  16. bool_t kxml_doc_start(void)
  17. {
  18. return BOOL_TRUE;
  19. }
  20. bool_t kxml_doc_stop(void)
  21. {
  22. return BOOL_TRUE;
  23. }
  24. kxml_doc_t *kxml_doc_read(const char *filename)
  25. {
  26. node_t *doc = roxml_load_doc((char *)filename);
  27. return (kxml_doc_t *)doc;
  28. }
  29. void kxml_doc_release(kxml_doc_t *doc)
  30. {
  31. if (!doc)
  32. return;
  33. roxml_release(RELEASE_ALL);
  34. roxml_close((node_t *)doc);
  35. }
  36. bool_t kxml_doc_is_valid(const kxml_doc_t *doc)
  37. {
  38. return (bool_t)(doc != NULL);
  39. }
  40. /*
  41. int kxml_doc_error_caps(kxml_doc_t *doc)
  42. {
  43. return kxml_ERR_NOCAPS;
  44. }
  45. int kxml_doc_get_err_line(kxml_doc_t *doc)
  46. {
  47. return -1;
  48. }
  49. int kxml_doc_get_err_col(kxml_doc_t *doc)
  50. {
  51. return -1;
  52. }
  53. const char *kxml_doc_get_err_msg(kxml_doc_t *doc)
  54. {
  55. return "";
  56. }
  57. */
  58. kxml_nodetype_e kxml_node_type(const kxml_node_t *node)
  59. {
  60. int type = 0;
  61. if (!node)
  62. return KXML_NODE_UNKNOWN;
  63. type = roxml_get_type((node_t *)node);
  64. switch (type) {
  65. case ROXML_ELM_NODE:
  66. return KXML_NODE_ELM;
  67. case ROXML_TXT_NODE:
  68. return KXML_NODE_TEXT;
  69. case ROXML_CMT_NODE:
  70. return KXML_NODE_COMMENT;
  71. case ROXML_PI_NODE:
  72. return KXML_NODE_PI;
  73. case ROXML_ATTR_NODE:
  74. return KXML_NODE_ATTR;
  75. default:
  76. break;
  77. }
  78. return KXML_NODE_UNKNOWN;
  79. }
  80. kxml_node_t *kxml_doc_root(const kxml_doc_t *doc)
  81. {
  82. node_t *root = NULL;
  83. char *name = NULL;
  84. if (!doc)
  85. return NULL;
  86. root = roxml_get_root((node_t *)doc);
  87. if (!root)
  88. return NULL;
  89. /* The root node is always documentRoot since libroxml-2.2.2. */
  90. /* It's good but not compatible with another XML parsers. */
  91. name = roxml_get_name(root, NULL, 0);
  92. if (0 == strcmp(name, "documentRoot"))
  93. root = roxml_get_chld(root, NULL, 0);
  94. roxml_release(name);
  95. return (kxml_node_t *)root;
  96. }
  97. kxml_node_t *kxml_node_parent(const kxml_node_t *node)
  98. {
  99. node_t *roxn = (node_t *)node;
  100. node_t *root = NULL;
  101. if (!node)
  102. return NULL;
  103. root = roxml_get_root(roxn);
  104. if (roxn != root)
  105. return (kxml_node_t *)roxml_get_parent(roxn);
  106. return NULL;
  107. }
  108. const kxml_node_t *kxml_node_next_child(const kxml_node_t *parent,
  109. const kxml_node_t *curchild)
  110. {
  111. node_t *roxc = (node_t *)curchild;
  112. node_t *roxp = (node_t *)parent;
  113. node_t *child = NULL;
  114. int count = 0;
  115. if (!parent)
  116. return NULL;
  117. if (roxc)
  118. return (kxml_node_t *)
  119. roxml_get_next_sibling(roxc);
  120. count = roxml_get_chld_nb(roxp);
  121. if (count)
  122. child = roxml_get_chld(roxp, NULL, 0);
  123. return (kxml_node_t *)child;
  124. }
  125. static int i_is_needle(char *src, const char *needle)
  126. {
  127. int nlen = strlen(needle);
  128. int slen = strlen(src);
  129. if (slen >= nlen) {
  130. if (strncmp(src, needle, nlen) == 0)
  131. return 1;
  132. }
  133. return 0;
  134. }
  135. /* warning: dst == src is valid */
  136. static void i_decode_and_copy(char *dst, char *src)
  137. {
  138. while (*src) {
  139. if (*src == '&') {
  140. if (i_is_needle(src, "&lt;")) {
  141. *dst++ = '<';
  142. src += 4;
  143. } else if (i_is_needle(src, "&gt;")) {
  144. *dst++ = '>';
  145. src += 4;
  146. } else if (i_is_needle(src, "&amp;")) {
  147. *dst++ = '&';
  148. src += 5;
  149. } else {
  150. *dst++ = *src++;
  151. }
  152. } else {
  153. *dst++ = *src++;
  154. }
  155. }
  156. *dst++ = 0;
  157. }
  158. char *kxml_node_attr(const kxml_node_t *node,
  159. const char *attrname)
  160. {
  161. node_t *roxn = (node_t *)node;
  162. node_t *attr = NULL;
  163. char *content = NULL;
  164. if (!node || !attrname)
  165. return NULL;
  166. attr = roxml_get_attr(roxn, (char *)attrname, 0);
  167. content = roxml_get_content(attr, NULL, 0, NULL);
  168. if (content)
  169. i_decode_and_copy(content, content);
  170. return content;
  171. }
  172. void kxml_node_attr_free(char *str)
  173. {
  174. if (!str)
  175. return;
  176. roxml_release(str);
  177. }
  178. char *kxml_node_content(const kxml_node_t *node)
  179. {
  180. char *content = NULL;
  181. if (!node)
  182. return NULL;
  183. content = roxml_get_content((node_t *)node, NULL, 0, NULL);
  184. if (content)
  185. i_decode_and_copy(content, content);
  186. return content;
  187. }
  188. void kxml_node_content_free(char *str)
  189. {
  190. if (!str)
  191. return;
  192. roxml_release(str);
  193. }
  194. char *kxml_node_name(const kxml_node_t *node)
  195. {
  196. if (!node)
  197. return NULL;
  198. return roxml_get_name((node_t *)node, NULL, 0);
  199. }
  200. void kxml_node_name_free(char *str)
  201. {
  202. if (!str)
  203. return;
  204. roxml_release(str);
  205. }