1999-07-15 18:24:29 +04:00
/*
* testXPath . c : a small tester program for XPath .
*
* See Copyright for the status of this software .
*
2001-06-24 16:13:24 +04:00
* daniel @ veillard . com
1999-07-15 18:24:29 +04:00
*/
2001-04-21 20:57:29 +04:00
# include "libxml.h"
2000-04-03 23:48:13 +04:00
# if defined(LIBXML_XPATH_ENABLED) && defined(LIBXML_DEBUG_ENABLED)
1999-09-22 13:46:25 +04:00
# include <string.h>
# ifdef HAVE_SYS_TYPES_H
1999-07-15 18:24:29 +04:00
# include <sys/types.h>
1999-09-22 13:46:25 +04:00
# endif
1999-07-15 18:24:29 +04: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 13:46:25 +04:00
# ifdef HAVE_STDLIB_H
1999-07-15 18:24:29 +04:00
# include <stdlib.h>
1999-09-22 13:46:25 +04:00
# endif
1999-07-15 18:24:29 +04:00
2000-04-03 23:48:13 +04:00
# include <libxml/xpath.h>
# include <libxml/tree.h>
# include <libxml/parser.h>
# include <libxml/debugXML.h>
# include <libxml/xmlmemory.h>
2000-10-03 03:04:54 +04:00
# include <libxml/parserInternals.h>
2000-11-06 19:43:11 +03:00
# include <libxml/xpathInternals.h>
2000-10-25 23:56:55 +04:00
# include <libxml/xmlerror.h>
2001-10-17 19:58:35 +04:00
# include <libxml/globals.h>
2000-10-04 17:33:43 +04:00
# if defined(LIBXML_XPTR_ENABLED)
# include <libxml/xpointer.h>
static int xptr = 0 ;
# endif
1999-07-15 18:24:29 +04:00
static int debug = 0 ;
2000-10-03 03:04:54 +04:00
static int valid = 0 ;
1999-07-15 18:24:29 +04:00
static int expr = 0 ;
2001-04-03 22:22:00 +04:00
static int tree = 0 ;
2001-07-08 17:15:55 +04:00
static int nocdata = 0 ;
1999-07-15 18:24:29 +04:00
static xmlDocPtr document = NULL ;
/*
* Default document
*/
1999-09-24 02:19:22 +04:00
static xmlChar buffer [ ] =
1999-07-15 18:24:29 +04:00
" <?xml version= \" 1.0 \" ?> \n \
< EXAMPLE prop1 = \ " gnome is great \" prop2= \" & linux too \" > \n \
< head > \ n \
< title > Welcome to Gnome < / title > \ n \
< / head > \ n \
< chapter > \ n \
< title > The Linux adventure < / title > \ n \
< p > bla bla bla . . . < / p > \ n \
< image href = \ " linus.gif \" /> \n \
< p > . . . < / p > \ n \
< / chapter > \ n \
< chapter > \ n \
< title > Chapter 2 < / title > \ n \
< p > this is chapter 2 . . . < / p > \ n \
< / chapter > \ n \
< chapter > \ n \
< title > Chapter 3 < / title > \ n \
< p > this is chapter 3 . . . < / p > \ n \
< / chapter > \ n \
< / EXAMPLE > \ n \
" ;
2001-03-24 20:00:36 +03:00
static void
testXPath ( const char * str ) {
1999-07-15 18:24:29 +04:00
xmlXPathObjectPtr res ;
xmlXPathContextPtr ctxt ;
2000-10-04 17:33:43 +04:00
# if defined(LIBXML_XPTR_ENABLED)
if ( xptr ) {
ctxt = xmlXPtrNewContext ( document , NULL , NULL ) ;
res = xmlXPtrEval ( BAD_CAST str , ctxt ) ;
} else {
# endif
ctxt = xmlXPathNewContext ( document ) ;
2001-01-03 18:24:58 +03:00
ctxt - > node = xmlDocGetRootElement ( document ) ;
2000-10-04 17:33:43 +04:00
if ( expr )
res = xmlXPathEvalExpression ( BAD_CAST str , ctxt ) ;
2001-04-03 22:22:00 +04:00
else {
/* res = xmlXPathEval(BAD_CAST str, ctxt); */
xmlXPathCompExprPtr comp ;
comp = xmlXPathCompile ( BAD_CAST str ) ;
if ( comp ! = NULL ) {
if ( tree )
xmlXPathDebugDumpCompExpr ( stdout , comp , 0 ) ;
res = xmlXPathCompiledEval ( comp , ctxt ) ;
2001-04-05 20:54:14 +04:00
xmlXPathFreeCompExpr ( comp ) ;
2001-04-03 22:22:00 +04:00
} else
res = NULL ;
}
2000-10-04 17:33:43 +04:00
# if defined(LIBXML_XPTR_ENABLED)
}
# endif
2000-10-13 03:15:24 +04:00
xmlXPathDebugDumpObject ( stdout , res , 0 ) ;
1999-07-15 18:24:29 +04:00
xmlXPathFreeObject ( res ) ;
1999-07-27 23:52:06 +04:00
xmlXPathFreeContext ( ctxt ) ;
1999-07-15 18:24:29 +04:00
}
2001-03-24 20:00:36 +03:00
static void
testXPathFile ( const char * filename ) {
1999-07-15 18:24:29 +04:00
FILE * input ;
2001-03-24 20:00:36 +03:00
char expression [ 5000 ] ;
2000-10-06 16:59:53 +04:00
int len ;
1999-07-15 18:24:29 +04:00
input = fopen ( filename , " r " ) ;
if ( input = = NULL ) {
2000-10-25 23:56:55 +04:00
xmlGenericError ( xmlGenericErrorContext ,
" Cannot open %s for reading \n " , filename ) ;
1999-07-15 18:24:29 +04:00
return ;
}
2001-03-24 20:00:36 +03:00
while ( fgets ( expression , 4500 , input ) ! = NULL ) {
len = strlen ( expression ) ;
2000-10-06 16:59:53 +04:00
len - - ;
while ( ( len > = 0 ) & &
2001-03-24 20:00:36 +03:00
( ( expression [ len ] = = ' \n ' ) | | ( expression [ len ] = = ' \t ' ) | |
( expression [ len ] = = ' \r ' ) | | ( expression [ len ] = = ' ' ) ) ) len - - ;
expression [ len + 1 ] = 0 ;
2000-10-06 16:59:53 +04:00
if ( len > = 0 ) {
2001-03-24 20:00:36 +03:00
printf ( " \n ======================== \n Expression: %s \n " , expression ) ;
testXPath ( expression ) ;
2000-10-06 16:59:53 +04:00
}
1999-07-15 18:24:29 +04:00
}
fclose ( input ) ;
}
int main ( int argc , char * * argv ) {
int i ;
int strings = 0 ;
int usefile = 0 ;
char * filename = NULL ;
for ( i = 1 ; i < argc ; i + + ) {
2000-10-04 17:33:43 +04:00
# if defined(LIBXML_XPTR_ENABLED)
if ( ( ! strcmp ( argv [ i ] , " -xptr " ) ) | | ( ! strcmp ( argv [ i ] , " --xptr " ) ) )
xptr + + ;
2001-08-16 20:27:41 +04:00
else
2000-10-04 17:33:43 +04:00
# endif
2001-08-16 20:27:41 +04:00
if ( ( ! strcmp ( argv [ i ] , " -debug " ) ) | | ( ! strcmp ( argv [ i ] , " --debug " ) ) )
1999-07-15 18:24:29 +04:00
debug + + ;
2001-07-08 17:15:55 +04:00
else if ( ( ! strcmp ( argv [ i ] , " -valid " ) ) | | ( ! strcmp ( argv [ i ] , " --valid " ) ) )
2000-10-03 03:04:54 +04:00
valid + + ;
2001-07-08 17:15:55 +04:00
else if ( ( ! strcmp ( argv [ i ] , " -expr " ) ) | | ( ! strcmp ( argv [ i ] , " --expr " ) ) )
1999-07-15 18:24:29 +04:00
expr + + ;
2001-07-08 17:15:55 +04:00
else if ( ( ! strcmp ( argv [ i ] , " -tree " ) ) | | ( ! strcmp ( argv [ i ] , " --tree " ) ) )
2001-04-03 22:22:00 +04:00
tree + + ;
2001-07-08 17:15:55 +04:00
else if ( ( ! strcmp ( argv [ i ] , " -nocdata " ) ) | |
( ! strcmp ( argv [ i ] , " --nocdata " ) ) )
nocdata + + ;
else if ( ( ! strcmp ( argv [ i ] , " -i " ) ) | | ( ! strcmp ( argv [ i ] , " --input " ) ) )
1999-07-15 18:24:29 +04:00
filename = argv [ + + i ] ;
2001-07-08 17:15:55 +04:00
else if ( ( ! strcmp ( argv [ i ] , " -f " ) ) | | ( ! strcmp ( argv [ i ] , " --file " ) ) )
1999-07-15 18:24:29 +04:00
usefile + + ;
}
2000-10-03 03:04:54 +04:00
if ( valid ! = 0 ) xmlDoValidityCheckingDefaultValue = 1 ;
2001-08-07 05:10:10 +04:00
xmlLoadExtDtdDefaultValue | = XML_DETECT_IDS ;
xmlLoadExtDtdDefaultValue | = XML_COMPLETE_ATTRS ;
2002-11-16 18:35:11 +03:00
xmlSubstituteEntitiesDefaultValue = 1 ;
2001-07-08 17:15:55 +04:00
if ( nocdata ! = 0 ) {
xmlDefaultSAXHandlerInit ( ) ;
xmlDefaultSAXHandler . cdataBlock = NULL ;
}
1999-07-15 18:24:29 +04:00
if ( document = = NULL ) {
if ( filename = = NULL )
document = xmlParseDoc ( buffer ) ;
else
document = xmlParseFile ( filename ) ;
}
for ( i = 1 ; i < argc ; i + + ) {
if ( ( ! strcmp ( argv [ i ] , " -i " ) ) | | ( ! strcmp ( argv [ i ] , " --input " ) ) ) {
i + + ; continue ;
}
if ( argv [ i ] [ 0 ] ! = ' - ' ) {
if ( usefile )
testXPathFile ( argv [ i ] ) ;
else
testXPath ( argv [ i ] ) ;
strings + + ;
}
}
if ( strings = = 0 ) {
printf ( " Usage : %s [--debug] [--copy] stringsorfiles ... \n " ,
argv [ 0 ] ) ;
printf ( " \t Parse the XPath strings and output the result of the parsing \n " ) ;
printf ( " \t --debug : dump a debug version of the result \n " ) ;
2000-10-03 03:04:54 +04:00
printf ( " \t --valid : switch on DTD support in the parser \n " ) ;
2000-10-26 18:07:44 +04:00
# if defined(LIBXML_XPTR_ENABLED)
printf ( " \t --xptr : expressions are XPointer expressions \n " ) ;
# endif
1999-07-15 18:24:29 +04:00
printf ( " \t --expr : debug XPath expressions only \n " ) ;
2001-04-03 22:22:00 +04:00
printf ( " \t --tree : show the compiled XPath tree \n " ) ;
2001-07-08 17:15:55 +04:00
printf ( " \t --nocdata : do not generate CDATA nodes \n " ) ;
1999-07-15 18:24:29 +04:00
printf ( " \t --input filename : or \n " ) ;
printf ( " \t -i filename : read the document from filename \n " ) ;
printf ( " \t --file : or \n " ) ;
printf ( " \t -f : read queries from files, args \n " ) ;
}
1999-12-01 12:51:45 +03:00
if ( document ! = NULL )
xmlFreeDoc ( document ) ;
xmlCleanupParser ( ) ;
xmlMemoryDump ( ) ;
1999-07-15 18:24:29 +04:00
return ( 0 ) ;
}
2000-04-03 23:48:13 +04:00
# else
# include <stdio.h>
int main ( int argc , char * * argv ) {
printf ( " %s : XPath/Debug support not compiled in \n " , argv [ 0 ] ) ;
return ( 0 ) ;
}
# endif /* LIBXML_XPATH_ENABLED */