X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=sherlock%2Fxml%2Fxml-test.c;h=f6738c56db4164cd3851dfdd7c7351ebeac88812;hb=6c0e6f3b91ed669384f7fe894e4bb4891ffa6440;hp=a7ecda83a13e9b589cdad600320db4983a3c77b2;hpb=4041eceea24b578a0c5edaf0b82361283e6bbafb;p=libucw.git diff --git a/sherlock/xml/xml-test.c b/sherlock/xml/xml-test.c index a7ecda83..f6738c56 100644 --- a/sherlock/xml/xml-test.c +++ b/sherlock/xml/xml-test.c @@ -1,7 +1,7 @@ /* * Sherlock Library -- A simple XML parser * - * (c) 2007 Pavel Charvat + * (c) 2007--2008 Pavel Charvat * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -9,30 +9,37 @@ #include "sherlock/sherlock.h" #include "sherlock/xml/xml.h" -#include "lib/getopt.h" -#include "lib/fastbuf.h" +#include "sherlock/xml/dtd.h" +#include "ucw/getopt.h" +#include "ucw/fastbuf.h" #include #include +#include enum { WANT_FIRST = 0x100, WANT_HIDE_ERRORS, - WANT_UNFOLD_CDATA, WANT_IGNORE_COMMENTS, WANT_IGNORE_PIS, + WANT_REPORT_BLOCKS, + WANT_REPORT_IGNORABLE, + WANT_FILE_ENTITIES, }; -static char *shortopts = "spd" CF_SHORT_OPTS; +static char *shortopts = "spdt" CF_SHORT_OPTS; static struct option longopts[] = { CF_LONG_OPTS { "sax", 0, 0, 's' }, { "pull", 0, 0, 'p' }, - { "dom", 0, 0, 'd' }, + { "dom", 0, 0, 't' }, + { "dtd", 0, 0, 'd' }, { "hide-errors", 0, 0, WANT_HIDE_ERRORS }, - { "unfold-cdata", 0, 0, WANT_UNFOLD_CDATA }, { "ignore-comments", 0, 0, WANT_IGNORE_COMMENTS }, { "ignore-pis", 0, 0, WANT_IGNORE_PIS }, + { "report-blocks", 0, 0, WANT_REPORT_BLOCKS }, + { "report-ignorable", 0, 0, WANT_REPORT_IGNORABLE }, + { "file-entities", 0, 0, WANT_FILE_ENTITIES }, { NULL, 0, 0, 0 } }; @@ -45,13 +52,16 @@ Usage: xml-test [options] < input.xml\n\ Options:\n" CF_USAGE "\ --s, --pull Test PULL interface\n\ +-p, --pull Test PULL interface\n\ -s, --sax Test SAX interface\n\ --d, --dom Test DOM interface\n\ +-t, --dom Test DOM interface\n\ +-d, --dtd Enable parsing of DTD\n\ --hide-errors Hide warnings and error messages\n\ - --unfold-cdata Unfold CDATA sections\n\ - --ignore-comments Ignore processing instructions\n\ - --ignore-pis Ignore comments\n\ + --ignore-comments Ignore comments\n\ + --ignore-pis Ignore processing instructions\n\ + --report-blocks Report blocks or characters and CDATA sections\n\ + --report-ignorable Report ignorable whitespace\n\ + --file-entities Resolve file external entities (not fully normative)\n\ \n", stderr); exit(1); } @@ -59,10 +69,13 @@ CF_USAGE static uns want_sax; static uns want_pull; static uns want_dom; +static uns want_parse_dtd; static uns want_hide_errors; -static uns want_unfold_cdata; static uns want_ignore_comments; static uns want_ignore_pis; +static uns want_report_blocks; +static uns want_report_ignorable; +static uns want_file_entities; static struct fastbuf *out; @@ -140,7 +153,7 @@ h_document_end(struct xml_context *ctx UNUSED) static void h_xml_decl(struct xml_context *ctx) { - bprintf(out, "SAX: xml_decl version=%s standalone=%d\n", ctx->version_str, ctx->standalone); + bprintf(out, "SAX: xml_decl version=%s standalone=%d fb_encoding=%s\n", ctx->version_str, ctx->standalone, ctx->src->fb_encoding); } static void @@ -186,10 +199,21 @@ h_chars(struct xml_context *ctx) } static void -h_cdata(struct xml_context *ctx) +h_block(struct xml_context *ctx UNUSED, char *text, uns len UNUSED) { - bputs(out, "SAX: cdata"); - show_node(ctx->node); + bprintf(out, "SAX: block text='%s'\n", text); +} + +static void +h_cdata(struct xml_context *ctx UNUSED, char *text, uns len UNUSED) +{ + bprintf(out, "SAX: cdata text='%s'\n", text); +} + +static void +h_ignorable(struct xml_context *ctx UNUSED, char *text, uns len UNUSED) +{ + bprintf(out, "SAX: ignorable text='%s'\n", text); } static void @@ -204,6 +228,12 @@ h_dtd_end(struct xml_context *ctx UNUSED) bputs(out, "SAX: dtd_end\n"); } +static void +h_resolve_entity(struct xml_context *ctx, struct xml_dtd_entity *e) +{ + xml_push_fastbuf(ctx, bopen(e->system_id, O_RDONLY, 4096)); +} + int main(int argc, char **argv) { @@ -219,21 +249,30 @@ main(int argc, char **argv) case 'p': want_pull++; break; - case 'd': + case 't': want_dom++; break; + case 'd': + want_parse_dtd++; + break; case WANT_HIDE_ERRORS: want_hide_errors++; break; - case WANT_UNFOLD_CDATA: - want_unfold_cdata++; - break; case WANT_IGNORE_COMMENTS: want_ignore_comments++; break; case WANT_IGNORE_PIS: want_ignore_pis++; break; + case WANT_REPORT_BLOCKS: + want_report_blocks++; + break; + case WANT_REPORT_IGNORABLE: + want_report_ignorable++; + break; + case WANT_FILE_ENTITIES: + want_file_entities++; + break; default: usage(); } @@ -256,23 +295,31 @@ main(int argc, char **argv) ctx.h_stag = h_stag; ctx.h_etag = h_etag; ctx.h_chars = h_chars; - ctx.h_cdata = h_cdata; + if (want_report_blocks) + { + ctx.h_block = h_block; + ctx.h_cdata = h_cdata; + } + if (want_report_ignorable) + ctx.h_ignorable = h_ignorable; ctx.h_dtd_start = h_dtd_start; ctx.h_dtd_end = h_dtd_end; } if (want_dom) ctx.flags |= XML_ALLOC_ALL; - if (want_unfold_cdata) - ctx.flags |= XML_UNFOLD_CDATA; + if (want_parse_dtd) + ctx.flags |= XML_PARSE_DTD; if (want_ignore_comments) ctx.flags &= ~(XML_REPORT_COMMENTS | XML_ALLOC_COMMENTS); if (want_ignore_pis) ctx.flags &= ~(XML_REPORT_PIS | XML_ALLOC_PIS); - xml_set_source(&ctx, bfdopen_shared(0, 4096)); + if (want_file_entities) + ctx.h_resolve_entity = h_resolve_entity; + xml_push_fastbuf(&ctx, bfdopen_shared(0, 4096)); bputs(out, "PULL: start\n"); if (want_pull) { - ctx.pull = XML_PULL_CHARS | XML_PULL_CDATA | XML_PULL_STAG | XML_PULL_ETAG | XML_PULL_COMMENT | XML_PULL_PI; + ctx.pull = XML_PULL_CHARS | XML_PULL_STAG | XML_PULL_ETAG | XML_PULL_COMMENT | XML_PULL_PI; uns state; while (state = xml_next(&ctx)) switch (state) @@ -281,10 +328,6 @@ main(int argc, char **argv) bputs(out, "PULL: chars"); show_node(ctx.node); break; - case XML_STATE_CDATA: - bputs(out, "PULL: cdata"); - show_node(ctx.node); - break; case XML_STATE_STAG: bputs(out, "PULL: stag"); show_node(ctx.node); @@ -313,7 +356,7 @@ main(int argc, char **argv) { bputs(out, "PULL: eof\n"); if (want_dom) - show_tree(ctx.root, 0); + show_tree(ctx.dom, 0); } xml_cleanup(&ctx);