xmlsave.c 70 KB

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