shell_libxml2.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 clish_xml API using roxml
  8. * ------------------------------------------------------
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include "config.h"
  12. #endif
  13. #if defined(HAVE_LIB_LIBXML2)
  14. #include <errno.h>
  15. #include <string.h>
  16. #include <libxml/parser.h>
  17. #include <libxml/tree.h>
  18. #include "xmlapi.h"
  19. #ifdef HAVE_LIB_LIBXSLT
  20. #include <libxslt/xslt.h>
  21. #include <libxslt/xsltInternals.h>
  22. #include <libxslt/transform.h>
  23. #include <libxslt/xsltutils.h>
  24. extern int xmlLoadExtDtdDefaultValue;
  25. /* dummy stuff ; really a xsltStylesheet */
  26. struct clish_xslt_s {
  27. int dummy;
  28. };
  29. #endif
  30. /* dummy stuff ; really a xmlDoc */
  31. struct clish_xmldoc_s {
  32. int dummy;
  33. };
  34. /* dummy stuff ; really a xmlNode */
  35. struct clish_xmlnode_s {
  36. int dummy;
  37. };
  38. static inline xmlDoc *xmldoc_to_doc(clish_xmldoc_t *doc)
  39. {
  40. return (xmlDoc*)doc;
  41. }
  42. static inline xmlNode *xmlnode_to_node(clish_xmlnode_t *node)
  43. {
  44. return (xmlNode*)node;
  45. }
  46. static inline clish_xmldoc_t *doc_to_xmldoc(xmlDoc *node)
  47. {
  48. return (clish_xmldoc_t*)node;
  49. }
  50. static inline clish_xmlnode_t *node_to_xmlnode(xmlNode *node)
  51. {
  52. return (clish_xmlnode_t*)node;
  53. }
  54. /*
  55. * public interface
  56. */
  57. int clish_xmldoc_start(void)
  58. {
  59. #ifdef HAVE_LIB_LIBXSLT
  60. /* The XSLT example contain these settings but I doubt
  61. * it's really necessary.
  62. */
  63. /* xmlSubstituteEntitiesDefault(1);
  64. xmlLoadExtDtdDefaultValue = 1;
  65. */
  66. #endif
  67. return 0;
  68. }
  69. int clish_xmldoc_stop(void)
  70. {
  71. #ifdef HAVE_LIB_LIBXSLT
  72. xsltCleanupGlobals();
  73. #endif
  74. xmlCleanupParser();
  75. return 0;
  76. }
  77. clish_xmldoc_t *clish_xmldoc_read(const char *filename)
  78. {
  79. xmlDoc *doc;
  80. doc = xmlReadFile(filename, NULL, 0);
  81. return doc_to_xmldoc(doc);
  82. }
  83. void clish_xmldoc_release(clish_xmldoc_t *doc)
  84. {
  85. if (doc)
  86. xmlFreeDoc(xmldoc_to_doc(doc));
  87. }
  88. int clish_xmldoc_is_valid(clish_xmldoc_t *doc)
  89. {
  90. return doc != NULL;
  91. }
  92. int clish_xmldoc_error_caps(clish_xmldoc_t *doc)
  93. {
  94. return CLISH_XMLERR_NOCAPS;
  95. }
  96. int clish_xmldoc_get_err_line(clish_xmldoc_t *doc)
  97. {
  98. return -1;
  99. }
  100. int clish_xmldoc_get_err_col(clish_xmldoc_t *doc)
  101. {
  102. return -1;
  103. }
  104. const char *clish_xmldoc_get_err_msg(clish_xmldoc_t *doc)
  105. {
  106. return "";
  107. }
  108. int clish_xmlnode_get_type(clish_xmlnode_t *node)
  109. {
  110. if (node) {
  111. xmlNode *n = xmlnode_to_node(node);
  112. switch (n->type) {
  113. case XML_ELEMENT_NODE:
  114. return CLISH_XMLNODE_ELM;
  115. case XML_TEXT_NODE:
  116. return CLISH_XMLNODE_TEXT;
  117. case XML_COMMENT_NODE:
  118. return CLISH_XMLNODE_COMMENT;
  119. case XML_PI_NODE:
  120. return CLISH_XMLNODE_PI;
  121. case XML_ATTRIBUTE_NODE:
  122. return CLISH_XMLNODE_ATTR;
  123. default:
  124. break;
  125. }
  126. }
  127. return CLISH_XMLNODE_UNKNOWN;
  128. }
  129. clish_xmlnode_t *clish_xmldoc_get_root(clish_xmldoc_t *doc)
  130. {
  131. if (doc) {
  132. xmlNode *root = xmlDocGetRootElement(xmldoc_to_doc(doc));
  133. return node_to_xmlnode(root);
  134. }
  135. return NULL;
  136. }
  137. clish_xmlnode_t *clish_xmlnode_parent(clish_xmlnode_t *node)
  138. {
  139. if (node) {
  140. xmlNode *n = xmlnode_to_node(node);
  141. xmlNode *root = xmlDocGetRootElement(n->doc);
  142. if (n != root)
  143. return node_to_xmlnode(n->parent);
  144. }
  145. return NULL;
  146. }
  147. clish_xmlnode_t *clish_xmlnode_next_child(clish_xmlnode_t *parent,
  148. clish_xmlnode_t *curchild)
  149. {
  150. xmlNode *child;
  151. if (!parent)
  152. return NULL;
  153. if (curchild) {
  154. child = xmlnode_to_node(curchild)->next;
  155. } else {
  156. child = xmlnode_to_node(parent)->children;
  157. }
  158. return node_to_xmlnode(child);
  159. }
  160. char *clish_xmlnode_fetch_attr(clish_xmlnode_t *node,
  161. const char *attrname)
  162. {
  163. xmlNode *n;
  164. if (!node || !attrname)
  165. return NULL;
  166. n = xmlnode_to_node(node);
  167. if (n->type == XML_ELEMENT_NODE) {
  168. xmlAttr *a = n->properties;
  169. while (a) {
  170. if (strcmp((char*)a->name, attrname) == 0) {
  171. if (a->children && a->children->content)
  172. return (char *)a->children->content;
  173. else
  174. return NULL;
  175. }
  176. a = a->next;
  177. }
  178. }
  179. return NULL;
  180. }
  181. int clish_xmlnode_get_content(clish_xmlnode_t *node, char *content,
  182. unsigned int *contentlen)
  183. {
  184. xmlNode *n;
  185. xmlNode *c;
  186. int rlen = 0;
  187. if (content && contentlen && *contentlen)
  188. *content = 0;
  189. if (!node || !content || !contentlen)
  190. return -EINVAL;
  191. if (*contentlen <= 1)
  192. return -EINVAL;
  193. *content = 0;
  194. n = xmlnode_to_node(node);
  195. /* first, get the content length */
  196. c = n->children;
  197. while (c) {
  198. if (c->type == XML_TEXT_NODE && !xmlIsBlankNode(c)) {
  199. rlen += strlen((char*)c->content);
  200. }
  201. c = c->next;
  202. }
  203. ++rlen;
  204. if (rlen <= *contentlen) {
  205. c = n->children;
  206. while (c) {
  207. if (c->type == XML_TEXT_NODE && !xmlIsBlankNode(c)) {
  208. strcat(content, (char*)c->content);
  209. }
  210. c = c->next;
  211. }
  212. return 0;
  213. } else {
  214. *contentlen = rlen;
  215. return -E2BIG;
  216. }
  217. }
  218. int clish_xmlnode_get_name(clish_xmlnode_t *node, char *name,
  219. unsigned int *namelen)
  220. {
  221. int rlen;
  222. xmlNode *n;
  223. if (name && namelen && *namelen)
  224. *name = 0;
  225. if (!node || !name || !namelen)
  226. return -EINVAL;
  227. if (*namelen <= 1)
  228. return -EINVAL;
  229. *name = 0;
  230. n = xmlnode_to_node(node);
  231. rlen = strlen((char*)n->name) + 1;
  232. if (rlen <= *namelen) {
  233. snprintf(name, *namelen, "%s", (char*)n->name);
  234. name[*namelen - 1] = '\0';
  235. return 0;
  236. } else {
  237. *namelen = rlen;
  238. return -E2BIG;
  239. }
  240. }
  241. void clish_xmlnode_print(clish_xmlnode_t *node, FILE *out)
  242. {
  243. xmlNode *n;
  244. xmlAttr *a;
  245. n = xmlnode_to_node(node);
  246. if (n && n->name) {
  247. fprintf(out, "<%s", (char*)n->name);
  248. a = n->properties;
  249. while (a) {
  250. char *av = "";
  251. if (a->children && a->children->content)
  252. av = (char*)a->children->content;
  253. fprintf(out, " %s='%s'", (char*)a->name, av);
  254. a = a->next;
  255. }
  256. fprintf(out, ">");
  257. }
  258. }
  259. void clish_xml_release(void *p)
  260. {
  261. /* do we allocate memory? not yet. */
  262. }
  263. #ifdef HAVE_LIB_LIBXSLT
  264. static inline xsltStylesheet *xslt_to_xsltStylesheet(clish_xslt_t *xslt)
  265. {
  266. return (xsltStylesheet*)xslt;
  267. }
  268. static inline clish_xslt_t *xsltStylesheet_to_xslt(xsltStylesheet *xslt)
  269. {
  270. return (clish_xslt_t*)xslt;
  271. }
  272. int clish_xslt_is_valid(clish_xslt_t *stylesheet)
  273. {
  274. return stylesheet != NULL;
  275. }
  276. clish_xmldoc_t *clish_xslt_apply(clish_xmldoc_t *xmldoc, clish_xslt_t *stylesheet)
  277. {
  278. xmlDoc *doc = xmldoc_to_doc(xmldoc);
  279. xsltStylesheetPtr cur = xslt_to_xsltStylesheet(stylesheet);
  280. xmlDoc *res;
  281. if (!doc || !cur)
  282. return doc_to_xmldoc(NULL);
  283. res = xsltApplyStylesheet(cur, doc, NULL);
  284. return doc_to_xmldoc(res);
  285. }
  286. clish_xslt_t *clish_xslt_read(const char *filename)
  287. {
  288. xsltStylesheet* cur = NULL;
  289. cur = xsltParseStylesheetFile((const xmlChar *)filename);
  290. return xsltStylesheet_to_xslt(cur);
  291. }
  292. void clish_xslt_release(clish_xslt_t *stylesheet)
  293. {
  294. xsltStylesheet* cur = xslt_to_xsltStylesheet(stylesheet);
  295. if (!cur)
  296. return;
  297. xsltFreeStylesheet(cur);
  298. }
  299. #endif /* HAVE_LIB_LIBXSLT */
  300. #endif /* HAVE_LIB_LIBXML2 */