1999-04-21 20:12:07 +00:00
/*
2001-12-31 16:16:02 +00:00
* testSAX . c : a small tester program for parsing using the SAX API .
1999-04-21 20:12:07 +00:00
*
* See Copyright for the status of this software .
*
2001-06-24 12:13:24 +00:00
* daniel @ veillard . com
1999-04-21 20:12:07 +00:00
*/
2001-04-21 16:57:29 +00:00
# include "libxml.h"
1999-09-22 09:46:25 +00:00
# include <string.h>
# include <stdarg.h>
# ifdef HAVE_SYS_TYPES_H
1999-04-21 20:12:07 +00:00
# include <sys/types.h>
1999-09-22 09:46:25 +00:00
# endif
1999-04-21 20:12:07 +00:00
# ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
# endif
# ifdef HAVE_FCNTL_H
# include <fcntl.h>
# endif
# ifdef HAVE_UNISTD_H
# include <unistd.h>
# endif
1999-09-22 09:46:25 +00:00
# ifdef HAVE_STDLIB_H
1999-04-21 20:12:07 +00:00
# include <stdlib.h>
1999-09-22 09:46:25 +00:00
# endif
1999-09-26 11:31:02 +00:00
# ifdef HAVE_STRING_H
# include <string.h>
# endif
1999-09-22 09:46:25 +00:00
1999-04-21 20:12:07 +00:00
2001-10-13 09:15:48 +00:00
# include <libxml/globals.h>
2000-10-09 12:30:39 +00:00
# include <libxml/xmlerror.h>
2000-04-03 19:48:13 +00:00
# include <libxml/parser.h>
# include <libxml/parserInternals.h> /* only for xmlNewInputFromFile() */
# include <libxml/tree.h>
# include <libxml/debugXML.h>
# include <libxml/xmlmemory.h>
1999-04-21 20:12:07 +00:00
static int debug = 0 ;
static int copy = 0 ;
static int recovery = 0 ;
1999-12-28 16:35:14 +00:00
static int push = 0 ;
2000-04-12 13:27:38 +00:00
static int speed = 0 ;
2002-03-18 18:36:20 +00:00
static int noent = 0 ;
2002-03-20 19:24:21 +00:00
static int quiet = 0 ;
static int callbacks = 0 ;
1999-04-21 20:12:07 +00:00
xmlSAXHandler emptySAXHandlerStruct = {
NULL , /* internalSubset */
NULL , /* isStandalone */
NULL , /* hasInternalSubset */
NULL , /* hasExternalSubset */
NULL , /* resolveEntity */
NULL , /* getEntity */
NULL , /* entityDecl */
NULL , /* notationDecl */
NULL , /* attributeDecl */
NULL , /* elementDecl */
NULL , /* unparsedEntityDecl */
NULL , /* setDocumentLocator */
NULL , /* startDocument */
NULL , /* endDocument */
NULL , /* startElement */
NULL , /* endElement */
NULL , /* reference */
NULL , /* characters */
NULL , /* ignorableWhitespace */
NULL , /* processingInstruction */
NULL , /* comment */
NULL , /* xmlParserWarning */
NULL , /* xmlParserError */
NULL , /* xmlParserError */
1999-08-10 19:04:08 +00:00
NULL , /* getParameterEntity */
2000-03-14 18:30:20 +00:00
NULL , /* cdataBlock; */
2001-10-13 09:15:48 +00:00
NULL , /* externalSubset; */
1
1999-04-21 20:12:07 +00:00
} ;
xmlSAXHandlerPtr emptySAXHandler = & emptySAXHandlerStruct ;
1999-06-02 17:44:04 +00:00
extern xmlSAXHandlerPtr debugSAXHandler ;
1999-04-21 20:12:07 +00:00
/************************************************************************
* *
* Debug Handlers *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* isStandaloneDebug :
* @ ctxt : An XML parser context
*
* Is this document tagged standalone ?
*
* Returns 1 if true
*/
2001-03-24 17:00:36 +00:00
static int
2001-03-26 16:28:29 +00:00
isStandaloneDebug ( void * ctx ATTRIBUTE_UNUSED )
1999-04-21 20:12:07 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ( 0 ) ;
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " SAX.isStandalone() \n " ) ;
1999-04-21 20:12:07 +00:00
return ( 0 ) ;
}
/**
* hasInternalSubsetDebug :
* @ ctxt : An XML parser context
*
* Does this document has an internal subset
*
* Returns 1 if true
*/
2001-03-24 17:00:36 +00:00
static int
2001-03-26 16:28:29 +00:00
hasInternalSubsetDebug ( void * ctx ATTRIBUTE_UNUSED )
1999-04-21 20:12:07 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ( 0 ) ;
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " SAX.hasInternalSubset() \n " ) ;
1999-04-21 20:12:07 +00:00
return ( 0 ) ;
}
/**
* hasExternalSubsetDebug :
* @ ctxt : An XML parser context
*
* Does this document has an external subset
*
* Returns 1 if true
*/
2001-03-24 17:00:36 +00:00
static int
2001-03-26 16:28:29 +00:00
hasExternalSubsetDebug ( void * ctx ATTRIBUTE_UNUSED )
1999-04-21 20:12:07 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ( 0 ) ;
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " SAX.hasExternalSubset() \n " ) ;
1999-04-21 20:12:07 +00:00
return ( 0 ) ;
}
/**
2000-04-24 11:33:38 +00:00
* internalSubsetDebug :
1999-04-21 20:12:07 +00:00
* @ ctxt : An XML parser context
*
* Does this document has an internal subset
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
internalSubsetDebug ( void * ctx ATTRIBUTE_UNUSED , const xmlChar * name ,
1999-09-23 22:19:22 +00:00
const xmlChar * ExternalID , const xmlChar * SystemID )
1999-04-21 20:12:07 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
2000-08-17 13:50:51 +00:00
fprintf ( stdout , " SAX.internalSubset(%s, " , name ) ;
if ( ExternalID = = NULL )
fprintf ( stdout , " , " ) ;
else
fprintf ( stdout , " %s, " , ExternalID ) ;
if ( SystemID = = NULL )
fprintf ( stdout , " ) \n " ) ;
else
fprintf ( stdout , " %s) \n " , SystemID ) ;
1999-04-21 20:12:07 +00:00
}
2001-03-24 17:00:36 +00:00
/**
* externalSubsetDebug :
* @ ctxt : An XML parser context
*
* Does this document has an external subset
*/
static void
2001-03-26 16:28:29 +00:00
externalSubsetDebug ( void * ctx ATTRIBUTE_UNUSED , const xmlChar * name ,
2001-03-24 17:00:36 +00:00
const xmlChar * ExternalID , const xmlChar * SystemID )
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
2001-03-24 17:00:36 +00:00
fprintf ( stdout , " SAX.externalSubset(%s, " , name ) ;
if ( ExternalID = = NULL )
fprintf ( stdout , " , " ) ;
else
fprintf ( stdout , " %s, " , ExternalID ) ;
if ( SystemID = = NULL )
fprintf ( stdout , " ) \n " ) ;
else
fprintf ( stdout , " %s) \n " , SystemID ) ;
}
1999-04-21 20:12:07 +00:00
/**
* resolveEntityDebug :
* @ ctxt : An XML parser context
* @ publicId : The public ID of the entity
* @ systemId : The system ID of the entity
*
* Special entity resolver , better left to the parser , it has
* more context than the application layer .
* The default behaviour is to NOT resolve the entities , in that case
* the ENTITY_REF nodes are built in the structure ( and the parameter
* values ) .
*
* Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour .
*/
2001-03-24 17:00:36 +00:00
static xmlParserInputPtr
2001-03-26 16:28:29 +00:00
resolveEntityDebug ( void * ctx ATTRIBUTE_UNUSED , const xmlChar * publicId , const xmlChar * systemId )
1999-04-21 20:12:07 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ( NULL ) ;
1999-09-26 11:31:02 +00:00
/* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
1999-08-29 21:02:19 +00:00
1999-06-22 21:49:07 +00:00
fprintf ( stdout , " SAX.resolveEntity( " ) ;
if ( publicId ! = NULL )
fprintf ( stdout , " %s " , ( char * ) publicId ) ;
else
fprintf ( stdout , " " ) ;
if ( systemId ! = NULL )
fprintf ( stdout , " , %s) \n " , ( char * ) systemId ) ;
else
fprintf ( stdout , " , ) \n " ) ;
1999-09-26 11:31:02 +00:00
/*********
1999-06-02 17:44:04 +00:00
if ( systemId ! = NULL ) {
1999-08-29 21:02:19 +00:00
return ( xmlNewInputFromFile ( ctxt , ( char * ) systemId ) ) ;
1999-06-02 17:44:04 +00:00
}
1999-09-26 11:31:02 +00:00
* * * * * * * * */
1999-04-21 20:12:07 +00:00
return ( NULL ) ;
}
/**
* getEntityDebug :
* @ ctxt : An XML parser context
* @ name : The entity name
*
* Get an entity by name
*
* Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour .
*/
2001-03-24 17:00:36 +00:00
static xmlEntityPtr
2001-03-26 16:28:29 +00:00
getEntityDebug ( void * ctx ATTRIBUTE_UNUSED , const xmlChar * name )
1999-04-21 20:12:07 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ( NULL ) ;
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " SAX.getEntity(%s) \n " , name ) ;
1999-04-21 20:12:07 +00:00
return ( NULL ) ;
}
1999-08-10 19:04:08 +00:00
/**
* getParameterEntityDebug :
* @ ctxt : An XML parser context
* @ name : The entity name
*
* Get a parameter entity by name
*
* Returns the xmlParserInputPtr
*/
2001-03-24 17:00:36 +00:00
static xmlEntityPtr
2001-03-26 16:28:29 +00:00
getParameterEntityDebug ( void * ctx ATTRIBUTE_UNUSED , const xmlChar * name )
1999-08-10 19:04:08 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ( NULL ) ;
1999-08-10 19:04:08 +00:00
fprintf ( stdout , " SAX.getParameterEntity(%s) \n " , name ) ;
return ( NULL ) ;
}
1999-04-21 20:12:07 +00:00
/**
* entityDeclDebug :
* @ ctxt : An XML parser context
* @ name : the entity name
* @ type : the entity type
* @ publicId : The public ID of the entity
* @ systemId : The system ID of the entity
* @ content : the entity value ( without processing ) .
*
* An entity definition has been parsed
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
entityDeclDebug ( void * ctx ATTRIBUTE_UNUSED , const xmlChar * name , int type ,
1999-09-23 22:19:22 +00:00
const xmlChar * publicId , const xmlChar * systemId , xmlChar * content )
1999-04-21 20:12:07 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " SAX.entityDecl(%s, %d, %s, %s, %s) \n " ,
1999-04-21 20:12:07 +00:00
name , type , publicId , systemId , content ) ;
}
/**
* attributeDeclDebug :
* @ ctxt : An XML parser context
* @ name : the attribute name
* @ type : the attribute type
*
* An attribute definition has been parsed
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
attributeDeclDebug ( void * ctx ATTRIBUTE_UNUSED , const xmlChar * elem , const xmlChar * name ,
1999-09-23 22:19:22 +00:00
int type , int def , const xmlChar * defaultValue ,
2001-03-26 16:28:29 +00:00
xmlEnumerationPtr tree ATTRIBUTE_UNUSED )
1999-04-21 20:12:07 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
2000-10-06 12:59:53 +00:00
if ( defaultValue = = NULL )
fprintf ( stdout , " SAX.attributeDecl(%s, %s, %d, %d, NULL, ...) \n " ,
elem , name , type , def ) ;
else
fprintf ( stdout , " SAX.attributeDecl(%s, %s, %d, %d, %s, ...) \n " ,
1999-04-21 20:12:07 +00:00
elem , name , type , def , defaultValue ) ;
}
/**
* elementDeclDebug :
* @ ctxt : An XML parser context
* @ name : the element name
* @ type : the element type
* @ content : the element value ( without processing ) .
*
* An element definition has been parsed
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
elementDeclDebug ( void * ctx ATTRIBUTE_UNUSED , const xmlChar * name , int type ,
xmlElementContentPtr content ATTRIBUTE_UNUSED )
1999-04-21 20:12:07 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " SAX.elementDecl(%s, %d, ...) \n " ,
1999-04-21 20:12:07 +00:00
name , type ) ;
}
/**
* notationDeclDebug :
* @ ctxt : An XML parser context
* @ name : The name of the notation
* @ publicId : The public ID of the entity
* @ systemId : The system ID of the entity
*
* What to do when a notation declaration has been parsed .
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
notationDeclDebug ( void * ctx ATTRIBUTE_UNUSED , const xmlChar * name ,
1999-09-23 22:19:22 +00:00
const xmlChar * publicId , const xmlChar * systemId )
1999-04-21 20:12:07 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " SAX.notationDecl(%s, %s, %s) \n " ,
1999-04-21 20:12:07 +00:00
( char * ) name , ( char * ) publicId , ( char * ) systemId ) ;
}
/**
* unparsedEntityDeclDebug :
* @ ctxt : An XML parser context
* @ name : The name of the entity
* @ publicId : The public ID of the entity
* @ systemId : The system ID of the entity
* @ notationName : the name of the notation
*
* What to do when an unparsed entity declaration is parsed
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
unparsedEntityDeclDebug ( void * ctx ATTRIBUTE_UNUSED , const xmlChar * name ,
1999-09-23 22:19:22 +00:00
const xmlChar * publicId , const xmlChar * systemId ,
const xmlChar * notationName )
1999-04-21 20:12:07 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " SAX.unparsedEntityDecl(%s, %s, %s, %s) \n " ,
1999-04-21 20:12:07 +00:00
( char * ) name , ( char * ) publicId , ( char * ) systemId ,
( char * ) notationName ) ;
}
/**
* setDocumentLocatorDebug :
* @ ctxt : An XML parser context
* @ loc : A SAX Locator
*
* Receive the document locator at startup , actually xmlDefaultSAXLocator
* Everything is available on the context , so this is useless in our case .
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
setDocumentLocatorDebug ( void * ctx ATTRIBUTE_UNUSED , xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED )
1999-04-21 20:12:07 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " SAX.setDocumentLocator() \n " ) ;
1999-04-21 20:12:07 +00:00
}
/**
* startDocumentDebug :
* @ ctxt : An XML parser context
*
* called when the document start being processed .
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
startDocumentDebug ( void * ctx ATTRIBUTE_UNUSED )
1999-04-21 20:12:07 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " SAX.startDocument() \n " ) ;
1999-04-21 20:12:07 +00:00
}
/**
* endDocumentDebug :
* @ ctxt : An XML parser context
*
* called when the document end has been detected .
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
endDocumentDebug ( void * ctx ATTRIBUTE_UNUSED )
1999-04-21 20:12:07 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " SAX.endDocument() \n " ) ;
1999-04-21 20:12:07 +00:00
}
/**
* startElementDebug :
* @ ctxt : An XML parser context
* @ name : The element name
*
* called when an opening tag has been processed .
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
startElementDebug ( void * ctx ATTRIBUTE_UNUSED , const xmlChar * name , const xmlChar * * atts )
1999-04-21 20:12:07 +00:00
{
int i ;
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " SAX.startElement(%s " , ( char * ) name ) ;
1999-04-21 20:12:07 +00:00
if ( atts ! = NULL ) {
for ( i = 0 ; ( atts [ i ] ! = NULL ) ; i + + ) {
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " , %s=' " , atts [ i + + ] ) ;
2000-08-17 13:50:51 +00:00
if ( atts [ i ] ! = NULL )
fprintf ( stdout , " %s' " , atts [ i ] ) ;
1999-04-21 20:12:07 +00:00
}
}
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " ) \n " ) ;
1999-04-21 20:12:07 +00:00
}
/**
* endElementDebug :
* @ ctxt : An XML parser context
* @ name : The element name
*
* called when the end of an element has been detected .
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
endElementDebug ( void * ctx ATTRIBUTE_UNUSED , const xmlChar * name )
1999-04-21 20:12:07 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " SAX.endElement(%s) \n " , ( char * ) name ) ;
1999-04-21 20:12:07 +00:00
}
/**
* charactersDebug :
* @ ctxt : An XML parser context
1999-09-23 22:19:22 +00:00
* @ ch : a xmlChar string
* @ len : the number of xmlChar
1999-04-21 20:12:07 +00:00
*
* receiving some chars from the parser .
* Question : how much at a time ? ? ?
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
charactersDebug ( void * ctx ATTRIBUTE_UNUSED , const xmlChar * ch , int len )
1999-04-21 20:12:07 +00:00
{
2000-08-12 21:12:04 +00:00
char output [ 40 ] ;
1999-07-27 19:52:06 +00:00
int i ;
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
2000-08-12 21:12:04 +00:00
for ( i = 0 ; ( i < len ) & & ( i < 30 ) ; i + + )
output [ i ] = ch [ i ] ;
output [ i ] = 0 ;
fprintf ( stdout , " SAX.characters(%s, %d) \n " , output , len ) ;
1999-04-21 20:12:07 +00:00
}
/**
* referenceDebug :
* @ ctxt : An XML parser context
* @ name : The entity name
*
* called when an entity reference is detected .
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
referenceDebug ( void * ctx ATTRIBUTE_UNUSED , const xmlChar * name )
1999-04-21 20:12:07 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " SAX.reference(%s) \n " , name ) ;
1999-04-21 20:12:07 +00:00
}
/**
* ignorableWhitespaceDebug :
* @ ctxt : An XML parser context
1999-09-23 22:19:22 +00:00
* @ ch : a xmlChar string
1999-04-21 20:12:07 +00:00
* @ start : the first char in the string
1999-09-23 22:19:22 +00:00
* @ len : the number of xmlChar
1999-04-21 20:12:07 +00:00
*
* receiving some ignorable whitespaces from the parser .
* Question : how much at a time ? ? ?
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
ignorableWhitespaceDebug ( void * ctx ATTRIBUTE_UNUSED , const xmlChar * ch , int len )
1999-04-21 20:12:07 +00:00
{
2000-08-12 21:12:04 +00:00
char output [ 40 ] ;
int i ;
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
2000-08-12 21:12:04 +00:00
for ( i = 0 ; ( i < len ) & & ( i < 30 ) ; i + + )
output [ i ] = ch [ i ] ;
output [ i ] = 0 ;
fprintf ( stdout , " SAX.ignorableWhitespace(%s, %d) \n " , output , len ) ;
1999-04-21 20:12:07 +00:00
}
/**
* processingInstructionDebug :
* @ ctxt : An XML parser context
* @ target : the target name
* @ data : the PI data ' s
1999-09-23 22:19:22 +00:00
* @ len : the number of xmlChar
1999-04-21 20:12:07 +00:00
*
* A processing instruction has been parsed .
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
processingInstructionDebug ( void * ctx ATTRIBUTE_UNUSED , const xmlChar * target ,
1999-09-23 22:19:22 +00:00
const xmlChar * data )
1999-04-21 20:12:07 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " SAX.processingInstruction(%s, %s) \n " ,
1999-04-21 20:12:07 +00:00
( char * ) target , ( char * ) data ) ;
}
2000-03-14 18:30:20 +00:00
/**
* cdataBlockDebug :
* @ ctx : the user data ( XML parser context )
* @ value : The pcdata content
* @ len : the block length
*
* called when a pcdata block has been parsed
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
cdataBlockDebug ( void * ctx ATTRIBUTE_UNUSED , const xmlChar * value , int len )
2000-03-14 18:30:20 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
2000-10-15 10:06:55 +00:00
fprintf ( stdout , " SAX.pcdata(%.20s, %d) \n " ,
2000-03-14 18:30:20 +00:00
( char * ) value , len ) ;
}
1999-04-21 20:12:07 +00:00
/**
* commentDebug :
* @ ctxt : An XML parser context
* @ value : the comment content
*
* A comment has been parsed .
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
commentDebug ( void * ctx ATTRIBUTE_UNUSED , const xmlChar * value )
1999-04-21 20:12:07 +00:00
{
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " SAX.comment(%s) \n " , value ) ;
1999-04-21 20:12:07 +00:00
}
/**
* warningDebug :
* @ ctxt : An XML parser context
* @ msg : the message to display / transmit
* @ . . . : extra parameters for the message display
*
* Display and format a warning messages , gives file , line , position and
* extra parameters .
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
warningDebug ( void * ctx ATTRIBUTE_UNUSED , const char * msg , . . . )
1999-04-21 20:12:07 +00:00
{
va_list args ;
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
1999-04-21 20:12:07 +00:00
va_start ( args , msg ) ;
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " SAX.warning: " ) ;
vfprintf ( stdout , msg , args ) ;
1999-04-21 20:12:07 +00:00
va_end ( args ) ;
}
/**
* errorDebug :
* @ ctxt : An XML parser context
* @ msg : the message to display / transmit
* @ . . . : extra parameters for the message display
*
* Display and format a error messages , gives file , line , position and
* extra parameters .
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
errorDebug ( void * ctx ATTRIBUTE_UNUSED , const char * msg , . . . )
1999-04-21 20:12:07 +00:00
{
va_list args ;
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
1999-04-21 20:12:07 +00:00
va_start ( args , msg ) ;
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " SAX.error: " ) ;
vfprintf ( stdout , msg , args ) ;
1999-04-21 20:12:07 +00:00
va_end ( args ) ;
}
/**
* fatalErrorDebug :
* @ ctxt : An XML parser context
* @ msg : the message to display / transmit
* @ . . . : extra parameters for the message display
*
* Display and format a fatalError messages , gives file , line , position and
* extra parameters .
*/
2001-03-24 17:00:36 +00:00
static void
2001-03-26 16:28:29 +00:00
fatalErrorDebug ( void * ctx ATTRIBUTE_UNUSED , const char * msg , . . . )
1999-04-21 20:12:07 +00:00
{
va_list args ;
2002-03-20 19:24:21 +00:00
callbacks + + ;
if ( quiet )
return ;
1999-04-21 20:12:07 +00:00
va_start ( args , msg ) ;
1999-05-29 11:51:49 +00:00
fprintf ( stdout , " SAX.fatalError: " ) ;
vfprintf ( stdout , msg , args ) ;
1999-04-21 20:12:07 +00:00
va_end ( args ) ;
}
xmlSAXHandler debugSAXHandlerStruct = {
internalSubsetDebug ,
isStandaloneDebug ,
hasInternalSubsetDebug ,
hasExternalSubsetDebug ,
resolveEntityDebug ,
getEntityDebug ,
entityDeclDebug ,
notationDeclDebug ,
attributeDeclDebug ,
elementDeclDebug ,
unparsedEntityDeclDebug ,
setDocumentLocatorDebug ,
startDocumentDebug ,
endDocumentDebug ,
startElementDebug ,
endElementDebug ,
referenceDebug ,
charactersDebug ,
ignorableWhitespaceDebug ,
processingInstructionDebug ,
commentDebug ,
warningDebug ,
errorDebug ,
fatalErrorDebug ,
1999-08-10 19:04:08 +00:00
getParameterEntityDebug ,
2001-03-24 17:00:36 +00:00
cdataBlockDebug ,
2001-10-13 09:15:48 +00:00
externalSubsetDebug ,
1
1999-04-21 20:12:07 +00:00
} ;
xmlSAXHandlerPtr debugSAXHandler = & debugSAXHandlerStruct ;
/************************************************************************
* *
* Debug *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-03-24 17:00:36 +00:00
static void
parseAndPrintFile ( char * filename ) {
1999-09-26 11:31:02 +00:00
int res ;
1999-04-21 20:12:07 +00:00
1999-12-28 16:35:14 +00:00
if ( push ) {
FILE * f ;
/*
* Empty callbacks for checking
*/
f = fopen ( filename , " r " ) ;
if ( f ! = NULL ) {
2001-03-24 17:00:36 +00:00
int ret ;
1999-12-28 16:35:14 +00:00
char chars [ 10 ] ;
xmlParserCtxtPtr ctxt ;
2001-03-24 17:00:36 +00:00
ret = fread ( chars , 1 , 4 , f ) ;
if ( ret > 0 ) {
1999-12-28 16:35:14 +00:00
ctxt = xmlCreatePushParserCtxt ( emptySAXHandler , NULL ,
2001-03-24 17:00:36 +00:00
chars , ret , filename ) ;
while ( ( ret = fread ( chars , 1 , 3 , f ) ) > 0 ) {
xmlParseChunk ( ctxt , chars , ret , 0 ) ;
1999-12-28 16:35:14 +00:00
}
xmlParseChunk ( ctxt , chars , 0 , 1 ) ;
xmlFreeParserCtxt ( ctxt ) ;
}
fclose ( f ) ;
} else {
2000-10-25 19:56:55 +00:00
xmlGenericError ( xmlGenericErrorContext ,
" Cannot read file %s \n " , filename ) ;
1999-12-28 16:35:14 +00:00
}
/*
* Debug callback
*/
f = fopen ( filename , " r " ) ;
if ( f ! = NULL ) {
2001-03-24 17:00:36 +00:00
int ret ;
1999-12-28 16:35:14 +00:00
char chars [ 10 ] ;
xmlParserCtxtPtr ctxt ;
2001-03-24 17:00:36 +00:00
ret = fread ( chars , 1 , 4 , f ) ;
if ( ret > 0 ) {
1999-12-28 16:35:14 +00:00
ctxt = xmlCreatePushParserCtxt ( debugSAXHandler , NULL ,
2001-03-24 17:00:36 +00:00
chars , ret , filename ) ;
while ( ( ret = fread ( chars , 1 , 3 , f ) ) > 0 ) {
xmlParseChunk ( ctxt , chars , ret , 0 ) ;
1999-12-28 16:35:14 +00:00
}
2001-03-24 17:00:36 +00:00
ret = xmlParseChunk ( ctxt , chars , 0 , 1 ) ;
1999-12-28 16:35:14 +00:00
xmlFreeParserCtxt ( ctxt ) ;
2001-03-24 17:00:36 +00:00
if ( ret ! = 0 ) {
1999-12-28 16:35:14 +00:00
fprintf ( stdout ,
2001-03-24 17:00:36 +00:00
" xmlSAXUserParseFile returned error %d \n " , ret ) ;
1999-12-28 16:35:14 +00:00
}
}
fclose ( f ) ;
}
} else {
2000-04-12 13:27:38 +00:00
if ( ! speed ) {
/*
* Empty callbacks for checking
*/
2002-03-20 19:24:21 +00:00
if ( ! quiet ) {
res = xmlSAXUserParseFile ( emptySAXHandler , NULL , filename ) ;
if ( res ! = 0 ) {
fprintf ( stdout , " xmlSAXUserParseFile returned error %d \n " , res ) ;
}
2000-04-12 13:27:38 +00:00
}
1999-04-21 20:12:07 +00:00
2000-04-12 13:27:38 +00:00
/*
* Debug callback
*/
2002-03-20 19:24:21 +00:00
callbacks = 0 ;
2000-04-12 13:27:38 +00:00
res = xmlSAXUserParseFile ( debugSAXHandler , NULL , filename ) ;
if ( res ! = 0 ) {
fprintf ( stdout , " xmlSAXUserParseFile returned error %d \n " , res ) ;
}
2002-03-20 19:24:21 +00:00
if ( quiet )
fprintf ( stdout , " %d callbacks generated \n " , callbacks ) ;
2000-04-12 13:27:38 +00:00
} else {
/*
* test 100 x the SAX parse
*/
int i ;
for ( i = 0 ; i < 100 ; i + + )
res = xmlSAXUserParseFile ( emptySAXHandler , NULL , filename ) ;
if ( res ! = 0 ) {
fprintf ( stdout , " xmlSAXUserParseFile returned error %d \n " , res ) ;
}
1999-12-28 16:35:14 +00:00
}
1999-04-21 20:12:07 +00:00
}
}
int main ( int argc , char * * argv ) {
int i ;
int files = 0 ;
for ( i = 1 ; i < argc ; i + + ) {
if ( ( ! strcmp ( argv [ i ] , " -debug " ) ) | | ( ! strcmp ( argv [ i ] , " --debug " ) ) )
debug + + ;
else if ( ( ! strcmp ( argv [ i ] , " -copy " ) ) | | ( ! strcmp ( argv [ i ] , " --copy " ) ) )
copy + + ;
else if ( ( ! strcmp ( argv [ i ] , " -recover " ) ) | |
( ! strcmp ( argv [ i ] , " --recover " ) ) )
recovery + + ;
1999-12-28 16:35:14 +00:00
else if ( ( ! strcmp ( argv [ i ] , " -push " ) ) | |
( ! strcmp ( argv [ i ] , " --push " ) ) )
push + + ;
2000-04-12 13:27:38 +00:00
else if ( ( ! strcmp ( argv [ i ] , " -speed " ) ) | |
( ! strcmp ( argv [ i ] , " --speed " ) ) )
speed + + ;
2002-03-18 18:36:20 +00:00
else if ( ( ! strcmp ( argv [ i ] , " -noent " ) ) | |
( ! strcmp ( argv [ i ] , " --noent " ) ) )
noent + + ;
2002-03-20 19:24:21 +00:00
else if ( ( ! strcmp ( argv [ i ] , " -quiet " ) ) | |
( ! strcmp ( argv [ i ] , " --quiet " ) ) )
quiet + + ;
1999-04-21 20:12:07 +00:00
}
2002-03-18 18:36:20 +00:00
if ( noent ! = 0 ) xmlSubstituteEntitiesDefault ( 1 ) ;
1999-04-21 20:12:07 +00:00
for ( i = 1 ; i < argc ; i + + ) {
if ( argv [ i ] [ 0 ] ! = ' - ' ) {
parseAndPrintFile ( argv [ i ] ) ;
files + + ;
}
}
1999-12-01 09:51:45 +00:00
xmlCleanupParser ( ) ;
xmlMemoryDump ( ) ;
1999-04-21 20:12:07 +00:00
return ( 0 ) ;
}