/*
* Sherlock Library -- A simple XML parser
*
- * (c) 2007 Pavel Charvat <pchar@ucw.cz>
+ * (c) 2007--2008 Pavel Charvat <pchar@ucw.cz>
*
* This software may be freely distributed and used according to the terms
* of the GNU Lesser General Public License.
#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 <stdio.h>
#include <stdlib.h>
+#include <fcntl.h>
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 }
};
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);
}
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;
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
}
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
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)
{
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();
}
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)
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);
{
bputs(out, "PULL: eof\n");
if (want_dom)
- show_tree(ctx.root, 0);
+ show_tree(ctx.dom, 0);
}
xml_cleanup(&ctx);