xmlsave.c 79 KB

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