blob: ff96e4bd0f66c12551a79021b0274e2e21e77260 [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 Veillardc2def842000-11-07 14:21:01 +000011#undef LIBXML_DLL_IMPORT
Daniel Veillard5099ae81999-04-21 20:12:07 +000012#else
Daniel Veillard7f7d1111999-09-22 09:46:25 +000013#include "config.h"
Daniel Veillard5099ae81999-04-21 20:12:07 +000014#endif
Daniel Veillard7f7d1111999-09-22 09:46:25 +000015
16#include <stdio.h>
17#include <string.h>
18#include <stdarg.h>
19
20#ifdef HAVE_SYS_TYPES_H
Daniel Veillard5099ae81999-04-21 20:12:07 +000021#include <sys/types.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000022#endif
Daniel Veillard5099ae81999-04-21 20:12:07 +000023#ifdef HAVE_SYS_STAT_H
24#include <sys/stat.h>
25#endif
26#ifdef HAVE_FCNTL_H
27#include <fcntl.h>
28#endif
29#ifdef HAVE_UNISTD_H
30#include <unistd.h>
31#endif
Daniel Veillard7f7d1111999-09-22 09:46:25 +000032#ifdef HAVE_STDLIB_H
Daniel Veillard5099ae81999-04-21 20:12:07 +000033#include <stdlib.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000034#endif
Daniel Veillard7a66ee61999-09-26 11:31:02 +000035#ifdef HAVE_STRING_H
36#include <string.h>
37#endif
Daniel Veillard7f7d1111999-09-22 09:46:25 +000038
Daniel Veillard5099ae81999-04-21 20:12:07 +000039
Daniel Veillardb71379b2000-10-09 12:30:39 +000040#include <libxml/xmlerror.h>
Daniel Veillard361d8452000-04-03 19:48:13 +000041#include <libxml/parser.h>
42#include <libxml/parserInternals.h> /* only for xmlNewInputFromFile() */
43#include <libxml/tree.h>
44#include <libxml/debugXML.h>
45#include <libxml/xmlmemory.h>
Daniel Veillard5099ae81999-04-21 20:12:07 +000046
47static int debug = 0;
48static int copy = 0;
49static int recovery = 0;
Daniel Veillarddbfd6411999-12-28 16:35:14 +000050static int push = 0;
Daniel Veillard5e873c42000-04-12 13:27:38 +000051static int speed = 0;
Daniel Veillard5099ae81999-04-21 20:12:07 +000052
53xmlSAXHandler emptySAXHandlerStruct = {
54 NULL, /* internalSubset */
55 NULL, /* isStandalone */
56 NULL, /* hasInternalSubset */
57 NULL, /* hasExternalSubset */
58 NULL, /* resolveEntity */
59 NULL, /* getEntity */
60 NULL, /* entityDecl */
61 NULL, /* notationDecl */
62 NULL, /* attributeDecl */
63 NULL, /* elementDecl */
64 NULL, /* unparsedEntityDecl */
65 NULL, /* setDocumentLocator */
66 NULL, /* startDocument */
67 NULL, /* endDocument */
68 NULL, /* startElement */
69 NULL, /* endElement */
70 NULL, /* reference */
71 NULL, /* characters */
72 NULL, /* ignorableWhitespace */
73 NULL, /* processingInstruction */
74 NULL, /* comment */
75 NULL, /* xmlParserWarning */
76 NULL, /* xmlParserError */
77 NULL, /* xmlParserError */
Daniel Veillardb05deb71999-08-10 19:04:08 +000078 NULL, /* getParameterEntity */
Daniel Veillardcf461992000-03-14 18:30:20 +000079 NULL, /* cdataBlock; */
Daniel Veillard5099ae81999-04-21 20:12:07 +000080};
81
82xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
Daniel Veillard011b63c1999-06-02 17:44:04 +000083extern xmlSAXHandlerPtr debugSAXHandler;
Daniel Veillard5099ae81999-04-21 20:12:07 +000084
Daniel Veillard5099ae81999-04-21 20:12:07 +000085/************************************************************************
86 * *
87 * Debug Handlers *
88 * *
89 ************************************************************************/
90
91/**
92 * isStandaloneDebug:
93 * @ctxt: An XML parser context
94 *
95 * Is this document tagged standalone ?
96 *
97 * Returns 1 if true
98 */
99int
Daniel Veillardb96e6431999-08-29 21:02:19 +0000100isStandaloneDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000101{
Daniel Veillard27d88741999-05-29 11:51:49 +0000102 fprintf(stdout, "SAX.isStandalone()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000103 return(0);
104}
105
106/**
107 * hasInternalSubsetDebug:
108 * @ctxt: An XML parser context
109 *
110 * Does this document has an internal subset
111 *
112 * Returns 1 if true
113 */
114int
Daniel Veillardb96e6431999-08-29 21:02:19 +0000115hasInternalSubsetDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000116{
Daniel Veillard27d88741999-05-29 11:51:49 +0000117 fprintf(stdout, "SAX.hasInternalSubset()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000118 return(0);
119}
120
121/**
122 * hasExternalSubsetDebug:
123 * @ctxt: An XML parser context
124 *
125 * Does this document has an external subset
126 *
127 * Returns 1 if true
128 */
129int
Daniel Veillardb96e6431999-08-29 21:02:19 +0000130hasExternalSubsetDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000131{
Daniel Veillard27d88741999-05-29 11:51:49 +0000132 fprintf(stdout, "SAX.hasExternalSubset()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000133 return(0);
134}
135
136/**
Daniel Veillard06047432000-04-24 11:33:38 +0000137 * internalSubsetDebug:
Daniel Veillard5099ae81999-04-21 20:12:07 +0000138 * @ctxt: An XML parser context
139 *
140 * Does this document has an internal subset
141 */
142void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000143internalSubsetDebug(void *ctx, const xmlChar *name,
144 const xmlChar *ExternalID, const xmlChar *SystemID)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000145{
Daniel Veillard808a3f12000-08-17 13:50:51 +0000146 fprintf(stdout, "SAX.internalSubset(%s,", name);
147 if (ExternalID == NULL)
148 fprintf(stdout, " ,");
149 else
150 fprintf(stdout, " %s,", ExternalID);
151 if (SystemID == NULL)
152 fprintf(stdout, " )\n");
153 else
154 fprintf(stdout, " %s)\n", SystemID);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000155}
156
157/**
158 * resolveEntityDebug:
159 * @ctxt: An XML parser context
160 * @publicId: The public ID of the entity
161 * @systemId: The system ID of the entity
162 *
163 * Special entity resolver, better left to the parser, it has
164 * more context than the application layer.
165 * The default behaviour is to NOT resolve the entities, in that case
166 * the ENTITY_REF nodes are built in the structure (and the parameter
167 * values).
168 *
169 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
170 */
171xmlParserInputPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000172resolveEntityDebug(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000173{
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000174 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
Daniel Veillardb96e6431999-08-29 21:02:19 +0000175
Daniel Veillard14fff061999-06-22 21:49:07 +0000176
177 fprintf(stdout, "SAX.resolveEntity(");
178 if (publicId != NULL)
179 fprintf(stdout, "%s", (char *)publicId);
180 else
181 fprintf(stdout, " ");
182 if (systemId != NULL)
183 fprintf(stdout, ", %s)\n", (char *)systemId);
184 else
185 fprintf(stdout, ", )\n");
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000186/*********
Daniel Veillard011b63c1999-06-02 17:44:04 +0000187 if (systemId != NULL) {
Daniel Veillardb96e6431999-08-29 21:02:19 +0000188 return(xmlNewInputFromFile(ctxt, (char *) systemId));
Daniel Veillard011b63c1999-06-02 17:44:04 +0000189 }
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000190 *********/
Daniel Veillard5099ae81999-04-21 20:12:07 +0000191 return(NULL);
192}
193
194/**
195 * getEntityDebug:
196 * @ctxt: An XML parser context
197 * @name: The entity name
198 *
199 * Get an entity by name
200 *
201 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
202 */
203xmlEntityPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000204getEntityDebug(void *ctx, const xmlChar *name)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000205{
Daniel Veillard27d88741999-05-29 11:51:49 +0000206 fprintf(stdout, "SAX.getEntity(%s)\n", name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000207 return(NULL);
208}
209
Daniel Veillardb05deb71999-08-10 19:04:08 +0000210/**
211 * getParameterEntityDebug:
212 * @ctxt: An XML parser context
213 * @name: The entity name
214 *
215 * Get a parameter entity by name
216 *
217 * Returns the xmlParserInputPtr
218 */
219xmlEntityPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000220getParameterEntityDebug(void *ctx, const xmlChar *name)
Daniel Veillardb05deb71999-08-10 19:04:08 +0000221{
222 fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
223 return(NULL);
224}
225
Daniel Veillard5099ae81999-04-21 20:12:07 +0000226
227/**
228 * entityDeclDebug:
229 * @ctxt: An XML parser context
230 * @name: the entity name
231 * @type: the entity type
232 * @publicId: The public ID of the entity
233 * @systemId: The system ID of the entity
234 * @content: the entity value (without processing).
235 *
236 * An entity definition has been parsed
237 */
238void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000239entityDeclDebug(void *ctx, const xmlChar *name, int type,
240 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000241{
Daniel Veillard27d88741999-05-29 11:51:49 +0000242 fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000243 name, type, publicId, systemId, content);
244}
245
246/**
247 * attributeDeclDebug:
248 * @ctxt: An XML parser context
249 * @name: the attribute name
250 * @type: the attribute type
251 *
252 * An attribute definition has been parsed
253 */
254void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000255attributeDeclDebug(void *ctx, const xmlChar *elem, const xmlChar *name,
256 int type, int def, const xmlChar *defaultValue,
Daniel Veillard5099ae81999-04-21 20:12:07 +0000257 xmlEnumerationPtr tree)
258{
Daniel Veillard7e99c632000-10-06 12:59:53 +0000259 if (defaultValue == NULL)
260 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, NULL, ...)\n",
261 elem, name, type, def);
262 else
263 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000264 elem, name, type, def, defaultValue);
265}
266
267/**
268 * elementDeclDebug:
269 * @ctxt: An XML parser context
270 * @name: the element name
271 * @type: the element type
272 * @content: the element value (without processing).
273 *
274 * An element definition has been parsed
275 */
276void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000277elementDeclDebug(void *ctx, const xmlChar *name, int type,
Daniel Veillard5099ae81999-04-21 20:12:07 +0000278 xmlElementContentPtr content)
279{
Daniel Veillard27d88741999-05-29 11:51:49 +0000280 fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000281 name, type);
282}
283
284/**
285 * notationDeclDebug:
286 * @ctxt: An XML parser context
287 * @name: The name of the notation
288 * @publicId: The public ID of the entity
289 * @systemId: The system ID of the entity
290 *
291 * What to do when a notation declaration has been parsed.
Daniel Veillard5099ae81999-04-21 20:12:07 +0000292 */
293void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000294notationDeclDebug(void *ctx, const xmlChar *name,
295 const xmlChar *publicId, const xmlChar *systemId)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000296{
Daniel Veillard27d88741999-05-29 11:51:49 +0000297 fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000298 (char *) name, (char *) publicId, (char *) systemId);
299}
300
301/**
302 * unparsedEntityDeclDebug:
303 * @ctxt: An XML parser context
304 * @name: The name of the entity
305 * @publicId: The public ID of the entity
306 * @systemId: The system ID of the entity
307 * @notationName: the name of the notation
308 *
309 * What to do when an unparsed entity declaration is parsed
Daniel Veillard5099ae81999-04-21 20:12:07 +0000310 */
311void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000312unparsedEntityDeclDebug(void *ctx, const xmlChar *name,
313 const xmlChar *publicId, const xmlChar *systemId,
314 const xmlChar *notationName)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000315{
Daniel Veillard27d88741999-05-29 11:51:49 +0000316 fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000317 (char *) name, (char *) publicId, (char *) systemId,
318 (char *) notationName);
319}
320
321/**
322 * setDocumentLocatorDebug:
323 * @ctxt: An XML parser context
324 * @loc: A SAX Locator
325 *
326 * Receive the document locator at startup, actually xmlDefaultSAXLocator
327 * Everything is available on the context, so this is useless in our case.
328 */
329void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000330setDocumentLocatorDebug(void *ctx, xmlSAXLocatorPtr loc)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000331{
Daniel Veillard27d88741999-05-29 11:51:49 +0000332 fprintf(stdout, "SAX.setDocumentLocator()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000333}
334
335/**
336 * startDocumentDebug:
337 * @ctxt: An XML parser context
338 *
339 * called when the document start being processed.
340 */
341void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000342startDocumentDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000343{
Daniel Veillard27d88741999-05-29 11:51:49 +0000344 fprintf(stdout, "SAX.startDocument()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000345}
346
347/**
348 * endDocumentDebug:
349 * @ctxt: An XML parser context
350 *
351 * called when the document end has been detected.
352 */
353void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000354endDocumentDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000355{
Daniel Veillard27d88741999-05-29 11:51:49 +0000356 fprintf(stdout, "SAX.endDocument()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000357}
358
359/**
360 * startElementDebug:
361 * @ctxt: An XML parser context
362 * @name: The element name
363 *
364 * called when an opening tag has been processed.
Daniel Veillard5099ae81999-04-21 20:12:07 +0000365 */
366void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000367startElementDebug(void *ctx, const xmlChar *name, const xmlChar **atts)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000368{
369 int i;
370
Daniel Veillard27d88741999-05-29 11:51:49 +0000371 fprintf(stdout, "SAX.startElement(%s", (char *) name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000372 if (atts != NULL) {
373 for (i = 0;(atts[i] != NULL);i++) {
Daniel Veillard27d88741999-05-29 11:51:49 +0000374 fprintf(stdout, ", %s='", atts[i++]);
Daniel Veillard808a3f12000-08-17 13:50:51 +0000375 if (atts[i] != NULL)
376 fprintf(stdout, "%s'", atts[i]);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000377 }
378 }
Daniel Veillard27d88741999-05-29 11:51:49 +0000379 fprintf(stdout, ")\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000380}
381
382/**
383 * endElementDebug:
384 * @ctxt: An XML parser context
385 * @name: The element name
386 *
387 * called when the end of an element has been detected.
388 */
389void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000390endElementDebug(void *ctx, const xmlChar *name)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000391{
Daniel Veillard27d88741999-05-29 11:51:49 +0000392 fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000393}
394
395/**
396 * charactersDebug:
397 * @ctxt: An XML parser context
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000398 * @ch: a xmlChar string
399 * @len: the number of xmlChar
Daniel Veillard5099ae81999-04-21 20:12:07 +0000400 *
401 * receiving some chars from the parser.
402 * Question: how much at a time ???
403 */
404void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000405charactersDebug(void *ctx, const xmlChar *ch, int len)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000406{
Daniel Veillard87b95392000-08-12 21:12:04 +0000407 char output[40];
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000408 int i;
409
Daniel Veillard87b95392000-08-12 21:12:04 +0000410 for (i = 0;(i<len) && (i < 30);i++)
411 output[i] = ch[i];
412 output[i] = 0;
413
414 fprintf(stdout, "SAX.characters(%s, %d)\n", output, len);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000415}
416
417/**
418 * referenceDebug:
419 * @ctxt: An XML parser context
420 * @name: The entity name
421 *
422 * called when an entity reference is detected.
423 */
424void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000425referenceDebug(void *ctx, const xmlChar *name)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000426{
Daniel Veillard27d88741999-05-29 11:51:49 +0000427 fprintf(stdout, "SAX.reference(%s)\n", name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000428}
429
430/**
431 * ignorableWhitespaceDebug:
432 * @ctxt: An XML parser context
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000433 * @ch: a xmlChar string
Daniel Veillard5099ae81999-04-21 20:12:07 +0000434 * @start: the first char in the string
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000435 * @len: the number of xmlChar
Daniel Veillard5099ae81999-04-21 20:12:07 +0000436 *
437 * receiving some ignorable whitespaces from the parser.
438 * Question: how much at a time ???
439 */
440void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000441ignorableWhitespaceDebug(void *ctx, const xmlChar *ch, int len)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000442{
Daniel Veillard87b95392000-08-12 21:12:04 +0000443 char output[40];
444 int i;
445
446 for (i = 0;(i<len) && (i < 30);i++)
447 output[i] = ch[i];
448 output[i] = 0;
449 fprintf(stdout, "SAX.ignorableWhitespace(%s, %d)\n", output, len);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000450}
451
452/**
453 * processingInstructionDebug:
454 * @ctxt: An XML parser context
455 * @target: the target name
456 * @data: the PI data's
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000457 * @len: the number of xmlChar
Daniel Veillard5099ae81999-04-21 20:12:07 +0000458 *
459 * A processing instruction has been parsed.
460 */
461void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000462processingInstructionDebug(void *ctx, const xmlChar *target,
463 const xmlChar *data)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000464{
Daniel Veillard27d88741999-05-29 11:51:49 +0000465 fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000466 (char *) target, (char *) data);
467}
468
469/**
Daniel Veillardcf461992000-03-14 18:30:20 +0000470 * cdataBlockDebug:
471 * @ctx: the user data (XML parser context)
472 * @value: The pcdata content
473 * @len: the block length
474 *
475 * called when a pcdata block has been parsed
476 */
477void
478cdataBlockDebug(void *ctx, const xmlChar *value, int len)
479{
Daniel Veillard39915622000-10-15 10:06:55 +0000480 fprintf(stdout, "SAX.pcdata(%.20s, %d)\n",
Daniel Veillardcf461992000-03-14 18:30:20 +0000481 (char *) value, len);
482}
483
484/**
Daniel Veillard5099ae81999-04-21 20:12:07 +0000485 * commentDebug:
486 * @ctxt: An XML parser context
487 * @value: the comment content
488 *
489 * A comment has been parsed.
490 */
491void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000492commentDebug(void *ctx, const xmlChar *value)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000493{
Daniel Veillard27d88741999-05-29 11:51:49 +0000494 fprintf(stdout, "SAX.comment(%s)\n", value);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000495}
496
497/**
498 * warningDebug:
499 * @ctxt: An XML parser context
500 * @msg: the message to display/transmit
501 * @...: extra parameters for the message display
502 *
503 * Display and format a warning messages, gives file, line, position and
504 * extra parameters.
505 */
506void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000507warningDebug(void *ctx, const char *msg, ...)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000508{
509 va_list args;
510
511 va_start(args, msg);
Daniel Veillard27d88741999-05-29 11:51:49 +0000512 fprintf(stdout, "SAX.warning: ");
513 vfprintf(stdout, msg, args);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000514 va_end(args);
515}
516
517/**
518 * errorDebug:
519 * @ctxt: An XML parser context
520 * @msg: the message to display/transmit
521 * @...: extra parameters for the message display
522 *
523 * Display and format a error messages, gives file, line, position and
524 * extra parameters.
525 */
526void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000527errorDebug(void *ctx, const char *msg, ...)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000528{
529 va_list args;
530
531 va_start(args, msg);
Daniel Veillard27d88741999-05-29 11:51:49 +0000532 fprintf(stdout, "SAX.error: ");
533 vfprintf(stdout, msg, args);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000534 va_end(args);
535}
536
537/**
538 * fatalErrorDebug:
539 * @ctxt: An XML parser context
540 * @msg: the message to display/transmit
541 * @...: extra parameters for the message display
542 *
543 * Display and format a fatalError messages, gives file, line, position and
544 * extra parameters.
545 */
546void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000547fatalErrorDebug(void *ctx, const char *msg, ...)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000548{
549 va_list args;
550
551 va_start(args, msg);
Daniel Veillard27d88741999-05-29 11:51:49 +0000552 fprintf(stdout, "SAX.fatalError: ");
553 vfprintf(stdout, msg, args);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000554 va_end(args);
555}
556
557xmlSAXHandler debugSAXHandlerStruct = {
558 internalSubsetDebug,
559 isStandaloneDebug,
560 hasInternalSubsetDebug,
561 hasExternalSubsetDebug,
562 resolveEntityDebug,
563 getEntityDebug,
564 entityDeclDebug,
565 notationDeclDebug,
566 attributeDeclDebug,
567 elementDeclDebug,
568 unparsedEntityDeclDebug,
569 setDocumentLocatorDebug,
570 startDocumentDebug,
571 endDocumentDebug,
572 startElementDebug,
573 endElementDebug,
574 referenceDebug,
575 charactersDebug,
576 ignorableWhitespaceDebug,
577 processingInstructionDebug,
578 commentDebug,
579 warningDebug,
580 errorDebug,
581 fatalErrorDebug,
Daniel Veillardb05deb71999-08-10 19:04:08 +0000582 getParameterEntityDebug,
Daniel Veillardcf461992000-03-14 18:30:20 +0000583 cdataBlockDebug
Daniel Veillard5099ae81999-04-21 20:12:07 +0000584};
585
586xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
587
588/************************************************************************
589 * *
590 * Debug *
591 * *
592 ************************************************************************/
593
594void parseAndPrintFile(char *filename) {
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000595 int res;
Daniel Veillard5099ae81999-04-21 20:12:07 +0000596
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000597 if (push) {
598 FILE *f;
Daniel Veillard5099ae81999-04-21 20:12:07 +0000599
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000600 /*
601 * Empty callbacks for checking
602 */
603 f = fopen(filename, "r");
604 if (f != NULL) {
605 int res;
606 char chars[10];
607 xmlParserCtxtPtr ctxt;
608
609 res = fread(chars, 1, 4, f);
610 if (res > 0) {
611 ctxt = xmlCreatePushParserCtxt(emptySAXHandler, NULL,
612 chars, res, filename);
613 while ((res = fread(chars, 1, 3, f)) > 0) {
614 xmlParseChunk(ctxt, chars, res, 0);
615 }
616 xmlParseChunk(ctxt, chars, 0, 1);
617 xmlFreeParserCtxt(ctxt);
618 }
619 fclose(f);
620 } else {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000621 xmlGenericError(xmlGenericErrorContext,
622 "Cannot read file %s\n", filename);
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000623 }
624 /*
625 * Debug callback
626 */
627 f = fopen(filename, "r");
628 if (f != NULL) {
629 int res;
630 char chars[10];
631 xmlParserCtxtPtr ctxt;
632
633 res = fread(chars, 1, 4, f);
634 if (res > 0) {
635 ctxt = xmlCreatePushParserCtxt(debugSAXHandler, NULL,
636 chars, res, filename);
637 while ((res = fread(chars, 1, 3, f)) > 0) {
638 xmlParseChunk(ctxt, chars, res, 0);
639 }
640 res = xmlParseChunk(ctxt, chars, 0, 1);
641 xmlFreeParserCtxt(ctxt);
642 if (res != 0) {
643 fprintf(stdout,
644 "xmlSAXUserParseFile returned error %d\n", res);
645 }
646 }
647 fclose(f);
648 }
649 } else {
Daniel Veillard5e873c42000-04-12 13:27:38 +0000650 if (!speed) {
651 /*
652 * Empty callbacks for checking
653 */
654 res = xmlSAXUserParseFile(emptySAXHandler, NULL, filename);
655 if (res != 0) {
656 fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res);
657 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000658
Daniel Veillard5e873c42000-04-12 13:27:38 +0000659 /*
660 * Debug callback
661 */
662 res = xmlSAXUserParseFile(debugSAXHandler, NULL, filename);
663 if (res != 0) {
664 fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res);
665 }
666 } else {
667 /*
668 * test 100x the SAX parse
669 */
670 int i;
671
672 for (i = 0; i<100;i++)
673 res = xmlSAXUserParseFile(emptySAXHandler, NULL, filename);
674 if (res != 0) {
675 fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res);
676 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000677 }
Daniel Veillard5099ae81999-04-21 20:12:07 +0000678 }
679}
680
Daniel Veillard5099ae81999-04-21 20:12:07 +0000681
682int main(int argc, char **argv) {
683 int i;
684 int files = 0;
685
686 for (i = 1; i < argc ; i++) {
687 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
688 debug++;
689 else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
690 copy++;
691 else if ((!strcmp(argv[i], "-recover")) ||
692 (!strcmp(argv[i], "--recover")))
693 recovery++;
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000694 else if ((!strcmp(argv[i], "-push")) ||
695 (!strcmp(argv[i], "--push")))
696 push++;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000697 else if ((!strcmp(argv[i], "-speed")) ||
698 (!strcmp(argv[i], "--speed")))
699 speed++;
Daniel Veillard5099ae81999-04-21 20:12:07 +0000700 }
701 for (i = 1; i < argc ; i++) {
702 if (argv[i][0] != '-') {
703 parseAndPrintFile(argv[i]);
704 files ++;
705 }
706 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000707 xmlCleanupParser();
708 xmlMemoryDump();
Daniel Veillard5099ae81999-04-21 20:12:07 +0000709
710 return(0);
711}