xmlsave.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725
  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;
  789. xmlAttrPtr attr;
  790. xmlChar *start, *end;
  791. xmlOutputBufferPtr buf;
  792. if (cur == NULL) return;
  793. buf = ctxt->buf;
  794. root = cur;
  795. while (1) {
  796. switch (cur->type) {
  797. case XML_DOCUMENT_NODE:
  798. case XML_HTML_DOCUMENT_NODE:
  799. xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur);
  800. break;
  801. case XML_DTD_NODE:
  802. xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
  803. break;
  804. case XML_DOCUMENT_FRAG_NODE:
  805. if (cur->children != NULL) {
  806. cur = cur->children;
  807. continue;
  808. }
  809. break;
  810. case XML_ELEMENT_DECL:
  811. xmlBufDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
  812. break;
  813. case XML_ATTRIBUTE_DECL:
  814. xmlBufDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
  815. break;
  816. case XML_ENTITY_DECL:
  817. xmlBufDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
  818. break;
  819. case XML_ELEMENT_NODE:
  820. if ((cur != root) && (ctxt->format == 1) && (xmlIndentTreeOutput))
  821. xmlOutputBufferWrite(buf, ctxt->indent_size *
  822. (ctxt->level > ctxt->indent_nr ?
  823. ctxt->indent_nr : ctxt->level),
  824. ctxt->indent);
  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. xmlNsListDumpOutputCtxt(ctxt, cur->nsDef);
  833. for (attr = cur->properties; attr != NULL; attr = attr->next)
  834. xmlAttrDumpOutput(ctxt, attr);
  835. if (cur->children == NULL) {
  836. if ((ctxt->options & XML_SAVE_NO_EMPTY) == 0) {
  837. if (ctxt->format == 2)
  838. xmlOutputBufferWriteWSNonSig(ctxt, 0);
  839. xmlOutputBufferWrite(buf, 2, "/>");
  840. } else {
  841. if (ctxt->format == 2)
  842. xmlOutputBufferWriteWSNonSig(ctxt, 1);
  843. xmlOutputBufferWrite(buf, 3, "></");
  844. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  845. xmlOutputBufferWriteString(buf,
  846. (const char *)cur->ns->prefix);
  847. xmlOutputBufferWrite(buf, 1, ":");
  848. }
  849. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  850. if (ctxt->format == 2)
  851. xmlOutputBufferWriteWSNonSig(ctxt, 0);
  852. xmlOutputBufferWrite(buf, 1, ">");
  853. }
  854. } else {
  855. if (ctxt->format == 1) {
  856. tmp = cur->children;
  857. while (tmp != NULL) {
  858. if ((tmp->type == XML_TEXT_NODE) ||
  859. (tmp->type == XML_CDATA_SECTION_NODE) ||
  860. (tmp->type == XML_ENTITY_REF_NODE)) {
  861. ctxt->format = 0;
  862. unformattedNode = cur;
  863. break;
  864. }
  865. tmp = tmp->next;
  866. }
  867. }
  868. if (ctxt->format == 2)
  869. xmlOutputBufferWriteWSNonSig(ctxt, 1);
  870. xmlOutputBufferWrite(buf, 1, ">");
  871. if (ctxt->format == 1) xmlOutputBufferWrite(buf, 1, "\n");
  872. if (ctxt->level >= 0) ctxt->level++;
  873. cur = cur->children;
  874. continue;
  875. }
  876. break;
  877. case XML_TEXT_NODE:
  878. if (cur->content == NULL)
  879. break;
  880. if (cur->name != xmlStringTextNoenc) {
  881. xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
  882. } else {
  883. /*
  884. * Disable escaping, needed for XSLT
  885. */
  886. xmlOutputBufferWriteString(buf, (const char *) cur->content);
  887. }
  888. break;
  889. case XML_PI_NODE:
  890. if ((cur != root) && (ctxt->format == 1) && (xmlIndentTreeOutput))
  891. xmlOutputBufferWrite(buf, ctxt->indent_size *
  892. (ctxt->level > ctxt->indent_nr ?
  893. ctxt->indent_nr : ctxt->level),
  894. ctxt->indent);
  895. if (cur->content != NULL) {
  896. xmlOutputBufferWrite(buf, 2, "<?");
  897. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  898. if (cur->content != NULL) {
  899. if (ctxt->format == 2)
  900. xmlOutputBufferWriteWSNonSig(ctxt, 0);
  901. else
  902. xmlOutputBufferWrite(buf, 1, " ");
  903. xmlOutputBufferWriteString(buf,
  904. (const char *)cur->content);
  905. }
  906. xmlOutputBufferWrite(buf, 2, "?>");
  907. } else {
  908. xmlOutputBufferWrite(buf, 2, "<?");
  909. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  910. if (ctxt->format == 2)
  911. xmlOutputBufferWriteWSNonSig(ctxt, 0);
  912. xmlOutputBufferWrite(buf, 2, "?>");
  913. }
  914. break;
  915. case XML_COMMENT_NODE:
  916. if ((cur != root) && (ctxt->format == 1) && (xmlIndentTreeOutput))
  917. xmlOutputBufferWrite(buf, ctxt->indent_size *
  918. (ctxt->level > ctxt->indent_nr ?
  919. ctxt->indent_nr : ctxt->level),
  920. ctxt->indent);
  921. if (cur->content != NULL) {
  922. xmlOutputBufferWrite(buf, 4, "<!--");
  923. xmlOutputBufferWriteString(buf, (const char *)cur->content);
  924. xmlOutputBufferWrite(buf, 3, "-->");
  925. }
  926. break;
  927. case XML_ENTITY_REF_NODE:
  928. xmlOutputBufferWrite(buf, 1, "&");
  929. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  930. xmlOutputBufferWrite(buf, 1, ";");
  931. break;
  932. case XML_CDATA_SECTION_NODE:
  933. if (cur->content == NULL || *cur->content == '\0') {
  934. xmlOutputBufferWrite(buf, 12, "<![CDATA[]]>");
  935. } else {
  936. start = end = cur->content;
  937. while (*end != '\0') {
  938. if ((*end == ']') && (*(end + 1) == ']') &&
  939. (*(end + 2) == '>')) {
  940. end = end + 2;
  941. xmlOutputBufferWrite(buf, 9, "<![CDATA[");
  942. xmlOutputBufferWrite(buf, end - start,
  943. (const char *)start);
  944. xmlOutputBufferWrite(buf, 3, "]]>");
  945. start = end;
  946. }
  947. end++;
  948. }
  949. if (start != end) {
  950. xmlOutputBufferWrite(buf, 9, "<![CDATA[");
  951. xmlOutputBufferWriteString(buf, (const char *)start);
  952. xmlOutputBufferWrite(buf, 3, "]]>");
  953. }
  954. }
  955. break;
  956. case XML_ATTRIBUTE_NODE:
  957. xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur);
  958. break;
  959. case XML_NAMESPACE_DECL:
  960. xmlNsDumpOutputCtxt(ctxt, (xmlNsPtr) cur);
  961. break;
  962. default:
  963. break;
  964. }
  965. while (1) {
  966. if (cur == root)
  967. return;
  968. if ((ctxt->format == 1) &&
  969. (cur->type != XML_XINCLUDE_START) &&
  970. (cur->type != XML_XINCLUDE_END))
  971. xmlOutputBufferWrite(buf, 1, "\n");
  972. if (cur->next != NULL) {
  973. cur = cur->next;
  974. break;
  975. }
  976. /*
  977. * The parent should never be NULL here but we want to handle
  978. * corrupted documents gracefully.
  979. */
  980. if (cur->parent == NULL)
  981. return;
  982. cur = cur->parent;
  983. if (cur->type == XML_ELEMENT_NODE) {
  984. if (ctxt->level > 0) ctxt->level--;
  985. if ((xmlIndentTreeOutput) && (ctxt->format == 1))
  986. xmlOutputBufferWrite(buf, ctxt->indent_size *
  987. (ctxt->level > ctxt->indent_nr ?
  988. ctxt->indent_nr : ctxt->level),
  989. ctxt->indent);
  990. xmlOutputBufferWrite(buf, 2, "</");
  991. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  992. xmlOutputBufferWriteString(buf,
  993. (const char *)cur->ns->prefix);
  994. xmlOutputBufferWrite(buf, 1, ":");
  995. }
  996. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  997. if (ctxt->format == 2)
  998. xmlOutputBufferWriteWSNonSig(ctxt, 0);
  999. xmlOutputBufferWrite(buf, 1, ">");
  1000. if (cur == unformattedNode) {
  1001. ctxt->format = format;
  1002. unformattedNode = NULL;
  1003. }
  1004. }
  1005. }
  1006. }
  1007. }
  1008. /**
  1009. * xmlDocContentDumpOutput:
  1010. * @cur: the document
  1011. *
  1012. * Dump an XML document.
  1013. */
  1014. static int
  1015. xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur) {
  1016. #ifdef LIBXML_HTML_ENABLED
  1017. xmlDtdPtr dtd;
  1018. int is_xhtml = 0;
  1019. #endif
  1020. const xmlChar *oldenc = cur->encoding;
  1021. const xmlChar *oldctxtenc = ctxt->encoding;
  1022. const xmlChar *encoding = ctxt->encoding;
  1023. xmlCharEncodingOutputFunc oldescape = ctxt->escape;
  1024. xmlCharEncodingOutputFunc oldescapeAttr = ctxt->escapeAttr;
  1025. xmlOutputBufferPtr buf = ctxt->buf;
  1026. xmlCharEncoding enc;
  1027. int switched_encoding = 0;
  1028. xmlInitParser();
  1029. if ((cur->type != XML_HTML_DOCUMENT_NODE) &&
  1030. (cur->type != XML_DOCUMENT_NODE))
  1031. return(-1);
  1032. if (ctxt->encoding != NULL) {
  1033. cur->encoding = BAD_CAST ctxt->encoding;
  1034. } else if (cur->encoding != NULL) {
  1035. encoding = cur->encoding;
  1036. }
  1037. if (((cur->type == XML_HTML_DOCUMENT_NODE) &&
  1038. ((ctxt->options & XML_SAVE_AS_XML) == 0) &&
  1039. ((ctxt->options & XML_SAVE_XHTML) == 0)) ||
  1040. (ctxt->options & XML_SAVE_AS_HTML)) {
  1041. #ifdef LIBXML_HTML_ENABLED
  1042. if (encoding != NULL)
  1043. htmlSetMetaEncoding(cur, (const xmlChar *) encoding);
  1044. if (encoding == NULL)
  1045. encoding = htmlGetMetaEncoding(cur);
  1046. if (encoding == NULL)
  1047. encoding = BAD_CAST "HTML";
  1048. if ((encoding != NULL) && (oldctxtenc == NULL) &&
  1049. (buf->encoder == NULL) && (buf->conv == NULL)) {
  1050. if (xmlSaveSwitchEncoding(ctxt, (const char*) encoding) < 0) {
  1051. cur->encoding = oldenc;
  1052. return(-1);
  1053. }
  1054. }
  1055. if (ctxt->options & XML_SAVE_FORMAT)
  1056. htmlDocContentDumpFormatOutput(buf, cur,
  1057. (const char *)encoding, 1);
  1058. else
  1059. htmlDocContentDumpFormatOutput(buf, cur,
  1060. (const char *)encoding, 0);
  1061. if (ctxt->encoding != NULL)
  1062. cur->encoding = oldenc;
  1063. return(0);
  1064. #else
  1065. return(-1);
  1066. #endif
  1067. } else if ((cur->type == XML_DOCUMENT_NODE) ||
  1068. (ctxt->options & XML_SAVE_AS_XML) ||
  1069. (ctxt->options & XML_SAVE_XHTML)) {
  1070. enc = xmlParseCharEncoding((const char*) encoding);
  1071. if ((encoding != NULL) && (oldctxtenc == NULL) &&
  1072. (buf->encoder == NULL) && (buf->conv == NULL) &&
  1073. ((ctxt->options & XML_SAVE_NO_DECL) == 0)) {
  1074. if ((enc != XML_CHAR_ENCODING_UTF8) &&
  1075. (enc != XML_CHAR_ENCODING_NONE) &&
  1076. (enc != XML_CHAR_ENCODING_ASCII)) {
  1077. /*
  1078. * we need to switch to this encoding but just for this
  1079. * document since we output the XMLDecl the conversion
  1080. * must be done to not generate not well formed documents.
  1081. */
  1082. if (xmlSaveSwitchEncoding(ctxt, (const char*) encoding) < 0) {
  1083. cur->encoding = oldenc;
  1084. return(-1);
  1085. }
  1086. switched_encoding = 1;
  1087. }
  1088. if (ctxt->escape == xmlEscapeEntities)
  1089. ctxt->escape = NULL;
  1090. if (ctxt->escapeAttr == xmlEscapeEntities)
  1091. ctxt->escapeAttr = NULL;
  1092. }
  1093. /*
  1094. * Save the XML declaration
  1095. */
  1096. if ((ctxt->options & XML_SAVE_NO_DECL) == 0) {
  1097. xmlOutputBufferWrite(buf, 14, "<?xml version=");
  1098. if (cur->version != NULL)
  1099. xmlBufWriteQuotedString(buf->buffer, cur->version);
  1100. else
  1101. xmlOutputBufferWrite(buf, 5, "\"1.0\"");
  1102. if (encoding != NULL) {
  1103. xmlOutputBufferWrite(buf, 10, " encoding=");
  1104. xmlBufWriteQuotedString(buf->buffer, (xmlChar *) encoding);
  1105. }
  1106. switch (cur->standalone) {
  1107. case 0:
  1108. xmlOutputBufferWrite(buf, 16, " standalone=\"no\"");
  1109. break;
  1110. case 1:
  1111. xmlOutputBufferWrite(buf, 17, " standalone=\"yes\"");
  1112. break;
  1113. }
  1114. xmlOutputBufferWrite(buf, 3, "?>\n");
  1115. }
  1116. #ifdef LIBXML_HTML_ENABLED
  1117. if (ctxt->options & XML_SAVE_XHTML)
  1118. is_xhtml = 1;
  1119. if ((ctxt->options & XML_SAVE_NO_XHTML) == 0) {
  1120. dtd = xmlGetIntSubset(cur);
  1121. if (dtd != NULL) {
  1122. is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
  1123. if (is_xhtml < 0) is_xhtml = 0;
  1124. }
  1125. }
  1126. #endif
  1127. if (cur->children != NULL) {
  1128. xmlNodePtr child = cur->children;
  1129. while (child != NULL) {
  1130. ctxt->level = 0;
  1131. #ifdef LIBXML_HTML_ENABLED
  1132. if (is_xhtml)
  1133. xhtmlNodeDumpOutput(ctxt, child);
  1134. else
  1135. #endif
  1136. xmlNodeDumpOutputInternal(ctxt, child);
  1137. if ((child->type != XML_XINCLUDE_START) &&
  1138. (child->type != XML_XINCLUDE_END))
  1139. xmlOutputBufferWrite(buf, 1, "\n");
  1140. child = child->next;
  1141. }
  1142. }
  1143. }
  1144. /*
  1145. * Restore the state of the saving context at the end of the document
  1146. */
  1147. if ((switched_encoding) && (oldctxtenc == NULL)) {
  1148. xmlSaveClearEncoding(ctxt);
  1149. ctxt->escape = oldescape;
  1150. ctxt->escapeAttr = oldescapeAttr;
  1151. }
  1152. cur->encoding = oldenc;
  1153. return(0);
  1154. }
  1155. #ifdef LIBXML_HTML_ENABLED
  1156. /************************************************************************
  1157. * *
  1158. * Functions specific to XHTML serialization *
  1159. * *
  1160. ************************************************************************/
  1161. /**
  1162. * xhtmlIsEmpty:
  1163. * @node: the node
  1164. *
  1165. * Check if a node is an empty xhtml node
  1166. *
  1167. * Returns 1 if the node is an empty node, 0 if not and -1 in case of error
  1168. */
  1169. static int
  1170. xhtmlIsEmpty(xmlNodePtr node) {
  1171. if (node == NULL)
  1172. return(-1);
  1173. if (node->type != XML_ELEMENT_NODE)
  1174. return(0);
  1175. if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME)))
  1176. return(0);
  1177. if (node->children != NULL)
  1178. return(0);
  1179. switch (node->name[0]) {
  1180. case 'a':
  1181. if (xmlStrEqual(node->name, BAD_CAST "area"))
  1182. return(1);
  1183. return(0);
  1184. case 'b':
  1185. if (xmlStrEqual(node->name, BAD_CAST "br"))
  1186. return(1);
  1187. if (xmlStrEqual(node->name, BAD_CAST "base"))
  1188. return(1);
  1189. if (xmlStrEqual(node->name, BAD_CAST "basefont"))
  1190. return(1);
  1191. return(0);
  1192. case 'c':
  1193. if (xmlStrEqual(node->name, BAD_CAST "col"))
  1194. return(1);
  1195. return(0);
  1196. case 'f':
  1197. if (xmlStrEqual(node->name, BAD_CAST "frame"))
  1198. return(1);
  1199. return(0);
  1200. case 'h':
  1201. if (xmlStrEqual(node->name, BAD_CAST "hr"))
  1202. return(1);
  1203. return(0);
  1204. case 'i':
  1205. if (xmlStrEqual(node->name, BAD_CAST "img"))
  1206. return(1);
  1207. if (xmlStrEqual(node->name, BAD_CAST "input"))
  1208. return(1);
  1209. if (xmlStrEqual(node->name, BAD_CAST "isindex"))
  1210. return(1);
  1211. return(0);
  1212. case 'l':
  1213. if (xmlStrEqual(node->name, BAD_CAST "link"))
  1214. return(1);
  1215. return(0);
  1216. case 'm':
  1217. if (xmlStrEqual(node->name, BAD_CAST "meta"))
  1218. return(1);
  1219. return(0);
  1220. case 'p':
  1221. if (xmlStrEqual(node->name, BAD_CAST "param"))
  1222. return(1);
  1223. return(0);
  1224. }
  1225. return(0);
  1226. }
  1227. /**
  1228. * xhtmlAttrListDumpOutput:
  1229. * @cur: the first attribute pointer
  1230. *
  1231. * Dump a list of XML attributes
  1232. */
  1233. static void
  1234. xhtmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
  1235. xmlAttrPtr xml_lang = NULL;
  1236. xmlAttrPtr lang = NULL;
  1237. xmlAttrPtr name = NULL;
  1238. xmlAttrPtr id = NULL;
  1239. xmlNodePtr parent;
  1240. xmlOutputBufferPtr buf;
  1241. if (cur == NULL) return;
  1242. buf = ctxt->buf;
  1243. parent = cur->parent;
  1244. while (cur != NULL) {
  1245. if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id")))
  1246. id = cur;
  1247. else
  1248. if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name")))
  1249. name = cur;
  1250. else
  1251. if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")))
  1252. lang = cur;
  1253. else
  1254. if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) &&
  1255. (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml")))
  1256. xml_lang = cur;
  1257. else if ((cur->ns == NULL) &&
  1258. ((cur->children == NULL) ||
  1259. (cur->children->content == NULL) ||
  1260. (cur->children->content[0] == 0)) &&
  1261. (htmlIsBooleanAttr(cur->name))) {
  1262. if (cur->children != NULL)
  1263. xmlFreeNode(cur->children);
  1264. cur->children = xmlNewText(cur->name);
  1265. if (cur->children != NULL)
  1266. cur->children->parent = (xmlNodePtr) cur;
  1267. }
  1268. xmlAttrDumpOutput(ctxt, cur);
  1269. cur = cur->next;
  1270. }
  1271. /*
  1272. * C.8
  1273. */
  1274. if ((name != NULL) && (id == NULL)) {
  1275. if ((parent != NULL) && (parent->name != NULL) &&
  1276. ((xmlStrEqual(parent->name, BAD_CAST "a")) ||
  1277. (xmlStrEqual(parent->name, BAD_CAST "p")) ||
  1278. (xmlStrEqual(parent->name, BAD_CAST "div")) ||
  1279. (xmlStrEqual(parent->name, BAD_CAST "img")) ||
  1280. (xmlStrEqual(parent->name, BAD_CAST "map")) ||
  1281. (xmlStrEqual(parent->name, BAD_CAST "applet")) ||
  1282. (xmlStrEqual(parent->name, BAD_CAST "form")) ||
  1283. (xmlStrEqual(parent->name, BAD_CAST "frame")) ||
  1284. (xmlStrEqual(parent->name, BAD_CAST "iframe")))) {
  1285. xmlOutputBufferWrite(buf, 5, " id=\"");
  1286. xmlAttrSerializeContent(buf, name);
  1287. xmlOutputBufferWrite(buf, 1, "\"");
  1288. }
  1289. }
  1290. /*
  1291. * C.7.
  1292. */
  1293. if ((lang != NULL) && (xml_lang == NULL)) {
  1294. xmlOutputBufferWrite(buf, 11, " xml:lang=\"");
  1295. xmlAttrSerializeContent(buf, lang);
  1296. xmlOutputBufferWrite(buf, 1, "\"");
  1297. } else
  1298. if ((xml_lang != NULL) && (lang == NULL)) {
  1299. xmlOutputBufferWrite(buf, 7, " lang=\"");
  1300. xmlAttrSerializeContent(buf, xml_lang);
  1301. xmlOutputBufferWrite(buf, 1, "\"");
  1302. }
  1303. }
  1304. /**
  1305. * xhtmlNodeDumpOutput:
  1306. * @buf: the XML buffer output
  1307. * @doc: the XHTML document
  1308. * @cur: the current node
  1309. * @level: the imbrication level for indenting
  1310. * @format: is formatting allowed
  1311. * @encoding: an optional encoding string
  1312. *
  1313. * Dump an XHTML node, recursive behaviour, children are printed too.
  1314. */
  1315. static void
  1316. xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
  1317. int format = ctxt->format, addmeta;
  1318. xmlNodePtr tmp, root, unformattedNode = NULL;
  1319. xmlChar *start, *end;
  1320. xmlOutputBufferPtr buf = ctxt->buf;
  1321. if (cur == NULL) return;
  1322. root = cur;
  1323. while (1) {
  1324. switch (cur->type) {
  1325. case XML_DOCUMENT_NODE:
  1326. case XML_HTML_DOCUMENT_NODE:
  1327. xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur);
  1328. break;
  1329. case XML_NAMESPACE_DECL:
  1330. xmlNsDumpOutputCtxt(ctxt, (xmlNsPtr) cur);
  1331. break;
  1332. case XML_DTD_NODE:
  1333. xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
  1334. break;
  1335. case XML_DOCUMENT_FRAG_NODE:
  1336. if (cur->children) {
  1337. cur = cur->children;
  1338. continue;
  1339. }
  1340. break;
  1341. case XML_ELEMENT_DECL:
  1342. xmlBufDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
  1343. break;
  1344. case XML_ATTRIBUTE_DECL:
  1345. xmlBufDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
  1346. break;
  1347. case XML_ENTITY_DECL:
  1348. xmlBufDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
  1349. break;
  1350. case XML_ELEMENT_NODE:
  1351. addmeta = 0;
  1352. if ((cur != root) && (ctxt->format == 1) && (xmlIndentTreeOutput))
  1353. xmlOutputBufferWrite(buf, ctxt->indent_size *
  1354. (ctxt->level > ctxt->indent_nr ?
  1355. ctxt->indent_nr : ctxt->level),
  1356. ctxt->indent);
  1357. xmlOutputBufferWrite(buf, 1, "<");
  1358. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  1359. xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
  1360. xmlOutputBufferWrite(buf, 1, ":");
  1361. }
  1362. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  1363. if (cur->nsDef)
  1364. xmlNsListDumpOutputCtxt(ctxt, cur->nsDef);
  1365. if ((xmlStrEqual(cur->name, BAD_CAST "html") &&
  1366. (cur->ns == NULL) && (cur->nsDef == NULL))) {
  1367. /*
  1368. * 3.1.1. Strictly Conforming Documents A.3.1.1 3/
  1369. */
  1370. xmlOutputBufferWriteString(buf,
  1371. " xmlns=\"http://www.w3.org/1999/xhtml\"");
  1372. }
  1373. if (cur->properties != NULL)
  1374. xhtmlAttrListDumpOutput(ctxt, cur->properties);
  1375. if ((cur->parent != NULL) &&
  1376. (cur->parent->parent == (xmlNodePtr) cur->doc) &&
  1377. xmlStrEqual(cur->name, BAD_CAST"head") &&
  1378. xmlStrEqual(cur->parent->name, BAD_CAST"html")) {
  1379. tmp = cur->children;
  1380. while (tmp != NULL) {
  1381. if (xmlStrEqual(tmp->name, BAD_CAST"meta")) {
  1382. xmlChar *httpequiv;
  1383. httpequiv = xmlGetProp(tmp, BAD_CAST"http-equiv");
  1384. if (httpequiv != NULL) {
  1385. if (xmlStrcasecmp(httpequiv,
  1386. BAD_CAST"Content-Type") == 0) {
  1387. xmlFree(httpequiv);
  1388. break;
  1389. }
  1390. xmlFree(httpequiv);
  1391. }
  1392. }
  1393. tmp = tmp->next;
  1394. }
  1395. if (tmp == NULL)
  1396. addmeta = 1;
  1397. }
  1398. if (cur->children == NULL) {
  1399. if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) &&
  1400. ((xhtmlIsEmpty(cur) == 1) && (addmeta == 0))) {
  1401. /*
  1402. * C.2. Empty Elements
  1403. */
  1404. xmlOutputBufferWrite(buf, 3, " />");
  1405. } else {
  1406. if (addmeta == 1) {
  1407. xmlOutputBufferWrite(buf, 1, ">");
  1408. if (ctxt->format == 1) {
  1409. xmlOutputBufferWrite(buf, 1, "\n");
  1410. if (xmlIndentTreeOutput)
  1411. xmlOutputBufferWrite(buf, ctxt->indent_size *
  1412. (ctxt->level + 1 > ctxt->indent_nr ?
  1413. ctxt->indent_nr : ctxt->level + 1),
  1414. ctxt->indent);
  1415. }
  1416. xmlOutputBufferWriteString(buf,
  1417. "<meta http-equiv=\"Content-Type\" "
  1418. "content=\"text/html; charset=");
  1419. if (ctxt->encoding) {
  1420. xmlOutputBufferWriteString(buf,
  1421. (const char *)ctxt->encoding);
  1422. } else {
  1423. xmlOutputBufferWrite(buf, 5, "UTF-8");
  1424. }
  1425. xmlOutputBufferWrite(buf, 4, "\" />");
  1426. if (ctxt->format == 1)
  1427. xmlOutputBufferWrite(buf, 1, "\n");
  1428. } else {
  1429. xmlOutputBufferWrite(buf, 1, ">");
  1430. }
  1431. /*
  1432. * C.3. Element Minimization and Empty Element Content
  1433. */
  1434. xmlOutputBufferWrite(buf, 2, "</");
  1435. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  1436. xmlOutputBufferWriteString(buf,
  1437. (const char *)cur->ns->prefix);
  1438. xmlOutputBufferWrite(buf, 1, ":");
  1439. }
  1440. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  1441. xmlOutputBufferWrite(buf, 1, ">");
  1442. }
  1443. } else {
  1444. xmlOutputBufferWrite(buf, 1, ">");
  1445. if (addmeta == 1) {
  1446. if (ctxt->format == 1) {
  1447. xmlOutputBufferWrite(buf, 1, "\n");
  1448. if (xmlIndentTreeOutput)
  1449. xmlOutputBufferWrite(buf, ctxt->indent_size *
  1450. (ctxt->level + 1 > ctxt->indent_nr ?
  1451. ctxt->indent_nr : ctxt->level + 1),
  1452. ctxt->indent);
  1453. }
  1454. xmlOutputBufferWriteString(buf,
  1455. "<meta http-equiv=\"Content-Type\" "
  1456. "content=\"text/html; charset=");
  1457. if (ctxt->encoding) {
  1458. xmlOutputBufferWriteString(buf,
  1459. (const char *)ctxt->encoding);
  1460. } else {
  1461. xmlOutputBufferWrite(buf, 5, "UTF-8");
  1462. }
  1463. xmlOutputBufferWrite(buf, 4, "\" />");
  1464. }
  1465. if (ctxt->format == 1) {
  1466. tmp = cur->children;
  1467. while (tmp != NULL) {
  1468. if ((tmp->type == XML_TEXT_NODE) ||
  1469. (tmp->type == XML_ENTITY_REF_NODE)) {
  1470. unformattedNode = cur;
  1471. ctxt->format = 0;
  1472. break;
  1473. }
  1474. tmp = tmp->next;
  1475. }
  1476. }
  1477. if (ctxt->format == 1) xmlOutputBufferWrite(buf, 1, "\n");
  1478. if (ctxt->level >= 0) ctxt->level++;
  1479. cur = cur->children;
  1480. continue;
  1481. }
  1482. break;
  1483. case XML_TEXT_NODE:
  1484. if (cur->content == NULL)
  1485. break;
  1486. if ((cur->name == xmlStringText) ||
  1487. (cur->name != xmlStringTextNoenc)) {
  1488. xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
  1489. } else {
  1490. /*
  1491. * Disable escaping, needed for XSLT
  1492. */
  1493. xmlOutputBufferWriteString(buf, (const char *) cur->content);
  1494. }
  1495. break;
  1496. case XML_PI_NODE:
  1497. if (cur->content != NULL) {
  1498. xmlOutputBufferWrite(buf, 2, "<?");
  1499. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  1500. if (cur->content != NULL) {
  1501. xmlOutputBufferWrite(buf, 1, " ");
  1502. xmlOutputBufferWriteString(buf,
  1503. (const char *)cur->content);
  1504. }
  1505. xmlOutputBufferWrite(buf, 2, "?>");
  1506. } else {
  1507. xmlOutputBufferWrite(buf, 2, "<?");
  1508. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  1509. xmlOutputBufferWrite(buf, 2, "?>");
  1510. }
  1511. break;
  1512. case XML_COMMENT_NODE:
  1513. if (cur->content != NULL) {
  1514. xmlOutputBufferWrite(buf, 4, "<!--");
  1515. xmlOutputBufferWriteString(buf, (const char *)cur->content);
  1516. xmlOutputBufferWrite(buf, 3, "-->");
  1517. }
  1518. break;
  1519. case XML_ENTITY_REF_NODE:
  1520. xmlOutputBufferWrite(buf, 1, "&");
  1521. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  1522. xmlOutputBufferWrite(buf, 1, ";");
  1523. break;
  1524. case XML_CDATA_SECTION_NODE:
  1525. if (cur->content == NULL || *cur->content == '\0') {
  1526. xmlOutputBufferWrite(buf, 12, "<![CDATA[]]>");
  1527. } else {
  1528. start = end = cur->content;
  1529. while (*end != '\0') {
  1530. if (*end == ']' && *(end + 1) == ']' &&
  1531. *(end + 2) == '>') {
  1532. end = end + 2;
  1533. xmlOutputBufferWrite(buf, 9, "<![CDATA[");
  1534. xmlOutputBufferWrite(buf, end - start,
  1535. (const char *)start);
  1536. xmlOutputBufferWrite(buf, 3, "]]>");
  1537. start = end;
  1538. }
  1539. end++;
  1540. }
  1541. if (start != end) {
  1542. xmlOutputBufferWrite(buf, 9, "<![CDATA[");
  1543. xmlOutputBufferWriteString(buf, (const char *)start);
  1544. xmlOutputBufferWrite(buf, 3, "]]>");
  1545. }
  1546. }
  1547. break;
  1548. case XML_ATTRIBUTE_NODE:
  1549. xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur);
  1550. break;
  1551. default:
  1552. break;
  1553. }
  1554. while (1) {
  1555. if (cur == root)
  1556. return;
  1557. if (ctxt->format == 1)
  1558. xmlOutputBufferWrite(buf, 1, "\n");
  1559. if (cur->next != NULL) {
  1560. cur = cur->next;
  1561. break;
  1562. }
  1563. /*
  1564. * The parent should never be NULL here but we want to handle
  1565. * corrupted documents gracefully.
  1566. */
  1567. if (cur->parent == NULL)
  1568. return;
  1569. cur = cur->parent;
  1570. if (cur->type == XML_ELEMENT_NODE) {
  1571. if (ctxt->level > 0) ctxt->level--;
  1572. if ((xmlIndentTreeOutput) && (ctxt->format == 1))
  1573. xmlOutputBufferWrite(buf, ctxt->indent_size *
  1574. (ctxt->level > ctxt->indent_nr ?
  1575. ctxt->indent_nr : ctxt->level),
  1576. ctxt->indent);
  1577. xmlOutputBufferWrite(buf, 2, "</");
  1578. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  1579. xmlOutputBufferWriteString(buf,
  1580. (const char *)cur->ns->prefix);
  1581. xmlOutputBufferWrite(buf, 1, ":");
  1582. }
  1583. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  1584. xmlOutputBufferWrite(buf, 1, ">");
  1585. if (cur == unformattedNode) {
  1586. ctxt->format = format;
  1587. unformattedNode = NULL;
  1588. }
  1589. }
  1590. }
  1591. }
  1592. }
  1593. #endif
  1594. /************************************************************************
  1595. * *
  1596. * Public entry points *
  1597. * *
  1598. ************************************************************************/
  1599. /**
  1600. * xmlSaveToFd:
  1601. * @fd: a file descriptor number
  1602. * @encoding: the encoding name to use or NULL
  1603. * @options: a set of xmlSaveOptions
  1604. *
  1605. * Create a document saving context serializing to a file descriptor
  1606. * with the encoding and the options given.
  1607. *
  1608. * Returns a new serialization context or NULL in case of error.
  1609. */
  1610. xmlSaveCtxtPtr
  1611. xmlSaveToFd(int fd, const char *encoding, int options)
  1612. {
  1613. xmlSaveCtxtPtr ret;
  1614. ret = xmlNewSaveCtxt(encoding, options);
  1615. if (ret == NULL) return(NULL);
  1616. ret->buf = xmlOutputBufferCreateFd(fd, ret->handler);
  1617. if (ret->buf == NULL) {
  1618. xmlCharEncCloseFunc(ret->handler);
  1619. xmlFreeSaveCtxt(ret);
  1620. return(NULL);
  1621. }
  1622. return(ret);
  1623. }
  1624. /**
  1625. * xmlSaveToFilename:
  1626. * @filename: a file name or an URL
  1627. * @encoding: the encoding name to use or NULL
  1628. * @options: a set of xmlSaveOptions
  1629. *
  1630. * Create a document saving context serializing to a filename or possibly
  1631. * to an URL (but this is less reliable) with the encoding and the options
  1632. * given.
  1633. *
  1634. * Returns a new serialization context or NULL in case of error.
  1635. */
  1636. xmlSaveCtxtPtr
  1637. xmlSaveToFilename(const char *filename, const char *encoding, int options)
  1638. {
  1639. xmlSaveCtxtPtr ret;
  1640. int compression = 0; /* TODO handle compression option */
  1641. ret = xmlNewSaveCtxt(encoding, options);
  1642. if (ret == NULL) return(NULL);
  1643. ret->buf = xmlOutputBufferCreateFilename(filename, ret->handler,
  1644. compression);
  1645. if (ret->buf == NULL) {
  1646. xmlCharEncCloseFunc(ret->handler);
  1647. xmlFreeSaveCtxt(ret);
  1648. return(NULL);
  1649. }
  1650. return(ret);
  1651. }
  1652. /**
  1653. * xmlSaveToBuffer:
  1654. * @buffer: a buffer
  1655. * @encoding: the encoding name to use or NULL
  1656. * @options: a set of xmlSaveOptions
  1657. *
  1658. * Create a document saving context serializing to a buffer
  1659. * with the encoding and the options given
  1660. *
  1661. * Returns a new serialization context or NULL in case of error.
  1662. */
  1663. xmlSaveCtxtPtr
  1664. xmlSaveToBuffer(xmlBufferPtr buffer, const char *encoding, int options)
  1665. {
  1666. xmlSaveCtxtPtr ret;
  1667. ret = xmlNewSaveCtxt(encoding, options);
  1668. if (ret == NULL) return(NULL);
  1669. ret->buf = xmlOutputBufferCreateBuffer(buffer, ret->handler);
  1670. if (ret->buf == NULL) {
  1671. xmlCharEncCloseFunc(ret->handler);
  1672. xmlFreeSaveCtxt(ret);
  1673. return(NULL);
  1674. }
  1675. return(ret);
  1676. }
  1677. /**
  1678. * xmlSaveToIO:
  1679. * @iowrite: an I/O write function
  1680. * @ioclose: an I/O close function
  1681. * @ioctx: an I/O handler
  1682. * @encoding: the encoding name to use or NULL
  1683. * @options: a set of xmlSaveOptions
  1684. *
  1685. * Create a document saving context serializing to a file descriptor
  1686. * with the encoding and the options given
  1687. *
  1688. * Returns a new serialization context or NULL in case of error.
  1689. */
  1690. xmlSaveCtxtPtr
  1691. xmlSaveToIO(xmlOutputWriteCallback iowrite,
  1692. xmlOutputCloseCallback ioclose,
  1693. void *ioctx, const char *encoding, int options)
  1694. {
  1695. xmlSaveCtxtPtr ret;
  1696. ret = xmlNewSaveCtxt(encoding, options);
  1697. if (ret == NULL) return(NULL);
  1698. ret->buf = xmlOutputBufferCreateIO(iowrite, ioclose, ioctx, ret->handler);
  1699. if (ret->buf == NULL) {
  1700. xmlCharEncCloseFunc(ret->handler);
  1701. xmlFreeSaveCtxt(ret);
  1702. return(NULL);
  1703. }
  1704. return(ret);
  1705. }
  1706. /**
  1707. * xmlSaveDoc:
  1708. * @ctxt: a document saving context
  1709. * @doc: a document
  1710. *
  1711. * Save a full document to a saving context
  1712. * TODO: The function is not fully implemented yet as it does not return the
  1713. * byte count but 0 instead
  1714. *
  1715. * Returns the number of byte written or -1 in case of error
  1716. */
  1717. long
  1718. xmlSaveDoc(xmlSaveCtxtPtr ctxt, xmlDocPtr doc)
  1719. {
  1720. long ret = 0;
  1721. if ((ctxt == NULL) || (doc == NULL)) return(-1);
  1722. if (xmlDocContentDumpOutput(ctxt, doc) < 0)
  1723. return(-1);
  1724. return(ret);
  1725. }
  1726. /**
  1727. * xmlSaveTree:
  1728. * @ctxt: a document saving context
  1729. * @node: the top node of the subtree to save
  1730. *
  1731. * Save a subtree starting at the node parameter to a saving context
  1732. * TODO: The function is not fully implemented yet as it does not return the
  1733. * byte count but 0 instead
  1734. *
  1735. * Returns the number of byte written or -1 in case of error
  1736. */
  1737. long
  1738. xmlSaveTree(xmlSaveCtxtPtr ctxt, xmlNodePtr cur)
  1739. {
  1740. long ret = 0;
  1741. if ((ctxt == NULL) || (cur == NULL)) return(-1);
  1742. #ifdef LIBXML_HTML_ENABLED
  1743. if (ctxt->options & XML_SAVE_XHTML) {
  1744. xhtmlNodeDumpOutput(ctxt, cur);
  1745. return(ret);
  1746. }
  1747. if (((cur->type != XML_NAMESPACE_DECL) && (cur->doc != NULL) &&
  1748. (cur->doc->type == XML_HTML_DOCUMENT_NODE) &&
  1749. ((ctxt->options & XML_SAVE_AS_XML) == 0)) ||
  1750. (ctxt->options & XML_SAVE_AS_HTML)) {
  1751. htmlNodeDumpOutputInternal(ctxt, cur);
  1752. return(ret);
  1753. }
  1754. #endif
  1755. xmlNodeDumpOutputInternal(ctxt, cur);
  1756. return(ret);
  1757. }
  1758. /**
  1759. * xmlSaveFlush:
  1760. * @ctxt: a document saving context
  1761. *
  1762. * Flush a document saving context, i.e. make sure that all bytes have
  1763. * been output.
  1764. *
  1765. * Returns the number of byte written or -1 in case of error.
  1766. */
  1767. int
  1768. xmlSaveFlush(xmlSaveCtxtPtr ctxt)
  1769. {
  1770. if (ctxt == NULL) return(-1);
  1771. if (ctxt->buf == NULL) return(-1);
  1772. return(xmlOutputBufferFlush(ctxt->buf));
  1773. }
  1774. /**
  1775. * xmlSaveClose:
  1776. * @ctxt: a document saving context
  1777. *
  1778. * Close a document saving context, i.e. make sure that all bytes have
  1779. * been output and free the associated data.
  1780. *
  1781. * Returns the number of byte written or -1 in case of error.
  1782. */
  1783. int
  1784. xmlSaveClose(xmlSaveCtxtPtr ctxt)
  1785. {
  1786. int ret;
  1787. if (ctxt == NULL) return(-1);
  1788. ret = xmlSaveFlush(ctxt);
  1789. xmlFreeSaveCtxt(ctxt);
  1790. return(ret);
  1791. }
  1792. /**
  1793. * xmlSaveSetEscape:
  1794. * @ctxt: a document saving context
  1795. * @escape: the escaping function
  1796. *
  1797. * Set a custom escaping function to be used for text in element content
  1798. *
  1799. * Returns 0 if successful or -1 in case of error.
  1800. */
  1801. int
  1802. xmlSaveSetEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
  1803. {
  1804. if (ctxt == NULL) return(-1);
  1805. ctxt->escape = escape;
  1806. return(0);
  1807. }
  1808. /**
  1809. * xmlSaveSetAttrEscape:
  1810. * @ctxt: a document saving context
  1811. * @escape: the escaping function
  1812. *
  1813. * Set a custom escaping function to be used for text in attribute content
  1814. *
  1815. * Returns 0 if successful or -1 in case of error.
  1816. */
  1817. int
  1818. xmlSaveSetAttrEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
  1819. {
  1820. if (ctxt == NULL) return(-1);
  1821. ctxt->escapeAttr = escape;
  1822. return(0);
  1823. }
  1824. /************************************************************************
  1825. * *
  1826. * Public entry points based on buffers *
  1827. * *
  1828. ************************************************************************/
  1829. /**
  1830. * xmlBufAttrSerializeTxtContent:
  1831. * @buf: and xmlBufPtr output
  1832. * @doc: the document
  1833. * @attr: the attribute node
  1834. * @string: the text content
  1835. *
  1836. * Serialize text attribute values to an xmlBufPtr
  1837. */
  1838. void
  1839. xmlBufAttrSerializeTxtContent(xmlBufPtr buf, xmlDocPtr doc,
  1840. xmlAttrPtr attr, const xmlChar * string)
  1841. {
  1842. xmlChar *base, *cur;
  1843. if (string == NULL)
  1844. return;
  1845. base = cur = (xmlChar *) string;
  1846. while (*cur != 0) {
  1847. if (*cur == '\n') {
  1848. if (base != cur)
  1849. xmlBufAdd(buf, base, cur - base);
  1850. xmlBufAdd(buf, BAD_CAST "&#10;", 5);
  1851. cur++;
  1852. base = cur;
  1853. } else if (*cur == '\r') {
  1854. if (base != cur)
  1855. xmlBufAdd(buf, base, cur - base);
  1856. xmlBufAdd(buf, BAD_CAST "&#13;", 5);
  1857. cur++;
  1858. base = cur;
  1859. } else if (*cur == '\t') {
  1860. if (base != cur)
  1861. xmlBufAdd(buf, base, cur - base);
  1862. xmlBufAdd(buf, BAD_CAST "&#9;", 4);
  1863. cur++;
  1864. base = cur;
  1865. } else if (*cur == '"') {
  1866. if (base != cur)
  1867. xmlBufAdd(buf, base, cur - base);
  1868. xmlBufAdd(buf, BAD_CAST "&quot;", 6);
  1869. cur++;
  1870. base = cur;
  1871. } else if (*cur == '<') {
  1872. if (base != cur)
  1873. xmlBufAdd(buf, base, cur - base);
  1874. xmlBufAdd(buf, BAD_CAST "&lt;", 4);
  1875. cur++;
  1876. base = cur;
  1877. } else if (*cur == '>') {
  1878. if (base != cur)
  1879. xmlBufAdd(buf, base, cur - base);
  1880. xmlBufAdd(buf, BAD_CAST "&gt;", 4);
  1881. cur++;
  1882. base = cur;
  1883. } else if (*cur == '&') {
  1884. if (base != cur)
  1885. xmlBufAdd(buf, base, cur - base);
  1886. xmlBufAdd(buf, BAD_CAST "&amp;", 5);
  1887. cur++;
  1888. base = cur;
  1889. } else if ((*cur >= 0x80) && (cur[1] != 0) &&
  1890. ((doc == NULL) || (doc->encoding == NULL))) {
  1891. /*
  1892. * We assume we have UTF-8 content.
  1893. */
  1894. unsigned char tmp[12];
  1895. int val = 0, l = 1;
  1896. if (base != cur)
  1897. xmlBufAdd(buf, base, cur - base);
  1898. if (*cur < 0xC0) {
  1899. xmlSaveErr(XML_SAVE_NOT_UTF8, (xmlNodePtr) attr, NULL);
  1900. xmlSerializeHexCharRef(tmp, *cur);
  1901. xmlBufAdd(buf, (xmlChar *) tmp, -1);
  1902. cur++;
  1903. base = cur;
  1904. continue;
  1905. } else if (*cur < 0xE0) {
  1906. val = (cur[0]) & 0x1F;
  1907. val <<= 6;
  1908. val |= (cur[1]) & 0x3F;
  1909. l = 2;
  1910. } else if ((*cur < 0xF0) && (cur [2] != 0)) {
  1911. val = (cur[0]) & 0x0F;
  1912. val <<= 6;
  1913. val |= (cur[1]) & 0x3F;
  1914. val <<= 6;
  1915. val |= (cur[2]) & 0x3F;
  1916. l = 3;
  1917. } else if ((*cur < 0xF8) && (cur [2] != 0) && (cur[3] != 0)) {
  1918. val = (cur[0]) & 0x07;
  1919. val <<= 6;
  1920. val |= (cur[1]) & 0x3F;
  1921. val <<= 6;
  1922. val |= (cur[2]) & 0x3F;
  1923. val <<= 6;
  1924. val |= (cur[3]) & 0x3F;
  1925. l = 4;
  1926. }
  1927. if ((l == 1) || (!IS_CHAR(val))) {
  1928. xmlSaveErr(XML_SAVE_CHAR_INVALID, (xmlNodePtr) attr, NULL);
  1929. xmlSerializeHexCharRef(tmp, *cur);
  1930. xmlBufAdd(buf, (xmlChar *) tmp, -1);
  1931. cur++;
  1932. base = cur;
  1933. continue;
  1934. }
  1935. /*
  1936. * We could do multiple things here. Just save
  1937. * as a char ref
  1938. */
  1939. xmlSerializeHexCharRef(tmp, val);
  1940. xmlBufAdd(buf, (xmlChar *) tmp, -1);
  1941. cur += l;
  1942. base = cur;
  1943. } else {
  1944. cur++;
  1945. }
  1946. }
  1947. if (base != cur)
  1948. xmlBufAdd(buf, base, cur - base);
  1949. }
  1950. /**
  1951. * xmlAttrSerializeTxtContent:
  1952. * @buf: the XML buffer output
  1953. * @doc: the document
  1954. * @attr: the attribute node
  1955. * @string: the text content
  1956. *
  1957. * Serialize text attribute values to an xml simple buffer
  1958. */
  1959. void
  1960. xmlAttrSerializeTxtContent(xmlBufferPtr buf, xmlDocPtr doc,
  1961. xmlAttrPtr attr, const xmlChar * string)
  1962. {
  1963. xmlBufPtr buffer;
  1964. if ((buf == NULL) || (string == NULL))
  1965. return;
  1966. buffer = xmlBufFromBuffer(buf);
  1967. if (buffer == NULL)
  1968. return;
  1969. xmlBufAttrSerializeTxtContent(buffer, doc, attr, string);
  1970. xmlBufBackToBuffer(buffer);
  1971. }
  1972. /**
  1973. * xmlNodeDump:
  1974. * @buf: the XML buffer output
  1975. * @doc: the document
  1976. * @cur: the current node
  1977. * @level: the imbrication level for indenting
  1978. * @format: is formatting allowed
  1979. *
  1980. * Dump an XML node, recursive behaviour,children are printed too.
  1981. * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
  1982. * or xmlKeepBlanksDefault(0) was called.
  1983. * Since this is using xmlBuffer structures it is limited to 2GB and somehow
  1984. * deprecated, use xmlNodeDumpOutput() instead.
  1985. *
  1986. * Returns the number of bytes written to the buffer or -1 in case of error
  1987. */
  1988. int
  1989. xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
  1990. int format)
  1991. {
  1992. xmlBufPtr buffer;
  1993. size_t ret;
  1994. if ((buf == NULL) || (cur == NULL))
  1995. return(-1);
  1996. buffer = xmlBufFromBuffer(buf);
  1997. if (buffer == NULL)
  1998. return(-1);
  1999. ret = xmlBufNodeDump(buffer, doc, cur, level, format);
  2000. xmlBufBackToBuffer(buffer);
  2001. if (ret > INT_MAX)
  2002. return(-1);
  2003. return((int) ret);
  2004. }
  2005. /**
  2006. * xmlBufNodeDump:
  2007. * @buf: the XML buffer output
  2008. * @doc: the document
  2009. * @cur: the current node
  2010. * @level: the imbrication level for indenting
  2011. * @format: is formatting allowed
  2012. *
  2013. * Dump an XML node, recursive behaviour,children are printed too.
  2014. * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
  2015. * or xmlKeepBlanksDefault(0) was called
  2016. *
  2017. * Returns the number of bytes written to the buffer, in case of error 0
  2018. * is returned or @buf stores the error
  2019. */
  2020. size_t
  2021. xmlBufNodeDump(xmlBufPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
  2022. int format)
  2023. {
  2024. size_t use;
  2025. int ret;
  2026. xmlOutputBufferPtr outbuf;
  2027. int oldalloc;
  2028. xmlInitParser();
  2029. if (cur == NULL) {
  2030. #ifdef DEBUG_TREE
  2031. xmlGenericError(xmlGenericErrorContext,
  2032. "xmlNodeDump : node == NULL\n");
  2033. #endif
  2034. return (-1);
  2035. }
  2036. if (buf == NULL) {
  2037. #ifdef DEBUG_TREE
  2038. xmlGenericError(xmlGenericErrorContext,
  2039. "xmlNodeDump : buf == NULL\n");
  2040. #endif
  2041. return (-1);
  2042. }
  2043. outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
  2044. if (outbuf == NULL) {
  2045. xmlSaveErrMemory("creating buffer");
  2046. return (-1);
  2047. }
  2048. memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
  2049. outbuf->buffer = buf;
  2050. outbuf->encoder = NULL;
  2051. outbuf->writecallback = NULL;
  2052. outbuf->closecallback = NULL;
  2053. outbuf->context = NULL;
  2054. outbuf->written = 0;
  2055. use = xmlBufUse(buf);
  2056. oldalloc = xmlBufGetAllocationScheme(buf);
  2057. xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_DOUBLEIT);
  2058. xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL);
  2059. xmlBufSetAllocationScheme(buf, oldalloc);
  2060. xmlFree(outbuf);
  2061. ret = xmlBufUse(buf) - use;
  2062. return (ret);
  2063. }
  2064. /**
  2065. * xmlElemDump:
  2066. * @f: the FILE * for the output
  2067. * @doc: the document
  2068. * @cur: the current node
  2069. *
  2070. * Dump an XML/HTML node, recursive behaviour, children are printed too.
  2071. */
  2072. void
  2073. xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur)
  2074. {
  2075. xmlOutputBufferPtr outbuf;
  2076. xmlInitParser();
  2077. if (cur == NULL) {
  2078. #ifdef DEBUG_TREE
  2079. xmlGenericError(xmlGenericErrorContext,
  2080. "xmlElemDump : cur == NULL\n");
  2081. #endif
  2082. return;
  2083. }
  2084. #ifdef DEBUG_TREE
  2085. if (doc == NULL) {
  2086. xmlGenericError(xmlGenericErrorContext,
  2087. "xmlElemDump : doc == NULL\n");
  2088. }
  2089. #endif
  2090. outbuf = xmlOutputBufferCreateFile(f, NULL);
  2091. if (outbuf == NULL)
  2092. return;
  2093. if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
  2094. #ifdef LIBXML_HTML_ENABLED
  2095. htmlNodeDumpOutput(outbuf, doc, cur, NULL);
  2096. #else
  2097. xmlSaveErr(XML_ERR_INTERNAL_ERROR, cur, "HTML support not compiled in\n");
  2098. #endif /* LIBXML_HTML_ENABLED */
  2099. } else
  2100. xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL);
  2101. xmlOutputBufferClose(outbuf);
  2102. }
  2103. /************************************************************************
  2104. * *
  2105. * Saving functions front-ends *
  2106. * *
  2107. ************************************************************************/
  2108. /**
  2109. * xmlNodeDumpOutput:
  2110. * @buf: the XML buffer output
  2111. * @doc: the document
  2112. * @cur: the current node
  2113. * @level: the imbrication level for indenting
  2114. * @format: is formatting allowed
  2115. * @encoding: an optional encoding string
  2116. *
  2117. * Dump an XML node, recursive behaviour, children are printed too.
  2118. * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
  2119. * or xmlKeepBlanksDefault(0) was called
  2120. */
  2121. void
  2122. xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
  2123. int level, int format, const char *encoding)
  2124. {
  2125. xmlSaveCtxt ctxt;
  2126. #ifdef LIBXML_HTML_ENABLED
  2127. xmlDtdPtr dtd;
  2128. int is_xhtml = 0;
  2129. #endif
  2130. xmlInitParser();
  2131. if ((buf == NULL) || (cur == NULL)) return;
  2132. if (encoding == NULL)
  2133. encoding = "UTF-8";
  2134. memset(&ctxt, 0, sizeof(ctxt));
  2135. ctxt.buf = buf;
  2136. ctxt.level = level;
  2137. ctxt.format = format ? 1 : 0;
  2138. ctxt.encoding = (const xmlChar *) encoding;
  2139. xmlSaveCtxtInit(&ctxt);
  2140. ctxt.options |= XML_SAVE_AS_XML;
  2141. #ifdef LIBXML_HTML_ENABLED
  2142. dtd = xmlGetIntSubset(doc);
  2143. if (dtd != NULL) {
  2144. is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
  2145. if (is_xhtml < 0)
  2146. is_xhtml = 0;
  2147. }
  2148. if (is_xhtml)
  2149. xhtmlNodeDumpOutput(&ctxt, cur);
  2150. else
  2151. #endif
  2152. xmlNodeDumpOutputInternal(&ctxt, cur);
  2153. }
  2154. /**
  2155. * xmlDocDumpFormatMemoryEnc:
  2156. * @out_doc: Document to generate XML text from
  2157. * @doc_txt_ptr: Memory pointer for allocated XML text
  2158. * @doc_txt_len: Length of the generated XML text
  2159. * @txt_encoding: Character encoding to use when generating XML text
  2160. * @format: should formatting spaces been added
  2161. *
  2162. * Dump the current DOM tree into memory using the character encoding specified
  2163. * by the caller. Note it is up to the caller of this function to free the
  2164. * allocated memory with xmlFree().
  2165. * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
  2166. * or xmlKeepBlanksDefault(0) was called
  2167. */
  2168. void
  2169. xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
  2170. int * doc_txt_len, const char * txt_encoding,
  2171. int format) {
  2172. xmlSaveCtxt ctxt;
  2173. int dummy = 0;
  2174. xmlOutputBufferPtr out_buff = NULL;
  2175. xmlCharEncodingHandlerPtr conv_hdlr = NULL;
  2176. if (doc_txt_len == NULL) {
  2177. doc_txt_len = &dummy; /* Continue, caller just won't get length */
  2178. }
  2179. if (doc_txt_ptr == NULL) {
  2180. *doc_txt_len = 0;
  2181. return;
  2182. }
  2183. *doc_txt_ptr = NULL;
  2184. *doc_txt_len = 0;
  2185. if (out_doc == NULL) {
  2186. /* No document, no output */
  2187. return;
  2188. }
  2189. /*
  2190. * Validate the encoding value, if provided.
  2191. * This logic is copied from xmlSaveFileEnc.
  2192. */
  2193. if (txt_encoding == NULL)
  2194. txt_encoding = (const char *) out_doc->encoding;
  2195. if (txt_encoding != NULL) {
  2196. conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
  2197. if ( conv_hdlr == NULL ) {
  2198. xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, (xmlNodePtr) out_doc,
  2199. txt_encoding);
  2200. return;
  2201. }
  2202. }
  2203. if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
  2204. xmlSaveErrMemory("creating buffer");
  2205. return;
  2206. }
  2207. memset(&ctxt, 0, sizeof(ctxt));
  2208. ctxt.buf = out_buff;
  2209. ctxt.level = 0;
  2210. ctxt.format = format ? 1 : 0;
  2211. ctxt.encoding = (const xmlChar *) txt_encoding;
  2212. xmlSaveCtxtInit(&ctxt);
  2213. ctxt.options |= XML_SAVE_AS_XML;
  2214. xmlDocContentDumpOutput(&ctxt, out_doc);
  2215. xmlOutputBufferFlush(out_buff);
  2216. if (out_buff->conv != NULL) {
  2217. *doc_txt_len = xmlBufUse(out_buff->conv);
  2218. *doc_txt_ptr = xmlStrndup(xmlBufContent(out_buff->conv), *doc_txt_len);
  2219. } else {
  2220. *doc_txt_len = xmlBufUse(out_buff->buffer);
  2221. *doc_txt_ptr = xmlStrndup(xmlBufContent(out_buff->buffer),*doc_txt_len);
  2222. }
  2223. (void)xmlOutputBufferClose(out_buff);
  2224. if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
  2225. *doc_txt_len = 0;
  2226. xmlSaveErrMemory("creating output");
  2227. }
  2228. return;
  2229. }
  2230. /**
  2231. * xmlDocDumpMemory:
  2232. * @cur: the document
  2233. * @mem: OUT: the memory pointer
  2234. * @size: OUT: the memory length
  2235. *
  2236. * Dump an XML document in memory and return the #xmlChar * and it's size
  2237. * in bytes. It's up to the caller to free the memory with xmlFree().
  2238. * The resulting byte array is zero terminated, though the last 0 is not
  2239. * included in the returned size.
  2240. */
  2241. void
  2242. xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
  2243. xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
  2244. }
  2245. /**
  2246. * xmlDocDumpFormatMemory:
  2247. * @cur: the document
  2248. * @mem: OUT: the memory pointer
  2249. * @size: OUT: the memory length
  2250. * @format: should formatting spaces been added
  2251. *
  2252. *
  2253. * Dump an XML document in memory and return the #xmlChar * and it's size.
  2254. * It's up to the caller to free the memory with xmlFree().
  2255. * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
  2256. * or xmlKeepBlanksDefault(0) was called
  2257. */
  2258. void
  2259. xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
  2260. xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
  2261. }
  2262. /**
  2263. * xmlDocDumpMemoryEnc:
  2264. * @out_doc: Document to generate XML text from
  2265. * @doc_txt_ptr: Memory pointer for allocated XML text
  2266. * @doc_txt_len: Length of the generated XML text
  2267. * @txt_encoding: Character encoding to use when generating XML text
  2268. *
  2269. * Dump the current DOM tree into memory using the character encoding specified
  2270. * by the caller. Note it is up to the caller of this function to free the
  2271. * allocated memory with xmlFree().
  2272. */
  2273. void
  2274. xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
  2275. int * doc_txt_len, const char * txt_encoding) {
  2276. xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
  2277. txt_encoding, 0);
  2278. }
  2279. /**
  2280. * xmlDocFormatDump:
  2281. * @f: the FILE*
  2282. * @cur: the document
  2283. * @format: should formatting spaces been added
  2284. *
  2285. * Dump an XML document to an open FILE.
  2286. *
  2287. * returns: the number of bytes written or -1 in case of failure.
  2288. * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
  2289. * or xmlKeepBlanksDefault(0) was called
  2290. */
  2291. int
  2292. xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) {
  2293. xmlSaveCtxt ctxt;
  2294. xmlOutputBufferPtr buf;
  2295. const char * encoding;
  2296. xmlCharEncodingHandlerPtr handler = NULL;
  2297. int ret;
  2298. if (cur == NULL) {
  2299. #ifdef DEBUG_TREE
  2300. xmlGenericError(xmlGenericErrorContext,
  2301. "xmlDocDump : document == NULL\n");
  2302. #endif
  2303. return(-1);
  2304. }
  2305. encoding = (const char *) cur->encoding;
  2306. if (encoding != NULL) {
  2307. handler = xmlFindCharEncodingHandler(encoding);
  2308. if (handler == NULL) {
  2309. xmlFree((char *) cur->encoding);
  2310. cur->encoding = NULL;
  2311. encoding = NULL;
  2312. }
  2313. }
  2314. buf = xmlOutputBufferCreateFile(f, handler);
  2315. if (buf == NULL) return(-1);
  2316. memset(&ctxt, 0, sizeof(ctxt));
  2317. ctxt.buf = buf;
  2318. ctxt.level = 0;
  2319. ctxt.format = format ? 1 : 0;
  2320. ctxt.encoding = (const xmlChar *) encoding;
  2321. xmlSaveCtxtInit(&ctxt);
  2322. ctxt.options |= XML_SAVE_AS_XML;
  2323. xmlDocContentDumpOutput(&ctxt, cur);
  2324. ret = xmlOutputBufferClose(buf);
  2325. return(ret);
  2326. }
  2327. /**
  2328. * xmlDocDump:
  2329. * @f: the FILE*
  2330. * @cur: the document
  2331. *
  2332. * Dump an XML document to an open FILE.
  2333. *
  2334. * returns: the number of bytes written or -1 in case of failure.
  2335. */
  2336. int
  2337. xmlDocDump(FILE *f, xmlDocPtr cur) {
  2338. return(xmlDocFormatDump (f, cur, 0));
  2339. }
  2340. /**
  2341. * xmlSaveFileTo:
  2342. * @buf: an output I/O buffer
  2343. * @cur: the document
  2344. * @encoding: the encoding if any assuming the I/O layer handles the transcoding
  2345. *
  2346. * Dump an XML document to an I/O buffer.
  2347. * Warning ! This call xmlOutputBufferClose() on buf which is not available
  2348. * after this call.
  2349. *
  2350. * returns: the number of bytes written or -1 in case of failure.
  2351. */
  2352. int
  2353. xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) {
  2354. xmlSaveCtxt ctxt;
  2355. int ret;
  2356. if (buf == NULL) return(-1);
  2357. if (cur == NULL) {
  2358. xmlOutputBufferClose(buf);
  2359. return(-1);
  2360. }
  2361. memset(&ctxt, 0, sizeof(ctxt));
  2362. ctxt.buf = buf;
  2363. ctxt.level = 0;
  2364. ctxt.format = 0;
  2365. ctxt.encoding = (const xmlChar *) encoding;
  2366. xmlSaveCtxtInit(&ctxt);
  2367. ctxt.options |= XML_SAVE_AS_XML;
  2368. xmlDocContentDumpOutput(&ctxt, cur);
  2369. ret = xmlOutputBufferClose(buf);
  2370. return(ret);
  2371. }
  2372. /**
  2373. * xmlSaveFormatFileTo:
  2374. * @buf: an output I/O buffer
  2375. * @cur: the document
  2376. * @encoding: the encoding if any assuming the I/O layer handles the transcoding
  2377. * @format: should formatting spaces been added
  2378. *
  2379. * Dump an XML document to an I/O buffer.
  2380. * Warning ! This call xmlOutputBufferClose() on buf which is not available
  2381. * after this call.
  2382. *
  2383. * returns: the number of bytes written or -1 in case of failure.
  2384. */
  2385. int
  2386. xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur,
  2387. const char *encoding, int format)
  2388. {
  2389. xmlSaveCtxt ctxt;
  2390. int ret;
  2391. if (buf == NULL) return(-1);
  2392. if ((cur == NULL) ||
  2393. ((cur->type != XML_DOCUMENT_NODE) &&
  2394. (cur->type != XML_HTML_DOCUMENT_NODE))) {
  2395. xmlOutputBufferClose(buf);
  2396. return(-1);
  2397. }
  2398. memset(&ctxt, 0, sizeof(ctxt));
  2399. ctxt.buf = buf;
  2400. ctxt.level = 0;
  2401. ctxt.format = format ? 1 : 0;
  2402. ctxt.encoding = (const xmlChar *) encoding;
  2403. xmlSaveCtxtInit(&ctxt);
  2404. ctxt.options |= XML_SAVE_AS_XML;
  2405. xmlDocContentDumpOutput(&ctxt, cur);
  2406. ret = xmlOutputBufferClose(buf);
  2407. return (ret);
  2408. }
  2409. /**
  2410. * xmlSaveFormatFileEnc:
  2411. * @filename: the filename or URL to output
  2412. * @cur: the document being saved
  2413. * @encoding: the name of the encoding to use or NULL.
  2414. * @format: should formatting spaces be added.
  2415. *
  2416. * Dump an XML document to a file or an URL.
  2417. *
  2418. * Returns the number of bytes written or -1 in case of error.
  2419. * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
  2420. * or xmlKeepBlanksDefault(0) was called
  2421. */
  2422. int
  2423. xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur,
  2424. const char * encoding, int format ) {
  2425. xmlSaveCtxt ctxt;
  2426. xmlOutputBufferPtr buf;
  2427. xmlCharEncodingHandlerPtr handler = NULL;
  2428. int ret;
  2429. if (cur == NULL)
  2430. return(-1);
  2431. if (encoding == NULL)
  2432. encoding = (const char *) cur->encoding;
  2433. if (encoding != NULL) {
  2434. handler = xmlFindCharEncodingHandler(encoding);
  2435. if (handler == NULL)
  2436. return(-1);
  2437. }
  2438. #ifdef LIBXML_ZLIB_ENABLED
  2439. if (cur->compression < 0) cur->compression = xmlGetCompressMode();
  2440. #endif
  2441. /*
  2442. * save the content to a temp buffer.
  2443. */
  2444. buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
  2445. if (buf == NULL) return(-1);
  2446. memset(&ctxt, 0, sizeof(ctxt));
  2447. ctxt.buf = buf;
  2448. ctxt.level = 0;
  2449. ctxt.format = format ? 1 : 0;
  2450. ctxt.encoding = (const xmlChar *) encoding;
  2451. xmlSaveCtxtInit(&ctxt);
  2452. ctxt.options |= XML_SAVE_AS_XML;
  2453. xmlDocContentDumpOutput(&ctxt, cur);
  2454. ret = xmlOutputBufferClose(buf);
  2455. return(ret);
  2456. }
  2457. /**
  2458. * xmlSaveFileEnc:
  2459. * @filename: the filename (or URL)
  2460. * @cur: the document
  2461. * @encoding: the name of an encoding (or NULL)
  2462. *
  2463. * Dump an XML document, converting it to the given encoding
  2464. *
  2465. * returns: the number of bytes written or -1 in case of failure.
  2466. */
  2467. int
  2468. xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
  2469. return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) );
  2470. }
  2471. /**
  2472. * xmlSaveFormatFile:
  2473. * @filename: the filename (or URL)
  2474. * @cur: the document
  2475. * @format: should formatting spaces been added
  2476. *
  2477. * Dump an XML document to a file. Will use compression if
  2478. * compiled in and enabled. If @filename is "-" the stdout file is
  2479. * used. If @format is set then the document will be indented on output.
  2480. * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
  2481. * or xmlKeepBlanksDefault(0) was called
  2482. *
  2483. * returns: the number of bytes written or -1 in case of failure.
  2484. */
  2485. int
  2486. xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
  2487. return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) );
  2488. }
  2489. /**
  2490. * xmlSaveFile:
  2491. * @filename: the filename (or URL)
  2492. * @cur: the document
  2493. *
  2494. * Dump an XML document to a file. Will use compression if
  2495. * compiled in and enabled. If @filename is "-" the stdout file is
  2496. * used.
  2497. * returns: the number of bytes written or -1 in case of failure.
  2498. */
  2499. int
  2500. xmlSaveFile(const char *filename, xmlDocPtr cur) {
  2501. return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
  2502. }
  2503. #endif /* LIBXML_OUTPUT_ENABLED */
  2504. #define bottom_xmlsave
  2505. #include "elfgcchack.h"