1
0

shell_libxml2.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 || c->type == XML_CDATA_SECTION_NODE)
  199. && !xmlIsBlankNode(c)) {
  200. rlen += strlen((char*)c->content);
  201. }
  202. c = c->next;
  203. }
  204. ++rlen;
  205. if (rlen <= *contentlen) {
  206. c = n->children;
  207. while (c) {
  208. if ((c->type == XML_TEXT_NODE || c->type == XML_CDATA_SECTION_NODE)
  209. && !xmlIsBlankNode(c)) {
  210. strcat(content, (char*)c->content);
  211. }
  212. c = c->next;
  213. }
  214. return 0;
  215. } else {
  216. *contentlen = rlen;
  217. return -E2BIG;
  218. }
  219. }
  220. int clish_xmlnode_get_name(clish_xmlnode_t *node, char *name,
  221. unsigned int *namelen)
  222. {
  223. int rlen;
  224. xmlNode *n;
  225. if (name && namelen && *namelen)
  226. *name = 0;
  227. if (!node || !name || !namelen)
  228. return -EINVAL;
  229. if (*namelen <= 1)
  230. return -EINVAL;
  231. *name = 0;
  232. n = xmlnode_to_node(node);
  233. rlen = strlen((char*)n->name) + 1;
  234. if (rlen <= *namelen) {
  235. snprintf(name, *namelen, "%s", (char*)n->name);
  236. name[*namelen - 1] = '\0';
  237. return 0;
  238. } else {
  239. *namelen = rlen;
  240. return -E2BIG;
  241. }
  242. }
  243. void clish_xmlnode_print(clish_xmlnode_t *node, FILE *out)
  244. {
  245. xmlNode *n;
  246. xmlAttr *a;
  247. n = xmlnode_to_node(node);
  248. if (n && n->name) {
  249. fprintf(out, "<%s", (char*)n->name);
  250. a = n->properties;
  251. while (a) {
  252. char *av = "";
  253. if (a->children && a->children->content)
  254. av = (char*)a->children->content;
  255. fprintf(out, " %s='%s'", (char*)a->name, av);
  256. a = a->next;
  257. }
  258. fprintf(out, ">");
  259. }
  260. }
  261. void clish_xml_release(void *p)
  262. {
  263. /* do we allocate memory? not yet. */
  264. }
  265. #ifdef HAVE_LIB_LIBXSLT
  266. static inline xsltStylesheet *xslt_to_xsltStylesheet(clish_xslt_t *xslt)
  267. {
  268. return (xsltStylesheet*)xslt;
  269. }
  270. static inline clish_xslt_t *xsltStylesheet_to_xslt(xsltStylesheet *xslt)
  271. {
  272. return (clish_xslt_t*)xslt;
  273. }
  274. int clish_xslt_is_valid(clish_xslt_t *stylesheet)
  275. {
  276. return stylesheet != NULL;
  277. }
  278. clish_xmldoc_t *clish_xslt_apply(clish_xmldoc_t *xmldoc, clish_xslt_t *stylesheet)
  279. {
  280. xmlDoc *doc = xmldoc_to_doc(xmldoc);
  281. xsltStylesheetPtr cur = xslt_to_xsltStylesheet(stylesheet);
  282. xmlDoc *res;
  283. if (!doc || !cur)
  284. return doc_to_xmldoc(NULL);
  285. res = xsltApplyStylesheet(cur, doc, NULL);
  286. return doc_to_xmldoc(res);
  287. }
  288. clish_xslt_t *clish_xslt_read(const char *filename)
  289. {
  290. xsltStylesheet* cur = NULL;
  291. cur = xsltParseStylesheetFile((const xmlChar *)filename);
  292. return xsltStylesheet_to_xslt(cur);
  293. }
  294. clish_xslt_t *clish_xslt_read_embedded(clish_xmldoc_t *xmldoc)
  295. {
  296. xsltStylesheet* cur = NULL;
  297. xmlDoc *doc = xmldoc_to_doc(xmldoc);
  298. cur = xsltLoadStylesheetPI(doc);
  299. return xsltStylesheet_to_xslt(cur);
  300. }
  301. void clish_xslt_release(clish_xslt_t *stylesheet)
  302. {
  303. xsltStylesheet* cur = xslt_to_xsltStylesheet(stylesheet);
  304. if (!cur)
  305. return;
  306. xsltFreeStylesheet(cur);
  307. }
  308. #endif /* HAVE_LIB_LIBXSLT */
  309. #endif /* HAVE_LIB_LIBXML2 */