1998-07-24 23:24:09 +04:00
/*
* tester . c : a small tester program for XML input .
*
* See Copyright for the status of this software .
*
1999-01-17 22:11:59 +03:00
* Daniel . Veillard @ w3 . org
1998-07-24 23:24:09 +04:00
*/
1998-08-13 07:39:55 +04:00
# ifdef WIN32
# define HAVE_FCNTL_H
# include <io.h>
# else
1999-09-22 13:46:25 +04:00
# include "config.h"
1998-08-13 07:39:55 +04:00
# endif
1999-09-22 13:46:25 +04:00
# include <stdio.h>
# include <string.h>
# ifdef HAVE_SYS_TYPES_H
1998-07-24 23:24:09 +04:00
# include <sys/types.h>
1999-09-22 13:46:25 +04:00
# endif
1998-08-13 07:39:55 +04:00
# ifdef HAVE_SYS_STAT_H
1998-07-24 23:24:09 +04:00
# include <sys/stat.h>
1998-08-13 07:39:55 +04:00
# endif
# ifdef HAVE_FCNTL_H
1998-07-24 23:24:09 +04:00
# include <fcntl.h>
1998-08-13 07:39:55 +04:00
# endif
# ifdef HAVE_UNISTD_H
1998-07-24 23:24:09 +04:00
# include <unistd.h>
1998-08-13 07:39:55 +04:00
# endif
1999-09-22 13:46:25 +04:00
# ifdef HAVE_STDLIB_H
1998-10-02 00:51:15 +04:00
# include <stdlib.h>
1999-09-22 13:46:25 +04:00
# endif
1998-07-24 23:24:09 +04:00
1999-09-03 02:04:43 +04:00
# include "xmlmemory.h"
1998-08-13 07:39:55 +04:00
# include "parser.h"
# include "tree.h"
1998-10-28 01:56:57 +03:00
# include "debugXML.h"
static int debug = 0 ;
1998-11-27 09:39:50 +03:00
static int copy = 0 ;
1999-01-17 22:11:59 +03:00
static int recovery = 0 ;
1999-06-02 21:44:04 +04:00
static int noent = 0 ;
1999-07-27 23:52:06 +04:00
static int noout = 0 ;
1999-08-10 23:04:08 +04:00
static int valid = 0 ;
static int repeat = 0 ;
extern int xmlDoValidityCheckingDefaultValue ;
1998-07-24 23:24:09 +04:00
1998-08-13 07:39:55 +04:00
/*
* Note : there is a couple of errors introduced on purpose .
1999-09-24 02:19:22 +04:00
static xmlChar buffer [ ] =
1999-02-08 18:13:10 +03:00
" <?xml version= \" 1.0 \" ?> \n \
1998-07-24 23:24:09 +04:00
< ? xml : namespace ns = \ " http://www.ietf.org/standards/dav/ \" prefix = \" D \" ?> \n \
< ? xml : namespace ns = \ " http://www.w3.com/standards/z39.50/ \" prefix = \" Z \" ?> \n \
< D : propertyupdate > \ n \
< D : set a = \ " 'toto' \" b> \n \
< D : prop > \ n \
< Z : authors > \ n \
< Z : Author > Jim Whitehead < / Z : Author > \ n \
< Z : Author > Roy Fielding < / Z : Author > \ n \
< / Z : authors > \ n \
< / D : prop > \ n \
< / D : set > \ n \
< D : remove > \ n \
< D : prop > < Z : Copyright - Owner / > < / D : prop > \ n \
< / D : remove > \ n \
< / D : propertyupdate > \ n \
\ n \
" ;
1999-07-15 18:24:29 +04:00
*/
1998-07-24 23:24:09 +04:00
1998-10-29 08:51:30 +03:00
/************************************************************************
* *
* Debug *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int treeTest ( void ) {
1998-11-27 09:39:50 +03:00
xmlDocPtr doc , tmp ;
xmlNodePtr tree , subtree ;
1998-10-29 08:51:30 +03:00
/*
* build a fake XML document
*/
1999-08-30 01:02:19 +04:00
doc = xmlNewDoc ( BAD_CAST " 1.0 " ) ;
doc - > root = xmlNewDocNode ( doc , NULL , BAD_CAST " EXAMPLE " , NULL ) ;
xmlSetProp ( doc - > root , BAD_CAST " prop1 " , BAD_CAST " gnome is great " ) ;
xmlSetProp ( doc - > root , BAD_CAST " prop2 " , BAD_CAST " &linux; too " ) ;
xmlSetProp ( doc - > root , BAD_CAST " emptyprop " , BAD_CAST " " ) ;
tree = xmlNewChild ( doc - > root , NULL , BAD_CAST " head " , NULL ) ;
subtree = xmlNewChild ( tree , NULL , BAD_CAST " title " ,
BAD_CAST " Welcome to Gnome " ) ;
tree = xmlNewChild ( doc - > root , NULL , BAD_CAST " chapter " , NULL ) ;
subtree = xmlNewChild ( tree , NULL , BAD_CAST " title " ,
BAD_CAST " The Linux adventure " ) ;
subtree = xmlNewChild ( tree , NULL , BAD_CAST " p " , BAD_CAST " bla bla bla ... " ) ;
subtree = xmlNewChild ( tree , NULL , BAD_CAST " image " , NULL ) ;
xmlSetProp ( subtree , BAD_CAST " href " , BAD_CAST " linus.gif " ) ;
1998-10-29 08:51:30 +03:00
1998-11-27 09:39:50 +03:00
/*
* test intermediate copy if needed .
*/
if ( copy ) {
tmp = doc ;
doc = xmlCopyDoc ( doc , 1 ) ;
xmlFreeDoc ( tmp ) ;
}
1998-10-29 08:51:30 +03:00
/*
* print it .
*/
1999-07-27 23:52:06 +04:00
if ( noout = = 0 )
xmlDocDump ( stdout , doc ) ;
1998-10-29 08:51:30 +03:00
/*
* free it .
*/
xmlFreeDoc ( doc ) ;
return ( 0 ) ;
}
1998-07-24 23:24:09 +04:00
1998-08-13 07:39:55 +04:00
void parseAndPrintFile ( char * filename ) {
1998-11-27 09:39:50 +03:00
xmlDocPtr doc , tmp ;
1998-08-13 07:39:55 +04:00
/*
* build an XML tree from a string ;
*/
1999-01-17 22:11:59 +03:00
if ( recovery )
doc = xmlRecoverFile ( filename ) ;
else
doc = xmlParseFile ( filename ) ;
1998-08-13 07:39:55 +04:00
1998-11-27 09:39:50 +03:00
/*
* test intermediate copy if needed .
*/
if ( copy ) {
tmp = doc ;
doc = xmlCopyDoc ( doc , 1 ) ;
xmlFreeDoc ( tmp ) ;
}
1998-08-13 07:39:55 +04:00
/*
* print it .
*/
1999-07-27 23:52:06 +04:00
if ( noout = = 0 ) {
if ( ! debug )
xmlDocDump ( stdout , doc ) ;
else
xmlDebugDumpDocument ( stdout , doc ) ;
}
1998-08-13 07:39:55 +04:00
/*
* free it .
*/
xmlFreeDoc ( doc ) ;
1998-07-24 23:24:09 +04:00
}
1999-09-24 02:19:22 +04:00
void parseAndPrintBuffer ( xmlChar * buf ) {
1998-11-27 09:39:50 +03:00
xmlDocPtr doc , tmp ;
1998-07-24 23:24:09 +04:00
/*
1998-08-13 07:39:55 +04:00
* build an XML tree from a string ;
1998-07-24 23:24:09 +04:00
*/
1999-01-17 22:11:59 +03:00
if ( recovery )
doc = xmlRecoverDoc ( buf ) ;
else
doc = xmlParseDoc ( buf ) ;
1998-07-24 23:24:09 +04:00
1998-11-27 09:39:50 +03:00
/*
* test intermediate copy if needed .
*/
if ( copy ) {
tmp = doc ;
doc = xmlCopyDoc ( doc , 1 ) ;
xmlFreeDoc ( tmp ) ;
}
1998-07-24 23:24:09 +04:00
/*
* print it .
*/
1998-10-28 01:56:57 +03:00
if ( ! debug )
xmlDocDump ( stdout , doc ) ;
else
xmlDebugDumpDocument ( stdout , doc ) ;
1998-07-24 23:24:09 +04:00
/*
* free it .
*/
xmlFreeDoc ( doc ) ;
}
int main ( int argc , char * * argv ) {
1999-08-10 23:04:08 +04:00
int i , count ;
1998-11-27 09:39:50 +03:00
int files = 0 ;
1998-07-24 23:24:09 +04:00
1998-11-27 09:39:50 +03:00
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 + + ;
1999-01-17 22:11:59 +03:00
else if ( ( ! strcmp ( argv [ i ] , " -recover " ) ) | |
( ! strcmp ( argv [ i ] , " --recover " ) ) )
recovery + + ;
1999-06-02 21:44:04 +04:00
else if ( ( ! strcmp ( argv [ i ] , " -noent " ) ) | |
( ! strcmp ( argv [ i ] , " --noent " ) ) )
noent + + ;
1999-07-27 23:52:06 +04:00
else if ( ( ! strcmp ( argv [ i ] , " -noout " ) ) | |
( ! strcmp ( argv [ i ] , " --noout " ) ) )
noout + + ;
1999-08-10 23:04:08 +04:00
else if ( ( ! strcmp ( argv [ i ] , " -valid " ) ) | |
( ! strcmp ( argv [ i ] , " --valid " ) ) )
valid + + ;
else if ( ( ! strcmp ( argv [ i ] , " -repeat " ) ) | |
( ! strcmp ( argv [ i ] , " --repeat " ) ) )
repeat + + ;
1998-11-27 09:39:50 +03:00
}
1999-06-02 21:44:04 +04:00
if ( noent ! = 0 ) xmlSubstituteEntitiesDefault ( 1 ) ;
1999-08-10 23:04:08 +04:00
if ( valid ! = 0 ) xmlDoValidityCheckingDefaultValue = 1 ;
1998-11-27 09:39:50 +03:00
for ( i = 1 ; i < argc ; i + + ) {
if ( argv [ i ] [ 0 ] ! = ' - ' ) {
1999-08-10 23:04:08 +04:00
if ( repeat ) {
for ( count = 0 ; count < 100 * repeat ; count + + )
parseAndPrintFile ( argv [ i ] ) ;
} else
parseAndPrintFile ( argv [ i ] ) ;
1998-11-27 09:39:50 +03:00
files + + ;
1998-07-24 23:24:09 +04:00
}
1998-11-27 09:39:50 +03:00
}
if ( files = = 0 ) {
1999-07-27 23:52:06 +04:00
printf ( " Usage : %s [--debug] [--copy] [--recover] [--noent] [--noout] XMLfiles ... \n " ,
1999-06-23 01:49:07 +04:00
argv [ 0 ] ) ;
printf ( " \t Parse the XML files and output the result of the parsing \n " ) ;
printf ( " \t --debug : dump a debug tree of the in-memory document \n " ) ;
printf ( " \t --copy : used to test the internal copy implementation \n " ) ;
printf ( " \t --recover : output what is parsable on broken XmL documents \n " ) ;
printf ( " \t --noent : substitute entity references by their value \n " ) ;
1999-07-27 23:52:06 +04:00
printf ( " \t --noout : don't output the result \n " ) ;
1999-08-10 23:04:08 +04:00
printf ( " \t --repeat : parse the file 100 times, for timing or profiling \n " ) ;
1998-10-29 08:51:30 +03:00
}
1999-09-03 02:04:43 +04:00
xmlMemoryDump ( ) ;
1998-07-24 23:24:09 +04:00
return ( 0 ) ;
}