shell_libxml2.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. clish_xmldoc_t *clish_xmldoc_read(const char *filename)
  58. {
  59. xmlDoc *doc;
  60. doc = xmlReadFile(filename, NULL, 0);
  61. return doc_to_xmldoc(doc);
  62. }
  63. void clish_xmldoc_release(clish_xmldoc_t *doc)
  64. {
  65. if (doc) {
  66. xmlFreeDoc(xmldoc_to_doc(doc));
  67. xmlCleanupParser();
  68. }
  69. }
  70. int clish_xmldoc_is_valid(clish_xmldoc_t *doc)
  71. {
  72. return doc != NULL;
  73. }
  74. int clish_xmldoc_error_caps(clish_xmldoc_t *doc)
  75. {
  76. return CLISH_XMLERR_NOCAPS;
  77. }
  78. int clish_xmldoc_get_err_line(clish_xmldoc_t *doc)
  79. {
  80. return -1;
  81. }
  82. int clish_xmldoc_get_err_col(clish_xmldoc_t *doc)
  83. {
  84. return -1;
  85. }
  86. const char *clish_xmldoc_get_err_msg(clish_xmldoc_t *doc)
  87. {
  88. return "";
  89. }
  90. int clish_xmlnode_get_type(clish_xmlnode_t *node)
  91. {
  92. if (node) {
  93. xmlNode *n = xmlnode_to_node(node);
  94. switch (n->type) {
  95. case XML_ELEMENT_NODE:
  96. return CLISH_XMLNODE_ELM;
  97. case XML_TEXT_NODE:
  98. return CLISH_XMLNODE_TEXT;
  99. case XML_COMMENT_NODE:
  100. return CLISH_XMLNODE_COMMENT;
  101. case XML_PI_NODE:
  102. return CLISH_XMLNODE_PI;
  103. case XML_ATTRIBUTE_NODE:
  104. return CLISH_XMLNODE_ATTR;
  105. default:
  106. break;
  107. }
  108. }
  109. return CLISH_XMLNODE_UNKNOWN;
  110. }
  111. clish_xmlnode_t *clish_xmldoc_get_root(clish_xmldoc_t *doc)
  112. {
  113. if (doc) {
  114. xmlNode *root = xmlDocGetRootElement(xmldoc_to_doc(doc));
  115. return node_to_xmlnode(root);
  116. }
  117. return NULL;
  118. }
  119. clish_xmlnode_t *clish_xmlnode_parent(clish_xmlnode_t *node)
  120. {
  121. if (node) {
  122. xmlNode *n = xmlnode_to_node(node);
  123. xmlNode *root = xmlDocGetRootElement(n->doc);
  124. if (n != root)
  125. return node_to_xmlnode(n->parent);
  126. }
  127. return NULL;
  128. }
  129. clish_xmlnode_t *clish_xmlnode_next_child(clish_xmlnode_t *parent,
  130. clish_xmlnode_t *curchild)
  131. {
  132. xmlNode *child;
  133. if (!parent)
  134. return NULL;
  135. if (curchild) {
  136. child = xmlnode_to_node(curchild)->next;
  137. } else {
  138. child = xmlnode_to_node(parent)->children;
  139. }
  140. return node_to_xmlnode(child);
  141. }
  142. char *clish_xmlnode_fetch_attr(clish_xmlnode_t *node,
  143. const char *attrname)
  144. {
  145. xmlNode *n;
  146. if (!node || !attrname)
  147. return NULL;
  148. n = xmlnode_to_node(node);
  149. if (n->type == XML_ELEMENT_NODE) {
  150. xmlAttr *a = n->properties;
  151. while (a) {
  152. if (strcmp((char*)a->name, attrname) == 0) {
  153. if (a->children && a->children->content)
  154. return (char *)a->children->content;
  155. else
  156. return NULL;
  157. }
  158. a = a->next;
  159. }
  160. }
  161. return NULL;
  162. }
  163. int clish_xmlnode_get_content(clish_xmlnode_t *node, char *content,
  164. unsigned int *contentlen)
  165. {
  166. xmlNode *n;
  167. xmlNode *c;
  168. int rlen = 0;
  169. if (content && contentlen && *contentlen)
  170. *content = 0;
  171. if (!node || !content || !contentlen)
  172. return -EINVAL;
  173. if (*contentlen <= 1)
  174. return -EINVAL;
  175. *content = 0;
  176. n = xmlnode_to_node(node);
  177. /* first, get the content length */
  178. c = n->children;
  179. while (c) {
  180. if (c->type == XML_TEXT_NODE && !xmlIsBlankNode(c)) {
  181. rlen += strlen((char*)c->content);
  182. }
  183. c = c->next;
  184. }
  185. ++rlen;
  186. if (rlen <= *contentlen) {
  187. c = n->children;
  188. while (c) {
  189. if (c->type == XML_TEXT_NODE && !xmlIsBlankNode(c)) {
  190. strcat(content, (char*)c->content);
  191. }
  192. c = c->next;
  193. }
  194. return 0;
  195. } else {
  196. *contentlen = rlen;
  197. return -E2BIG;
  198. }
  199. }
  200. int clish_xmlnode_get_name(clish_xmlnode_t *node, char *name,
  201. unsigned int *namelen)
  202. {
  203. int rlen;
  204. xmlNode *n;
  205. if (name && namelen && *namelen)
  206. *name = 0;
  207. if (!node || !name || !namelen)
  208. return -EINVAL;
  209. if (*namelen <= 1)
  210. return -EINVAL;
  211. *name = 0;
  212. n = xmlnode_to_node(node);
  213. rlen = strlen((char*)n->name) + 1;
  214. if (rlen <= *namelen) {
  215. snprintf(name, *namelen, "%s", (char*)n->name);
  216. name[*namelen - 1] = '\0';
  217. return 0;
  218. } else {
  219. *namelen = rlen;
  220. return -E2BIG;
  221. }
  222. }
  223. void clish_xmlnode_print(clish_xmlnode_t *node, FILE *out)
  224. {
  225. xmlNode *n;
  226. xmlAttr *a;
  227. n = xmlnode_to_node(node);
  228. if (n && n->name) {
  229. fprintf(out, "<%s", (char*)n->name);
  230. a = n->properties;
  231. while (a) {
  232. char *av = "";
  233. if (a->children && a->children->content)
  234. av = (char*)a->children->content;
  235. fprintf(out, " %s='%s'", (char*)a->name, av);
  236. a = a->next;
  237. }
  238. fprintf(out, ">");
  239. }
  240. }
  241. void clish_xml_release(void *p)
  242. {
  243. /* do we allocate memory? not yet. */
  244. }
  245. #ifdef HAVE_LIB_LIBXSLT
  246. static inline xsltStylesheet *xslt_to_xsltStylesheet(clish_xslt_t *xslt)
  247. {
  248. return (xsltStylesheet*)doc;
  249. }
  250. static inline clish_xslt_t *xsltStylesheet_to_xslt(xsltStylesheet *xslt)
  251. {
  252. return (clish_xslt_t*)xslt;
  253. }
  254. int clish_xslt_is_valid(clish_xslt_t *stylesheet)
  255. {
  256. return stylesheet != NULL;
  257. }
  258. clish_xmldoc_t *clish_xslt_apply(clish_xmldoc_t *xmldoc, clish_xslt_t *stylesheet)
  259. {
  260. xmlDoc *doc = xmldoc_to_doc(xmldoc);
  261. xsltStylesheetPtr cur = xslt_to_xsltStylesheet(stylesheet);
  262. xmlDoc *res;
  263. if (!doc || !cur)
  264. return doc_to_xmldoc(NULL);
  265. res = xsltApplyStylesheet(cur, doc, NULL);
  266. return doc_to_xmldoc(res);
  267. }
  268. clish_xslt_t *clish_xslt_read(const char *filename)
  269. {
  270. xsltStylesheet* cur = NULL;
  271. xmlSubstituteEntitiesDefault(1);
  272. xmlLoadExtDtdDefaultValue = 1;
  273. cur = xsltParseStylesheetFile((const xmlChar *)filename);
  274. return xsltStylesheet_to_xslt(cur);
  275. }
  276. void clish_xslt_release(clish_xslt_t *stylesheet)
  277. {
  278. xsltStylesheet* cur = xslt_to_xsltStylesheet(stylesheet);
  279. if (!cur)
  280. return;
  281. xsltFreeStylesheet(cur);
  282. xsltCleanupGlobals();
  283. }
  284. #endif /* HAVE_LIB_LIBXSLT */
  285. #endif /* HAVE_LIB_LIBXML2 */