shell_libxml2.c 7.1 KB

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