blob: 8b435171307619f3ed252fd2233d2749bcce2ef7 [file] [log] [blame]
Daniel Veillard5099ae81999-04-21 20:12:07 +00001/*
2 * tester.c : a small tester program for parsing using the SAX API.
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel.Veillard@w3.org
7 */
8
9#ifdef WIN32
Daniel Veillard3c558c31999-12-22 11:30:41 +000010#include "win32config.h"
Daniel Veillard5099ae81999-04-21 20:12:07 +000011#else
Daniel Veillard7f7d1111999-09-22 09:46:25 +000012#include "config.h"
Daniel Veillard5099ae81999-04-21 20:12:07 +000013#endif
Daniel Veillard7f7d1111999-09-22 09:46:25 +000014
15#include <stdio.h>
16#include <string.h>
17#include <stdarg.h>
18
19#ifdef HAVE_SYS_TYPES_H
Daniel Veillard5099ae81999-04-21 20:12:07 +000020#include <sys/types.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000021#endif
Daniel Veillard5099ae81999-04-21 20:12:07 +000022#ifdef HAVE_SYS_STAT_H
23#include <sys/stat.h>
24#endif
25#ifdef HAVE_FCNTL_H
26#include <fcntl.h>
27#endif
28#ifdef HAVE_UNISTD_H
29#include <unistd.h>
30#endif
Daniel Veillard7f7d1111999-09-22 09:46:25 +000031#ifdef HAVE_STDLIB_H
Daniel Veillard5099ae81999-04-21 20:12:07 +000032#include <stdlib.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000033#endif
Daniel Veillard7a66ee61999-09-26 11:31:02 +000034#ifdef HAVE_STRING_H
35#include <string.h>
36#endif
Daniel Veillard7f7d1111999-09-22 09:46:25 +000037
Daniel Veillard5099ae81999-04-21 20:12:07 +000038
Daniel Veillard361d8452000-04-03 19:48:13 +000039#include <libxml/parser.h>
40#include <libxml/parserInternals.h> /* only for xmlNewInputFromFile() */
41#include <libxml/tree.h>
42#include <libxml/debugXML.h>
43#include <libxml/xmlmemory.h>
Daniel Veillard5099ae81999-04-21 20:12:07 +000044
45static int debug = 0;
46static int copy = 0;
47static int recovery = 0;
Daniel Veillarddbfd6411999-12-28 16:35:14 +000048static int push = 0;
Daniel Veillard5e873c42000-04-12 13:27:38 +000049static int speed = 0;
Daniel Veillard5099ae81999-04-21 20:12:07 +000050
51xmlSAXHandler emptySAXHandlerStruct = {
52 NULL, /* internalSubset */
53 NULL, /* isStandalone */
54 NULL, /* hasInternalSubset */
55 NULL, /* hasExternalSubset */
56 NULL, /* resolveEntity */
57 NULL, /* getEntity */
58 NULL, /* entityDecl */
59 NULL, /* notationDecl */
60 NULL, /* attributeDecl */
61 NULL, /* elementDecl */
62 NULL, /* unparsedEntityDecl */
63 NULL, /* setDocumentLocator */
64 NULL, /* startDocument */
65 NULL, /* endDocument */
66 NULL, /* startElement */
67 NULL, /* endElement */
68 NULL, /* reference */
69 NULL, /* characters */
70 NULL, /* ignorableWhitespace */
71 NULL, /* processingInstruction */
72 NULL, /* comment */
73 NULL, /* xmlParserWarning */
74 NULL, /* xmlParserError */
75 NULL, /* xmlParserError */
Daniel Veillardb05deb71999-08-10 19:04:08 +000076 NULL, /* getParameterEntity */
Daniel Veillardcf461992000-03-14 18:30:20 +000077 NULL, /* cdataBlock; */
Daniel Veillard5099ae81999-04-21 20:12:07 +000078};
79
80xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
Daniel Veillard011b63c1999-06-02 17:44:04 +000081extern xmlSAXHandlerPtr debugSAXHandler;
Daniel Veillard5099ae81999-04-21 20:12:07 +000082
Daniel Veillard5099ae81999-04-21 20:12:07 +000083/************************************************************************
84 * *
85 * Debug Handlers *
86 * *
87 ************************************************************************/
88
89/**
90 * isStandaloneDebug:
91 * @ctxt: An XML parser context
92 *
93 * Is this document tagged standalone ?
94 *
95 * Returns 1 if true
96 */
97int
Daniel Veillardb96e6431999-08-29 21:02:19 +000098isStandaloneDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +000099{
Daniel Veillard27d88741999-05-29 11:51:49 +0000100 fprintf(stdout, "SAX.isStandalone()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000101 return(0);
102}
103
104/**
105 * hasInternalSubsetDebug:
106 * @ctxt: An XML parser context
107 *
108 * Does this document has an internal subset
109 *
110 * Returns 1 if true
111 */
112int
Daniel Veillardb96e6431999-08-29 21:02:19 +0000113hasInternalSubsetDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000114{
Daniel Veillard27d88741999-05-29 11:51:49 +0000115 fprintf(stdout, "SAX.hasInternalSubset()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000116 return(0);
117}
118
119/**
120 * hasExternalSubsetDebug:
121 * @ctxt: An XML parser context
122 *
123 * Does this document has an external subset
124 *
125 * Returns 1 if true
126 */
127int
Daniel Veillardb96e6431999-08-29 21:02:19 +0000128hasExternalSubsetDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000129{
Daniel Veillard27d88741999-05-29 11:51:49 +0000130 fprintf(stdout, "SAX.hasExternalSubset()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000131 return(0);
132}
133
134/**
Daniel Veillard06047432000-04-24 11:33:38 +0000135 * internalSubsetDebug:
Daniel Veillard5099ae81999-04-21 20:12:07 +0000136 * @ctxt: An XML parser context
137 *
138 * Does this document has an internal subset
139 */
140void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000141internalSubsetDebug(void *ctx, const xmlChar *name,
142 const xmlChar *ExternalID, const xmlChar *SystemID)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000143{
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000144 /* xmlDtdPtr externalSubset; */
Daniel Veillard011b63c1999-06-02 17:44:04 +0000145
Daniel Veillard27d88741999-05-29 11:51:49 +0000146 fprintf(stdout, "SAX.internalSubset(%s, %s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000147 name, ExternalID, SystemID);
Daniel Veillard011b63c1999-06-02 17:44:04 +0000148
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000149/***********
Daniel Veillard011b63c1999-06-02 17:44:04 +0000150 if ((ExternalID != NULL) || (SystemID != NULL)) {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000151 externalSubset = xmlParseDTD(ExternalID, SystemID);
152 if (externalSubset != NULL) {
153 xmlFreeDtd(externalSubset);
154 }
Daniel Veillard011b63c1999-06-02 17:44:04 +0000155 }
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000156 ***********/
Daniel Veillard5099ae81999-04-21 20:12:07 +0000157}
158
159/**
160 * resolveEntityDebug:
161 * @ctxt: An XML parser context
162 * @publicId: The public ID of the entity
163 * @systemId: The system ID of the entity
164 *
165 * Special entity resolver, better left to the parser, it has
166 * more context than the application layer.
167 * The default behaviour is to NOT resolve the entities, in that case
168 * the ENTITY_REF nodes are built in the structure (and the parameter
169 * values).
170 *
171 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
172 */
173xmlParserInputPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000174resolveEntityDebug(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000175{
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000176 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
Daniel Veillardb96e6431999-08-29 21:02:19 +0000177
Daniel Veillard14fff061999-06-22 21:49:07 +0000178
179 fprintf(stdout, "SAX.resolveEntity(");
180 if (publicId != NULL)
181 fprintf(stdout, "%s", (char *)publicId);
182 else
183 fprintf(stdout, " ");
184 if (systemId != NULL)
185 fprintf(stdout, ", %s)\n", (char *)systemId);
186 else
187 fprintf(stdout, ", )\n");
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000188/*********
Daniel Veillard011b63c1999-06-02 17:44:04 +0000189 if (systemId != NULL) {
Daniel Veillardb96e6431999-08-29 21:02:19 +0000190 return(xmlNewInputFromFile(ctxt, (char *) systemId));
Daniel Veillard011b63c1999-06-02 17:44:04 +0000191 }
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000192 *********/
Daniel Veillard5099ae81999-04-21 20:12:07 +0000193 return(NULL);
194}
195
196/**
197 * getEntityDebug:
198 * @ctxt: An XML parser context
199 * @name: The entity name
200 *
201 * Get an entity by name
202 *
203 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
204 */
205xmlEntityPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000206getEntityDebug(void *ctx, const xmlChar *name)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000207{
Daniel Veillard27d88741999-05-29 11:51:49 +0000208 fprintf(stdout, "SAX.getEntity(%s)\n", name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000209 return(NULL);
210}
211
Daniel Veillardb05deb71999-08-10 19:04:08 +0000212/**
213 * getParameterEntityDebug:
214 * @ctxt: An XML parser context
215 * @name: The entity name
216 *
217 * Get a parameter entity by name
218 *
219 * Returns the xmlParserInputPtr
220 */
221xmlEntityPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000222getParameterEntityDebug(void *ctx, const xmlChar *name)
Daniel Veillardb05deb71999-08-10 19:04:08 +0000223{
224 fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
225 return(NULL);
226}
227
Daniel Veillard5099ae81999-04-21 20:12:07 +0000228
229/**
230 * entityDeclDebug:
231 * @ctxt: An XML parser context
232 * @name: the entity name
233 * @type: the entity type
234 * @publicId: The public ID of the entity
235 * @systemId: The system ID of the entity
236 * @content: the entity value (without processing).
237 *
238 * An entity definition has been parsed
239 */
240void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000241entityDeclDebug(void *ctx, const xmlChar *name, int type,
242 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000243{
Daniel Veillard27d88741999-05-29 11:51:49 +0000244 fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000245 name, type, publicId, systemId, content);
246}
247
248/**
249 * attributeDeclDebug:
250 * @ctxt: An XML parser context
251 * @name: the attribute name
252 * @type: the attribute type
253 *
254 * An attribute definition has been parsed
255 */
256void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000257attributeDeclDebug(void *ctx, const xmlChar *elem, const xmlChar *name,
258 int type, int def, const xmlChar *defaultValue,
Daniel Veillard5099ae81999-04-21 20:12:07 +0000259 xmlEnumerationPtr tree)
260{
Daniel Veillard27d88741999-05-29 11:51:49 +0000261 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000262 elem, name, type, def, defaultValue);
263}
264
265/**
266 * elementDeclDebug:
267 * @ctxt: An XML parser context
268 * @name: the element name
269 * @type: the element type
270 * @content: the element value (without processing).
271 *
272 * An element definition has been parsed
273 */
274void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000275elementDeclDebug(void *ctx, const xmlChar *name, int type,
Daniel Veillard5099ae81999-04-21 20:12:07 +0000276 xmlElementContentPtr content)
277{
Daniel Veillard27d88741999-05-29 11:51:49 +0000278 fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000279 name, type);
280}
281
282/**
283 * notationDeclDebug:
284 * @ctxt: An XML parser context
285 * @name: The name of the notation
286 * @publicId: The public ID of the entity
287 * @systemId: The system ID of the entity
288 *
289 * What to do when a notation declaration has been parsed.
Daniel Veillard5099ae81999-04-21 20:12:07 +0000290 */
291void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000292notationDeclDebug(void *ctx, const xmlChar *name,
293 const xmlChar *publicId, const xmlChar *systemId)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000294{
Daniel Veillard27d88741999-05-29 11:51:49 +0000295 fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000296 (char *) name, (char *) publicId, (char *) systemId);
297}
298
299/**
300 * unparsedEntityDeclDebug:
301 * @ctxt: An XML parser context
302 * @name: The name of the entity
303 * @publicId: The public ID of the entity
304 * @systemId: The system ID of the entity
305 * @notationName: the name of the notation
306 *
307 * What to do when an unparsed entity declaration is parsed
Daniel Veillard5099ae81999-04-21 20:12:07 +0000308 */
309void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000310unparsedEntityDeclDebug(void *ctx, const xmlChar *name,
311 const xmlChar *publicId, const xmlChar *systemId,
312 const xmlChar *notationName)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000313{
Daniel Veillard27d88741999-05-29 11:51:49 +0000314 fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000315 (char *) name, (char *) publicId, (char *) systemId,
316 (char *) notationName);
317}
318
319/**
320 * setDocumentLocatorDebug:
321 * @ctxt: An XML parser context
322 * @loc: A SAX Locator
323 *
324 * Receive the document locator at startup, actually xmlDefaultSAXLocator
325 * Everything is available on the context, so this is useless in our case.
326 */
327void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000328setDocumentLocatorDebug(void *ctx, xmlSAXLocatorPtr loc)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000329{
Daniel Veillard27d88741999-05-29 11:51:49 +0000330 fprintf(stdout, "SAX.setDocumentLocator()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000331}
332
333/**
334 * startDocumentDebug:
335 * @ctxt: An XML parser context
336 *
337 * called when the document start being processed.
338 */
339void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000340startDocumentDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000341{
Daniel Veillard27d88741999-05-29 11:51:49 +0000342 fprintf(stdout, "SAX.startDocument()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000343}
344
345/**
346 * endDocumentDebug:
347 * @ctxt: An XML parser context
348 *
349 * called when the document end has been detected.
350 */
351void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000352endDocumentDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000353{
Daniel Veillard27d88741999-05-29 11:51:49 +0000354 fprintf(stdout, "SAX.endDocument()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000355}
356
357/**
358 * startElementDebug:
359 * @ctxt: An XML parser context
360 * @name: The element name
361 *
362 * called when an opening tag has been processed.
Daniel Veillard5099ae81999-04-21 20:12:07 +0000363 */
364void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000365startElementDebug(void *ctx, const xmlChar *name, const xmlChar **atts)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000366{
367 int i;
368
Daniel Veillard27d88741999-05-29 11:51:49 +0000369 fprintf(stdout, "SAX.startElement(%s", (char *) name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000370 if (atts != NULL) {
371 for (i = 0;(atts[i] != NULL);i++) {
Daniel Veillard27d88741999-05-29 11:51:49 +0000372 fprintf(stdout, ", %s='", atts[i++]);
373 fprintf(stdout, "%s'", atts[i]);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000374 }
375 }
Daniel Veillard27d88741999-05-29 11:51:49 +0000376 fprintf(stdout, ")\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000377}
378
379/**
380 * endElementDebug:
381 * @ctxt: An XML parser context
382 * @name: The element name
383 *
384 * called when the end of an element has been detected.
385 */
386void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000387endElementDebug(void *ctx, const xmlChar *name)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000388{
Daniel Veillard27d88741999-05-29 11:51:49 +0000389 fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000390}
391
392/**
393 * charactersDebug:
394 * @ctxt: An XML parser context
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000395 * @ch: a xmlChar string
396 * @len: the number of xmlChar
Daniel Veillard5099ae81999-04-21 20:12:07 +0000397 *
398 * receiving some chars from the parser.
399 * Question: how much at a time ???
400 */
401void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000402charactersDebug(void *ctx, const xmlChar *ch, int len)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000403{
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000404 int i;
405
406 fprintf(stdout, "SAX.characters(");
407 for (i = 0;(i < len) && (i < 30);i++)
408 fprintf(stdout, "%c", ch[i]);
409 fprintf(stdout, ", %d)\n", len);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000410}
411
412/**
413 * referenceDebug:
414 * @ctxt: An XML parser context
415 * @name: The entity name
416 *
417 * called when an entity reference is detected.
418 */
419void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000420referenceDebug(void *ctx, const xmlChar *name)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000421{
Daniel Veillard27d88741999-05-29 11:51:49 +0000422 fprintf(stdout, "SAX.reference(%s)\n", name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000423}
424
425/**
426 * ignorableWhitespaceDebug:
427 * @ctxt: An XML parser context
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000428 * @ch: a xmlChar string
Daniel Veillard5099ae81999-04-21 20:12:07 +0000429 * @start: the first char in the string
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000430 * @len: the number of xmlChar
Daniel Veillard5099ae81999-04-21 20:12:07 +0000431 *
432 * receiving some ignorable whitespaces from the parser.
433 * Question: how much at a time ???
434 */
435void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000436ignorableWhitespaceDebug(void *ctx, const xmlChar *ch, int len)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000437{
Daniel Veillard27d88741999-05-29 11:51:49 +0000438 fprintf(stdout, "SAX.ignorableWhitespace(%.30s, %d)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000439 (char *) ch, len);
440}
441
442/**
443 * processingInstructionDebug:
444 * @ctxt: An XML parser context
445 * @target: the target name
446 * @data: the PI data's
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000447 * @len: the number of xmlChar
Daniel Veillard5099ae81999-04-21 20:12:07 +0000448 *
449 * A processing instruction has been parsed.
450 */
451void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000452processingInstructionDebug(void *ctx, const xmlChar *target,
453 const xmlChar *data)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000454{
Daniel Veillard27d88741999-05-29 11:51:49 +0000455 fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000456 (char *) target, (char *) data);
457}
458
459/**
Daniel Veillardcf461992000-03-14 18:30:20 +0000460 * cdataBlockDebug:
461 * @ctx: the user data (XML parser context)
462 * @value: The pcdata content
463 * @len: the block length
464 *
465 * called when a pcdata block has been parsed
466 */
467void
468cdataBlockDebug(void *ctx, const xmlChar *value, int len)
469{
470 fprintf(stderr, "SAX.pcdata(%.20s, %d)\n",
471 (char *) value, len);
472}
473
474/**
Daniel Veillard5099ae81999-04-21 20:12:07 +0000475 * commentDebug:
476 * @ctxt: An XML parser context
477 * @value: the comment content
478 *
479 * A comment has been parsed.
480 */
481void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000482commentDebug(void *ctx, const xmlChar *value)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000483{
Daniel Veillard27d88741999-05-29 11:51:49 +0000484 fprintf(stdout, "SAX.comment(%s)\n", value);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000485}
486
487/**
488 * warningDebug:
489 * @ctxt: An XML parser context
490 * @msg: the message to display/transmit
491 * @...: extra parameters for the message display
492 *
493 * Display and format a warning messages, gives file, line, position and
494 * extra parameters.
495 */
496void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000497warningDebug(void *ctx, const char *msg, ...)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000498{
499 va_list args;
500
501 va_start(args, msg);
Daniel Veillard27d88741999-05-29 11:51:49 +0000502 fprintf(stdout, "SAX.warning: ");
503 vfprintf(stdout, msg, args);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000504 va_end(args);
505}
506
507/**
508 * errorDebug:
509 * @ctxt: An XML parser context
510 * @msg: the message to display/transmit
511 * @...: extra parameters for the message display
512 *
513 * Display and format a error messages, gives file, line, position and
514 * extra parameters.
515 */
516void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000517errorDebug(void *ctx, const char *msg, ...)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000518{
519 va_list args;
520
521 va_start(args, msg);
Daniel Veillard27d88741999-05-29 11:51:49 +0000522 fprintf(stdout, "SAX.error: ");
523 vfprintf(stdout, msg, args);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000524 va_end(args);
525}
526
527/**
528 * fatalErrorDebug:
529 * @ctxt: An XML parser context
530 * @msg: the message to display/transmit
531 * @...: extra parameters for the message display
532 *
533 * Display and format a fatalError messages, gives file, line, position and
534 * extra parameters.
535 */
536void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000537fatalErrorDebug(void *ctx, const char *msg, ...)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000538{
539 va_list args;
540
541 va_start(args, msg);
Daniel Veillard27d88741999-05-29 11:51:49 +0000542 fprintf(stdout, "SAX.fatalError: ");
543 vfprintf(stdout, msg, args);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000544 va_end(args);
545}
546
547xmlSAXHandler debugSAXHandlerStruct = {
548 internalSubsetDebug,
549 isStandaloneDebug,
550 hasInternalSubsetDebug,
551 hasExternalSubsetDebug,
552 resolveEntityDebug,
553 getEntityDebug,
554 entityDeclDebug,
555 notationDeclDebug,
556 attributeDeclDebug,
557 elementDeclDebug,
558 unparsedEntityDeclDebug,
559 setDocumentLocatorDebug,
560 startDocumentDebug,
561 endDocumentDebug,
562 startElementDebug,
563 endElementDebug,
564 referenceDebug,
565 charactersDebug,
566 ignorableWhitespaceDebug,
567 processingInstructionDebug,
568 commentDebug,
569 warningDebug,
570 errorDebug,
571 fatalErrorDebug,
Daniel Veillardb05deb71999-08-10 19:04:08 +0000572 getParameterEntityDebug,
Daniel Veillardcf461992000-03-14 18:30:20 +0000573 cdataBlockDebug
Daniel Veillard5099ae81999-04-21 20:12:07 +0000574};
575
576xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
577
578/************************************************************************
579 * *
580 * Debug *
581 * *
582 ************************************************************************/
583
584void parseAndPrintFile(char *filename) {
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000585 int res;
Daniel Veillard5099ae81999-04-21 20:12:07 +0000586
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000587 if (push) {
588 FILE *f;
Daniel Veillard5099ae81999-04-21 20:12:07 +0000589
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000590 /*
591 * Empty callbacks for checking
592 */
593 f = fopen(filename, "r");
594 if (f != NULL) {
595 int res;
596 char chars[10];
597 xmlParserCtxtPtr ctxt;
598
599 res = fread(chars, 1, 4, f);
600 if (res > 0) {
601 ctxt = xmlCreatePushParserCtxt(emptySAXHandler, NULL,
602 chars, res, filename);
603 while ((res = fread(chars, 1, 3, f)) > 0) {
604 xmlParseChunk(ctxt, chars, res, 0);
605 }
606 xmlParseChunk(ctxt, chars, 0, 1);
607 xmlFreeParserCtxt(ctxt);
608 }
609 fclose(f);
610 } else {
611 fprintf(stderr, "Cannot read file %s\n", filename);
612 }
613 /*
614 * Debug callback
615 */
616 f = fopen(filename, "r");
617 if (f != NULL) {
618 int res;
619 char chars[10];
620 xmlParserCtxtPtr ctxt;
621
622 res = fread(chars, 1, 4, f);
623 if (res > 0) {
624 ctxt = xmlCreatePushParserCtxt(debugSAXHandler, NULL,
625 chars, res, filename);
626 while ((res = fread(chars, 1, 3, f)) > 0) {
627 xmlParseChunk(ctxt, chars, res, 0);
628 }
629 res = xmlParseChunk(ctxt, chars, 0, 1);
630 xmlFreeParserCtxt(ctxt);
631 if (res != 0) {
632 fprintf(stdout,
633 "xmlSAXUserParseFile returned error %d\n", res);
634 }
635 }
636 fclose(f);
637 }
638 } else {
Daniel Veillard5e873c42000-04-12 13:27:38 +0000639 if (!speed) {
640 /*
641 * Empty callbacks for checking
642 */
643 res = xmlSAXUserParseFile(emptySAXHandler, NULL, filename);
644 if (res != 0) {
645 fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res);
646 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000647
Daniel Veillard5e873c42000-04-12 13:27:38 +0000648 /*
649 * Debug callback
650 */
651 res = xmlSAXUserParseFile(debugSAXHandler, NULL, filename);
652 if (res != 0) {
653 fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res);
654 }
655 } else {
656 /*
657 * test 100x the SAX parse
658 */
659 int i;
660
661 for (i = 0; i<100;i++)
662 res = xmlSAXUserParseFile(emptySAXHandler, NULL, filename);
663 if (res != 0) {
664 fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res);
665 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000666 }
Daniel Veillard5099ae81999-04-21 20:12:07 +0000667 }
668}
669
Daniel Veillard5099ae81999-04-21 20:12:07 +0000670
671int main(int argc, char **argv) {
672 int i;
673 int files = 0;
674
675 for (i = 1; i < argc ; i++) {
676 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
677 debug++;
678 else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
679 copy++;
680 else if ((!strcmp(argv[i], "-recover")) ||
681 (!strcmp(argv[i], "--recover")))
682 recovery++;
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000683 else if ((!strcmp(argv[i], "-push")) ||
684 (!strcmp(argv[i], "--push")))
685 push++;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000686 else if ((!strcmp(argv[i], "-speed")) ||
687 (!strcmp(argv[i], "--speed")))
688 speed++;
Daniel Veillard5099ae81999-04-21 20:12:07 +0000689 }
690 for (i = 1; i < argc ; i++) {
691 if (argv[i][0] != '-') {
692 parseAndPrintFile(argv[i]);
693 files ++;
694 }
695 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000696 xmlCleanupParser();
697 xmlMemoryDump();
Daniel Veillard5099ae81999-04-21 20:12:07 +0000698
699 return(0);
700}