2 * Copyright (c) 2002, Index Data.
3 * See the file LICENSE for details.
5 * $Id: d1_expat.c,v 1.2 2002-05-21 07:43:16 adam Exp $
14 #include <yaz/xmalloc.h>
16 #include <yaz/data1.h>
21 data1_node *d1_stack[256];
27 static void cb_start (void *user, const char *el, const char **attr)
29 struct user_info *ui = (struct user_info*) user;
30 ui->d1_stack[ui->level] = data1_mk_tag (ui->dh, ui->nmem, el, attr,
31 ui->d1_stack[ui->level-1]);
33 printf ("cb_start %s\n", el);
36 static void cb_end (void *user, const char *el)
38 struct user_info *ui = (struct user_info*) user;
41 printf ("cb_end %s\n", el);
44 static void cb_chardata (void *user, const char *s, int len)
46 struct user_info *ui = (struct user_info*) user;
49 for (i = 0; i<len; i++)
50 if (!strchr ("\n\n ", s[i]))
54 ui->d1_stack[ui->level] = data1_mk_text_n (ui->dh, ui->nmem, s, len,
55 ui->d1_stack[ui->level -1]);
59 static void cb_decl (void *user, const char *version, const char*encoding,
62 printf ("decl version=%s encoding=%s\n", version ? version : "null",
63 encoding ? encoding : "null");
66 static void cb_processing (void *userData, const char *target,
69 printf ("decl processing target=%s data=%s\n", target ? target : "null",
70 data ? data : "null");
73 static void cb_comment (void *userData, const char *data)
75 printf ("decl comment data=%s\n", data ? data : "null");
78 static void cb_doctype_start (void *userData, const char *doctypeName,
79 const char *sysid, const char *pubid,
80 int has_internal_subset)
82 printf ("doctype start doctype=%s sysid=%s pubid=%s\n",
83 doctypeName, sysid, pubid);
86 static void cb_doctype_end (void *userData)
88 printf ("doctype end\n");
92 static void cb_entity_decl (void *userData, const char *entityName,
93 int is_parameter_entity,
94 const char *value, int value_length,
95 const char *base, const char *systemId,
96 const char *publicId, const char *notationName)
98 printf ("entity %s is_para_entry=%d value=%.*s base=%s systemId=%s\n"
99 " publicId=%s notationName=%s\n",
100 entityName, is_parameter_entity, value_length, value,
101 base, systemId, publicId, notationName);
105 #define XML_CHUNK 1024
107 data1_node *data1_read_xml (data1_handle dh,
108 int (*rf)(void *, char *, size_t), void *fh,
112 struct user_info uinfo;
118 uinfo.d1_stack[0] = data1_mk_root (dh, m, "root");
119 uinfo.d1_stack[1] = 0; /* indicate no children (see end of routine) */
121 parser = XML_ParserCreate (0 /* encoding */);
123 XML_SetElementHandler (parser, cb_start, cb_end);
124 XML_SetCharacterDataHandler (parser, cb_chardata);
125 XML_SetXmlDeclHandler (parser, cb_decl);
126 XML_SetProcessingInstructionHandler (parser, cb_processing);
127 XML_SetUserData (parser, &uinfo);
128 XML_SetCommentHandler (parser, cb_comment);
129 XML_SetDoctypeDeclHandler (parser, cb_doctype_start, cb_doctype_end);
130 XML_SetEntityDeclHandler (parser, cb_entity_decl);
135 void *buf = XML_GetBuffer (parser, XML_CHUNK);
141 r = (*rf)(fh, buf, XML_CHUNK);
149 XML_ParseBuffer (parser, r, done);
151 XML_ParserFree (parser);
152 if (!uinfo.d1_stack[1])
154 return uinfo.d1_stack[0];