]> mj.ucw.cz Git - libucw.git/blobdiff - sherlock/xml/xml-test.c
ucw. docs: document single-line doc. comments
[libucw.git] / sherlock / xml / xml-test.c
index db252c8ddd3b5446f84540e5fa1cb977439bf86d..f6738c56db4164cd3851dfdd7c7351ebeac88812 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *     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.
@@ -9,32 +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 <stdio.h>
 #include <stdlib.h>
+#include <fcntl.h>
 
 enum {
   WANT_FIRST = 0x100,
-  WANT_PARSE_DTD,
   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' },
-  { "dtd",             0, 0, WANT_PARSE_DTD },
+  { "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 }
 };
 
@@ -49,12 +54,14 @@ CF_USAGE
 "\
 -p, --pull              Test PULL interface\n\
 -s, --sax               Test SAX interface\n\
--d, --dom               Test DOM interface\n\
-    --dtd               Enable parsing of DTD\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);
 }
@@ -64,9 +71,11 @@ 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;
 
@@ -144,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
@@ -190,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
@@ -208,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)
 {
@@ -223,24 +249,30 @@ main(int argc, char **argv)
        case 'p':
          want_pull++;
          break;
-       case 'd':
+       case 't':
          want_dom++;
          break;
-       case WANT_PARSE_DTD:
+       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();
       }
@@ -263,7 +295,13 @@ 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;
     }
@@ -271,17 +309,17 @@ main(int argc, char **argv)
     ctx.flags |= XML_ALLOC_ALL;
   if (want_parse_dtd)
     ctx.flags |= XML_PARSE_DTD;
-  if (want_unfold_cdata)
-    ctx.flags |= XML_UNFOLD_CDATA;
   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)
@@ -290,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);