xinclude.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591
  1. /*
  2. * xinclude.c : Code to implement XInclude processing
  3. *
  4. * World Wide Web Consortium W3C Last Call Working Draft 10 November 2003
  5. * http://www.w3.org/TR/2003/WD-xinclude-20031110
  6. *
  7. * See Copyright for the status of this software.
  8. *
  9. * daniel@veillard.com
  10. */
  11. #define IN_LIBXML
  12. #include "libxml.h"
  13. #include <string.h>
  14. #include <libxml/xmlmemory.h>
  15. #include <libxml/tree.h>
  16. #include <libxml/parser.h>
  17. #include <libxml/uri.h>
  18. #include <libxml/xpointer.h>
  19. #include <libxml/parserInternals.h>
  20. #include <libxml/xmlerror.h>
  21. #include <libxml/encoding.h>
  22. #include <libxml/globals.h>
  23. #ifdef LIBXML_XINCLUDE_ENABLED
  24. #include <libxml/xinclude.h>
  25. #define XINCLUDE_MAX_DEPTH 40
  26. /* #define DEBUG_XINCLUDE */
  27. #ifdef DEBUG_XINCLUDE
  28. #ifdef LIBXML_DEBUG_ENABLED
  29. #include <libxml/debugXML.h>
  30. #endif
  31. #endif
  32. /************************************************************************
  33. * *
  34. * XInclude context handling *
  35. * *
  36. ************************************************************************/
  37. /*
  38. * An XInclude context
  39. */
  40. typedef xmlChar *xmlURL;
  41. typedef struct _xmlXIncludeRef xmlXIncludeRef;
  42. typedef xmlXIncludeRef *xmlXIncludeRefPtr;
  43. struct _xmlXIncludeRef {
  44. xmlChar *URI; /* the fully resolved resource URL */
  45. xmlChar *fragment; /* the fragment in the URI */
  46. xmlDocPtr doc; /* the parsed document */
  47. xmlNodePtr ref; /* the node making the reference in the source */
  48. xmlNodePtr inc; /* the included copy */
  49. int xml; /* xml or txt */
  50. int count; /* how many refs use that specific doc */
  51. xmlXPathObjectPtr xptr; /* the xpointer if needed */
  52. int emptyFb; /* flag to show fallback empty */
  53. };
  54. struct _xmlXIncludeCtxt {
  55. xmlDocPtr doc; /* the source document */
  56. int incBase; /* the first include for this document */
  57. int incNr; /* number of includes */
  58. int incMax; /* size of includes tab */
  59. xmlXIncludeRefPtr *incTab; /* array of included references */
  60. int txtNr; /* number of unparsed documents */
  61. int txtMax; /* size of unparsed documents tab */
  62. xmlNodePtr *txtTab; /* array of unparsed text nodes */
  63. xmlURL *txturlTab; /* array of unparsed text URLs */
  64. xmlChar * url; /* the current URL processed */
  65. int urlNr; /* number of URLs stacked */
  66. int urlMax; /* size of URL stack */
  67. xmlChar * *urlTab; /* URL stack */
  68. int nbErrors; /* the number of errors detected */
  69. int legacy; /* using XINCLUDE_OLD_NS */
  70. int parseFlags; /* the flags used for parsing XML documents */
  71. xmlChar * base; /* the current xml:base */
  72. void *_private; /* application data */
  73. };
  74. static int
  75. xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree);
  76. /************************************************************************
  77. * *
  78. * XInclude error handler *
  79. * *
  80. ************************************************************************/
  81. /**
  82. * xmlXIncludeErrMemory:
  83. * @extra: extra information
  84. *
  85. * Handle an out of memory condition
  86. */
  87. static void
  88. xmlXIncludeErrMemory(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node,
  89. const char *extra)
  90. {
  91. if (ctxt != NULL)
  92. ctxt->nbErrors++;
  93. __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
  94. XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0,
  95. extra, NULL, NULL, 0, 0,
  96. "Memory allocation failed : %s\n", extra);
  97. }
  98. /**
  99. * xmlXIncludeErr:
  100. * @ctxt: the XInclude context
  101. * @node: the context node
  102. * @msg: the error message
  103. * @extra: extra information
  104. *
  105. * Handle an XInclude error
  106. */
  107. static void
  108. xmlXIncludeErr(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
  109. const char *msg, const xmlChar *extra)
  110. {
  111. if (ctxt != NULL)
  112. ctxt->nbErrors++;
  113. __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
  114. error, XML_ERR_ERROR, NULL, 0,
  115. (const char *) extra, NULL, NULL, 0, 0,
  116. msg, (const char *) extra);
  117. }
  118. #if 0
  119. /**
  120. * xmlXIncludeWarn:
  121. * @ctxt: the XInclude context
  122. * @node: the context node
  123. * @msg: the error message
  124. * @extra: extra information
  125. *
  126. * Emit an XInclude warning.
  127. */
  128. static void
  129. xmlXIncludeWarn(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
  130. const char *msg, const xmlChar *extra)
  131. {
  132. __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
  133. error, XML_ERR_WARNING, NULL, 0,
  134. (const char *) extra, NULL, NULL, 0, 0,
  135. msg, (const char *) extra);
  136. }
  137. #endif
  138. /**
  139. * xmlXIncludeGetProp:
  140. * @ctxt: the XInclude context
  141. * @cur: the node
  142. * @name: the attribute name
  143. *
  144. * Get an XInclude attribute
  145. *
  146. * Returns the value (to be freed) or NULL if not found
  147. */
  148. static xmlChar *
  149. xmlXIncludeGetProp(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur,
  150. const xmlChar *name) {
  151. xmlChar *ret;
  152. ret = xmlGetNsProp(cur, XINCLUDE_NS, name);
  153. if (ret != NULL)
  154. return(ret);
  155. if (ctxt->legacy != 0) {
  156. ret = xmlGetNsProp(cur, XINCLUDE_OLD_NS, name);
  157. if (ret != NULL)
  158. return(ret);
  159. }
  160. ret = xmlGetProp(cur, name);
  161. return(ret);
  162. }
  163. /**
  164. * xmlXIncludeFreeRef:
  165. * @ref: the XInclude reference
  166. *
  167. * Free an XInclude reference
  168. */
  169. static void
  170. xmlXIncludeFreeRef(xmlXIncludeRefPtr ref) {
  171. if (ref == NULL)
  172. return;
  173. #ifdef DEBUG_XINCLUDE
  174. xmlGenericError(xmlGenericErrorContext, "Freeing ref\n");
  175. #endif
  176. if (ref->doc != NULL) {
  177. #ifdef DEBUG_XINCLUDE
  178. xmlGenericError(xmlGenericErrorContext, "Freeing doc %s\n", ref->URI);
  179. #endif
  180. xmlFreeDoc(ref->doc);
  181. }
  182. if (ref->URI != NULL)
  183. xmlFree(ref->URI);
  184. if (ref->fragment != NULL)
  185. xmlFree(ref->fragment);
  186. if (ref->xptr != NULL)
  187. xmlXPathFreeObject(ref->xptr);
  188. xmlFree(ref);
  189. }
  190. /**
  191. * xmlXIncludeNewRef:
  192. * @ctxt: the XInclude context
  193. * @URI: the resource URI
  194. *
  195. * Creates a new reference within an XInclude context
  196. *
  197. * Returns the new set
  198. */
  199. static xmlXIncludeRefPtr
  200. xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt, const xmlChar *URI,
  201. xmlNodePtr ref) {
  202. xmlXIncludeRefPtr ret;
  203. #ifdef DEBUG_XINCLUDE
  204. xmlGenericError(xmlGenericErrorContext, "New ref %s\n", URI);
  205. #endif
  206. ret = (xmlXIncludeRefPtr) xmlMalloc(sizeof(xmlXIncludeRef));
  207. if (ret == NULL) {
  208. xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
  209. return(NULL);
  210. }
  211. memset(ret, 0, sizeof(xmlXIncludeRef));
  212. if (URI == NULL)
  213. ret->URI = NULL;
  214. else
  215. ret->URI = xmlStrdup(URI);
  216. ret->fragment = NULL;
  217. ret->ref = ref;
  218. ret->doc = NULL;
  219. ret->count = 0;
  220. ret->xml = 0;
  221. ret->inc = NULL;
  222. if (ctxt->incMax == 0) {
  223. ctxt->incMax = 4;
  224. ctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(ctxt->incMax *
  225. sizeof(ctxt->incTab[0]));
  226. if (ctxt->incTab == NULL) {
  227. xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
  228. xmlXIncludeFreeRef(ret);
  229. return(NULL);
  230. }
  231. }
  232. if (ctxt->incNr >= ctxt->incMax) {
  233. ctxt->incMax *= 2;
  234. ctxt->incTab = (xmlXIncludeRefPtr *) xmlRealloc(ctxt->incTab,
  235. ctxt->incMax * sizeof(ctxt->incTab[0]));
  236. if (ctxt->incTab == NULL) {
  237. xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
  238. xmlXIncludeFreeRef(ret);
  239. return(NULL);
  240. }
  241. }
  242. ctxt->incTab[ctxt->incNr++] = ret;
  243. return(ret);
  244. }
  245. /**
  246. * xmlXIncludeNewContext:
  247. * @doc: an XML Document
  248. *
  249. * Creates a new XInclude context
  250. *
  251. * Returns the new set
  252. */
  253. xmlXIncludeCtxtPtr
  254. xmlXIncludeNewContext(xmlDocPtr doc) {
  255. xmlXIncludeCtxtPtr ret;
  256. #ifdef DEBUG_XINCLUDE
  257. xmlGenericError(xmlGenericErrorContext, "New context\n");
  258. #endif
  259. if (doc == NULL)
  260. return(NULL);
  261. ret = (xmlXIncludeCtxtPtr) xmlMalloc(sizeof(xmlXIncludeCtxt));
  262. if (ret == NULL) {
  263. xmlXIncludeErrMemory(NULL, (xmlNodePtr) doc,
  264. "creating XInclude context");
  265. return(NULL);
  266. }
  267. memset(ret, 0, sizeof(xmlXIncludeCtxt));
  268. ret->doc = doc;
  269. ret->incNr = 0;
  270. ret->incBase = 0;
  271. ret->incMax = 0;
  272. ret->incTab = NULL;
  273. ret->nbErrors = 0;
  274. return(ret);
  275. }
  276. /**
  277. * xmlXIncludeURLPush:
  278. * @ctxt: the parser context
  279. * @value: the url
  280. *
  281. * Pushes a new url on top of the url stack
  282. *
  283. * Returns -1 in case of error, the index in the stack otherwise
  284. */
  285. static int
  286. xmlXIncludeURLPush(xmlXIncludeCtxtPtr ctxt,
  287. const xmlChar *value)
  288. {
  289. if (ctxt->urlNr > XINCLUDE_MAX_DEPTH) {
  290. xmlXIncludeErr(ctxt, NULL, XML_XINCLUDE_RECURSION,
  291. "detected a recursion in %s\n", value);
  292. return(-1);
  293. }
  294. if (ctxt->urlTab == NULL) {
  295. ctxt->urlMax = 4;
  296. ctxt->urlNr = 0;
  297. ctxt->urlTab = (xmlChar * *) xmlMalloc(
  298. ctxt->urlMax * sizeof(ctxt->urlTab[0]));
  299. if (ctxt->urlTab == NULL) {
  300. xmlXIncludeErrMemory(ctxt, NULL, "adding URL");
  301. return (-1);
  302. }
  303. }
  304. if (ctxt->urlNr >= ctxt->urlMax) {
  305. ctxt->urlMax *= 2;
  306. ctxt->urlTab =
  307. (xmlChar * *) xmlRealloc(ctxt->urlTab,
  308. ctxt->urlMax *
  309. sizeof(ctxt->urlTab[0]));
  310. if (ctxt->urlTab == NULL) {
  311. xmlXIncludeErrMemory(ctxt, NULL, "adding URL");
  312. return (-1);
  313. }
  314. }
  315. ctxt->url = ctxt->urlTab[ctxt->urlNr] = xmlStrdup(value);
  316. return (ctxt->urlNr++);
  317. }
  318. /**
  319. * xmlXIncludeURLPop:
  320. * @ctxt: the parser context
  321. *
  322. * Pops the top URL from the URL stack
  323. */
  324. static void
  325. xmlXIncludeURLPop(xmlXIncludeCtxtPtr ctxt)
  326. {
  327. xmlChar * ret;
  328. if (ctxt->urlNr <= 0)
  329. return;
  330. ctxt->urlNr--;
  331. if (ctxt->urlNr > 0)
  332. ctxt->url = ctxt->urlTab[ctxt->urlNr - 1];
  333. else
  334. ctxt->url = NULL;
  335. ret = ctxt->urlTab[ctxt->urlNr];
  336. ctxt->urlTab[ctxt->urlNr] = NULL;
  337. if (ret != NULL)
  338. xmlFree(ret);
  339. }
  340. /**
  341. * xmlXIncludeFreeContext:
  342. * @ctxt: the XInclude context
  343. *
  344. * Free an XInclude context
  345. */
  346. void
  347. xmlXIncludeFreeContext(xmlXIncludeCtxtPtr ctxt) {
  348. int i;
  349. #ifdef DEBUG_XINCLUDE
  350. xmlGenericError(xmlGenericErrorContext, "Freeing context\n");
  351. #endif
  352. if (ctxt == NULL)
  353. return;
  354. while (ctxt->urlNr > 0)
  355. xmlXIncludeURLPop(ctxt);
  356. if (ctxt->urlTab != NULL)
  357. xmlFree(ctxt->urlTab);
  358. for (i = 0;i < ctxt->incNr;i++) {
  359. if (ctxt->incTab[i] != NULL)
  360. xmlXIncludeFreeRef(ctxt->incTab[i]);
  361. }
  362. if (ctxt->txturlTab != NULL) {
  363. for (i = 0;i < ctxt->txtNr;i++) {
  364. if (ctxt->txturlTab[i] != NULL)
  365. xmlFree(ctxt->txturlTab[i]);
  366. }
  367. }
  368. if (ctxt->incTab != NULL)
  369. xmlFree(ctxt->incTab);
  370. if (ctxt->txtTab != NULL)
  371. xmlFree(ctxt->txtTab);
  372. if (ctxt->txturlTab != NULL)
  373. xmlFree(ctxt->txturlTab);
  374. if (ctxt->base != NULL) {
  375. xmlFree(ctxt->base);
  376. }
  377. xmlFree(ctxt);
  378. }
  379. /**
  380. * xmlXIncludeParseFile:
  381. * @ctxt: the XInclude context
  382. * @URL: the URL or file path
  383. *
  384. * parse a document for XInclude
  385. */
  386. static xmlDocPtr
  387. xmlXIncludeParseFile(xmlXIncludeCtxtPtr ctxt, const char *URL) {
  388. xmlDocPtr ret;
  389. xmlParserCtxtPtr pctxt;
  390. xmlParserInputPtr inputStream;
  391. xmlInitParser();
  392. pctxt = xmlNewParserCtxt();
  393. if (pctxt == NULL) {
  394. xmlXIncludeErrMemory(ctxt, NULL, "cannot allocate parser context");
  395. return(NULL);
  396. }
  397. /*
  398. * pass in the application data to the parser context.
  399. */
  400. pctxt->_private = ctxt->_private;
  401. /*
  402. * try to ensure that new documents included are actually
  403. * built with the same dictionary as the including document.
  404. */
  405. if ((ctxt->doc != NULL) && (ctxt->doc->dict != NULL)) {
  406. if (pctxt->dict != NULL)
  407. xmlDictFree(pctxt->dict);
  408. pctxt->dict = ctxt->doc->dict;
  409. xmlDictReference(pctxt->dict);
  410. }
  411. xmlCtxtUseOptions(pctxt, ctxt->parseFlags | XML_PARSE_DTDLOAD);
  412. inputStream = xmlLoadExternalEntity(URL, NULL, pctxt);
  413. if (inputStream == NULL) {
  414. xmlFreeParserCtxt(pctxt);
  415. return(NULL);
  416. }
  417. inputPush(pctxt, inputStream);
  418. if (pctxt->directory == NULL)
  419. pctxt->directory = xmlParserGetDirectory(URL);
  420. pctxt->loadsubset |= XML_DETECT_IDS;
  421. xmlParseDocument(pctxt);
  422. if (pctxt->wellFormed) {
  423. ret = pctxt->myDoc;
  424. }
  425. else {
  426. ret = NULL;
  427. if (pctxt->myDoc != NULL)
  428. xmlFreeDoc(pctxt->myDoc);
  429. pctxt->myDoc = NULL;
  430. }
  431. xmlFreeParserCtxt(pctxt);
  432. return(ret);
  433. }
  434. /**
  435. * xmlXIncludeAddNode:
  436. * @ctxt: the XInclude context
  437. * @cur: the new node
  438. *
  439. * Add a new node to process to an XInclude context
  440. */
  441. static int
  442. xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) {
  443. xmlXIncludeRefPtr ref;
  444. xmlURIPtr uri;
  445. xmlChar *URL;
  446. xmlChar *fragment = NULL;
  447. xmlChar *href;
  448. xmlChar *parse;
  449. xmlChar *base;
  450. xmlChar *URI;
  451. int xml = 1, i; /* default Issue 64 */
  452. int local = 0;
  453. if (ctxt == NULL)
  454. return(-1);
  455. if (cur == NULL)
  456. return(-1);
  457. #ifdef DEBUG_XINCLUDE
  458. xmlGenericError(xmlGenericErrorContext, "Add node\n");
  459. #endif
  460. /*
  461. * read the attributes
  462. */
  463. href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
  464. if (href == NULL) {
  465. href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
  466. if (href == NULL)
  467. return(-1);
  468. }
  469. if ((href[0] == '#') || (href[0] == 0))
  470. local = 1;
  471. parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
  472. if (parse != NULL) {
  473. if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
  474. xml = 1;
  475. else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
  476. xml = 0;
  477. else {
  478. xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
  479. "invalid value %s for 'parse'\n", parse);
  480. if (href != NULL)
  481. xmlFree(href);
  482. if (parse != NULL)
  483. xmlFree(parse);
  484. return(-1);
  485. }
  486. }
  487. /*
  488. * compute the URI
  489. */
  490. base = xmlNodeGetBase(ctxt->doc, cur);
  491. if (base == NULL) {
  492. URI = xmlBuildURI(href, ctxt->doc->URL);
  493. } else {
  494. URI = xmlBuildURI(href, base);
  495. }
  496. if (URI == NULL) {
  497. xmlChar *escbase;
  498. xmlChar *eschref;
  499. /*
  500. * Some escaping may be needed
  501. */
  502. escbase = xmlURIEscape(base);
  503. eschref = xmlURIEscape(href);
  504. URI = xmlBuildURI(eschref, escbase);
  505. if (escbase != NULL)
  506. xmlFree(escbase);
  507. if (eschref != NULL)
  508. xmlFree(eschref);
  509. }
  510. if (parse != NULL)
  511. xmlFree(parse);
  512. if (href != NULL)
  513. xmlFree(href);
  514. if (base != NULL)
  515. xmlFree(base);
  516. if (URI == NULL) {
  517. xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
  518. "failed build URL\n", NULL);
  519. return(-1);
  520. }
  521. fragment = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE_XPOINTER);
  522. /*
  523. * Check the URL and remove any fragment identifier
  524. */
  525. uri = xmlParseURI((const char *)URI);
  526. if (uri == NULL) {
  527. xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
  528. "invalid value URI %s\n", URI);
  529. if (fragment != NULL)
  530. xmlFree(fragment);
  531. xmlFree(URI);
  532. return(-1);
  533. }
  534. if (uri->fragment != NULL) {
  535. if (ctxt->legacy != 0) {
  536. if (fragment == NULL) {
  537. fragment = (xmlChar *) uri->fragment;
  538. } else {
  539. xmlFree(uri->fragment);
  540. }
  541. } else {
  542. xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_FRAGMENT_ID,
  543. "Invalid fragment identifier in URI %s use the xpointer attribute\n",
  544. URI);
  545. if (fragment != NULL)
  546. xmlFree(fragment);
  547. xmlFreeURI(uri);
  548. xmlFree(URI);
  549. return(-1);
  550. }
  551. uri->fragment = NULL;
  552. }
  553. URL = xmlSaveUri(uri);
  554. xmlFreeURI(uri);
  555. xmlFree(URI);
  556. if (URL == NULL) {
  557. xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
  558. "invalid value URI %s\n", URI);
  559. if (fragment != NULL)
  560. xmlFree(fragment);
  561. return(-1);
  562. }
  563. /*
  564. * If local and xml then we need a fragment
  565. */
  566. if ((local == 1) && (xml == 1) &&
  567. ((fragment == NULL) || (fragment[0] == 0))) {
  568. xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
  569. "detected a local recursion with no xpointer in %s\n",
  570. URL);
  571. if (fragment != NULL)
  572. xmlFree(fragment);
  573. return(-1);
  574. }
  575. /*
  576. * Check the URL against the stack for recursions
  577. */
  578. if ((!local) && (xml == 1)) {
  579. for (i = 0;i < ctxt->urlNr;i++) {
  580. if (xmlStrEqual(URL, ctxt->urlTab[i])) {
  581. xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
  582. "detected a recursion in %s\n", URL);
  583. return(-1);
  584. }
  585. }
  586. }
  587. ref = xmlXIncludeNewRef(ctxt, URL, cur);
  588. if (ref == NULL) {
  589. return(-1);
  590. }
  591. ref->fragment = fragment;
  592. ref->doc = NULL;
  593. ref->xml = xml;
  594. ref->count = 1;
  595. xmlFree(URL);
  596. return(0);
  597. }
  598. /**
  599. * xmlXIncludeRecurseDoc:
  600. * @ctxt: the XInclude context
  601. * @doc: the new document
  602. * @url: the associated URL
  603. *
  604. * The XInclude recursive nature is handled at this point.
  605. */
  606. static void
  607. xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
  608. const xmlURL url ATTRIBUTE_UNUSED) {
  609. xmlXIncludeCtxtPtr newctxt;
  610. int i;
  611. /*
  612. * Avoid recursion in already substitued resources
  613. for (i = 0;i < ctxt->urlNr;i++) {
  614. if (xmlStrEqual(doc->URL, ctxt->urlTab[i]))
  615. return;
  616. }
  617. */
  618. #ifdef DEBUG_XINCLUDE
  619. xmlGenericError(xmlGenericErrorContext, "Recursing in doc %s\n", doc->URL);
  620. #endif
  621. /*
  622. * Handle recursion here.
  623. */
  624. newctxt = xmlXIncludeNewContext(doc);
  625. if (newctxt != NULL) {
  626. /*
  627. * Copy the private user data
  628. */
  629. newctxt->_private = ctxt->_private;
  630. /*
  631. * Copy the existing document set
  632. */
  633. newctxt->incMax = ctxt->incMax;
  634. newctxt->incNr = ctxt->incNr;
  635. newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax *
  636. sizeof(newctxt->incTab[0]));
  637. if (newctxt->incTab == NULL) {
  638. xmlXIncludeErrMemory(ctxt, (xmlNodePtr) doc, "processing doc");
  639. xmlFree(newctxt);
  640. return;
  641. }
  642. /*
  643. * copy the urlTab
  644. */
  645. newctxt->urlMax = ctxt->urlMax;
  646. newctxt->urlNr = ctxt->urlNr;
  647. newctxt->urlTab = ctxt->urlTab;
  648. /*
  649. * Inherit the existing base
  650. */
  651. newctxt->base = xmlStrdup(ctxt->base);
  652. /*
  653. * Inherit the documents already in use by other includes
  654. */
  655. newctxt->incBase = ctxt->incNr;
  656. for (i = 0;i < ctxt->incNr;i++) {
  657. newctxt->incTab[i] = ctxt->incTab[i];
  658. newctxt->incTab[i]->count++; /* prevent the recursion from
  659. freeing it */
  660. }
  661. /*
  662. * The new context should also inherit the Parse Flags
  663. * (bug 132597)
  664. */
  665. newctxt->parseFlags = ctxt->parseFlags;
  666. xmlXIncludeDoProcess(newctxt, doc, xmlDocGetRootElement(doc));
  667. for (i = 0;i < ctxt->incNr;i++) {
  668. newctxt->incTab[i]->count--;
  669. newctxt->incTab[i] = NULL;
  670. }
  671. /* urlTab may have been reallocated */
  672. ctxt->urlTab = newctxt->urlTab;
  673. ctxt->urlMax = newctxt->urlMax;
  674. newctxt->urlMax = 0;
  675. newctxt->urlNr = 0;
  676. newctxt->urlTab = NULL;
  677. xmlXIncludeFreeContext(newctxt);
  678. }
  679. #ifdef DEBUG_XINCLUDE
  680. xmlGenericError(xmlGenericErrorContext, "Done recursing in doc %s\n", url);
  681. #endif
  682. }
  683. /**
  684. * xmlXIncludeAddTxt:
  685. * @ctxt: the XInclude context
  686. * @txt: the new text node
  687. * @url: the associated URL
  688. *
  689. * Add a new txtument to the list
  690. */
  691. static void
  692. xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) {
  693. #ifdef DEBUG_XINCLUDE
  694. xmlGenericError(xmlGenericErrorContext, "Adding text %s\n", url);
  695. #endif
  696. if (ctxt->txtMax == 0) {
  697. ctxt->txtMax = 4;
  698. ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax *
  699. sizeof(ctxt->txtTab[0]));
  700. if (ctxt->txtTab == NULL) {
  701. xmlXIncludeErrMemory(ctxt, NULL, "processing text");
  702. return;
  703. }
  704. ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax *
  705. sizeof(ctxt->txturlTab[0]));
  706. if (ctxt->txturlTab == NULL) {
  707. xmlXIncludeErrMemory(ctxt, NULL, "processing text");
  708. return;
  709. }
  710. }
  711. if (ctxt->txtNr >= ctxt->txtMax) {
  712. ctxt->txtMax *= 2;
  713. ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab,
  714. ctxt->txtMax * sizeof(ctxt->txtTab[0]));
  715. if (ctxt->txtTab == NULL) {
  716. xmlXIncludeErrMemory(ctxt, NULL, "processing text");
  717. return;
  718. }
  719. ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab,
  720. ctxt->txtMax * sizeof(ctxt->txturlTab[0]));
  721. if (ctxt->txturlTab == NULL) {
  722. xmlXIncludeErrMemory(ctxt, NULL, "processing text");
  723. return;
  724. }
  725. }
  726. ctxt->txtTab[ctxt->txtNr] = txt;
  727. ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url);
  728. ctxt->txtNr++;
  729. }
  730. /************************************************************************
  731. * *
  732. * Node copy with specific semantic *
  733. * *
  734. ************************************************************************/
  735. static xmlNodePtr
  736. xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
  737. xmlDocPtr source, xmlNodePtr elem);
  738. /**
  739. * xmlXIncludeCopyNode:
  740. * @ctxt: the XInclude context
  741. * @target: the document target
  742. * @source: the document source
  743. * @elem: the element
  744. *
  745. * Make a copy of the node while preserving the XInclude semantic
  746. * of the Infoset copy
  747. */
  748. static xmlNodePtr
  749. xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
  750. xmlDocPtr source, xmlNodePtr elem) {
  751. xmlNodePtr result = NULL;
  752. if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
  753. (elem == NULL))
  754. return(NULL);
  755. if (elem->type == XML_DTD_NODE)
  756. return(NULL);
  757. if (elem->type == XML_DOCUMENT_NODE)
  758. result = xmlXIncludeCopyNodeList(ctxt, target, source, elem->children);
  759. else
  760. result = xmlDocCopyNode(elem, target, 1);
  761. return(result);
  762. }
  763. /**
  764. * xmlXIncludeCopyNodeList:
  765. * @ctxt: the XInclude context
  766. * @target: the document target
  767. * @source: the document source
  768. * @elem: the element list
  769. *
  770. * Make a copy of the node list while preserving the XInclude semantic
  771. * of the Infoset copy
  772. */
  773. static xmlNodePtr
  774. xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
  775. xmlDocPtr source, xmlNodePtr elem) {
  776. xmlNodePtr cur, res, result = NULL, last = NULL;
  777. if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
  778. (elem == NULL))
  779. return(NULL);
  780. cur = elem;
  781. while (cur != NULL) {
  782. res = xmlXIncludeCopyNode(ctxt, target, source, cur);
  783. if (res != NULL) {
  784. if (result == NULL) {
  785. result = last = res;
  786. } else {
  787. last->next = res;
  788. res->prev = last;
  789. last = res;
  790. }
  791. }
  792. cur = cur->next;
  793. }
  794. return(result);
  795. }
  796. /**
  797. * xmlXIncludeGetNthChild:
  798. * @cur: the node
  799. * @no: the child number
  800. *
  801. * Returns the @n'th element child of @cur or NULL
  802. */
  803. static xmlNodePtr
  804. xmlXIncludeGetNthChild(xmlNodePtr cur, int no) {
  805. int i;
  806. if (cur == NULL)
  807. return(cur);
  808. cur = cur->children;
  809. for (i = 0;i <= no;cur = cur->next) {
  810. if (cur == NULL)
  811. return(cur);
  812. if ((cur->type == XML_ELEMENT_NODE) ||
  813. (cur->type == XML_DOCUMENT_NODE) ||
  814. (cur->type == XML_HTML_DOCUMENT_NODE)) {
  815. i++;
  816. if (i == no)
  817. break;
  818. }
  819. }
  820. return(cur);
  821. }
  822. xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */
  823. /**
  824. * xmlXIncludeCopyRange:
  825. * @ctxt: the XInclude context
  826. * @target: the document target
  827. * @source: the document source
  828. * @obj: the XPointer result from the evaluation.
  829. *
  830. * Build a node list tree copy of the XPointer result.
  831. *
  832. * Returns an xmlNodePtr list or NULL.
  833. * The caller has to free the node tree.
  834. */
  835. static xmlNodePtr
  836. xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
  837. xmlDocPtr source, xmlXPathObjectPtr range) {
  838. /* pointers to generated nodes */
  839. xmlNodePtr list = NULL, last = NULL, listParent = NULL;
  840. xmlNodePtr tmp, tmp2;
  841. /* pointers to traversal nodes */
  842. xmlNodePtr start, cur, end;
  843. int index1, index2;
  844. int level = 0, lastLevel = 0, endLevel = 0, endFlag = 0;
  845. if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
  846. (range == NULL))
  847. return(NULL);
  848. if (range->type != XPATH_RANGE)
  849. return(NULL);
  850. start = (xmlNodePtr) range->user;
  851. if (start == NULL)
  852. return(NULL);
  853. end = range->user2;
  854. if (end == NULL)
  855. return(xmlDocCopyNode(start, target, 1));
  856. cur = start;
  857. index1 = range->index;
  858. index2 = range->index2;
  859. /*
  860. * level is depth of the current node under consideration
  861. * list is the pointer to the root of the output tree
  862. * listParent is a pointer to the parent of output tree (within
  863. the included file) in case we need to add another level
  864. * last is a pointer to the last node added to the output tree
  865. * lastLevel is the depth of last (relative to the root)
  866. */
  867. while (cur != NULL) {
  868. /*
  869. * Check if our output tree needs a parent
  870. */
  871. if (level < 0) {
  872. while (level < 0) {
  873. /* copy must include namespaces and properties */
  874. tmp2 = xmlDocCopyNode(listParent, target, 2);
  875. xmlAddChild(tmp2, list);
  876. list = tmp2;
  877. listParent = listParent->parent;
  878. level++;
  879. }
  880. last = list;
  881. lastLevel = 0;
  882. }
  883. /*
  884. * Check whether we need to change our insertion point
  885. */
  886. while (level < lastLevel) {
  887. last = last->parent;
  888. lastLevel --;
  889. }
  890. if (cur == end) { /* Are we at the end of the range? */
  891. if (cur->type == XML_TEXT_NODE) {
  892. const xmlChar *content = cur->content;
  893. int len;
  894. if (content == NULL) {
  895. tmp = xmlNewTextLen(NULL, 0);
  896. } else {
  897. len = index2;
  898. if ((cur == start) && (index1 > 1)) {
  899. content += (index1 - 1);
  900. len -= (index1 - 1);
  901. } else {
  902. len = index2;
  903. }
  904. tmp = xmlNewTextLen(content, len);
  905. }
  906. /* single sub text node selection */
  907. if (list == NULL)
  908. return(tmp);
  909. /* prune and return full set */
  910. if (level == lastLevel)
  911. xmlAddNextSibling(last, tmp);
  912. else
  913. xmlAddChild(last, tmp);
  914. return(list);
  915. } else { /* ending node not a text node */
  916. endLevel = level; /* remember the level of the end node */
  917. endFlag = 1;
  918. /* last node - need to take care of properties + namespaces */
  919. tmp = xmlDocCopyNode(cur, target, 2);
  920. if (list == NULL) {
  921. list = tmp;
  922. listParent = cur->parent;
  923. } else {
  924. if (level == lastLevel)
  925. xmlAddNextSibling(last, tmp);
  926. else {
  927. xmlAddChild(last, tmp);
  928. lastLevel = level;
  929. }
  930. }
  931. last = tmp;
  932. if (index2 > 1) {
  933. end = xmlXIncludeGetNthChild(cur, index2 - 1);
  934. index2 = 0;
  935. }
  936. if ((cur == start) && (index1 > 1)) {
  937. cur = xmlXIncludeGetNthChild(cur, index1 - 1);
  938. index1 = 0;
  939. } else {
  940. cur = cur->children;
  941. }
  942. level++; /* increment level to show change */
  943. /*
  944. * Now gather the remaining nodes from cur to end
  945. */
  946. continue; /* while */
  947. }
  948. } else if (cur == start) { /* Not at the end, are we at start? */
  949. if ((cur->type == XML_TEXT_NODE) ||
  950. (cur->type == XML_CDATA_SECTION_NODE)) {
  951. const xmlChar *content = cur->content;
  952. if (content == NULL) {
  953. tmp = xmlNewTextLen(NULL, 0);
  954. } else {
  955. if (index1 > 1) {
  956. content += (index1 - 1);
  957. index1 = 0;
  958. }
  959. tmp = xmlNewText(content);
  960. }
  961. last = list = tmp;
  962. listParent = cur->parent;
  963. } else { /* Not text node */
  964. /*
  965. * start of the range - need to take care of
  966. * properties and namespaces
  967. */
  968. tmp = xmlDocCopyNode(cur, target, 2);
  969. list = last = tmp;
  970. listParent = cur->parent;
  971. if (index1 > 1) { /* Do we need to position? */
  972. cur = xmlXIncludeGetNthChild(cur, index1 - 1);
  973. level = lastLevel = 1;
  974. index1 = 0;
  975. /*
  976. * Now gather the remaining nodes from cur to end
  977. */
  978. continue; /* while */
  979. }
  980. }
  981. } else {
  982. tmp = NULL;
  983. switch (cur->type) {
  984. case XML_DTD_NODE:
  985. case XML_ELEMENT_DECL:
  986. case XML_ATTRIBUTE_DECL:
  987. case XML_ENTITY_NODE:
  988. /* Do not copy DTD informations */
  989. break;
  990. case XML_ENTITY_DECL:
  991. /* handle crossing entities -> stack needed */
  992. break;
  993. case XML_XINCLUDE_START:
  994. case XML_XINCLUDE_END:
  995. /* don't consider it part of the tree content */
  996. break;
  997. case XML_ATTRIBUTE_NODE:
  998. /* Humm, should not happen ! */
  999. break;
  1000. default:
  1001. /*
  1002. * Middle of the range - need to take care of
  1003. * properties and namespaces
  1004. */
  1005. tmp = xmlDocCopyNode(cur, target, 2);
  1006. break;
  1007. }
  1008. if (tmp != NULL) {
  1009. if (level == lastLevel)
  1010. xmlAddNextSibling(last, tmp);
  1011. else {
  1012. xmlAddChild(last, tmp);
  1013. lastLevel = level;
  1014. }
  1015. last = tmp;
  1016. }
  1017. }
  1018. /*
  1019. * Skip to next node in document order
  1020. */
  1021. cur = xmlXPtrAdvanceNode(cur, &level);
  1022. if (endFlag && (level >= endLevel))
  1023. break;
  1024. }
  1025. return(list);
  1026. }
  1027. /**
  1028. * xmlXIncludeBuildNodeList:
  1029. * @ctxt: the XInclude context
  1030. * @target: the document target
  1031. * @source: the document source
  1032. * @obj: the XPointer result from the evaluation.
  1033. *
  1034. * Build a node list tree copy of the XPointer result.
  1035. * This will drop Attributes and Namespace declarations.
  1036. *
  1037. * Returns an xmlNodePtr list or NULL.
  1038. * the caller has to free the node tree.
  1039. */
  1040. static xmlNodePtr
  1041. xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
  1042. xmlDocPtr source, xmlXPathObjectPtr obj) {
  1043. xmlNodePtr list = NULL, last = NULL;
  1044. int i;
  1045. if (source == NULL)
  1046. source = ctxt->doc;
  1047. if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
  1048. (obj == NULL))
  1049. return(NULL);
  1050. switch (obj->type) {
  1051. case XPATH_NODESET: {
  1052. xmlNodeSetPtr set = obj->nodesetval;
  1053. if (set == NULL)
  1054. return(NULL);
  1055. for (i = 0;i < set->nodeNr;i++) {
  1056. if (set->nodeTab[i] == NULL)
  1057. continue;
  1058. switch (set->nodeTab[i]->type) {
  1059. case XML_TEXT_NODE:
  1060. case XML_CDATA_SECTION_NODE:
  1061. case XML_ELEMENT_NODE:
  1062. case XML_ENTITY_REF_NODE:
  1063. case XML_ENTITY_NODE:
  1064. case XML_PI_NODE:
  1065. case XML_COMMENT_NODE:
  1066. case XML_DOCUMENT_NODE:
  1067. case XML_HTML_DOCUMENT_NODE:
  1068. #ifdef LIBXML_DOCB_ENABLED
  1069. case XML_DOCB_DOCUMENT_NODE:
  1070. #endif
  1071. case XML_XINCLUDE_END:
  1072. break;
  1073. case XML_XINCLUDE_START: {
  1074. xmlNodePtr tmp, cur = set->nodeTab[i];
  1075. cur = cur->next;
  1076. while (cur != NULL) {
  1077. switch(cur->type) {
  1078. case XML_TEXT_NODE:
  1079. case XML_CDATA_SECTION_NODE:
  1080. case XML_ELEMENT_NODE:
  1081. case XML_ENTITY_REF_NODE:
  1082. case XML_ENTITY_NODE:
  1083. case XML_PI_NODE:
  1084. case XML_COMMENT_NODE:
  1085. tmp = xmlXIncludeCopyNode(ctxt, target,
  1086. source, cur);
  1087. if (last == NULL) {
  1088. list = last = tmp;
  1089. } else {
  1090. xmlAddNextSibling(last, tmp);
  1091. last = tmp;
  1092. }
  1093. cur = cur->next;
  1094. continue;
  1095. default:
  1096. break;
  1097. }
  1098. break;
  1099. }
  1100. continue;
  1101. }
  1102. case XML_ATTRIBUTE_NODE:
  1103. case XML_NAMESPACE_DECL:
  1104. case XML_DOCUMENT_TYPE_NODE:
  1105. case XML_DOCUMENT_FRAG_NODE:
  1106. case XML_NOTATION_NODE:
  1107. case XML_DTD_NODE:
  1108. case XML_ELEMENT_DECL:
  1109. case XML_ATTRIBUTE_DECL:
  1110. case XML_ENTITY_DECL:
  1111. continue; /* for */
  1112. }
  1113. if (last == NULL)
  1114. list = last = xmlXIncludeCopyNode(ctxt, target, source,
  1115. set->nodeTab[i]);
  1116. else {
  1117. xmlAddNextSibling(last,
  1118. xmlXIncludeCopyNode(ctxt, target, source,
  1119. set->nodeTab[i]));
  1120. if (last->next != NULL)
  1121. last = last->next;
  1122. }
  1123. }
  1124. break;
  1125. }
  1126. case XPATH_LOCATIONSET: {
  1127. xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
  1128. if (set == NULL)
  1129. return(NULL);
  1130. for (i = 0;i < set->locNr;i++) {
  1131. if (last == NULL)
  1132. list = last = xmlXIncludeCopyXPointer(ctxt, target, source,
  1133. set->locTab[i]);
  1134. else
  1135. xmlAddNextSibling(last,
  1136. xmlXIncludeCopyXPointer(ctxt, target, source,
  1137. set->locTab[i]));
  1138. if (last != NULL) {
  1139. while (last->next != NULL)
  1140. last = last->next;
  1141. }
  1142. }
  1143. break;
  1144. }
  1145. #ifdef LIBXML_XPTR_ENABLED
  1146. case XPATH_RANGE:
  1147. return(xmlXIncludeCopyRange(ctxt, target, source, obj));
  1148. #endif
  1149. case XPATH_POINT:
  1150. /* points are ignored in XInclude */
  1151. break;
  1152. default:
  1153. break;
  1154. }
  1155. return(list);
  1156. }
  1157. /************************************************************************
  1158. * *
  1159. * XInclude I/O handling *
  1160. * *
  1161. ************************************************************************/
  1162. typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
  1163. typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
  1164. struct _xmlXIncludeMergeData {
  1165. xmlDocPtr doc;
  1166. xmlXIncludeCtxtPtr ctxt;
  1167. };
  1168. /**
  1169. * xmlXIncludeMergeOneEntity:
  1170. * @ent: the entity
  1171. * @doc: the including doc
  1172. * @nr: the entity name
  1173. *
  1174. * Inplements the merge of one entity
  1175. */
  1176. static void
  1177. xmlXIncludeMergeEntity(xmlEntityPtr ent, xmlXIncludeMergeDataPtr data,
  1178. xmlChar *name ATTRIBUTE_UNUSED) {
  1179. xmlEntityPtr ret, prev;
  1180. xmlDocPtr doc;
  1181. xmlXIncludeCtxtPtr ctxt;
  1182. if ((ent == NULL) || (data == NULL))
  1183. return;
  1184. ctxt = data->ctxt;
  1185. doc = data->doc;
  1186. if ((ctxt == NULL) || (doc == NULL))
  1187. return;
  1188. switch (ent->etype) {
  1189. case XML_INTERNAL_PARAMETER_ENTITY:
  1190. case XML_EXTERNAL_PARAMETER_ENTITY:
  1191. case XML_INTERNAL_PREDEFINED_ENTITY:
  1192. return;
  1193. case XML_INTERNAL_GENERAL_ENTITY:
  1194. case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
  1195. case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
  1196. break;
  1197. }
  1198. ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
  1199. ent->SystemID, ent->content);
  1200. if (ret != NULL) {
  1201. if (ent->URI != NULL)
  1202. ret->URI = xmlStrdup(ent->URI);
  1203. } else {
  1204. prev = xmlGetDocEntity(doc, ent->name);
  1205. if (prev != NULL) {
  1206. if (ent->etype != prev->etype)
  1207. goto error;
  1208. if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
  1209. if (!xmlStrEqual(ent->SystemID, prev->SystemID))
  1210. goto error;
  1211. } else if ((ent->ExternalID != NULL) &&
  1212. (prev->ExternalID != NULL)) {
  1213. if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
  1214. goto error;
  1215. } else if ((ent->content != NULL) && (prev->content != NULL)) {
  1216. if (!xmlStrEqual(ent->content, prev->content))
  1217. goto error;
  1218. } else {
  1219. goto error;
  1220. }
  1221. }
  1222. }
  1223. return;
  1224. error:
  1225. switch (ent->etype) {
  1226. case XML_INTERNAL_PARAMETER_ENTITY:
  1227. case XML_EXTERNAL_PARAMETER_ENTITY:
  1228. case XML_INTERNAL_PREDEFINED_ENTITY:
  1229. case XML_INTERNAL_GENERAL_ENTITY:
  1230. case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
  1231. return;
  1232. case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
  1233. break;
  1234. }
  1235. xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
  1236. "mismatch in redefinition of entity %s\n",
  1237. ent->name);
  1238. }
  1239. /**
  1240. * xmlXIncludeMergeEntities:
  1241. * @ctxt: an XInclude context
  1242. * @doc: the including doc
  1243. * @from: the included doc
  1244. *
  1245. * Inplements the entity merge
  1246. *
  1247. * Returns 0 if merge succeeded, -1 if some processing failed
  1248. */
  1249. static int
  1250. xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
  1251. xmlDocPtr from) {
  1252. xmlNodePtr cur;
  1253. xmlDtdPtr target, source;
  1254. if (ctxt == NULL)
  1255. return(-1);
  1256. if ((from == NULL) || (from->intSubset == NULL))
  1257. return(0);
  1258. target = doc->intSubset;
  1259. if (target == NULL) {
  1260. cur = xmlDocGetRootElement(doc);
  1261. if (cur == NULL)
  1262. return(-1);
  1263. target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
  1264. if (target == NULL)
  1265. return(-1);
  1266. }
  1267. source = from->intSubset;
  1268. if ((source != NULL) && (source->entities != NULL)) {
  1269. xmlXIncludeMergeData data;
  1270. data.ctxt = ctxt;
  1271. data.doc = doc;
  1272. xmlHashScan((xmlHashTablePtr) source->entities,
  1273. (xmlHashScanner) xmlXIncludeMergeEntity, &data);
  1274. }
  1275. source = from->extSubset;
  1276. if ((source != NULL) && (source->entities != NULL)) {
  1277. xmlXIncludeMergeData data;
  1278. data.ctxt = ctxt;
  1279. data.doc = doc;
  1280. /*
  1281. * don't duplicate existing stuff when external subsets are the same
  1282. */
  1283. if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
  1284. (!xmlStrEqual(target->SystemID, source->SystemID))) {
  1285. xmlHashScan((xmlHashTablePtr) source->entities,
  1286. (xmlHashScanner) xmlXIncludeMergeEntity, &data);
  1287. }
  1288. }
  1289. return(0);
  1290. }
  1291. /**
  1292. * xmlXIncludeLoadDoc:
  1293. * @ctxt: the XInclude context
  1294. * @url: the associated URL
  1295. * @nr: the xinclude node number
  1296. *
  1297. * Load the document, and store the result in the XInclude context
  1298. *
  1299. * Returns 0 in case of success, -1 in case of failure
  1300. */
  1301. static int
  1302. xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
  1303. xmlDocPtr doc;
  1304. xmlURIPtr uri;
  1305. xmlChar *URL;
  1306. xmlChar *fragment = NULL;
  1307. int i = 0;
  1308. #ifdef LIBXML_XPTR_ENABLED
  1309. int saveFlags;
  1310. #endif
  1311. #ifdef DEBUG_XINCLUDE
  1312. xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr);
  1313. #endif
  1314. /*
  1315. * Check the URL and remove any fragment identifier
  1316. */
  1317. uri = xmlParseURI((const char *)url);
  1318. if (uri == NULL) {
  1319. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
  1320. XML_XINCLUDE_HREF_URI,
  1321. "invalid value URI %s\n", url);
  1322. return(-1);
  1323. }
  1324. if (uri->fragment != NULL) {
  1325. fragment = (xmlChar *) uri->fragment;
  1326. uri->fragment = NULL;
  1327. }
  1328. if ((ctxt->incTab != NULL) && (ctxt->incTab[nr] != NULL) &&
  1329. (ctxt->incTab[nr]->fragment != NULL)) {
  1330. if (fragment != NULL) xmlFree(fragment);
  1331. fragment = xmlStrdup(ctxt->incTab[nr]->fragment);
  1332. }
  1333. URL = xmlSaveUri(uri);
  1334. xmlFreeURI(uri);
  1335. if (URL == NULL) {
  1336. if (ctxt->incTab != NULL)
  1337. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
  1338. XML_XINCLUDE_HREF_URI,
  1339. "invalid value URI %s\n", url);
  1340. else
  1341. xmlXIncludeErr(ctxt, NULL,
  1342. XML_XINCLUDE_HREF_URI,
  1343. "invalid value URI %s\n", url);
  1344. if (fragment != NULL)
  1345. xmlFree(fragment);
  1346. return(-1);
  1347. }
  1348. /*
  1349. * Handling of references to the local document are done
  1350. * directly through ctxt->doc.
  1351. */
  1352. if ((URL[0] == 0) || (URL[0] == '#') ||
  1353. ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
  1354. doc = NULL;
  1355. goto loaded;
  1356. }
  1357. /*
  1358. * Prevent reloading twice the document.
  1359. */
  1360. for (i = 0; i < ctxt->incNr; i++) {
  1361. if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) &&
  1362. (ctxt->incTab[i]->doc != NULL)) {
  1363. doc = ctxt->incTab[i]->doc;
  1364. #ifdef DEBUG_XINCLUDE
  1365. printf("Already loaded %s\n", URL);
  1366. #endif
  1367. goto loaded;
  1368. }
  1369. }
  1370. /*
  1371. * Load it.
  1372. */
  1373. #ifdef DEBUG_XINCLUDE
  1374. printf("loading %s\n", URL);
  1375. #endif
  1376. #ifdef LIBXML_XPTR_ENABLED
  1377. /*
  1378. * If this is an XPointer evaluation, we want to assure that
  1379. * all entities have been resolved prior to processing the
  1380. * referenced document
  1381. */
  1382. saveFlags = ctxt->parseFlags;
  1383. if (fragment != NULL) { /* if this is an XPointer eval */
  1384. ctxt->parseFlags |= XML_PARSE_NOENT;
  1385. }
  1386. #endif
  1387. doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
  1388. #ifdef LIBXML_XPTR_ENABLED
  1389. ctxt->parseFlags = saveFlags;
  1390. #endif
  1391. if (doc == NULL) {
  1392. xmlFree(URL);
  1393. if (fragment != NULL)
  1394. xmlFree(fragment);
  1395. return(-1);
  1396. }
  1397. ctxt->incTab[nr]->doc = doc;
  1398. /*
  1399. * It's possible that the requested URL has been mapped to a
  1400. * completely different location (e.g. through a catalog entry).
  1401. * To check for this, we compare the URL with that of the doc
  1402. * and change it if they disagree (bug 146988).
  1403. */
  1404. if (!xmlStrEqual(URL, doc->URL)) {
  1405. xmlFree(URL);
  1406. URL = xmlStrdup(doc->URL);
  1407. }
  1408. for (i = nr + 1; i < ctxt->incNr; i++) {
  1409. if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) {
  1410. ctxt->incTab[nr]->count++;
  1411. #ifdef DEBUG_XINCLUDE
  1412. printf("Increasing %s count since reused\n", URL);
  1413. #endif
  1414. break;
  1415. }
  1416. }
  1417. /*
  1418. * Make sure we have all entities fixed up
  1419. */
  1420. xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
  1421. /*
  1422. * We don't need the DTD anymore, free up space
  1423. if (doc->intSubset != NULL) {
  1424. xmlUnlinkNode((xmlNodePtr) doc->intSubset);
  1425. xmlFreeNode((xmlNodePtr) doc->intSubset);
  1426. doc->intSubset = NULL;
  1427. }
  1428. if (doc->extSubset != NULL) {
  1429. xmlUnlinkNode((xmlNodePtr) doc->extSubset);
  1430. xmlFreeNode((xmlNodePtr) doc->extSubset);
  1431. doc->extSubset = NULL;
  1432. }
  1433. */
  1434. xmlXIncludeRecurseDoc(ctxt, doc, URL);
  1435. loaded:
  1436. if (fragment == NULL) {
  1437. /*
  1438. * Add the top children list as the replacement copy.
  1439. */
  1440. if (doc == NULL)
  1441. {
  1442. /* Hopefully a DTD declaration won't be copied from
  1443. * the same document */
  1444. ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children);
  1445. } else {
  1446. ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc,
  1447. doc, doc->children);
  1448. }
  1449. }
  1450. #ifdef LIBXML_XPTR_ENABLED
  1451. else {
  1452. /*
  1453. * Computes the XPointer expression and make a copy used
  1454. * as the replacement copy.
  1455. */
  1456. xmlXPathObjectPtr xptr;
  1457. xmlXPathContextPtr xptrctxt;
  1458. xmlNodeSetPtr set;
  1459. if (doc == NULL) {
  1460. xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref,
  1461. NULL);
  1462. } else {
  1463. xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
  1464. }
  1465. if (xptrctxt == NULL) {
  1466. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
  1467. XML_XINCLUDE_XPTR_FAILED,
  1468. "could not create XPointer context\n", NULL);
  1469. xmlFree(URL);
  1470. xmlFree(fragment);
  1471. return(-1);
  1472. }
  1473. xptr = xmlXPtrEval(fragment, xptrctxt);
  1474. if (xptr == NULL) {
  1475. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
  1476. XML_XINCLUDE_XPTR_FAILED,
  1477. "XPointer evaluation failed: #%s\n",
  1478. fragment);
  1479. xmlXPathFreeContext(xptrctxt);
  1480. xmlFree(URL);
  1481. xmlFree(fragment);
  1482. return(-1);
  1483. }
  1484. switch (xptr->type) {
  1485. case XPATH_UNDEFINED:
  1486. case XPATH_BOOLEAN:
  1487. case XPATH_NUMBER:
  1488. case XPATH_STRING:
  1489. case XPATH_POINT:
  1490. case XPATH_USERS:
  1491. case XPATH_XSLT_TREE:
  1492. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
  1493. XML_XINCLUDE_XPTR_RESULT,
  1494. "XPointer is not a range: #%s\n",
  1495. fragment);
  1496. xmlXPathFreeContext(xptrctxt);
  1497. xmlFree(URL);
  1498. xmlFree(fragment);
  1499. return(-1);
  1500. case XPATH_NODESET:
  1501. if ((xptr->nodesetval == NULL) ||
  1502. (xptr->nodesetval->nodeNr <= 0)) {
  1503. xmlXPathFreeContext(xptrctxt);
  1504. xmlFree(URL);
  1505. xmlFree(fragment);
  1506. return(-1);
  1507. }
  1508. case XPATH_RANGE:
  1509. case XPATH_LOCATIONSET:
  1510. break;
  1511. }
  1512. set = xptr->nodesetval;
  1513. if (set != NULL) {
  1514. for (i = 0;i < set->nodeNr;i++) {
  1515. if (set->nodeTab[i] == NULL)
  1516. continue;
  1517. switch (set->nodeTab[i]->type) {
  1518. case XML_ELEMENT_NODE:
  1519. case XML_TEXT_NODE:
  1520. case XML_CDATA_SECTION_NODE:
  1521. case XML_ENTITY_REF_NODE:
  1522. case XML_ENTITY_NODE:
  1523. case XML_PI_NODE:
  1524. case XML_COMMENT_NODE:
  1525. case XML_DOCUMENT_NODE:
  1526. case XML_HTML_DOCUMENT_NODE:
  1527. #ifdef LIBXML_DOCB_ENABLED
  1528. case XML_DOCB_DOCUMENT_NODE:
  1529. #endif
  1530. continue;
  1531. case XML_ATTRIBUTE_NODE:
  1532. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
  1533. XML_XINCLUDE_XPTR_RESULT,
  1534. "XPointer selects an attribute: #%s\n",
  1535. fragment);
  1536. set->nodeTab[i] = NULL;
  1537. continue;
  1538. case XML_NAMESPACE_DECL:
  1539. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
  1540. XML_XINCLUDE_XPTR_RESULT,
  1541. "XPointer selects a namespace: #%s\n",
  1542. fragment);
  1543. set->nodeTab[i] = NULL;
  1544. continue;
  1545. case XML_DOCUMENT_TYPE_NODE:
  1546. case XML_DOCUMENT_FRAG_NODE:
  1547. case XML_NOTATION_NODE:
  1548. case XML_DTD_NODE:
  1549. case XML_ELEMENT_DECL:
  1550. case XML_ATTRIBUTE_DECL:
  1551. case XML_ENTITY_DECL:
  1552. case XML_XINCLUDE_START:
  1553. case XML_XINCLUDE_END:
  1554. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
  1555. XML_XINCLUDE_XPTR_RESULT,
  1556. "XPointer selects unexpected nodes: #%s\n",
  1557. fragment);
  1558. set->nodeTab[i] = NULL;
  1559. set->nodeTab[i] = NULL;
  1560. continue; /* for */
  1561. }
  1562. }
  1563. }
  1564. if (doc == NULL) {
  1565. ctxt->incTab[nr]->xptr = xptr;
  1566. ctxt->incTab[nr]->inc = NULL;
  1567. } else {
  1568. ctxt->incTab[nr]->inc =
  1569. xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr);
  1570. xmlXPathFreeObject(xptr);
  1571. }
  1572. xmlXPathFreeContext(xptrctxt);
  1573. xmlFree(fragment);
  1574. }
  1575. #endif
  1576. /*
  1577. * Do the xml:base fixup if needed
  1578. */
  1579. if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/')) &&
  1580. (!(ctxt->parseFlags & XML_PARSE_NOBASEFIX)) &&
  1581. (!(doc->parseFlags & XML_PARSE_NOBASEFIX))) {
  1582. xmlNodePtr node;
  1583. xmlChar *base;
  1584. xmlChar *curBase;
  1585. /*
  1586. * The base is only adjusted if "necessary", i.e. if the xinclude node
  1587. * has a base specified, or the URL is relative
  1588. */
  1589. base = xmlGetNsProp(ctxt->incTab[nr]->ref, BAD_CAST "base",
  1590. XML_XML_NAMESPACE);
  1591. if (base == NULL) {
  1592. /*
  1593. * No xml:base on the xinclude node, so we check whether the
  1594. * URI base is different than (relative to) the context base
  1595. */
  1596. curBase = xmlBuildRelativeURI(URL, ctxt->base);
  1597. if (curBase == NULL) { /* Error return */
  1598. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
  1599. XML_XINCLUDE_HREF_URI,
  1600. "trying to build relative URI from %s\n", URL);
  1601. } else {
  1602. /* If the URI doesn't contain a slash, it's not relative */
  1603. if (!xmlStrchr(curBase, (xmlChar) '/'))
  1604. xmlFree(curBase);
  1605. else
  1606. base = curBase;
  1607. }
  1608. }
  1609. if (base != NULL) { /* Adjustment may be needed */
  1610. node = ctxt->incTab[nr]->inc;
  1611. while (node != NULL) {
  1612. /* Only work on element nodes */
  1613. if (node->type == XML_ELEMENT_NODE) {
  1614. curBase = xmlNodeGetBase(node->doc, node);
  1615. /* If no current base, set it */
  1616. if (curBase == NULL) {
  1617. xmlNodeSetBase(node, base);
  1618. } else {
  1619. /*
  1620. * If the current base is the same as the
  1621. * URL of the document, then reset it to be
  1622. * the specified xml:base or the relative URI
  1623. */
  1624. if (xmlStrEqual(curBase, node->doc->URL)) {
  1625. xmlNodeSetBase(node, base);
  1626. } else {
  1627. /*
  1628. * If the element already has an xml:base
  1629. * set, then relativise it if necessary
  1630. */
  1631. xmlChar *xmlBase;
  1632. xmlBase = xmlGetNsProp(node,
  1633. BAD_CAST "base",
  1634. XML_XML_NAMESPACE);
  1635. if (xmlBase != NULL) {
  1636. xmlChar *relBase;
  1637. relBase = xmlBuildURI(xmlBase, base);
  1638. if (relBase == NULL) { /* error */
  1639. xmlXIncludeErr(ctxt,
  1640. ctxt->incTab[nr]->ref,
  1641. XML_XINCLUDE_HREF_URI,
  1642. "trying to rebuild base from %s\n",
  1643. xmlBase);
  1644. } else {
  1645. xmlNodeSetBase(node, relBase);
  1646. xmlFree(relBase);
  1647. }
  1648. xmlFree(xmlBase);
  1649. }
  1650. }
  1651. xmlFree(curBase);
  1652. }
  1653. }
  1654. node = node->next;
  1655. }
  1656. xmlFree(base);
  1657. }
  1658. }
  1659. if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) &&
  1660. (ctxt->incTab[nr]->count <= 1)) {
  1661. #ifdef DEBUG_XINCLUDE
  1662. printf("freeing %s\n", ctxt->incTab[nr]->doc->URL);
  1663. #endif
  1664. xmlFreeDoc(ctxt->incTab[nr]->doc);
  1665. ctxt->incTab[nr]->doc = NULL;
  1666. }
  1667. xmlFree(URL);
  1668. return(0);
  1669. }
  1670. /**
  1671. * xmlXIncludeLoadTxt:
  1672. * @ctxt: the XInclude context
  1673. * @url: the associated URL
  1674. * @nr: the xinclude node number
  1675. *
  1676. * Load the content, and store the result in the XInclude context
  1677. *
  1678. * Returns 0 in case of success, -1 in case of failure
  1679. */
  1680. static int
  1681. xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
  1682. xmlParserInputBufferPtr buf;
  1683. xmlNodePtr node;
  1684. xmlURIPtr uri;
  1685. xmlChar *URL;
  1686. int i;
  1687. xmlChar *encoding = NULL;
  1688. xmlCharEncoding enc = (xmlCharEncoding) 0;
  1689. /*
  1690. * Check the URL and remove any fragment identifier
  1691. */
  1692. uri = xmlParseURI((const char *)url);
  1693. if (uri == NULL) {
  1694. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
  1695. "invalid value URI %s\n", url);
  1696. return(-1);
  1697. }
  1698. if (uri->fragment != NULL) {
  1699. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT,
  1700. "fragment identifier forbidden for text: %s\n",
  1701. (const xmlChar *) uri->fragment);
  1702. xmlFreeURI(uri);
  1703. return(-1);
  1704. }
  1705. URL = xmlSaveUri(uri);
  1706. xmlFreeURI(uri);
  1707. if (URL == NULL) {
  1708. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
  1709. "invalid value URI %s\n", url);
  1710. return(-1);
  1711. }
  1712. /*
  1713. * Handling of references to the local document are done
  1714. * directly through ctxt->doc.
  1715. */
  1716. if (URL[0] == 0) {
  1717. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
  1718. XML_XINCLUDE_TEXT_DOCUMENT,
  1719. "text serialization of document not available\n", NULL);
  1720. xmlFree(URL);
  1721. return(-1);
  1722. }
  1723. /*
  1724. * Prevent reloading twice the document.
  1725. */
  1726. for (i = 0; i < ctxt->txtNr; i++) {
  1727. if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
  1728. node = xmlCopyNode(ctxt->txtTab[i], 1);
  1729. goto loaded;
  1730. }
  1731. }
  1732. /*
  1733. * Try to get the encoding if available
  1734. */
  1735. if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) {
  1736. encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING);
  1737. }
  1738. if (encoding != NULL) {
  1739. /*
  1740. * TODO: we should not have to remap to the xmlCharEncoding
  1741. * predefined set, a better interface than
  1742. * xmlParserInputBufferCreateFilename should allow any
  1743. * encoding supported by iconv
  1744. */
  1745. enc = xmlParseCharEncoding((const char *) encoding);
  1746. if (enc == XML_CHAR_ENCODING_ERROR) {
  1747. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
  1748. XML_XINCLUDE_UNKNOWN_ENCODING,
  1749. "encoding %s not supported\n", encoding);
  1750. xmlFree(encoding);
  1751. xmlFree(URL);
  1752. return(-1);
  1753. }
  1754. xmlFree(encoding);
  1755. }
  1756. /*
  1757. * Load it.
  1758. */
  1759. buf = xmlParserInputBufferCreateFilename((const char *)URL, enc);
  1760. if (buf == NULL) {
  1761. xmlFree(URL);
  1762. return(-1);
  1763. }
  1764. node = xmlNewText(NULL);
  1765. /*
  1766. * Scan all chars from the resource and add the to the node
  1767. */
  1768. while (xmlParserInputBufferRead(buf, 128) > 0) {
  1769. int len;
  1770. const xmlChar *content;
  1771. content = xmlBufferContent(buf->buffer);
  1772. len = xmlBufferLength(buf->buffer);
  1773. for (i = 0;i < len;) {
  1774. int cur;
  1775. int l;
  1776. cur = xmlStringCurrentChar(NULL, &content[i], &l);
  1777. if (!IS_CHAR(cur)) {
  1778. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
  1779. XML_XINCLUDE_INVALID_CHAR,
  1780. "%s contains invalid char\n", URL);
  1781. xmlFreeParserInputBuffer(buf);
  1782. xmlFree(URL);
  1783. return(-1);
  1784. } else {
  1785. xmlNodeAddContentLen(node, &content[i], l);
  1786. }
  1787. i += l;
  1788. }
  1789. xmlBufferShrink(buf->buffer, len);
  1790. }
  1791. xmlFreeParserInputBuffer(buf);
  1792. xmlXIncludeAddTxt(ctxt, node, URL);
  1793. loaded:
  1794. /*
  1795. * Add the element as the replacement copy.
  1796. */
  1797. ctxt->incTab[nr]->inc = node;
  1798. xmlFree(URL);
  1799. return(0);
  1800. }
  1801. /**
  1802. * xmlXIncludeLoadFallback:
  1803. * @ctxt: the XInclude context
  1804. * @fallback: the fallback node
  1805. * @nr: the xinclude node number
  1806. *
  1807. * Load the content of the fallback node, and store the result
  1808. * in the XInclude context
  1809. *
  1810. * Returns 0 in case of success, -1 in case of failure
  1811. */
  1812. static int
  1813. xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
  1814. xmlXIncludeCtxtPtr newctxt;
  1815. int ret = 0;
  1816. if ((fallback == NULL) || (ctxt == NULL))
  1817. return(-1);
  1818. if (fallback->children != NULL) {
  1819. /*
  1820. * It's possible that the fallback also has 'includes'
  1821. * (Bug 129969), so we re-process the fallback just in case
  1822. */
  1823. newctxt = xmlXIncludeNewContext(ctxt->doc);
  1824. if (newctxt == NULL)
  1825. return (-1);
  1826. newctxt->_private = ctxt->_private;
  1827. newctxt->base = xmlStrdup(ctxt->base); /* Inherit the base from the existing context */
  1828. xmlXIncludeSetFlags(newctxt, ctxt->parseFlags);
  1829. ret = xmlXIncludeDoProcess(newctxt, ctxt->doc, fallback->children);
  1830. if (ctxt->nbErrors > 0)
  1831. ret = -1;
  1832. else if (ret > 0)
  1833. ret = 0; /* xmlXIncludeDoProcess can return +ve number */
  1834. xmlXIncludeFreeContext(newctxt);
  1835. ctxt->incTab[nr]->inc = xmlDocCopyNodeList(ctxt->doc,
  1836. fallback->children);
  1837. } else {
  1838. ctxt->incTab[nr]->inc = NULL;
  1839. ctxt->incTab[nr]->emptyFb = 1; /* flag empty callback */
  1840. }
  1841. return(ret);
  1842. }
  1843. /************************************************************************
  1844. * *
  1845. * XInclude Processing *
  1846. * *
  1847. ************************************************************************/
  1848. /**
  1849. * xmlXIncludePreProcessNode:
  1850. * @ctxt: an XInclude context
  1851. * @node: an XInclude node
  1852. *
  1853. * Implement the XInclude preprocessing, currently just adding the element
  1854. * for further processing.
  1855. *
  1856. * Returns the result list or NULL in case of error
  1857. */
  1858. static xmlNodePtr
  1859. xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
  1860. xmlXIncludeAddNode(ctxt, node);
  1861. return(NULL);
  1862. }
  1863. /**
  1864. * xmlXIncludeLoadNode:
  1865. * @ctxt: an XInclude context
  1866. * @nr: the node number
  1867. *
  1868. * Find and load the infoset replacement for the given node.
  1869. *
  1870. * Returns 0 if substitution succeeded, -1 if some processing failed
  1871. */
  1872. static int
  1873. xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
  1874. xmlNodePtr cur;
  1875. xmlChar *href;
  1876. xmlChar *parse;
  1877. xmlChar *base;
  1878. xmlChar *oldBase;
  1879. xmlChar *URI;
  1880. int xml = 1; /* default Issue 64 */
  1881. int ret;
  1882. if (ctxt == NULL)
  1883. return(-1);
  1884. if ((nr < 0) || (nr >= ctxt->incNr))
  1885. return(-1);
  1886. cur = ctxt->incTab[nr]->ref;
  1887. if (cur == NULL)
  1888. return(-1);
  1889. /*
  1890. * read the attributes
  1891. */
  1892. href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
  1893. if (href == NULL) {
  1894. href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
  1895. if (href == NULL)
  1896. return(-1);
  1897. }
  1898. parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
  1899. if (parse != NULL) {
  1900. if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
  1901. xml = 1;
  1902. else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
  1903. xml = 0;
  1904. else {
  1905. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
  1906. XML_XINCLUDE_PARSE_VALUE,
  1907. "invalid value %s for 'parse'\n", parse);
  1908. if (href != NULL)
  1909. xmlFree(href);
  1910. if (parse != NULL)
  1911. xmlFree(parse);
  1912. return(-1);
  1913. }
  1914. }
  1915. /*
  1916. * compute the URI
  1917. */
  1918. base = xmlNodeGetBase(ctxt->doc, cur);
  1919. if (base == NULL) {
  1920. URI = xmlBuildURI(href, ctxt->doc->URL);
  1921. } else {
  1922. URI = xmlBuildURI(href, base);
  1923. }
  1924. if (URI == NULL) {
  1925. xmlChar *escbase;
  1926. xmlChar *eschref;
  1927. /*
  1928. * Some escaping may be needed
  1929. */
  1930. escbase = xmlURIEscape(base);
  1931. eschref = xmlURIEscape(href);
  1932. URI = xmlBuildURI(eschref, escbase);
  1933. if (escbase != NULL)
  1934. xmlFree(escbase);
  1935. if (eschref != NULL)
  1936. xmlFree(eschref);
  1937. }
  1938. if (URI == NULL) {
  1939. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
  1940. XML_XINCLUDE_HREF_URI, "failed build URL\n", NULL);
  1941. if (parse != NULL)
  1942. xmlFree(parse);
  1943. if (href != NULL)
  1944. xmlFree(href);
  1945. if (base != NULL)
  1946. xmlFree(base);
  1947. return(-1);
  1948. }
  1949. #ifdef DEBUG_XINCLUDE
  1950. xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
  1951. xml ? "xml": "text");
  1952. xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
  1953. #endif
  1954. /*
  1955. * Save the base for this include (saving the current one)
  1956. */
  1957. oldBase = ctxt->base;
  1958. ctxt->base = base;
  1959. if (xml) {
  1960. ret = xmlXIncludeLoadDoc(ctxt, URI, nr);
  1961. /* xmlXIncludeGetFragment(ctxt, cur, URI); */
  1962. } else {
  1963. ret = xmlXIncludeLoadTxt(ctxt, URI, nr);
  1964. }
  1965. /*
  1966. * Restore the original base before checking for fallback
  1967. */
  1968. ctxt->base = oldBase;
  1969. if (ret < 0) {
  1970. xmlNodePtr children;
  1971. /*
  1972. * Time to try a fallback if availble
  1973. */
  1974. #ifdef DEBUG_XINCLUDE
  1975. xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
  1976. #endif
  1977. children = cur->children;
  1978. while (children != NULL) {
  1979. if ((children->type == XML_ELEMENT_NODE) &&
  1980. (children->ns != NULL) &&
  1981. (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
  1982. ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
  1983. (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
  1984. ret = xmlXIncludeLoadFallback(ctxt, children, nr);
  1985. if (ret == 0)
  1986. break;
  1987. }
  1988. children = children->next;
  1989. }
  1990. }
  1991. if (ret < 0) {
  1992. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
  1993. XML_XINCLUDE_NO_FALLBACK,
  1994. "could not load %s, and no fallback was found\n",
  1995. URI);
  1996. }
  1997. /*
  1998. * Cleanup
  1999. */
  2000. if (URI != NULL)
  2001. xmlFree(URI);
  2002. if (parse != NULL)
  2003. xmlFree(parse);
  2004. if (href != NULL)
  2005. xmlFree(href);
  2006. if (base != NULL)
  2007. xmlFree(base);
  2008. return(0);
  2009. }
  2010. /**
  2011. * xmlXIncludeIncludeNode:
  2012. * @ctxt: an XInclude context
  2013. * @nr: the node number
  2014. *
  2015. * Inplement the infoset replacement for the given node
  2016. *
  2017. * Returns 0 if substitution succeeded, -1 if some processing failed
  2018. */
  2019. static int
  2020. xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) {
  2021. xmlNodePtr cur, end, list, tmp;
  2022. if (ctxt == NULL)
  2023. return(-1);
  2024. if ((nr < 0) || (nr >= ctxt->incNr))
  2025. return(-1);
  2026. cur = ctxt->incTab[nr]->ref;
  2027. if (cur == NULL)
  2028. return(-1);
  2029. /*
  2030. * If we stored an XPointer a late computation may be needed
  2031. */
  2032. if ((ctxt->incTab[nr]->inc == NULL) &&
  2033. (ctxt->incTab[nr]->xptr != NULL)) {
  2034. ctxt->incTab[nr]->inc =
  2035. xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc,
  2036. ctxt->incTab[nr]->xptr);
  2037. xmlXPathFreeObject(ctxt->incTab[nr]->xptr);
  2038. ctxt->incTab[nr]->xptr = NULL;
  2039. }
  2040. list = ctxt->incTab[nr]->inc;
  2041. ctxt->incTab[nr]->inc = NULL;
  2042. /*
  2043. * Check against the risk of generating a multi-rooted document
  2044. */
  2045. if ((cur->parent != NULL) &&
  2046. (cur->parent->type != XML_ELEMENT_NODE)) {
  2047. int nb_elem = 0;
  2048. tmp = list;
  2049. while (tmp != NULL) {
  2050. if (tmp->type == XML_ELEMENT_NODE)
  2051. nb_elem++;
  2052. tmp = tmp->next;
  2053. }
  2054. if (nb_elem > 1) {
  2055. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
  2056. XML_XINCLUDE_MULTIPLE_ROOT,
  2057. "XInclude error: would result in multiple root nodes\n",
  2058. NULL);
  2059. return(-1);
  2060. }
  2061. }
  2062. if (ctxt->parseFlags & XML_PARSE_NOXINCNODE) {
  2063. /*
  2064. * Add the list of nodes
  2065. */
  2066. while (list != NULL) {
  2067. end = list;
  2068. list = list->next;
  2069. xmlAddPrevSibling(cur, end);
  2070. }
  2071. xmlUnlinkNode(cur);
  2072. xmlFreeNode(cur);
  2073. } else {
  2074. /*
  2075. * Change the current node as an XInclude start one, and add an
  2076. * XInclude end one
  2077. */
  2078. cur->type = XML_XINCLUDE_START;
  2079. end = xmlNewDocNode(cur->doc, cur->ns, cur->name, NULL);
  2080. if (end == NULL) {
  2081. xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
  2082. XML_XINCLUDE_BUILD_FAILED,
  2083. "failed to build node\n", NULL);
  2084. return(-1);
  2085. }
  2086. end->type = XML_XINCLUDE_END;
  2087. xmlAddNextSibling(cur, end);
  2088. /*
  2089. * Add the list of nodes
  2090. */
  2091. while (list != NULL) {
  2092. cur = list;
  2093. list = list->next;
  2094. xmlAddPrevSibling(end, cur);
  2095. }
  2096. }
  2097. return(0);
  2098. }
  2099. /**
  2100. * xmlXIncludeTestNode:
  2101. * @ctxt: the XInclude processing context
  2102. * @node: an XInclude node
  2103. *
  2104. * test if the node is an XInclude node
  2105. *
  2106. * Returns 1 true, 0 otherwise
  2107. */
  2108. static int
  2109. xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
  2110. if (node == NULL)
  2111. return(0);
  2112. if (node->type != XML_ELEMENT_NODE)
  2113. return(0);
  2114. if (node->ns == NULL)
  2115. return(0);
  2116. if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
  2117. (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
  2118. if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
  2119. if (ctxt->legacy == 0) {
  2120. #if 0 /* wait for the XML Core Working Group to get something stable ! */
  2121. xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS,
  2122. "Deprecated XInclude namespace found, use %s",
  2123. XINCLUDE_NS);
  2124. #endif
  2125. ctxt->legacy = 1;
  2126. }
  2127. }
  2128. if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
  2129. xmlNodePtr child = node->children;
  2130. int nb_fallback = 0;
  2131. while (child != NULL) {
  2132. if ((child->type == XML_ELEMENT_NODE) &&
  2133. (child->ns != NULL) &&
  2134. ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
  2135. (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
  2136. if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
  2137. xmlXIncludeErr(ctxt, node,
  2138. XML_XINCLUDE_INCLUDE_IN_INCLUDE,
  2139. "%s has an 'include' child\n",
  2140. XINCLUDE_NODE);
  2141. return(0);
  2142. }
  2143. if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
  2144. nb_fallback++;
  2145. }
  2146. }
  2147. child = child->next;
  2148. }
  2149. if (nb_fallback > 1) {
  2150. xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
  2151. "%s has multiple fallback children\n",
  2152. XINCLUDE_NODE);
  2153. return(0);
  2154. }
  2155. return(1);
  2156. }
  2157. if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
  2158. if ((node->parent == NULL) ||
  2159. (node->parent->type != XML_ELEMENT_NODE) ||
  2160. (node->parent->ns == NULL) ||
  2161. ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
  2162. (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
  2163. (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
  2164. xmlXIncludeErr(ctxt, node,
  2165. XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
  2166. "%s is not the child of an 'include'\n",
  2167. XINCLUDE_FALLBACK);
  2168. }
  2169. }
  2170. }
  2171. return(0);
  2172. }
  2173. /**
  2174. * xmlXIncludeDoProcess:
  2175. * @ctxt: the XInclude processing context
  2176. * @doc: an XML document
  2177. * @tree: the top of the tree to process
  2178. *
  2179. * Implement the XInclude substitution on the XML document @doc
  2180. *
  2181. * Returns 0 if no substitution were done, -1 if some processing failed
  2182. * or the number of substitutions done.
  2183. */
  2184. static int
  2185. xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree) {
  2186. xmlNodePtr cur;
  2187. int ret = 0;
  2188. int i, start;
  2189. if ((doc == NULL) || (tree == NULL))
  2190. return(-1);
  2191. if (ctxt == NULL)
  2192. return(-1);
  2193. if (doc->URL != NULL) {
  2194. ret = xmlXIncludeURLPush(ctxt, doc->URL);
  2195. if (ret < 0)
  2196. return(-1);
  2197. }
  2198. start = ctxt->incNr;
  2199. /*
  2200. * First phase: lookup the elements in the document
  2201. */
  2202. cur = tree;
  2203. if (xmlXIncludeTestNode(ctxt, cur) == 1)
  2204. xmlXIncludePreProcessNode(ctxt, cur);
  2205. while ((cur != NULL) && (cur != tree->parent)) {
  2206. /* TODO: need to work on entities -> stack */
  2207. if ((cur->children != NULL) &&
  2208. (cur->children->type != XML_ENTITY_DECL) &&
  2209. (cur->children->type != XML_XINCLUDE_START) &&
  2210. (cur->children->type != XML_XINCLUDE_END)) {
  2211. cur = cur->children;
  2212. if (xmlXIncludeTestNode(ctxt, cur))
  2213. xmlXIncludePreProcessNode(ctxt, cur);
  2214. } else if (cur->next != NULL) {
  2215. cur = cur->next;
  2216. if (xmlXIncludeTestNode(ctxt, cur))
  2217. xmlXIncludePreProcessNode(ctxt, cur);
  2218. } else {
  2219. if (cur == tree)
  2220. break;
  2221. do {
  2222. cur = cur->parent;
  2223. if ((cur == NULL) || (cur == tree->parent))
  2224. break; /* do */
  2225. if (cur->next != NULL) {
  2226. cur = cur->next;
  2227. if (xmlXIncludeTestNode(ctxt, cur))
  2228. xmlXIncludePreProcessNode(ctxt, cur);
  2229. break; /* do */
  2230. }
  2231. } while (cur != NULL);
  2232. }
  2233. }
  2234. /*
  2235. * Second Phase : collect the infosets fragments
  2236. */
  2237. for (i = start;i < ctxt->incNr; i++) {
  2238. xmlXIncludeLoadNode(ctxt, i);
  2239. ret++;
  2240. }
  2241. /*
  2242. * Third phase: extend the original document infoset.
  2243. *
  2244. * Originally we bypassed the inclusion if there were any errors
  2245. * encountered on any of the XIncludes. A bug was raised (bug
  2246. * 132588) requesting that we output the XIncludes without error,
  2247. * so the check for inc!=NULL || xptr!=NULL was put in. This may
  2248. * give some other problems in the future, but for now it seems to
  2249. * work ok.
  2250. *
  2251. */
  2252. for (i = ctxt->incBase;i < ctxt->incNr; i++) {
  2253. if ((ctxt->incTab[i]->inc != NULL) ||
  2254. (ctxt->incTab[i]->xptr != NULL) ||
  2255. (ctxt->incTab[i]->emptyFb != 0)) /* (empty fallback) */
  2256. xmlXIncludeIncludeNode(ctxt, i);
  2257. }
  2258. if (doc->URL != NULL)
  2259. xmlXIncludeURLPop(ctxt);
  2260. return(ret);
  2261. }
  2262. /**
  2263. * xmlXIncludeSetFlags:
  2264. * @ctxt: an XInclude processing context
  2265. * @flags: a set of xmlParserOption used for parsing XML includes
  2266. *
  2267. * Set the flags used for further processing of XML resources.
  2268. *
  2269. * Returns 0 in case of success and -1 in case of error.
  2270. */
  2271. int
  2272. xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
  2273. if (ctxt == NULL)
  2274. return(-1);
  2275. ctxt->parseFlags = flags;
  2276. return(0);
  2277. }
  2278. /**
  2279. * xmlXIncludeProcessTreeFlagsData:
  2280. * @tree: an XML node
  2281. * @flags: a set of xmlParserOption used for parsing XML includes
  2282. * @data: application data that will be passed to the parser context
  2283. * in the _private field of the parser context(s)
  2284. *
  2285. * Implement the XInclude substitution on the XML node @tree
  2286. *
  2287. * Returns 0 if no substitution were done, -1 if some processing failed
  2288. * or the number of substitutions done.
  2289. */
  2290. int
  2291. xmlXIncludeProcessTreeFlagsData(xmlNodePtr tree, int flags, void *data) {
  2292. xmlXIncludeCtxtPtr ctxt;
  2293. int ret = 0;
  2294. if ((tree == NULL) || (tree->doc == NULL))
  2295. return(-1);
  2296. ctxt = xmlXIncludeNewContext(tree->doc);
  2297. if (ctxt == NULL)
  2298. return(-1);
  2299. ctxt->_private = data;
  2300. ctxt->base = xmlStrdup((xmlChar *)tree->doc->URL);
  2301. xmlXIncludeSetFlags(ctxt, flags);
  2302. ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
  2303. if ((ret >= 0) && (ctxt->nbErrors > 0))
  2304. ret = -1;
  2305. xmlXIncludeFreeContext(ctxt);
  2306. return(ret);
  2307. }
  2308. /**
  2309. * xmlXIncludeProcessFlagsData:
  2310. * @doc: an XML document
  2311. * @flags: a set of xmlParserOption used for parsing XML includes
  2312. * @data: application data that will be passed to the parser context
  2313. * in the _private field of the parser context(s)
  2314. *
  2315. * Implement the XInclude substitution on the XML document @doc
  2316. *
  2317. * Returns 0 if no substitution were done, -1 if some processing failed
  2318. * or the number of substitutions done.
  2319. */
  2320. int
  2321. xmlXIncludeProcessFlagsData(xmlDocPtr doc, int flags, void *data) {
  2322. xmlNodePtr tree;
  2323. if (doc == NULL)
  2324. return(-1);
  2325. tree = xmlDocGetRootElement(doc);
  2326. if (tree == NULL)
  2327. return(-1);
  2328. return(xmlXIncludeProcessTreeFlagsData(tree, flags, data));
  2329. }
  2330. /**
  2331. * xmlXIncludeProcessFlags:
  2332. * @doc: an XML document
  2333. * @flags: a set of xmlParserOption used for parsing XML includes
  2334. *
  2335. * Implement the XInclude substitution on the XML document @doc
  2336. *
  2337. * Returns 0 if no substitution were done, -1 if some processing failed
  2338. * or the number of substitutions done.
  2339. */
  2340. int
  2341. xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
  2342. return xmlXIncludeProcessFlagsData(doc, flags, NULL);
  2343. }
  2344. /**
  2345. * xmlXIncludeProcess:
  2346. * @doc: an XML document
  2347. *
  2348. * Implement the XInclude substitution on the XML document @doc
  2349. *
  2350. * Returns 0 if no substitution were done, -1 if some processing failed
  2351. * or the number of substitutions done.
  2352. */
  2353. int
  2354. xmlXIncludeProcess(xmlDocPtr doc) {
  2355. return(xmlXIncludeProcessFlags(doc, 0));
  2356. }
  2357. /**
  2358. * xmlXIncludeProcessTreeFlags:
  2359. * @tree: a node in an XML document
  2360. * @flags: a set of xmlParserOption used for parsing XML includes
  2361. *
  2362. * Implement the XInclude substitution for the given subtree
  2363. *
  2364. * Returns 0 if no substitution were done, -1 if some processing failed
  2365. * or the number of substitutions done.
  2366. */
  2367. int
  2368. xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
  2369. xmlXIncludeCtxtPtr ctxt;
  2370. int ret = 0;
  2371. if ((tree == NULL) || (tree->doc == NULL))
  2372. return(-1);
  2373. ctxt = xmlXIncludeNewContext(tree->doc);
  2374. if (ctxt == NULL)
  2375. return(-1);
  2376. ctxt->base = xmlNodeGetBase(tree->doc, tree);
  2377. xmlXIncludeSetFlags(ctxt, flags);
  2378. ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
  2379. if ((ret >= 0) && (ctxt->nbErrors > 0))
  2380. ret = -1;
  2381. xmlXIncludeFreeContext(ctxt);
  2382. return(ret);
  2383. }
  2384. /**
  2385. * xmlXIncludeProcessTree:
  2386. * @tree: a node in an XML document
  2387. *
  2388. * Implement the XInclude substitution for the given subtree
  2389. *
  2390. * Returns 0 if no substitution were done, -1 if some processing failed
  2391. * or the number of substitutions done.
  2392. */
  2393. int
  2394. xmlXIncludeProcessTree(xmlNodePtr tree) {
  2395. return(xmlXIncludeProcessTreeFlags(tree, 0));
  2396. }
  2397. /**
  2398. * xmlXIncludeProcessNode:
  2399. * @ctxt: an existing XInclude context
  2400. * @node: a node in an XML document
  2401. *
  2402. * Implement the XInclude substitution for the given subtree reusing
  2403. * the informations and data coming from the given context.
  2404. *
  2405. * Returns 0 if no substitution were done, -1 if some processing failed
  2406. * or the number of substitutions done.
  2407. */
  2408. int
  2409. xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
  2410. int ret = 0;
  2411. if ((node == NULL) || (node->doc == NULL) || (ctxt == NULL))
  2412. return(-1);
  2413. ret = xmlXIncludeDoProcess(ctxt, node->doc, node);
  2414. if ((ret >= 0) && (ctxt->nbErrors > 0))
  2415. ret = -1;
  2416. return(ret);
  2417. }
  2418. #else /* !LIBXML_XINCLUDE_ENABLED */
  2419. #endif
  2420. #define bottom_xinclude
  2421. #include "elfgcchack.h"