Debian Codegraph @

cutl/details/genx/genx.h

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