2001-02-23 20:55:21 +03:00
/*
* xinclude . c : Code to implement XInclude processing
*
2003-12-09 14:35:37 +03:00
* World Wide Web Consortium W3C Last Call Working Draft 10 November 2003
* http : //www.w3.org/TR/2003/WD-xinclude-20031110
2001-02-23 20:55:21 +03:00
*
* See Copyright for the status of this software .
*
2001-06-24 16:13:24 +04:00
* daniel @ veillard . com
2001-02-23 20:55:21 +03:00
*/
2002-03-18 22:37:11 +03:00
# define IN_LIBXML
2001-04-21 20:57:29 +04:00
# include "libxml.h"
2001-02-23 20:55:21 +03:00
# include <string.h>
# include <libxml/xmlmemory.h>
# include <libxml/tree.h>
# include <libxml/parser.h>
# include <libxml/uri.h>
2013-06-17 15:01:33 +04:00
# include <libxml/xpath.h>
2001-02-23 20:55:21 +03:00
# include <libxml/xpointer.h>
# include <libxml/parserInternals.h>
# include <libxml/xmlerror.h>
2002-11-20 16:28:31 +03:00
# include <libxml/encoding.h>
2001-10-17 19:58:35 +04:00
# include <libxml/globals.h>
2001-02-23 20:55:21 +03:00
# ifdef LIBXML_XINCLUDE_ENABLED
# include <libxml/xinclude.h>
2022-08-26 02:22:33 +03:00
# include "private/buf.h"
# include "private/error.h"
2022-10-22 20:08:43 +03:00
# include "private/tree.h"
2022-10-30 14:21:20 +03:00
# include "private/xinclude.h"
2001-02-23 20:55:21 +03:00
2003-02-13 14:02:08 +03:00
# define XINCLUDE_MAX_DEPTH 40
2003-08-14 19:44:40 +04:00
/* #define DEBUG_XINCLUDE */
2001-06-21 15:20:21 +04:00
# ifdef DEBUG_XINCLUDE
# ifdef LIBXML_DEBUG_ENABLED
# include <libxml/debugXML.h>
# endif
# endif
2001-02-23 20:55:21 +03:00
/************************************************************************
* *
2003-12-30 11:30:19 +03:00
* XInclude context handling *
2001-02-23 20:55:21 +03:00
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* An XInclude context
*/
2001-02-26 04:36:19 +03:00
typedef xmlChar * xmlURL ;
2002-09-05 14:52:10 +04:00
typedef struct _xmlXIncludeRef xmlXIncludeRef ;
typedef xmlXIncludeRef * xmlXIncludeRefPtr ;
struct _xmlXIncludeRef {
2003-12-30 11:30:19 +03:00
xmlChar * URI ; /* the fully resolved resource URL */
2002-09-05 14:52:10 +04:00
xmlChar * fragment ; /* the fragment in the URI */
2022-10-23 14:57:33 +03:00
xmlNodePtr elem ; /* the xi:include element */
2002-09-05 14:52:10 +04:00
xmlNodePtr inc ; /* the included copy */
int xml ; /* xml or txt */
2020-08-17 01:54:12 +03:00
int fallback ; /* fallback was loaded */
2020-08-22 01:43:18 +03:00
int emptyFb ; /* flag to show fallback empty */
2022-10-22 17:09:21 +03:00
int expanding ; /* flag to detect inclusion loops */
2022-10-23 18:52:29 +03:00
int replace ; /* should the node be replaced? */
2002-09-05 14:52:10 +04:00
} ;
2022-10-23 17:02:48 +03:00
typedef struct _xmlXIncludeDoc xmlXIncludeDoc ;
typedef xmlXIncludeDoc * xmlXIncludeDocPtr ;
struct _xmlXIncludeDoc {
xmlDocPtr doc ; /* the parsed document */
xmlChar * url ; /* the URL */
int expanding ; /* flag to detect inclusion loops */
} ;
typedef struct _xmlXIncludeTxt xmlXIncludeTxt ;
typedef xmlXIncludeTxt * xmlXIncludeTxtPtr ;
struct _xmlXIncludeTxt {
xmlChar * text ; /* text string */
xmlChar * url ; /* the URL */
} ;
2001-02-23 20:55:21 +03:00
struct _xmlXIncludeCtxt {
xmlDocPtr doc ; /* the source document */
int incNr ; /* number of includes */
int incMax ; /* size of includes tab */
2002-09-05 14:52:10 +04:00
xmlXIncludeRefPtr * incTab ; /* array of included references */
2001-02-23 20:55:21 +03:00
int txtNr ; /* number of unparsed documents */
int txtMax ; /* size of unparsed documents tab */
2022-10-23 17:02:48 +03:00
xmlXIncludeTxt * txtTab ; /* array of unparsed documents */
2003-02-11 21:03:05 +03:00
2022-10-23 17:02:48 +03:00
int urlNr ; /* number of documents stacked */
int urlMax ; /* size of document stack */
xmlXIncludeDoc * urlTab ; /* document stack */
2003-02-13 14:02:08 +03:00
2003-02-11 21:03:05 +03:00
int nbErrors ; /* the number of errors detected */
2003-12-08 20:41:29 +03:00
int legacy ; /* using XINCLUDE_OLD_NS */
2003-12-09 14:35:37 +03:00
int parseFlags ; /* the flags used for parsing XML documents */
2004-06-07 12:57:27 +04:00
xmlChar * base ; /* the current xml:base */
2006-09-29 13:16:00 +04:00
void * _private ; /* application data */
2020-06-05 14:43:45 +03:00
unsigned long incTotal ; /* total number of processed inclusions */
2022-10-22 17:50:18 +03:00
int depth ; /* recursion depth */
2022-10-30 14:21:20 +03:00
int isStream ; /* streaming mode */
2001-02-23 20:55:21 +03:00
} ;
2022-10-22 03:19:22 +03:00
static xmlXIncludeRefPtr
xmlXIncludeExpandNode ( xmlXIncludeCtxtPtr ctxt , xmlNodePtr node ) ;
2022-10-21 17:17:48 +03:00
static int
2022-10-23 14:57:33 +03:00
xmlXIncludeLoadNode ( xmlXIncludeCtxtPtr ctxt , xmlXIncludeRefPtr ref ) ;
2022-10-21 17:17:48 +03:00
2001-05-23 17:44:21 +04:00
static int
2022-10-23 18:52:29 +03:00
xmlXIncludeDoProcess ( xmlXIncludeCtxtPtr ctxt , xmlNodePtr tree ) ;
2001-05-23 17:44:21 +04:00
2003-08-14 19:44:40 +04:00
2003-10-09 02:38:13 +04:00
/************************************************************************
* *
2012-09-11 09:26:36 +04:00
* XInclude error handler *
2003-10-09 02:38:13 +04:00
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-14 19:44:40 +04:00
/**
2003-10-09 02:38:13 +04:00
* xmlXIncludeErrMemory :
2003-12-30 11:30:19 +03:00
* @ extra : extra information
2003-08-14 19:44:40 +04:00
*
2003-10-09 02:38:13 +04:00
* Handle an out of memory condition
2003-08-14 19:44:40 +04:00
*/
static void
2003-10-09 02:38:13 +04:00
xmlXIncludeErrMemory ( xmlXIncludeCtxtPtr ctxt , xmlNodePtr node ,
const char * extra )
2003-08-14 19:44:40 +04:00
{
2003-10-09 02:38:13 +04:00
if ( ctxt ! = NULL )
ctxt - > nbErrors + + ;
2003-10-10 18:10:40 +04:00
__xmlRaiseError ( NULL , NULL , NULL , ctxt , node , XML_FROM_XINCLUDE ,
2003-10-09 02:38:13 +04:00
XML_ERR_NO_MEMORY , XML_ERR_ERROR , NULL , 0 ,
extra , NULL , NULL , 0 , 0 ,
" Memory allocation failed : %s \n " , extra ) ;
}
2003-08-14 19:44:40 +04:00
2003-10-09 02:38:13 +04:00
/**
* xmlXIncludeErr :
* @ ctxt : the XInclude context
* @ node : the context node
* @ msg : the error message
2003-12-30 11:30:19 +03:00
* @ extra : extra information
2003-10-09 02:38:13 +04:00
*
2003-12-08 20:41:29 +03:00
* Handle an XInclude error
2003-10-09 02:38:13 +04:00
*/
2016-05-13 10:13:17 +03:00
static void LIBXML_ATTR_FORMAT ( 4 , 0 )
2003-10-09 02:38:13 +04:00
xmlXIncludeErr ( xmlXIncludeCtxtPtr ctxt , xmlNodePtr node , int error ,
const char * msg , const xmlChar * extra )
{
if ( ctxt ! = NULL )
ctxt - > nbErrors + + ;
2003-10-10 18:10:40 +04:00
__xmlRaiseError ( NULL , NULL , NULL , ctxt , node , XML_FROM_XINCLUDE ,
2003-10-09 02:38:13 +04:00
error , XML_ERR_ERROR , NULL , 0 ,
( const char * ) extra , NULL , NULL , 0 , 0 ,
msg , ( const char * ) extra ) ;
2003-08-14 19:44:40 +04:00
}
2004-02-25 14:52:31 +03:00
#if 0
2003-12-08 20:41:29 +03:00
/**
* xmlXIncludeWarn :
* @ ctxt : the XInclude context
* @ node : the context node
* @ msg : the error message
2003-12-30 11:30:19 +03:00
* @ extra : extra information
2003-12-08 20:41:29 +03:00
*
* Emit an XInclude warning .
*/
2016-05-13 10:13:17 +03:00
static void LIBXML_ATTR_FORMAT ( 4 , 0 )
2003-12-08 20:41:29 +03:00
xmlXIncludeWarn ( xmlXIncludeCtxtPtr ctxt , xmlNodePtr node , int error ,
const char * msg , const xmlChar * extra )
{
__xmlRaiseError ( NULL , NULL , NULL , ctxt , node , XML_FROM_XINCLUDE ,
error , XML_ERR_WARNING , NULL , 0 ,
( const char * ) extra , NULL , NULL , 0 , 0 ,
msg , ( const char * ) extra ) ;
}
2004-02-25 14:52:31 +03:00
# endif
2003-12-08 20:41:29 +03:00
/**
* xmlXIncludeGetProp :
* @ ctxt : the XInclude context
* @ cur : the node
* @ name : the attribute name
*
* Get an XInclude attribute
*
* Returns the value ( to be freed ) or NULL if not found
*/
static xmlChar *
xmlXIncludeGetProp ( xmlXIncludeCtxtPtr ctxt , xmlNodePtr cur ,
const xmlChar * name ) {
xmlChar * ret ;
ret = xmlGetNsProp ( cur , XINCLUDE_NS , name ) ;
if ( ret ! = NULL )
return ( ret ) ;
if ( ctxt - > legacy ! = 0 ) {
ret = xmlGetNsProp ( cur , XINCLUDE_OLD_NS , name ) ;
if ( ret ! = NULL )
return ( ret ) ;
}
ret = xmlGetProp ( cur , name ) ;
return ( ret ) ;
}
2002-09-05 14:52:10 +04:00
/**
* xmlXIncludeFreeRef :
* @ ref : the XInclude reference
*
* Free an XInclude reference
*/
static void
xmlXIncludeFreeRef ( xmlXIncludeRefPtr ref ) {
if ( ref = = NULL )
return ;
# ifdef DEBUG_XINCLUDE
xmlGenericError ( xmlGenericErrorContext , " Freeing ref \n " ) ;
# endif
if ( ref - > URI ! = NULL )
xmlFree ( ref - > URI ) ;
if ( ref - > fragment ! = NULL )
xmlFree ( ref - > fragment ) ;
xmlFree ( ref ) ;
}
/**
* xmlXIncludeNewRef :
* @ ctxt : the XInclude context
* @ URI : the resource URI
2022-10-23 14:57:33 +03:00
* @ elem : the xi : include element
2002-09-05 14:52:10 +04:00
*
* Creates a new reference within an XInclude context
*
* Returns the new set
*/
static xmlXIncludeRefPtr
xmlXIncludeNewRef ( xmlXIncludeCtxtPtr ctxt , const xmlChar * URI ,
2022-10-23 14:57:33 +03:00
xmlNodePtr elem ) {
2002-09-05 14:52:10 +04:00
xmlXIncludeRefPtr ret ;
# ifdef DEBUG_XINCLUDE
xmlGenericError ( xmlGenericErrorContext , " New ref %s \n " , URI ) ;
# endif
ret = ( xmlXIncludeRefPtr ) xmlMalloc ( sizeof ( xmlXIncludeRef ) ) ;
2003-10-09 02:38:13 +04:00
if ( ret = = NULL ) {
2022-10-23 14:57:33 +03:00
xmlXIncludeErrMemory ( ctxt , elem , " growing XInclude context " ) ;
2002-09-05 14:52:10 +04:00
return ( NULL ) ;
2003-10-09 02:38:13 +04:00
}
2002-09-05 14:52:10 +04:00
memset ( ret , 0 , sizeof ( xmlXIncludeRef ) ) ;
if ( URI = = NULL )
ret - > URI = NULL ;
else
ret - > URI = xmlStrdup ( URI ) ;
ret - > fragment = NULL ;
2022-10-23 14:57:33 +03:00
ret - > elem = elem ;
2002-09-05 14:52:10 +04:00
ret - > xml = 0 ;
ret - > inc = NULL ;
if ( ctxt - > incMax = = 0 ) {
ctxt - > incMax = 4 ;
ctxt - > incTab = ( xmlXIncludeRefPtr * ) xmlMalloc ( ctxt - > incMax *
sizeof ( ctxt - > incTab [ 0 ] ) ) ;
if ( ctxt - > incTab = = NULL ) {
2022-10-23 14:57:33 +03:00
xmlXIncludeErrMemory ( ctxt , elem , " growing XInclude context " ) ;
2002-09-05 14:52:10 +04:00
xmlXIncludeFreeRef ( ret ) ;
return ( NULL ) ;
}
}
if ( ctxt - > incNr > = ctxt - > incMax ) {
ctxt - > incMax * = 2 ;
ctxt - > incTab = ( xmlXIncludeRefPtr * ) xmlRealloc ( ctxt - > incTab ,
ctxt - > incMax * sizeof ( ctxt - > incTab [ 0 ] ) ) ;
if ( ctxt - > incTab = = NULL ) {
2022-10-23 14:57:33 +03:00
xmlXIncludeErrMemory ( ctxt , elem , " growing XInclude context " ) ;
2002-09-05 14:52:10 +04:00
xmlXIncludeFreeRef ( ret ) ;
return ( NULL ) ;
}
}
ctxt - > incTab [ ctxt - > incNr + + ] = ret ;
return ( ret ) ;
}
2001-05-23 17:44:21 +04:00
/**
* xmlXIncludeNewContext :
* @ doc : an XML Document
*
* Creates a new XInclude context
*
* Returns the new set
*/
2003-11-03 15:31:38 +03:00
xmlXIncludeCtxtPtr
2001-05-23 17:44:21 +04:00
xmlXIncludeNewContext ( xmlDocPtr doc ) {
xmlXIncludeCtxtPtr ret ;
2002-09-05 14:52:10 +04:00
# ifdef DEBUG_XINCLUDE
xmlGenericError ( xmlGenericErrorContext , " New context \n " ) ;
# endif
2001-05-23 17:44:21 +04:00
if ( doc = = NULL )
return ( NULL ) ;
ret = ( xmlXIncludeCtxtPtr ) xmlMalloc ( sizeof ( xmlXIncludeCtxt ) ) ;
2003-10-09 02:38:13 +04:00
if ( ret = = NULL ) {
xmlXIncludeErrMemory ( NULL , ( xmlNodePtr ) doc ,
" creating XInclude context " ) ;
2001-05-23 17:44:21 +04:00
return ( NULL ) ;
2003-10-09 02:38:13 +04:00
}
2001-05-23 17:44:21 +04:00
memset ( ret , 0 , sizeof ( xmlXIncludeCtxt ) ) ;
ret - > doc = doc ;
ret - > incNr = 0 ;
ret - > incMax = 0 ;
ret - > incTab = NULL ;
2003-02-11 21:03:05 +03:00
ret - > nbErrors = 0 ;
2001-05-23 17:44:21 +04:00
return ( ret ) ;
}
/**
* xmlXIncludeFreeContext :
* @ ctxt : the XInclude context
*
* Free an XInclude context
*/
2003-11-03 15:31:38 +03:00
void
2001-05-23 17:44:21 +04:00
xmlXIncludeFreeContext ( xmlXIncludeCtxtPtr ctxt ) {
int i ;
2002-09-05 14:52:10 +04:00
# ifdef DEBUG_XINCLUDE
xmlGenericError ( xmlGenericErrorContext , " Freeing context \n " ) ;
# endif
2001-05-23 17:44:21 +04:00
if ( ctxt = = NULL )
return ;
2022-10-23 17:02:48 +03:00
if ( ctxt - > urlTab ! = NULL ) {
for ( i = 0 ; i < ctxt - > urlNr ; i + + ) {
xmlFreeDoc ( ctxt - > urlTab [ i ] . doc ) ;
xmlFree ( ctxt - > urlTab [ i ] . url ) ;
}
2003-02-13 14:02:08 +03:00
xmlFree ( ctxt - > urlTab ) ;
2022-10-23 17:02:48 +03:00
}
2002-09-05 14:52:10 +04:00
for ( i = 0 ; i < ctxt - > incNr ; i + + ) {
if ( ctxt - > incTab [ i ] ! = NULL )
xmlXIncludeFreeRef ( ctxt - > incTab [ i ] ) ;
2001-05-23 17:44:21 +04:00
}
2020-09-28 13:28:29 +03:00
if ( ctxt - > incTab ! = NULL )
xmlFree ( ctxt - > incTab ) ;
if ( ctxt - > txtTab ! = NULL ) {
for ( i = 0 ; i < ctxt - > txtNr ; i + + ) {
2022-10-23 17:02:48 +03:00
xmlFree ( ctxt - > txtTab [ i ] . text ) ;
xmlFree ( ctxt - > txtTab [ i ] . url ) ;
2020-09-28 13:28:29 +03:00
}
xmlFree ( ctxt - > txtTab ) ;
}
2004-11-05 13:03:46 +03:00
if ( ctxt - > base ! = NULL ) {
xmlFree ( ctxt - > base ) ;
}
2001-05-23 17:44:21 +04:00
xmlFree ( ctxt ) ;
}
2003-08-14 19:44:40 +04:00
/**
* xmlXIncludeParseFile :
* @ ctxt : the XInclude context
* @ URL : the URL or file path
2012-09-11 09:26:36 +04:00
*
2003-12-30 11:30:19 +03:00
* parse a document for XInclude
2003-08-14 19:44:40 +04:00
*/
static xmlDocPtr
2003-12-09 14:35:37 +03:00
xmlXIncludeParseFile ( xmlXIncludeCtxtPtr ctxt , const char * URL ) {
2003-08-14 19:44:40 +04:00
xmlDocPtr ret ;
xmlParserCtxtPtr pctxt ;
2003-12-09 14:35:37 +03:00
xmlParserInputPtr inputStream ;
2003-08-14 19:44:40 +04:00
xmlInitParser ( ) ;
2003-12-09 14:35:37 +03:00
pctxt = xmlNewParserCtxt ( ) ;
2003-08-14 19:44:40 +04:00
if ( pctxt = = NULL ) {
2003-12-09 14:35:37 +03:00
xmlXIncludeErrMemory ( ctxt , NULL , " cannot allocate parser context " ) ;
return ( NULL ) ;
}
2006-09-29 13:16:00 +04:00
/*
* pass in the application data to the parser context .
*/
pctxt - > _private = ctxt - > _private ;
2012-09-11 09:26:36 +04:00
2003-12-09 14:35:37 +03:00
/*
2003-12-30 11:30:19 +03:00
* try to ensure that new documents included are actually
2003-12-09 14:35:37 +03:00
* built with the same dictionary as the including document .
*/
2009-08-25 21:24:15 +04:00
if ( ( ctxt - > doc ! = NULL ) & & ( ctxt - > doc - > dict ! = NULL ) ) {
if ( pctxt - > dict ! = NULL )
xmlDictFree ( pctxt - > dict ) ;
2003-12-09 14:35:37 +03:00
pctxt - > dict = ctxt - > doc - > dict ;
xmlDictReference ( pctxt - > dict ) ;
}
xmlCtxtUseOptions ( pctxt , ctxt - > parseFlags | XML_PARSE_DTDLOAD ) ;
2012-09-11 09:26:36 +04:00
2019-09-20 13:44:17 +03:00
/* Don't read from stdin. */
if ( ( URL ! = NULL ) & & ( strcmp ( URL , " - " ) = = 0 ) )
URL = " ./- " ;
2003-12-09 14:35:37 +03:00
inputStream = xmlLoadExternalEntity ( URL , NULL , pctxt ) ;
if ( inputStream = = NULL ) {
xmlFreeParserCtxt ( pctxt ) ;
2003-08-14 19:44:40 +04:00
return ( NULL ) ;
}
2003-12-09 14:35:37 +03:00
inputPush ( pctxt , inputStream ) ;
2008-03-14 13:54:00 +03:00
if ( pctxt - > directory = = NULL )
pctxt - > directory = xmlParserGetDirectory ( URL ) ;
2003-08-14 19:44:40 +04:00
2005-02-12 04:08:22 +03:00
pctxt - > loadsubset | = XML_DETECT_IDS ;
2003-08-14 19:44:40 +04:00
xmlParseDocument ( pctxt ) ;
2003-12-31 17:05:15 +03:00
if ( pctxt - > wellFormed ) {
2003-08-14 19:44:40 +04:00
ret = pctxt - > myDoc ;
2003-12-31 17:05:15 +03:00
}
2003-08-14 19:44:40 +04:00
else {
ret = NULL ;
2004-03-22 18:22:58 +03:00
if ( pctxt - > myDoc ! = NULL )
2004-01-23 16:15:13 +03:00
xmlFreeDoc ( pctxt - > myDoc ) ;
2003-08-14 19:44:40 +04:00
pctxt - > myDoc = NULL ;
}
xmlFreeParserCtxt ( pctxt ) ;
2012-09-11 09:26:36 +04:00
2003-08-14 19:44:40 +04:00
return ( ret ) ;
}
2001-02-23 20:55:21 +03:00
/**
* xmlXIncludeAddNode :
* @ ctxt : the XInclude context
2002-09-05 14:52:10 +04:00
* @ cur : the new node
2012-09-11 09:26:36 +04:00
*
2001-02-23 20:55:21 +03:00
* Add a new node to process to an XInclude context
*/
2022-10-23 14:57:33 +03:00
static xmlXIncludeRefPtr
2002-09-05 14:52:10 +04:00
xmlXIncludeAddNode ( xmlXIncludeCtxtPtr ctxt , xmlNodePtr cur ) {
xmlXIncludeRefPtr ref ;
xmlURIPtr uri ;
xmlChar * URL ;
xmlChar * fragment = NULL ;
xmlChar * href ;
xmlChar * parse ;
xmlChar * base ;
xmlChar * URI ;
2022-10-23 17:02:48 +03:00
int xml = 1 ;
2003-02-13 14:02:08 +03:00
int local = 0 ;
2002-09-05 14:52:10 +04:00
if ( ctxt = = NULL )
2022-10-23 14:57:33 +03:00
return ( NULL ) ;
2002-09-05 14:52:10 +04:00
if ( cur = = NULL )
2022-10-23 14:57:33 +03:00
return ( NULL ) ;
2002-09-05 14:52:10 +04:00
# ifdef DEBUG_XINCLUDE
xmlGenericError ( xmlGenericErrorContext , " Add node \n " ) ;
# endif
/*
* read the attributes
*/
2003-12-08 20:41:29 +03:00
href = xmlXIncludeGetProp ( ctxt , cur , XINCLUDE_HREF ) ;
2002-09-05 14:52:10 +04:00
if ( href = = NULL ) {
2003-12-08 20:41:29 +03:00
href = xmlStrdup ( BAD_CAST " " ) ; /* @@@@ href is now optional */
2012-09-11 09:26:36 +04:00
if ( href = = NULL )
2022-10-23 14:57:33 +03:00
return ( NULL ) ;
2001-02-23 20:55:21 +03:00
}
2003-12-08 20:41:29 +03:00
parse = xmlXIncludeGetProp ( ctxt , cur , XINCLUDE_PARSE ) ;
2002-09-05 14:52:10 +04:00
if ( parse ! = NULL ) {
if ( xmlStrEqual ( parse , XINCLUDE_PARSE_XML ) )
xml = 1 ;
else if ( xmlStrEqual ( parse , XINCLUDE_PARSE_TEXT ) )
xml = 0 ;
else {
2003-10-09 02:38:13 +04:00
xmlXIncludeErr ( ctxt , cur , XML_XINCLUDE_PARSE_VALUE ,
" invalid value %s for 'parse' \n " , parse ) ;
2002-09-05 14:52:10 +04:00
if ( href ! = NULL )
xmlFree ( href ) ;
if ( parse ! = NULL )
xmlFree ( parse ) ;
2022-10-23 14:57:33 +03:00
return ( NULL ) ;
2001-02-23 20:55:21 +03:00
}
}
2002-09-05 14:52:10 +04:00
/*
* compute the URI
*/
base = xmlNodeGetBase ( ctxt - > doc , cur ) ;
if ( base = = NULL ) {
URI = xmlBuildURI ( href , ctxt - > doc - > URL ) ;
} else {
URI = xmlBuildURI ( href , base ) ;
}
if ( URI = = NULL ) {
xmlChar * escbase ;
xmlChar * eschref ;
/*
* Some escaping may be needed
*/
escbase = xmlURIEscape ( base ) ;
eschref = xmlURIEscape ( href ) ;
URI = xmlBuildURI ( eschref , escbase ) ;
if ( escbase ! = NULL )
xmlFree ( escbase ) ;
if ( eschref ! = NULL )
xmlFree ( eschref ) ;
}
if ( parse ! = NULL )
xmlFree ( parse ) ;
if ( href ! = NULL )
xmlFree ( href ) ;
if ( base ! = NULL )
xmlFree ( base ) ;
if ( URI = = NULL ) {
2003-10-09 02:38:13 +04:00
xmlXIncludeErr ( ctxt , cur , XML_XINCLUDE_HREF_URI ,
" failed build URL \n " , NULL ) ;
2022-10-23 14:57:33 +03:00
return ( NULL ) ;
2002-09-05 14:52:10 +04:00
}
2003-12-09 14:35:37 +03:00
fragment = xmlXIncludeGetProp ( ctxt , cur , XINCLUDE_PARSE_XPOINTER ) ;
2002-09-05 14:52:10 +04:00
/*
* Check the URL and remove any fragment identifier
*/
uri = xmlParseURI ( ( const char * ) URI ) ;
if ( uri = = NULL ) {
2003-10-09 02:38:13 +04:00
xmlXIncludeErr ( ctxt , cur , XML_XINCLUDE_HREF_URI ,
" invalid value URI %s \n " , URI ) ;
2003-12-09 14:35:37 +03:00
if ( fragment ! = NULL )
xmlFree ( fragment ) ;
xmlFree ( URI ) ;
2022-10-23 14:57:33 +03:00
return ( NULL ) ;
2002-09-05 14:52:10 +04:00
}
2003-12-09 14:35:37 +03:00
2002-09-05 14:52:10 +04:00
if ( uri - > fragment ! = NULL ) {
2003-12-09 14:35:37 +03:00
if ( ctxt - > legacy ! = 0 ) {
if ( fragment = = NULL ) {
fragment = ( xmlChar * ) uri - > fragment ;
} else {
xmlFree ( uri - > fragment ) ;
}
} else {
xmlXIncludeErr ( ctxt , cur , XML_XINCLUDE_FRAGMENT_ID ,
" Invalid fragment identifier in URI %s use the xpointer attribute \n " ,
URI ) ;
if ( fragment ! = NULL )
xmlFree ( fragment ) ;
xmlFreeURI ( uri ) ;
xmlFree ( URI ) ;
2022-10-23 14:57:33 +03:00
return ( NULL ) ;
2003-12-09 14:35:37 +03:00
}
2002-09-05 14:52:10 +04:00
uri - > fragment = NULL ;
}
URL = xmlSaveUri ( uri ) ;
xmlFreeURI ( uri ) ;
if ( URL = = NULL ) {
2003-10-09 02:38:13 +04:00
xmlXIncludeErr ( ctxt , cur , XML_XINCLUDE_HREF_URI ,
" invalid value URI %s \n " , URI ) ;
2002-09-05 14:52:10 +04:00
if ( fragment ! = NULL )
xmlFree ( fragment ) ;
2022-11-02 18:13:27 +03:00
xmlFree ( URI ) ;
2022-10-23 14:57:33 +03:00
return ( NULL ) ;
2002-09-05 14:52:10 +04:00
}
2022-11-02 18:13:27 +03:00
xmlFree ( URI ) ;
2002-09-05 14:52:10 +04:00
2022-03-02 04:57:49 +03:00
if ( xmlStrEqual ( URL , ctxt - > doc - > URL ) )
local = 1 ;
2008-02-08 12:56:31 +03:00
/*
* If local and xml then we need a fragment
*/
if ( ( local = = 1 ) & & ( xml = = 1 ) & &
( ( fragment = = NULL ) | | ( fragment [ 0 ] = = 0 ) ) ) {
xmlXIncludeErr ( ctxt , cur , XML_XINCLUDE_RECURSION ,
" detected a local recursion with no xpointer in %s \n " ,
URL ) ;
2020-08-19 14:13:20 +03:00
xmlFree ( URL ) ;
xmlFree ( fragment ) ;
2022-10-23 14:57:33 +03:00
return ( NULL ) ;
2008-02-08 12:56:31 +03:00
}
2002-09-05 14:52:10 +04:00
ref = xmlXIncludeNewRef ( ctxt , URL , cur ) ;
2020-08-19 14:13:20 +03:00
xmlFree ( URL ) ;
2002-09-05 14:52:10 +04:00
if ( ref = = NULL ) {
2022-10-23 14:57:33 +03:00
return ( NULL ) ;
2002-09-05 14:52:10 +04:00
}
ref - > fragment = fragment ;
ref - > xml = xml ;
2022-10-23 14:57:33 +03:00
return ( ref ) ;
2001-02-23 20:55:21 +03:00
}
/**
2002-09-05 14:52:10 +04:00
* xmlXIncludeRecurseDoc :
2001-02-23 20:55:21 +03:00
* @ ctxt : the XInclude context
* @ doc : the new document
* @ url : the associated URL
2012-09-11 09:26:36 +04:00
*
2002-09-05 14:52:10 +04:00
* The XInclude recursive nature is handled at this point .
2001-02-23 20:55:21 +03:00
*/
2001-03-24 20:00:36 +03:00
static void
2002-09-24 18:13:13 +04:00
xmlXIncludeRecurseDoc ( xmlXIncludeCtxtPtr ctxt , xmlDocPtr doc ,
2002-09-26 13:47:36 +04:00
const xmlURL url ATTRIBUTE_UNUSED ) {
2022-10-23 18:52:29 +03:00
xmlDocPtr oldDoc ;
xmlXIncludeRefPtr * oldIncTab ;
2022-10-30 22:28:20 +03:00
int oldIncMax , oldIncNr , oldIsStream ;
2001-05-23 17:44:21 +04:00
int i ;
2022-10-23 18:52:29 +03:00
oldDoc = ctxt - > doc ;
oldIncMax = ctxt - > incMax ;
oldIncNr = ctxt - > incNr ;
oldIncTab = ctxt - > incTab ;
2022-10-30 22:28:20 +03:00
oldIsStream = ctxt - > isStream ;
2022-10-23 18:52:29 +03:00
ctxt - > doc = doc ;
ctxt - > incMax = 0 ;
ctxt - > incNr = 0 ;
ctxt - > incTab = NULL ;
2022-10-30 22:28:20 +03:00
ctxt - > isStream = 0 ;
2004-06-07 12:57:27 +04:00
2022-10-23 18:52:29 +03:00
xmlXIncludeDoProcess ( ctxt , xmlDocGetRootElement ( doc ) ) ;
2003-03-27 17:24:00 +03:00
2022-10-23 18:52:29 +03:00
if ( ctxt - > incTab ! = NULL ) {
for ( i = 0 ; i < ctxt - > incNr ; i + + )
xmlXIncludeFreeRef ( ctxt - > incTab [ i ] ) ;
xmlFree ( ctxt - > incTab ) ;
2001-05-23 17:44:21 +04:00
}
2022-10-23 18:52:29 +03:00
ctxt - > doc = oldDoc ;
ctxt - > incMax = oldIncMax ;
ctxt - > incNr = oldIncNr ;
ctxt - > incTab = oldIncTab ;
2022-10-30 22:28:20 +03:00
ctxt - > isStream = oldIsStream ;
2001-02-23 20:55:21 +03:00
}
2002-02-10 14:57:22 +03:00
/************************************************************************
* *
* Node copy with specific semantic *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* xmlXIncludeCopyNode :
* @ ctxt : the XInclude context
* @ elem : the element
2022-10-23 18:52:29 +03:00
* @ copyChildren : copy children instead of node if true
2012-09-11 09:26:36 +04:00
*
2022-10-22 20:08:43 +03:00
* Make a copy of the node while expanding nested XIncludes .
2022-10-22 03:17:39 +03:00
*
* Returns a node list , not a single node .
2002-02-10 14:57:22 +03:00
*/
static xmlNodePtr
2022-10-23 18:52:29 +03:00
xmlXIncludeCopyNode ( xmlXIncludeCtxtPtr ctxt , xmlNodePtr elem ,
int copyChildren ) {
2002-02-10 14:57:22 +03:00
xmlNodePtr result = NULL ;
2022-10-22 20:08:43 +03:00
xmlNodePtr insertParent = NULL ;
xmlNodePtr insertLast = NULL ;
2022-10-23 18:52:29 +03:00
xmlNodePtr cur ;
if ( copyChildren ) {
cur = elem - > children ;
if ( cur = = NULL )
return ( NULL ) ;
} else {
cur = elem ;
}
2022-10-22 20:08:43 +03:00
while ( 1 ) {
xmlNodePtr copy = NULL ;
int recurse = 0 ;
if ( ( cur - > type = = XML_DOCUMENT_NODE ) | |
( cur - > type = = XML_DTD_NODE ) ) {
;
} else if ( ( cur - > type = = XML_ELEMENT_NODE ) & &
( cur - > ns ! = NULL ) & &
( xmlStrEqual ( cur - > name , XINCLUDE_NODE ) ) & &
( ( xmlStrEqual ( cur - > ns - > href , XINCLUDE_NS ) ) | |
( xmlStrEqual ( cur - > ns - > href , XINCLUDE_OLD_NS ) ) ) ) {
xmlXIncludeRefPtr ref = xmlXIncludeExpandNode ( ctxt , cur ) ;
if ( ref = = NULL )
goto error ;
/*
* TODO : Insert XML_XINCLUDE_START and XML_XINCLUDE_END nodes
*/
if ( ref - > inc ! = NULL ) {
copy = xmlStaticCopyNodeList ( ref - > inc , ctxt - > doc ,
insertParent ) ;
if ( copy = = NULL )
goto error ;
}
} else {
copy = xmlStaticCopyNode ( cur , ctxt - > doc , insertParent , 2 ) ;
if ( copy = = NULL )
goto error ;
recurse = ( cur - > type ! = XML_ENTITY_REF_NODE ) & &
( cur - > children ! = NULL ) ;
}
2002-02-10 14:57:22 +03:00
2022-10-22 20:08:43 +03:00
if ( copy ! = NULL ) {
if ( result = = NULL )
result = copy ;
if ( insertLast ! = NULL ) {
insertLast - > next = copy ;
copy - > prev = insertLast ;
} else if ( insertParent ! = NULL ) {
insertParent - > children = copy ;
}
insertLast = copy ;
while ( insertLast - > next ! = NULL ) {
insertLast = insertLast - > next ;
}
}
2002-02-10 14:57:22 +03:00
2022-10-22 20:08:43 +03:00
if ( recurse ) {
cur = cur - > children ;
insertParent = insertLast ;
insertLast = NULL ;
continue ;
}
2002-02-10 14:57:22 +03:00
2022-10-23 18:52:29 +03:00
if ( cur = = elem )
return ( result ) ;
while ( cur - > next = = NULL ) {
2022-10-22 20:08:43 +03:00
cur = cur - > parent ;
2022-10-23 18:52:29 +03:00
if ( cur = = elem )
return ( result ) ;
2022-10-22 20:08:43 +03:00
insertParent - > last = insertLast ;
insertLast = insertParent ;
insertParent = insertParent - > parent ;
}
cur = cur - > next ;
2002-02-10 14:57:22 +03:00
}
2022-10-22 20:08:43 +03:00
error :
xmlFreeNodeList ( result ) ;
return ( NULL ) ;
2002-02-10 14:57:22 +03:00
}
2022-04-21 00:17:14 +03:00
# ifdef LIBXML_XPTR_LOCS_ENABLED
2002-02-10 14:57:22 +03:00
/**
2003-12-30 11:30:19 +03:00
* xmlXIncludeGetNthChild :
2002-02-10 14:57:22 +03:00
* @ cur : the node
* @ no : the child number
*
2003-12-30 11:30:19 +03:00
* Returns the @ n ' th element child of @ cur or NULL
2002-02-10 14:57:22 +03:00
*/
static xmlNodePtr
xmlXIncludeGetNthChild ( xmlNodePtr cur , int no ) {
int i ;
2012-08-09 10:24:02 +04:00
if ( ( cur = = NULL ) | | ( cur - > type = = XML_NAMESPACE_DECL ) )
return ( NULL ) ;
2002-02-10 14:57:22 +03:00
cur = cur - > children ;
for ( i = 0 ; i < = no ; cur = cur - > next ) {
2012-09-11 09:26:36 +04:00
if ( cur = = NULL )
2002-02-10 14:57:22 +03:00
return ( cur ) ;
if ( ( cur - > type = = XML_ELEMENT_NODE ) | |
( cur - > type = = XML_DOCUMENT_NODE ) | |
( cur - > type = = XML_HTML_DOCUMENT_NODE ) ) {
i + + ;
if ( i = = no )
break ;
}
}
return ( cur ) ;
}
2003-12-31 10:59:17 +03:00
xmlNodePtr xmlXPtrAdvanceNode ( xmlNodePtr cur , int * level ) ; /* in xpointer.c */
2002-02-10 14:57:22 +03:00
/**
* xmlXIncludeCopyRange :
* @ ctxt : the XInclude context
* @ obj : the XPointer result from the evaluation .
*
* Build a node list tree copy of the XPointer result .
*
* Returns an xmlNodePtr list or NULL .
2003-12-30 11:30:19 +03:00
* The caller has to free the node tree .
2002-02-10 14:57:22 +03:00
*/
static xmlNodePtr
2022-10-22 20:20:20 +03:00
xmlXIncludeCopyRange ( xmlXIncludeCtxtPtr ctxt , xmlXPathObjectPtr range ) {
2002-02-10 14:57:22 +03:00
/* pointers to generated nodes */
2003-12-31 10:59:17 +03:00
xmlNodePtr list = NULL , last = NULL , listParent = NULL ;
xmlNodePtr tmp , tmp2 ;
2002-02-10 14:57:22 +03:00
/* pointers to traversal nodes */
xmlNodePtr start , cur , end ;
int index1 , index2 ;
2004-02-07 11:53:23 +03:00
int level = 0 , lastLevel = 0 , endLevel = 0 , endFlag = 0 ;
2002-02-10 14:57:22 +03:00
2022-10-22 20:20:20 +03:00
if ( ( ctxt = = NULL ) | | ( range = = NULL ) )
2002-02-10 14:57:22 +03:00
return ( NULL ) ;
if ( range - > type ! = XPATH_RANGE )
return ( NULL ) ;
start = ( xmlNodePtr ) range - > user ;
2012-08-09 10:24:02 +04:00
if ( ( start = = NULL ) | | ( start - > type = = XML_NAMESPACE_DECL ) )
2002-02-10 14:57:22 +03:00
return ( NULL ) ;
end = range - > user2 ;
if ( end = = NULL )
2022-10-22 20:20:20 +03:00
return ( xmlDocCopyNode ( start , ctxt - > doc , 1 ) ) ;
2012-08-09 10:24:02 +04:00
if ( end - > type = = XML_NAMESPACE_DECL )
return ( NULL ) ;
2002-02-10 14:57:22 +03:00
cur = start ;
index1 = range - > index ;
index2 = range - > index2 ;
2003-12-31 10:59:17 +03:00
/*
* level is depth of the current node under consideration
* list is the pointer to the root of the output tree
* listParent is a pointer to the parent of output tree ( within
the included file ) in case we need to add another level
* last is a pointer to the last node added to the output tree
* lastLevel is the depth of last ( relative to the root )
*/
2002-02-10 14:57:22 +03:00
while ( cur ! = NULL ) {
2003-12-31 10:59:17 +03:00
/*
* Check if our output tree needs a parent
*/
if ( level < 0 ) {
while ( level < 0 ) {
2004-03-09 19:19:02 +03:00
/* copy must include namespaces and properties */
2022-10-22 20:20:20 +03:00
tmp2 = xmlDocCopyNode ( listParent , ctxt - > doc , 2 ) ;
2003-12-31 10:59:17 +03:00
xmlAddChild ( tmp2 , list ) ;
list = tmp2 ;
listParent = listParent - > parent ;
level + + ;
}
last = list ;
lastLevel = 0 ;
}
/*
* Check whether we need to change our insertion point
*/
while ( level < lastLevel ) {
last = last - > parent ;
lastLevel - - ;
}
2003-12-30 11:30:19 +03:00
if ( cur = = end ) { /* Are we at the end of the range? */
2002-02-10 14:57:22 +03:00
if ( cur - > type = = XML_TEXT_NODE ) {
const xmlChar * content = cur - > content ;
int len ;
if ( content = = NULL ) {
2022-10-22 20:20:20 +03:00
tmp = xmlNewDocTextLen ( ctxt - > doc , NULL , 0 ) ;
2002-02-10 14:57:22 +03:00
} else {
len = index2 ;
if ( ( cur = = start ) & & ( index1 > 1 ) ) {
content + = ( index1 - 1 ) ;
len - = ( index1 - 1 ) ;
} else {
len = index2 ;
}
2022-10-22 20:20:20 +03:00
tmp = xmlNewDocTextLen ( ctxt - > doc , content , len ) ;
2002-02-10 14:57:22 +03:00
}
/* single sub text node selection */
if ( list = = NULL )
return ( tmp ) ;
/* prune and return full set */
2003-12-31 10:59:17 +03:00
if ( level = = lastLevel )
2002-02-10 14:57:22 +03:00
xmlAddNextSibling ( last , tmp ) ;
2012-09-11 09:26:36 +04:00
else
2003-12-31 10:59:17 +03:00
xmlAddChild ( last , tmp ) ;
2002-02-10 14:57:22 +03:00
return ( list ) ;
2003-12-30 11:30:19 +03:00
} else { /* ending node not a text node */
2004-02-07 11:53:23 +03:00
endLevel = level ; /* remember the level of the end node */
endFlag = 1 ;
2004-03-09 19:19:02 +03:00
/* last node - need to take care of properties + namespaces */
2022-10-22 20:20:20 +03:00
tmp = xmlDocCopyNode ( cur , ctxt - > doc , 2 ) ;
2003-12-31 10:59:17 +03:00
if ( list = = NULL ) {
2002-02-10 14:57:22 +03:00
list = tmp ;
2003-12-31 10:59:17 +03:00
listParent = cur - > parent ;
2021-07-08 05:24:36 +03:00
last = tmp ;
2003-12-31 10:59:17 +03:00
} else {
if ( level = = lastLevel )
2021-07-08 05:24:36 +03:00
last = xmlAddNextSibling ( last , tmp ) ;
2003-12-31 10:59:17 +03:00
else {
2021-07-08 05:24:36 +03:00
last = xmlAddChild ( last , tmp ) ;
2003-12-31 10:59:17 +03:00
lastLevel = level ;
}
2002-02-10 14:57:22 +03:00
}
if ( index2 > 1 ) {
end = xmlXIncludeGetNthChild ( cur , index2 - 1 ) ;
index2 = 0 ;
}
if ( ( cur = = start ) & & ( index1 > 1 ) ) {
cur = xmlXIncludeGetNthChild ( cur , index1 - 1 ) ;
index1 = 0 ;
2004-02-07 11:53:23 +03:00
} else {
2002-02-10 14:57:22 +03:00
cur = cur - > children ;
}
2004-02-07 11:53:23 +03:00
level + + ; /* increment level to show change */
2002-02-10 14:57:22 +03:00
/*
* Now gather the remaining nodes from cur to end
*/
2004-02-07 11:53:23 +03:00
continue ; /* while */
2002-02-10 14:57:22 +03:00
}
2003-12-31 10:59:17 +03:00
} else if ( cur = = start ) { /* Not at the end, are we at start? */
2002-02-10 14:57:22 +03:00
if ( ( cur - > type = = XML_TEXT_NODE ) | |
( cur - > type = = XML_CDATA_SECTION_NODE ) ) {
const xmlChar * content = cur - > content ;
if ( content = = NULL ) {
2022-10-22 20:20:20 +03:00
tmp = xmlNewDocTextLen ( ctxt - > doc , NULL , 0 ) ;
2002-02-10 14:57:22 +03:00
} else {
if ( index1 > 1 ) {
content + = ( index1 - 1 ) ;
2003-12-30 11:30:19 +03:00
index1 = 0 ;
2002-02-10 14:57:22 +03:00
}
2022-10-22 20:20:20 +03:00
tmp = xmlNewDocText ( ctxt - > doc , content ) ;
2002-02-10 14:57:22 +03:00
}
last = list = tmp ;
2003-12-31 10:59:17 +03:00
listParent = cur - > parent ;
2003-12-30 11:30:19 +03:00
} else { /* Not text node */
2004-03-09 19:19:02 +03:00
/*
* start of the range - need to take care of
* properties and namespaces
*/
2022-10-22 20:20:20 +03:00
tmp = xmlDocCopyNode ( cur , ctxt - > doc , 2 ) ;
2003-12-31 10:59:17 +03:00
list = last = tmp ;
listParent = cur - > parent ;
2003-12-30 11:30:19 +03:00
if ( index1 > 1 ) { /* Do we need to position? */
2002-02-10 14:57:22 +03:00
cur = xmlXIncludeGetNthChild ( cur , index1 - 1 ) ;
2003-12-31 10:59:17 +03:00
level = lastLevel = 1 ;
2002-02-10 14:57:22 +03:00
index1 = 0 ;
/*
* Now gather the remaining nodes from cur to end
*/
continue ; /* while */
}
}
} else {
tmp = NULL ;
switch ( cur - > type ) {
case XML_DTD_NODE :
case XML_ELEMENT_DECL :
case XML_ATTRIBUTE_DECL :
case XML_ENTITY_NODE :
2020-03-08 19:19:42 +03:00
/* Do not copy DTD information */
2002-02-10 14:57:22 +03:00
break ;
case XML_ENTITY_DECL :
/* handle crossing entities -> stack needed */
break ;
case XML_XINCLUDE_START :
case XML_XINCLUDE_END :
/* don't consider it part of the tree content */
break ;
case XML_ATTRIBUTE_NODE :
/* Humm, should not happen ! */
break ;
default :
2004-03-09 19:19:02 +03:00
/*
* Middle of the range - need to take care of
* properties and namespaces
*/
2022-10-22 20:20:20 +03:00
tmp = xmlDocCopyNode ( cur , ctxt - > doc , 2 ) ;
2002-02-10 14:57:22 +03:00
break ;
}
if ( tmp ! = NULL ) {
2003-12-31 10:59:17 +03:00
if ( level = = lastLevel )
2021-07-08 05:24:36 +03:00
last = xmlAddNextSibling ( last , tmp ) ;
2002-02-10 14:57:22 +03:00
else {
2021-07-08 05:24:36 +03:00
last = xmlAddChild ( last , tmp ) ;
2003-12-31 10:59:17 +03:00
lastLevel = level ;
2002-02-10 14:57:22 +03:00
}
}
}
/*
* Skip to next node in document order
*/
2003-12-31 10:59:17 +03:00
cur = xmlXPtrAdvanceNode ( cur , & level ) ;
2004-02-07 11:53:23 +03:00
if ( endFlag & & ( level > = endLevel ) )
break ;
2002-02-10 14:57:22 +03:00
}
return ( list ) ;
}
2022-04-21 00:17:14 +03:00
# endif /* LIBXML_XPTR_LOCS_ENABLED */
2002-02-10 14:57:22 +03:00
/**
2022-10-22 20:20:20 +03:00
* xmlXIncludeCopyXPointer :
2002-02-10 14:57:22 +03:00
* @ ctxt : the XInclude context
* @ obj : the XPointer result from the evaluation .
*
* Build a node list tree copy of the XPointer result .
* This will drop Attributes and Namespace declarations .
*
* Returns an xmlNodePtr list or NULL .
* the caller has to free the node tree .
*/
static xmlNodePtr
2022-10-22 20:20:20 +03:00
xmlXIncludeCopyXPointer ( xmlXIncludeCtxtPtr ctxt , xmlXPathObjectPtr obj ) {
2022-10-22 03:17:39 +03:00
xmlNodePtr list = NULL , last = NULL , copy ;
2002-02-10 14:57:22 +03:00
int i ;
2022-10-22 20:20:20 +03:00
if ( ( ctxt = = NULL ) | | ( obj = = NULL ) )
2002-02-10 14:57:22 +03:00
return ( NULL ) ;
switch ( obj - > type ) {
case XPATH_NODESET : {
xmlNodeSetPtr set = obj - > nodesetval ;
if ( set = = NULL )
return ( NULL ) ;
for ( i = 0 ; i < set - > nodeNr ; i + + ) {
2022-10-22 18:48:25 +03:00
xmlNodePtr node ;
2002-02-10 14:57:22 +03:00
if ( set - > nodeTab [ i ] = = NULL )
continue ;
switch ( set - > nodeTab [ i ] - > type ) {
2022-10-22 18:48:25 +03:00
case XML_DOCUMENT_NODE :
case XML_HTML_DOCUMENT_NODE :
node = xmlDocGetRootElement (
( xmlDocPtr ) set - > nodeTab [ i ] ) ;
if ( node = = NULL ) {
xmlXIncludeErr ( ctxt , set - > nodeTab [ i ] ,
XML_ERR_INTERNAL_ERROR ,
" document without root \n " , NULL ) ;
continue ;
}
break ;
2002-02-10 14:57:22 +03:00
case XML_TEXT_NODE :
case XML_CDATA_SECTION_NODE :
case XML_ELEMENT_NODE :
case XML_PI_NODE :
case XML_COMMENT_NODE :
2022-10-22 18:48:25 +03:00
node = set - > nodeTab [ i ] ;
2002-02-10 14:57:22 +03:00
break ;
2022-10-22 18:48:25 +03:00
default :
xmlXIncludeErr ( ctxt , set - > nodeTab [ i ] ,
XML_XINCLUDE_XPTR_RESULT ,
" invalid node type in XPtr result \n " ,
NULL ) ;
2002-02-10 14:57:22 +03:00
continue ; /* for */
}
2022-10-22 03:17:39 +03:00
/*
* OPTIMIZE TODO : External documents should already be
* expanded , so xmlDocCopyNode should work as well .
* xmlXIncludeCopyNode is only required for the initial
* document .
*/
2022-10-23 18:52:29 +03:00
copy = xmlXIncludeCopyNode ( ctxt , node , 0 ) ;
2022-10-22 03:17:39 +03:00
if ( copy = = NULL ) {
xmlFreeNodeList ( list ) ;
return ( NULL ) ;
}
if ( last = = NULL ) {
list = copy ;
} else {
while ( last - > next ! = NULL )
last = last - > next ;
copy - > prev = last ;
last - > next = copy ;
2002-02-10 14:57:22 +03:00
}
2022-10-22 03:17:39 +03:00
last = copy ;
2002-02-10 14:57:22 +03:00
}
break ;
}
2022-04-21 00:17:14 +03:00
# ifdef LIBXML_XPTR_LOCS_ENABLED
2002-02-10 14:57:22 +03:00
case XPATH_LOCATIONSET : {
xmlLocationSetPtr set = ( xmlLocationSetPtr ) obj - > user ;
if ( set = = NULL )
return ( NULL ) ;
for ( i = 0 ; i < set - > locNr ; i + + ) {
if ( last = = NULL )
2022-10-22 20:20:20 +03:00
list = last = xmlXIncludeCopyXPointer ( ctxt ,
2002-02-10 14:57:22 +03:00
set - > locTab [ i ] ) ;
else
xmlAddNextSibling ( last ,
2022-10-22 20:20:20 +03:00
xmlXIncludeCopyXPointer ( ctxt , set - > locTab [ i ] ) ) ;
2002-02-10 14:57:22 +03:00
if ( last ! = NULL ) {
while ( last - > next ! = NULL )
last = last - > next ;
}
}
break ;
}
case XPATH_RANGE :
2022-10-22 20:20:20 +03:00
return ( xmlXIncludeCopyRange ( ctxt , obj ) ) ;
2002-02-10 14:57:22 +03:00
case XPATH_POINT :
/* points are ignored in XInclude */
break ;
2022-04-21 00:17:14 +03:00
# endif
2002-02-10 14:57:22 +03:00
default :
break ;
}
return ( list ) ;
}
2001-02-23 20:55:21 +03:00
/************************************************************************
* *
* XInclude I / O handling *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-02-13 14:02:08 +03:00
typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData ;
typedef xmlXIncludeMergeData * xmlXIncludeMergeDataPtr ;
struct _xmlXIncludeMergeData {
xmlDocPtr doc ;
xmlXIncludeCtxtPtr ctxt ;
} ;
2003-02-05 01:48:53 +03:00
/**
* xmlXIncludeMergeOneEntity :
* @ ent : the entity
* @ doc : the including doc
2022-10-23 14:57:33 +03:00
* @ name : the entity name
2003-02-05 01:48:53 +03:00
*
2019-09-30 18:04:54 +03:00
* Implements the merge of one entity
2003-02-05 01:48:53 +03:00
*/
static void
2017-11-09 18:42:47 +03:00
xmlXIncludeMergeEntity ( void * payload , void * vdata ,
const xmlChar * name ATTRIBUTE_UNUSED ) {
xmlEntityPtr ent = ( xmlEntityPtr ) payload ;
xmlXIncludeMergeDataPtr data = ( xmlXIncludeMergeDataPtr ) vdata ;
2003-02-13 14:02:08 +03:00
xmlEntityPtr ret , prev ;
xmlDocPtr doc ;
xmlXIncludeCtxtPtr ctxt ;
2003-02-05 01:48:53 +03:00
2003-02-13 14:02:08 +03:00
if ( ( ent = = NULL ) | | ( data = = NULL ) )
return ;
ctxt = data - > ctxt ;
doc = data - > doc ;
if ( ( ctxt = = NULL ) | | ( doc = = NULL ) )
2003-02-05 01:48:53 +03:00
return ;
2003-02-13 14:02:08 +03:00
switch ( ent - > etype ) {
case XML_INTERNAL_PARAMETER_ENTITY :
case XML_EXTERNAL_PARAMETER_ENTITY :
case XML_INTERNAL_PREDEFINED_ENTITY :
return ;
case XML_INTERNAL_GENERAL_ENTITY :
case XML_EXTERNAL_GENERAL_PARSED_ENTITY :
case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY :
break ;
}
2003-02-05 01:48:53 +03:00
ret = xmlAddDocEntity ( doc , ent - > name , ent - > etype , ent - > ExternalID ,
ent - > SystemID , ent - > content ) ;
if ( ret ! = NULL ) {
if ( ent - > URI ! = NULL )
ret - > URI = xmlStrdup ( ent - > URI ) ;
2003-02-13 14:02:08 +03:00
} else {
prev = xmlGetDocEntity ( doc , ent - > name ) ;
if ( prev ! = NULL ) {
if ( ent - > etype ! = prev - > etype )
goto error ;
2012-09-11 09:26:36 +04:00
2003-02-13 14:02:08 +03:00
if ( ( ent - > SystemID ! = NULL ) & & ( prev - > SystemID ! = NULL ) ) {
if ( ! xmlStrEqual ( ent - > SystemID , prev - > SystemID ) )
goto error ;
2003-03-29 19:41:55 +03:00
} else if ( ( ent - > ExternalID ! = NULL ) & &
( prev - > ExternalID ! = NULL ) ) {
2003-02-13 14:02:08 +03:00
if ( ! xmlStrEqual ( ent - > ExternalID , prev - > ExternalID ) )
goto error ;
2003-02-24 21:16:47 +03:00
} else if ( ( ent - > content ! = NULL ) & & ( prev - > content ! = NULL ) ) {
if ( ! xmlStrEqual ( ent - > content , prev - > content ) )
goto error ;
2003-02-13 14:02:08 +03:00
} else {
goto error ;
}
}
2003-02-05 01:48:53 +03:00
}
2003-02-13 14:02:08 +03:00
return ;
error :
2003-03-31 20:09:37 +04:00
switch ( ent - > etype ) {
case XML_INTERNAL_PARAMETER_ENTITY :
case XML_EXTERNAL_PARAMETER_ENTITY :
case XML_INTERNAL_PREDEFINED_ENTITY :
case XML_INTERNAL_GENERAL_ENTITY :
case XML_EXTERNAL_GENERAL_PARSED_ENTITY :
return ;
case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY :
break ;
}
2003-10-09 02:38:13 +04:00
xmlXIncludeErr ( ctxt , ( xmlNodePtr ) ent , XML_XINCLUDE_ENTITY_DEF_MISMATCH ,
" mismatch in redefinition of entity %s \n " ,
ent - > name ) ;
2003-02-05 01:48:53 +03:00
}
/**
* xmlXIncludeMergeEntities :
* @ ctxt : an XInclude context
* @ doc : the including doc
* @ from : the included doc
*
2019-09-30 18:04:54 +03:00
* Implements the entity merge
2003-02-05 01:48:53 +03:00
*
* Returns 0 if merge succeeded , - 1 if some processing failed
*/
static int
xmlXIncludeMergeEntities ( xmlXIncludeCtxtPtr ctxt , xmlDocPtr doc ,
xmlDocPtr from ) {
xmlNodePtr cur ;
xmlDtdPtr target , source ;
if ( ctxt = = NULL )
return ( - 1 ) ;
if ( ( from = = NULL ) | | ( from - > intSubset = = NULL ) )
return ( 0 ) ;
target = doc - > intSubset ;
if ( target = = NULL ) {
cur = xmlDocGetRootElement ( doc ) ;
if ( cur = = NULL )
return ( - 1 ) ;
target = xmlCreateIntSubset ( doc , cur - > name , NULL , NULL ) ;
if ( target = = NULL )
return ( - 1 ) ;
}
source = from - > intSubset ;
if ( ( source ! = NULL ) & & ( source - > entities ! = NULL ) ) {
2003-02-13 14:02:08 +03:00
xmlXIncludeMergeData data ;
data . ctxt = ctxt ;
data . doc = doc ;
2003-02-05 01:48:53 +03:00
xmlHashScan ( ( xmlHashTablePtr ) source - > entities ,
2017-11-09 18:42:47 +03:00
xmlXIncludeMergeEntity , & data ) ;
2003-02-05 01:48:53 +03:00
}
source = from - > extSubset ;
if ( ( source ! = NULL ) & & ( source - > entities ! = NULL ) ) {
2003-02-13 14:02:08 +03:00
xmlXIncludeMergeData data ;
data . ctxt = ctxt ;
data . doc = doc ;
2003-02-05 01:48:53 +03:00
/*
* don ' t duplicate existing stuff when external subsets are the same
*/
if ( ( ! xmlStrEqual ( target - > ExternalID , source - > ExternalID ) ) & &
( ! xmlStrEqual ( target - > SystemID , source - > SystemID ) ) ) {
xmlHashScan ( ( xmlHashTablePtr ) source - > entities ,
2017-11-09 18:42:47 +03:00
xmlXIncludeMergeEntity , & data ) ;
2003-02-05 01:48:53 +03:00
}
}
return ( 0 ) ;
}
2001-02-23 20:55:21 +03:00
/**
* xmlXIncludeLoadDoc :
* @ ctxt : the XInclude context
* @ url : the associated URL
2022-10-23 14:57:33 +03:00
* @ ref : an XMLXincludeRefPtr
2012-09-11 09:26:36 +04:00
*
2001-02-23 20:55:21 +03:00
* Load the document , and store the result in the XInclude context
2002-08-14 18:11:30 +04:00
*
* Returns 0 in case of success , - 1 in case of failure
2001-02-23 20:55:21 +03:00
*/
2002-08-14 18:11:30 +04:00
static int
2022-10-23 14:57:33 +03:00
xmlXIncludeLoadDoc ( xmlXIncludeCtxtPtr ctxt , const xmlChar * url ,
xmlXIncludeRefPtr ref ) {
2022-10-23 17:02:48 +03:00
xmlXIncludeDocPtr cache ;
2001-02-23 20:55:21 +03:00
xmlDocPtr doc ;
xmlURIPtr uri ;
2022-10-30 14:32:14 +03:00
xmlChar * URL = NULL ;
2001-02-23 20:55:21 +03:00
xmlChar * fragment = NULL ;
2002-09-05 14:52:10 +04:00
int i = 0 ;
2022-10-30 14:32:14 +03:00
int ret = - 1 ;
2004-03-08 17:42:31 +03:00
# ifdef LIBXML_XPTR_ENABLED
int saveFlags ;
# endif
2002-09-05 14:52:10 +04:00
# ifdef DEBUG_XINCLUDE
2022-10-23 14:57:33 +03:00
xmlGenericError ( xmlGenericErrorContext , " Loading doc %s \n " , url ) ;
2002-09-05 14:52:10 +04:00
# endif
2001-02-23 20:55:21 +03:00
/*
* Check the URL and remove any fragment identifier
*/
uri = xmlParseURI ( ( const char * ) url ) ;
if ( uri = = NULL ) {
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , ref - > elem , XML_XINCLUDE_HREF_URI ,
2003-10-09 02:38:13 +04:00
" invalid value URI %s \n " , url ) ;
2022-10-30 14:32:14 +03:00
goto error ;
2001-02-23 20:55:21 +03:00
}
if ( uri - > fragment ! = NULL ) {
fragment = ( xmlChar * ) uri - > fragment ;
uri - > fragment = NULL ;
}
2022-10-23 14:57:33 +03:00
if ( ref - > fragment ! = NULL ) {
2003-12-24 14:06:25 +03:00
if ( fragment ! = NULL ) xmlFree ( fragment ) ;
2022-10-23 14:57:33 +03:00
fragment = xmlStrdup ( ref - > fragment ) ;
2003-12-24 14:06:25 +03:00
}
2001-02-23 20:55:21 +03:00
URL = xmlSaveUri ( uri ) ;
xmlFreeURI ( uri ) ;
if ( URL = = NULL ) {
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , ref - > elem , XML_XINCLUDE_HREF_URI ,
" invalid value URI %s \n " , url ) ;
2022-10-30 14:32:14 +03:00
goto error ;
2001-02-23 20:55:21 +03:00
}
/*
* Handling of references to the local document are done
* directly through ctxt - > doc .
*/
2003-02-13 14:02:08 +03:00
if ( ( URL [ 0 ] = = 0 ) | | ( URL [ 0 ] = = ' # ' ) | |
( ( ctxt - > doc ! = NULL ) & & ( xmlStrEqual ( URL , ctxt - > doc - > URL ) ) ) ) {
2022-10-21 16:56:12 +03:00
doc = ctxt - > doc ;
2001-02-23 20:55:21 +03:00
goto loaded ;
}
/*
2022-10-23 17:02:48 +03:00
* Prevent reloading the document twice .
2001-02-23 20:55:21 +03:00
*/
2022-10-23 18:52:29 +03:00
for ( i = 0 ; i < ctxt - > urlNr ; i + + ) {
if ( xmlStrEqual ( URL , ctxt - > urlTab [ i ] . url ) ) {
2002-09-05 14:52:10 +04:00
# ifdef DEBUG_XINCLUDE
printf ( " Already loaded %s \n " , URL ) ;
# endif
2022-10-23 18:52:29 +03:00
if ( ctxt - > urlTab [ i ] . expanding ) {
2022-10-23 17:02:48 +03:00
xmlXIncludeErr ( ctxt , ref - > elem , XML_XINCLUDE_RECURSION ,
" inclusion loop detected \n " , NULL ) ;
2022-10-30 14:32:14 +03:00
goto error ;
2022-10-23 17:02:48 +03:00
}
2022-10-23 18:52:29 +03:00
doc = ctxt - > urlTab [ i ] . doc ;
2022-10-30 14:32:14 +03:00
if ( doc = = NULL )
goto error ;
2001-02-23 20:55:21 +03:00
goto loaded ;
}
}
2002-09-05 14:52:10 +04:00
2001-02-23 20:55:21 +03:00
/*
* Load it .
*/
2002-09-05 14:52:10 +04:00
# ifdef DEBUG_XINCLUDE
printf ( " loading %s \n " , URL ) ;
# endif
2004-03-08 17:42:31 +03:00
# ifdef LIBXML_XPTR_ENABLED
/*
* If this is an XPointer evaluation , we want to assure that
* all entities have been resolved prior to processing the
* referenced document
*/
saveFlags = ctxt - > parseFlags ;
if ( fragment ! = NULL ) { /* if this is an XPointer eval */
ctxt - > parseFlags | = XML_PARSE_NOENT ;
}
# endif
2003-08-14 19:44:40 +04:00
doc = xmlXIncludeParseFile ( ctxt , ( const char * ) URL ) ;
2004-03-08 17:42:31 +03:00
# ifdef LIBXML_XPTR_ENABLED
ctxt - > parseFlags = saveFlags ;
# endif
2022-10-23 17:02:48 +03:00
/* Also cache NULL docs */
2022-10-23 18:52:29 +03:00
if ( ctxt - > urlNr > = ctxt - > urlMax ) {
2022-10-23 17:02:48 +03:00
xmlXIncludeDoc * tmp ;
2022-10-23 18:52:29 +03:00
size_t newSize = ctxt - > urlMax ? ctxt - > urlMax * 2 : 8 ;
2022-10-23 17:02:48 +03:00
2022-10-23 18:52:29 +03:00
tmp = xmlRealloc ( ctxt - > urlTab , sizeof ( xmlXIncludeDoc ) * newSize ) ;
2022-10-23 17:02:48 +03:00
if ( tmp = = NULL ) {
xmlXIncludeErrMemory ( ctxt , ref - > elem ,
" growing XInclude URL table " ) ;
2022-10-30 14:32:14 +03:00
goto error ;
2022-10-23 17:02:48 +03:00
}
2022-10-23 18:52:29 +03:00
ctxt - > urlMax = newSize ;
ctxt - > urlTab = tmp ;
2022-10-23 17:02:48 +03:00
}
2022-10-23 18:52:29 +03:00
cache = & ctxt - > urlTab [ ctxt - > urlNr + + ] ;
2022-10-23 17:02:48 +03:00
cache - > doc = doc ;
cache - > url = xmlStrdup ( URL ) ;
cache - > expanding = 0 ;
2022-10-30 14:32:14 +03:00
if ( doc = = NULL )
goto error ;
2004-07-26 04:20:13 +04:00
/*
* It ' s possible that the requested URL has been mapped to a
* completely different location ( e . g . through a catalog entry ) .
* To check for this , we compare the URL with that of the doc
* and change it if they disagree ( bug 146988 ) .
*/
if ( ! xmlStrEqual ( URL , doc - > URL ) ) {
xmlFree ( URL ) ;
URL = xmlStrdup ( doc - > URL ) ;
}
2002-09-05 14:52:10 +04:00
/*
2003-02-05 01:48:53 +03:00
* Make sure we have all entities fixed up
2002-09-05 14:52:10 +04:00
*/
2003-02-05 01:48:53 +03:00
xmlXIncludeMergeEntities ( ctxt , ctxt - > doc , doc ) ;
2002-09-05 14:52:10 +04:00
/*
* We don ' t need the DTD anymore , free up space
if ( doc - > intSubset ! = NULL ) {
xmlUnlinkNode ( ( xmlNodePtr ) doc - > intSubset ) ;
xmlFreeNode ( ( xmlNodePtr ) doc - > intSubset ) ;
doc - > intSubset = NULL ;
}
if ( doc - > extSubset ! = NULL ) {
xmlUnlinkNode ( ( xmlNodePtr ) doc - > extSubset ) ;
xmlFreeNode ( ( xmlNodePtr ) doc - > extSubset ) ;
doc - > extSubset = NULL ;
}
*/
2022-10-23 17:02:48 +03:00
cache - > expanding = 1 ;
2002-09-05 14:52:10 +04:00
xmlXIncludeRecurseDoc ( ctxt , doc , URL ) ;
2022-10-23 17:02:48 +03:00
cache - > expanding = 0 ;
2001-02-23 20:55:21 +03:00
loaded :
if ( fragment = = NULL ) {
/*
* Add the top children list as the replacement copy .
*/
2022-10-23 14:57:33 +03:00
ref - > inc = xmlDocCopyNode ( xmlDocGetRootElement ( doc ) , ctxt - > doc , 1 ) ;
2012-09-11 09:26:36 +04:00
}
2003-09-02 00:59:40 +04:00
# ifdef LIBXML_XPTR_ENABLED
else {
2001-02-23 20:55:21 +03:00
/*
* Computes the XPointer expression and make a copy used
* as the replacement copy .
*/
xmlXPathObjectPtr xptr ;
xmlXPathContextPtr xptrctxt ;
2001-06-19 22:09:42 +04:00
xmlNodeSetPtr set ;
2001-02-23 20:55:21 +03:00
2022-10-30 22:28:20 +03:00
if ( ctxt - > isStream & & doc = = ctxt - > doc ) {
2022-10-30 14:21:20 +03:00
xmlXIncludeErr ( ctxt , ref - > elem , XML_XINCLUDE_XPTR_FAILED ,
" XPointer expressions not allowed in streaming "
" mode \n " , NULL ) ;
2022-10-30 14:32:14 +03:00
goto error ;
2022-10-30 14:21:20 +03:00
}
2022-10-21 16:56:12 +03:00
xptrctxt = xmlXPtrNewContext ( doc , NULL , NULL ) ;
2001-02-23 20:55:21 +03:00
if ( xptrctxt = = NULL ) {
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , ref - > elem , XML_XINCLUDE_XPTR_FAILED ,
2003-11-19 19:24:26 +03:00
" could not create XPointer context \n " , NULL ) ;
2022-10-30 14:32:14 +03:00
goto error ;
2001-02-23 20:55:21 +03:00
}
xptr = xmlXPtrEval ( fragment , xptrctxt ) ;
if ( xptr = = NULL ) {
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , ref - > elem , XML_XINCLUDE_XPTR_FAILED ,
2003-10-09 02:38:13 +04:00
" XPointer evaluation failed: #%s \n " ,
fragment ) ;
2001-02-23 20:55:21 +03:00
xmlXPathFreeContext ( xptrctxt ) ;
2022-10-30 14:32:14 +03:00
goto error ;
2001-02-23 20:55:21 +03:00
}
2001-06-19 22:09:42 +04:00
switch ( xptr - > type ) {
case XPATH_UNDEFINED :
case XPATH_BOOLEAN :
case XPATH_NUMBER :
case XPATH_STRING :
2022-04-21 00:17:14 +03:00
# ifdef LIBXML_XPTR_LOCS_ENABLED
2001-06-19 22:09:42 +04:00
case XPATH_POINT :
2022-04-21 00:17:14 +03:00
# endif
2001-06-19 22:09:42 +04:00
case XPATH_USERS :
case XPATH_XSLT_TREE :
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , ref - > elem , XML_XINCLUDE_XPTR_RESULT ,
2003-10-09 02:38:13 +04:00
" XPointer is not a range: #%s \n " ,
fragment ) ;
2020-05-30 16:32:25 +03:00
xmlXPathFreeObject ( xptr ) ;
2001-06-19 22:09:42 +04:00
xmlXPathFreeContext ( xptrctxt ) ;
2022-10-30 14:32:14 +03:00
goto error ;
2001-06-19 22:09:42 +04:00
case XPATH_NODESET :
2003-11-03 20:13:52 +03:00
if ( ( xptr - > nodesetval = = NULL ) | |
( xptr - > nodesetval - > nodeNr < = 0 ) ) {
2020-05-30 16:32:25 +03:00
xmlXPathFreeObject ( xptr ) ;
2003-11-03 20:13:52 +03:00
xmlXPathFreeContext ( xptrctxt ) ;
2022-10-30 14:32:14 +03:00
goto error ;
2003-11-03 20:13:52 +03:00
}
2004-06-08 06:01:28 +04:00
2022-04-21 00:17:14 +03:00
# ifdef LIBXML_XPTR_LOCS_ENABLED
2001-06-19 22:09:42 +04:00
case XPATH_RANGE :
case XPATH_LOCATIONSET :
break ;
2022-04-21 00:17:14 +03:00
# endif
2001-06-19 22:09:42 +04:00
}
set = xptr - > nodesetval ;
if ( set ! = NULL ) {
for ( i = 0 ; i < set - > nodeNr ; i + + ) {
if ( set - > nodeTab [ i ] = = NULL )
continue ;
switch ( set - > nodeTab [ i ] - > type ) {
2007-07-18 22:04:55 +04:00
case XML_ELEMENT_NODE :
2001-06-19 22:09:42 +04:00
case XML_TEXT_NODE :
case XML_CDATA_SECTION_NODE :
case XML_ENTITY_REF_NODE :
case XML_ENTITY_NODE :
case XML_PI_NODE :
case XML_COMMENT_NODE :
case XML_DOCUMENT_NODE :
case XML_HTML_DOCUMENT_NODE :
continue ;
2004-06-08 06:01:28 +04:00
2001-06-19 22:09:42 +04:00
case XML_ATTRIBUTE_NODE :
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , ref - > elem ,
2003-10-09 02:38:13 +04:00
XML_XINCLUDE_XPTR_RESULT ,
" XPointer selects an attribute: #%s \n " ,
fragment ) ;
2001-06-19 22:09:42 +04:00
set - > nodeTab [ i ] = NULL ;
continue ;
case XML_NAMESPACE_DECL :
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , ref - > elem ,
2003-10-09 02:38:13 +04:00
XML_XINCLUDE_XPTR_RESULT ,
" XPointer selects a namespace: #%s \n " ,
fragment ) ;
2001-06-19 22:09:42 +04:00
set - > nodeTab [ i ] = NULL ;
continue ;
case XML_DOCUMENT_TYPE_NODE :
case XML_DOCUMENT_FRAG_NODE :
case XML_NOTATION_NODE :
case XML_DTD_NODE :
case XML_ELEMENT_DECL :
case XML_ATTRIBUTE_DECL :
case XML_ENTITY_DECL :
case XML_XINCLUDE_START :
case XML_XINCLUDE_END :
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , ref - > elem ,
2003-10-09 02:38:13 +04:00
XML_XINCLUDE_XPTR_RESULT ,
" XPointer selects unexpected nodes: #%s \n " ,
fragment ) ;
2001-06-19 22:09:42 +04:00
set - > nodeTab [ i ] = NULL ;
set - > nodeTab [ i ] = NULL ;
continue ; /* for */
}
}
}
2022-10-23 14:57:33 +03:00
ref - > inc = xmlXIncludeCopyXPointer ( ctxt , xptr ) ;
2022-10-21 16:56:12 +03:00
xmlXPathFreeObject ( xptr ) ;
2001-02-23 20:55:21 +03:00
xmlXPathFreeContext ( xptrctxt ) ;
}
2003-09-02 00:59:40 +04:00
# endif
2002-08-14 18:45:25 +04:00
/*
* Do the xml : base fixup if needed
*/
2013-05-06 06:20:18 +04:00
if ( ( doc ! = NULL ) & & ( URL ! = NULL ) & &
2008-08-26 11:26:55 +04:00
( ! ( ctxt - > parseFlags & XML_PARSE_NOBASEFIX ) ) & &
( ! ( doc - > parseFlags & XML_PARSE_NOBASEFIX ) ) ) {
2002-08-14 18:45:25 +04:00
xmlNodePtr node ;
2005-06-01 07:37:59 +04:00
xmlChar * base ;
2004-06-08 06:01:28 +04:00
xmlChar * curBase ;
2002-08-14 18:45:25 +04:00
2004-06-07 12:57:27 +04:00
/*
2005-06-01 07:37:59 +04:00
* The base is only adjusted if " necessary " , i . e . if the xinclude node
* has a base specified , or the URL is relative
2004-06-07 12:57:27 +04:00
*/
2022-10-23 14:57:33 +03:00
base = xmlGetNsProp ( ref - > elem , BAD_CAST " base " , XML_XML_NAMESPACE ) ;
2005-06-01 07:37:59 +04:00
if ( base = = NULL ) {
/*
* No xml : base on the xinclude node , so we check whether the
* URI base is different than ( relative to ) the context base
*/
curBase = xmlBuildRelativeURI ( URL , ctxt - > base ) ;
if ( curBase = = NULL ) { /* Error return */
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , ref - > elem , XML_XINCLUDE_HREF_URI ,
2004-06-07 12:57:27 +04:00
" trying to build relative URI from %s \n " , URL ) ;
2005-06-01 07:37:59 +04:00
} else {
/* If the URI doesn't contain a slash, it's not relative */
2022-09-01 03:58:00 +03:00
if ( ! xmlStrchr ( curBase , ' / ' ) )
2005-06-01 07:37:59 +04:00
xmlFree ( curBase ) ;
else
base = curBase ;
}
}
if ( base ! = NULL ) { /* Adjustment may be needed */
2022-10-23 14:57:33 +03:00
node = ref - > inc ;
2005-06-01 07:37:59 +04:00
while ( node ! = NULL ) {
/* Only work on element nodes */
if ( node - > type = = XML_ELEMENT_NODE ) {
curBase = xmlNodeGetBase ( node - > doc , node ) ;
/* If no current base, set it */
if ( curBase = = NULL ) {
xmlNodeSetBase ( node , base ) ;
} else {
/*
* If the current base is the same as the
* URL of the document , then reset it to be
* the specified xml : base or the relative URI
*/
if ( xmlStrEqual ( curBase , node - > doc - > URL ) ) {
xmlNodeSetBase ( node , base ) ;
} else {
/*
* If the element already has an xml : base
* set , then relativise it if necessary
*/
xmlChar * xmlBase ;
xmlBase = xmlGetNsProp ( node ,
BAD_CAST " base " ,
XML_XML_NAMESPACE ) ;
if ( xmlBase ! = NULL ) {
xmlChar * relBase ;
relBase = xmlBuildURI ( xmlBase , base ) ;
if ( relBase = = NULL ) { /* error */
2012-09-11 09:26:36 +04:00
xmlXIncludeErr ( ctxt ,
2022-10-23 14:57:33 +03:00
ref - > elem ,
2005-06-01 07:37:59 +04:00
XML_XINCLUDE_HREF_URI ,
" trying to rebuild base from %s \n " ,
xmlBase ) ;
} else {
xmlNodeSetBase ( node , relBase ) ;
xmlFree ( relBase ) ;
}
xmlFree ( xmlBase ) ;
}
}
xmlFree ( curBase ) ;
2004-06-08 06:01:28 +04:00
}
2004-06-07 12:57:27 +04:00
}
2005-06-01 07:37:59 +04:00
node = node - > next ;
2004-06-07 12:57:27 +04:00
}
2005-06-01 07:37:59 +04:00
xmlFree ( base ) ;
2002-08-14 18:45:25 +04:00
}
}
2022-10-30 14:32:14 +03:00
ret = 0 ;
error :
2001-02-23 20:55:21 +03:00
xmlFree ( URL ) ;
2022-10-30 14:32:14 +03:00
xmlFree ( fragment ) ;
return ( ret ) ;
2001-02-23 20:55:21 +03:00
}
/**
* xmlXIncludeLoadTxt :
* @ ctxt : the XInclude context
* @ url : the associated URL
2022-10-23 14:57:33 +03:00
* @ ref : an XMLXincludeRefPtr
2012-09-11 09:26:36 +04:00
*
2001-02-23 20:55:21 +03:00
* Load the content , and store the result in the XInclude context
2002-08-14 18:11:30 +04:00
*
* Returns 0 in case of success , - 1 in case of failure
2001-02-23 20:55:21 +03:00
*/
2002-08-14 18:11:30 +04:00
static int
2022-10-23 14:57:33 +03:00
xmlXIncludeLoadTxt ( xmlXIncludeCtxtPtr ctxt , const xmlChar * url ,
xmlXIncludeRefPtr ref ) {
2001-02-23 20:55:21 +03:00
xmlParserInputBufferPtr buf ;
xmlNodePtr node ;
xmlURIPtr uri ;
xmlChar * URL ;
int i ;
2002-11-20 16:28:31 +03:00
xmlChar * encoding = NULL ;
2003-07-31 18:47:38 +04:00
xmlCharEncoding enc = ( xmlCharEncoding ) 0 ;
2012-05-10 16:59:33 +04:00
xmlParserCtxtPtr pctxt ;
xmlParserInputPtr inputStream ;
2012-08-17 16:42:52 +04:00
int xinclude_multibyte_fallback_used = 0 ;
2002-11-20 16:28:31 +03:00
2019-09-20 13:44:17 +03:00
/* Don't read from stdin. */
if ( xmlStrcmp ( url , BAD_CAST " - " ) = = 0 )
url = BAD_CAST " ./- " ;
2001-02-23 20:55:21 +03:00
/*
* Check the URL and remove any fragment identifier
*/
uri = xmlParseURI ( ( const char * ) url ) ;
if ( uri = = NULL ) {
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , ref - > elem , XML_XINCLUDE_HREF_URI ,
2003-10-09 02:38:13 +04:00
" invalid value URI %s \n " , url ) ;
2002-08-14 18:11:30 +04:00
return ( - 1 ) ;
2001-02-23 20:55:21 +03:00
}
if ( uri - > fragment ! = NULL ) {
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , ref - > elem , XML_XINCLUDE_TEXT_FRAGMENT ,
2003-10-09 02:38:13 +04:00
" fragment identifier forbidden for text: %s \n " ,
( const xmlChar * ) uri - > fragment ) ;
2001-02-23 20:55:21 +03:00
xmlFreeURI ( uri ) ;
2002-08-14 18:11:30 +04:00
return ( - 1 ) ;
2001-02-23 20:55:21 +03:00
}
URL = xmlSaveUri ( uri ) ;
xmlFreeURI ( uri ) ;
if ( URL = = NULL ) {
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , ref - > elem , XML_XINCLUDE_HREF_URI ,
2003-10-09 02:38:13 +04:00
" invalid value URI %s \n " , url ) ;
2002-08-14 18:11:30 +04:00
return ( - 1 ) ;
2001-02-23 20:55:21 +03:00
}
/*
* Handling of references to the local document are done
* directly through ctxt - > doc .
*/
if ( URL [ 0 ] = = 0 ) {
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , ref - > elem , XML_XINCLUDE_TEXT_DOCUMENT ,
2003-10-09 02:38:13 +04:00
" text serialization of document not available \n " , NULL ) ;
2001-02-23 20:55:21 +03:00
xmlFree ( URL ) ;
2002-08-14 18:11:30 +04:00
return ( - 1 ) ;
2001-02-23 20:55:21 +03:00
}
/*
2022-10-23 17:02:48 +03:00
* Prevent reloading the document twice .
2001-02-23 20:55:21 +03:00
*/
2022-10-23 18:52:29 +03:00
for ( i = 0 ; i < ctxt - > txtNr ; i + + ) {
if ( xmlStrEqual ( URL , ctxt - > txtTab [ i ] . url ) ) {
2022-10-23 17:02:48 +03:00
node = xmlNewDocText ( ctxt - > doc , ctxt - > txtTab [ i ] . text ) ;
2001-02-23 20:55:21 +03:00
goto loaded ;
}
}
2022-10-23 17:02:48 +03:00
2002-11-20 16:28:31 +03:00
/*
* Try to get the encoding if available
*/
2022-10-23 14:57:33 +03:00
if ( ref - > elem ! = NULL ) {
encoding = xmlGetProp ( ref - > elem , XINCLUDE_PARSE_ENCODING ) ;
2002-11-20 16:28:31 +03:00
}
if ( encoding ! = NULL ) {
/*
* TODO : we should not have to remap to the xmlCharEncoding
* predefined set , a better interface than
* xmlParserInputBufferCreateFilename should allow any
* encoding supported by iconv
*/
enc = xmlParseCharEncoding ( ( const char * ) encoding ) ;
if ( enc = = XML_CHAR_ENCODING_ERROR ) {
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , ref - > elem , XML_XINCLUDE_UNKNOWN_ENCODING ,
2003-10-09 02:38:13 +04:00
" encoding %s not supported \n " , encoding ) ;
2002-11-20 16:28:31 +03:00
xmlFree ( encoding ) ;
xmlFree ( URL ) ;
return ( - 1 ) ;
}
xmlFree ( encoding ) ;
}
2001-02-23 20:55:21 +03:00
/*
* Load it .
*/
2012-05-10 16:59:33 +04:00
pctxt = xmlNewParserCtxt ( ) ;
inputStream = xmlLoadExternalEntity ( ( const char * ) URL , NULL , pctxt ) ;
if ( inputStream = = NULL ) {
xmlFreeParserCtxt ( pctxt ) ;
xmlFree ( URL ) ;
return ( - 1 ) ;
}
buf = inputStream - > buf ;
2001-02-23 20:55:21 +03:00
if ( buf = = NULL ) {
2012-05-10 16:59:33 +04:00
xmlFreeInputStream ( inputStream ) ;
xmlFreeParserCtxt ( pctxt ) ;
2001-02-23 20:55:21 +03:00
xmlFree ( URL ) ;
2002-08-14 18:11:30 +04:00
return ( - 1 ) ;
2001-02-23 20:55:21 +03:00
}
2012-05-10 16:59:33 +04:00
if ( buf - > encoder )
xmlCharEncCloseFunc ( buf - > encoder ) ;
buf - > encoder = xmlGetCharEncodingHandler ( enc ) ;
2022-05-21 00:28:25 +03:00
node = xmlNewDocText ( ctxt - > doc , NULL ) ;
2001-02-23 20:55:21 +03:00
/*
* Scan all chars from the resource and add the to the node
*/
2012-08-17 16:42:52 +04:00
xinclude_multibyte_fallback :
2001-02-23 20:55:21 +03:00
while ( xmlParserInputBufferRead ( buf , 128 ) > 0 ) {
int len ;
const xmlChar * content ;
2012-07-16 10:40:37 +04:00
content = xmlBufContent ( buf - > buffer ) ;
len = xmlBufLength ( buf - > buffer ) ;
2002-11-20 16:28:31 +03:00
for ( i = 0 ; i < len ; ) {
int cur ;
int l ;
cur = xmlStringCurrentChar ( NULL , & content [ i ] , & l ) ;
if ( ! IS_CHAR ( cur ) ) {
2019-09-30 18:04:54 +03:00
/* Handle split multibyte char at buffer boundary */
2012-08-17 16:42:52 +04:00
if ( ( ( len - i ) < 4 ) & & ( ! xinclude_multibyte_fallback_used ) ) {
xinclude_multibyte_fallback_used = 1 ;
xmlBufShrink ( buf - > buffer , i ) ;
goto xinclude_multibyte_fallback ;
} else {
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , ref - > elem , XML_XINCLUDE_INVALID_CHAR ,
2012-08-17 16:42:52 +04:00
" %s contains invalid char \n " , URL ) ;
2019-08-13 15:08:53 +03:00
xmlFreeParserCtxt ( pctxt ) ;
2012-08-17 16:42:52 +04:00
xmlFreeParserInputBuffer ( buf ) ;
xmlFree ( URL ) ;
return ( - 1 ) ;
}
2001-02-23 20:55:21 +03:00
} else {
2012-08-17 16:42:52 +04:00
xinclude_multibyte_fallback_used = 0 ;
2002-11-20 16:28:31 +03:00
xmlNodeAddContentLen ( node , & content [ i ] , l ) ;
2001-02-23 20:55:21 +03:00
}
2002-11-20 16:28:31 +03:00
i + = l ;
2001-02-23 20:55:21 +03:00
}
2012-07-16 10:40:37 +04:00
xmlBufShrink ( buf - > buffer , len ) ;
2001-02-23 20:55:21 +03:00
}
2012-05-10 16:59:33 +04:00
xmlFreeParserCtxt ( pctxt ) ;
xmlFreeInputStream ( inputStream ) ;
2001-02-23 20:55:21 +03:00
2022-10-23 18:52:29 +03:00
if ( ctxt - > txtNr > = ctxt - > txtMax ) {
2022-10-23 17:02:48 +03:00
xmlXIncludeTxt * tmp ;
2022-10-23 18:52:29 +03:00
size_t newSize = ctxt - > txtMax ? ctxt - > txtMax * 2 : 8 ;
2022-10-23 17:02:48 +03:00
2022-10-23 18:52:29 +03:00
tmp = xmlRealloc ( ctxt - > txtTab , sizeof ( xmlXIncludeTxt ) * newSize ) ;
2022-10-23 17:02:48 +03:00
if ( tmp = = NULL ) {
xmlXIncludeErrMemory ( ctxt , ref - > elem ,
" growing XInclude text table " ) ;
return ( - 1 ) ;
}
2022-10-23 18:52:29 +03:00
ctxt - > txtMax = newSize ;
ctxt - > txtTab = tmp ;
2022-10-23 17:02:48 +03:00
}
2022-10-23 18:52:29 +03:00
ctxt - > txtTab [ ctxt - > txtNr ] . text = xmlStrdup ( node - > content ) ;
ctxt - > txtTab [ ctxt - > txtNr ] . url = xmlStrdup ( URL ) ;
ctxt - > txtNr + + ;
2022-10-23 17:02:48 +03:00
2001-02-23 20:55:21 +03:00
loaded :
/*
* Add the element as the replacement copy .
*/
2022-10-23 14:57:33 +03:00
ref - > inc = node ;
2001-02-23 20:55:21 +03:00
xmlFree ( URL ) ;
2002-08-14 18:11:30 +04:00
return ( 0 ) ;
}
/**
* xmlXIncludeLoadFallback :
* @ ctxt : the XInclude context
* @ fallback : the fallback node
2022-10-23 14:57:33 +03:00
* @ ref : an XMLXincludeRefPtr
2012-09-11 09:26:36 +04:00
*
2002-09-05 14:52:10 +04:00
* Load the content of the fallback node , and store the result
2002-08-14 18:11:30 +04:00
* in the XInclude context
*
* Returns 0 in case of success , - 1 in case of failure
*/
static int
2022-10-23 14:57:33 +03:00
xmlXIncludeLoadFallback ( xmlXIncludeCtxtPtr ctxt , xmlNodePtr fallback ,
xmlXIncludeRefPtr ref ) {
2004-01-02 17:59:41 +03:00
int ret = 0 ;
2022-10-23 18:52:29 +03:00
int oldNbErrors ;
2012-08-09 10:24:02 +04:00
if ( ( fallback = = NULL ) | | ( fallback - > type = = XML_NAMESPACE_DECL ) | |
( ctxt = = NULL ) )
2002-08-14 18:11:30 +04:00
return ( - 1 ) ;
2004-02-06 12:33:59 +03:00
if ( fallback - > children ! = NULL ) {
/*
* It ' s possible that the fallback also has ' includes '
* ( Bug 129969 ) , so we re - process the fallback just in case
*/
2022-10-23 18:52:29 +03:00
oldNbErrors = ctxt - > nbErrors ;
ref - > inc = xmlXIncludeCopyNode ( ctxt , fallback , 1 ) ;
2019-09-13 16:45:21 +03:00
if ( ctxt - > nbErrors > oldNbErrors )
2004-02-06 12:33:59 +03:00
ret = - 1 ;
2022-10-23 18:52:29 +03:00
else if ( ref - > inc = = NULL )
2022-10-23 14:57:33 +03:00
ref - > emptyFb = 1 ;
2004-02-06 12:33:59 +03:00
} else {
2022-10-23 14:57:33 +03:00
ref - > inc = NULL ;
ref - > emptyFb = 1 ; /* flag empty callback */
2004-02-06 12:33:59 +03:00
}
2022-10-23 14:57:33 +03:00
ref - > fallback = 1 ;
2004-01-02 17:59:41 +03:00
return ( ret ) ;
2001-02-23 20:55:21 +03:00
}
/************************************************************************
* *
* XInclude Processing *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
2022-10-21 17:17:48 +03:00
* xmlXIncludeExpandNode :
2001-02-23 20:55:21 +03:00
* @ ctxt : an XInclude context
* @ node : an XInclude node
*
2022-10-21 17:17:48 +03:00
* If the XInclude node wasn ' t processed yet , create a new RefPtr ,
* add it to ctxt - > incTab and load the included items .
2001-02-23 20:55:21 +03:00
*
2022-10-21 17:17:48 +03:00
* Returns the new or existing xmlXIncludeRefPtr , or NULL in case of error .
2001-02-23 20:55:21 +03:00
*/
2022-10-21 17:17:48 +03:00
static xmlXIncludeRefPtr
xmlXIncludeExpandNode ( xmlXIncludeCtxtPtr ctxt , xmlNodePtr node ) {
2022-10-23 14:57:33 +03:00
xmlXIncludeRefPtr ref ;
int i ;
2022-10-21 17:17:48 +03:00
2022-10-22 17:50:18 +03:00
if ( ctxt - > depth > = XINCLUDE_MAX_DEPTH ) {
xmlXIncludeErr ( ctxt , node , XML_XINCLUDE_RECURSION ,
" maximum recursion depth exceeded \n " , NULL ) ;
return ( NULL ) ;
}
2022-10-23 18:52:29 +03:00
for ( i = 0 ; i < ctxt - > incNr ; i + + ) {
2022-10-23 14:57:33 +03:00
if ( ctxt - > incTab [ i ] - > elem = = node ) {
2022-10-22 17:09:21 +03:00
if ( ctxt - > incTab [ i ] - > expanding ) {
xmlXIncludeErr ( ctxt , node , XML_XINCLUDE_RECURSION ,
" inclusion loop detected \n " , NULL ) ;
return ( NULL ) ;
}
2022-10-21 17:17:48 +03:00
return ( ctxt - > incTab [ i ] ) ;
2022-10-22 17:09:21 +03:00
}
2022-10-21 17:17:48 +03:00
}
2022-10-23 14:57:33 +03:00
ref = xmlXIncludeAddNode ( ctxt , node ) ;
if ( ref = = NULL )
2022-10-21 17:17:48 +03:00
return ( NULL ) ;
2022-10-23 14:57:33 +03:00
ref - > expanding = 1 ;
2022-10-22 17:50:18 +03:00
ctxt - > depth + + ;
2022-10-23 14:57:33 +03:00
xmlXIncludeLoadNode ( ctxt , ref ) ;
2022-10-22 17:50:18 +03:00
ctxt - > depth - - ;
2022-10-23 14:57:33 +03:00
ref - > expanding = 0 ;
2022-10-21 17:17:48 +03:00
2022-10-23 14:57:33 +03:00
return ( ref ) ;
2001-02-23 20:55:21 +03:00
}
/**
* xmlXIncludeLoadNode :
* @ ctxt : an XInclude context
2022-10-23 14:57:33 +03:00
* @ ref : an xmlXIncludeRefPtr
2001-02-23 20:55:21 +03:00
*
* Find and load the infoset replacement for the given node .
*
2001-12-31 19:16:02 +03:00
* Returns 0 if substitution succeeded , - 1 if some processing failed
2001-02-23 20:55:21 +03:00
*/
2001-03-24 20:00:36 +03:00
static int
2022-10-23 14:57:33 +03:00
xmlXIncludeLoadNode ( xmlXIncludeCtxtPtr ctxt , xmlXIncludeRefPtr ref ) {
2001-02-23 20:55:21 +03:00
xmlNodePtr cur ;
xmlChar * href ;
xmlChar * parse ;
xmlChar * base ;
2004-06-07 12:57:27 +04:00
xmlChar * oldBase ;
2001-02-23 20:55:21 +03:00
xmlChar * URI ;
int xml = 1 ; /* default Issue 64 */
2002-08-14 18:11:30 +04:00
int ret ;
2001-02-23 20:55:21 +03:00
2022-10-23 14:57:33 +03:00
if ( ( ctxt = = NULL ) | | ( ref = = NULL ) )
2001-02-23 20:55:21 +03:00
return ( - 1 ) ;
2022-10-23 14:57:33 +03:00
cur = ref - > elem ;
2001-02-23 20:55:21 +03:00
if ( cur = = NULL )
return ( - 1 ) ;
/*
* read the attributes
*/
2003-12-08 20:41:29 +03:00
href = xmlXIncludeGetProp ( ctxt , cur , XINCLUDE_HREF ) ;
2001-02-23 20:55:21 +03:00
if ( href = = NULL ) {
2004-01-25 22:54:59 +03:00
href = xmlStrdup ( BAD_CAST " " ) ; /* @@@@ href is now optional */
2012-09-11 09:26:36 +04:00
if ( href = = NULL )
2004-01-25 22:54:59 +03:00
return ( - 1 ) ;
2001-02-23 20:55:21 +03:00
}
2003-12-08 20:41:29 +03:00
parse = xmlXIncludeGetProp ( ctxt , cur , XINCLUDE_PARSE ) ;
2001-02-23 20:55:21 +03:00
if ( parse ! = NULL ) {
if ( xmlStrEqual ( parse , XINCLUDE_PARSE_XML ) )
xml = 1 ;
else if ( xmlStrEqual ( parse , XINCLUDE_PARSE_TEXT ) )
xml = 0 ;
else {
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , cur , XML_XINCLUDE_PARSE_VALUE ,
2003-10-09 02:38:13 +04:00
" invalid value %s for 'parse' \n " , parse ) ;
2001-02-23 20:55:21 +03:00
if ( href ! = NULL )
xmlFree ( href ) ;
if ( parse ! = NULL )
xmlFree ( parse ) ;
return ( - 1 ) ;
}
}
/*
* compute the URI
*/
base = xmlNodeGetBase ( ctxt - > doc , cur ) ;
if ( base = = NULL ) {
URI = xmlBuildURI ( href , ctxt - > doc - > URL ) ;
} else {
URI = xmlBuildURI ( href , base ) ;
}
if ( URI = = NULL ) {
xmlChar * escbase ;
xmlChar * eschref ;
/*
2001-12-31 19:16:02 +03:00
* Some escaping may be needed
2001-02-23 20:55:21 +03:00
*/
escbase = xmlURIEscape ( base ) ;
eschref = xmlURIEscape ( href ) ;
URI = xmlBuildURI ( eschref , escbase ) ;
if ( escbase ! = NULL )
xmlFree ( escbase ) ;
if ( eschref ! = NULL )
xmlFree ( eschref ) ;
}
if ( URI = = NULL ) {
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , cur , XML_XINCLUDE_HREF_URI ,
" failed build URL \n " , NULL ) ;
2001-02-23 20:55:21 +03:00
if ( parse ! = NULL )
xmlFree ( parse ) ;
if ( href ! = NULL )
xmlFree ( href ) ;
if ( base ! = NULL )
xmlFree ( base ) ;
return ( - 1 ) ;
}
# ifdef DEBUG_XINCLUDE
xmlGenericError ( xmlGenericErrorContext , " parse: %s \n " ,
xml ? " xml " : " text " ) ;
xmlGenericError ( xmlGenericErrorContext , " URI: %s \n " , URI ) ;
# endif
/*
2004-06-07 12:57:27 +04:00
* Save the base for this include ( saving the current one )
2001-02-23 20:55:21 +03:00
*/
2004-06-07 12:57:27 +04:00
oldBase = ctxt - > base ;
ctxt - > base = base ;
2001-02-23 20:55:21 +03:00
if ( xml ) {
2022-10-23 14:57:33 +03:00
ret = xmlXIncludeLoadDoc ( ctxt , URI , ref ) ;
2001-02-23 20:55:21 +03:00
/* xmlXIncludeGetFragment(ctxt, cur, URI); */
} else {
2022-10-23 14:57:33 +03:00
ret = xmlXIncludeLoadTxt ( ctxt , URI , ref ) ;
2002-08-14 18:11:30 +04:00
}
2004-06-07 12:57:27 +04:00
/*
* Restore the original base before checking for fallback
*/
ctxt - > base = oldBase ;
2012-09-11 09:26:36 +04:00
2002-08-14 18:11:30 +04:00
if ( ret < 0 ) {
xmlNodePtr children ;
/*
2019-09-30 18:04:54 +03:00
* Time to try a fallback if available
2002-08-14 18:11:30 +04:00
*/
# ifdef DEBUG_XINCLUDE
xmlGenericError ( xmlGenericErrorContext , " error looking for fallback \n " ) ;
# endif
children = cur - > children ;
while ( children ! = NULL ) {
if ( ( children - > type = = XML_ELEMENT_NODE ) & &
( children - > ns ! = NULL ) & &
( xmlStrEqual ( children - > name , XINCLUDE_FALLBACK ) ) & &
2003-12-08 20:41:29 +03:00
( ( xmlStrEqual ( children - > ns - > href , XINCLUDE_NS ) ) | |
( xmlStrEqual ( children - > ns - > href , XINCLUDE_OLD_NS ) ) ) ) {
2022-10-23 14:57:33 +03:00
ret = xmlXIncludeLoadFallback ( ctxt , children , ref ) ;
2020-08-17 00:38:48 +03:00
break ;
2002-08-14 18:11:30 +04:00
}
children = children - > next ;
}
}
if ( ret < 0 ) {
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , cur , XML_XINCLUDE_NO_FALLBACK ,
2003-10-09 02:38:13 +04:00
" could not load %s, and no fallback was found \n " ,
URI ) ;
2001-02-23 20:55:21 +03:00
}
/*
* Cleanup
*/
if ( URI ! = NULL )
xmlFree ( URI ) ;
if ( parse ! = NULL )
xmlFree ( parse ) ;
if ( href ! = NULL )
xmlFree ( href ) ;
if ( base ! = NULL )
xmlFree ( base ) ;
return ( 0 ) ;
}
/**
* xmlXIncludeIncludeNode :
* @ ctxt : an XInclude context
2022-10-23 14:57:33 +03:00
* @ ref : an xmlXIncludeRefPtr
2001-02-23 20:55:21 +03:00
*
2019-09-30 18:04:54 +03:00
* Implement the infoset replacement for the given node
2001-02-23 20:55:21 +03:00
*
2001-12-31 19:16:02 +03:00
* Returns 0 if substitution succeeded , - 1 if some processing failed
2001-02-23 20:55:21 +03:00
*/
2001-03-24 20:00:36 +03:00
static int
2022-10-23 14:57:33 +03:00
xmlXIncludeIncludeNode ( xmlXIncludeCtxtPtr ctxt , xmlXIncludeRefPtr ref ) {
2003-02-13 14:02:08 +03:00
xmlNodePtr cur , end , list , tmp ;
2001-02-23 20:55:21 +03:00
2022-10-23 14:57:33 +03:00
if ( ( ctxt = = NULL ) | | ( ref = = NULL ) )
2001-02-23 20:55:21 +03:00
return ( - 1 ) ;
2022-10-23 14:57:33 +03:00
cur = ref - > elem ;
2012-08-09 10:24:02 +04:00
if ( ( cur = = NULL ) | | ( cur - > type = = XML_NAMESPACE_DECL ) )
2001-02-23 20:55:21 +03:00
return ( - 1 ) ;
2022-10-23 14:57:33 +03:00
list = ref - > inc ;
ref - > inc = NULL ;
ref - > emptyFb = 0 ;
2003-02-13 14:02:08 +03:00
/*
* Check against the risk of generating a multi - rooted document
*/
if ( ( cur - > parent ! = NULL ) & &
( cur - > parent - > type ! = XML_ELEMENT_NODE ) ) {
int nb_elem = 0 ;
tmp = list ;
while ( tmp ! = NULL ) {
if ( tmp - > type = = XML_ELEMENT_NODE )
nb_elem + + ;
tmp = tmp - > next ;
}
if ( nb_elem > 1 ) {
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , ref - > elem , XML_XINCLUDE_MULTIPLE_ROOT ,
2003-10-09 02:38:13 +04:00
" XInclude error: would result in multiple root nodes \n " ,
NULL ) ;
2020-06-10 16:11:40 +03:00
xmlFreeNodeList ( list ) ;
2003-02-13 14:02:08 +03:00
return ( - 1 ) ;
}
}
2004-08-16 16:34:50 +04:00
if ( ctxt - > parseFlags & XML_PARSE_NOXINCNODE ) {
/*
* Add the list of nodes
*/
while ( list ! = NULL ) {
end = list ;
list = list - > next ;
2001-02-23 20:55:21 +03:00
2004-08-16 16:34:50 +04:00
xmlAddPrevSibling ( cur , end ) ;
}
2022-04-10 15:09:29 +03:00
/*
* FIXME : xmlUnlinkNode doesn ' t coalesce text nodes .
*/
2004-08-16 16:34:50 +04:00
xmlUnlinkNode ( cur ) ;
xmlFreeNode ( cur ) ;
} else {
2020-08-06 18:51:57 +03:00
xmlNodePtr child , next ;
2004-08-16 16:34:50 +04:00
/*
* Change the current node as an XInclude start one , and add an
* XInclude end one
*/
2022-10-23 14:57:33 +03:00
if ( ref - > fallback )
2020-08-17 01:54:12 +03:00
xmlUnsetProp ( cur , BAD_CAST " href " ) ;
2004-08-16 16:34:50 +04:00
cur - > type = XML_XINCLUDE_START ;
2020-08-19 14:07:28 +03:00
/* Remove fallback children */
for ( child = cur - > children ; child ! = NULL ; child = next ) {
next = child - > next ;
xmlUnlinkNode ( child ) ;
xmlFreeNode ( child ) ;
}
2004-10-26 20:06:51 +04:00
end = xmlNewDocNode ( cur - > doc , cur - > ns , cur - > name , NULL ) ;
2004-08-16 16:34:50 +04:00
if ( end = = NULL ) {
2022-10-23 14:57:33 +03:00
xmlXIncludeErr ( ctxt , ref - > elem , XML_XINCLUDE_BUILD_FAILED ,
2004-08-16 16:34:50 +04:00
" failed to build node \n " , NULL ) ;
2020-06-10 16:11:40 +03:00
xmlFreeNodeList ( list ) ;
2004-08-16 16:34:50 +04:00
return ( - 1 ) ;
}
end - > type = XML_XINCLUDE_END ;
xmlAddNextSibling ( cur , end ) ;
2001-02-23 20:55:21 +03:00
2004-08-16 16:34:50 +04:00
/*
* Add the list of nodes
*/
while ( list ! = NULL ) {
2020-08-19 14:07:28 +03:00
cur = list ;
list = list - > next ;
2020-08-17 01:05:19 +03:00
2020-08-19 14:07:28 +03:00
xmlAddPrevSibling ( end , cur ) ;
}
2001-02-23 20:55:21 +03:00
}
2002-02-10 14:57:22 +03:00
2012-09-11 09:26:36 +04:00
2001-02-23 20:55:21 +03:00
return ( 0 ) ;
}
/**
* xmlXIncludeTestNode :
2003-02-13 14:02:08 +03:00
* @ ctxt : the XInclude processing context
2001-02-23 20:55:21 +03:00
* @ node : an XInclude node
*
* test if the node is an XInclude node
*
* Returns 1 true , 0 otherwise
*/
2001-03-24 20:00:36 +03:00
static int
2003-02-13 14:02:08 +03:00
xmlXIncludeTestNode ( xmlXIncludeCtxtPtr ctxt , xmlNodePtr node ) {
2001-02-23 20:55:21 +03:00
if ( node = = NULL )
return ( 0 ) ;
2003-07-06 21:35:43 +04:00
if ( node - > type ! = XML_ELEMENT_NODE )
return ( 0 ) ;
2001-02-23 20:55:21 +03:00
if ( node - > ns = = NULL )
return ( 0 ) ;
2003-12-08 20:41:29 +03:00
if ( ( xmlStrEqual ( node - > ns - > href , XINCLUDE_NS ) ) | |
( xmlStrEqual ( node - > ns - > href , XINCLUDE_OLD_NS ) ) ) {
if ( xmlStrEqual ( node - > ns - > href , XINCLUDE_OLD_NS ) ) {
if ( ctxt - > legacy = = 0 ) {
2004-02-09 15:39:02 +03:00
#if 0 /* wait for the XML Core Working Group to get something stable ! */
2003-12-08 20:41:29 +03:00
xmlXIncludeWarn ( ctxt , node , XML_XINCLUDE_DEPRECATED_NS ,
" Deprecated XInclude namespace found, use %s " ,
XINCLUDE_NS ) ;
2004-02-09 15:39:02 +03:00
# endif
2003-12-09 14:35:37 +03:00
ctxt - > legacy = 1 ;
2003-12-08 20:41:29 +03:00
}
}
2003-02-13 14:02:08 +03:00
if ( xmlStrEqual ( node - > name , XINCLUDE_NODE ) ) {
xmlNodePtr child = node - > children ;
int nb_fallback = 0 ;
while ( child ! = NULL ) {
if ( ( child - > type = = XML_ELEMENT_NODE ) & &
( child - > ns ! = NULL ) & &
2003-12-08 20:41:29 +03:00
( ( xmlStrEqual ( child - > ns - > href , XINCLUDE_NS ) ) | |
( xmlStrEqual ( child - > ns - > href , XINCLUDE_OLD_NS ) ) ) ) {
2003-02-13 14:02:08 +03:00
if ( xmlStrEqual ( child - > name , XINCLUDE_NODE ) ) {
2003-10-09 02:38:13 +04:00
xmlXIncludeErr ( ctxt , node ,
XML_XINCLUDE_INCLUDE_IN_INCLUDE ,
" %s has an 'include' child \n " ,
XINCLUDE_NODE ) ;
2003-02-13 14:02:08 +03:00
return ( 0 ) ;
}
if ( xmlStrEqual ( child - > name , XINCLUDE_FALLBACK ) ) {
nb_fallback + + ;
}
}
child = child - > next ;
}
if ( nb_fallback > 1 ) {
2003-10-09 02:38:13 +04:00
xmlXIncludeErr ( ctxt , node , XML_XINCLUDE_FALLBACKS_IN_INCLUDE ,
" %s has multiple fallback children \n " ,
XINCLUDE_NODE ) ;
2003-02-13 14:02:08 +03:00
return ( 0 ) ;
}
return ( 1 ) ;
}
if ( xmlStrEqual ( node - > name , XINCLUDE_FALLBACK ) ) {
if ( ( node - > parent = = NULL ) | |
( node - > parent - > type ! = XML_ELEMENT_NODE ) | |
( node - > parent - > ns = = NULL ) | |
2003-12-08 20:41:29 +03:00
( ( ! xmlStrEqual ( node - > parent - > ns - > href , XINCLUDE_NS ) ) & &
( ! xmlStrEqual ( node - > parent - > ns - > href , XINCLUDE_OLD_NS ) ) ) | |
2003-02-13 14:02:08 +03:00
( ! xmlStrEqual ( node - > parent - > name , XINCLUDE_NODE ) ) ) {
2003-10-09 02:38:13 +04:00
xmlXIncludeErr ( ctxt , node ,
XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE ,
" %s is not the child of an 'include' \n " ,
XINCLUDE_FALLBACK ) ;
2003-02-13 14:02:08 +03:00
}
}
}
2001-02-23 20:55:21 +03:00
return ( 0 ) ;
}
/**
2001-05-23 17:44:21 +04:00
* xmlXIncludeDoProcess :
2003-02-13 14:02:08 +03:00
* @ ctxt : the XInclude processing context
2003-07-23 00:52:14 +04:00
* @ tree : the top of the tree to process
2001-02-23 20:55:21 +03:00
*
* Implement the XInclude substitution on the XML document @ doc
*
2001-12-31 19:16:02 +03:00
* Returns 0 if no substitution were done , - 1 if some processing failed
2001-02-23 20:55:21 +03:00
* or the number of substitutions done .
*/
2001-05-23 17:44:21 +04:00
static int
2022-10-23 18:52:29 +03:00
xmlXIncludeDoProcess ( xmlXIncludeCtxtPtr ctxt , xmlNodePtr tree ) {
xmlXIncludeRefPtr ref ;
2001-02-23 20:55:21 +03:00
xmlNodePtr cur ;
int ret = 0 ;
2022-10-30 14:03:51 +03:00
int i , start ;
2001-02-23 20:55:21 +03:00
2022-10-23 17:54:22 +03:00
if ( ( tree = = NULL ) | | ( tree - > type = = XML_NAMESPACE_DECL ) )
2001-02-23 20:55:21 +03:00
return ( - 1 ) ;
if ( ctxt = = NULL )
return ( - 1 ) ;
/*
* First phase : lookup the elements in the document
*/
2022-10-30 14:03:51 +03:00
start = ctxt - > incNr ;
2022-10-23 18:52:29 +03:00
cur = tree ;
2020-08-07 19:39:19 +03:00
do {
2001-02-23 20:55:21 +03:00
/* TODO: need to work on entities -> stack */
2020-06-10 17:34:52 +03:00
if ( xmlXIncludeTestNode ( ctxt , cur ) = = 1 ) {
2020-06-05 14:43:45 +03:00
# ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
/*
* Avoid superlinear expansion by limiting the total number
* of replacements .
*/
if ( ctxt - > incTotal > = 20 )
2022-10-29 16:38:16 +03:00
break ;
2020-06-05 14:43:45 +03:00
# endif
ctxt - > incTotal + + ;
2022-10-23 18:52:29 +03:00
ref = xmlXIncludeExpandNode ( ctxt , cur ) ;
/*
* Mark direct includes .
*/
if ( ref ! = NULL )
ref - > replace = 1 ;
2020-06-10 17:34:52 +03:00
} else if ( ( cur - > children ! = NULL ) & &
2021-04-22 20:26:28 +03:00
( ( cur - > type = = XML_DOCUMENT_NODE ) | |
( cur - > type = = XML_ELEMENT_NODE ) ) ) {
2020-06-10 17:34:52 +03:00
cur = cur - > children ;
continue ;
}
2020-08-07 19:39:19 +03:00
do {
if ( cur = = tree )
break ;
if ( cur - > next ! = NULL ) {
cur = cur - > next ;
break ;
}
cur = cur - > parent ;
} while ( cur ! = NULL ) ;
} while ( ( cur ! = NULL ) & & ( cur ! = tree ) ) ;
2001-02-23 20:55:21 +03:00
/*
2022-10-21 17:17:48 +03:00
* Second phase : extend the original document infoset .
2001-02-23 20:55:21 +03:00
*/
2022-10-30 14:03:51 +03:00
for ( i = start ; i < ctxt - > incNr ; i + + ) {
2022-10-23 18:52:29 +03:00
if ( ctxt - > incTab [ i ] - > replace ! = 0 ) {
if ( ( ctxt - > incTab [ i ] - > inc ! = NULL ) | |
( ctxt - > incTab [ i ] - > emptyFb ! = 0 ) ) { /* (empty fallback) */
xmlXIncludeIncludeNode ( ctxt , ctxt - > incTab [ i ] ) ;
}
2022-10-30 14:03:51 +03:00
ctxt - > incTab [ i ] - > replace = 0 ;
2022-10-23 18:52:29 +03:00
} else {
/*
* Ignore includes which were added indirectly , for example
* inside xi : fallback elements .
*/
if ( ctxt - > incTab [ i ] - > inc ! = NULL ) {
xmlFreeNodeList ( ctxt - > incTab [ i ] - > inc ) ;
ctxt - > incTab [ i ] - > inc = NULL ;
}
}
2022-10-21 17:17:48 +03:00
ret + + ;
2001-02-23 20:55:21 +03:00
}
2022-10-30 14:21:20 +03:00
if ( ctxt - > isStream ) {
/*
* incTab references nodes which will eventually be deleted in
* streaming mode . The table is only required for XPointer
* expressions which aren ' t allowed in streaming mode .
*/
for ( i = 0 ; i < ctxt - > incNr ; i + + ) {
xmlXIncludeFreeRef ( ctxt - > incTab [ i ] ) ;
}
ctxt - > incNr = 0 ;
}
2001-05-23 17:44:21 +04:00
return ( ret ) ;
}
/**
2003-12-09 14:35:37 +03:00
* xmlXIncludeSetFlags :
* @ ctxt : an XInclude processing context
* @ flags : a set of xmlParserOption used for parsing XML includes
*
* Set the flags used for further processing of XML resources .
*
* Returns 0 in case of success and - 1 in case of error .
*/
int
xmlXIncludeSetFlags ( xmlXIncludeCtxtPtr ctxt , int flags ) {
if ( ctxt = = NULL )
return ( - 1 ) ;
ctxt - > parseFlags = flags ;
return ( 0 ) ;
}
2009-08-24 21:45:54 +04:00
2022-10-30 14:21:20 +03:00
/**
* xmlXIncludeSetStreamingMode :
* @ ctxt : an XInclude processing context
* @ mode : whether streaming mode should be enabled
*
* In streaming mode , XPointer expressions aren ' t allowed .
*
* Returns 0 in case of success and - 1 in case of error .
*/
int
xmlXIncludeSetStreamingMode ( xmlXIncludeCtxtPtr ctxt , int mode ) {
if ( ctxt = = NULL )
return ( - 1 ) ;
ctxt - > isStream = ! ! mode ;
return ( 0 ) ;
}
2009-08-24 21:45:54 +04:00
/**
* xmlXIncludeProcessTreeFlagsData :
* @ tree : an XML node
* @ flags : a set of xmlParserOption used for parsing XML includes
* @ data : application data that will be passed to the parser context
* in the _private field of the parser context ( s )
*
* Implement the XInclude substitution on the XML node @ tree
*
* Returns 0 if no substitution were done , - 1 if some processing failed
* or the number of substitutions done .
*/
int
xmlXIncludeProcessTreeFlagsData ( xmlNodePtr tree , int flags , void * data ) {
xmlXIncludeCtxtPtr ctxt ;
int ret = 0 ;
2012-08-09 10:24:02 +04:00
if ( ( tree = = NULL ) | | ( tree - > type = = XML_NAMESPACE_DECL ) | |
( tree - > doc = = NULL ) )
2009-08-24 21:45:54 +04:00
return ( - 1 ) ;
ctxt = xmlXIncludeNewContext ( tree - > doc ) ;
if ( ctxt = = NULL )
return ( - 1 ) ;
ctxt - > _private = data ;
ctxt - > base = xmlStrdup ( ( xmlChar * ) tree - > doc - > URL ) ;
xmlXIncludeSetFlags ( ctxt , flags ) ;
2022-10-23 18:52:29 +03:00
ret = xmlXIncludeDoProcess ( ctxt , tree ) ;
2009-08-24 21:45:54 +04:00
if ( ( ret > = 0 ) & & ( ctxt - > nbErrors > 0 ) )
ret = - 1 ;
xmlXIncludeFreeContext ( ctxt ) ;
return ( ret ) ;
}
2003-12-09 14:35:37 +03:00
/**
2006-09-29 13:16:00 +04:00
* xmlXIncludeProcessFlagsData :
2001-05-23 17:44:21 +04:00
* @ doc : an XML document
2003-12-09 14:35:37 +03:00
* @ flags : a set of xmlParserOption used for parsing XML includes
2006-09-29 13:16:00 +04:00
* @ data : application data that will be passed to the parser context
* in the _private field of the parser context ( s )
2001-05-23 17:44:21 +04:00
*
* Implement the XInclude substitution on the XML document @ doc
*
2001-12-31 19:16:02 +03:00
* Returns 0 if no substitution were done , - 1 if some processing failed
2001-05-23 17:44:21 +04:00
* or the number of substitutions done .
*/
int
2006-09-29 13:16:00 +04:00
xmlXIncludeProcessFlagsData ( xmlDocPtr doc , int flags , void * data ) {
2003-07-23 00:52:14 +04:00
xmlNodePtr tree ;
2001-05-23 17:44:21 +04:00
if ( doc = = NULL )
return ( - 1 ) ;
2003-07-23 00:52:14 +04:00
tree = xmlDocGetRootElement ( doc ) ;
if ( tree = = NULL )
return ( - 1 ) ;
2009-08-24 21:45:54 +04:00
return ( xmlXIncludeProcessTreeFlagsData ( tree , flags , data ) ) ;
2003-07-23 00:52:14 +04:00
}
2006-09-29 13:16:00 +04:00
/**
* xmlXIncludeProcessFlags :
* @ doc : an XML document
* @ flags : a set of xmlParserOption used for parsing XML includes
*
* Implement the XInclude substitution on the XML document @ doc
*
* Returns 0 if no substitution were done , - 1 if some processing failed
* or the number of substitutions done .
*/
int
xmlXIncludeProcessFlags ( xmlDocPtr doc , int flags ) {
return xmlXIncludeProcessFlagsData ( doc , flags , NULL ) ;
}
2003-07-23 00:52:14 +04:00
/**
2003-12-09 14:35:37 +03:00
* xmlXIncludeProcess :
* @ doc : an XML document
*
* Implement the XInclude substitution on the XML document @ doc
*
* Returns 0 if no substitution were done , - 1 if some processing failed
* or the number of substitutions done .
*/
int
xmlXIncludeProcess ( xmlDocPtr doc ) {
return ( xmlXIncludeProcessFlags ( doc , 0 ) ) ;
}
/**
* xmlXIncludeProcessTreeFlags :
2003-07-23 00:52:14 +04:00
* @ tree : a node in an XML document
2003-12-09 14:35:37 +03:00
* @ flags : a set of xmlParserOption used for parsing XML includes
2003-07-23 00:52:14 +04:00
*
* Implement the XInclude substitution for the given subtree
*
* Returns 0 if no substitution were done , - 1 if some processing failed
* or the number of substitutions done .
*/
int
2003-12-09 14:35:37 +03:00
xmlXIncludeProcessTreeFlags ( xmlNodePtr tree , int flags ) {
2003-07-23 00:52:14 +04:00
xmlXIncludeCtxtPtr ctxt ;
int ret = 0 ;
2012-08-09 10:24:02 +04:00
if ( ( tree = = NULL ) | | ( tree - > type = = XML_NAMESPACE_DECL ) | |
( tree - > doc = = NULL ) )
2003-07-23 00:52:14 +04:00
return ( - 1 ) ;
ctxt = xmlXIncludeNewContext ( tree - > doc ) ;
if ( ctxt = = NULL )
return ( - 1 ) ;
2004-06-07 12:57:27 +04:00
ctxt - > base = xmlNodeGetBase ( tree - > doc , tree ) ;
2003-12-09 14:35:37 +03:00
xmlXIncludeSetFlags ( ctxt , flags ) ;
2022-10-23 18:52:29 +03:00
ret = xmlXIncludeDoProcess ( ctxt , tree ) ;
2003-02-11 21:03:05 +03:00
if ( ( ret > = 0 ) & & ( ctxt - > nbErrors > 0 ) )
ret = - 1 ;
2001-05-23 17:44:21 +04:00
2001-02-23 20:55:21 +03:00
xmlXIncludeFreeContext ( ctxt ) ;
return ( ret ) ;
}
2003-12-09 14:35:37 +03:00
/**
* xmlXIncludeProcessTree :
* @ tree : a node in an XML document
*
* Implement the XInclude substitution for the given subtree
*
* Returns 0 if no substitution were done , - 1 if some processing failed
* or the number of substitutions done .
*/
int
xmlXIncludeProcessTree ( xmlNodePtr tree ) {
return ( xmlXIncludeProcessTreeFlags ( tree , 0 ) ) ;
}
2003-11-03 15:31:38 +03:00
/**
* xmlXIncludeProcessNode :
* @ ctxt : an existing XInclude context
* @ node : a node in an XML document
*
* Implement the XInclude substitution for the given subtree reusing
2020-03-08 19:19:42 +03:00
* the information and data coming from the given context .
2003-11-03 15:31:38 +03:00
*
* Returns 0 if no substitution were done , - 1 if some processing failed
* or the number of substitutions done .
*/
int
xmlXIncludeProcessNode ( xmlXIncludeCtxtPtr ctxt , xmlNodePtr node ) {
int ret = 0 ;
2012-08-09 10:24:02 +04:00
if ( ( node = = NULL ) | | ( node - > type = = XML_NAMESPACE_DECL ) | |
( node - > doc = = NULL ) | | ( ctxt = = NULL ) )
2003-11-03 15:31:38 +03:00
return ( - 1 ) ;
2022-10-23 18:52:29 +03:00
ret = xmlXIncludeDoProcess ( ctxt , node ) ;
2003-11-03 15:31:38 +03:00
if ( ( ret > = 0 ) & & ( ctxt - > nbErrors > 0 ) )
ret = - 1 ;
return ( ret ) ;
}
2001-02-23 20:55:21 +03:00
# else /* !LIBXML_XINCLUDE_ENABLED */
# endif