3 * synopsis: Example of custom Input/Output
4 * purpose: Demonstrate the use of xmlRegisterInputCallbacks
5 * to build a custom I/O layer, this is used in an
6 * XInclude method context to show how dynamic document can
7 * be built in a clean way.
9 * test: io1 > io1.tmp && diff io1.tmp $(srcdir)/io1.res
10 * author: Daniel Veillard
11 * copy: see Copyright for the status of this software.
16 #include <libxml/parser.h>
17 #include <libxml/tree.h>
18 #include <libxml/xinclude.h>
19 #include <libxml/xmlIO.h>
21 #ifdef LIBXML_XINCLUDE_ENABLED
22 static const char *result = "<list><people>a</people><people>b</people></list>";
23 static const char *cur = NULL;
28 * @URI: an URI to test
30 * Check for an sql: query
32 * Returns 1 if yes and 0 if another Input module should be used
35 sqlMatch(const char * URI) {
36 if ((URI != NULL) && (!strncmp(URI, "sql:", 4)))
43 * @URI: an URI to test
45 * Return a pointer to the sql: query handler, in this example simply
46 * the current pointer...
48 * Returns an Input context or NULL in case or error
51 sqlOpen(const char * URI) {
52 if ((URI == NULL) || (strncmp(URI, "sql:", 4)))
55 rlen = strlen(result);
61 * @context: the read context
63 * Close the sql: query handler
65 * Returns 0 or -1 in case of error
68 sqlClose(void * context) {
69 if (context == NULL) return(-1);
77 * @context: the read context
78 * @buffer: where to store data
79 * @len: number of bytes to read
81 * Implement an sql: query read.
83 * Returns the number of bytes read or -1 in case of error
86 sqlRead(void * context, char * buffer, int len) {
87 const char *ptr = (const char *) context;
89 if ((context == NULL) || (buffer == NULL) || (len < 0))
92 if (len > rlen) len = rlen;
93 memcpy(buffer, ptr, len);
98 const char *include = "<?xml version='1.0'?>\n\
99 <document xmlns:xi=\"http://www.w3.org/2003/XInclude\">\n\
100 <p>List of people:</p>\n\
101 <xi:include href=\"sql:select_name_from_people\"/>\n\
108 * this initialize the library and check potential ABI mismatches
109 * between the version it was compiled for and the actual shared
115 * register the new I/O handlers
117 if (xmlRegisterInputCallbacks(sqlMatch, sqlOpen, sqlRead, sqlClose) < 0) {
118 fprintf(stderr, "failed to register SQL handler\n");
122 * parse include into a document
124 doc = xmlReadMemory(include, strlen(include), "include.xml", NULL, 0);
126 fprintf(stderr, "failed to parse the including file\n");
131 * apply the XInclude process, this should trigger the I/O just
134 if (xmlXIncludeProcess(doc) <= 0) {
135 fprintf(stderr, "XInclude processing failed\n");
139 #ifdef LIBXML_OUTPUT_ENABLED
141 * save the output for checking to stdout
143 xmlDocDump(stdout, doc);
152 * Cleanup function for the XML library.
156 * this is to debug memory for regression tests
163 fprintf(stderr, "XInclude support not compiled in\n");