xmlsave.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735
  1. /*
  2. * xmlsave.c: Implementation of the document serializer
  3. *
  4. * See Copyright for the status of this software.
  5. *
  6. * daniel@veillard.com
  7. */
  8. #define IN_LIBXML
  9. #include "libxml.h"
  10. #include <string.h>
  11. #include <libxml/xmlmemory.h>
  12. #include <libxml/parserInternals.h>
  13. #include <libxml/tree.h>
  14. #include <libxml/xmlsave.h>
  15. #define MAX_INDENT 60
  16. #include <libxml/HTMLtree.h>
  17. #include "buf.h"
  18. #include "enc.h"
  19. #include "save.h"
  20. /************************************************************************
  21. * *
  22. * XHTML detection *
  23. * *
  24. ************************************************************************/
  25. #define XHTML_STRICT_PUBLIC_ID BAD_CAST \
  26. "-//W3C//DTD XHTML 1.0 Strict//EN"
  27. #define XHTML_STRICT_SYSTEM_ID BAD_CAST \
  28. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
  29. #define XHTML_FRAME_PUBLIC_ID BAD_CAST \
  30. "-//W3C//DTD XHTML 1.0 Frameset//EN"
  31. #define XHTML_FRAME_SYSTEM_ID BAD_CAST \
  32. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"
  33. #define XHTML_TRANS_PUBLIC_ID BAD_CAST \
  34. "-//W3C//DTD XHTML 1.0 Transitional//EN"
  35. #define XHTML_TRANS_SYSTEM_ID BAD_CAST \
  36. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  37. #define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml"
  38. /**
  39. * xmlIsXHTML:
  40. * @systemID: the system identifier
  41. * @publicID: the public identifier
  42. *
  43. * Try to find if the document correspond to an XHTML DTD
  44. *
  45. * Returns 1 if true, 0 if not and -1 in case of error
  46. */
  47. int
  48. xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) {
  49. if ((systemID == NULL) && (publicID == NULL))
  50. return(-1);
  51. if (publicID != NULL) {
  52. if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1);
  53. if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1);
  54. if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1);
  55. }
  56. if (systemID != NULL) {
  57. if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1);
  58. if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1);
  59. if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1);
  60. }
  61. return(0);
  62. }
  63. #ifdef LIBXML_OUTPUT_ENABLED
  64. #define TODO \
  65. xmlGenericError(xmlGenericErrorContext, \
  66. "Unimplemented block at %s:%d\n", \
  67. __FILE__, __LINE__);
  68. struct _xmlSaveCtxt {
  69. void *_private;
  70. int type;
  71. int fd;
  72. const xmlChar *filename;
  73. const xmlChar *encoding;
  74. xmlCharEncodingHandlerPtr handler;
  75. xmlOutputBufferPtr buf;
  76. int options;
  77. int level;
  78. int format;
  79. char indent[MAX_INDENT + 1]; /* array for indenting output */
  80. int indent_nr;
  81. int indent_size;
  82. xmlCharEncodingOutputFunc escape; /* used for element content */
  83. xmlCharEncodingOutputFunc escapeAttr;/* used for attribute content */
  84. };
  85. /************************************************************************
  86. * *
  87. * Output error handlers *
  88. * *
  89. ************************************************************************/
  90. /**
  91. * xmlSaveErrMemory:
  92. * @extra: extra information
  93. *
  94. * Handle an out of memory condition
  95. */
  96. static void
  97. xmlSaveErrMemory(const char *extra)
  98. {
  99. __xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra);
  100. }
  101. /**
  102. * xmlSaveErr:
  103. * @code: the error number
  104. * @node: the location of the error.
  105. * @extra: extra information
  106. *
  107. * Handle an out of memory condition
  108. */
  109. static void
  110. xmlSaveErr(int code, xmlNodePtr node, const char *extra)
  111. {
  112. const char *msg = NULL;
  113. switch(code) {
  114. case XML_SAVE_NOT_UTF8:
  115. msg = "string is not in UTF-8\n";
  116. break;
  117. case XML_SAVE_CHAR_INVALID:
  118. msg = "invalid character value\n";
  119. break;
  120. case XML_SAVE_UNKNOWN_ENCODING:
  121. msg = "unknown encoding %s\n";
  122. break;
  123. case XML_SAVE_NO_DOCTYPE:
  124. msg = "document has no DOCTYPE\n";
  125. break;
  126. default:
  127. msg = "unexpected error number\n";
  128. }
  129. __xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra);
  130. }
  131. /************************************************************************
  132. * *
  133. * Special escaping routines *
  134. * *
  135. ************************************************************************/
  136. static unsigned char *
  137. xmlSerializeHexCharRef(unsigned char *out, int val) {
  138. unsigned char *ptr;
  139. *out++ = '&';
  140. *out++ = '#';
  141. *out++ = 'x';
  142. if (val < 0x10) ptr = out;
  143. else if (val < 0x100) ptr = out + 1;
  144. else if (val < 0x1000) ptr = out + 2;
  145. else if (val < 0x10000) ptr = out + 3;
  146. else if (val < 0x100000) ptr = out + 4;
  147. else ptr = out + 5;
  148. out = ptr + 1;
  149. while (val > 0) {
  150. switch (val & 0xF) {
  151. case 0: *ptr-- = '0'; break;
  152. case 1: *ptr-- = '1'; break;
  153. case 2: *ptr-- = '2'; break;
  154. case 3: *ptr-- = '3'; break;
  155. case 4: *ptr-- = '4'; break;
  156. case 5: *ptr-- = '5'; break;
  157. case 6: *ptr-- = '6'; break;
  158. case 7: *ptr-- = '7'; break;
  159. case 8: *ptr-- = '8'; break;
  160. case 9: *ptr-- = '9'; break;
  161. case 0xA: *ptr-- = 'A'; break;
  162. case 0xB: *ptr-- = 'B'; break;
  163. case 0xC: *ptr-- = 'C'; break;
  164. case 0xD: *ptr-- = 'D'; break;
  165. case 0xE: *ptr-- = 'E'; break;
  166. case 0xF: *ptr-- = 'F'; break;
  167. default: *ptr-- = '0'; break;
  168. }
  169. val >>= 4;
  170. }
  171. *out++ = ';';
  172. *out = 0;
  173. return(out);
  174. }
  175. /**
  176. * xmlEscapeEntities:
  177. * @out: a pointer to an array of bytes to store the result
  178. * @outlen: the length of @out
  179. * @in: a pointer to an array of unescaped UTF-8 bytes
  180. * @inlen: the length of @in
  181. *
  182. * Take a block of UTF-8 chars in and escape them. Used when there is no
  183. * encoding specified.
  184. *
  185. * Returns 0 if success, or -1 otherwise
  186. * The value of @inlen after return is the number of octets consumed
  187. * if the return value is positive, else unpredictable.
  188. * The value of @outlen after return is the number of octets consumed.
  189. */
  190. static int
  191. xmlEscapeEntities(unsigned char* out, int *outlen,
  192. const xmlChar* in, int *inlen) {
  193. unsigned char* outstart = out;
  194. const unsigned char* base = in;
  195. unsigned char* outend = out + *outlen;
  196. const unsigned char* inend;
  197. int val;
  198. inend = in + (*inlen);
  199. while ((in < inend) && (out < outend)) {
  200. if (*in == '<') {
  201. if (outend - out < 4) break;
  202. *out++ = '&';
  203. *out++ = 'l';
  204. *out++ = 't';
  205. *out++ = ';';
  206. in++;
  207. continue;
  208. } else if (*in == '>') {
  209. if (outend - out < 4) break;
  210. *out++ = '&';
  211. *out++ = 'g';
  212. *out++ = 't';
  213. *out++ = ';';
  214. in++;
  215. continue;
  216. } else if (*in == '&') {
  217. if (outend - out < 5) break;
  218. *out++ = '&';
  219. *out++ = 'a';
  220. *out++ = 'm';
  221. *out++ = 'p';
  222. *out++ = ';';
  223. in++;
  224. continue;
  225. } else if (((*in >= 0x20) && (*in < 0x80)) ||
  226. (*in == '\n') || (*in == '\t')) {
  227. /*
  228. * default case, just copy !
  229. */
  230. *out++ = *in++;
  231. continue;
  232. } else if (*in >= 0x80) {
  233. /*
  234. * We assume we have UTF-8 input.
  235. */
  236. if (outend - out < 11) break;
  237. if (*in < 0xC0) {
  238. xmlSaveErr(XML_SAVE_NOT_UTF8, NULL, NULL);
  239. in++;
  240. goto error;
  241. } else if (*in < 0xE0) {
  242. if (inend - in < 2) break;
  243. val = (in[0]) & 0x1F;
  244. val <<= 6;
  245. val |= (in[1]) & 0x3F;
  246. in += 2;
  247. } else if (*in < 0xF0) {
  248. if (inend - in < 3) break;
  249. val = (in[0]) & 0x0F;
  250. val <<= 6;
  251. val |= (in[1]) & 0x3F;
  252. val <<= 6;
  253. val |= (in[2]) & 0x3F;
  254. in += 3;
  255. } else if (*in < 0xF8) {
  256. if (inend - in < 4) break;
  257. val = (in[0]) & 0x07;
  258. val <<= 6;
  259. val |= (in[1]) & 0x3F;
  260. val <<= 6;
  261. val |= (in[2]) & 0x3F;
  262. val <<= 6;
  263. val |= (in[3]) & 0x3F;
  264. in += 4;
  265. } else {
  266. xmlSaveErr(XML_SAVE_CHAR_INVALID, NULL, NULL);
  267. in++;
  268. goto error;
  269. }
  270. if (!IS_CHAR(val)) {
  271. xmlSaveErr(XML_SAVE_CHAR_INVALID, NULL, NULL);
  272. in++;
  273. goto error;
  274. }
  275. /*
  276. * We could do multiple things here. Just save as a char ref
  277. */
  278. out = xmlSerializeHexCharRef(out, val);
  279. } else if (IS_BYTE_CHAR(*in)) {
  280. if (outend - out < 6) break;
  281. out = xmlSerializeHexCharRef(out, *in++);
  282. } else {
  283. xmlGenericError(xmlGenericErrorContext,
  284. "xmlEscapeEntities : char out of range\n");
  285. in++;
  286. goto error;
  287. }
  288. }
  289. *outlen = out - outstart;
  290. *inlen = in - base;
  291. return(0);
  292. error:
  293. *outlen = out - outstart;
  294. *inlen = in - base;
  295. return(-1);
  296. }
  297. /************************************************************************
  298. * *
  299. * Allocation and deallocation *
  300. * *
  301. ************************************************************************/
  302. /**
  303. * xmlSaveCtxtInit:
  304. * @ctxt: the saving context
  305. *
  306. * Initialize a saving context
  307. */
  308. static void
  309. xmlSaveCtxtInit(xmlSaveCtxtPtr ctxt)
  310. {
  311. int i;
  312. int len;
  313. if (ctxt == NULL) return;
  314. if ((ctxt->encoding == NULL) && (ctxt->escape == NULL))
  315. ctxt->escape = xmlEscapeEntities;
  316. len = xmlStrlen((xmlChar *)xmlTreeIndentString);
  317. if ((xmlTreeIndentString == NULL) || (len == 0)) {
  318. memset(&ctxt->indent[0], 0, MAX_INDENT + 1);
  319. } else {
  320. ctxt->indent_size = len;
  321. ctxt->indent_nr = MAX_INDENT / ctxt->indent_size;
  322. for (i = 0;i < ctxt->indent_nr;i++)
  323. memcpy(&ctxt->indent[i * ctxt->indent_size], xmlTreeIndentString,
  324. ctxt->indent_size);
  325. ctxt->indent[ctxt->indent_nr * ctxt->indent_size] = 0;
  326. }
  327. if (xmlSaveNoEmptyTags) {
  328. ctxt->options |= XML_SAVE_NO_EMPTY;
  329. }
  330. }
  331. /**
  332. * xmlFreeSaveCtxt:
  333. *
  334. * Free a saving context, destroying the output in any remaining buffer
  335. */
  336. static void
  337. xmlFreeSaveCtxt(xmlSaveCtxtPtr ctxt)
  338. {
  339. if (ctxt == NULL) return;
  340. if (ctxt->encoding != NULL)
  341. xmlFree((char *) ctxt->encoding);
  342. if (ctxt->buf != NULL)
  343. xmlOutputBufferClose(ctxt->buf);
  344. xmlFree(ctxt);
  345. }
  346. /**
  347. * xmlNewSaveCtxt:
  348. *
  349. * Create a new saving context
  350. *
  351. * Returns the new structure or NULL in case of error
  352. */
  353. static xmlSaveCtxtPtr
  354. xmlNewSaveCtxt(const char *encoding, int options)
  355. {
  356. xmlSaveCtxtPtr ret;
  357. ret = (xmlSaveCtxtPtr) xmlMalloc(sizeof(xmlSaveCtxt));
  358. if (ret == NULL) {
  359. xmlSaveErrMemory("creating saving context");
  360. return ( NULL );
  361. }
  362. memset(ret, 0, sizeof(xmlSaveCtxt));
  363. if (encoding != NULL) {
  364. ret->handler = xmlFindCharEncodingHandler(encoding);
  365. if (ret->handler == NULL) {
  366. xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
  367. xmlFreeSaveCtxt(ret);
  368. return(NULL);
  369. }
  370. ret->encoding = xmlStrdup((const xmlChar *)encoding);
  371. ret->escape = NULL;
  372. }
  373. xmlSaveCtxtInit(ret);
  374. /*
  375. * Use the options
  376. */
  377. /* Re-check this option as it may already have been set */
  378. if ((ret->options & XML_SAVE_NO_EMPTY) && ! (options & XML_SAVE_NO_EMPTY)) {
  379. options |= XML_SAVE_NO_EMPTY;
  380. }
  381. ret->options = options;
  382. if (options & XML_SAVE_FORMAT)
  383. ret->format = 1;
  384. else if (options & XML_SAVE_WSNONSIG)
  385. ret->format = 2;
  386. return(ret);
  387. }
  388. /************************************************************************
  389. * *
  390. * Dumping XML tree content to a simple buffer *
  391. * *
  392. ************************************************************************/
  393. /**
  394. * xmlAttrSerializeContent:
  395. * @buf: the XML buffer output
  396. * @doc: the document
  397. * @attr: the attribute pointer
  398. *
  399. * Serialize the attribute in the buffer
  400. */
  401. static void
  402. xmlAttrSerializeContent(xmlOutputBufferPtr buf, xmlAttrPtr attr)
  403. {
  404. xmlNodePtr children;
  405. children = attr->children;
  406. while (children != NULL) {
  407. switch (children->type) {
  408. case XML_TEXT_NODE:
  409. xmlBufAttrSerializeTxtContent(buf->buffer, attr->doc,
  410. attr, children->content);
  411. break;
  412. case XML_ENTITY_REF_NODE:
  413. xmlBufAdd(buf->buffer, BAD_CAST "&", 1);
  414. xmlBufAdd(buf->buffer, children->name,
  415. xmlStrlen(children->name));
  416. xmlBufAdd(buf->buffer, BAD_CAST ";", 1);
  417. break;
  418. default:
  419. /* should not happen unless we have a badly built tree */
  420. break;
  421. }
  422. children = children->next;
  423. }
  424. }
  425. /**
  426. * xmlBufDumpNotationTable:
  427. * @buf: an xmlBufPtr output
  428. * @table: A notation table
  429. *
  430. * This will dump the content of the notation table as an XML DTD definition
  431. */
  432. void
  433. xmlBufDumpNotationTable(xmlBufPtr buf, xmlNotationTablePtr table) {
  434. xmlBufferPtr buffer;
  435. buffer = xmlBufferCreate();
  436. if (buffer == NULL) {
  437. /*
  438. * TODO set the error in buf
  439. */
  440. return;
  441. }
  442. xmlDumpNotationTable(buffer, table);
  443. xmlBufMergeBuffer(buf, buffer);
  444. }
  445. /**
  446. * xmlBufDumpElementDecl:
  447. * @buf: an xmlBufPtr output
  448. * @elem: An element table
  449. *
  450. * This will dump the content of the element declaration as an XML
  451. * DTD definition
  452. */
  453. void
  454. xmlBufDumpElementDecl(xmlBufPtr buf, xmlElementPtr elem) {
  455. xmlBufferPtr buffer;
  456. buffer = xmlBufferCreate();
  457. if (buffer == NULL) {
  458. /*
  459. * TODO set the error in buf
  460. */
  461. return;
  462. }
  463. xmlDumpElementDecl(buffer, elem);
  464. xmlBufMergeBuffer(buf, buffer);
  465. }
  466. /**
  467. * xmlBufDumpAttributeDecl:
  468. * @buf: an xmlBufPtr output
  469. * @attr: An attribute declaration
  470. *
  471. * This will dump the content of the attribute declaration as an XML
  472. * DTD definition
  473. */
  474. void
  475. xmlBufDumpAttributeDecl(xmlBufPtr buf, xmlAttributePtr attr) {
  476. xmlBufferPtr buffer;
  477. buffer = xmlBufferCreate();
  478. if (buffer == NULL) {
  479. /*
  480. * TODO set the error in buf
  481. */
  482. return;
  483. }
  484. xmlDumpAttributeDecl(buffer, attr);
  485. xmlBufMergeBuffer(buf, buffer);
  486. }
  487. /**
  488. * xmlBufDumpEntityDecl:
  489. * @buf: an xmlBufPtr output
  490. * @ent: An entity table
  491. *
  492. * This will dump the content of the entity table as an XML DTD definition
  493. */
  494. void
  495. xmlBufDumpEntityDecl(xmlBufPtr buf, xmlEntityPtr ent) {
  496. xmlBufferPtr buffer;
  497. buffer = xmlBufferCreate();
  498. if (buffer == NULL) {
  499. /*
  500. * TODO set the error in buf
  501. */
  502. return;
  503. }
  504. xmlDumpEntityDecl(buffer, ent);
  505. xmlBufMergeBuffer(buf, buffer);
  506. }
  507. /************************************************************************
  508. * *
  509. * Dumping XML tree content to an I/O output buffer *
  510. * *
  511. ************************************************************************/
  512. static int xmlSaveSwitchEncoding(xmlSaveCtxtPtr ctxt, const char *encoding) {
  513. xmlOutputBufferPtr buf = ctxt->buf;
  514. if ((encoding != NULL) && (buf->encoder == NULL) && (buf->conv == NULL)) {
  515. buf->encoder = xmlFindCharEncodingHandler((const char *)encoding);
  516. if (buf->encoder == NULL) {
  517. xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL,
  518. (const char *)encoding);
  519. return(-1);
  520. }
  521. buf->conv = xmlBufCreate();
  522. if (buf->conv == NULL) {
  523. xmlCharEncCloseFunc(buf->encoder);
  524. xmlSaveErrMemory("creating encoding buffer");
  525. return(-1);
  526. }
  527. /*
  528. * initialize the state, e.g. if outputting a BOM
  529. */
  530. xmlCharEncOutput(buf, 1);
  531. }
  532. return(0);
  533. }
  534. static int xmlSaveClearEncoding(xmlSaveCtxtPtr ctxt) {
  535. xmlOutputBufferPtr buf = ctxt->buf;
  536. xmlOutputBufferFlush(buf);
  537. xmlCharEncCloseFunc(buf->encoder);
  538. xmlBufFree(buf->conv);
  539. buf->encoder = NULL;
  540. buf->conv = NULL;
  541. return(0);
  542. }
  543. #ifdef LIBXML_HTML_ENABLED
  544. static void
  545. xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
  546. #endif
  547. static void xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
  548. void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur);
  549. static int xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur);
  550. /**
  551. * xmlOutputBufferWriteWSNonSig:
  552. * @ctxt: The save context
  553. * @extra: Number of extra indents to apply to ctxt->level
  554. *
  555. * Write out formatting for non-significant whitespace output.
  556. */
  557. static void
  558. xmlOutputBufferWriteWSNonSig(xmlSaveCtxtPtr ctxt, int extra)
  559. {
  560. int i;
  561. if ((ctxt == NULL) || (ctxt->buf == NULL))
  562. return;
  563. xmlOutputBufferWrite(ctxt->buf, 1, "\n");
  564. for (i = 0; i < (ctxt->level + extra); i += ctxt->indent_nr) {
  565. xmlOutputBufferWrite(ctxt->buf, ctxt->indent_size *
  566. ((ctxt->level + extra - i) > ctxt->indent_nr ?
  567. ctxt->indent_nr : (ctxt->level + extra - i)),
  568. ctxt->indent);
  569. }
  570. }
  571. /**
  572. * xmlNsDumpOutput:
  573. * @buf: the XML buffer output
  574. * @cur: a namespace
  575. * @ctxt: the output save context. Optional.
  576. *
  577. * Dump a local Namespace definition.
  578. * Should be called in the context of attributes dumps.
  579. * If @ctxt is supplied, @buf should be its buffer.
  580. */
  581. static void
  582. xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur, xmlSaveCtxtPtr ctxt) {
  583. if ((cur == NULL) || (buf == NULL)) return;
  584. if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) {
  585. if (xmlStrEqual(cur->prefix, BAD_CAST "xml"))
  586. return;
  587. if (ctxt != NULL && ctxt->format == 2)
  588. xmlOutputBufferWriteWSNonSig(ctxt, 2);
  589. else
  590. xmlOutputBufferWrite(buf, 1, " ");
  591. /* Within the context of an element attributes */
  592. if (cur->prefix != NULL) {
  593. xmlOutputBufferWrite(buf, 6, "xmlns:");
  594. xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
  595. } else
  596. xmlOutputBufferWrite(buf, 5, "xmlns");
  597. xmlOutputBufferWrite(buf, 1, "=");
  598. xmlBufWriteQuotedString(buf->buffer, cur->href);
  599. }
  600. }
  601. /**
  602. * xmlNsDumpOutputCtxt
  603. * @ctxt: the save context
  604. * @cur: a namespace
  605. *
  606. * Dump a local Namespace definition to a save context.
  607. * Should be called in the context of attribute dumps.
  608. */
  609. static void
  610. xmlNsDumpOutputCtxt(xmlSaveCtxtPtr ctxt, xmlNsPtr cur) {
  611. xmlNsDumpOutput(ctxt->buf, cur, ctxt);
  612. }
  613. /**
  614. * xmlNsListDumpOutputCtxt
  615. * @ctxt: the save context
  616. * @cur: the first namespace
  617. *
  618. * Dump a list of local namespace definitions to a save context.
  619. * Should be called in the context of attribute dumps.
  620. */
  621. static void
  622. xmlNsListDumpOutputCtxt(xmlSaveCtxtPtr ctxt, xmlNsPtr cur) {
  623. while (cur != NULL) {
  624. xmlNsDumpOutput(ctxt->buf, cur, ctxt);
  625. cur = cur->next;
  626. }
  627. }
  628. /**
  629. * xmlNsListDumpOutput:
  630. * @buf: the XML buffer output
  631. * @cur: the first namespace
  632. *
  633. * Dump a list of local Namespace definitions.
  634. * Should be called in the context of attributes dumps.
  635. */
  636. void
  637. xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
  638. while (cur != NULL) {
  639. xmlNsDumpOutput(buf, cur, NULL);
  640. cur = cur->next;
  641. }
  642. }
  643. /**
  644. * xmlDtdDumpOutput:
  645. * @buf: the XML buffer output
  646. * @dtd: the pointer to the DTD
  647. *
  648. * Dump the XML document DTD, if any.
  649. */
  650. static void
  651. xmlDtdDumpOutput(xmlSaveCtxtPtr ctxt, xmlDtdPtr dtd) {
  652. xmlOutputBufferPtr buf;
  653. xmlNodePtr cur;
  654. int format, level;
  655. if (dtd == NULL) return;
  656. if ((ctxt == NULL) || (ctxt->buf == NULL))
  657. return;
  658. buf = ctxt->buf;
  659. xmlOutputBufferWrite(buf, 10, "<!DOCTYPE ");
  660. xmlOutputBufferWriteString(buf, (const char *)dtd->name);
  661. if (dtd->ExternalID != NULL) {
  662. xmlOutputBufferWrite(buf, 8, " PUBLIC ");
  663. xmlBufWriteQuotedString(buf->buffer, dtd->ExternalID);
  664. xmlOutputBufferWrite(buf, 1, " ");
  665. xmlBufWriteQuotedString(buf->buffer, dtd->SystemID);
  666. } else if (dtd->SystemID != NULL) {
  667. xmlOutputBufferWrite(buf, 8, " SYSTEM ");
  668. xmlBufWriteQuotedString(buf->buffer, dtd->SystemID);
  669. }
  670. if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
  671. (dtd->attributes == NULL) && (dtd->notations == NULL) &&
  672. (dtd->pentities == NULL)) {
  673. xmlOutputBufferWrite(buf, 1, ">");
  674. return;
  675. }
  676. xmlOutputBufferWrite(buf, 3, " [\n");
  677. /*
  678. * Dump the notations first they are not in the DTD children list
  679. * Do this only on a standalone DTD or on the internal subset though.
  680. */
  681. if ((dtd->notations != NULL) && ((dtd->doc == NULL) ||
  682. (dtd->doc->intSubset == dtd))) {
  683. xmlBufDumpNotationTable(buf->buffer,
  684. (xmlNotationTablePtr) dtd->notations);
  685. }
  686. format = ctxt->format;
  687. level = ctxt->level;
  688. ctxt->format = 0;
  689. ctxt->level = -1;
  690. for (cur = dtd->children; cur != NULL; cur = cur->next) {
  691. xmlNodeDumpOutputInternal(ctxt, cur);
  692. }
  693. ctxt->format = format;
  694. ctxt->level = level;
  695. xmlOutputBufferWrite(buf, 2, "]>");
  696. }
  697. /**
  698. * xmlAttrDumpOutput:
  699. * @buf: the XML buffer output
  700. * @cur: the attribute pointer
  701. *
  702. * Dump an XML attribute
  703. */
  704. static void
  705. xmlAttrDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
  706. xmlOutputBufferPtr buf;
  707. if (cur == NULL) return;
  708. buf = ctxt->buf;
  709. if (buf == NULL) return;
  710. if (ctxt->format == 2)
  711. xmlOutputBufferWriteWSNonSig(ctxt, 2);
  712. else
  713. xmlOutputBufferWrite(buf, 1, " ");
  714. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  715. xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
  716. xmlOutputBufferWrite(buf, 1, ":");
  717. }
  718. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  719. xmlOutputBufferWrite(buf, 2, "=\"");
  720. xmlAttrSerializeContent(buf, cur);
  721. xmlOutputBufferWrite(buf, 1, "\"");
  722. }
  723. #ifdef LIBXML_HTML_ENABLED
  724. /**
  725. * htmlNodeDumpOutputInternal:
  726. * @cur: the current node
  727. *
  728. * Dump an HTML node, recursive behaviour, children are printed too.
  729. */
  730. static int
  731. htmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
  732. const xmlChar *oldenc = NULL;
  733. const xmlChar *oldctxtenc = ctxt->encoding;
  734. const xmlChar *encoding = ctxt->encoding;
  735. xmlOutputBufferPtr buf = ctxt->buf;
  736. int switched_encoding = 0;
  737. xmlDocPtr doc;
  738. xmlInitParser();
  739. doc = cur->doc;
  740. if (doc != NULL) {
  741. oldenc = doc->encoding;
  742. if (ctxt->encoding != NULL) {
  743. doc->encoding = BAD_CAST ctxt->encoding;
  744. } else if (doc->encoding != NULL) {
  745. encoding = doc->encoding;
  746. }
  747. }
  748. if ((encoding != NULL) && (doc != NULL))
  749. htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
  750. if ((encoding == NULL) && (doc != NULL))
  751. encoding = htmlGetMetaEncoding(doc);
  752. if (encoding == NULL)
  753. encoding = BAD_CAST "HTML";
  754. if ((encoding != NULL) && (oldctxtenc == NULL) &&
  755. (buf->encoder == NULL) && (buf->conv == NULL)) {
  756. if (xmlSaveSwitchEncoding(ctxt, (const char*) encoding) < 0) {
  757. doc->encoding = oldenc;
  758. return(-1);
  759. }
  760. switched_encoding = 1;
  761. }
  762. if (ctxt->options & XML_SAVE_FORMAT)
  763. htmlNodeDumpFormatOutput(buf, doc, cur,
  764. (const char *)encoding, 1);
  765. else
  766. htmlNodeDumpFormatOutput(buf, doc, cur,
  767. (const char *)encoding, 0);
  768. /*
  769. * Restore the state of the saving context at the end of the document
  770. */
  771. if ((switched_encoding) && (oldctxtenc == NULL)) {
  772. xmlSaveClearEncoding(ctxt);
  773. }
  774. if (doc != NULL)
  775. doc->encoding = oldenc;
  776. return(0);
  777. }
  778. #endif
  779. /**
  780. * xmlNodeDumpOutputInternal:
  781. * @cur: the current node
  782. *
  783. * Dump an XML node, recursive behaviour, children are printed too.
  784. */
  785. static void
  786. xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
  787. int format = ctxt->format;
  788. xmlNodePtr tmp, root, unformattedNode = NULL, parent;
  789. xmlAttrPtr attr;
  790. xmlChar *start, *end;
  791. xmlOutputBufferPtr buf;
  792. if (cur == NULL) return;
  793. buf = ctxt->buf;
  794. root = cur;
  795. parent = cur->parent;
  796. while (1) {
  797. switch (cur->type) {
  798. case XML_DOCUMENT_NODE:
  799. case XML_HTML_DOCUMENT_NODE:
  800. xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur);
  801. break;
  802. case XML_DTD_NODE:
  803. xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
  804. break;
  805. case XML_DOCUMENT_FRAG_NODE:
  806. /* Always validate cur->parent when descending. */
  807. if ((cur->parent == parent) && (cur->children != NULL)) {
  808. parent = cur;
  809. cur = cur->children;
  810. continue;
  811. }
  812. break;
  813. case XML_ELEMENT_DECL:
  814. xmlBufDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
  815. break;
  816. case XML_ATTRIBUTE_DECL:
  817. xmlBufDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
  818. break;
  819. case XML_ENTITY_DECL:
  820. xmlBufDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
  821. break;
  822. case XML_ELEMENT_NODE:
  823. if ((cur != root) && (ctxt->format == 1) &&
  824. (xmlIndentTreeOutput))
  825. xmlOutputBufferWrite(buf, ctxt->indent_size *
  826. (ctxt->level > ctxt->indent_nr ?
  827. ctxt->indent_nr : ctxt->level),
  828. ctxt->indent);
  829. /*
  830. * Some users like lxml are known to pass nodes with a corrupted
  831. * tree structure. Fall back to a recursive call to handle this
  832. * case.
  833. */
  834. if ((cur->parent != parent) && (cur->children != NULL)) {
  835. xmlNodeDumpOutputInternal(ctxt, cur);
  836. break;
  837. }
  838. xmlOutputBufferWrite(buf, 1, "<");
  839. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  840. xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
  841. xmlOutputBufferWrite(buf, 1, ":");
  842. }
  843. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  844. if (cur->nsDef)
  845. xmlNsListDumpOutputCtxt(ctxt, cur->nsDef);
  846. for (attr = cur->properties; attr != NULL; attr = attr->next)
  847. xmlAttrDumpOutput(ctxt, attr);
  848. if (cur->children == NULL) {
  849. if ((ctxt->options & XML_SAVE_NO_EMPTY) == 0) {
  850. if (ctxt->format == 2)
  851. xmlOutputBufferWriteWSNonSig(ctxt, 0);
  852. xmlOutputBufferWrite(buf, 2, "/>");
  853. } else {
  854. if (ctxt->format == 2)
  855. xmlOutputBufferWriteWSNonSig(ctxt, 1);
  856. xmlOutputBufferWrite(buf, 3, "></");
  857. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  858. xmlOutputBufferWriteString(buf,
  859. (const char *)cur->ns->prefix);
  860. xmlOutputBufferWrite(buf, 1, ":");
  861. }
  862. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  863. if (ctxt->format == 2)
  864. xmlOutputBufferWriteWSNonSig(ctxt, 0);
  865. xmlOutputBufferWrite(buf, 1, ">");
  866. }
  867. } else {
  868. if (ctxt->format == 1) {
  869. tmp = cur->children;
  870. while (tmp != NULL) {
  871. if ((tmp->type == XML_TEXT_NODE) ||
  872. (tmp->type == XML_CDATA_SECTION_NODE) ||
  873. (tmp->type == XML_ENTITY_REF_NODE)) {
  874. ctxt->format = 0;
  875. unformattedNode = cur;
  876. break;
  877. }
  878. tmp = tmp->next;
  879. }
  880. }
  881. if (ctxt->format == 2)
  882. xmlOutputBufferWriteWSNonSig(ctxt, 1);
  883. xmlOutputBufferWrite(buf, 1, ">");
  884. if (ctxt->format == 1) xmlOutputBufferWrite(buf, 1, "\n");
  885. if (ctxt->level >= 0) ctxt->level++;
  886. parent = cur;
  887. cur = cur->children;
  888. continue;
  889. }
  890. break;
  891. case XML_TEXT_NODE:
  892. if (cur->content == NULL)
  893. break;
  894. if (cur->name != xmlStringTextNoenc) {
  895. xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
  896. } else {
  897. /*
  898. * Disable escaping, needed for XSLT
  899. */
  900. xmlOutputBufferWriteString(buf, (const char *) cur->content);
  901. }
  902. break;
  903. case XML_PI_NODE:
  904. if ((cur != root) && (ctxt->format == 1) && (xmlIndentTreeOutput))
  905. xmlOutputBufferWrite(buf, ctxt->indent_size *
  906. (ctxt->level > ctxt->indent_nr ?
  907. ctxt->indent_nr : ctxt->level),
  908. ctxt->indent);
  909. if (cur->content != NULL) {
  910. xmlOutputBufferWrite(buf, 2, "<?");
  911. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  912. if (cur->content != NULL) {
  913. if (ctxt->format == 2)
  914. xmlOutputBufferWriteWSNonSig(ctxt, 0);
  915. else
  916. xmlOutputBufferWrite(buf, 1, " ");
  917. xmlOutputBufferWriteString(buf,
  918. (const char *)cur->content);
  919. }
  920. xmlOutputBufferWrite(buf, 2, "?>");
  921. } else {
  922. xmlOutputBufferWrite(buf, 2, "<?");
  923. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  924. if (ctxt->format == 2)
  925. xmlOutputBufferWriteWSNonSig(ctxt, 0);
  926. xmlOutputBufferWrite(buf, 2, "?>");
  927. }
  928. break;
  929. case XML_COMMENT_NODE:
  930. if ((cur != root) && (ctxt->format == 1) && (xmlIndentTreeOutput))
  931. xmlOutputBufferWrite(buf, ctxt->indent_size *
  932. (ctxt->level > ctxt->indent_nr ?
  933. ctxt->indent_nr : ctxt->level),
  934. ctxt->indent);
  935. if (cur->content != NULL) {
  936. xmlOutputBufferWrite(buf, 4, "<!--");
  937. xmlOutputBufferWriteString(buf, (const char *)cur->content);
  938. xmlOutputBufferWrite(buf, 3, "-->");
  939. }
  940. break;
  941. case XML_ENTITY_REF_NODE:
  942. xmlOutputBufferWrite(buf, 1, "&");
  943. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  944. xmlOutputBufferWrite(buf, 1, ";");
  945. break;
  946. case XML_CDATA_SECTION_NODE:
  947. if (cur->content == NULL || *cur->content == '\0') {
  948. xmlOutputBufferWrite(buf, 12, "<![CDATA[]]>");
  949. } else {
  950. start = end = cur->content;
  951. while (*end != '\0') {
  952. if ((*end == ']') && (*(end + 1) == ']') &&
  953. (*(end + 2) == '>')) {
  954. end = end + 2;
  955. xmlOutputBufferWrite(buf, 9, "<![CDATA[");
  956. xmlOutputBufferWrite(buf, end - start,
  957. (const char *)start);
  958. xmlOutputBufferWrite(buf, 3, "]]>");
  959. start = end;
  960. }
  961. end++;
  962. }
  963. if (start != end) {
  964. xmlOutputBufferWrite(buf, 9, "<![CDATA[");
  965. xmlOutputBufferWriteString(buf, (const char *)start);
  966. xmlOutputBufferWrite(buf, 3, "]]>");
  967. }
  968. }
  969. break;
  970. case XML_ATTRIBUTE_NODE:
  971. xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur);
  972. break;
  973. case XML_NAMESPACE_DECL:
  974. xmlNsDumpOutputCtxt(ctxt, (xmlNsPtr) cur);
  975. break;
  976. default:
  977. break;
  978. }
  979. while (1) {
  980. if (cur == root)
  981. return;
  982. if ((ctxt->format == 1) &&
  983. (cur->type != XML_XINCLUDE_START) &&
  984. (cur->type != XML_XINCLUDE_END))
  985. xmlOutputBufferWrite(buf, 1, "\n");
  986. if (cur->next != NULL) {
  987. cur = cur->next;
  988. break;
  989. }
  990. cur = parent;
  991. /* cur->parent was validated when descending. */
  992. parent = cur->parent;
  993. if (cur->type == XML_ELEMENT_NODE) {
  994. if (ctxt->level > 0) ctxt->level--;
  995. if ((xmlIndentTreeOutput) && (ctxt->format == 1))
  996. xmlOutputBufferWrite(buf, ctxt->indent_size *
  997. (ctxt->level > ctxt->indent_nr ?
  998. ctxt->indent_nr : ctxt->level),
  999. ctxt->indent);
  1000. xmlOutputBufferWrite(buf, 2, "</");
  1001. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  1002. xmlOutputBufferWriteString(buf,
  1003. (const char *)cur->ns->prefix);
  1004. xmlOutputBufferWrite(buf, 1, ":");
  1005. }
  1006. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  1007. if (ctxt->format == 2)
  1008. xmlOutputBufferWriteWSNonSig(ctxt, 0);
  1009. xmlOutputBufferWrite(buf, 1, ">");
  1010. if (cur == unformattedNode) {
  1011. ctxt->format = format;
  1012. unformattedNode = NULL;
  1013. }
  1014. }
  1015. }
  1016. }
  1017. }
  1018. /**
  1019. * xmlDocContentDumpOutput:
  1020. * @cur: the document
  1021. *
  1022. * Dump an XML document.
  1023. */
  1024. static int
  1025. xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur) {
  1026. #ifdef LIBXML_HTML_ENABLED
  1027. xmlDtdPtr dtd;
  1028. int is_xhtml = 0;
  1029. #endif
  1030. const xmlChar *oldenc = cur->encoding;
  1031. const xmlChar *oldctxtenc = ctxt->encoding;
  1032. const xmlChar *encoding = ctxt->encoding;
  1033. xmlCharEncodingOutputFunc oldescape = ctxt->escape;
  1034. xmlCharEncodingOutputFunc oldescapeAttr = ctxt->escapeAttr;
  1035. xmlOutputBufferPtr buf = ctxt->buf;
  1036. xmlCharEncoding enc;
  1037. int switched_encoding = 0;
  1038. xmlInitParser();
  1039. if ((cur->type != XML_HTML_DOCUMENT_NODE) &&
  1040. (cur->type != XML_DOCUMENT_NODE))
  1041. return(-1);
  1042. if (ctxt->encoding != NULL) {
  1043. cur->encoding = BAD_CAST ctxt->encoding;
  1044. } else if (cur->encoding != NULL) {
  1045. encoding = cur->encoding;
  1046. }
  1047. if (((cur->type == XML_HTML_DOCUMENT_NODE) &&
  1048. ((ctxt->options & XML_SAVE_AS_XML) == 0) &&
  1049. ((ctxt->options & XML_SAVE_XHTML) == 0)) ||
  1050. (ctxt->options & XML_SAVE_AS_HTML)) {
  1051. #ifdef LIBXML_HTML_ENABLED
  1052. if (encoding != NULL)
  1053. htmlSetMetaEncoding(cur, (const xmlChar *) encoding);
  1054. if (encoding == NULL)
  1055. encoding = htmlGetMetaEncoding(cur);
  1056. if (encoding == NULL)
  1057. encoding = BAD_CAST "HTML";
  1058. if ((encoding != NULL) && (oldctxtenc == NULL) &&
  1059. (buf->encoder == NULL) && (buf->conv == NULL)) {
  1060. if (xmlSaveSwitchEncoding(ctxt, (const char*) encoding) < 0) {
  1061. cur->encoding = oldenc;
  1062. return(-1);
  1063. }
  1064. }
  1065. if (ctxt->options & XML_SAVE_FORMAT)
  1066. htmlDocContentDumpFormatOutput(buf, cur,
  1067. (const char *)encoding, 1);
  1068. else
  1069. htmlDocContentDumpFormatOutput(buf, cur,
  1070. (const char *)encoding, 0);
  1071. if (ctxt->encoding != NULL)
  1072. cur->encoding = oldenc;
  1073. return(0);
  1074. #else
  1075. return(-1);
  1076. #endif
  1077. } else if ((cur->type == XML_DOCUMENT_NODE) ||
  1078. (ctxt->options & XML_SAVE_AS_XML) ||
  1079. (ctxt->options & XML_SAVE_XHTML)) {
  1080. enc = xmlParseCharEncoding((const char*) encoding);
  1081. if ((encoding != NULL) && (oldctxtenc == NULL) &&
  1082. (buf->encoder == NULL) && (buf->conv == NULL) &&
  1083. ((ctxt->options & XML_SAVE_NO_DECL) == 0)) {
  1084. if ((enc != XML_CHAR_ENCODING_UTF8) &&
  1085. (enc != XML_CHAR_ENCODING_NONE) &&
  1086. (enc != XML_CHAR_ENCODING_ASCII)) {
  1087. /*
  1088. * we need to switch to this encoding but just for this
  1089. * document since we output the XMLDecl the conversion
  1090. * must be done to not generate not well formed documents.
  1091. */
  1092. if (xmlSaveSwitchEncoding(ctxt, (const char*) encoding) < 0) {
  1093. cur->encoding = oldenc;
  1094. return(-1);
  1095. }
  1096. switched_encoding = 1;
  1097. }
  1098. if (ctxt->escape == xmlEscapeEntities)
  1099. ctxt->escape = NULL;
  1100. if (ctxt->escapeAttr == xmlEscapeEntities)
  1101. ctxt->escapeAttr = NULL;
  1102. }
  1103. /*
  1104. * Save the XML declaration
  1105. */
  1106. if ((ctxt->options & XML_SAVE_NO_DECL) == 0) {
  1107. xmlOutputBufferWrite(buf, 14, "<?xml version=");
  1108. if (cur->version != NULL)
  1109. xmlBufWriteQuotedString(buf->buffer, cur->version);
  1110. else
  1111. xmlOutputBufferWrite(buf, 5, "\"1.0\"");
  1112. if (encoding != NULL) {
  1113. xmlOutputBufferWrite(buf, 10, " encoding=");
  1114. xmlBufWriteQuotedString(buf->buffer, (xmlChar *) encoding);
  1115. }
  1116. switch (cur->standalone) {
  1117. case 0:
  1118. xmlOutputBufferWrite(buf, 16, " standalone=\"no\"");
  1119. break;
  1120. case 1:
  1121. xmlOutputBufferWrite(buf, 17, " standalone=\"yes\"");
  1122. break;
  1123. }
  1124. xmlOutputBufferWrite(buf, 3, "?>\n");
  1125. }
  1126. #ifdef LIBXML_HTML_ENABLED
  1127. if (ctxt->options & XML_SAVE_XHTML)
  1128. is_xhtml = 1;
  1129. if ((ctxt->options & XML_SAVE_NO_XHTML) == 0) {
  1130. dtd = xmlGetIntSubset(cur);
  1131. if (dtd != NULL) {
  1132. is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
  1133. if (is_xhtml < 0) is_xhtml = 0;
  1134. }
  1135. }
  1136. #endif
  1137. if (cur->children != NULL) {
  1138. xmlNodePtr child = cur->children;
  1139. while (child != NULL) {
  1140. ctxt->level = 0;
  1141. #ifdef LIBXML_HTML_ENABLED
  1142. if (is_xhtml)
  1143. xhtmlNodeDumpOutput(ctxt, child);
  1144. else
  1145. #endif
  1146. xmlNodeDumpOutputInternal(ctxt, child);
  1147. if ((child->type != XML_XINCLUDE_START) &&
  1148. (child->type != XML_XINCLUDE_END))
  1149. xmlOutputBufferWrite(buf, 1, "\n");
  1150. child = child->next;
  1151. }
  1152. }
  1153. }
  1154. /*
  1155. * Restore the state of the saving context at the end of the document
  1156. */
  1157. if ((switched_encoding) && (oldctxtenc == NULL)) {
  1158. xmlSaveClearEncoding(ctxt);
  1159. ctxt->escape = oldescape;
  1160. ctxt->escapeAttr = oldescapeAttr;
  1161. }
  1162. cur->encoding = oldenc;
  1163. return(0);
  1164. }
  1165. #ifdef LIBXML_HTML_ENABLED
  1166. /************************************************************************
  1167. * *
  1168. * Functions specific to XHTML serialization *
  1169. * *
  1170. ************************************************************************/
  1171. /**
  1172. * xhtmlIsEmpty:
  1173. * @node: the node
  1174. *
  1175. * Check if a node is an empty xhtml node
  1176. *
  1177. * Returns 1 if the node is an empty node, 0 if not and -1 in case of error
  1178. */
  1179. static int
  1180. xhtmlIsEmpty(xmlNodePtr node) {
  1181. if (node == NULL)
  1182. return(-1);
  1183. if (node->type != XML_ELEMENT_NODE)
  1184. return(0);
  1185. if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME)))
  1186. return(0);
  1187. if (node->children != NULL)
  1188. return(0);
  1189. switch (node->name[0]) {
  1190. case 'a':
  1191. if (xmlStrEqual(node->name, BAD_CAST "area"))
  1192. return(1);
  1193. return(0);
  1194. case 'b':
  1195. if (xmlStrEqual(node->name, BAD_CAST "br"))
  1196. return(1);
  1197. if (xmlStrEqual(node->name, BAD_CAST "base"))
  1198. return(1);
  1199. if (xmlStrEqual(node->name, BAD_CAST "basefont"))
  1200. return(1);
  1201. return(0);
  1202. case 'c':
  1203. if (xmlStrEqual(node->name, BAD_CAST "col"))
  1204. return(1);
  1205. return(0);
  1206. case 'f':
  1207. if (xmlStrEqual(node->name, BAD_CAST "frame"))
  1208. return(1);
  1209. return(0);
  1210. case 'h':
  1211. if (xmlStrEqual(node->name, BAD_CAST "hr"))
  1212. return(1);
  1213. return(0);
  1214. case 'i':
  1215. if (xmlStrEqual(node->name, BAD_CAST "img"))
  1216. return(1);
  1217. if (xmlStrEqual(node->name, BAD_CAST "input"))
  1218. return(1);
  1219. if (xmlStrEqual(node->name, BAD_CAST "isindex"))
  1220. return(1);
  1221. return(0);
  1222. case 'l':
  1223. if (xmlStrEqual(node->name, BAD_CAST "link"))
  1224. return(1);
  1225. return(0);
  1226. case 'm':
  1227. if (xmlStrEqual(node->name, BAD_CAST "meta"))
  1228. return(1);
  1229. return(0);
  1230. case 'p':
  1231. if (xmlStrEqual(node->name, BAD_CAST "param"))
  1232. return(1);
  1233. return(0);
  1234. }
  1235. return(0);
  1236. }
  1237. /**
  1238. * xhtmlAttrListDumpOutput:
  1239. * @cur: the first attribute pointer
  1240. *
  1241. * Dump a list of XML attributes
  1242. */
  1243. static void
  1244. xhtmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
  1245. xmlAttrPtr xml_lang = NULL;
  1246. xmlAttrPtr lang = NULL;
  1247. xmlAttrPtr name = NULL;
  1248. xmlAttrPtr id = NULL;
  1249. xmlNodePtr parent;
  1250. xmlOutputBufferPtr buf;
  1251. if (cur == NULL) return;
  1252. buf = ctxt->buf;
  1253. parent = cur->parent;
  1254. while (cur != NULL) {
  1255. if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id")))
  1256. id = cur;
  1257. else
  1258. if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name")))
  1259. name = cur;
  1260. else
  1261. if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")))
  1262. lang = cur;
  1263. else
  1264. if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) &&
  1265. (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml")))
  1266. xml_lang = cur;
  1267. else if ((cur->ns == NULL) &&
  1268. ((cur->children == NULL) ||
  1269. (cur->children->content == NULL) ||
  1270. (cur->children->content[0] == 0)) &&
  1271. (htmlIsBooleanAttr(cur->name))) {
  1272. if (cur->children != NULL)
  1273. xmlFreeNode(cur->children);
  1274. cur->children = xmlNewText(cur->name);
  1275. if (cur->children != NULL)
  1276. cur->children->parent = (xmlNodePtr) cur;
  1277. }
  1278. xmlAttrDumpOutput(ctxt, cur);
  1279. cur = cur->next;
  1280. }
  1281. /*
  1282. * C.8
  1283. */
  1284. if ((name != NULL) && (id == NULL)) {
  1285. if ((parent != NULL) && (parent->name != NULL) &&
  1286. ((xmlStrEqual(parent->name, BAD_CAST "a")) ||
  1287. (xmlStrEqual(parent->name, BAD_CAST "p")) ||
  1288. (xmlStrEqual(parent->name, BAD_CAST "div")) ||
  1289. (xmlStrEqual(parent->name, BAD_CAST "img")) ||
  1290. (xmlStrEqual(parent->name, BAD_CAST "map")) ||
  1291. (xmlStrEqual(parent->name, BAD_CAST "applet")) ||
  1292. (xmlStrEqual(parent->name, BAD_CAST "form")) ||
  1293. (xmlStrEqual(parent->name, BAD_CAST "frame")) ||
  1294. (xmlStrEqual(parent->name, BAD_CAST "iframe")))) {
  1295. xmlOutputBufferWrite(buf, 5, " id=\"");
  1296. xmlAttrSerializeContent(buf, name);
  1297. xmlOutputBufferWrite(buf, 1, "\"");
  1298. }
  1299. }
  1300. /*
  1301. * C.7.
  1302. */
  1303. if ((lang != NULL) && (xml_lang == NULL)) {
  1304. xmlOutputBufferWrite(buf, 11, " xml:lang=\"");
  1305. xmlAttrSerializeContent(buf, lang);
  1306. xmlOutputBufferWrite(buf, 1, "\"");
  1307. } else
  1308. if ((xml_lang != NULL) && (lang == NULL)) {
  1309. xmlOutputBufferWrite(buf, 7, " lang=\"");
  1310. xmlAttrSerializeContent(buf, xml_lang);
  1311. xmlOutputBufferWrite(buf, 1, "\"");
  1312. }
  1313. }
  1314. /**
  1315. * xhtmlNodeDumpOutput:
  1316. * @buf: the XML buffer output
  1317. * @doc: the XHTML document
  1318. * @cur: the current node
  1319. * @level: the imbrication level for indenting
  1320. * @format: is formatting allowed
  1321. * @encoding: an optional encoding string
  1322. *
  1323. * Dump an XHTML node, recursive behaviour, children are printed too.
  1324. */
  1325. static void
  1326. xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
  1327. int format = ctxt->format, addmeta;
  1328. xmlNodePtr tmp, root, unformattedNode = NULL;
  1329. xmlChar *start, *end;
  1330. xmlOutputBufferPtr buf = ctxt->buf;
  1331. if (cur == NULL) return;
  1332. root = cur;
  1333. while (1) {
  1334. switch (cur->type) {
  1335. case XML_DOCUMENT_NODE:
  1336. case XML_HTML_DOCUMENT_NODE:
  1337. xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur);
  1338. break;
  1339. case XML_NAMESPACE_DECL:
  1340. xmlNsDumpOutputCtxt(ctxt, (xmlNsPtr) cur);
  1341. break;
  1342. case XML_DTD_NODE:
  1343. xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
  1344. break;
  1345. case XML_DOCUMENT_FRAG_NODE:
  1346. if (cur->children) {
  1347. cur = cur->children;
  1348. continue;
  1349. }
  1350. break;
  1351. case XML_ELEMENT_DECL:
  1352. xmlBufDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
  1353. break;
  1354. case XML_ATTRIBUTE_DECL:
  1355. xmlBufDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
  1356. break;
  1357. case XML_ENTITY_DECL:
  1358. xmlBufDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
  1359. break;
  1360. case XML_ELEMENT_NODE:
  1361. addmeta = 0;
  1362. if ((cur != root) && (ctxt->format == 1) && (xmlIndentTreeOutput))
  1363. xmlOutputBufferWrite(buf, ctxt->indent_size *
  1364. (ctxt->level > ctxt->indent_nr ?
  1365. ctxt->indent_nr : ctxt->level),
  1366. ctxt->indent);
  1367. xmlOutputBufferWrite(buf, 1, "<");
  1368. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  1369. xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
  1370. xmlOutputBufferWrite(buf, 1, ":");
  1371. }
  1372. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  1373. if (cur->nsDef)
  1374. xmlNsListDumpOutputCtxt(ctxt, cur->nsDef);
  1375. if ((xmlStrEqual(cur->name, BAD_CAST "html") &&
  1376. (cur->ns == NULL) && (cur->nsDef == NULL))) {
  1377. /*
  1378. * 3.1.1. Strictly Conforming Documents A.3.1.1 3/
  1379. */
  1380. xmlOutputBufferWriteString(buf,
  1381. " xmlns=\"http://www.w3.org/1999/xhtml\"");
  1382. }
  1383. if (cur->properties != NULL)
  1384. xhtmlAttrListDumpOutput(ctxt, cur->properties);
  1385. if ((cur->parent != NULL) &&
  1386. (cur->parent->parent == (xmlNodePtr) cur->doc) &&
  1387. xmlStrEqual(cur->name, BAD_CAST"head") &&
  1388. xmlStrEqual(cur->parent->name, BAD_CAST"html")) {
  1389. tmp = cur->children;
  1390. while (tmp != NULL) {
  1391. if (xmlStrEqual(tmp->name, BAD_CAST"meta")) {
  1392. xmlChar *httpequiv;
  1393. httpequiv = xmlGetProp(tmp, BAD_CAST"http-equiv");
  1394. if (httpequiv != NULL) {
  1395. if (xmlStrcasecmp(httpequiv,
  1396. BAD_CAST"Content-Type") == 0) {
  1397. xmlFree(httpequiv);
  1398. break;
  1399. }
  1400. xmlFree(httpequiv);
  1401. }
  1402. }
  1403. tmp = tmp->next;
  1404. }
  1405. if (tmp == NULL)
  1406. addmeta = 1;
  1407. }
  1408. if (cur->children == NULL) {
  1409. if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) &&
  1410. ((xhtmlIsEmpty(cur) == 1) && (addmeta == 0))) {
  1411. /*
  1412. * C.2. Empty Elements
  1413. */
  1414. xmlOutputBufferWrite(buf, 3, " />");
  1415. } else {
  1416. if (addmeta == 1) {
  1417. xmlOutputBufferWrite(buf, 1, ">");
  1418. if (ctxt->format == 1) {
  1419. xmlOutputBufferWrite(buf, 1, "\n");
  1420. if (xmlIndentTreeOutput)
  1421. xmlOutputBufferWrite(buf, ctxt->indent_size *
  1422. (ctxt->level + 1 > ctxt->indent_nr ?
  1423. ctxt->indent_nr : ctxt->level + 1),
  1424. ctxt->indent);
  1425. }
  1426. xmlOutputBufferWriteString(buf,
  1427. "<meta http-equiv=\"Content-Type\" "
  1428. "content=\"text/html; charset=");
  1429. if (ctxt->encoding) {
  1430. xmlOutputBufferWriteString(buf,
  1431. (const char *)ctxt->encoding);
  1432. } else {
  1433. xmlOutputBufferWrite(buf, 5, "UTF-8");
  1434. }
  1435. xmlOutputBufferWrite(buf, 4, "\" />");
  1436. if (ctxt->format == 1)
  1437. xmlOutputBufferWrite(buf, 1, "\n");
  1438. } else {
  1439. xmlOutputBufferWrite(buf, 1, ">");
  1440. }
  1441. /*
  1442. * C.3. Element Minimization and Empty Element Content
  1443. */
  1444. xmlOutputBufferWrite(buf, 2, "</");
  1445. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  1446. xmlOutputBufferWriteString(buf,
  1447. (const char *)cur->ns->prefix);
  1448. xmlOutputBufferWrite(buf, 1, ":");
  1449. }
  1450. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  1451. xmlOutputBufferWrite(buf, 1, ">");
  1452. }
  1453. } else {
  1454. xmlOutputBufferWrite(buf, 1, ">");
  1455. if (addmeta == 1) {
  1456. if (ctxt->format == 1) {
  1457. xmlOutputBufferWrite(buf, 1, "\n");
  1458. if (xmlIndentTreeOutput)
  1459. xmlOutputBufferWrite(buf, ctxt->indent_size *
  1460. (ctxt->level + 1 > ctxt->indent_nr ?
  1461. ctxt->indent_nr : ctxt->level + 1),
  1462. ctxt->indent);
  1463. }
  1464. xmlOutputBufferWriteString(buf,
  1465. "<meta http-equiv=\"Content-Type\" "
  1466. "content=\"text/html; charset=");
  1467. if (ctxt->encoding) {
  1468. xmlOutputBufferWriteString(buf,
  1469. (const char *)ctxt->encoding);
  1470. } else {
  1471. xmlOutputBufferWrite(buf, 5, "UTF-8");
  1472. }
  1473. xmlOutputBufferWrite(buf, 4, "\" />");
  1474. }
  1475. if (ctxt->format == 1) {
  1476. tmp = cur->children;
  1477. while (tmp != NULL) {
  1478. if ((tmp->type == XML_TEXT_NODE) ||
  1479. (tmp->type == XML_ENTITY_REF_NODE)) {
  1480. unformattedNode = cur;
  1481. ctxt->format = 0;
  1482. break;
  1483. }
  1484. tmp = tmp->next;
  1485. }
  1486. }
  1487. if (ctxt->format == 1) xmlOutputBufferWrite(buf, 1, "\n");
  1488. if (ctxt->level >= 0) ctxt->level++;
  1489. cur = cur->children;
  1490. continue;
  1491. }
  1492. break;
  1493. case XML_TEXT_NODE:
  1494. if (cur->content == NULL)
  1495. break;
  1496. if ((cur->name == xmlStringText) ||
  1497. (cur->name != xmlStringTextNoenc)) {
  1498. xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
  1499. } else {
  1500. /*
  1501. * Disable escaping, needed for XSLT
  1502. */
  1503. xmlOutputBufferWriteString(buf, (const char *) cur->content);
  1504. }
  1505. break;
  1506. case XML_PI_NODE:
  1507. if (cur->content != NULL) {
  1508. xmlOutputBufferWrite(buf, 2, "<?");
  1509. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  1510. if (cur->content != NULL) {
  1511. xmlOutputBufferWrite(buf, 1, " ");
  1512. xmlOutputBufferWriteString(buf,
  1513. (const char *)cur->content);
  1514. }
  1515. xmlOutputBufferWrite(buf, 2, "?>");
  1516. } else {
  1517. xmlOutputBufferWrite(buf, 2, "<?");
  1518. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  1519. xmlOutputBufferWrite(buf, 2, "?>");
  1520. }
  1521. break;
  1522. case XML_COMMENT_NODE:
  1523. if (cur->content != NULL) {
  1524. xmlOutputBufferWrite(buf, 4, "<!--");
  1525. xmlOutputBufferWriteString(buf, (const char *)cur->content);
  1526. xmlOutputBufferWrite(buf, 3, "-->");
  1527. }
  1528. break;
  1529. case XML_ENTITY_REF_NODE:
  1530. xmlOutputBufferWrite(buf, 1, "&");
  1531. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  1532. xmlOutputBufferWrite(buf, 1, ";");
  1533. break;
  1534. case XML_CDATA_SECTION_NODE:
  1535. if (cur->content == NULL || *cur->content == '\0') {
  1536. xmlOutputBufferWrite(buf, 12, "<![CDATA[]]>");
  1537. } else {
  1538. start = end = cur->content;
  1539. while (*end != '\0') {
  1540. if (*end == ']' && *(end + 1) == ']' &&
  1541. *(end + 2) == '>') {
  1542. end = end + 2;
  1543. xmlOutputBufferWrite(buf, 9, "<![CDATA[");
  1544. xmlOutputBufferWrite(buf, end - start,
  1545. (const char *)start);
  1546. xmlOutputBufferWrite(buf, 3, "]]>");
  1547. start = end;
  1548. }
  1549. end++;
  1550. }
  1551. if (start != end) {
  1552. xmlOutputBufferWrite(buf, 9, "<![CDATA[");
  1553. xmlOutputBufferWriteString(buf, (const char *)start);
  1554. xmlOutputBufferWrite(buf, 3, "]]>");
  1555. }
  1556. }
  1557. break;
  1558. case XML_ATTRIBUTE_NODE:
  1559. xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur);
  1560. break;
  1561. default:
  1562. break;
  1563. }
  1564. while (1) {
  1565. if (cur == root)
  1566. return;
  1567. if (ctxt->format == 1)
  1568. xmlOutputBufferWrite(buf, 1, "\n");
  1569. if (cur->next != NULL) {
  1570. cur = cur->next;
  1571. break;
  1572. }
  1573. /*
  1574. * The parent should never be NULL here but we want to handle
  1575. * corrupted documents gracefully.
  1576. */
  1577. if (cur->parent == NULL)
  1578. return;
  1579. cur = cur->parent;
  1580. if (cur->type == XML_ELEMENT_NODE) {
  1581. if (ctxt->level > 0) ctxt->level--;
  1582. if ((xmlIndentTreeOutput) && (ctxt->format == 1))
  1583. xmlOutputBufferWrite(buf, ctxt->indent_size *
  1584. (ctxt->level > ctxt->indent_nr ?
  1585. ctxt->indent_nr : ctxt->level),
  1586. ctxt->indent);
  1587. xmlOutputBufferWrite(buf, 2, "</");
  1588. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  1589. xmlOutputBufferWriteString(buf,
  1590. (const char *)cur->ns->prefix);
  1591. xmlOutputBufferWrite(buf, 1, ":");
  1592. }
  1593. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  1594. xmlOutputBufferWrite(buf, 1, ">");
  1595. if (cur == unformattedNode) {
  1596. ctxt->format = format;
  1597. unformattedNode = NULL;
  1598. }
  1599. }
  1600. }
  1601. }
  1602. }
  1603. #endif
  1604. /************************************************************************
  1605. * *
  1606. * Public entry points *
  1607. * *
  1608. ************************************************************************/
  1609. /**
  1610. * xmlSaveToFd:
  1611. * @fd: a file descriptor number
  1612. * @encoding: the encoding name to use or NULL
  1613. * @options: a set of xmlSaveOptions
  1614. *
  1615. * Create a document saving context serializing to a file descriptor
  1616. * with the encoding and the options given.
  1617. *
  1618. * Returns a new serialization context or NULL in case of error.
  1619. */
  1620. xmlSaveCtxtPtr
  1621. xmlSaveToFd(int fd, const char *encoding, int options)
  1622. {
  1623. xmlSaveCtxtPtr ret;
  1624. ret = xmlNewSaveCtxt(encoding, options);
  1625. if (ret == NULL) return(NULL);
  1626. ret->buf = xmlOutputBufferCreateFd(fd, ret->handler);
  1627. if (ret->buf == NULL) {
  1628. xmlCharEncCloseFunc(ret->handler);
  1629. xmlFreeSaveCtxt(ret);
  1630. return(NULL);
  1631. }
  1632. return(ret);
  1633. }
  1634. /**
  1635. * xmlSaveToFilename:
  1636. * @filename: a file name or an URL
  1637. * @encoding: the encoding name to use or NULL
  1638. * @options: a set of xmlSaveOptions
  1639. *
  1640. * Create a document saving context serializing to a filename or possibly
  1641. * to an URL (but this is less reliable) with the encoding and the options
  1642. * given.
  1643. *
  1644. * Returns a new serialization context or NULL in case of error.
  1645. */
  1646. xmlSaveCtxtPtr
  1647. xmlSaveToFilename(const char *filename, const char *encoding, int options)
  1648. {
  1649. xmlSaveCtxtPtr ret;
  1650. int compression = 0; /* TODO handle compression option */
  1651. ret = xmlNewSaveCtxt(encoding, options);
  1652. if (ret == NULL) return(NULL);
  1653. ret->buf = xmlOutputBufferCreateFilename(filename, ret->handler,
  1654. compression);
  1655. if (ret->buf == NULL) {
  1656. xmlCharEncCloseFunc(ret->handler);
  1657. xmlFreeSaveCtxt(ret);
  1658. return(NULL);
  1659. }
  1660. return(ret);
  1661. }
  1662. /**
  1663. * xmlSaveToBuffer:
  1664. * @buffer: a buffer
  1665. * @encoding: the encoding name to use or NULL
  1666. * @options: a set of xmlSaveOptions
  1667. *
  1668. * Create a document saving context serializing to a buffer
  1669. * with the encoding and the options given
  1670. *
  1671. * Returns a new serialization context or NULL in case of error.
  1672. */
  1673. xmlSaveCtxtPtr
  1674. xmlSaveToBuffer(xmlBufferPtr buffer, const char *encoding, int options)
  1675. {
  1676. xmlSaveCtxtPtr ret;
  1677. ret = xmlNewSaveCtxt(encoding, options);
  1678. if (ret == NULL) return(NULL);
  1679. ret->buf = xmlOutputBufferCreateBuffer(buffer, ret->handler);
  1680. if (ret->buf == NULL) {
  1681. xmlCharEncCloseFunc(ret->handler);
  1682. xmlFreeSaveCtxt(ret);
  1683. return(NULL);
  1684. }
  1685. return(ret);
  1686. }
  1687. /**
  1688. * xmlSaveToIO:
  1689. * @iowrite: an I/O write function
  1690. * @ioclose: an I/O close function
  1691. * @ioctx: an I/O handler
  1692. * @encoding: the encoding name to use or NULL
  1693. * @options: a set of xmlSaveOptions
  1694. *
  1695. * Create a document saving context serializing to a file descriptor
  1696. * with the encoding and the options given
  1697. *
  1698. * Returns a new serialization context or NULL in case of error.
  1699. */
  1700. xmlSaveCtxtPtr
  1701. xmlSaveToIO(xmlOutputWriteCallback iowrite,
  1702. xmlOutputCloseCallback ioclose,
  1703. void *ioctx, const char *encoding, int options)
  1704. {
  1705. xmlSaveCtxtPtr ret;
  1706. ret = xmlNewSaveCtxt(encoding, options);
  1707. if (ret == NULL) return(NULL);
  1708. ret->buf = xmlOutputBufferCreateIO(iowrite, ioclose, ioctx, ret->handler);
  1709. if (ret->buf == NULL) {
  1710. xmlCharEncCloseFunc(ret->handler);
  1711. xmlFreeSaveCtxt(ret);
  1712. return(NULL);
  1713. }
  1714. return(ret);
  1715. }
  1716. /**
  1717. * xmlSaveDoc:
  1718. * @ctxt: a document saving context
  1719. * @doc: a document
  1720. *
  1721. * Save a full document to a saving context
  1722. * TODO: The function is not fully implemented yet as it does not return the
  1723. * byte count but 0 instead
  1724. *
  1725. * Returns the number of byte written or -1 in case of error
  1726. */
  1727. long
  1728. xmlSaveDoc(xmlSaveCtxtPtr ctxt, xmlDocPtr doc)
  1729. {
  1730. long ret = 0;
  1731. if ((ctxt == NULL) || (doc == NULL)) return(-1);
  1732. if (xmlDocContentDumpOutput(ctxt, doc) < 0)
  1733. return(-1);
  1734. return(ret);
  1735. }
  1736. /**
  1737. * xmlSaveTree:
  1738. * @ctxt: a document saving context
  1739. * @node: the top node of the subtree to save
  1740. *
  1741. * Save a subtree starting at the node parameter to a saving context
  1742. * TODO: The function is not fully implemented yet as it does not return the
  1743. * byte count but 0 instead
  1744. *
  1745. * Returns the number of byte written or -1 in case of error
  1746. */
  1747. long
  1748. xmlSaveTree(xmlSaveCtxtPtr ctxt, xmlNodePtr cur)
  1749. {
  1750. long ret = 0;
  1751. if ((ctxt == NULL) || (cur == NULL)) return(-1);
  1752. #ifdef LIBXML_HTML_ENABLED
  1753. if (ctxt->options & XML_SAVE_XHTML) {
  1754. xhtmlNodeDumpOutput(ctxt, cur);
  1755. return(ret);
  1756. }
  1757. if (((cur->type != XML_NAMESPACE_DECL) && (cur->doc != NULL) &&
  1758. (cur->doc->type == XML_HTML_DOCUMENT_NODE) &&
  1759. ((ctxt->options & XML_SAVE_AS_XML) == 0)) ||
  1760. (ctxt->options & XML_SAVE_AS_HTML)) {
  1761. htmlNodeDumpOutputInternal(ctxt, cur);
  1762. return(ret);
  1763. }
  1764. #endif
  1765. xmlNodeDumpOutputInternal(ctxt, cur);
  1766. return(ret);
  1767. }
  1768. /**
  1769. * xmlSaveFlush:
  1770. * @ctxt: a document saving context
  1771. *
  1772. * Flush a document saving context, i.e. make sure that all bytes have
  1773. * been output.
  1774. *
  1775. * Returns the number of byte written or -1 in case of error.
  1776. */
  1777. int
  1778. xmlSaveFlush(xmlSaveCtxtPtr ctxt)
  1779. {
  1780. if (ctxt == NULL) return(-1);
  1781. if (ctxt->buf == NULL) return(-1);
  1782. return(xmlOutputBufferFlush(ctxt->buf));
  1783. }
  1784. /**
  1785. * xmlSaveClose:
  1786. * @ctxt: a document saving context
  1787. *
  1788. * Close a document saving context, i.e. make sure that all bytes have
  1789. * been output and free the associated data.
  1790. *
  1791. * Returns the number of byte written or -1 in case of error.
  1792. */
  1793. int
  1794. xmlSaveClose(xmlSaveCtxtPtr ctxt)
  1795. {
  1796. int ret;
  1797. if (ctxt == NULL) return(-1);
  1798. ret = xmlSaveFlush(ctxt);
  1799. xmlFreeSaveCtxt(ctxt);
  1800. return(ret);
  1801. }
  1802. /**
  1803. * xmlSaveSetEscape:
  1804. * @ctxt: a document saving context
  1805. * @escape: the escaping function
  1806. *
  1807. * Set a custom escaping function to be used for text in element content
  1808. *
  1809. * Returns 0 if successful or -1 in case of error.
  1810. */
  1811. int
  1812. xmlSaveSetEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
  1813. {
  1814. if (ctxt == NULL) return(-1);
  1815. ctxt->escape = escape;
  1816. return(0);
  1817. }
  1818. /**
  1819. * xmlSaveSetAttrEscape:
  1820. * @ctxt: a document saving context
  1821. * @escape: the escaping function
  1822. *
  1823. * Set a custom escaping function to be used for text in attribute content
  1824. *
  1825. * Returns 0 if successful or -1 in case of error.
  1826. */
  1827. int
  1828. xmlSaveSetAttrEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
  1829. {
  1830. if (ctxt == NULL) return(-1);
  1831. ctxt->escapeAttr = escape;
  1832. return(0);
  1833. }
  1834. /************************************************************************
  1835. * *
  1836. * Public entry points based on buffers *
  1837. * *
  1838. ************************************************************************/
  1839. /**
  1840. * xmlBufAttrSerializeTxtContent:
  1841. * @buf: and xmlBufPtr output
  1842. * @doc: the document
  1843. * @attr: the attribute node
  1844. * @string: the text content
  1845. *
  1846. * Serialize text attribute values to an xmlBufPtr
  1847. */
  1848. void
  1849. xmlBufAttrSerializeTxtContent(xmlBufPtr buf, xmlDocPtr doc,
  1850. xmlAttrPtr attr, const xmlChar * string)
  1851. {
  1852. xmlChar *base, *cur;
  1853. if (string == NULL)
  1854. return;
  1855. base = cur = (xmlChar *) string;
  1856. while (*cur != 0) {
  1857. if (*cur == '\n') {
  1858. if (base != cur)
  1859. xmlBufAdd(buf, base, cur - base);
  1860. xmlBufAdd(buf, BAD_CAST "&#10;", 5);
  1861. cur++;
  1862. base = cur;
  1863. } else if (*cur == '\r') {
  1864. if (base != cur)
  1865. xmlBufAdd(buf, base, cur - base);
  1866. xmlBufAdd(buf, BAD_CAST "&#13;", 5);
  1867. cur++;
  1868. base = cur;
  1869. } else if (*cur == '\t') {
  1870. if (base != cur)
  1871. xmlBufAdd(buf, base, cur - base);
  1872. xmlBufAdd(buf, BAD_CAST "&#9;", 4);
  1873. cur++;
  1874. base = cur;
  1875. } else if (*cur == '"') {
  1876. if (base != cur)
  1877. xmlBufAdd(buf, base, cur - base);
  1878. xmlBufAdd(buf, BAD_CAST "&quot;", 6);
  1879. cur++;
  1880. base = cur;
  1881. } else if (*cur == '<') {
  1882. if (base != cur)
  1883. xmlBufAdd(buf, base, cur - base);
  1884. xmlBufAdd(buf, BAD_CAST "&lt;", 4);
  1885. cur++;
  1886. base = cur;
  1887. } else if (*cur == '>') {
  1888. if (base != cur)
  1889. xmlBufAdd(buf, base, cur - base);
  1890. xmlBufAdd(buf, BAD_CAST "&gt;", 4);
  1891. cur++;
  1892. base = cur;
  1893. } else if (*cur == '&') {
  1894. if (base != cur)
  1895. xmlBufAdd(buf, base, cur - base);
  1896. xmlBufAdd(buf, BAD_CAST "&amp;", 5);
  1897. cur++;
  1898. base = cur;
  1899. } else if ((*cur >= 0x80) && (cur[1] != 0) &&
  1900. ((doc == NULL) || (doc->encoding == NULL))) {
  1901. /*
  1902. * We assume we have UTF-8 content.
  1903. */
  1904. unsigned char tmp[12];
  1905. int val = 0, l = 1;
  1906. if (base != cur)
  1907. xmlBufAdd(buf, base, cur - base);
  1908. if (*cur < 0xC0) {
  1909. xmlSaveErr(XML_SAVE_NOT_UTF8, (xmlNodePtr) attr, NULL);
  1910. xmlSerializeHexCharRef(tmp, *cur);
  1911. xmlBufAdd(buf, (xmlChar *) tmp, -1);
  1912. cur++;
  1913. base = cur;
  1914. continue;
  1915. } else if (*cur < 0xE0) {
  1916. val = (cur[0]) & 0x1F;
  1917. val <<= 6;
  1918. val |= (cur[1]) & 0x3F;
  1919. l = 2;
  1920. } else if ((*cur < 0xF0) && (cur [2] != 0)) {
  1921. val = (cur[0]) & 0x0F;
  1922. val <<= 6;
  1923. val |= (cur[1]) & 0x3F;
  1924. val <<= 6;
  1925. val |= (cur[2]) & 0x3F;
  1926. l = 3;
  1927. } else if ((*cur < 0xF8) && (cur [2] != 0) && (cur[3] != 0)) {
  1928. val = (cur[0]) & 0x07;
  1929. val <<= 6;
  1930. val |= (cur[1]) & 0x3F;
  1931. val <<= 6;
  1932. val |= (cur[2]) & 0x3F;
  1933. val <<= 6;
  1934. val |= (cur[3]) & 0x3F;
  1935. l = 4;
  1936. }
  1937. if ((l == 1) || (!IS_CHAR(val))) {
  1938. xmlSaveErr(XML_SAVE_CHAR_INVALID, (xmlNodePtr) attr, NULL);
  1939. xmlSerializeHexCharRef(tmp, *cur);
  1940. xmlBufAdd(buf, (xmlChar *) tmp, -1);
  1941. cur++;
  1942. base = cur;
  1943. continue;
  1944. }
  1945. /*
  1946. * We could do multiple things here. Just save
  1947. * as a char ref
  1948. */
  1949. xmlSerializeHexCharRef(tmp, val);
  1950. xmlBufAdd(buf, (xmlChar *) tmp, -1);
  1951. cur += l;
  1952. base = cur;
  1953. } else {
  1954. cur++;
  1955. }
  1956. }
  1957. if (base != cur)
  1958. xmlBufAdd(buf, base, cur - base);
  1959. }
  1960. /**
  1961. * xmlAttrSerializeTxtContent:
  1962. * @buf: the XML buffer output
  1963. * @doc: the document
  1964. * @attr: the attribute node
  1965. * @string: the text content
  1966. *
  1967. * Serialize text attribute values to an xml simple buffer
  1968. */
  1969. void
  1970. xmlAttrSerializeTxtContent(xmlBufferPtr buf, xmlDocPtr doc,
  1971. xmlAttrPtr attr, const xmlChar * string)
  1972. {
  1973. xmlBufPtr buffer;
  1974. if ((buf == NULL) || (string == NULL))
  1975. return;
  1976. buffer = xmlBufFromBuffer(buf);
  1977. if (buffer == NULL)
  1978. return;
  1979. xmlBufAttrSerializeTxtContent(buffer, doc, attr, string);
  1980. xmlBufBackToBuffer(buffer);
  1981. }
  1982. /**
  1983. * xmlNodeDump:
  1984. * @buf: the XML buffer output
  1985. * @doc: the document
  1986. * @cur: the current node
  1987. * @level: the imbrication level for indenting
  1988. * @format: is formatting allowed
  1989. *
  1990. * Dump an XML node, recursive behaviour,children are printed too.
  1991. * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
  1992. * or xmlKeepBlanksDefault(0) was called.
  1993. * Since this is using xmlBuffer structures it is limited to 2GB and somehow
  1994. * deprecated, use xmlNodeDumpOutput() instead.
  1995. *
  1996. * Returns the number of bytes written to the buffer or -1 in case of error
  1997. */
  1998. int
  1999. xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
  2000. int format)
  2001. {
  2002. xmlBufPtr buffer;
  2003. size_t ret;
  2004. if ((buf == NULL) || (cur == NULL))
  2005. return(-1);
  2006. buffer = xmlBufFromBuffer(buf);
  2007. if (buffer == NULL)
  2008. return(-1);
  2009. ret = xmlBufNodeDump(buffer, doc, cur, level, format);
  2010. xmlBufBackToBuffer(buffer);
  2011. if (ret > INT_MAX)
  2012. return(-1);
  2013. return((int) ret);
  2014. }
  2015. /**
  2016. * xmlBufNodeDump:
  2017. * @buf: the XML buffer output
  2018. * @doc: the document
  2019. * @cur: the current node
  2020. * @level: the imbrication level for indenting
  2021. * @format: is formatting allowed
  2022. *
  2023. * Dump an XML node, recursive behaviour,children are printed too.
  2024. * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
  2025. * or xmlKeepBlanksDefault(0) was called
  2026. *
  2027. * Returns the number of bytes written to the buffer, in case of error 0
  2028. * is returned or @buf stores the error
  2029. */
  2030. size_t
  2031. xmlBufNodeDump(xmlBufPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
  2032. int format)
  2033. {
  2034. size_t use;
  2035. int ret;
  2036. xmlOutputBufferPtr outbuf;
  2037. int oldalloc;
  2038. xmlInitParser();
  2039. if (cur == NULL) {
  2040. #ifdef DEBUG_TREE
  2041. xmlGenericError(xmlGenericErrorContext,
  2042. "xmlNodeDump : node == NULL\n");
  2043. #endif
  2044. return (-1);
  2045. }
  2046. if (buf == NULL) {
  2047. #ifdef DEBUG_TREE
  2048. xmlGenericError(xmlGenericErrorContext,
  2049. "xmlNodeDump : buf == NULL\n");
  2050. #endif
  2051. return (-1);
  2052. }
  2053. outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
  2054. if (outbuf == NULL) {
  2055. xmlSaveErrMemory("creating buffer");
  2056. return (-1);
  2057. }
  2058. memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
  2059. outbuf->buffer = buf;
  2060. outbuf->encoder = NULL;
  2061. outbuf->writecallback = NULL;
  2062. outbuf->closecallback = NULL;
  2063. outbuf->context = NULL;
  2064. outbuf->written = 0;
  2065. use = xmlBufUse(buf);
  2066. oldalloc = xmlBufGetAllocationScheme(buf);
  2067. xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_DOUBLEIT);
  2068. xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL);
  2069. xmlBufSetAllocationScheme(buf, oldalloc);
  2070. xmlFree(outbuf);
  2071. ret = xmlBufUse(buf) - use;
  2072. return (ret);
  2073. }
  2074. /**
  2075. * xmlElemDump:
  2076. * @f: the FILE * for the output
  2077. * @doc: the document
  2078. * @cur: the current node
  2079. *
  2080. * Dump an XML/HTML node, recursive behaviour, children are printed too.
  2081. */
  2082. void
  2083. xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur)
  2084. {
  2085. xmlOutputBufferPtr outbuf;
  2086. xmlInitParser();
  2087. if (cur == NULL) {
  2088. #ifdef DEBUG_TREE
  2089. xmlGenericError(xmlGenericErrorContext,
  2090. "xmlElemDump : cur == NULL\n");
  2091. #endif
  2092. return;
  2093. }
  2094. #ifdef DEBUG_TREE
  2095. if (doc == NULL) {
  2096. xmlGenericError(xmlGenericErrorContext,
  2097. "xmlElemDump : doc == NULL\n");
  2098. }
  2099. #endif
  2100. outbuf = xmlOutputBufferCreateFile(f, NULL);
  2101. if (outbuf == NULL)
  2102. return;
  2103. if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
  2104. #ifdef LIBXML_HTML_ENABLED
  2105. htmlNodeDumpOutput(outbuf, doc, cur, NULL);
  2106. #else
  2107. xmlSaveErr(XML_ERR_INTERNAL_ERROR, cur, "HTML support not compiled in\n");
  2108. #endif /* LIBXML_HTML_ENABLED */
  2109. } else
  2110. xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL);
  2111. xmlOutputBufferClose(outbuf);
  2112. }
  2113. /************************************************************************
  2114. * *
  2115. * Saving functions front-ends *
  2116. * *
  2117. ************************************************************************/
  2118. /**
  2119. * xmlNodeDumpOutput:
  2120. * @buf: the XML buffer output
  2121. * @doc: the document
  2122. * @cur: the current node
  2123. * @level: the imbrication level for indenting
  2124. * @format: is formatting allowed
  2125. * @encoding: an optional encoding string
  2126. *
  2127. * Dump an XML node, recursive behaviour, children are printed too.
  2128. * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
  2129. * or xmlKeepBlanksDefault(0) was called
  2130. */
  2131. void
  2132. xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
  2133. int level, int format, const char *encoding)
  2134. {
  2135. xmlSaveCtxt ctxt;
  2136. #ifdef LIBXML_HTML_ENABLED
  2137. xmlDtdPtr dtd;
  2138. int is_xhtml = 0;
  2139. #endif
  2140. xmlInitParser();
  2141. if ((buf == NULL) || (cur == NULL)) return;
  2142. if (encoding == NULL)
  2143. encoding = "UTF-8";
  2144. memset(&ctxt, 0, sizeof(ctxt));
  2145. ctxt.buf = buf;
  2146. ctxt.level = level;
  2147. ctxt.format = format ? 1 : 0;
  2148. ctxt.encoding = (const xmlChar *) encoding;
  2149. xmlSaveCtxtInit(&ctxt);
  2150. ctxt.options |= XML_SAVE_AS_XML;
  2151. #ifdef LIBXML_HTML_ENABLED
  2152. dtd = xmlGetIntSubset(doc);
  2153. if (dtd != NULL) {
  2154. is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
  2155. if (is_xhtml < 0)
  2156. is_xhtml = 0;
  2157. }
  2158. if (is_xhtml)
  2159. xhtmlNodeDumpOutput(&ctxt, cur);
  2160. else
  2161. #endif
  2162. xmlNodeDumpOutputInternal(&ctxt, cur);
  2163. }
  2164. /**
  2165. * xmlDocDumpFormatMemoryEnc:
  2166. * @out_doc: Document to generate XML text from
  2167. * @doc_txt_ptr: Memory pointer for allocated XML text
  2168. * @doc_txt_len: Length of the generated XML text
  2169. * @txt_encoding: Character encoding to use when generating XML text
  2170. * @format: should formatting spaces been added
  2171. *
  2172. * Dump the current DOM tree into memory using the character encoding specified
  2173. * by the caller. Note it is up to the caller of this function to free the
  2174. * allocated memory with xmlFree().
  2175. * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
  2176. * or xmlKeepBlanksDefault(0) was called
  2177. */
  2178. void
  2179. xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
  2180. int * doc_txt_len, const char * txt_encoding,
  2181. int format) {
  2182. xmlSaveCtxt ctxt;
  2183. int dummy = 0;
  2184. xmlOutputBufferPtr out_buff = NULL;
  2185. xmlCharEncodingHandlerPtr conv_hdlr = NULL;
  2186. if (doc_txt_len == NULL) {
  2187. doc_txt_len = &dummy; /* Continue, caller just won't get length */
  2188. }
  2189. if (doc_txt_ptr == NULL) {
  2190. *doc_txt_len = 0;
  2191. return;
  2192. }
  2193. *doc_txt_ptr = NULL;
  2194. *doc_txt_len = 0;
  2195. if (out_doc == NULL) {
  2196. /* No document, no output */
  2197. return;
  2198. }
  2199. /*
  2200. * Validate the encoding value, if provided.
  2201. * This logic is copied from xmlSaveFileEnc.
  2202. */
  2203. if (txt_encoding == NULL)
  2204. txt_encoding = (const char *) out_doc->encoding;
  2205. if (txt_encoding != NULL) {
  2206. conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
  2207. if ( conv_hdlr == NULL ) {
  2208. xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, (xmlNodePtr) out_doc,
  2209. txt_encoding);
  2210. return;
  2211. }
  2212. }
  2213. if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
  2214. xmlSaveErrMemory("creating buffer");
  2215. return;
  2216. }
  2217. memset(&ctxt, 0, sizeof(ctxt));
  2218. ctxt.buf = out_buff;
  2219. ctxt.level = 0;
  2220. ctxt.format = format ? 1 : 0;
  2221. ctxt.encoding = (const xmlChar *) txt_encoding;
  2222. xmlSaveCtxtInit(&ctxt);
  2223. ctxt.options |= XML_SAVE_AS_XML;
  2224. xmlDocContentDumpOutput(&ctxt, out_doc);
  2225. xmlOutputBufferFlush(out_buff);
  2226. if (out_buff->conv != NULL) {
  2227. *doc_txt_len = xmlBufUse(out_buff->conv);
  2228. *doc_txt_ptr = xmlStrndup(xmlBufContent(out_buff->conv), *doc_txt_len);
  2229. } else {
  2230. *doc_txt_len = xmlBufUse(out_buff->buffer);
  2231. *doc_txt_ptr = xmlStrndup(xmlBufContent(out_buff->buffer),*doc_txt_len);
  2232. }
  2233. (void)xmlOutputBufferClose(out_buff);
  2234. if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
  2235. *doc_txt_len = 0;
  2236. xmlSaveErrMemory("creating output");
  2237. }
  2238. return;
  2239. }
  2240. /**
  2241. * xmlDocDumpMemory:
  2242. * @cur: the document
  2243. * @mem: OUT: the memory pointer
  2244. * @size: OUT: the memory length
  2245. *
  2246. * Dump an XML document in memory and return the #xmlChar * and it's size
  2247. * in bytes. It's up to the caller to free the memory with xmlFree().
  2248. * The resulting byte array is zero terminated, though the last 0 is not
  2249. * included in the returned size.
  2250. */
  2251. void
  2252. xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
  2253. xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
  2254. }
  2255. /**
  2256. * xmlDocDumpFormatMemory:
  2257. * @cur: the document
  2258. * @mem: OUT: the memory pointer
  2259. * @size: OUT: the memory length
  2260. * @format: should formatting spaces been added
  2261. *
  2262. *
  2263. * Dump an XML document in memory and return the #xmlChar * and it's size.
  2264. * It's up to the caller to free the memory with xmlFree().
  2265. * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
  2266. * or xmlKeepBlanksDefault(0) was called
  2267. */
  2268. void
  2269. xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
  2270. xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
  2271. }
  2272. /**
  2273. * xmlDocDumpMemoryEnc:
  2274. * @out_doc: Document to generate XML text from
  2275. * @doc_txt_ptr: Memory pointer for allocated XML text
  2276. * @doc_txt_len: Length of the generated XML text
  2277. * @txt_encoding: Character encoding to use when generating XML text
  2278. *
  2279. * Dump the current DOM tree into memory using the character encoding specified
  2280. * by the caller. Note it is up to the caller of this function to free the
  2281. * allocated memory with xmlFree().
  2282. */
  2283. void
  2284. xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
  2285. int * doc_txt_len, const char * txt_encoding) {
  2286. xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
  2287. txt_encoding, 0);
  2288. }
  2289. /**
  2290. * xmlDocFormatDump:
  2291. * @f: the FILE*
  2292. * @cur: the document
  2293. * @format: should formatting spaces been added
  2294. *
  2295. * Dump an XML document to an open FILE.
  2296. *
  2297. * returns: the number of bytes written or -1 in case of failure.
  2298. * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
  2299. * or xmlKeepBlanksDefault(0) was called
  2300. */
  2301. int
  2302. xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) {
  2303. xmlSaveCtxt ctxt;
  2304. xmlOutputBufferPtr buf;
  2305. const char * encoding;
  2306. xmlCharEncodingHandlerPtr handler = NULL;
  2307. int ret;
  2308. if (cur == NULL) {
  2309. #ifdef DEBUG_TREE
  2310. xmlGenericError(xmlGenericErrorContext,
  2311. "xmlDocDump : document == NULL\n");
  2312. #endif
  2313. return(-1);
  2314. }
  2315. encoding = (const char *) cur->encoding;
  2316. if (encoding != NULL) {
  2317. handler = xmlFindCharEncodingHandler(encoding);
  2318. if (handler == NULL) {
  2319. xmlFree((char *) cur->encoding);
  2320. cur->encoding = NULL;
  2321. encoding = NULL;
  2322. }
  2323. }
  2324. buf = xmlOutputBufferCreateFile(f, handler);
  2325. if (buf == NULL) return(-1);
  2326. memset(&ctxt, 0, sizeof(ctxt));
  2327. ctxt.buf = buf;
  2328. ctxt.level = 0;
  2329. ctxt.format = format ? 1 : 0;
  2330. ctxt.encoding = (const xmlChar *) encoding;
  2331. xmlSaveCtxtInit(&ctxt);
  2332. ctxt.options |= XML_SAVE_AS_XML;
  2333. xmlDocContentDumpOutput(&ctxt, cur);
  2334. ret = xmlOutputBufferClose(buf);
  2335. return(ret);
  2336. }
  2337. /**
  2338. * xmlDocDump:
  2339. * @f: the FILE*
  2340. * @cur: the document
  2341. *
  2342. * Dump an XML document to an open FILE.
  2343. *
  2344. * returns: the number of bytes written or -1 in case of failure.
  2345. */
  2346. int
  2347. xmlDocDump(FILE *f, xmlDocPtr cur) {
  2348. return(xmlDocFormatDump (f, cur, 0));
  2349. }
  2350. /**
  2351. * xmlSaveFileTo:
  2352. * @buf: an output I/O buffer
  2353. * @cur: the document
  2354. * @encoding: the encoding if any assuming the I/O layer handles the transcoding
  2355. *
  2356. * Dump an XML document to an I/O buffer.
  2357. * Warning ! This call xmlOutputBufferClose() on buf which is not available
  2358. * after this call.
  2359. *
  2360. * returns: the number of bytes written or -1 in case of failure.
  2361. */
  2362. int
  2363. xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) {
  2364. xmlSaveCtxt ctxt;
  2365. int ret;
  2366. if (buf == NULL) return(-1);
  2367. if (cur == NULL) {
  2368. xmlOutputBufferClose(buf);
  2369. return(-1);
  2370. }
  2371. memset(&ctxt, 0, sizeof(ctxt));
  2372. ctxt.buf = buf;
  2373. ctxt.level = 0;
  2374. ctxt.format = 0;
  2375. ctxt.encoding = (const xmlChar *) encoding;
  2376. xmlSaveCtxtInit(&ctxt);
  2377. ctxt.options |= XML_SAVE_AS_XML;
  2378. xmlDocContentDumpOutput(&ctxt, cur);
  2379. ret = xmlOutputBufferClose(buf);
  2380. return(ret);
  2381. }
  2382. /**
  2383. * xmlSaveFormatFileTo:
  2384. * @buf: an output I/O buffer
  2385. * @cur: the document
  2386. * @encoding: the encoding if any assuming the I/O layer handles the transcoding
  2387. * @format: should formatting spaces been added
  2388. *
  2389. * Dump an XML document to an I/O buffer.
  2390. * Warning ! This call xmlOutputBufferClose() on buf which is not available
  2391. * after this call.
  2392. *
  2393. * returns: the number of bytes written or -1 in case of failure.
  2394. */
  2395. int
  2396. xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur,
  2397. const char *encoding, int format)
  2398. {
  2399. xmlSaveCtxt ctxt;
  2400. int ret;
  2401. if (buf == NULL) return(-1);
  2402. if ((cur == NULL) ||
  2403. ((cur->type != XML_DOCUMENT_NODE) &&
  2404. (cur->type != XML_HTML_DOCUMENT_NODE))) {
  2405. xmlOutputBufferClose(buf);
  2406. return(-1);
  2407. }
  2408. memset(&ctxt, 0, sizeof(ctxt));
  2409. ctxt.buf = buf;
  2410. ctxt.level = 0;
  2411. ctxt.format = format ? 1 : 0;
  2412. ctxt.encoding = (const xmlChar *) encoding;
  2413. xmlSaveCtxtInit(&ctxt);
  2414. ctxt.options |= XML_SAVE_AS_XML;
  2415. xmlDocContentDumpOutput(&ctxt, cur);
  2416. ret = xmlOutputBufferClose(buf);
  2417. return (ret);
  2418. }
  2419. /**
  2420. * xmlSaveFormatFileEnc:
  2421. * @filename: the filename or URL to output
  2422. * @cur: the document being saved
  2423. * @encoding: the name of the encoding to use or NULL.
  2424. * @format: should formatting spaces be added.
  2425. *
  2426. * Dump an XML document to a file or an URL.
  2427. *
  2428. * Returns the number of bytes written or -1 in case of error.
  2429. * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
  2430. * or xmlKeepBlanksDefault(0) was called
  2431. */
  2432. int
  2433. xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur,
  2434. const char * encoding, int format ) {
  2435. xmlSaveCtxt ctxt;
  2436. xmlOutputBufferPtr buf;
  2437. xmlCharEncodingHandlerPtr handler = NULL;
  2438. int ret;
  2439. if (cur == NULL)
  2440. return(-1);
  2441. if (encoding == NULL)
  2442. encoding = (const char *) cur->encoding;
  2443. if (encoding != NULL) {
  2444. handler = xmlFindCharEncodingHandler(encoding);
  2445. if (handler == NULL)
  2446. return(-1);
  2447. }
  2448. #ifdef LIBXML_ZLIB_ENABLED
  2449. if (cur->compression < 0) cur->compression = xmlGetCompressMode();
  2450. #endif
  2451. /*
  2452. * save the content to a temp buffer.
  2453. */
  2454. buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
  2455. if (buf == NULL) return(-1);
  2456. memset(&ctxt, 0, sizeof(ctxt));
  2457. ctxt.buf = buf;
  2458. ctxt.level = 0;
  2459. ctxt.format = format ? 1 : 0;
  2460. ctxt.encoding = (const xmlChar *) encoding;
  2461. xmlSaveCtxtInit(&ctxt);
  2462. ctxt.options |= XML_SAVE_AS_XML;
  2463. xmlDocContentDumpOutput(&ctxt, cur);
  2464. ret = xmlOutputBufferClose(buf);
  2465. return(ret);
  2466. }
  2467. /**
  2468. * xmlSaveFileEnc:
  2469. * @filename: the filename (or URL)
  2470. * @cur: the document
  2471. * @encoding: the name of an encoding (or NULL)
  2472. *
  2473. * Dump an XML document, converting it to the given encoding
  2474. *
  2475. * returns: the number of bytes written or -1 in case of failure.
  2476. */
  2477. int
  2478. xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
  2479. return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) );
  2480. }
  2481. /**
  2482. * xmlSaveFormatFile:
  2483. * @filename: the filename (or URL)
  2484. * @cur: the document
  2485. * @format: should formatting spaces been added
  2486. *
  2487. * Dump an XML document to a file. Will use compression if
  2488. * compiled in and enabled. If @filename is "-" the stdout file is
  2489. * used. If @format is set then the document will be indented on output.
  2490. * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
  2491. * or xmlKeepBlanksDefault(0) was called
  2492. *
  2493. * returns: the number of bytes written or -1 in case of failure.
  2494. */
  2495. int
  2496. xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
  2497. return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) );
  2498. }
  2499. /**
  2500. * xmlSaveFile:
  2501. * @filename: the filename (or URL)
  2502. * @cur: the document
  2503. *
  2504. * Dump an XML document to a file. Will use compression if
  2505. * compiled in and enabled. If @filename is "-" the stdout file is
  2506. * used.
  2507. * returns: the number of bytes written or -1 in case of failure.
  2508. */
  2509. int
  2510. xmlSaveFile(const char *filename, xmlDocPtr cur) {
  2511. return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
  2512. }
  2513. #endif /* LIBXML_OUTPUT_ENABLED */
  2514. #define bottom_xmlsave
  2515. #include "elfgcchack.h"