xmlapi.h 5.1 KB

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