genx.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * genx - C-callable library for generating XML documents
  3. */
  4. /*
  5. * Copyright (c) 2007-2013 Code Synthesis Tools CC.
  6. * Copyright (c) 2004 by Tim Bray and Sun Microsystems.
  7. *
  8. * For copying permission, see the accompanying COPYING file.
  9. */
  10. #ifndef GENX_H
  11. #define GENX_H
  12. #include <stddef.h> /* size_t */
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /*
  17. * Note on error handling: genx routines mostly return
  18. * GENX_SUCCESS (guaranteed to be zero) in normal circumstances, one of
  19. * these other GENX_ values on a memory allocation or I/O failure or if the
  20. * call would result in non-well-formed output.
  21. * You can associate an error message with one of these codes explicitly
  22. * or with the most recent error using genxGetErrorMessage() and
  23. * genxLastErrorMessage(); see below.
  24. */
  25. typedef enum
  26. {
  27. GENX_SUCCESS = 0,
  28. GENX_BAD_UTF8,
  29. GENX_NON_XML_CHARACTER,
  30. GENX_BAD_NAME,
  31. GENX_ALLOC_FAILED,
  32. GENX_BAD_NAMESPACE_NAME,
  33. GENX_INTERNAL_ERROR,
  34. GENX_DUPLICATE_PREFIX,
  35. GENX_SEQUENCE_ERROR,
  36. GENX_NO_START_TAG,
  37. GENX_IO_ERROR,
  38. GENX_MISSING_VALUE,
  39. GENX_MALFORMED_COMMENT,
  40. GENX_XML_PI_TARGET,
  41. GENX_MALFORMED_PI,
  42. GENX_DUPLICATE_ATTRIBUTE,
  43. GENX_ATTRIBUTE_IN_DEFAULT_NAMESPACE,
  44. GENX_DUPLICATE_NAMESPACE,
  45. GENX_BAD_DEFAULT_DECLARATION
  46. } genxStatus;
  47. /* character types */
  48. #define GENX_XML_CHAR 1
  49. #define GENX_LETTER 2
  50. #define GENX_NAMECHAR 4
  51. /* The size of the character table. Valid values are 0x100 (first 255
  52. chars are checked) and 0x10000 (all chars are checked). */
  53. #ifndef GENX_CHAR_TABLE_SIZE
  54. # define GENX_CHAR_TABLE_SIZE 0x100
  55. #endif
  56. /* a UTF-8 string */
  57. typedef unsigned char * utf8;
  58. typedef const unsigned char * constUtf8;
  59. /*
  60. * genx's own types
  61. */
  62. typedef struct genxWriter_rec * genxWriter;
  63. typedef struct genxNamespace_rec * genxNamespace;
  64. typedef struct genxElement_rec * genxElement;
  65. typedef struct genxAttribute_rec * genxAttribute;
  66. typedef void * (*genxAlloc) (void * userData, size_t bytes);
  67. typedef void (*genxDealloc) (void * userData, void* data);
  68. /*
  69. * Constructors, set/get
  70. */
  71. /*
  72. * Create a new writer. For generating multiple XML documents, it's most
  73. * efficient to re-use the same genx object. However, you can only write
  74. * one document at a time with a writer.
  75. * Returns NULL if it fails, which can only be due to an allocation failure.
  76. */
  77. genxWriter genxNew(genxAlloc alloc, genxDealloc dealloc, void * userData);
  78. /*
  79. * Reset the writer object back into usable state after an error or
  80. * interruption.
  81. */
  82. genxStatus genxReset (genxWriter w);
  83. /*
  84. * Dispose of a writer, freeing all associated memory
  85. */
  86. void genxDispose(genxWriter w);
  87. /*
  88. * Set/get
  89. */
  90. /*
  91. * The userdata pointer will be passed to memory-allocation
  92. * and I/O callbacks. If not set, genx will pass NULL
  93. */
  94. void genxSetUserData(genxWriter w, void * userData);
  95. void * genxGetUserData(genxWriter w);
  96. /*
  97. * Set/get pretty-printing. If indentation is set to 0, then no pretty-
  98. * printing is performed.
  99. */
  100. genxStatus genxSetPrettyPrint(genxWriter w, int indentation);
  101. int genxGetPrettyPrint(genxWriter w);
  102. /*
  103. * Set/get canonicalization. If true, then output explicit closing
  104. * tags and sort attributes. Default is false.
  105. */
  106. genxStatus genxSetCanonical(genxWriter w, int flag);
  107. int genxGetCanonical(genxWriter w);
  108. /*
  109. * User-provided memory allocator, if desired. For example, if you were
  110. * in an Apache module, you could arrange for genx to use ap_palloc by
  111. * making the pool accessible via the userData call.
  112. * The "dealloc" is to be used to free memory allocated with "alloc". If
  113. * alloc is provided but dealloc is NULL, genx will not attempt to free
  114. * the memory; this would be appropriate in an Apache context.
  115. * If "alloc" is not provided, genx routines use malloc() to allocate memory
  116. */
  117. void genxSetAlloc(genxWriter w, genxAlloc alloc);
  118. void genxSetDealloc(genxWriter w, genxDealloc dealloc);
  119. genxAlloc genxGetAlloc(genxWriter w);
  120. genxDealloc genxGetDealloc(genxWriter w);
  121. /*
  122. * Get the prefix associated with a namespace
  123. */
  124. utf8 genxGetNamespacePrefix(genxNamespace ns);
  125. /*
  126. * Declaration functions
  127. */
  128. /*
  129. * Declare a namespace. The provided prefix is the default but can be
  130. * overridden by genxAddNamespace. If no default prefiix is provided,
  131. * genx will generate one of the form g-%d.
  132. * On error, returns NULL and signals via statusp
  133. */
  134. genxNamespace genxDeclareNamespace(genxWriter w,
  135. constUtf8 uri, constUtf8 prefix,
  136. genxStatus * statusP);
  137. /*
  138. * Declare an element
  139. * If something failed, returns NULL and sets the status code via statusP
  140. */
  141. genxElement genxDeclareElement(genxWriter w,
  142. genxNamespace ns, constUtf8 type,
  143. genxStatus * statusP);
  144. /*
  145. * Declare an attribute
  146. */
  147. genxAttribute genxDeclareAttribute(genxWriter w,
  148. genxNamespace ns,
  149. constUtf8 name, genxStatus * statusP);
  150. /*
  151. * Writing XML
  152. */
  153. /*
  154. * Caller-provided I/O package.
  155. * First form is for a null-terminated string.
  156. * for second, if you have s="abcdef" and want to send "abc", you'd call
  157. * sendBounded(userData, s, s + 3)
  158. */
  159. typedef struct
  160. {
  161. genxStatus (* send)(void * userData, constUtf8 s);
  162. genxStatus (* sendBounded)(void * userData, constUtf8 start, constUtf8 end);
  163. genxStatus (* flush)(void * userData);
  164. } genxSender;
  165. genxStatus genxStartDocSender(genxWriter w, genxSender * sender);
  166. /*
  167. * End a document. Calls "flush"
  168. */
  169. genxStatus genxEndDocument(genxWriter w);
  170. /*
  171. * Write XML declaration. If encoding or standalone are NULL, then those
  172. * attributes are omitted.
  173. */
  174. genxStatus genxXmlDeclaration(genxWriter w,
  175. constUtf8 version,
  176. constUtf8 encoding,
  177. constUtf8 standalone);
  178. /*
  179. * Write a comment
  180. */
  181. genxStatus genxComment(genxWriter w, constUtf8 text);
  182. /*
  183. * Write a PI
  184. */
  185. genxStatus genxPI(genxWriter w, constUtf8 target, constUtf8 text);
  186. /*
  187. * Start an element
  188. */
  189. genxStatus genxStartElementLiteral(genxWriter w,
  190. constUtf8 xmlns, constUtf8 type);
  191. /*
  192. * Start a predeclared element
  193. * - element must have been declared
  194. */
  195. genxStatus genxStartElement(genxElement e);
  196. /*
  197. * Write an attribute
  198. */
  199. genxStatus genxAddAttributeLiteral(genxWriter w, constUtf8 xmlns,
  200. constUtf8 name, constUtf8 value);
  201. /*
  202. * Start an attribute
  203. */
  204. genxStatus genxStartAttributeLiteral(genxWriter w,
  205. constUtf8 xmlns, constUtf8 name);
  206. /*
  207. * Write a predeclared attribute
  208. */
  209. genxStatus genxAddAttribute(genxAttribute a, constUtf8 value);
  210. /*
  211. * Start a predeclared attribute
  212. */
  213. genxStatus genxStartAttribute(genxAttribute a);
  214. /*
  215. * End an attribute
  216. */
  217. genxStatus genxEndAttribute(genxWriter w);
  218. /*
  219. * add a namespace declaration
  220. */
  221. genxStatus genxAddNamespaceLiteral(genxWriter w,
  222. constUtf8 uri, constUtf8 prefix);
  223. /*
  224. * add a predefined namespace declaration
  225. */
  226. genxStatus genxAddNamespace(genxNamespace ns, constUtf8 prefix);
  227. /*
  228. * Clear default namespace declaration
  229. */
  230. genxStatus genxUnsetDefaultNamespace(genxWriter w);
  231. /*
  232. * Write an end tag
  233. */
  234. genxStatus genxEndElement(genxWriter w);
  235. /*
  236. * Write some text
  237. * You can't write any text outside the root element, except with
  238. * genxComment and genxPI.
  239. */
  240. genxStatus genxAddText(genxWriter w, constUtf8 start);
  241. genxStatus genxAddCountedText(genxWriter w, constUtf8 start, size_t byteCount);
  242. genxStatus genxAddBoundedText(genxWriter w, constUtf8 start, constUtf8 end);
  243. /*
  244. * Write one character. The integer value is the Unicode character
  245. * value, as usually expressed in U+XXXX notation.
  246. */
  247. genxStatus genxAddCharacter(genxWriter w, int c);
  248. /*
  249. * Utility routines
  250. */
  251. /*
  252. * Return the Unicode character encoded by the UTF-8 pointed-to by the
  253. * argument, and advance the argument past the encoding of the character.
  254. * Returns -1 if the UTF-8 is malformed, in which case advances the
  255. * argument to point at the first byte past the point past the malformed
  256. * ones.
  257. */
  258. int genxNextUnicodeChar(constUtf8 * sp);
  259. /*
  260. * Scan a buffer allegedly full of UTF-8 encoded XML characters; return
  261. * one of GENX_SUCCESS, GENX_BAD_UTF8, or GENX_NON_XML_CHARACTER
  262. */
  263. genxStatus genxCheckText(genxWriter w, constUtf8 s);
  264. /*
  265. * return character status, the OR of GENX_XML_CHAR,
  266. * GENX_LETTER, and GENX_NAMECHAR
  267. */
  268. int genxCharClass(genxWriter w, int c);
  269. /*
  270. * Silently wipe any non-XML characters out of a chunk of text.
  271. * If you call this on a string before you pass it addText or
  272. * addAttribute, you will never get an error from genx unless
  273. * (a) there's a bug in your software, e.g. a malformed element name, or
  274. * (b) there's a memory allocation or I/O error
  275. * The output can never be longer than the input.
  276. * Returns true if any changes were made.
  277. */
  278. int genxScrubText(genxWriter w, constUtf8 in, utf8 out);
  279. /*
  280. * return error messages
  281. */
  282. char * genxGetErrorMessage(genxWriter w, genxStatus status);
  283. char * genxLastErrorMessage(genxWriter w);
  284. /*
  285. * return version
  286. */
  287. char * genxGetVersion();
  288. #ifdef __cplusplus
  289. }
  290. #endif
  291. #endif /* GENX_H */