xmlapi.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * xmlapi.h
  3. *
  4. * private klish file: internal XML API
  5. */
  6. #ifndef clish_xmlapi_included_h
  7. #define clish_xmlapi_included_h
  8. #ifdef HAVE_CONFIG_H
  9. #include "config.h"
  10. #endif
  11. #include <stdlib.h>
  12. #include <errno.h>
  13. #include <stdio.h> /* need for FILE */
  14. /*
  15. * XML document (opaque type)
  16. * The real type is defined by the selected external API
  17. */
  18. typedef struct clish_xmldoc_s clish_xmldoc_t;
  19. /*
  20. * XML node (opaque type)
  21. * The real type is defined by the selected external API
  22. */
  23. typedef struct clish_xmlnode_s clish_xmlnode_t;
  24. /*
  25. * Start and Stop XML parser engine.
  26. * Some parsers need a global cleanup at the end of the programm.
  27. */
  28. int clish_xmldoc_start(void);
  29. int clish_xmldoc_stop(void);
  30. /*
  31. * read an XML document
  32. */
  33. clish_xmldoc_t *clish_xmldoc_read(const char *filename);
  34. /*
  35. * release a previously opened XML document
  36. */
  37. void clish_xmldoc_release(clish_xmldoc_t *doc);
  38. /*
  39. * check if a doc is valid (i.e. it loaded successfully)
  40. */
  41. int clish_xmldoc_is_valid(clish_xmldoc_t *doc);
  42. /*
  43. * XML implementation error capabilitiess
  44. * The real capabilities is or'ed using the following
  45. * constants
  46. */
  47. typedef enum {
  48. CLISH_XMLERR_NOCAPS = 0,
  49. CLISH_XMLERR_LINE = 0x10,
  50. CLISH_XMLERR_COL = 0x20,
  51. CLISH_XMLERR_DESC = 0x40
  52. } clish_xmlerrcaps_e;
  53. /*
  54. * does this specific implementation define any error?
  55. * -> get the capabilities
  56. */
  57. int clish_xmldoc_error_caps(clish_xmldoc_t *doc);
  58. typedef enum {
  59. CLISH_XMLNODE_DOC,
  60. CLISH_XMLNODE_ELM,
  61. CLISH_XMLNODE_TEXT,
  62. CLISH_XMLNODE_ATTR,
  63. CLISH_XMLNODE_COMMENT,
  64. CLISH_XMLNODE_PI,
  65. CLISH_XMLNODE_DECL,
  66. CLISH_XMLNODE_UNKNOWN,
  67. } clish_xmlnodetype_e;
  68. /*
  69. * get error description, when available
  70. */
  71. int clish_xmldoc_get_err_line(clish_xmldoc_t *doc);
  72. int clish_xmldoc_get_err_col(clish_xmldoc_t *doc);
  73. const char *clish_xmldoc_get_err_msg(clish_xmldoc_t *doc);
  74. /*
  75. * get the node type
  76. */
  77. int clish_xmlnode_get_type(clish_xmlnode_t *node);
  78. /*
  79. * get the document root
  80. */
  81. clish_xmlnode_t *clish_xmldoc_get_root(clish_xmldoc_t *doc);
  82. /*
  83. * get the next child or NULL. If curchild is NULL,
  84. * then the function returns the first child.
  85. */
  86. clish_xmlnode_t *clish_xmlnode_next_child(
  87. clish_xmlnode_t *parent,
  88. clish_xmlnode_t *curchild);
  89. /*
  90. * get the parent node.
  91. * returns NULL if node is the document root node.
  92. */
  93. clish_xmlnode_t *clish_xmlnode_parent(clish_xmlnode_t *node);
  94. /*
  95. * get the node name.
  96. * neither name not namelen shall be NULL. *namelen is the length of the
  97. * name buffer. If it's too small, we return -E2BIG and set *namelen to
  98. * the minimum length value.
  99. * returns < 0 on error. On error, name shall not be modified.
  100. */
  101. int clish_xmlnode_get_name(
  102. clish_xmlnode_t *node,
  103. char *name,
  104. unsigned int *namelen);
  105. /*
  106. * get the node name
  107. * dynamically allocate the buffer (it must be freed once you don't need it
  108. * anymore) that will contain all the content of the node.
  109. * return NULL on error.
  110. */
  111. static inline char* clish_xmlnode_get_all_name(clish_xmlnode_t *node)
  112. {
  113. char *name = NULL;
  114. unsigned int nlen = 2048;
  115. int result;
  116. do {
  117. name = (char*)realloc(name, nlen);
  118. result = clish_xmlnode_get_name(node, name, &nlen);
  119. } while (result == -E2BIG);
  120. if (result < 0) {
  121. free(name);
  122. return NULL;
  123. }
  124. return name;
  125. }
  126. /*
  127. * get the node content.
  128. * neither content not contentlen shall be NULL. *contentlen is the length
  129. * of the content buffer. If it's too small, we return -E2BIG and set
  130. * *contentlen to the minimum length value (including space for the \0
  131. * character) so that two subsequent calls to this functions are going
  132. * to succeed if the forst one failed because of a too small buffer.
  133. * returns < 0 on error. On error, content shall not be modified.
  134. */
  135. int clish_xmlnode_get_content(
  136. clish_xmlnode_t *node,
  137. char *content,
  138. unsigned int *contentlen);
  139. /*
  140. * get the node content
  141. * dynamically allocate the buffer (it must be freed once you don't need it
  142. * anymore) that will contain all the content of the node.
  143. * return NULL on error.
  144. */
  145. static inline char* clish_xmlnode_get_all_content(clish_xmlnode_t *node)
  146. {
  147. char *content = NULL;
  148. unsigned int clen = 2048;
  149. int result;
  150. do {
  151. content = (char*)realloc(content, clen);
  152. result = clish_xmlnode_get_content(node, content, &clen);
  153. } while (result == -E2BIG);
  154. if (result < 0) {
  155. free(content);
  156. return NULL;
  157. }
  158. return content;
  159. }
  160. /*
  161. * get an attribute by name. May return NULL if the
  162. * attribute is not found
  163. * Special: allocate memory (to free with clish_xml_release())
  164. */
  165. char *clish_xmlnode_fetch_attr(
  166. clish_xmlnode_t *node,
  167. const char *attrname);
  168. /*
  169. * Free a pointer allocated by the XML backend
  170. */
  171. void clish_xml_release(void *p);
  172. /*
  173. * print an XML node to the out file
  174. */
  175. void clish_xmlnode_print(clish_xmlnode_t *node, FILE *out);
  176. #ifdef HAVE_LIB_LIBXSLT
  177. /*
  178. * XSLT stylesheet (opaque type)
  179. * The real type is defined by the selected external API
  180. */
  181. typedef struct clish_xslt_s clish_xslt_t;
  182. /*
  183. * Load an XSLT stylesheet
  184. */
  185. clish_xslt_t *clish_xslt_read(const char *filename);
  186. /*
  187. * Load an embedded XSLT stylesheet from already
  188. * loaded XML document.
  189. */
  190. clish_xslt_t *clish_xslt_read_embedded(clish_xmldoc_t *xmldoc);
  191. /* Apply XSLT stylesheet */
  192. clish_xmldoc_t *clish_xslt_apply(clish_xmldoc_t *xmldoc, clish_xslt_t *stylesheet);
  193. /*
  194. * Release a previously opened XSLT stylesheet
  195. */
  196. void clish_xslt_release(clish_xslt_t *stylesheet);
  197. /*
  198. * Check if a stylesheet is valid (i.e. it loaded successfully)
  199. */
  200. int clish_xslt_is_valid(clish_xslt_t *stylesheet);
  201. #endif /* HAVE_LIB_LIBXSLT */
  202. #endif /* clish_xmlapi_included_h */