blob: d5079afb13f4a6a37866f2461015fffa2bf768a5 [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 Veillard5099ae81999-04-21 20:12:07 +000049
50xmlSAXHandler emptySAXHandlerStruct = {
51 NULL, /* internalSubset */
52 NULL, /* isStandalone */
53 NULL, /* hasInternalSubset */
54 NULL, /* hasExternalSubset */
55 NULL, /* resolveEntity */
56 NULL, /* getEntity */
57 NULL, /* entityDecl */
58 NULL, /* notationDecl */
59 NULL, /* attributeDecl */
60 NULL, /* elementDecl */
61 NULL, /* unparsedEntityDecl */
62 NULL, /* setDocumentLocator */
63 NULL, /* startDocument */
64 NULL, /* endDocument */
65 NULL, /* startElement */
66 NULL, /* endElement */
67 NULL, /* reference */
68 NULL, /* characters */
69 NULL, /* ignorableWhitespace */
70 NULL, /* processingInstruction */
71 NULL, /* comment */
72 NULL, /* xmlParserWarning */
73 NULL, /* xmlParserError */
74 NULL, /* xmlParserError */
Daniel Veillardb05deb71999-08-10 19:04:08 +000075 NULL, /* getParameterEntity */
Daniel Veillardcf461992000-03-14 18:30:20 +000076 NULL, /* cdataBlock; */
Daniel Veillard5099ae81999-04-21 20:12:07 +000077};
78
79xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
Daniel Veillard011b63c1999-06-02 17:44:04 +000080extern xmlSAXHandlerPtr debugSAXHandler;
Daniel Veillard5099ae81999-04-21 20:12:07 +000081
Daniel Veillard5099ae81999-04-21 20:12:07 +000082/************************************************************************
83 * *
84 * Debug Handlers *
85 * *
86 ************************************************************************/
87
88/**
89 * isStandaloneDebug:
90 * @ctxt: An XML parser context
91 *
92 * Is this document tagged standalone ?
93 *
94 * Returns 1 if true
95 */
96int
Daniel Veillardb96e6431999-08-29 21:02:19 +000097isStandaloneDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +000098{
Daniel Veillard27d88741999-05-29 11:51:49 +000099 fprintf(stdout, "SAX.isStandalone()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000100 return(0);
101}
102
103/**
104 * hasInternalSubsetDebug:
105 * @ctxt: An XML parser context
106 *
107 * Does this document has an internal subset
108 *
109 * Returns 1 if true
110 */
111int
Daniel Veillardb96e6431999-08-29 21:02:19 +0000112hasInternalSubsetDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000113{
Daniel Veillard27d88741999-05-29 11:51:49 +0000114 fprintf(stdout, "SAX.hasInternalSubset()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000115 return(0);
116}
117
118/**
119 * hasExternalSubsetDebug:
120 * @ctxt: An XML parser context
121 *
122 * Does this document has an external subset
123 *
124 * Returns 1 if true
125 */
126int
Daniel Veillardb96e6431999-08-29 21:02:19 +0000127hasExternalSubsetDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000128{
Daniel Veillard27d88741999-05-29 11:51:49 +0000129 fprintf(stdout, "SAX.hasExternalSubset()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000130 return(0);
131}
132
133/**
134 * hasInternalSubsetDebug:
135 * @ctxt: An XML parser context
136 *
137 * Does this document has an internal subset
138 */
139void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000140internalSubsetDebug(void *ctx, const xmlChar *name,
141 const xmlChar *ExternalID, const xmlChar *SystemID)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000142{
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000143 /* xmlDtdPtr externalSubset; */
Daniel Veillard011b63c1999-06-02 17:44:04 +0000144
Daniel Veillard27d88741999-05-29 11:51:49 +0000145 fprintf(stdout, "SAX.internalSubset(%s, %s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000146 name, ExternalID, SystemID);
Daniel Veillard011b63c1999-06-02 17:44:04 +0000147
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000148/***********
Daniel Veillard011b63c1999-06-02 17:44:04 +0000149 if ((ExternalID != NULL) || (SystemID != NULL)) {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000150 externalSubset = xmlParseDTD(ExternalID, SystemID);
151 if (externalSubset != NULL) {
152 xmlFreeDtd(externalSubset);
153 }
Daniel Veillard011b63c1999-06-02 17:44:04 +0000154 }
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000155 ***********/
Daniel Veillard5099ae81999-04-21 20:12:07 +0000156}
157
158/**
159 * resolveEntityDebug:
160 * @ctxt: An XML parser context
161 * @publicId: The public ID of the entity
162 * @systemId: The system ID of the entity
163 *
164 * Special entity resolver, better left to the parser, it has
165 * more context than the application layer.
166 * The default behaviour is to NOT resolve the entities, in that case
167 * the ENTITY_REF nodes are built in the structure (and the parameter
168 * values).
169 *
170 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
171 */
172xmlParserInputPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000173resolveEntityDebug(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000174{
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000175 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
Daniel Veillardb96e6431999-08-29 21:02:19 +0000176
Daniel Veillard14fff061999-06-22 21:49:07 +0000177
178 fprintf(stdout, "SAX.resolveEntity(");
179 if (publicId != NULL)
180 fprintf(stdout, "%s", (char *)publicId);
181 else
182 fprintf(stdout, " ");
183 if (systemId != NULL)
184 fprintf(stdout, ", %s)\n", (char *)systemId);
185 else
186 fprintf(stdout, ", )\n");
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000187/*********
Daniel Veillard011b63c1999-06-02 17:44:04 +0000188 if (systemId != NULL) {
Daniel Veillardb96e6431999-08-29 21:02:19 +0000189 return(xmlNewInputFromFile(ctxt, (char *) systemId));
Daniel Veillard011b63c1999-06-02 17:44:04 +0000190 }
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000191 *********/
Daniel Veillard5099ae81999-04-21 20:12:07 +0000192 return(NULL);
193}
194
195/**
196 * getEntityDebug:
197 * @ctxt: An XML parser context
198 * @name: The entity name
199 *
200 * Get an entity by name
201 *
202 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
203 */
204xmlEntityPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000205getEntityDebug(void *ctx, const xmlChar *name)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000206{
Daniel Veillard27d88741999-05-29 11:51:49 +0000207 fprintf(stdout, "SAX.getEntity(%s)\n", name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000208 return(NULL);
209}
210
Daniel Veillardb05deb71999-08-10 19:04:08 +0000211/**
212 * getParameterEntityDebug:
213 * @ctxt: An XML parser context
214 * @name: The entity name
215 *
216 * Get a parameter entity by name
217 *
218 * Returns the xmlParserInputPtr
219 */
220xmlEntityPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000221getParameterEntityDebug(void *ctx, const xmlChar *name)
Daniel Veillardb05deb71999-08-10 19:04:08 +0000222{
223 fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
224 return(NULL);
225}
226
Daniel Veillard5099ae81999-04-21 20:12:07 +0000227
228/**
229 * entityDeclDebug:
230 * @ctxt: An XML parser context
231 * @name: the entity name
232 * @type: the entity type
233 * @publicId: The public ID of the entity
234 * @systemId: The system ID of the entity
235 * @content: the entity value (without processing).
236 *
237 * An entity definition has been parsed
238 */
239void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000240entityDeclDebug(void *ctx, const xmlChar *name, int type,
241 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000242{
Daniel Veillard27d88741999-05-29 11:51:49 +0000243 fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000244 name, type, publicId, systemId, content);
245}
246
247/**
248 * attributeDeclDebug:
249 * @ctxt: An XML parser context
250 * @name: the attribute name
251 * @type: the attribute type
252 *
253 * An attribute definition has been parsed
254 */
255void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000256attributeDeclDebug(void *ctx, const xmlChar *elem, const xmlChar *name,
257 int type, int def, const xmlChar *defaultValue,
Daniel Veillard5099ae81999-04-21 20:12:07 +0000258 xmlEnumerationPtr tree)
259{
Daniel Veillard27d88741999-05-29 11:51:49 +0000260 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000261 elem, name, type, def, defaultValue);
262}
263
264/**
265 * elementDeclDebug:
266 * @ctxt: An XML parser context
267 * @name: the element name
268 * @type: the element type
269 * @content: the element value (without processing).
270 *
271 * An element definition has been parsed
272 */
273void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000274elementDeclDebug(void *ctx, const xmlChar *name, int type,
Daniel Veillard5099ae81999-04-21 20:12:07 +0000275 xmlElementContentPtr content)
276{
Daniel Veillard27d88741999-05-29 11:51:49 +0000277 fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000278 name, type);
279}
280
281/**
282 * notationDeclDebug:
283 * @ctxt: An XML parser context
284 * @name: The name of the notation
285 * @publicId: The public ID of the entity
286 * @systemId: The system ID of the entity
287 *
288 * What to do when a notation declaration has been parsed.
Daniel Veillard5099ae81999-04-21 20:12:07 +0000289 */
290void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000291notationDeclDebug(void *ctx, const xmlChar *name,
292 const xmlChar *publicId, const xmlChar *systemId)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000293{
Daniel Veillard27d88741999-05-29 11:51:49 +0000294 fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000295 (char *) name, (char *) publicId, (char *) systemId);
296}
297
298/**
299 * unparsedEntityDeclDebug:
300 * @ctxt: An XML parser context
301 * @name: The name of the entity
302 * @publicId: The public ID of the entity
303 * @systemId: The system ID of the entity
304 * @notationName: the name of the notation
305 *
306 * What to do when an unparsed entity declaration is parsed
Daniel Veillard5099ae81999-04-21 20:12:07 +0000307 */
308void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000309unparsedEntityDeclDebug(void *ctx, const xmlChar *name,
310 const xmlChar *publicId, const xmlChar *systemId,
311 const xmlChar *notationName)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000312{
Daniel Veillard27d88741999-05-29 11:51:49 +0000313 fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000314 (char *) name, (char *) publicId, (char *) systemId,
315 (char *) notationName);
316}
317
318/**
319 * setDocumentLocatorDebug:
320 * @ctxt: An XML parser context
321 * @loc: A SAX Locator
322 *
323 * Receive the document locator at startup, actually xmlDefaultSAXLocator
324 * Everything is available on the context, so this is useless in our case.
325 */
326void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000327setDocumentLocatorDebug(void *ctx, xmlSAXLocatorPtr loc)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000328{
Daniel Veillard27d88741999-05-29 11:51:49 +0000329 fprintf(stdout, "SAX.setDocumentLocator()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000330}
331
332/**
333 * startDocumentDebug:
334 * @ctxt: An XML parser context
335 *
336 * called when the document start being processed.
337 */
338void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000339startDocumentDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000340{
Daniel Veillard27d88741999-05-29 11:51:49 +0000341 fprintf(stdout, "SAX.startDocument()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000342}
343
344/**
345 * endDocumentDebug:
346 * @ctxt: An XML parser context
347 *
348 * called when the document end has been detected.
349 */
350void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000351endDocumentDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000352{
Daniel Veillard27d88741999-05-29 11:51:49 +0000353 fprintf(stdout, "SAX.endDocument()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000354}
355
356/**
357 * startElementDebug:
358 * @ctxt: An XML parser context
359 * @name: The element name
360 *
361 * called when an opening tag has been processed.
Daniel Veillard5099ae81999-04-21 20:12:07 +0000362 */
363void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000364startElementDebug(void *ctx, const xmlChar *name, const xmlChar **atts)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000365{
366 int i;
367
Daniel Veillard27d88741999-05-29 11:51:49 +0000368 fprintf(stdout, "SAX.startElement(%s", (char *) name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000369 if (atts != NULL) {
370 for (i = 0;(atts[i] != NULL);i++) {
Daniel Veillard27d88741999-05-29 11:51:49 +0000371 fprintf(stdout, ", %s='", atts[i++]);
372 fprintf(stdout, "%s'", atts[i]);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000373 }
374 }
Daniel Veillard27d88741999-05-29 11:51:49 +0000375 fprintf(stdout, ")\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000376}
377
378/**
379 * endElementDebug:
380 * @ctxt: An XML parser context
381 * @name: The element name
382 *
383 * called when the end of an element has been detected.
384 */
385void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000386endElementDebug(void *ctx, const xmlChar *name)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000387{
Daniel Veillard27d88741999-05-29 11:51:49 +0000388 fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000389}
390
391/**
392 * charactersDebug:
393 * @ctxt: An XML parser context
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000394 * @ch: a xmlChar string
395 * @len: the number of xmlChar
Daniel Veillard5099ae81999-04-21 20:12:07 +0000396 *
397 * receiving some chars from the parser.
398 * Question: how much at a time ???
399 */
400void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000401charactersDebug(void *ctx, const xmlChar *ch, int len)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000402{
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000403 int i;
404
405 fprintf(stdout, "SAX.characters(");
406 for (i = 0;(i < len) && (i < 30);i++)
407 fprintf(stdout, "%c", ch[i]);
408 fprintf(stdout, ", %d)\n", len);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000409}
410
411/**
412 * referenceDebug:
413 * @ctxt: An XML parser context
414 * @name: The entity name
415 *
416 * called when an entity reference is detected.
417 */
418void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000419referenceDebug(void *ctx, const xmlChar *name)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000420{
Daniel Veillard27d88741999-05-29 11:51:49 +0000421 fprintf(stdout, "SAX.reference(%s)\n", name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000422}
423
424/**
425 * ignorableWhitespaceDebug:
426 * @ctxt: An XML parser context
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000427 * @ch: a xmlChar string
Daniel Veillard5099ae81999-04-21 20:12:07 +0000428 * @start: the first char in the string
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000429 * @len: the number of xmlChar
Daniel Veillard5099ae81999-04-21 20:12:07 +0000430 *
431 * receiving some ignorable whitespaces from the parser.
432 * Question: how much at a time ???
433 */
434void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000435ignorableWhitespaceDebug(void *ctx, const xmlChar *ch, int len)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000436{
Daniel Veillard27d88741999-05-29 11:51:49 +0000437 fprintf(stdout, "SAX.ignorableWhitespace(%.30s, %d)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000438 (char *) ch, len);
439}
440
441/**
442 * processingInstructionDebug:
443 * @ctxt: An XML parser context
444 * @target: the target name
445 * @data: the PI data's
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000446 * @len: the number of xmlChar
Daniel Veillard5099ae81999-04-21 20:12:07 +0000447 *
448 * A processing instruction has been parsed.
449 */
450void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000451processingInstructionDebug(void *ctx, const xmlChar *target,
452 const xmlChar *data)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000453{
Daniel Veillard27d88741999-05-29 11:51:49 +0000454 fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000455 (char *) target, (char *) data);
456}
457
458/**
Daniel Veillardcf461992000-03-14 18:30:20 +0000459 * cdataBlockDebug:
460 * @ctx: the user data (XML parser context)
461 * @value: The pcdata content
462 * @len: the block length
463 *
464 * called when a pcdata block has been parsed
465 */
466void
467cdataBlockDebug(void *ctx, const xmlChar *value, int len)
468{
469 fprintf(stderr, "SAX.pcdata(%.20s, %d)\n",
470 (char *) value, len);
471}
472
473/**
Daniel Veillard5099ae81999-04-21 20:12:07 +0000474 * commentDebug:
475 * @ctxt: An XML parser context
476 * @value: the comment content
477 *
478 * A comment has been parsed.
479 */
480void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000481commentDebug(void *ctx, const xmlChar *value)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000482{
Daniel Veillard27d88741999-05-29 11:51:49 +0000483 fprintf(stdout, "SAX.comment(%s)\n", value);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000484}
485
486/**
487 * warningDebug:
488 * @ctxt: An XML parser context
489 * @msg: the message to display/transmit
490 * @...: extra parameters for the message display
491 *
492 * Display and format a warning messages, gives file, line, position and
493 * extra parameters.
494 */
495void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000496warningDebug(void *ctx, const char *msg, ...)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000497{
498 va_list args;
499
500 va_start(args, msg);
Daniel Veillard27d88741999-05-29 11:51:49 +0000501 fprintf(stdout, "SAX.warning: ");
502 vfprintf(stdout, msg, args);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000503 va_end(args);
504}
505
506/**
507 * errorDebug:
508 * @ctxt: An XML parser context
509 * @msg: the message to display/transmit
510 * @...: extra parameters for the message display
511 *
512 * Display and format a error messages, gives file, line, position and
513 * extra parameters.
514 */
515void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000516errorDebug(void *ctx, const char *msg, ...)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000517{
518 va_list args;
519
520 va_start(args, msg);
Daniel Veillard27d88741999-05-29 11:51:49 +0000521 fprintf(stdout, "SAX.error: ");
522 vfprintf(stdout, msg, args);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000523 va_end(args);
524}
525
526/**
527 * fatalErrorDebug:
528 * @ctxt: An XML parser context
529 * @msg: the message to display/transmit
530 * @...: extra parameters for the message display
531 *
532 * Display and format a fatalError messages, gives file, line, position and
533 * extra parameters.
534 */
535void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000536fatalErrorDebug(void *ctx, const char *msg, ...)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000537{
538 va_list args;
539
540 va_start(args, msg);
Daniel Veillard27d88741999-05-29 11:51:49 +0000541 fprintf(stdout, "SAX.fatalError: ");
542 vfprintf(stdout, msg, args);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000543 va_end(args);
544}
545
546xmlSAXHandler debugSAXHandlerStruct = {
547 internalSubsetDebug,
548 isStandaloneDebug,
549 hasInternalSubsetDebug,
550 hasExternalSubsetDebug,
551 resolveEntityDebug,
552 getEntityDebug,
553 entityDeclDebug,
554 notationDeclDebug,
555 attributeDeclDebug,
556 elementDeclDebug,
557 unparsedEntityDeclDebug,
558 setDocumentLocatorDebug,
559 startDocumentDebug,
560 endDocumentDebug,
561 startElementDebug,
562 endElementDebug,
563 referenceDebug,
564 charactersDebug,
565 ignorableWhitespaceDebug,
566 processingInstructionDebug,
567 commentDebug,
568 warningDebug,
569 errorDebug,
570 fatalErrorDebug,
Daniel Veillardb05deb71999-08-10 19:04:08 +0000571 getParameterEntityDebug,
Daniel Veillardcf461992000-03-14 18:30:20 +0000572 cdataBlockDebug
Daniel Veillard5099ae81999-04-21 20:12:07 +0000573};
574
575xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
576
577/************************************************************************
578 * *
579 * Debug *
580 * *
581 ************************************************************************/
582
583void parseAndPrintFile(char *filename) {
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000584 int res;
Daniel Veillard5099ae81999-04-21 20:12:07 +0000585
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000586 if (push) {
587 FILE *f;
Daniel Veillard5099ae81999-04-21 20:12:07 +0000588
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000589 /*
590 * Empty callbacks for checking
591 */
592 f = fopen(filename, "r");
593 if (f != NULL) {
594 int res;
595 char chars[10];
596 xmlParserCtxtPtr ctxt;
597
598 res = fread(chars, 1, 4, f);
599 if (res > 0) {
600 ctxt = xmlCreatePushParserCtxt(emptySAXHandler, NULL,
601 chars, res, filename);
602 while ((res = fread(chars, 1, 3, f)) > 0) {
603 xmlParseChunk(ctxt, chars, res, 0);
604 }
605 xmlParseChunk(ctxt, chars, 0, 1);
606 xmlFreeParserCtxt(ctxt);
607 }
608 fclose(f);
609 } else {
610 fprintf(stderr, "Cannot read file %s\n", filename);
611 }
612 /*
613 * Debug callback
614 */
615 f = fopen(filename, "r");
616 if (f != NULL) {
617 int res;
618 char chars[10];
619 xmlParserCtxtPtr ctxt;
620
621 res = fread(chars, 1, 4, f);
622 if (res > 0) {
623 ctxt = xmlCreatePushParserCtxt(debugSAXHandler, NULL,
624 chars, res, filename);
625 while ((res = fread(chars, 1, 3, f)) > 0) {
626 xmlParseChunk(ctxt, chars, res, 0);
627 }
628 res = xmlParseChunk(ctxt, chars, 0, 1);
629 xmlFreeParserCtxt(ctxt);
630 if (res != 0) {
631 fprintf(stdout,
632 "xmlSAXUserParseFile returned error %d\n", res);
633 }
634 }
635 fclose(f);
636 }
637 } else {
638 /*
639 * Empty callbacks for checking
640 */
641 res = xmlSAXUserParseFile(emptySAXHandler, NULL, filename);
642 if (res != 0) {
643 fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res);
644 }
645
646 /*
647 * Debug callback
648 */
649 res = xmlSAXUserParseFile(debugSAXHandler, NULL, filename);
650 if (res != 0) {
651 fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res);
652 }
Daniel Veillard5099ae81999-04-21 20:12:07 +0000653 }
654}
655
Daniel Veillard5099ae81999-04-21 20:12:07 +0000656
657int main(int argc, char **argv) {
658 int i;
659 int files = 0;
660
661 for (i = 1; i < argc ; i++) {
662 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
663 debug++;
664 else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
665 copy++;
666 else if ((!strcmp(argv[i], "-recover")) ||
667 (!strcmp(argv[i], "--recover")))
668 recovery++;
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000669 else if ((!strcmp(argv[i], "-push")) ||
670 (!strcmp(argv[i], "--push")))
671 push++;
Daniel Veillard5099ae81999-04-21 20:12:07 +0000672 }
673 for (i = 1; i < argc ; i++) {
674 if (argv[i][0] != '-') {
675 parseAndPrintFile(argv[i]);
676 files ++;
677 }
678 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000679 xmlCleanupParser();
680 xmlMemoryDump();
Daniel Veillard5099ae81999-04-21 20:12:07 +0000681
682 return(0);
683}