#include <stdio.h>
#include <string.h>

#include <libxml/parser.h>

/*
 * gcc -Wall -o t test.c `xml2-config --cflags` `xml2-config --libs`
 */

static void
startElementDebug(void *ctx, const xmlChar *name, const xmlChar **atts)
{
  int i;

  fprintf(stdout, "SAX.startElement(%s", (char *) name);
  if (atts != NULL) {
    for (i = 0;(atts[i] != NULL);i++) {
      fprintf(stdout, ", %s='", atts[i++]);
      if (atts[i] != NULL)
        fprintf(stdout, "%s'", atts[i]);
    }
  }
  fprintf(stdout, ")\n");
}

static void
charactersDebug(void *ctx, const xmlChar *ch, int len)
{
    char output[40];
    int i;

    for (i = 0;(i<len) && (i < 30);i++)
	output[i] = ch[i];
    output[i] = 0;

    fprintf(stdout, "SAX.characters(%s, %d)\n", output, len);
}


#define DATA "<?xml version=\"1.0\"?>\
<!DOCTYPE root [\
   <!ENTITY foo 'abc'>\
   <!ENTITY bar 'def'>\
]>\
<root attribute=\"&foo;\">\
   <element>&bar;</element>\
</root>\
"

static const xmlChar data[]=DATA;

int
main(int argc, char *argv[]) 
{
  xmlSAXHandler sax;
  size_t len=sizeof(data);
  int i;

  memset(&sax, '\0', sizeof(xmlSAXHandler));
  sax.startElement = startElementDebug;
  sax.characters   = charactersDebug;
  sax.initialized  = 1;

  for(i=0; i<2; i++) {
    fprintf(stdout, "xmlCreatePushParserCtxt with replaceEntities = %d\n", i);
    
    xmlParserCtxtPtr xc = xmlCreatePushParserCtxt(&sax, NULL, 
                                                  data, len, NULL);
    xc->replaceEntities = i;
    xmlParseChunk(xc, data, len, 1);
    xmlFreeParserCtxt(xc);

    fprintf(stdout, "----------\n");
  }

  return 0;
}
