2 * testSAX.c : a small tester program for parsing using the SAX API.
4 * See Copyright for the status of this software.
14 #ifdef HAVE_SYS_TYPES_H
15 #include <sys/types.h>
17 #ifdef HAVE_SYS_STAT_H
34 #include <libxml/globals.h>
35 #include <libxml/xmlerror.h>
36 #include <libxml/parser.h>
37 #include <libxml/parserInternals.h> /* only for xmlNewInputFromFile() */
38 #include <libxml/tree.h>
39 #include <libxml/debugXML.h>
40 #include <libxml/xmlmemory.h>
44 static int recovery
= 0;
49 static int callbacks
= 0;
51 xmlSAXHandler emptySAXHandlerStruct
= {
52 NULL
, /* internalSubset */
53 NULL
, /* isStandalone */
54 NULL
, /* hasInternalSubset */
55 NULL
, /* hasExternalSubset */
56 NULL
, /* resolveEntity */
58 NULL
, /* entityDecl */
59 NULL
, /* notationDecl */
60 NULL
, /* attributeDecl */
61 NULL
, /* elementDecl */
62 NULL
, /* unparsedEntityDecl */
63 NULL
, /* setDocumentLocator */
64 NULL
, /* startDocument */
65 NULL
, /* endDocument */
66 NULL
, /* startElement */
67 NULL
, /* endElement */
69 NULL
, /* characters */
70 NULL
, /* ignorableWhitespace */
71 NULL
, /* processingInstruction */
73 NULL
, /* xmlParserWarning */
74 NULL
, /* xmlParserError */
75 NULL
, /* xmlParserError */
76 NULL
, /* getParameterEntity */
77 NULL
, /* cdataBlock; */
78 NULL
, /* externalSubset; */
82 xmlSAXHandlerPtr emptySAXHandler
= &emptySAXHandlerStruct
;
83 extern xmlSAXHandlerPtr debugSAXHandler
;
85 /************************************************************************
89 ************************************************************************/
93 * @ctxt: An XML parser context
95 * Is this document tagged standalone ?
100 isStandaloneDebug(void *ctx ATTRIBUTE_UNUSED
)
105 fprintf(stdout
, "SAX.isStandalone()\n");
110 * hasInternalSubsetDebug:
111 * @ctxt: An XML parser context
113 * Does this document has an internal subset
118 hasInternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED
)
123 fprintf(stdout
, "SAX.hasInternalSubset()\n");
128 * hasExternalSubsetDebug:
129 * @ctxt: An XML parser context
131 * Does this document has an external subset
136 hasExternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED
)
141 fprintf(stdout
, "SAX.hasExternalSubset()\n");
146 * internalSubsetDebug:
147 * @ctxt: An XML parser context
149 * Does this document has an internal subset
152 internalSubsetDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
,
153 const xmlChar
*ExternalID
, const xmlChar
*SystemID
)
158 fprintf(stdout
, "SAX.internalSubset(%s,", name
);
159 if (ExternalID
== NULL
)
160 fprintf(stdout
, " ,");
162 fprintf(stdout
, " %s,", ExternalID
);
163 if (SystemID
== NULL
)
164 fprintf(stdout
, " )\n");
166 fprintf(stdout
, " %s)\n", SystemID
);
170 * externalSubsetDebug:
171 * @ctxt: An XML parser context
173 * Does this document has an external subset
176 externalSubsetDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
,
177 const xmlChar
*ExternalID
, const xmlChar
*SystemID
)
182 fprintf(stdout
, "SAX.externalSubset(%s,", name
);
183 if (ExternalID
== NULL
)
184 fprintf(stdout
, " ,");
186 fprintf(stdout
, " %s,", ExternalID
);
187 if (SystemID
== NULL
)
188 fprintf(stdout
, " )\n");
190 fprintf(stdout
, " %s)\n", SystemID
);
194 * resolveEntityDebug:
195 * @ctxt: An XML parser context
196 * @publicId: The public ID of the entity
197 * @systemId: The system ID of the entity
199 * Special entity resolver, better left to the parser, it has
200 * more context than the application layer.
201 * The default behaviour is to NOT resolve the entities, in that case
202 * the ENTITY_REF nodes are built in the structure (and the parameter
205 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
207 static xmlParserInputPtr
208 resolveEntityDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*publicId
, const xmlChar
*systemId
)
213 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
216 fprintf(stdout
, "SAX.resolveEntity(");
217 if (publicId
!= NULL
)
218 fprintf(stdout
, "%s", (char *)publicId
);
220 fprintf(stdout
, " ");
221 if (systemId
!= NULL
)
222 fprintf(stdout
, ", %s)\n", (char *)systemId
);
224 fprintf(stdout
, ", )\n");
226 if (systemId != NULL) {
227 return(xmlNewInputFromFile(ctxt, (char *) systemId));
235 * @ctxt: An XML parser context
236 * @name: The entity name
238 * Get an entity by name
240 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
243 getEntityDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
)
248 fprintf(stdout
, "SAX.getEntity(%s)\n", name
);
253 * getParameterEntityDebug:
254 * @ctxt: An XML parser context
255 * @name: The entity name
257 * Get a parameter entity by name
259 * Returns the xmlParserInputPtr
262 getParameterEntityDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
)
267 fprintf(stdout
, "SAX.getParameterEntity(%s)\n", name
);
274 * @ctxt: An XML parser context
275 * @name: the entity name
276 * @type: the entity type
277 * @publicId: The public ID of the entity
278 * @systemId: The system ID of the entity
279 * @content: the entity value (without processing).
281 * An entity definition has been parsed
284 entityDeclDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
, int type
,
285 const xmlChar
*publicId
, const xmlChar
*systemId
, xmlChar
*content
)
290 fprintf(stdout
, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
291 name
, type
, publicId
, systemId
, content
);
295 * attributeDeclDebug:
296 * @ctxt: An XML parser context
297 * @name: the attribute name
298 * @type: the attribute type
300 * An attribute definition has been parsed
303 attributeDeclDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*elem
, const xmlChar
*name
,
304 int type
, int def
, const xmlChar
*defaultValue
,
305 xmlEnumerationPtr tree ATTRIBUTE_UNUSED
)
310 if (defaultValue
== NULL
)
311 fprintf(stdout
, "SAX.attributeDecl(%s, %s, %d, %d, NULL, ...)\n",
312 elem
, name
, type
, def
);
314 fprintf(stdout
, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
315 elem
, name
, type
, def
, defaultValue
);
320 * @ctxt: An XML parser context
321 * @name: the element name
322 * @type: the element type
323 * @content: the element value (without processing).
325 * An element definition has been parsed
328 elementDeclDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
, int type
,
329 xmlElementContentPtr content ATTRIBUTE_UNUSED
)
334 fprintf(stdout
, "SAX.elementDecl(%s, %d, ...)\n",
340 * @ctxt: An XML parser context
341 * @name: The name of the notation
342 * @publicId: The public ID of the entity
343 * @systemId: The system ID of the entity
345 * What to do when a notation declaration has been parsed.
348 notationDeclDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
,
349 const xmlChar
*publicId
, const xmlChar
*systemId
)
354 fprintf(stdout
, "SAX.notationDecl(%s, %s, %s)\n",
355 (char *) name
, (char *) publicId
, (char *) systemId
);
359 * unparsedEntityDeclDebug:
360 * @ctxt: An XML parser context
361 * @name: The name of the entity
362 * @publicId: The public ID of the entity
363 * @systemId: The system ID of the entity
364 * @notationName: the name of the notation
366 * What to do when an unparsed entity declaration is parsed
369 unparsedEntityDeclDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
,
370 const xmlChar
*publicId
, const xmlChar
*systemId
,
371 const xmlChar
*notationName
)
376 fprintf(stdout
, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
377 (char *) name
, (char *) publicId
, (char *) systemId
,
378 (char *) notationName
);
382 * setDocumentLocatorDebug:
383 * @ctxt: An XML parser context
384 * @loc: A SAX Locator
386 * Receive the document locator at startup, actually xmlDefaultSAXLocator
387 * Everything is available on the context, so this is useless in our case.
390 setDocumentLocatorDebug(void *ctx ATTRIBUTE_UNUSED
, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED
)
395 fprintf(stdout
, "SAX.setDocumentLocator()\n");
399 * startDocumentDebug:
400 * @ctxt: An XML parser context
402 * called when the document start being processed.
405 startDocumentDebug(void *ctx ATTRIBUTE_UNUSED
)
410 fprintf(stdout
, "SAX.startDocument()\n");
415 * @ctxt: An XML parser context
417 * called when the document end has been detected.
420 endDocumentDebug(void *ctx ATTRIBUTE_UNUSED
)
425 fprintf(stdout
, "SAX.endDocument()\n");
430 * @ctxt: An XML parser context
431 * @name: The element name
433 * called when an opening tag has been processed.
436 startElementDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
, const xmlChar
**atts
)
443 fprintf(stdout
, "SAX.startElement(%s", (char *) name
);
445 for (i
= 0;(atts
[i
] != NULL
);i
++) {
446 fprintf(stdout
, ", %s='", atts
[i
++]);
448 fprintf(stdout
, "%s'", atts
[i
]);
451 fprintf(stdout
, ")\n");
456 * @ctxt: An XML parser context
457 * @name: The element name
459 * called when the end of an element has been detected.
462 endElementDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
)
467 fprintf(stdout
, "SAX.endElement(%s)\n", (char *) name
);
472 * @ctxt: An XML parser context
473 * @ch: a xmlChar string
474 * @len: the number of xmlChar
476 * receiving some chars from the parser.
477 * Question: how much at a time ???
480 charactersDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*ch
, int len
)
488 for (i
= 0;(i
<len
) && (i
< 30);i
++)
492 fprintf(stdout
, "SAX.characters(%s, %d)\n", output
, len
);
497 * @ctxt: An XML parser context
498 * @name: The entity name
500 * called when an entity reference is detected.
503 referenceDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
)
508 fprintf(stdout
, "SAX.reference(%s)\n", name
);
512 * ignorableWhitespaceDebug:
513 * @ctxt: An XML parser context
514 * @ch: a xmlChar string
515 * @start: the first char in the string
516 * @len: the number of xmlChar
518 * receiving some ignorable whitespaces from the parser.
519 * Question: how much at a time ???
522 ignorableWhitespaceDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*ch
, int len
)
530 for (i
= 0;(i
<len
) && (i
< 30);i
++)
533 fprintf(stdout
, "SAX.ignorableWhitespace(%s, %d)\n", output
, len
);
537 * processingInstructionDebug:
538 * @ctxt: An XML parser context
539 * @target: the target name
540 * @data: the PI data's
541 * @len: the number of xmlChar
543 * A processing instruction has been parsed.
546 processingInstructionDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*target
,
552 fprintf(stdout
, "SAX.processingInstruction(%s, %s)\n",
553 (char *) target
, (char *) data
);
558 * @ctx: the user data (XML parser context)
559 * @value: The pcdata content
560 * @len: the block length
562 * called when a pcdata block has been parsed
565 cdataBlockDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*value
, int len
)
570 fprintf(stdout
, "SAX.pcdata(%.20s, %d)\n",
571 (char *) value
, len
);
576 * @ctxt: An XML parser context
577 * @value: the comment content
579 * A comment has been parsed.
582 commentDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*value
)
587 fprintf(stdout
, "SAX.comment(%s)\n", value
);
592 * @ctxt: An XML parser context
593 * @msg: the message to display/transmit
594 * @...: extra parameters for the message display
596 * Display and format a warning messages, gives file, line, position and
600 warningDebug(void *ctx ATTRIBUTE_UNUSED
, const char *msg
, ...)
608 fprintf(stdout
, "SAX.warning: ");
609 vfprintf(stdout
, msg
, args
);
615 * @ctxt: An XML parser context
616 * @msg: the message to display/transmit
617 * @...: extra parameters for the message display
619 * Display and format a error messages, gives file, line, position and
623 errorDebug(void *ctx ATTRIBUTE_UNUSED
, const char *msg
, ...)
631 fprintf(stdout
, "SAX.error: ");
632 vfprintf(stdout
, msg
, args
);
638 * @ctxt: An XML parser context
639 * @msg: the message to display/transmit
640 * @...: extra parameters for the message display
642 * Display and format a fatalError messages, gives file, line, position and
646 fatalErrorDebug(void *ctx ATTRIBUTE_UNUSED
, const char *msg
, ...)
654 fprintf(stdout
, "SAX.fatalError: ");
655 vfprintf(stdout
, msg
, args
);
659 xmlSAXHandler debugSAXHandlerStruct
= {
662 hasInternalSubsetDebug
,
663 hasExternalSubsetDebug
,
670 unparsedEntityDeclDebug
,
671 setDocumentLocatorDebug
,
678 ignorableWhitespaceDebug
,
679 processingInstructionDebug
,
684 getParameterEntityDebug
,
690 xmlSAXHandlerPtr debugSAXHandler
= &debugSAXHandlerStruct
;
692 /************************************************************************
696 ************************************************************************/
699 parseAndPrintFile(char *filename
) {
706 * Empty callbacks for checking
708 f
= fopen(filename
, "r");
712 xmlParserCtxtPtr ctxt
;
714 ret
= fread(chars
, 1, 4, f
);
716 ctxt
= xmlCreatePushParserCtxt(emptySAXHandler
, NULL
,
717 chars
, ret
, filename
);
718 while ((ret
= fread(chars
, 1, 3, f
)) > 0) {
719 xmlParseChunk(ctxt
, chars
, ret
, 0);
721 xmlParseChunk(ctxt
, chars
, 0, 1);
722 xmlFreeParserCtxt(ctxt
);
726 xmlGenericError(xmlGenericErrorContext
,
727 "Cannot read file %s\n", filename
);
732 f
= fopen(filename
, "r");
736 xmlParserCtxtPtr ctxt
;
738 ret
= fread(chars
, 1, 4, f
);
740 ctxt
= xmlCreatePushParserCtxt(debugSAXHandler
, NULL
,
741 chars
, ret
, filename
);
742 while ((ret
= fread(chars
, 1, 3, f
)) > 0) {
743 xmlParseChunk(ctxt
, chars
, ret
, 0);
745 ret
= xmlParseChunk(ctxt
, chars
, 0, 1);
746 xmlFreeParserCtxt(ctxt
);
749 "xmlSAXUserParseFile returned error %d\n", ret
);
757 * Empty callbacks for checking
760 res
= xmlSAXUserParseFile(emptySAXHandler
, NULL
, filename
);
762 fprintf(stdout
, "xmlSAXUserParseFile returned error %d\n", res
);
770 res
= xmlSAXUserParseFile(debugSAXHandler
, NULL
, filename
);
772 fprintf(stdout
, "xmlSAXUserParseFile returned error %d\n", res
);
775 fprintf(stdout
, "%d callbacks generated\n", callbacks
);
778 * test 100x the SAX parse
782 for (i
= 0; i
<100;i
++)
783 res
= xmlSAXUserParseFile(emptySAXHandler
, NULL
, filename
);
785 fprintf(stdout
, "xmlSAXUserParseFile returned error %d\n", res
);
792 int main(int argc
, char **argv
) {
796 for (i
= 1; i
< argc
; i
++) {
797 if ((!strcmp(argv
[i
], "-debug")) || (!strcmp(argv
[i
], "--debug")))
799 else if ((!strcmp(argv
[i
], "-copy")) || (!strcmp(argv
[i
], "--copy")))
801 else if ((!strcmp(argv
[i
], "-recover")) ||
802 (!strcmp(argv
[i
], "--recover")))
804 else if ((!strcmp(argv
[i
], "-push")) ||
805 (!strcmp(argv
[i
], "--push")))
807 else if ((!strcmp(argv
[i
], "-speed")) ||
808 (!strcmp(argv
[i
], "--speed")))
810 else if ((!strcmp(argv
[i
], "-noent")) ||
811 (!strcmp(argv
[i
], "--noent")))
813 else if ((!strcmp(argv
[i
], "-quiet")) ||
814 (!strcmp(argv
[i
], "--quiet")))
817 if (noent
!= 0) xmlSubstituteEntitiesDefault(1);
818 for (i
= 1; i
< argc
; i
++) {
819 if (argv
[i
][0] != '-') {
820 parseAndPrintFile(argv
[i
]);