blob: e5568fc47b049c02a8f7ec22fc5292f40f3c9c95 [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 Veillard32bc74e2000-07-14 14:49:25 +000039#include <libxml/xml-error.h>
Daniel Veillard361d8452000-04-03 19:48:13 +000040#include <libxml/parser.h>
41#include <libxml/parserInternals.h> /* only for xmlNewInputFromFile() */
42#include <libxml/tree.h>
43#include <libxml/debugXML.h>
44#include <libxml/xmlmemory.h>
Daniel Veillard5099ae81999-04-21 20:12:07 +000045
46static int debug = 0;
47static int copy = 0;
48static int recovery = 0;
Daniel Veillarddbfd6411999-12-28 16:35:14 +000049static int push = 0;
Daniel Veillard5e873c42000-04-12 13:27:38 +000050static int speed = 0;
Daniel Veillard5099ae81999-04-21 20:12:07 +000051
52xmlSAXHandler emptySAXHandlerStruct = {
53 NULL, /* internalSubset */
54 NULL, /* isStandalone */
55 NULL, /* hasInternalSubset */
56 NULL, /* hasExternalSubset */
57 NULL, /* resolveEntity */
58 NULL, /* getEntity */
59 NULL, /* entityDecl */
60 NULL, /* notationDecl */
61 NULL, /* attributeDecl */
62 NULL, /* elementDecl */
63 NULL, /* unparsedEntityDecl */
64 NULL, /* setDocumentLocator */
65 NULL, /* startDocument */
66 NULL, /* endDocument */
67 NULL, /* startElement */
68 NULL, /* endElement */
69 NULL, /* reference */
70 NULL, /* characters */
71 NULL, /* ignorableWhitespace */
72 NULL, /* processingInstruction */
73 NULL, /* comment */
74 NULL, /* xmlParserWarning */
75 NULL, /* xmlParserError */
76 NULL, /* xmlParserError */
Daniel Veillardb05deb71999-08-10 19:04:08 +000077 NULL, /* getParameterEntity */
Daniel Veillardcf461992000-03-14 18:30:20 +000078 NULL, /* cdataBlock; */
Daniel Veillard5099ae81999-04-21 20:12:07 +000079};
80
81xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
Daniel Veillard011b63c1999-06-02 17:44:04 +000082extern xmlSAXHandlerPtr debugSAXHandler;
Daniel Veillard5099ae81999-04-21 20:12:07 +000083
Daniel Veillard5099ae81999-04-21 20:12:07 +000084/************************************************************************
85 * *
86 * Debug Handlers *
87 * *
88 ************************************************************************/
89
90/**
91 * isStandaloneDebug:
92 * @ctxt: An XML parser context
93 *
94 * Is this document tagged standalone ?
95 *
96 * Returns 1 if true
97 */
98int
Daniel Veillardb96e6431999-08-29 21:02:19 +000099isStandaloneDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000100{
Daniel Veillard27d88741999-05-29 11:51:49 +0000101 fprintf(stdout, "SAX.isStandalone()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000102 return(0);
103}
104
105/**
106 * hasInternalSubsetDebug:
107 * @ctxt: An XML parser context
108 *
109 * Does this document has an internal subset
110 *
111 * Returns 1 if true
112 */
113int
Daniel Veillardb96e6431999-08-29 21:02:19 +0000114hasInternalSubsetDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000115{
Daniel Veillard27d88741999-05-29 11:51:49 +0000116 fprintf(stdout, "SAX.hasInternalSubset()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000117 return(0);
118}
119
120/**
121 * hasExternalSubsetDebug:
122 * @ctxt: An XML parser context
123 *
124 * Does this document has an external subset
125 *
126 * Returns 1 if true
127 */
128int
Daniel Veillardb96e6431999-08-29 21:02:19 +0000129hasExternalSubsetDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000130{
Daniel Veillard27d88741999-05-29 11:51:49 +0000131 fprintf(stdout, "SAX.hasExternalSubset()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000132 return(0);
133}
134
135/**
Daniel Veillard06047432000-04-24 11:33:38 +0000136 * internalSubsetDebug:
Daniel Veillard5099ae81999-04-21 20:12:07 +0000137 * @ctxt: An XML parser context
138 *
139 * Does this document has an internal subset
140 */
141void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000142internalSubsetDebug(void *ctx, const xmlChar *name,
143 const xmlChar *ExternalID, const xmlChar *SystemID)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000144{
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000145 /* xmlDtdPtr externalSubset; */
Daniel Veillard011b63c1999-06-02 17:44:04 +0000146
Daniel Veillard27d88741999-05-29 11:51:49 +0000147 fprintf(stdout, "SAX.internalSubset(%s, %s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000148 name, ExternalID, SystemID);
Daniel Veillard011b63c1999-06-02 17:44:04 +0000149
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000150/***********
Daniel Veillard011b63c1999-06-02 17:44:04 +0000151 if ((ExternalID != NULL) || (SystemID != NULL)) {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000152 externalSubset = xmlParseDTD(ExternalID, SystemID);
153 if (externalSubset != NULL) {
154 xmlFreeDtd(externalSubset);
155 }
Daniel Veillard011b63c1999-06-02 17:44:04 +0000156 }
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000157 ***********/
Daniel Veillard5099ae81999-04-21 20:12:07 +0000158}
159
160/**
161 * resolveEntityDebug:
162 * @ctxt: An XML parser context
163 * @publicId: The public ID of the entity
164 * @systemId: The system ID of the entity
165 *
166 * Special entity resolver, better left to the parser, it has
167 * more context than the application layer.
168 * The default behaviour is to NOT resolve the entities, in that case
169 * the ENTITY_REF nodes are built in the structure (and the parameter
170 * values).
171 *
172 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
173 */
174xmlParserInputPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000175resolveEntityDebug(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000176{
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000177 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
Daniel Veillardb96e6431999-08-29 21:02:19 +0000178
Daniel Veillard14fff061999-06-22 21:49:07 +0000179
180 fprintf(stdout, "SAX.resolveEntity(");
181 if (publicId != NULL)
182 fprintf(stdout, "%s", (char *)publicId);
183 else
184 fprintf(stdout, " ");
185 if (systemId != NULL)
186 fprintf(stdout, ", %s)\n", (char *)systemId);
187 else
188 fprintf(stdout, ", )\n");
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000189/*********
Daniel Veillard011b63c1999-06-02 17:44:04 +0000190 if (systemId != NULL) {
Daniel Veillardb96e6431999-08-29 21:02:19 +0000191 return(xmlNewInputFromFile(ctxt, (char *) systemId));
Daniel Veillard011b63c1999-06-02 17:44:04 +0000192 }
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000193 *********/
Daniel Veillard5099ae81999-04-21 20:12:07 +0000194 return(NULL);
195}
196
197/**
198 * getEntityDebug:
199 * @ctxt: An XML parser context
200 * @name: The entity name
201 *
202 * Get an entity by name
203 *
204 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
205 */
206xmlEntityPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000207getEntityDebug(void *ctx, const xmlChar *name)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000208{
Daniel Veillard27d88741999-05-29 11:51:49 +0000209 fprintf(stdout, "SAX.getEntity(%s)\n", name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000210 return(NULL);
211}
212
Daniel Veillardb05deb71999-08-10 19:04:08 +0000213/**
214 * getParameterEntityDebug:
215 * @ctxt: An XML parser context
216 * @name: The entity name
217 *
218 * Get a parameter entity by name
219 *
220 * Returns the xmlParserInputPtr
221 */
222xmlEntityPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000223getParameterEntityDebug(void *ctx, const xmlChar *name)
Daniel Veillardb05deb71999-08-10 19:04:08 +0000224{
225 fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
226 return(NULL);
227}
228
Daniel Veillard5099ae81999-04-21 20:12:07 +0000229
230/**
231 * entityDeclDebug:
232 * @ctxt: An XML parser context
233 * @name: the entity name
234 * @type: the entity type
235 * @publicId: The public ID of the entity
236 * @systemId: The system ID of the entity
237 * @content: the entity value (without processing).
238 *
239 * An entity definition has been parsed
240 */
241void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000242entityDeclDebug(void *ctx, const xmlChar *name, int type,
243 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000244{
Daniel Veillard27d88741999-05-29 11:51:49 +0000245 fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000246 name, type, publicId, systemId, content);
247}
248
249/**
250 * attributeDeclDebug:
251 * @ctxt: An XML parser context
252 * @name: the attribute name
253 * @type: the attribute type
254 *
255 * An attribute definition has been parsed
256 */
257void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000258attributeDeclDebug(void *ctx, const xmlChar *elem, const xmlChar *name,
259 int type, int def, const xmlChar *defaultValue,
Daniel Veillard5099ae81999-04-21 20:12:07 +0000260 xmlEnumerationPtr tree)
261{
Daniel Veillard27d88741999-05-29 11:51:49 +0000262 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000263 elem, name, type, def, defaultValue);
264}
265
266/**
267 * elementDeclDebug:
268 * @ctxt: An XML parser context
269 * @name: the element name
270 * @type: the element type
271 * @content: the element value (without processing).
272 *
273 * An element definition has been parsed
274 */
275void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000276elementDeclDebug(void *ctx, const xmlChar *name, int type,
Daniel Veillard5099ae81999-04-21 20:12:07 +0000277 xmlElementContentPtr content)
278{
Daniel Veillard27d88741999-05-29 11:51:49 +0000279 fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000280 name, type);
281}
282
283/**
284 * notationDeclDebug:
285 * @ctxt: An XML parser context
286 * @name: The name of the notation
287 * @publicId: The public ID of the entity
288 * @systemId: The system ID of the entity
289 *
290 * What to do when a notation declaration has been parsed.
Daniel Veillard5099ae81999-04-21 20:12:07 +0000291 */
292void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000293notationDeclDebug(void *ctx, const xmlChar *name,
294 const xmlChar *publicId, const xmlChar *systemId)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000295{
Daniel Veillard27d88741999-05-29 11:51:49 +0000296 fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000297 (char *) name, (char *) publicId, (char *) systemId);
298}
299
300/**
301 * unparsedEntityDeclDebug:
302 * @ctxt: An XML parser context
303 * @name: The name of the entity
304 * @publicId: The public ID of the entity
305 * @systemId: The system ID of the entity
306 * @notationName: the name of the notation
307 *
308 * What to do when an unparsed entity declaration is parsed
Daniel Veillard5099ae81999-04-21 20:12:07 +0000309 */
310void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000311unparsedEntityDeclDebug(void *ctx, const xmlChar *name,
312 const xmlChar *publicId, const xmlChar *systemId,
313 const xmlChar *notationName)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000314{
Daniel Veillard27d88741999-05-29 11:51:49 +0000315 fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000316 (char *) name, (char *) publicId, (char *) systemId,
317 (char *) notationName);
318}
319
320/**
321 * setDocumentLocatorDebug:
322 * @ctxt: An XML parser context
323 * @loc: A SAX Locator
324 *
325 * Receive the document locator at startup, actually xmlDefaultSAXLocator
326 * Everything is available on the context, so this is useless in our case.
327 */
328void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000329setDocumentLocatorDebug(void *ctx, xmlSAXLocatorPtr loc)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000330{
Daniel Veillard27d88741999-05-29 11:51:49 +0000331 fprintf(stdout, "SAX.setDocumentLocator()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000332}
333
334/**
335 * startDocumentDebug:
336 * @ctxt: An XML parser context
337 *
338 * called when the document start being processed.
339 */
340void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000341startDocumentDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000342{
Daniel Veillard27d88741999-05-29 11:51:49 +0000343 fprintf(stdout, "SAX.startDocument()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000344}
345
346/**
347 * endDocumentDebug:
348 * @ctxt: An XML parser context
349 *
350 * called when the document end has been detected.
351 */
352void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000353endDocumentDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000354{
Daniel Veillard27d88741999-05-29 11:51:49 +0000355 fprintf(stdout, "SAX.endDocument()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000356}
357
358/**
359 * startElementDebug:
360 * @ctxt: An XML parser context
361 * @name: The element name
362 *
363 * called when an opening tag has been processed.
Daniel Veillard5099ae81999-04-21 20:12:07 +0000364 */
365void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000366startElementDebug(void *ctx, const xmlChar *name, const xmlChar **atts)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000367{
368 int i;
369
Daniel Veillard27d88741999-05-29 11:51:49 +0000370 fprintf(stdout, "SAX.startElement(%s", (char *) name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000371 if (atts != NULL) {
372 for (i = 0;(atts[i] != NULL);i++) {
Daniel Veillard27d88741999-05-29 11:51:49 +0000373 fprintf(stdout, ", %s='", atts[i++]);
374 fprintf(stdout, "%s'", atts[i]);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000375 }
376 }
Daniel Veillard27d88741999-05-29 11:51:49 +0000377 fprintf(stdout, ")\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000378}
379
380/**
381 * endElementDebug:
382 * @ctxt: An XML parser context
383 * @name: The element name
384 *
385 * called when the end of an element has been detected.
386 */
387void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000388endElementDebug(void *ctx, const xmlChar *name)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000389{
Daniel Veillard27d88741999-05-29 11:51:49 +0000390 fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000391}
392
393/**
394 * charactersDebug:
395 * @ctxt: An XML parser context
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000396 * @ch: a xmlChar string
397 * @len: the number of xmlChar
Daniel Veillard5099ae81999-04-21 20:12:07 +0000398 *
399 * receiving some chars from the parser.
400 * Question: how much at a time ???
401 */
402void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000403charactersDebug(void *ctx, const xmlChar *ch, int len)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000404{
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000405 int i;
406
407 fprintf(stdout, "SAX.characters(");
408 for (i = 0;(i < len) && (i < 30);i++)
409 fprintf(stdout, "%c", ch[i]);
410 fprintf(stdout, ", %d)\n", len);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000411}
412
413/**
414 * referenceDebug:
415 * @ctxt: An XML parser context
416 * @name: The entity name
417 *
418 * called when an entity reference is detected.
419 */
420void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000421referenceDebug(void *ctx, const xmlChar *name)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000422{
Daniel Veillard27d88741999-05-29 11:51:49 +0000423 fprintf(stdout, "SAX.reference(%s)\n", name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000424}
425
426/**
427 * ignorableWhitespaceDebug:
428 * @ctxt: An XML parser context
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000429 * @ch: a xmlChar string
Daniel Veillard5099ae81999-04-21 20:12:07 +0000430 * @start: the first char in the string
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000431 * @len: the number of xmlChar
Daniel Veillard5099ae81999-04-21 20:12:07 +0000432 *
433 * receiving some ignorable whitespaces from the parser.
434 * Question: how much at a time ???
435 */
436void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000437ignorableWhitespaceDebug(void *ctx, const xmlChar *ch, int len)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000438{
Daniel Veillard27d88741999-05-29 11:51:49 +0000439 fprintf(stdout, "SAX.ignorableWhitespace(%.30s, %d)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000440 (char *) ch, len);
441}
442
443/**
444 * processingInstructionDebug:
445 * @ctxt: An XML parser context
446 * @target: the target name
447 * @data: the PI data's
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000448 * @len: the number of xmlChar
Daniel Veillard5099ae81999-04-21 20:12:07 +0000449 *
450 * A processing instruction has been parsed.
451 */
452void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000453processingInstructionDebug(void *ctx, const xmlChar *target,
454 const xmlChar *data)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000455{
Daniel Veillard27d88741999-05-29 11:51:49 +0000456 fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000457 (char *) target, (char *) data);
458}
459
460/**
Daniel Veillardcf461992000-03-14 18:30:20 +0000461 * cdataBlockDebug:
462 * @ctx: the user data (XML parser context)
463 * @value: The pcdata content
464 * @len: the block length
465 *
466 * called when a pcdata block has been parsed
467 */
468void
469cdataBlockDebug(void *ctx, const xmlChar *value, int len)
470{
471 fprintf(stderr, "SAX.pcdata(%.20s, %d)\n",
472 (char *) value, len);
473}
474
475/**
Daniel Veillard5099ae81999-04-21 20:12:07 +0000476 * commentDebug:
477 * @ctxt: An XML parser context
478 * @value: the comment content
479 *
480 * A comment has been parsed.
481 */
482void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000483commentDebug(void *ctx, const xmlChar *value)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000484{
Daniel Veillard27d88741999-05-29 11:51:49 +0000485 fprintf(stdout, "SAX.comment(%s)\n", value);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000486}
487
488/**
489 * warningDebug:
490 * @ctxt: An XML parser context
491 * @msg: the message to display/transmit
492 * @...: extra parameters for the message display
493 *
494 * Display and format a warning messages, gives file, line, position and
495 * extra parameters.
496 */
497void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000498warningDebug(void *ctx, const char *msg, ...)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000499{
500 va_list args;
501
502 va_start(args, msg);
Daniel Veillard27d88741999-05-29 11:51:49 +0000503 fprintf(stdout, "SAX.warning: ");
504 vfprintf(stdout, msg, args);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000505 va_end(args);
506}
507
508/**
509 * errorDebug:
510 * @ctxt: An XML parser context
511 * @msg: the message to display/transmit
512 * @...: extra parameters for the message display
513 *
514 * Display and format a error messages, gives file, line, position and
515 * extra parameters.
516 */
517void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000518errorDebug(void *ctx, const char *msg, ...)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000519{
520 va_list args;
521
522 va_start(args, msg);
Daniel Veillard27d88741999-05-29 11:51:49 +0000523 fprintf(stdout, "SAX.error: ");
524 vfprintf(stdout, msg, args);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000525 va_end(args);
526}
527
528/**
529 * fatalErrorDebug:
530 * @ctxt: An XML parser context
531 * @msg: the message to display/transmit
532 * @...: extra parameters for the message display
533 *
534 * Display and format a fatalError messages, gives file, line, position and
535 * extra parameters.
536 */
537void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000538fatalErrorDebug(void *ctx, const char *msg, ...)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000539{
540 va_list args;
541
542 va_start(args, msg);
Daniel Veillard27d88741999-05-29 11:51:49 +0000543 fprintf(stdout, "SAX.fatalError: ");
544 vfprintf(stdout, msg, args);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000545 va_end(args);
546}
547
548xmlSAXHandler debugSAXHandlerStruct = {
549 internalSubsetDebug,
550 isStandaloneDebug,
551 hasInternalSubsetDebug,
552 hasExternalSubsetDebug,
553 resolveEntityDebug,
554 getEntityDebug,
555 entityDeclDebug,
556 notationDeclDebug,
557 attributeDeclDebug,
558 elementDeclDebug,
559 unparsedEntityDeclDebug,
560 setDocumentLocatorDebug,
561 startDocumentDebug,
562 endDocumentDebug,
563 startElementDebug,
564 endElementDebug,
565 referenceDebug,
566 charactersDebug,
567 ignorableWhitespaceDebug,
568 processingInstructionDebug,
569 commentDebug,
570 warningDebug,
571 errorDebug,
572 fatalErrorDebug,
Daniel Veillardb05deb71999-08-10 19:04:08 +0000573 getParameterEntityDebug,
Daniel Veillardcf461992000-03-14 18:30:20 +0000574 cdataBlockDebug
Daniel Veillard5099ae81999-04-21 20:12:07 +0000575};
576
577xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
578
579/************************************************************************
580 * *
581 * Debug *
582 * *
583 ************************************************************************/
584
585void parseAndPrintFile(char *filename) {
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000586 int res;
Daniel Veillard5099ae81999-04-21 20:12:07 +0000587
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000588 if (push) {
589 FILE *f;
Daniel Veillard5099ae81999-04-21 20:12:07 +0000590
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000591 /*
592 * Empty callbacks for checking
593 */
594 f = fopen(filename, "r");
595 if (f != NULL) {
596 int res;
597 char chars[10];
598 xmlParserCtxtPtr ctxt;
599
600 res = fread(chars, 1, 4, f);
601 if (res > 0) {
602 ctxt = xmlCreatePushParserCtxt(emptySAXHandler, NULL,
603 chars, res, filename);
604 while ((res = fread(chars, 1, 3, f)) > 0) {
605 xmlParseChunk(ctxt, chars, res, 0);
606 }
607 xmlParseChunk(ctxt, chars, 0, 1);
608 xmlFreeParserCtxt(ctxt);
609 }
610 fclose(f);
611 } else {
612 fprintf(stderr, "Cannot read file %s\n", filename);
613 }
614 /*
615 * Debug callback
616 */
617 f = fopen(filename, "r");
618 if (f != NULL) {
619 int res;
620 char chars[10];
621 xmlParserCtxtPtr ctxt;
622
623 res = fread(chars, 1, 4, f);
624 if (res > 0) {
625 ctxt = xmlCreatePushParserCtxt(debugSAXHandler, NULL,
626 chars, res, filename);
627 while ((res = fread(chars, 1, 3, f)) > 0) {
628 xmlParseChunk(ctxt, chars, res, 0);
629 }
630 res = xmlParseChunk(ctxt, chars, 0, 1);
631 xmlFreeParserCtxt(ctxt);
632 if (res != 0) {
633 fprintf(stdout,
634 "xmlSAXUserParseFile returned error %d\n", res);
635 }
636 }
637 fclose(f);
638 }
639 } else {
Daniel Veillard5e873c42000-04-12 13:27:38 +0000640 if (!speed) {
641 /*
642 * Empty callbacks for checking
643 */
644 res = xmlSAXUserParseFile(emptySAXHandler, NULL, filename);
645 if (res != 0) {
646 fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res);
647 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000648
Daniel Veillard5e873c42000-04-12 13:27:38 +0000649 /*
650 * Debug callback
651 */
652 res = xmlSAXUserParseFile(debugSAXHandler, NULL, filename);
653 if (res != 0) {
654 fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res);
655 }
656 } else {
657 /*
658 * test 100x the SAX parse
659 */
660 int i;
661
662 for (i = 0; i<100;i++)
663 res = xmlSAXUserParseFile(emptySAXHandler, NULL, filename);
664 if (res != 0) {
665 fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res);
666 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000667 }
Daniel Veillard5099ae81999-04-21 20:12:07 +0000668 }
669}
670
Daniel Veillard5099ae81999-04-21 20:12:07 +0000671
672int main(int argc, char **argv) {
673 int i;
674 int files = 0;
675
676 for (i = 1; i < argc ; i++) {
677 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
678 debug++;
679 else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
680 copy++;
681 else if ((!strcmp(argv[i], "-recover")) ||
682 (!strcmp(argv[i], "--recover")))
683 recovery++;
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000684 else if ((!strcmp(argv[i], "-push")) ||
685 (!strcmp(argv[i], "--push")))
686 push++;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000687 else if ((!strcmp(argv[i], "-speed")) ||
688 (!strcmp(argv[i], "--speed")))
689 speed++;
Daniel Veillard5099ae81999-04-21 20:12:07 +0000690 }
691 for (i = 1; i < argc ; i++) {
692 if (argv[i][0] != '-') {
693 parseAndPrintFile(argv[i]);
694 files ++;
695 }
696 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000697 xmlCleanupParser();
698 xmlMemoryDump();
Daniel Veillard5099ae81999-04-21 20:12:07 +0000699
700 return(0);
701}