source: trunk/third/libxml/testSAX.c @ 15360

Revision 15360, 14.8 KB checked in by ghudson, 24 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r15359, which included commits to RCS files with non-trunk default branches.
Line 
1/*
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
10#include "win32config.h"
11#else
12#include "config.h"
13#endif
14
15#include <stdio.h>
16#include <string.h>
17#include <stdarg.h>
18
19#ifdef HAVE_SYS_TYPES_H
20#include <sys/types.h>
21#endif
22#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
31#ifdef HAVE_STDLIB_H
32#include <stdlib.h>
33#endif
34#ifdef HAVE_STRING_H
35#include <string.h>
36#endif
37
38
39#include "parser.h"
40#include "parserInternals.h" /* only for xmlNewInputFromFile() */
41#include "tree.h"
42#include "debugXML.h"
43#include "xmlmemory.h"
44
45static int debug = 0;
46static int copy = 0;
47static int recovery = 0;
48static int push = 0;
49
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 */
75    NULL, /* getParameterEntity */
76};
77
78xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
79extern xmlSAXHandlerPtr debugSAXHandler;
80
81/************************************************************************
82 *                                                                      *
83 *                              Debug Handlers                          *
84 *                                                                      *
85 ************************************************************************/
86
87/**
88 * isStandaloneDebug:
89 * @ctxt:  An XML parser context
90 *
91 * Is this document tagged standalone ?
92 *
93 * Returns 1 if true
94 */
95int
96isStandaloneDebug(void *ctx)
97{
98    fprintf(stdout, "SAX.isStandalone()\n");
99    return(0);
100}
101
102/**
103 * hasInternalSubsetDebug:
104 * @ctxt:  An XML parser context
105 *
106 * Does this document has an internal subset
107 *
108 * Returns 1 if true
109 */
110int
111hasInternalSubsetDebug(void *ctx)
112{
113    fprintf(stdout, "SAX.hasInternalSubset()\n");
114    return(0);
115}
116
117/**
118 * hasExternalSubsetDebug:
119 * @ctxt:  An XML parser context
120 *
121 * Does this document has an external subset
122 *
123 * Returns 1 if true
124 */
125int
126hasExternalSubsetDebug(void *ctx)
127{
128    fprintf(stdout, "SAX.hasExternalSubset()\n");
129    return(0);
130}
131
132/**
133 * hasInternalSubsetDebug:
134 * @ctxt:  An XML parser context
135 *
136 * Does this document has an internal subset
137 */
138void
139internalSubsetDebug(void *ctx, const xmlChar *name,
140               const xmlChar *ExternalID, const xmlChar *SystemID)
141{
142    /* xmlDtdPtr externalSubset; */
143
144    fprintf(stdout, "SAX.internalSubset(%s, %s, %s)\n",
145            name, ExternalID, SystemID);
146
147/***********
148    if ((ExternalID != NULL) || (SystemID != NULL)) {
149        externalSubset = xmlParseDTD(ExternalID, SystemID);
150        if (externalSubset != NULL) {
151            xmlFreeDtd(externalSubset);
152        }
153    }
154 ***********/
155}
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
172resolveEntityDebug(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
173{
174    /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
175
176   
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");
186/*********
187    if (systemId != NULL) {
188        return(xmlNewInputFromFile(ctxt, (char *) systemId));
189    }
190 *********/
191    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
204getEntityDebug(void *ctx, const xmlChar *name)
205{
206    fprintf(stdout, "SAX.getEntity(%s)\n", name);
207    return(NULL);
208}
209
210/**
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
220getParameterEntityDebug(void *ctx, const xmlChar *name)
221{
222    fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
223    return(NULL);
224}
225
226
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
239entityDeclDebug(void *ctx, const xmlChar *name, int type,
240          const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
241{
242    fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
243            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
255attributeDeclDebug(void *ctx, const xmlChar *elem, const xmlChar *name,
256              int type, int def, const xmlChar *defaultValue,
257              xmlEnumerationPtr tree)
258{
259    fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
260            elem, name, type, def, defaultValue);
261}
262
263/**
264 * elementDeclDebug:
265 * @ctxt:  An XML parser context
266 * @name:  the element name
267 * @type:  the element type
268 * @content: the element value (without processing).
269 *
270 * An element definition has been parsed
271 */
272void
273elementDeclDebug(void *ctx, const xmlChar *name, int type,
274            xmlElementContentPtr content)
275{
276    fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
277            name, type);
278}
279
280/**
281 * notationDeclDebug:
282 * @ctxt:  An XML parser context
283 * @name: The name of the notation
284 * @publicId: The public ID of the entity
285 * @systemId: The system ID of the entity
286 *
287 * What to do when a notation declaration has been parsed.
288 */
289void
290notationDeclDebug(void *ctx, const xmlChar *name,
291             const xmlChar *publicId, const xmlChar *systemId)
292{
293    fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
294            (char *) name, (char *) publicId, (char *) systemId);
295}
296
297/**
298 * unparsedEntityDeclDebug:
299 * @ctxt:  An XML parser context
300 * @name: The name of the entity
301 * @publicId: The public ID of the entity
302 * @systemId: The system ID of the entity
303 * @notationName: the name of the notation
304 *
305 * What to do when an unparsed entity declaration is parsed
306 */
307void
308unparsedEntityDeclDebug(void *ctx, const xmlChar *name,
309                   const xmlChar *publicId, const xmlChar *systemId,
310                   const xmlChar *notationName)
311{
312    fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
313            (char *) name, (char *) publicId, (char *) systemId,
314            (char *) notationName);
315}
316
317/**
318 * setDocumentLocatorDebug:
319 * @ctxt:  An XML parser context
320 * @loc: A SAX Locator
321 *
322 * Receive the document locator at startup, actually xmlDefaultSAXLocator
323 * Everything is available on the context, so this is useless in our case.
324 */
325void
326setDocumentLocatorDebug(void *ctx, xmlSAXLocatorPtr loc)
327{
328    fprintf(stdout, "SAX.setDocumentLocator()\n");
329}
330
331/**
332 * startDocumentDebug:
333 * @ctxt:  An XML parser context
334 *
335 * called when the document start being processed.
336 */
337void
338startDocumentDebug(void *ctx)
339{
340    fprintf(stdout, "SAX.startDocument()\n");
341}
342
343/**
344 * endDocumentDebug:
345 * @ctxt:  An XML parser context
346 *
347 * called when the document end has been detected.
348 */
349void
350endDocumentDebug(void *ctx)
351{
352    fprintf(stdout, "SAX.endDocument()\n");
353}
354
355/**
356 * startElementDebug:
357 * @ctxt:  An XML parser context
358 * @name:  The element name
359 *
360 * called when an opening tag has been processed.
361 */
362void
363startElementDebug(void *ctx, const xmlChar *name, const xmlChar **atts)
364{
365    int i;
366
367    fprintf(stdout, "SAX.startElement(%s", (char *) name);
368    if (atts != NULL) {
369        for (i = 0;(atts[i] != NULL);i++) {
370            fprintf(stdout, ", %s='", atts[i++]);
371            fprintf(stdout, "%s'", atts[i]);
372        }
373    }
374    fprintf(stdout, ")\n");
375}
376
377/**
378 * endElementDebug:
379 * @ctxt:  An XML parser context
380 * @name:  The element name
381 *
382 * called when the end of an element has been detected.
383 */
384void
385endElementDebug(void *ctx, const xmlChar *name)
386{
387    fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
388}
389
390/**
391 * charactersDebug:
392 * @ctxt:  An XML parser context
393 * @ch:  a xmlChar string
394 * @len: the number of xmlChar
395 *
396 * receiving some chars from the parser.
397 * Question: how much at a time ???
398 */
399void
400charactersDebug(void *ctx, const xmlChar *ch, int len)
401{
402    int i;
403
404    fprintf(stdout, "SAX.characters(");
405    for (i = 0;(i < len) && (i < 30);i++)
406        fprintf(stdout, "%c", ch[i]);
407    fprintf(stdout, ", %d)\n", len);
408}
409
410/**
411 * referenceDebug:
412 * @ctxt:  An XML parser context
413 * @name:  The entity name
414 *
415 * called when an entity reference is detected.
416 */
417void
418referenceDebug(void *ctx, const xmlChar *name)
419{
420    fprintf(stdout, "SAX.reference(%s)\n", name);
421}
422
423/**
424 * ignorableWhitespaceDebug:
425 * @ctxt:  An XML parser context
426 * @ch:  a xmlChar string
427 * @start: the first char in the string
428 * @len: the number of xmlChar
429 *
430 * receiving some ignorable whitespaces from the parser.
431 * Question: how much at a time ???
432 */
433void
434ignorableWhitespaceDebug(void *ctx, const xmlChar *ch, int len)
435{
436    fprintf(stdout, "SAX.ignorableWhitespace(%.30s, %d)\n",
437            (char *) ch, len);
438}
439
440/**
441 * processingInstructionDebug:
442 * @ctxt:  An XML parser context
443 * @target:  the target name
444 * @data: the PI data's
445 * @len: the number of xmlChar
446 *
447 * A processing instruction has been parsed.
448 */
449void
450processingInstructionDebug(void *ctx, const xmlChar *target,
451                      const xmlChar *data)
452{
453    fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
454            (char *) target, (char *) data);
455}
456
457/**
458 * commentDebug:
459 * @ctxt:  An XML parser context
460 * @value:  the comment content
461 *
462 * A comment has been parsed.
463 */
464void
465commentDebug(void *ctx, const xmlChar *value)
466{
467    fprintf(stdout, "SAX.comment(%s)\n", value);
468}
469
470/**
471 * warningDebug:
472 * @ctxt:  An XML parser context
473 * @msg:  the message to display/transmit
474 * @...:  extra parameters for the message display
475 *
476 * Display and format a warning messages, gives file, line, position and
477 * extra parameters.
478 */
479void
480warningDebug(void *ctx, const char *msg, ...)
481{
482    va_list args;
483
484    va_start(args, msg);
485    fprintf(stdout, "SAX.warning: ");
486    vfprintf(stdout, msg, args);
487    va_end(args);
488}
489
490/**
491 * errorDebug:
492 * @ctxt:  An XML parser context
493 * @msg:  the message to display/transmit
494 * @...:  extra parameters for the message display
495 *
496 * Display and format a error messages, gives file, line, position and
497 * extra parameters.
498 */
499void
500errorDebug(void *ctx, const char *msg, ...)
501{
502    va_list args;
503
504    va_start(args, msg);
505    fprintf(stdout, "SAX.error: ");
506    vfprintf(stdout, msg, args);
507    va_end(args);
508}
509
510/**
511 * fatalErrorDebug:
512 * @ctxt:  An XML parser context
513 * @msg:  the message to display/transmit
514 * @...:  extra parameters for the message display
515 *
516 * Display and format a fatalError messages, gives file, line, position and
517 * extra parameters.
518 */
519void
520fatalErrorDebug(void *ctx, const char *msg, ...)
521{
522    va_list args;
523
524    va_start(args, msg);
525    fprintf(stdout, "SAX.fatalError: ");
526    vfprintf(stdout, msg, args);
527    va_end(args);
528}
529
530xmlSAXHandler debugSAXHandlerStruct = {
531    internalSubsetDebug,
532    isStandaloneDebug,
533    hasInternalSubsetDebug,
534    hasExternalSubsetDebug,
535    resolveEntityDebug,
536    getEntityDebug,
537    entityDeclDebug,
538    notationDeclDebug,
539    attributeDeclDebug,
540    elementDeclDebug,
541    unparsedEntityDeclDebug,
542    setDocumentLocatorDebug,
543    startDocumentDebug,
544    endDocumentDebug,
545    startElementDebug,
546    endElementDebug,
547    referenceDebug,
548    charactersDebug,
549    ignorableWhitespaceDebug,
550    processingInstructionDebug,
551    commentDebug,
552    warningDebug,
553    errorDebug,
554    fatalErrorDebug,
555    getParameterEntityDebug,
556};
557
558xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
559
560/************************************************************************
561 *                                                                      *
562 *                              Debug                                   *
563 *                                                                      *
564 ************************************************************************/
565
566void parseAndPrintFile(char *filename) {
567    int res;
568
569    if (push) {
570        FILE *f;
571
572        /*
573         * Empty callbacks for checking
574         */
575        f = fopen(filename, "r");
576        if (f != NULL) {
577            int res;
578            char chars[10];
579            xmlParserCtxtPtr ctxt;
580
581            res = fread(chars, 1, 4, f);
582            if (res > 0) {
583                ctxt = xmlCreatePushParserCtxt(emptySAXHandler, NULL,
584                            chars, res, filename);
585                while ((res = fread(chars, 1, 3, f)) > 0) {
586                    xmlParseChunk(ctxt, chars, res, 0);
587                }
588                xmlParseChunk(ctxt, chars, 0, 1);
589                xmlFreeParserCtxt(ctxt);
590            }
591            fclose(f);
592        } else {
593            fprintf(stderr, "Cannot read file %s\n", filename);
594        }
595        /*
596         * Debug callback
597         */
598        f = fopen(filename, "r");
599        if (f != NULL) {
600            int res;
601            char chars[10];
602            xmlParserCtxtPtr ctxt;
603
604            res = fread(chars, 1, 4, f);
605            if (res > 0) {
606                ctxt = xmlCreatePushParserCtxt(debugSAXHandler, NULL,
607                            chars, res, filename);
608                while ((res = fread(chars, 1, 3, f)) > 0) {
609                    xmlParseChunk(ctxt, chars, res, 0);
610                }
611                res = xmlParseChunk(ctxt, chars, 0, 1);
612                xmlFreeParserCtxt(ctxt);
613                if (res != 0) {
614                    fprintf(stdout,
615                            "xmlSAXUserParseFile returned error %d\n", res);
616                }
617            }
618            fclose(f);
619        }
620    } else {
621        /*
622         * Empty callbacks for checking
623         */
624        res = xmlSAXUserParseFile(emptySAXHandler, NULL, filename);
625        if (res != 0) {
626            fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res);
627        }
628
629        /*
630         * Debug callback
631         */
632        res = xmlSAXUserParseFile(debugSAXHandler, NULL, filename);
633        if (res != 0) {
634            fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res);
635        }
636    }
637}
638
639
640int main(int argc, char **argv) {
641    int i;
642    int files = 0;
643
644    for (i = 1; i < argc ; i++) {
645        if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
646            debug++;
647        else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
648            copy++;
649        else if ((!strcmp(argv[i], "-recover")) ||
650                 (!strcmp(argv[i], "--recover")))
651            recovery++;
652        else if ((!strcmp(argv[i], "-push")) ||
653                 (!strcmp(argv[i], "--push")))
654            push++;
655    }
656    for (i = 1; i < argc ; i++) {
657        if (argv[i][0] != '-') {
658            parseAndPrintFile(argv[i]);
659            files ++;
660        }
661    }
662    xmlCleanupParser();
663    xmlMemoryDump();
664
665    return(0);
666}
Note: See TracBrowser for help on using the repository browser.