xmlwriter.c 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713
  1. /*
  2. * xmlwriter.c: XML text writer implementation
  3. *
  4. * For license and disclaimer see the license and disclaimer of
  5. * libxml2.
  6. *
  7. * alfred@mickautsch.de
  8. */
  9. #define IN_LIBXML
  10. #include "libxml.h"
  11. #include <string.h>
  12. #include <libxml/xmlmemory.h>
  13. #include <libxml/parser.h>
  14. #include <libxml/uri.h>
  15. #include <libxml/HTMLtree.h>
  16. #ifdef LIBXML_WRITER_ENABLED
  17. #include <libxml/xmlwriter.h>
  18. #define B64LINELEN 72
  19. #define B64CRLF "\r\n"
  20. /*
  21. * The following VA_COPY was coded following an example in
  22. * the Samba project. It may not be sufficient for some
  23. * esoteric implementations of va_list (i.e. it may need
  24. * something involving a memcpy) but (hopefully) will be
  25. * sufficient for libxml2.
  26. */
  27. #ifndef VA_COPY
  28. #ifdef HAVE_VA_COPY
  29. #define VA_COPY(dest, src) va_copy(dest, src)
  30. #else
  31. #ifdef HAVE___VA_COPY
  32. #define VA_COPY(dest,src) __va_copy(dest, src)
  33. #else
  34. #define VA_COPY(dest,src) (dest) = (src)
  35. #endif
  36. #endif
  37. #endif
  38. /*
  39. * Types are kept private
  40. */
  41. typedef enum {
  42. XML_TEXTWRITER_NONE = 0,
  43. XML_TEXTWRITER_NAME,
  44. XML_TEXTWRITER_ATTRIBUTE,
  45. XML_TEXTWRITER_TEXT,
  46. XML_TEXTWRITER_PI,
  47. XML_TEXTWRITER_PI_TEXT,
  48. XML_TEXTWRITER_CDATA,
  49. XML_TEXTWRITER_DTD,
  50. XML_TEXTWRITER_DTD_TEXT,
  51. XML_TEXTWRITER_DTD_ELEM,
  52. XML_TEXTWRITER_DTD_ELEM_TEXT,
  53. XML_TEXTWRITER_DTD_ATTL,
  54. XML_TEXTWRITER_DTD_ATTL_TEXT,
  55. XML_TEXTWRITER_DTD_ENTY, /* entity */
  56. XML_TEXTWRITER_DTD_ENTY_TEXT,
  57. XML_TEXTWRITER_DTD_PENT, /* parameter entity */
  58. XML_TEXTWRITER_COMMENT
  59. } xmlTextWriterState;
  60. typedef struct _xmlTextWriterStackEntry xmlTextWriterStackEntry;
  61. struct _xmlTextWriterStackEntry {
  62. xmlChar *name;
  63. xmlTextWriterState state;
  64. };
  65. typedef struct _xmlTextWriterNsStackEntry xmlTextWriterNsStackEntry;
  66. struct _xmlTextWriterNsStackEntry {
  67. xmlChar *prefix;
  68. xmlChar *uri;
  69. xmlLinkPtr elem;
  70. };
  71. struct _xmlTextWriter {
  72. xmlOutputBufferPtr out; /* output buffer */
  73. xmlListPtr nodes; /* element name stack */
  74. xmlListPtr nsstack; /* name spaces stack */
  75. int level;
  76. int indent; /* enable indent */
  77. int doindent; /* internal indent flag */
  78. xmlChar *ichar; /* indent character */
  79. char qchar; /* character used for quoting attribute values */
  80. xmlParserCtxtPtr ctxt;
  81. int no_doc_free;
  82. xmlDocPtr doc;
  83. };
  84. static void xmlFreeTextWriterStackEntry(xmlLinkPtr lk);
  85. static int xmlCmpTextWriterStackEntry(const void *data0,
  86. const void *data1);
  87. static int xmlTextWriterOutputNSDecl(xmlTextWriterPtr writer);
  88. static void xmlFreeTextWriterNsStackEntry(xmlLinkPtr lk);
  89. static int xmlCmpTextWriterNsStackEntry(const void *data0,
  90. const void *data1);
  91. static int xmlTextWriterWriteDocCallback(void *context,
  92. const xmlChar * str, int len);
  93. static int xmlTextWriterCloseDocCallback(void *context);
  94. static xmlChar *xmlTextWriterVSprintf(const char *format, va_list argptr);
  95. static int xmlOutputBufferWriteBase64(xmlOutputBufferPtr out, int len,
  96. const unsigned char *data);
  97. static void xmlTextWriterStartDocumentCallback(void *ctx);
  98. static int xmlTextWriterWriteIndent(xmlTextWriterPtr writer);
  99. static int
  100. xmlTextWriterHandleStateDependencies(xmlTextWriterPtr writer,
  101. xmlTextWriterStackEntry * p);
  102. /**
  103. * xmlWriterErrMsg:
  104. * @ctxt: a writer context
  105. * @error: the error number
  106. * @msg: the error message
  107. *
  108. * Handle a writer error
  109. */
  110. static void
  111. xmlWriterErrMsg(xmlTextWriterPtr ctxt, xmlParserErrors error,
  112. const char *msg)
  113. {
  114. if (ctxt != NULL) {
  115. __xmlRaiseError(NULL, NULL, NULL, ctxt->ctxt,
  116. NULL, XML_FROM_WRITER, error, XML_ERR_FATAL,
  117. NULL, 0, NULL, NULL, NULL, 0, 0, "%s", msg);
  118. } else {
  119. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_WRITER, error,
  120. XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, "%s", msg);
  121. }
  122. }
  123. /**
  124. * xmlWriterErrMsgInt:
  125. * @ctxt: a writer context
  126. * @error: the error number
  127. * @msg: the error message
  128. * @val: an int
  129. *
  130. * Handle a writer error
  131. */
  132. static void
  133. xmlWriterErrMsgInt(xmlTextWriterPtr ctxt, xmlParserErrors error,
  134. const char *msg, int val)
  135. {
  136. if (ctxt != NULL) {
  137. __xmlRaiseError(NULL, NULL, NULL, ctxt->ctxt,
  138. NULL, XML_FROM_WRITER, error, XML_ERR_FATAL,
  139. NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
  140. } else {
  141. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_WRITER, error,
  142. XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
  143. }
  144. }
  145. /**
  146. * xmlNewTextWriter:
  147. * @out: an xmlOutputBufferPtr
  148. *
  149. * Create a new xmlNewTextWriter structure using an xmlOutputBufferPtr
  150. * NOTE: the @out parameter will be deallocated when the writer is closed
  151. * (if the call succeed.)
  152. *
  153. * Returns the new xmlTextWriterPtr or NULL in case of error
  154. */
  155. xmlTextWriterPtr
  156. xmlNewTextWriter(xmlOutputBufferPtr out)
  157. {
  158. xmlTextWriterPtr ret;
  159. ret = (xmlTextWriterPtr) xmlMalloc(sizeof(xmlTextWriter));
  160. if (ret == NULL) {
  161. xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
  162. "xmlNewTextWriter : out of memory!\n");
  163. return NULL;
  164. }
  165. memset(ret, 0, (size_t) sizeof(xmlTextWriter));
  166. ret->nodes = xmlListCreate((xmlListDeallocator)
  167. xmlFreeTextWriterStackEntry,
  168. (xmlListDataCompare)
  169. xmlCmpTextWriterStackEntry);
  170. if (ret->nodes == NULL) {
  171. xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
  172. "xmlNewTextWriter : out of memory!\n");
  173. xmlFree(ret);
  174. return NULL;
  175. }
  176. ret->nsstack = xmlListCreate((xmlListDeallocator)
  177. xmlFreeTextWriterNsStackEntry,
  178. (xmlListDataCompare)
  179. xmlCmpTextWriterNsStackEntry);
  180. if (ret->nsstack == NULL) {
  181. xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
  182. "xmlNewTextWriter : out of memory!\n");
  183. xmlListDelete(ret->nodes);
  184. xmlFree(ret);
  185. return NULL;
  186. }
  187. ret->out = out;
  188. ret->ichar = xmlStrdup(BAD_CAST " ");
  189. ret->qchar = '"';
  190. if (!ret->ichar) {
  191. xmlListDelete(ret->nodes);
  192. xmlListDelete(ret->nsstack);
  193. xmlFree(ret);
  194. xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
  195. "xmlNewTextWriter : out of memory!\n");
  196. return NULL;
  197. }
  198. ret->doc = xmlNewDoc(NULL);
  199. ret->no_doc_free = 0;
  200. return ret;
  201. }
  202. /**
  203. * xmlNewTextWriterFilename:
  204. * @uri: the URI of the resource for the output
  205. * @compression: compress the output?
  206. *
  207. * Create a new xmlNewTextWriter structure with @uri as output
  208. *
  209. * Returns the new xmlTextWriterPtr or NULL in case of error
  210. */
  211. xmlTextWriterPtr
  212. xmlNewTextWriterFilename(const char *uri, int compression)
  213. {
  214. xmlTextWriterPtr ret;
  215. xmlOutputBufferPtr out;
  216. out = xmlOutputBufferCreateFilename(uri, NULL, compression);
  217. if (out == NULL) {
  218. xmlWriterErrMsg(NULL, XML_IO_EIO,
  219. "xmlNewTextWriterFilename : cannot open uri\n");
  220. return NULL;
  221. }
  222. ret = xmlNewTextWriter(out);
  223. if (ret == NULL) {
  224. xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
  225. "xmlNewTextWriterFilename : out of memory!\n");
  226. xmlOutputBufferClose(out);
  227. return NULL;
  228. }
  229. ret->indent = 0;
  230. ret->doindent = 0;
  231. return ret;
  232. }
  233. /**
  234. * xmlNewTextWriterMemory:
  235. * @buf: xmlBufferPtr
  236. * @compression: compress the output?
  237. *
  238. * Create a new xmlNewTextWriter structure with @buf as output
  239. * TODO: handle compression
  240. *
  241. * Returns the new xmlTextWriterPtr or NULL in case of error
  242. */
  243. xmlTextWriterPtr
  244. xmlNewTextWriterMemory(xmlBufferPtr buf, int compression ATTRIBUTE_UNUSED)
  245. {
  246. xmlTextWriterPtr ret;
  247. xmlOutputBufferPtr out;
  248. /*::todo handle compression */
  249. out = xmlOutputBufferCreateBuffer(buf, NULL);
  250. if (out == NULL) {
  251. xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
  252. "xmlNewTextWriterMemory : out of memory!\n");
  253. return NULL;
  254. }
  255. ret = xmlNewTextWriter(out);
  256. if (ret == NULL) {
  257. xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
  258. "xmlNewTextWriterMemory : out of memory!\n");
  259. xmlOutputBufferClose(out);
  260. return NULL;
  261. }
  262. return ret;
  263. }
  264. /**
  265. * xmlNewTextWriterPushParser:
  266. * @ctxt: xmlParserCtxtPtr to hold the new XML document tree
  267. * @compression: compress the output?
  268. *
  269. * Create a new xmlNewTextWriter structure with @ctxt as output
  270. * NOTE: the @ctxt context will be freed with the resulting writer
  271. * (if the call succeeds).
  272. * TODO: handle compression
  273. *
  274. * Returns the new xmlTextWriterPtr or NULL in case of error
  275. */
  276. xmlTextWriterPtr
  277. xmlNewTextWriterPushParser(xmlParserCtxtPtr ctxt,
  278. int compression ATTRIBUTE_UNUSED)
  279. {
  280. xmlTextWriterPtr ret;
  281. xmlOutputBufferPtr out;
  282. if (ctxt == NULL) {
  283. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  284. "xmlNewTextWriterPushParser : invalid context!\n");
  285. return NULL;
  286. }
  287. out = xmlOutputBufferCreateIO((xmlOutputWriteCallback)
  288. xmlTextWriterWriteDocCallback,
  289. (xmlOutputCloseCallback)
  290. xmlTextWriterCloseDocCallback,
  291. (void *) ctxt, NULL);
  292. if (out == NULL) {
  293. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  294. "xmlNewTextWriterPushParser : error at xmlOutputBufferCreateIO!\n");
  295. return NULL;
  296. }
  297. ret = xmlNewTextWriter(out);
  298. if (ret == NULL) {
  299. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  300. "xmlNewTextWriterPushParser : error at xmlNewTextWriter!\n");
  301. xmlOutputBufferClose(out);
  302. return NULL;
  303. }
  304. ret->ctxt = ctxt;
  305. return ret;
  306. }
  307. /**
  308. * xmlNewTextWriterDoc:
  309. * @doc: address of a xmlDocPtr to hold the new XML document tree
  310. * @compression: compress the output?
  311. *
  312. * Create a new xmlNewTextWriter structure with @*doc as output
  313. *
  314. * Returns the new xmlTextWriterPtr or NULL in case of error
  315. */
  316. xmlTextWriterPtr
  317. xmlNewTextWriterDoc(xmlDocPtr * doc, int compression)
  318. {
  319. xmlTextWriterPtr ret;
  320. xmlSAXHandler saxHandler;
  321. xmlParserCtxtPtr ctxt;
  322. memset(&saxHandler, '\0', sizeof(saxHandler));
  323. xmlSAX2InitDefaultSAXHandler(&saxHandler, 1);
  324. saxHandler.startDocument = xmlTextWriterStartDocumentCallback;
  325. saxHandler.startElement = xmlSAX2StartElement;
  326. saxHandler.endElement = xmlSAX2EndElement;
  327. ctxt = xmlCreatePushParserCtxt(&saxHandler, NULL, NULL, 0, NULL);
  328. if (ctxt == NULL) {
  329. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  330. "xmlNewTextWriterDoc : error at xmlCreatePushParserCtxt!\n");
  331. return NULL;
  332. }
  333. /*
  334. * For some reason this seems to completely break if node names
  335. * are interned.
  336. */
  337. ctxt->dictNames = 0;
  338. ctxt->myDoc = xmlNewDoc(BAD_CAST XML_DEFAULT_VERSION);
  339. if (ctxt->myDoc == NULL) {
  340. xmlFreeParserCtxt(ctxt);
  341. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  342. "xmlNewTextWriterDoc : error at xmlNewDoc!\n");
  343. return NULL;
  344. }
  345. ret = xmlNewTextWriterPushParser(ctxt, compression);
  346. if (ret == NULL) {
  347. xmlFreeDoc(ctxt->myDoc);
  348. xmlFreeParserCtxt(ctxt);
  349. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  350. "xmlNewTextWriterDoc : error at xmlNewTextWriterPushParser!\n");
  351. return NULL;
  352. }
  353. xmlSetDocCompressMode(ctxt->myDoc, compression);
  354. if (doc != NULL) {
  355. *doc = ctxt->myDoc;
  356. ret->no_doc_free = 1;
  357. }
  358. return ret;
  359. }
  360. /**
  361. * xmlNewTextWriterTree:
  362. * @doc: xmlDocPtr
  363. * @node: xmlNodePtr or NULL for doc->children
  364. * @compression: compress the output?
  365. *
  366. * Create a new xmlNewTextWriter structure with @doc as output
  367. * starting at @node
  368. *
  369. * Returns the new xmlTextWriterPtr or NULL in case of error
  370. */
  371. xmlTextWriterPtr
  372. xmlNewTextWriterTree(xmlDocPtr doc, xmlNodePtr node, int compression)
  373. {
  374. xmlTextWriterPtr ret;
  375. xmlSAXHandler saxHandler;
  376. xmlParserCtxtPtr ctxt;
  377. if (doc == NULL) {
  378. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  379. "xmlNewTextWriterTree : invalid document tree!\n");
  380. return NULL;
  381. }
  382. memset(&saxHandler, '\0', sizeof(saxHandler));
  383. xmlSAX2InitDefaultSAXHandler(&saxHandler, 1);
  384. saxHandler.startDocument = xmlTextWriterStartDocumentCallback;
  385. saxHandler.startElement = xmlSAX2StartElement;
  386. saxHandler.endElement = xmlSAX2EndElement;
  387. ctxt = xmlCreatePushParserCtxt(&saxHandler, NULL, NULL, 0, NULL);
  388. if (ctxt == NULL) {
  389. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  390. "xmlNewTextWriterDoc : error at xmlCreatePushParserCtxt!\n");
  391. return NULL;
  392. }
  393. /*
  394. * For some reason this seems to completely break if node names
  395. * are interned.
  396. */
  397. ctxt->dictNames = 0;
  398. ret = xmlNewTextWriterPushParser(ctxt, compression);
  399. if (ret == NULL) {
  400. xmlFreeParserCtxt(ctxt);
  401. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  402. "xmlNewTextWriterDoc : error at xmlNewTextWriterPushParser!\n");
  403. return NULL;
  404. }
  405. ctxt->myDoc = doc;
  406. ctxt->node = node;
  407. ret->no_doc_free = 1;
  408. xmlSetDocCompressMode(doc, compression);
  409. return ret;
  410. }
  411. /**
  412. * xmlFreeTextWriter:
  413. * @writer: the xmlTextWriterPtr
  414. *
  415. * Deallocate all the resources associated to the writer
  416. */
  417. void
  418. xmlFreeTextWriter(xmlTextWriterPtr writer)
  419. {
  420. if (writer == NULL)
  421. return;
  422. if (writer->out != NULL)
  423. xmlOutputBufferClose(writer->out);
  424. if (writer->nodes != NULL)
  425. xmlListDelete(writer->nodes);
  426. if (writer->nsstack != NULL)
  427. xmlListDelete(writer->nsstack);
  428. if (writer->ctxt != NULL) {
  429. if ((writer->ctxt->myDoc != NULL) && (writer->no_doc_free == 0)) {
  430. xmlFreeDoc(writer->ctxt->myDoc);
  431. writer->ctxt->myDoc = NULL;
  432. }
  433. xmlFreeParserCtxt(writer->ctxt);
  434. }
  435. if (writer->doc != NULL)
  436. xmlFreeDoc(writer->doc);
  437. if (writer->ichar != NULL)
  438. xmlFree(writer->ichar);
  439. xmlFree(writer);
  440. }
  441. /**
  442. * xmlTextWriterStartDocument:
  443. * @writer: the xmlTextWriterPtr
  444. * @version: the xml version ("1.0") or NULL for default ("1.0")
  445. * @encoding: the encoding or NULL for default
  446. * @standalone: "yes" or "no" or NULL for default
  447. *
  448. * Start a new xml document
  449. *
  450. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  451. */
  452. int
  453. xmlTextWriterStartDocument(xmlTextWriterPtr writer, const char *version,
  454. const char *encoding, const char *standalone)
  455. {
  456. int count;
  457. int sum;
  458. xmlLinkPtr lk;
  459. xmlCharEncodingHandlerPtr encoder;
  460. if ((writer == NULL) || (writer->out == NULL)) {
  461. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  462. "xmlTextWriterStartDocument : invalid writer!\n");
  463. return -1;
  464. }
  465. lk = xmlListFront(writer->nodes);
  466. if ((lk != NULL) && (xmlLinkGetData(lk) != NULL)) {
  467. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  468. "xmlTextWriterStartDocument : not allowed in this context!\n");
  469. return -1;
  470. }
  471. encoder = NULL;
  472. if (encoding != NULL) {
  473. encoder = xmlFindCharEncodingHandler(encoding);
  474. if (encoder == NULL) {
  475. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  476. "xmlTextWriterStartDocument : out of memory!\n");
  477. return -1;
  478. }
  479. }
  480. writer->out->encoder = encoder;
  481. if (encoder != NULL) {
  482. if (writer->out->conv == NULL) {
  483. writer->out->conv = xmlBufferCreateSize(4000);
  484. }
  485. xmlCharEncOutFunc(encoder, writer->out->conv, NULL);
  486. if ((writer->doc != NULL) && (writer->doc->encoding == NULL))
  487. writer->doc->encoding = xmlStrdup((xmlChar *)writer->out->encoder->name);
  488. } else
  489. writer->out->conv = NULL;
  490. sum = 0;
  491. count = xmlOutputBufferWriteString(writer->out, "<?xml version=");
  492. if (count < 0)
  493. return -1;
  494. sum += count;
  495. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  496. if (count < 0)
  497. return -1;
  498. sum += count;
  499. if (version != 0)
  500. count = xmlOutputBufferWriteString(writer->out, version);
  501. else
  502. count = xmlOutputBufferWriteString(writer->out, "1.0");
  503. if (count < 0)
  504. return -1;
  505. sum += count;
  506. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  507. if (count < 0)
  508. return -1;
  509. sum += count;
  510. if (writer->out->encoder != 0) {
  511. count = xmlOutputBufferWriteString(writer->out, " encoding=");
  512. if (count < 0)
  513. return -1;
  514. sum += count;
  515. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  516. if (count < 0)
  517. return -1;
  518. sum += count;
  519. count =
  520. xmlOutputBufferWriteString(writer->out,
  521. writer->out->encoder->name);
  522. if (count < 0)
  523. return -1;
  524. sum += count;
  525. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  526. if (count < 0)
  527. return -1;
  528. sum += count;
  529. }
  530. if (standalone != 0) {
  531. count = xmlOutputBufferWriteString(writer->out, " standalone=");
  532. if (count < 0)
  533. return -1;
  534. sum += count;
  535. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  536. if (count < 0)
  537. return -1;
  538. sum += count;
  539. count = xmlOutputBufferWriteString(writer->out, standalone);
  540. if (count < 0)
  541. return -1;
  542. sum += count;
  543. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  544. if (count < 0)
  545. return -1;
  546. sum += count;
  547. }
  548. count = xmlOutputBufferWriteString(writer->out, "?>\n");
  549. if (count < 0)
  550. return -1;
  551. sum += count;
  552. return sum;
  553. }
  554. /**
  555. * xmlTextWriterEndDocument:
  556. * @writer: the xmlTextWriterPtr
  557. *
  558. * End an xml document. All open elements are closed, and
  559. * the content is flushed to the output.
  560. *
  561. * Returns the bytes written or -1 in case of error
  562. */
  563. int
  564. xmlTextWriterEndDocument(xmlTextWriterPtr writer)
  565. {
  566. int count;
  567. int sum;
  568. xmlLinkPtr lk;
  569. xmlTextWriterStackEntry *p;
  570. if (writer == NULL) {
  571. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  572. "xmlTextWriterEndDocument : invalid writer!\n");
  573. return -1;
  574. }
  575. sum = 0;
  576. while ((lk = xmlListFront(writer->nodes)) != NULL) {
  577. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  578. if (p == 0)
  579. break;
  580. switch (p->state) {
  581. case XML_TEXTWRITER_NAME:
  582. case XML_TEXTWRITER_ATTRIBUTE:
  583. case XML_TEXTWRITER_TEXT:
  584. count = xmlTextWriterEndElement(writer);
  585. if (count < 0)
  586. return -1;
  587. sum += count;
  588. break;
  589. case XML_TEXTWRITER_PI:
  590. case XML_TEXTWRITER_PI_TEXT:
  591. count = xmlTextWriterEndPI(writer);
  592. if (count < 0)
  593. return -1;
  594. sum += count;
  595. break;
  596. case XML_TEXTWRITER_CDATA:
  597. count = xmlTextWriterEndCDATA(writer);
  598. if (count < 0)
  599. return -1;
  600. sum += count;
  601. break;
  602. case XML_TEXTWRITER_DTD:
  603. case XML_TEXTWRITER_DTD_TEXT:
  604. case XML_TEXTWRITER_DTD_ELEM:
  605. case XML_TEXTWRITER_DTD_ELEM_TEXT:
  606. case XML_TEXTWRITER_DTD_ATTL:
  607. case XML_TEXTWRITER_DTD_ATTL_TEXT:
  608. case XML_TEXTWRITER_DTD_ENTY:
  609. case XML_TEXTWRITER_DTD_ENTY_TEXT:
  610. case XML_TEXTWRITER_DTD_PENT:
  611. count = xmlTextWriterEndDTD(writer);
  612. if (count < 0)
  613. return -1;
  614. sum += count;
  615. break;
  616. case XML_TEXTWRITER_COMMENT:
  617. count = xmlTextWriterEndComment(writer);
  618. if (count < 0)
  619. return -1;
  620. sum += count;
  621. break;
  622. default:
  623. break;
  624. }
  625. }
  626. if (!writer->indent) {
  627. count = xmlOutputBufferWriteString(writer->out, "\n");
  628. if (count < 0)
  629. return -1;
  630. sum += count;
  631. }
  632. sum += xmlTextWriterFlush(writer);
  633. return sum;
  634. }
  635. /**
  636. * xmlTextWriterStartComment:
  637. * @writer: the xmlTextWriterPtr
  638. *
  639. * Start an xml comment.
  640. *
  641. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  642. */
  643. int
  644. xmlTextWriterStartComment(xmlTextWriterPtr writer)
  645. {
  646. int count;
  647. int sum;
  648. xmlLinkPtr lk;
  649. xmlTextWriterStackEntry *p;
  650. if (writer == NULL) {
  651. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  652. "xmlTextWriterStartComment : invalid writer!\n");
  653. return -1;
  654. }
  655. sum = 0;
  656. lk = xmlListFront(writer->nodes);
  657. if (lk != 0) {
  658. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  659. if (p != 0) {
  660. switch (p->state) {
  661. case XML_TEXTWRITER_TEXT:
  662. case XML_TEXTWRITER_NONE:
  663. break;
  664. case XML_TEXTWRITER_NAME:
  665. /* Output namespace declarations */
  666. count = xmlTextWriterOutputNSDecl(writer);
  667. if (count < 0)
  668. return -1;
  669. sum += count;
  670. count = xmlOutputBufferWriteString(writer->out, ">");
  671. if (count < 0)
  672. return -1;
  673. sum += count;
  674. if (writer->indent) {
  675. count =
  676. xmlOutputBufferWriteString(writer->out, "\n");
  677. if (count < 0)
  678. return -1;
  679. sum += count;
  680. }
  681. p->state = XML_TEXTWRITER_TEXT;
  682. break;
  683. default:
  684. return -1;
  685. }
  686. }
  687. }
  688. p = (xmlTextWriterStackEntry *)
  689. xmlMalloc(sizeof(xmlTextWriterStackEntry));
  690. if (p == 0) {
  691. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  692. "xmlTextWriterStartElement : out of memory!\n");
  693. return -1;
  694. }
  695. p->name = NULL;
  696. p->state = XML_TEXTWRITER_COMMENT;
  697. xmlListPushFront(writer->nodes, p);
  698. if (writer->indent) {
  699. count = xmlTextWriterWriteIndent(writer);
  700. if (count < 0)
  701. return -1;
  702. sum += count;
  703. }
  704. count = xmlOutputBufferWriteString(writer->out, "<!--");
  705. if (count < 0)
  706. return -1;
  707. sum += count;
  708. return sum;
  709. }
  710. /**
  711. * xmlTextWriterEndComment:
  712. * @writer: the xmlTextWriterPtr
  713. *
  714. * End the current xml coment.
  715. *
  716. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  717. */
  718. int
  719. xmlTextWriterEndComment(xmlTextWriterPtr writer)
  720. {
  721. int count;
  722. int sum;
  723. xmlLinkPtr lk;
  724. xmlTextWriterStackEntry *p;
  725. if (writer == NULL) {
  726. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  727. "xmlTextWriterEndComment : invalid writer!\n");
  728. return -1;
  729. }
  730. lk = xmlListFront(writer->nodes);
  731. if (lk == 0) {
  732. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  733. "xmlTextWriterEndComment : not allowed in this context!\n");
  734. return -1;
  735. }
  736. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  737. if (p == 0)
  738. return -1;
  739. sum = 0;
  740. switch (p->state) {
  741. case XML_TEXTWRITER_COMMENT:
  742. count = xmlOutputBufferWriteString(writer->out, "-->");
  743. if (count < 0)
  744. return -1;
  745. sum += count;
  746. break;
  747. default:
  748. return -1;
  749. }
  750. if (writer->indent) {
  751. count = xmlOutputBufferWriteString(writer->out, "\n");
  752. if (count < 0)
  753. return -1;
  754. sum += count;
  755. }
  756. xmlListPopFront(writer->nodes);
  757. return sum;
  758. }
  759. /**
  760. * xmlTextWriterWriteFormatComment:
  761. * @writer: the xmlTextWriterPtr
  762. * @format: format string (see printf)
  763. * @...: extra parameters for the format
  764. *
  765. * Write an xml comment.
  766. *
  767. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  768. */
  769. int XMLCDECL
  770. xmlTextWriterWriteFormatComment(xmlTextWriterPtr writer,
  771. const char *format, ...)
  772. {
  773. int rc;
  774. va_list ap;
  775. va_start(ap, format);
  776. rc = xmlTextWriterWriteVFormatComment(writer, format, ap);
  777. va_end(ap);
  778. return rc;
  779. }
  780. /**
  781. * xmlTextWriterWriteVFormatComment:
  782. * @writer: the xmlTextWriterPtr
  783. * @format: format string (see printf)
  784. * @argptr: pointer to the first member of the variable argument list.
  785. *
  786. * Write an xml comment.
  787. *
  788. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  789. */
  790. int
  791. xmlTextWriterWriteVFormatComment(xmlTextWriterPtr writer,
  792. const char *format, va_list argptr)
  793. {
  794. int rc;
  795. xmlChar *buf;
  796. if (writer == NULL) {
  797. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  798. "xmlTextWriterWriteVFormatComment : invalid writer!\n");
  799. return -1;
  800. }
  801. buf = xmlTextWriterVSprintf(format, argptr);
  802. if (buf == NULL)
  803. return -1;
  804. rc = xmlTextWriterWriteComment(writer, buf);
  805. xmlFree(buf);
  806. return rc;
  807. }
  808. /**
  809. * xmlTextWriterWriteComment:
  810. * @writer: the xmlTextWriterPtr
  811. * @content: comment string
  812. *
  813. * Write an xml comment.
  814. *
  815. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  816. */
  817. int
  818. xmlTextWriterWriteComment(xmlTextWriterPtr writer, const xmlChar * content)
  819. {
  820. int count;
  821. int sum;
  822. sum = 0;
  823. count = xmlTextWriterStartComment(writer);
  824. if (count < 0)
  825. return -1;
  826. sum += count;
  827. count = xmlTextWriterWriteString(writer, content);
  828. if (count < 0)
  829. return -1;
  830. sum += count;
  831. count = xmlTextWriterEndComment(writer);
  832. if (count < 0)
  833. return -1;
  834. sum += count;
  835. return sum;
  836. }
  837. /**
  838. * xmlTextWriterStartElement:
  839. * @writer: the xmlTextWriterPtr
  840. * @name: element name
  841. *
  842. * Start an xml element.
  843. *
  844. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  845. */
  846. int
  847. xmlTextWriterStartElement(xmlTextWriterPtr writer, const xmlChar * name)
  848. {
  849. int count;
  850. int sum;
  851. xmlLinkPtr lk;
  852. xmlTextWriterStackEntry *p;
  853. if ((writer == NULL) || (name == NULL) || (*name == '\0'))
  854. return -1;
  855. sum = 0;
  856. lk = xmlListFront(writer->nodes);
  857. if (lk != 0) {
  858. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  859. if (p != 0) {
  860. switch (p->state) {
  861. case XML_TEXTWRITER_PI:
  862. case XML_TEXTWRITER_PI_TEXT:
  863. return -1;
  864. case XML_TEXTWRITER_NONE:
  865. break;
  866. case XML_TEXTWRITER_ATTRIBUTE:
  867. count = xmlTextWriterEndAttribute(writer);
  868. if (count < 0)
  869. return -1;
  870. sum += count;
  871. /* fallthrough */
  872. case XML_TEXTWRITER_NAME:
  873. /* Output namespace declarations */
  874. count = xmlTextWriterOutputNSDecl(writer);
  875. if (count < 0)
  876. return -1;
  877. sum += count;
  878. count = xmlOutputBufferWriteString(writer->out, ">");
  879. if (count < 0)
  880. return -1;
  881. sum += count;
  882. if (writer->indent)
  883. count =
  884. xmlOutputBufferWriteString(writer->out, "\n");
  885. p->state = XML_TEXTWRITER_TEXT;
  886. break;
  887. default:
  888. break;
  889. }
  890. }
  891. }
  892. p = (xmlTextWriterStackEntry *)
  893. xmlMalloc(sizeof(xmlTextWriterStackEntry));
  894. if (p == 0) {
  895. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  896. "xmlTextWriterStartElement : out of memory!\n");
  897. return -1;
  898. }
  899. p->name = xmlStrdup(name);
  900. if (p->name == 0) {
  901. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  902. "xmlTextWriterStartElement : out of memory!\n");
  903. xmlFree(p);
  904. return -1;
  905. }
  906. p->state = XML_TEXTWRITER_NAME;
  907. xmlListPushFront(writer->nodes, p);
  908. if (writer->indent) {
  909. count = xmlTextWriterWriteIndent(writer);
  910. sum += count;
  911. }
  912. count = xmlOutputBufferWriteString(writer->out, "<");
  913. if (count < 0)
  914. return -1;
  915. sum += count;
  916. count =
  917. xmlOutputBufferWriteString(writer->out, (const char *) p->name);
  918. if (count < 0)
  919. return -1;
  920. sum += count;
  921. return sum;
  922. }
  923. /**
  924. * xmlTextWriterStartElementNS:
  925. * @writer: the xmlTextWriterPtr
  926. * @prefix: namespace prefix or NULL
  927. * @name: element local name
  928. * @namespaceURI: namespace URI or NULL
  929. *
  930. * Start an xml element with namespace support.
  931. *
  932. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  933. */
  934. int
  935. xmlTextWriterStartElementNS(xmlTextWriterPtr writer,
  936. const xmlChar * prefix, const xmlChar * name,
  937. const xmlChar * namespaceURI)
  938. {
  939. int count;
  940. int sum;
  941. xmlChar *buf;
  942. if ((writer == NULL) || (name == NULL) || (*name == '\0'))
  943. return -1;
  944. buf = NULL;
  945. if (prefix != 0) {
  946. buf = xmlStrdup(prefix);
  947. buf = xmlStrcat(buf, BAD_CAST ":");
  948. }
  949. buf = xmlStrcat(buf, name);
  950. sum = 0;
  951. count = xmlTextWriterStartElement(writer, buf);
  952. xmlFree(buf);
  953. if (count < 0)
  954. return -1;
  955. sum += count;
  956. if (namespaceURI != 0) {
  957. xmlTextWriterNsStackEntry *p = (xmlTextWriterNsStackEntry *)
  958. xmlMalloc(sizeof(xmlTextWriterNsStackEntry));
  959. if (p == 0) {
  960. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  961. "xmlTextWriterStartElementNS : out of memory!\n");
  962. return -1;
  963. }
  964. buf = xmlStrdup(BAD_CAST "xmlns");
  965. if (prefix != 0) {
  966. buf = xmlStrcat(buf, BAD_CAST ":");
  967. buf = xmlStrcat(buf, prefix);
  968. }
  969. p->prefix = buf;
  970. p->uri = xmlStrdup(namespaceURI);
  971. if (p->uri == 0) {
  972. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  973. "xmlTextWriterStartElementNS : out of memory!\n");
  974. xmlFree(p);
  975. return -1;
  976. }
  977. p->elem = xmlListFront(writer->nodes);
  978. xmlListPushFront(writer->nsstack, p);
  979. }
  980. return sum;
  981. }
  982. /**
  983. * xmlTextWriterEndElement:
  984. * @writer: the xmlTextWriterPtr
  985. *
  986. * End the current xml element.
  987. *
  988. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  989. */
  990. int
  991. xmlTextWriterEndElement(xmlTextWriterPtr writer)
  992. {
  993. int count;
  994. int sum;
  995. xmlLinkPtr lk;
  996. xmlTextWriterStackEntry *p;
  997. if (writer == NULL)
  998. return -1;
  999. lk = xmlListFront(writer->nodes);
  1000. if (lk == 0) {
  1001. xmlListDelete(writer->nsstack);
  1002. writer->nsstack = NULL;
  1003. return -1;
  1004. }
  1005. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  1006. if (p == 0) {
  1007. xmlListDelete(writer->nsstack);
  1008. writer->nsstack = NULL;
  1009. return -1;
  1010. }
  1011. sum = 0;
  1012. switch (p->state) {
  1013. case XML_TEXTWRITER_ATTRIBUTE:
  1014. count = xmlTextWriterEndAttribute(writer);
  1015. if (count < 0) {
  1016. xmlListDelete(writer->nsstack);
  1017. writer->nsstack = NULL;
  1018. return -1;
  1019. }
  1020. sum += count;
  1021. /* fallthrough */
  1022. case XML_TEXTWRITER_NAME:
  1023. /* Output namespace declarations */
  1024. count = xmlTextWriterOutputNSDecl(writer);
  1025. if (count < 0)
  1026. return -1;
  1027. sum += count;
  1028. if (writer->indent) /* next element needs indent */
  1029. writer->doindent = 1;
  1030. count = xmlOutputBufferWriteString(writer->out, "/>");
  1031. if (count < 0)
  1032. return -1;
  1033. sum += count;
  1034. break;
  1035. case XML_TEXTWRITER_TEXT:
  1036. if ((writer->indent) && (writer->doindent)) {
  1037. count = xmlTextWriterWriteIndent(writer);
  1038. sum += count;
  1039. writer->doindent = 1;
  1040. } else
  1041. writer->doindent = 1;
  1042. count = xmlOutputBufferWriteString(writer->out, "</");
  1043. if (count < 0)
  1044. return -1;
  1045. sum += count;
  1046. count = xmlOutputBufferWriteString(writer->out,
  1047. (const char *) p->name);
  1048. if (count < 0)
  1049. return -1;
  1050. sum += count;
  1051. count = xmlOutputBufferWriteString(writer->out, ">");
  1052. if (count < 0)
  1053. return -1;
  1054. sum += count;
  1055. break;
  1056. default:
  1057. return -1;
  1058. }
  1059. if (writer->indent) {
  1060. count = xmlOutputBufferWriteString(writer->out, "\n");
  1061. sum += count;
  1062. }
  1063. xmlListPopFront(writer->nodes);
  1064. return sum;
  1065. }
  1066. /**
  1067. * xmlTextWriterFullEndElement:
  1068. * @writer: the xmlTextWriterPtr
  1069. *
  1070. * End the current xml element. Writes an end tag even if the element is empty
  1071. *
  1072. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1073. */
  1074. int
  1075. xmlTextWriterFullEndElement(xmlTextWriterPtr writer)
  1076. {
  1077. int count;
  1078. int sum;
  1079. xmlLinkPtr lk;
  1080. xmlTextWriterStackEntry *p;
  1081. if (writer == NULL)
  1082. return -1;
  1083. lk = xmlListFront(writer->nodes);
  1084. if (lk == 0)
  1085. return -1;
  1086. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  1087. if (p == 0)
  1088. return -1;
  1089. sum = 0;
  1090. switch (p->state) {
  1091. case XML_TEXTWRITER_ATTRIBUTE:
  1092. count = xmlTextWriterEndAttribute(writer);
  1093. if (count < 0)
  1094. return -1;
  1095. sum += count;
  1096. /* fallthrough */
  1097. case XML_TEXTWRITER_NAME:
  1098. /* Output namespace declarations */
  1099. count = xmlTextWriterOutputNSDecl(writer);
  1100. if (count < 0)
  1101. return -1;
  1102. sum += count;
  1103. count = xmlOutputBufferWriteString(writer->out, ">");
  1104. if (count < 0)
  1105. return -1;
  1106. sum += count;
  1107. if (writer->indent)
  1108. writer->doindent = 0;
  1109. /* fallthrough */
  1110. case XML_TEXTWRITER_TEXT:
  1111. if ((writer->indent) && (writer->doindent)) {
  1112. count = xmlTextWriterWriteIndent(writer);
  1113. sum += count;
  1114. writer->doindent = 1;
  1115. } else
  1116. writer->doindent = 1;
  1117. count = xmlOutputBufferWriteString(writer->out, "</");
  1118. if (count < 0)
  1119. return -1;
  1120. sum += count;
  1121. count = xmlOutputBufferWriteString(writer->out,
  1122. (const char *) p->name);
  1123. if (count < 0)
  1124. return -1;
  1125. sum += count;
  1126. count = xmlOutputBufferWriteString(writer->out, ">");
  1127. if (count < 0)
  1128. return -1;
  1129. sum += count;
  1130. break;
  1131. default:
  1132. return -1;
  1133. }
  1134. if (writer->indent) {
  1135. count = xmlOutputBufferWriteString(writer->out, "\n");
  1136. sum += count;
  1137. }
  1138. xmlListPopFront(writer->nodes);
  1139. return sum;
  1140. }
  1141. /**
  1142. * xmlTextWriterWriteFormatRaw:
  1143. * @writer: the xmlTextWriterPtr
  1144. * @format: format string (see printf)
  1145. * @...: extra parameters for the format
  1146. *
  1147. * Write a formatted raw xml text.
  1148. *
  1149. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1150. */
  1151. int XMLCDECL
  1152. xmlTextWriterWriteFormatRaw(xmlTextWriterPtr writer, const char *format,
  1153. ...)
  1154. {
  1155. int rc;
  1156. va_list ap;
  1157. va_start(ap, format);
  1158. rc = xmlTextWriterWriteVFormatRaw(writer, format, ap);
  1159. va_end(ap);
  1160. return rc;
  1161. }
  1162. /**
  1163. * xmlTextWriterWriteVFormatRaw:
  1164. * @writer: the xmlTextWriterPtr
  1165. * @format: format string (see printf)
  1166. * @argptr: pointer to the first member of the variable argument list.
  1167. *
  1168. * Write a formatted raw xml text.
  1169. *
  1170. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1171. */
  1172. int
  1173. xmlTextWriterWriteVFormatRaw(xmlTextWriterPtr writer, const char *format,
  1174. va_list argptr)
  1175. {
  1176. int rc;
  1177. xmlChar *buf;
  1178. if (writer == NULL)
  1179. return -1;
  1180. buf = xmlTextWriterVSprintf(format, argptr);
  1181. if (buf == NULL)
  1182. return -1;
  1183. rc = xmlTextWriterWriteRaw(writer, buf);
  1184. xmlFree(buf);
  1185. return rc;
  1186. }
  1187. /**
  1188. * xmlTextWriterWriteRawLen:
  1189. * @writer: the xmlTextWriterPtr
  1190. * @content: text string
  1191. * @len: length of the text string
  1192. *
  1193. * Write an xml text.
  1194. * TODO: what about entities and special chars??
  1195. *
  1196. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1197. */
  1198. int
  1199. xmlTextWriterWriteRawLen(xmlTextWriterPtr writer, const xmlChar * content,
  1200. int len)
  1201. {
  1202. int count;
  1203. int sum;
  1204. xmlLinkPtr lk;
  1205. xmlTextWriterStackEntry *p;
  1206. if (writer == NULL) {
  1207. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  1208. "xmlTextWriterWriteRawLen : invalid writer!\n");
  1209. return -1;
  1210. }
  1211. if ((content == NULL) || (len < 0)) {
  1212. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  1213. "xmlTextWriterWriteRawLen : invalid content!\n");
  1214. return -1;
  1215. }
  1216. sum = 0;
  1217. lk = xmlListFront(writer->nodes);
  1218. if (lk != 0) {
  1219. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  1220. count = xmlTextWriterHandleStateDependencies(writer, p);
  1221. if (count < 0)
  1222. return -1;
  1223. sum += count;
  1224. }
  1225. if (writer->indent)
  1226. writer->doindent = 0;
  1227. if (content != NULL) {
  1228. count =
  1229. xmlOutputBufferWrite(writer->out, len, (const char *) content);
  1230. if (count < 0)
  1231. return -1;
  1232. sum += count;
  1233. }
  1234. return sum;
  1235. }
  1236. /**
  1237. * xmlTextWriterWriteRaw:
  1238. * @writer: the xmlTextWriterPtr
  1239. * @content: text string
  1240. *
  1241. * Write a raw xml text.
  1242. *
  1243. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1244. */
  1245. int
  1246. xmlTextWriterWriteRaw(xmlTextWriterPtr writer, const xmlChar * content)
  1247. {
  1248. return xmlTextWriterWriteRawLen(writer, content, xmlStrlen(content));
  1249. }
  1250. /**
  1251. * xmlTextWriterWriteFormatString:
  1252. * @writer: the xmlTextWriterPtr
  1253. * @format: format string (see printf)
  1254. * @...: extra parameters for the format
  1255. *
  1256. * Write a formatted xml text.
  1257. *
  1258. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1259. */
  1260. int XMLCDECL
  1261. xmlTextWriterWriteFormatString(xmlTextWriterPtr writer, const char *format,
  1262. ...)
  1263. {
  1264. int rc;
  1265. va_list ap;
  1266. if ((writer == NULL) || (format == NULL))
  1267. return -1;
  1268. va_start(ap, format);
  1269. rc = xmlTextWriterWriteVFormatString(writer, format, ap);
  1270. va_end(ap);
  1271. return rc;
  1272. }
  1273. /**
  1274. * xmlTextWriterWriteVFormatString:
  1275. * @writer: the xmlTextWriterPtr
  1276. * @format: format string (see printf)
  1277. * @argptr: pointer to the first member of the variable argument list.
  1278. *
  1279. * Write a formatted xml text.
  1280. *
  1281. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1282. */
  1283. int
  1284. xmlTextWriterWriteVFormatString(xmlTextWriterPtr writer,
  1285. const char *format, va_list argptr)
  1286. {
  1287. int rc;
  1288. xmlChar *buf;
  1289. if ((writer == NULL) || (format == NULL))
  1290. return -1;
  1291. buf = xmlTextWriterVSprintf(format, argptr);
  1292. if (buf == NULL)
  1293. return -1;
  1294. rc = xmlTextWriterWriteString(writer, buf);
  1295. xmlFree(buf);
  1296. return rc;
  1297. }
  1298. /**
  1299. * xmlTextWriterWriteString:
  1300. * @writer: the xmlTextWriterPtr
  1301. * @content: text string
  1302. *
  1303. * Write an xml text.
  1304. *
  1305. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1306. */
  1307. int
  1308. xmlTextWriterWriteString(xmlTextWriterPtr writer, const xmlChar * content)
  1309. {
  1310. int count;
  1311. int sum;
  1312. xmlLinkPtr lk;
  1313. xmlTextWriterStackEntry *p;
  1314. xmlChar *buf;
  1315. if ((writer == NULL) || (content == NULL))
  1316. return -1;
  1317. sum = 0;
  1318. buf = (xmlChar *) content;
  1319. lk = xmlListFront(writer->nodes);
  1320. if (lk != 0) {
  1321. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  1322. if (p != 0) {
  1323. switch (p->state) {
  1324. case XML_TEXTWRITER_NAME:
  1325. case XML_TEXTWRITER_TEXT:
  1326. #if 0
  1327. buf = NULL;
  1328. xmlOutputBufferWriteEscape(writer->out, content, NULL);
  1329. #endif
  1330. buf = xmlEncodeSpecialChars(NULL, content);
  1331. break;
  1332. case XML_TEXTWRITER_ATTRIBUTE:
  1333. buf = NULL;
  1334. xmlAttrSerializeTxtContent(writer->out->buffer, writer->doc,
  1335. NULL, content);
  1336. break;
  1337. default:
  1338. break;
  1339. }
  1340. }
  1341. }
  1342. if (buf != NULL) {
  1343. count = xmlTextWriterWriteRaw(writer, buf);
  1344. if (buf != content) /* buf was allocated by us, so free it */
  1345. xmlFree(buf);
  1346. if (count < 0)
  1347. return -1;
  1348. sum += count;
  1349. }
  1350. return sum;
  1351. }
  1352. /**
  1353. * xmlOutputBufferWriteBase64:
  1354. * @out: the xmlOutputBufferPtr
  1355. * @data: binary data
  1356. * @len: the number of bytes to encode
  1357. *
  1358. * Write base64 encoded data to an xmlOutputBuffer.
  1359. * Adapted from John Walker's base64.c (http://www.fourmilab.ch/).
  1360. *
  1361. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1362. */
  1363. static int
  1364. xmlOutputBufferWriteBase64(xmlOutputBufferPtr out, int len,
  1365. const unsigned char *data)
  1366. {
  1367. static unsigned char dtable[64] =
  1368. {'A','B','C','D','E','F','G','H','I','J','K','L','M',
  1369. 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  1370. 'a','b','c','d','e','f','g','h','i','j','k','l','m',
  1371. 'n','o','p','q','r','s','t','u','v','w','x','y','z',
  1372. '0','1','2','3','4','5','6','7','8','9','+','/'};
  1373. int i;
  1374. int linelen;
  1375. int count;
  1376. int sum;
  1377. if ((out == NULL) || (len < 0) || (data == NULL))
  1378. return(-1);
  1379. linelen = 0;
  1380. sum = 0;
  1381. i = 0;
  1382. while (1) {
  1383. unsigned char igroup[3];
  1384. unsigned char ogroup[4];
  1385. int c;
  1386. int n;
  1387. igroup[0] = igroup[1] = igroup[2] = 0;
  1388. for (n = 0; n < 3 && i < len; n++, i++) {
  1389. c = data[i];
  1390. igroup[n] = (unsigned char) c;
  1391. }
  1392. if (n > 0) {
  1393. ogroup[0] = dtable[igroup[0] >> 2];
  1394. ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
  1395. ogroup[2] =
  1396. dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
  1397. ogroup[3] = dtable[igroup[2] & 0x3F];
  1398. if (n < 3) {
  1399. ogroup[3] = '=';
  1400. if (n < 2) {
  1401. ogroup[2] = '=';
  1402. }
  1403. }
  1404. if (linelen >= B64LINELEN) {
  1405. count = xmlOutputBufferWrite(out, 2, B64CRLF);
  1406. if (count == -1)
  1407. return -1;
  1408. sum += count;
  1409. linelen = 0;
  1410. }
  1411. count = xmlOutputBufferWrite(out, 4, (const char *) ogroup);
  1412. if (count == -1)
  1413. return -1;
  1414. sum += count;
  1415. linelen += 4;
  1416. }
  1417. if (i >= len)
  1418. break;
  1419. }
  1420. return sum;
  1421. }
  1422. /**
  1423. * xmlTextWriterWriteBase64:
  1424. * @writer: the xmlTextWriterPtr
  1425. * @data: binary data
  1426. * @start: the position within the data of the first byte to encode
  1427. * @len: the number of bytes to encode
  1428. *
  1429. * Write an base64 encoded xml text.
  1430. *
  1431. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1432. */
  1433. int
  1434. xmlTextWriterWriteBase64(xmlTextWriterPtr writer, const char *data,
  1435. int start, int len)
  1436. {
  1437. int count;
  1438. int sum;
  1439. xmlLinkPtr lk;
  1440. xmlTextWriterStackEntry *p;
  1441. if ((writer == NULL) || (data == NULL) || (start < 0) || (len < 0))
  1442. return -1;
  1443. sum = 0;
  1444. lk = xmlListFront(writer->nodes);
  1445. if (lk != 0) {
  1446. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  1447. if (p != 0) {
  1448. count = xmlTextWriterHandleStateDependencies(writer, p);
  1449. if (count < 0)
  1450. return -1;
  1451. sum += count;
  1452. }
  1453. }
  1454. if (writer->indent)
  1455. writer->doindent = 0;
  1456. count =
  1457. xmlOutputBufferWriteBase64(writer->out, len,
  1458. (unsigned char *) data + start);
  1459. if (count < 0)
  1460. return -1;
  1461. sum += count;
  1462. return sum;
  1463. }
  1464. /**
  1465. * xmlOutputBufferWriteBinHex:
  1466. * @out: the xmlOutputBufferPtr
  1467. * @data: binary data
  1468. * @len: the number of bytes to encode
  1469. *
  1470. * Write hqx encoded data to an xmlOutputBuffer.
  1471. * ::todo
  1472. *
  1473. * Returns the bytes written (may be 0 because of buffering)
  1474. * or -1 in case of error
  1475. */
  1476. static int
  1477. xmlOutputBufferWriteBinHex(xmlOutputBufferPtr out,
  1478. int len, const unsigned char *data)
  1479. {
  1480. int count;
  1481. int sum;
  1482. static char hex[16] =
  1483. {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  1484. int i;
  1485. if ((out == NULL) || (data == NULL) || (len < 0)) {
  1486. return -1;
  1487. }
  1488. sum = 0;
  1489. for (i = 0; i < len; i++) {
  1490. count =
  1491. xmlOutputBufferWrite(out, 1,
  1492. (const char *) &hex[data[i] >> 4]);
  1493. if (count == -1)
  1494. return -1;
  1495. sum += count;
  1496. count =
  1497. xmlOutputBufferWrite(out, 1,
  1498. (const char *) &hex[data[i] & 0xF]);
  1499. if (count == -1)
  1500. return -1;
  1501. sum += count;
  1502. }
  1503. return sum;
  1504. }
  1505. /**
  1506. * xmlTextWriterWriteBinHex:
  1507. * @writer: the xmlTextWriterPtr
  1508. * @data: binary data
  1509. * @start: the position within the data of the first byte to encode
  1510. * @len: the number of bytes to encode
  1511. *
  1512. * Write a BinHex encoded xml text.
  1513. *
  1514. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1515. */
  1516. int
  1517. xmlTextWriterWriteBinHex(xmlTextWriterPtr writer, const char *data,
  1518. int start, int len)
  1519. {
  1520. int count;
  1521. int sum;
  1522. xmlLinkPtr lk;
  1523. xmlTextWriterStackEntry *p;
  1524. if ((writer == NULL) || (data == NULL) || (start < 0) || (len < 0))
  1525. return -1;
  1526. sum = 0;
  1527. lk = xmlListFront(writer->nodes);
  1528. if (lk != 0) {
  1529. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  1530. if (p != 0) {
  1531. count = xmlTextWriterHandleStateDependencies(writer, p);
  1532. if (count < 0)
  1533. return -1;
  1534. sum += count;
  1535. }
  1536. }
  1537. if (writer->indent)
  1538. writer->doindent = 0;
  1539. count =
  1540. xmlOutputBufferWriteBinHex(writer->out, len,
  1541. (unsigned char *) data + start);
  1542. if (count < 0)
  1543. return -1;
  1544. sum += count;
  1545. return sum;
  1546. }
  1547. /**
  1548. * xmlTextWriterStartAttribute:
  1549. * @writer: the xmlTextWriterPtr
  1550. * @name: element name
  1551. *
  1552. * Start an xml attribute.
  1553. *
  1554. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1555. */
  1556. int
  1557. xmlTextWriterStartAttribute(xmlTextWriterPtr writer, const xmlChar * name)
  1558. {
  1559. int count;
  1560. int sum;
  1561. xmlLinkPtr lk;
  1562. xmlTextWriterStackEntry *p;
  1563. if ((writer == NULL) || (name == NULL) || (*name == '\0'))
  1564. return -1;
  1565. sum = 0;
  1566. lk = xmlListFront(writer->nodes);
  1567. if (lk == 0)
  1568. return -1;
  1569. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  1570. if (p == 0)
  1571. return -1;
  1572. switch (p->state) {
  1573. case XML_TEXTWRITER_ATTRIBUTE:
  1574. count = xmlTextWriterEndAttribute(writer);
  1575. if (count < 0)
  1576. return -1;
  1577. sum += count;
  1578. /* fallthrough */
  1579. case XML_TEXTWRITER_NAME:
  1580. count = xmlOutputBufferWriteString(writer->out, " ");
  1581. if (count < 0)
  1582. return -1;
  1583. sum += count;
  1584. count =
  1585. xmlOutputBufferWriteString(writer->out,
  1586. (const char *) name);
  1587. if (count < 0)
  1588. return -1;
  1589. sum += count;
  1590. count = xmlOutputBufferWriteString(writer->out, "=");
  1591. if (count < 0)
  1592. return -1;
  1593. sum += count;
  1594. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  1595. if (count < 0)
  1596. return -1;
  1597. sum += count;
  1598. p->state = XML_TEXTWRITER_ATTRIBUTE;
  1599. break;
  1600. default:
  1601. return -1;
  1602. }
  1603. return sum;
  1604. }
  1605. /**
  1606. * xmlTextWriterStartAttributeNS:
  1607. * @writer: the xmlTextWriterPtr
  1608. * @prefix: namespace prefix or NULL
  1609. * @name: element local name
  1610. * @namespaceURI: namespace URI or NULL
  1611. *
  1612. * Start an xml attribute with namespace support.
  1613. *
  1614. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1615. */
  1616. int
  1617. xmlTextWriterStartAttributeNS(xmlTextWriterPtr writer,
  1618. const xmlChar * prefix, const xmlChar * name,
  1619. const xmlChar * namespaceURI)
  1620. {
  1621. int count;
  1622. int sum;
  1623. xmlChar *buf;
  1624. xmlTextWriterNsStackEntry *p;
  1625. if ((writer == NULL) || (name == NULL) || (*name == '\0'))
  1626. return -1;
  1627. /* Handle namespace first in case of error */
  1628. if (namespaceURI != 0) {
  1629. xmlTextWriterNsStackEntry nsentry, *curns;
  1630. buf = xmlStrdup(BAD_CAST "xmlns");
  1631. if (prefix != 0) {
  1632. buf = xmlStrcat(buf, BAD_CAST ":");
  1633. buf = xmlStrcat(buf, prefix);
  1634. }
  1635. nsentry.prefix = buf;
  1636. nsentry.uri = (xmlChar *)namespaceURI;
  1637. nsentry.elem = xmlListFront(writer->nodes);
  1638. curns = (xmlTextWriterNsStackEntry *)xmlListSearch(writer->nsstack,
  1639. (void *)&nsentry);
  1640. if ((curns != NULL)) {
  1641. xmlFree(buf);
  1642. if (xmlStrcmp(curns->uri, namespaceURI) == 0) {
  1643. /* Namespace already defined on element skip */
  1644. buf = NULL;
  1645. } else {
  1646. /* Prefix mismatch so error out */
  1647. return -1;
  1648. }
  1649. }
  1650. /* Do not add namespace decl to list - it is already there */
  1651. if (buf != NULL) {
  1652. p = (xmlTextWriterNsStackEntry *)
  1653. xmlMalloc(sizeof(xmlTextWriterNsStackEntry));
  1654. if (p == 0) {
  1655. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  1656. "xmlTextWriterStartAttributeNS : out of memory!\n");
  1657. return -1;
  1658. }
  1659. p->prefix = buf;
  1660. p->uri = xmlStrdup(namespaceURI);
  1661. if (p->uri == 0) {
  1662. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  1663. "xmlTextWriterStartAttributeNS : out of memory!\n");
  1664. xmlFree(p);
  1665. return -1;
  1666. }
  1667. p->elem = xmlListFront(writer->nodes);
  1668. xmlListPushFront(writer->nsstack, p);
  1669. }
  1670. }
  1671. buf = NULL;
  1672. if (prefix != 0) {
  1673. buf = xmlStrdup(prefix);
  1674. buf = xmlStrcat(buf, BAD_CAST ":");
  1675. }
  1676. buf = xmlStrcat(buf, name);
  1677. sum = 0;
  1678. count = xmlTextWriterStartAttribute(writer, buf);
  1679. xmlFree(buf);
  1680. if (count < 0)
  1681. return -1;
  1682. sum += count;
  1683. return sum;
  1684. }
  1685. /**
  1686. * xmlTextWriterEndAttribute:
  1687. * @writer: the xmlTextWriterPtr
  1688. *
  1689. * End the current xml element.
  1690. *
  1691. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1692. */
  1693. int
  1694. xmlTextWriterEndAttribute(xmlTextWriterPtr writer)
  1695. {
  1696. int count;
  1697. int sum;
  1698. xmlLinkPtr lk;
  1699. xmlTextWriterStackEntry *p;
  1700. if (writer == NULL)
  1701. return -1;
  1702. lk = xmlListFront(writer->nodes);
  1703. if (lk == 0) {
  1704. return -1;
  1705. }
  1706. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  1707. if (p == 0) {
  1708. return -1;
  1709. }
  1710. sum = 0;
  1711. switch (p->state) {
  1712. case XML_TEXTWRITER_ATTRIBUTE:
  1713. p->state = XML_TEXTWRITER_NAME;
  1714. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  1715. if (count < 0) {
  1716. return -1;
  1717. }
  1718. sum += count;
  1719. break;
  1720. default:
  1721. return -1;
  1722. }
  1723. return sum;
  1724. }
  1725. /**
  1726. * xmlTextWriterWriteFormatAttribute:
  1727. * @writer: the xmlTextWriterPtr
  1728. * @name: attribute name
  1729. * @format: format string (see printf)
  1730. * @...: extra parameters for the format
  1731. *
  1732. * Write a formatted xml attribute.
  1733. *
  1734. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1735. */
  1736. int XMLCDECL
  1737. xmlTextWriterWriteFormatAttribute(xmlTextWriterPtr writer,
  1738. const xmlChar * name, const char *format,
  1739. ...)
  1740. {
  1741. int rc;
  1742. va_list ap;
  1743. va_start(ap, format);
  1744. rc = xmlTextWriterWriteVFormatAttribute(writer, name, format, ap);
  1745. va_end(ap);
  1746. return rc;
  1747. }
  1748. /**
  1749. * xmlTextWriterWriteVFormatAttribute:
  1750. * @writer: the xmlTextWriterPtr
  1751. * @name: attribute name
  1752. * @format: format string (see printf)
  1753. * @argptr: pointer to the first member of the variable argument list.
  1754. *
  1755. * Write a formatted xml attribute.
  1756. *
  1757. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1758. */
  1759. int
  1760. xmlTextWriterWriteVFormatAttribute(xmlTextWriterPtr writer,
  1761. const xmlChar * name,
  1762. const char *format, va_list argptr)
  1763. {
  1764. int rc;
  1765. xmlChar *buf;
  1766. if (writer == NULL)
  1767. return -1;
  1768. buf = xmlTextWriterVSprintf(format, argptr);
  1769. if (buf == NULL)
  1770. return -1;
  1771. rc = xmlTextWriterWriteAttribute(writer, name, buf);
  1772. xmlFree(buf);
  1773. return rc;
  1774. }
  1775. /**
  1776. * xmlTextWriterWriteAttribute:
  1777. * @writer: the xmlTextWriterPtr
  1778. * @name: attribute name
  1779. * @content: attribute content
  1780. *
  1781. * Write an xml attribute.
  1782. *
  1783. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1784. */
  1785. int
  1786. xmlTextWriterWriteAttribute(xmlTextWriterPtr writer, const xmlChar * name,
  1787. const xmlChar * content)
  1788. {
  1789. int count;
  1790. int sum;
  1791. sum = 0;
  1792. count = xmlTextWriterStartAttribute(writer, name);
  1793. if (count < 0)
  1794. return -1;
  1795. sum += count;
  1796. count = xmlTextWriterWriteString(writer, content);
  1797. if (count < 0)
  1798. return -1;
  1799. sum += count;
  1800. count = xmlTextWriterEndAttribute(writer);
  1801. if (count < 0)
  1802. return -1;
  1803. sum += count;
  1804. return sum;
  1805. }
  1806. /**
  1807. * xmlTextWriterWriteFormatAttributeNS:
  1808. * @writer: the xmlTextWriterPtr
  1809. * @prefix: namespace prefix
  1810. * @name: attribute local name
  1811. * @namespaceURI: namespace URI
  1812. * @format: format string (see printf)
  1813. * @...: extra parameters for the format
  1814. *
  1815. * Write a formatted xml attribute.with namespace support
  1816. *
  1817. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1818. */
  1819. int XMLCDECL
  1820. xmlTextWriterWriteFormatAttributeNS(xmlTextWriterPtr writer,
  1821. const xmlChar * prefix,
  1822. const xmlChar * name,
  1823. const xmlChar * namespaceURI,
  1824. const char *format, ...)
  1825. {
  1826. int rc;
  1827. va_list ap;
  1828. va_start(ap, format);
  1829. rc = xmlTextWriterWriteVFormatAttributeNS(writer, prefix, name,
  1830. namespaceURI, format, ap);
  1831. va_end(ap);
  1832. return rc;
  1833. }
  1834. /**
  1835. * xmlTextWriterWriteVFormatAttributeNS:
  1836. * @writer: the xmlTextWriterPtr
  1837. * @prefix: namespace prefix
  1838. * @name: attribute local name
  1839. * @namespaceURI: namespace URI
  1840. * @format: format string (see printf)
  1841. * @argptr: pointer to the first member of the variable argument list.
  1842. *
  1843. * Write a formatted xml attribute.with namespace support
  1844. *
  1845. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1846. */
  1847. int
  1848. xmlTextWriterWriteVFormatAttributeNS(xmlTextWriterPtr writer,
  1849. const xmlChar * prefix,
  1850. const xmlChar * name,
  1851. const xmlChar * namespaceURI,
  1852. const char *format, va_list argptr)
  1853. {
  1854. int rc;
  1855. xmlChar *buf;
  1856. if (writer == NULL)
  1857. return -1;
  1858. buf = xmlTextWriterVSprintf(format, argptr);
  1859. if (buf == NULL)
  1860. return -1;
  1861. rc = xmlTextWriterWriteAttributeNS(writer, prefix, name, namespaceURI,
  1862. buf);
  1863. xmlFree(buf);
  1864. return rc;
  1865. }
  1866. /**
  1867. * xmlTextWriterWriteAttributeNS:
  1868. * @writer: the xmlTextWriterPtr
  1869. * @prefix: namespace prefix
  1870. * @name: attribute local name
  1871. * @namespaceURI: namespace URI
  1872. * @content: attribute content
  1873. *
  1874. * Write an xml attribute.
  1875. *
  1876. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1877. */
  1878. int
  1879. xmlTextWriterWriteAttributeNS(xmlTextWriterPtr writer,
  1880. const xmlChar * prefix, const xmlChar * name,
  1881. const xmlChar * namespaceURI,
  1882. const xmlChar * content)
  1883. {
  1884. int count;
  1885. int sum;
  1886. if ((writer == NULL) || (name == NULL) || (*name == '\0'))
  1887. return -1;
  1888. sum = 0;
  1889. count = xmlTextWriterStartAttributeNS(writer, prefix, name, namespaceURI);
  1890. if (count < 0)
  1891. return -1;
  1892. sum += count;
  1893. count = xmlTextWriterWriteString(writer, content);
  1894. if (count < 0)
  1895. return -1;
  1896. sum += count;
  1897. count = xmlTextWriterEndAttribute(writer);
  1898. if (count < 0)
  1899. return -1;
  1900. sum += count;
  1901. return sum;
  1902. }
  1903. /**
  1904. * xmlTextWriterWriteFormatElement:
  1905. * @writer: the xmlTextWriterPtr
  1906. * @name: element name
  1907. * @format: format string (see printf)
  1908. * @...: extra parameters for the format
  1909. *
  1910. * Write a formatted xml element.
  1911. *
  1912. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1913. */
  1914. int XMLCDECL
  1915. xmlTextWriterWriteFormatElement(xmlTextWriterPtr writer,
  1916. const xmlChar * name, const char *format,
  1917. ...)
  1918. {
  1919. int rc;
  1920. va_list ap;
  1921. va_start(ap, format);
  1922. rc = xmlTextWriterWriteVFormatElement(writer, name, format, ap);
  1923. va_end(ap);
  1924. return rc;
  1925. }
  1926. /**
  1927. * xmlTextWriterWriteVFormatElement:
  1928. * @writer: the xmlTextWriterPtr
  1929. * @name: element name
  1930. * @format: format string (see printf)
  1931. * @argptr: pointer to the first member of the variable argument list.
  1932. *
  1933. * Write a formatted xml element.
  1934. *
  1935. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1936. */
  1937. int
  1938. xmlTextWriterWriteVFormatElement(xmlTextWriterPtr writer,
  1939. const xmlChar * name, const char *format,
  1940. va_list argptr)
  1941. {
  1942. int rc;
  1943. xmlChar *buf;
  1944. if (writer == NULL)
  1945. return -1;
  1946. buf = xmlTextWriterVSprintf(format, argptr);
  1947. if (buf == NULL)
  1948. return -1;
  1949. rc = xmlTextWriterWriteElement(writer, name, buf);
  1950. xmlFree(buf);
  1951. return rc;
  1952. }
  1953. /**
  1954. * xmlTextWriterWriteElement:
  1955. * @writer: the xmlTextWriterPtr
  1956. * @name: element name
  1957. * @content: element content
  1958. *
  1959. * Write an xml element.
  1960. *
  1961. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1962. */
  1963. int
  1964. xmlTextWriterWriteElement(xmlTextWriterPtr writer, const xmlChar * name,
  1965. const xmlChar * content)
  1966. {
  1967. int count;
  1968. int sum;
  1969. sum = 0;
  1970. count = xmlTextWriterStartElement(writer, name);
  1971. if (count == -1)
  1972. return -1;
  1973. sum += count;
  1974. count = xmlTextWriterWriteString(writer, content);
  1975. if (count == -1)
  1976. return -1;
  1977. sum += count;
  1978. count = xmlTextWriterEndElement(writer);
  1979. if (count == -1)
  1980. return -1;
  1981. sum += count;
  1982. return sum;
  1983. }
  1984. /**
  1985. * xmlTextWriterWriteFormatElementNS:
  1986. * @writer: the xmlTextWriterPtr
  1987. * @prefix: namespace prefix
  1988. * @name: element local name
  1989. * @namespaceURI: namespace URI
  1990. * @format: format string (see printf)
  1991. * @...: extra parameters for the format
  1992. *
  1993. * Write a formatted xml element with namespace support.
  1994. *
  1995. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1996. */
  1997. int XMLCDECL
  1998. xmlTextWriterWriteFormatElementNS(xmlTextWriterPtr writer,
  1999. const xmlChar * prefix,
  2000. const xmlChar * name,
  2001. const xmlChar * namespaceURI,
  2002. const char *format, ...)
  2003. {
  2004. int rc;
  2005. va_list ap;
  2006. va_start(ap, format);
  2007. rc = xmlTextWriterWriteVFormatElementNS(writer, prefix, name,
  2008. namespaceURI, format, ap);
  2009. va_end(ap);
  2010. return rc;
  2011. }
  2012. /**
  2013. * xmlTextWriterWriteVFormatElementNS:
  2014. * @writer: the xmlTextWriterPtr
  2015. * @prefix: namespace prefix
  2016. * @name: element local name
  2017. * @namespaceURI: namespace URI
  2018. * @format: format string (see printf)
  2019. * @argptr: pointer to the first member of the variable argument list.
  2020. *
  2021. * Write a formatted xml element with namespace support.
  2022. *
  2023. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2024. */
  2025. int
  2026. xmlTextWriterWriteVFormatElementNS(xmlTextWriterPtr writer,
  2027. const xmlChar * prefix,
  2028. const xmlChar * name,
  2029. const xmlChar * namespaceURI,
  2030. const char *format, va_list argptr)
  2031. {
  2032. int rc;
  2033. xmlChar *buf;
  2034. if (writer == NULL)
  2035. return -1;
  2036. buf = xmlTextWriterVSprintf(format, argptr);
  2037. if (buf == NULL)
  2038. return -1;
  2039. rc = xmlTextWriterWriteElementNS(writer, prefix, name, namespaceURI,
  2040. buf);
  2041. xmlFree(buf);
  2042. return rc;
  2043. }
  2044. /**
  2045. * xmlTextWriterWriteElementNS:
  2046. * @writer: the xmlTextWriterPtr
  2047. * @prefix: namespace prefix
  2048. * @name: element local name
  2049. * @namespaceURI: namespace URI
  2050. * @content: element content
  2051. *
  2052. * Write an xml element with namespace support.
  2053. *
  2054. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2055. */
  2056. int
  2057. xmlTextWriterWriteElementNS(xmlTextWriterPtr writer,
  2058. const xmlChar * prefix, const xmlChar * name,
  2059. const xmlChar * namespaceURI,
  2060. const xmlChar * content)
  2061. {
  2062. int count;
  2063. int sum;
  2064. if ((writer == NULL) || (name == NULL) || (*name == '\0'))
  2065. return -1;
  2066. sum = 0;
  2067. count =
  2068. xmlTextWriterStartElementNS(writer, prefix, name, namespaceURI);
  2069. if (count < 0)
  2070. return -1;
  2071. sum += count;
  2072. count = xmlTextWriterWriteString(writer, content);
  2073. if (count == -1)
  2074. return -1;
  2075. sum += count;
  2076. count = xmlTextWriterEndElement(writer);
  2077. if (count == -1)
  2078. return -1;
  2079. sum += count;
  2080. return sum;
  2081. }
  2082. /**
  2083. * xmlTextWriterStartPI:
  2084. * @writer: the xmlTextWriterPtr
  2085. * @target: PI target
  2086. *
  2087. * Start an xml PI.
  2088. *
  2089. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2090. */
  2091. int
  2092. xmlTextWriterStartPI(xmlTextWriterPtr writer, const xmlChar * target)
  2093. {
  2094. int count;
  2095. int sum;
  2096. xmlLinkPtr lk;
  2097. xmlTextWriterStackEntry *p;
  2098. if ((writer == NULL) || (target == NULL) || (*target == '\0'))
  2099. return -1;
  2100. if (xmlStrcasecmp(target, (const xmlChar *) "xml") == 0) {
  2101. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  2102. "xmlTextWriterStartPI : target name [Xx][Mm][Ll] is reserved for xml standardization!\n");
  2103. return -1;
  2104. }
  2105. sum = 0;
  2106. lk = xmlListFront(writer->nodes);
  2107. if (lk != 0) {
  2108. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  2109. if (p != 0) {
  2110. switch (p->state) {
  2111. case XML_TEXTWRITER_ATTRIBUTE:
  2112. count = xmlTextWriterEndAttribute(writer);
  2113. if (count < 0)
  2114. return -1;
  2115. sum += count;
  2116. /* fallthrough */
  2117. case XML_TEXTWRITER_NAME:
  2118. /* Output namespace declarations */
  2119. count = xmlTextWriterOutputNSDecl(writer);
  2120. if (count < 0)
  2121. return -1;
  2122. sum += count;
  2123. count = xmlOutputBufferWriteString(writer->out, ">");
  2124. if (count < 0)
  2125. return -1;
  2126. sum += count;
  2127. p->state = XML_TEXTWRITER_TEXT;
  2128. break;
  2129. case XML_TEXTWRITER_NONE:
  2130. case XML_TEXTWRITER_TEXT:
  2131. case XML_TEXTWRITER_DTD:
  2132. break;
  2133. case XML_TEXTWRITER_PI:
  2134. case XML_TEXTWRITER_PI_TEXT:
  2135. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  2136. "xmlTextWriterStartPI : nested PI!\n");
  2137. return -1;
  2138. default:
  2139. return -1;
  2140. }
  2141. }
  2142. }
  2143. p = (xmlTextWriterStackEntry *)
  2144. xmlMalloc(sizeof(xmlTextWriterStackEntry));
  2145. if (p == 0) {
  2146. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  2147. "xmlTextWriterStartPI : out of memory!\n");
  2148. return -1;
  2149. }
  2150. p->name = xmlStrdup(target);
  2151. if (p->name == 0) {
  2152. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  2153. "xmlTextWriterStartPI : out of memory!\n");
  2154. xmlFree(p);
  2155. return -1;
  2156. }
  2157. p->state = XML_TEXTWRITER_PI;
  2158. xmlListPushFront(writer->nodes, p);
  2159. count = xmlOutputBufferWriteString(writer->out, "<?");
  2160. if (count < 0)
  2161. return -1;
  2162. sum += count;
  2163. count =
  2164. xmlOutputBufferWriteString(writer->out, (const char *) p->name);
  2165. if (count < 0)
  2166. return -1;
  2167. sum += count;
  2168. return sum;
  2169. }
  2170. /**
  2171. * xmlTextWriterEndPI:
  2172. * @writer: the xmlTextWriterPtr
  2173. *
  2174. * End the current xml PI.
  2175. *
  2176. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2177. */
  2178. int
  2179. xmlTextWriterEndPI(xmlTextWriterPtr writer)
  2180. {
  2181. int count;
  2182. int sum;
  2183. xmlLinkPtr lk;
  2184. xmlTextWriterStackEntry *p;
  2185. if (writer == NULL)
  2186. return -1;
  2187. lk = xmlListFront(writer->nodes);
  2188. if (lk == 0)
  2189. return 0;
  2190. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  2191. if (p == 0)
  2192. return 0;
  2193. sum = 0;
  2194. switch (p->state) {
  2195. case XML_TEXTWRITER_PI:
  2196. case XML_TEXTWRITER_PI_TEXT:
  2197. count = xmlOutputBufferWriteString(writer->out, "?>");
  2198. if (count < 0)
  2199. return -1;
  2200. sum += count;
  2201. break;
  2202. default:
  2203. return -1;
  2204. }
  2205. if (writer->indent) {
  2206. count = xmlOutputBufferWriteString(writer->out, "\n");
  2207. if (count < 0)
  2208. return -1;
  2209. sum += count;
  2210. }
  2211. xmlListPopFront(writer->nodes);
  2212. return sum;
  2213. }
  2214. /**
  2215. * xmlTextWriterWriteFormatPI:
  2216. * @writer: the xmlTextWriterPtr
  2217. * @target: PI target
  2218. * @format: format string (see printf)
  2219. * @...: extra parameters for the format
  2220. *
  2221. * Write a formatted PI.
  2222. *
  2223. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2224. */
  2225. int XMLCDECL
  2226. xmlTextWriterWriteFormatPI(xmlTextWriterPtr writer, const xmlChar * target,
  2227. const char *format, ...)
  2228. {
  2229. int rc;
  2230. va_list ap;
  2231. va_start(ap, format);
  2232. rc = xmlTextWriterWriteVFormatPI(writer, target, format, ap);
  2233. va_end(ap);
  2234. return rc;
  2235. }
  2236. /**
  2237. * xmlTextWriterWriteVFormatPI:
  2238. * @writer: the xmlTextWriterPtr
  2239. * @target: PI target
  2240. * @format: format string (see printf)
  2241. * @argptr: pointer to the first member of the variable argument list.
  2242. *
  2243. * Write a formatted xml PI.
  2244. *
  2245. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2246. */
  2247. int
  2248. xmlTextWriterWriteVFormatPI(xmlTextWriterPtr writer,
  2249. const xmlChar * target, const char *format,
  2250. va_list argptr)
  2251. {
  2252. int rc;
  2253. xmlChar *buf;
  2254. if (writer == NULL)
  2255. return -1;
  2256. buf = xmlTextWriterVSprintf(format, argptr);
  2257. if (buf == NULL)
  2258. return -1;
  2259. rc = xmlTextWriterWritePI(writer, target, buf);
  2260. xmlFree(buf);
  2261. return rc;
  2262. }
  2263. /**
  2264. * xmlTextWriterWritePI:
  2265. * @writer: the xmlTextWriterPtr
  2266. * @target: PI target
  2267. * @content: PI content
  2268. *
  2269. * Write an xml PI.
  2270. *
  2271. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2272. */
  2273. int
  2274. xmlTextWriterWritePI(xmlTextWriterPtr writer, const xmlChar * target,
  2275. const xmlChar * content)
  2276. {
  2277. int count;
  2278. int sum;
  2279. sum = 0;
  2280. count = xmlTextWriterStartPI(writer, target);
  2281. if (count == -1)
  2282. return -1;
  2283. sum += count;
  2284. if (content != 0) {
  2285. count = xmlTextWriterWriteString(writer, content);
  2286. if (count == -1)
  2287. return -1;
  2288. sum += count;
  2289. }
  2290. count = xmlTextWriterEndPI(writer);
  2291. if (count == -1)
  2292. return -1;
  2293. sum += count;
  2294. return sum;
  2295. }
  2296. /**
  2297. * xmlTextWriterStartCDATA:
  2298. * @writer: the xmlTextWriterPtr
  2299. *
  2300. * Start an xml CDATA section.
  2301. *
  2302. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2303. */
  2304. int
  2305. xmlTextWriterStartCDATA(xmlTextWriterPtr writer)
  2306. {
  2307. int count;
  2308. int sum;
  2309. xmlLinkPtr lk;
  2310. xmlTextWriterStackEntry *p;
  2311. if (writer == NULL)
  2312. return -1;
  2313. sum = 0;
  2314. lk = xmlListFront(writer->nodes);
  2315. if (lk != 0) {
  2316. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  2317. if (p != 0) {
  2318. switch (p->state) {
  2319. case XML_TEXTWRITER_NONE:
  2320. case XML_TEXTWRITER_TEXT:
  2321. case XML_TEXTWRITER_PI:
  2322. case XML_TEXTWRITER_PI_TEXT:
  2323. break;
  2324. case XML_TEXTWRITER_ATTRIBUTE:
  2325. count = xmlTextWriterEndAttribute(writer);
  2326. if (count < 0)
  2327. return -1;
  2328. sum += count;
  2329. /* fallthrough */
  2330. case XML_TEXTWRITER_NAME:
  2331. /* Output namespace declarations */
  2332. count = xmlTextWriterOutputNSDecl(writer);
  2333. if (count < 0)
  2334. return -1;
  2335. sum += count;
  2336. count = xmlOutputBufferWriteString(writer->out, ">");
  2337. if (count < 0)
  2338. return -1;
  2339. sum += count;
  2340. p->state = XML_TEXTWRITER_TEXT;
  2341. break;
  2342. case XML_TEXTWRITER_CDATA:
  2343. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  2344. "xmlTextWriterStartCDATA : CDATA not allowed in this context!\n");
  2345. return -1;
  2346. default:
  2347. return -1;
  2348. }
  2349. }
  2350. }
  2351. p = (xmlTextWriterStackEntry *)
  2352. xmlMalloc(sizeof(xmlTextWriterStackEntry));
  2353. if (p == 0) {
  2354. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  2355. "xmlTextWriterStartCDATA : out of memory!\n");
  2356. return -1;
  2357. }
  2358. p->name = NULL;
  2359. p->state = XML_TEXTWRITER_CDATA;
  2360. xmlListPushFront(writer->nodes, p);
  2361. count = xmlOutputBufferWriteString(writer->out, "<![CDATA[");
  2362. if (count < 0)
  2363. return -1;
  2364. sum += count;
  2365. return sum;
  2366. }
  2367. /**
  2368. * xmlTextWriterEndCDATA:
  2369. * @writer: the xmlTextWriterPtr
  2370. *
  2371. * End an xml CDATA section.
  2372. *
  2373. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2374. */
  2375. int
  2376. xmlTextWriterEndCDATA(xmlTextWriterPtr writer)
  2377. {
  2378. int count;
  2379. int sum;
  2380. xmlLinkPtr lk;
  2381. xmlTextWriterStackEntry *p;
  2382. if (writer == NULL)
  2383. return -1;
  2384. lk = xmlListFront(writer->nodes);
  2385. if (lk == 0)
  2386. return -1;
  2387. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  2388. if (p == 0)
  2389. return -1;
  2390. sum = 0;
  2391. switch (p->state) {
  2392. case XML_TEXTWRITER_CDATA:
  2393. count = xmlOutputBufferWriteString(writer->out, "]]>");
  2394. if (count < 0)
  2395. return -1;
  2396. sum += count;
  2397. break;
  2398. default:
  2399. return -1;
  2400. }
  2401. xmlListPopFront(writer->nodes);
  2402. return sum;
  2403. }
  2404. /**
  2405. * xmlTextWriterWriteFormatCDATA:
  2406. * @writer: the xmlTextWriterPtr
  2407. * @format: format string (see printf)
  2408. * @...: extra parameters for the format
  2409. *
  2410. * Write a formatted xml CDATA.
  2411. *
  2412. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2413. */
  2414. int XMLCDECL
  2415. xmlTextWriterWriteFormatCDATA(xmlTextWriterPtr writer, const char *format,
  2416. ...)
  2417. {
  2418. int rc;
  2419. va_list ap;
  2420. va_start(ap, format);
  2421. rc = xmlTextWriterWriteVFormatCDATA(writer, format, ap);
  2422. va_end(ap);
  2423. return rc;
  2424. }
  2425. /**
  2426. * xmlTextWriterWriteVFormatCDATA:
  2427. * @writer: the xmlTextWriterPtr
  2428. * @format: format string (see printf)
  2429. * @argptr: pointer to the first member of the variable argument list.
  2430. *
  2431. * Write a formatted xml CDATA.
  2432. *
  2433. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2434. */
  2435. int
  2436. xmlTextWriterWriteVFormatCDATA(xmlTextWriterPtr writer, const char *format,
  2437. va_list argptr)
  2438. {
  2439. int rc;
  2440. xmlChar *buf;
  2441. if (writer == NULL)
  2442. return -1;
  2443. buf = xmlTextWriterVSprintf(format, argptr);
  2444. if (buf == NULL)
  2445. return -1;
  2446. rc = xmlTextWriterWriteCDATA(writer, buf);
  2447. xmlFree(buf);
  2448. return rc;
  2449. }
  2450. /**
  2451. * xmlTextWriterWriteCDATA:
  2452. * @writer: the xmlTextWriterPtr
  2453. * @content: CDATA content
  2454. *
  2455. * Write an xml CDATA.
  2456. *
  2457. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2458. */
  2459. int
  2460. xmlTextWriterWriteCDATA(xmlTextWriterPtr writer, const xmlChar * content)
  2461. {
  2462. int count;
  2463. int sum;
  2464. sum = 0;
  2465. count = xmlTextWriterStartCDATA(writer);
  2466. if (count == -1)
  2467. return -1;
  2468. sum += count;
  2469. if (content != 0) {
  2470. count = xmlTextWriterWriteString(writer, content);
  2471. if (count == -1)
  2472. return -1;
  2473. sum += count;
  2474. }
  2475. count = xmlTextWriterEndCDATA(writer);
  2476. if (count == -1)
  2477. return -1;
  2478. sum += count;
  2479. return sum;
  2480. }
  2481. /**
  2482. * xmlTextWriterStartDTD:
  2483. * @writer: the xmlTextWriterPtr
  2484. * @name: the name of the DTD
  2485. * @pubid: the public identifier, which is an alternative to the system identifier
  2486. * @sysid: the system identifier, which is the URI of the DTD
  2487. *
  2488. * Start an xml DTD.
  2489. *
  2490. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2491. */
  2492. int
  2493. xmlTextWriterStartDTD(xmlTextWriterPtr writer,
  2494. const xmlChar * name,
  2495. const xmlChar * pubid, const xmlChar * sysid)
  2496. {
  2497. int count;
  2498. int sum;
  2499. xmlLinkPtr lk;
  2500. xmlTextWriterStackEntry *p;
  2501. if (writer == NULL || name == NULL || *name == '\0')
  2502. return -1;
  2503. sum = 0;
  2504. lk = xmlListFront(writer->nodes);
  2505. if ((lk != NULL) && (xmlLinkGetData(lk) != NULL)) {
  2506. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  2507. "xmlTextWriterStartDTD : DTD allowed only in prolog!\n");
  2508. return -1;
  2509. }
  2510. p = (xmlTextWriterStackEntry *)
  2511. xmlMalloc(sizeof(xmlTextWriterStackEntry));
  2512. if (p == 0) {
  2513. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  2514. "xmlTextWriterStartDTD : out of memory!\n");
  2515. return -1;
  2516. }
  2517. p->name = xmlStrdup(name);
  2518. if (p->name == 0) {
  2519. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  2520. "xmlTextWriterStartDTD : out of memory!\n");
  2521. xmlFree(p);
  2522. return -1;
  2523. }
  2524. p->state = XML_TEXTWRITER_DTD;
  2525. xmlListPushFront(writer->nodes, p);
  2526. count = xmlOutputBufferWriteString(writer->out, "<!DOCTYPE ");
  2527. if (count < 0)
  2528. return -1;
  2529. sum += count;
  2530. count = xmlOutputBufferWriteString(writer->out, (const char *) name);
  2531. if (count < 0)
  2532. return -1;
  2533. sum += count;
  2534. if (pubid != 0) {
  2535. if (sysid == 0) {
  2536. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  2537. "xmlTextWriterStartDTD : system identifier needed!\n");
  2538. return -1;
  2539. }
  2540. if (writer->indent)
  2541. count = xmlOutputBufferWrite(writer->out, 1, "\n");
  2542. else
  2543. count = xmlOutputBufferWrite(writer->out, 1, " ");
  2544. if (count < 0)
  2545. return -1;
  2546. sum += count;
  2547. count = xmlOutputBufferWriteString(writer->out, "PUBLIC ");
  2548. if (count < 0)
  2549. return -1;
  2550. sum += count;
  2551. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  2552. if (count < 0)
  2553. return -1;
  2554. sum += count;
  2555. count =
  2556. xmlOutputBufferWriteString(writer->out, (const char *) pubid);
  2557. if (count < 0)
  2558. return -1;
  2559. sum += count;
  2560. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  2561. if (count < 0)
  2562. return -1;
  2563. sum += count;
  2564. }
  2565. if (sysid != 0) {
  2566. if (pubid == 0) {
  2567. if (writer->indent)
  2568. count = xmlOutputBufferWrite(writer->out, 1, "\n");
  2569. else
  2570. count = xmlOutputBufferWrite(writer->out, 1, " ");
  2571. if (count < 0)
  2572. return -1;
  2573. sum += count;
  2574. count = xmlOutputBufferWriteString(writer->out, "SYSTEM ");
  2575. if (count < 0)
  2576. return -1;
  2577. sum += count;
  2578. } else {
  2579. if (writer->indent)
  2580. count = xmlOutputBufferWriteString(writer->out, "\n ");
  2581. else
  2582. count = xmlOutputBufferWrite(writer->out, 1, " ");
  2583. if (count < 0)
  2584. return -1;
  2585. sum += count;
  2586. }
  2587. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  2588. if (count < 0)
  2589. return -1;
  2590. sum += count;
  2591. count =
  2592. xmlOutputBufferWriteString(writer->out, (const char *) sysid);
  2593. if (count < 0)
  2594. return -1;
  2595. sum += count;
  2596. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  2597. if (count < 0)
  2598. return -1;
  2599. sum += count;
  2600. }
  2601. return sum;
  2602. }
  2603. /**
  2604. * xmlTextWriterEndDTD:
  2605. * @writer: the xmlTextWriterPtr
  2606. *
  2607. * End an xml DTD.
  2608. *
  2609. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2610. */
  2611. int
  2612. xmlTextWriterEndDTD(xmlTextWriterPtr writer)
  2613. {
  2614. int loop;
  2615. int count;
  2616. int sum;
  2617. xmlLinkPtr lk;
  2618. xmlTextWriterStackEntry *p;
  2619. if (writer == NULL)
  2620. return -1;
  2621. sum = 0;
  2622. loop = 1;
  2623. while (loop) {
  2624. lk = xmlListFront(writer->nodes);
  2625. if (lk == NULL)
  2626. break;
  2627. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  2628. if (p == 0)
  2629. break;
  2630. switch (p->state) {
  2631. case XML_TEXTWRITER_DTD_TEXT:
  2632. count = xmlOutputBufferWriteString(writer->out, "]");
  2633. if (count < 0)
  2634. return -1;
  2635. sum += count;
  2636. /* fallthrough */
  2637. case XML_TEXTWRITER_DTD:
  2638. count = xmlOutputBufferWriteString(writer->out, ">");
  2639. if (writer->indent) {
  2640. if (count < 0)
  2641. return -1;
  2642. sum += count;
  2643. count = xmlOutputBufferWriteString(writer->out, "\n");
  2644. }
  2645. xmlListPopFront(writer->nodes);
  2646. break;
  2647. case XML_TEXTWRITER_DTD_ELEM:
  2648. case XML_TEXTWRITER_DTD_ELEM_TEXT:
  2649. count = xmlTextWriterEndDTDElement(writer);
  2650. break;
  2651. case XML_TEXTWRITER_DTD_ATTL:
  2652. case XML_TEXTWRITER_DTD_ATTL_TEXT:
  2653. count = xmlTextWriterEndDTDAttlist(writer);
  2654. break;
  2655. case XML_TEXTWRITER_DTD_ENTY:
  2656. case XML_TEXTWRITER_DTD_PENT:
  2657. case XML_TEXTWRITER_DTD_ENTY_TEXT:
  2658. count = xmlTextWriterEndDTDEntity(writer);
  2659. break;
  2660. case XML_TEXTWRITER_COMMENT:
  2661. count = xmlTextWriterEndComment(writer);
  2662. break;
  2663. default:
  2664. loop = 0;
  2665. continue;
  2666. }
  2667. if (count < 0)
  2668. return -1;
  2669. sum += count;
  2670. }
  2671. return sum;
  2672. }
  2673. /**
  2674. * xmlTextWriterWriteFormatDTD:
  2675. * @writer: the xmlTextWriterPtr
  2676. * @name: the name of the DTD
  2677. * @pubid: the public identifier, which is an alternative to the system identifier
  2678. * @sysid: the system identifier, which is the URI of the DTD
  2679. * @format: format string (see printf)
  2680. * @...: extra parameters for the format
  2681. *
  2682. * Write a DTD with a formatted markup declarations part.
  2683. *
  2684. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2685. */
  2686. int XMLCDECL
  2687. xmlTextWriterWriteFormatDTD(xmlTextWriterPtr writer,
  2688. const xmlChar * name,
  2689. const xmlChar * pubid,
  2690. const xmlChar * sysid, const char *format, ...)
  2691. {
  2692. int rc;
  2693. va_list ap;
  2694. va_start(ap, format);
  2695. rc = xmlTextWriterWriteVFormatDTD(writer, name, pubid, sysid, format,
  2696. ap);
  2697. va_end(ap);
  2698. return rc;
  2699. }
  2700. /**
  2701. * xmlTextWriterWriteVFormatDTD:
  2702. * @writer: the xmlTextWriterPtr
  2703. * @name: the name of the DTD
  2704. * @pubid: the public identifier, which is an alternative to the system identifier
  2705. * @sysid: the system identifier, which is the URI of the DTD
  2706. * @format: format string (see printf)
  2707. * @argptr: pointer to the first member of the variable argument list.
  2708. *
  2709. * Write a DTD with a formatted markup declarations part.
  2710. *
  2711. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2712. */
  2713. int
  2714. xmlTextWriterWriteVFormatDTD(xmlTextWriterPtr writer,
  2715. const xmlChar * name,
  2716. const xmlChar * pubid,
  2717. const xmlChar * sysid,
  2718. const char *format, va_list argptr)
  2719. {
  2720. int rc;
  2721. xmlChar *buf;
  2722. if (writer == NULL)
  2723. return -1;
  2724. buf = xmlTextWriterVSprintf(format, argptr);
  2725. if (buf == NULL)
  2726. return -1;
  2727. rc = xmlTextWriterWriteDTD(writer, name, pubid, sysid, buf);
  2728. xmlFree(buf);
  2729. return rc;
  2730. }
  2731. /**
  2732. * xmlTextWriterWriteDTD:
  2733. * @writer: the xmlTextWriterPtr
  2734. * @name: the name of the DTD
  2735. * @pubid: the public identifier, which is an alternative to the system identifier
  2736. * @sysid: the system identifier, which is the URI of the DTD
  2737. * @subset: string content of the DTD
  2738. *
  2739. * Write a DTD.
  2740. *
  2741. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2742. */
  2743. int
  2744. xmlTextWriterWriteDTD(xmlTextWriterPtr writer,
  2745. const xmlChar * name,
  2746. const xmlChar * pubid,
  2747. const xmlChar * sysid, const xmlChar * subset)
  2748. {
  2749. int count;
  2750. int sum;
  2751. sum = 0;
  2752. count = xmlTextWriterStartDTD(writer, name, pubid, sysid);
  2753. if (count == -1)
  2754. return -1;
  2755. sum += count;
  2756. if (subset != 0) {
  2757. count = xmlTextWriterWriteString(writer, subset);
  2758. if (count == -1)
  2759. return -1;
  2760. sum += count;
  2761. }
  2762. count = xmlTextWriterEndDTD(writer);
  2763. if (count == -1)
  2764. return -1;
  2765. sum += count;
  2766. return sum;
  2767. }
  2768. /**
  2769. * xmlTextWriterStartDTDElement:
  2770. * @writer: the xmlTextWriterPtr
  2771. * @name: the name of the DTD element
  2772. *
  2773. * Start an xml DTD element.
  2774. *
  2775. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2776. */
  2777. int
  2778. xmlTextWriterStartDTDElement(xmlTextWriterPtr writer, const xmlChar * name)
  2779. {
  2780. int count;
  2781. int sum;
  2782. xmlLinkPtr lk;
  2783. xmlTextWriterStackEntry *p;
  2784. if (writer == NULL || name == NULL || *name == '\0')
  2785. return -1;
  2786. sum = 0;
  2787. lk = xmlListFront(writer->nodes);
  2788. if (lk == 0) {
  2789. return -1;
  2790. }
  2791. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  2792. if (p != 0) {
  2793. switch (p->state) {
  2794. case XML_TEXTWRITER_DTD:
  2795. count = xmlOutputBufferWriteString(writer->out, " [");
  2796. if (count < 0)
  2797. return -1;
  2798. sum += count;
  2799. if (writer->indent) {
  2800. count = xmlOutputBufferWriteString(writer->out, "\n");
  2801. if (count < 0)
  2802. return -1;
  2803. sum += count;
  2804. }
  2805. p->state = XML_TEXTWRITER_DTD_TEXT;
  2806. /* fallthrough */
  2807. case XML_TEXTWRITER_DTD_TEXT:
  2808. case XML_TEXTWRITER_NONE:
  2809. break;
  2810. default:
  2811. return -1;
  2812. }
  2813. }
  2814. p = (xmlTextWriterStackEntry *)
  2815. xmlMalloc(sizeof(xmlTextWriterStackEntry));
  2816. if (p == 0) {
  2817. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  2818. "xmlTextWriterStartDTDElement : out of memory!\n");
  2819. return -1;
  2820. }
  2821. p->name = xmlStrdup(name);
  2822. if (p->name == 0) {
  2823. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  2824. "xmlTextWriterStartDTDElement : out of memory!\n");
  2825. xmlFree(p);
  2826. return -1;
  2827. }
  2828. p->state = XML_TEXTWRITER_DTD_ELEM;
  2829. xmlListPushFront(writer->nodes, p);
  2830. if (writer->indent) {
  2831. count = xmlTextWriterWriteIndent(writer);
  2832. if (count < 0)
  2833. return -1;
  2834. sum += count;
  2835. }
  2836. count = xmlOutputBufferWriteString(writer->out, "<!ELEMENT ");
  2837. if (count < 0)
  2838. return -1;
  2839. sum += count;
  2840. count = xmlOutputBufferWriteString(writer->out, (const char *) name);
  2841. if (count < 0)
  2842. return -1;
  2843. sum += count;
  2844. return sum;
  2845. }
  2846. /**
  2847. * xmlTextWriterEndDTDElement:
  2848. * @writer: the xmlTextWriterPtr
  2849. *
  2850. * End an xml DTD element.
  2851. *
  2852. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2853. */
  2854. int
  2855. xmlTextWriterEndDTDElement(xmlTextWriterPtr writer)
  2856. {
  2857. int count;
  2858. int sum;
  2859. xmlLinkPtr lk;
  2860. xmlTextWriterStackEntry *p;
  2861. if (writer == NULL)
  2862. return -1;
  2863. sum = 0;
  2864. lk = xmlListFront(writer->nodes);
  2865. if (lk == 0)
  2866. return -1;
  2867. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  2868. if (p == 0)
  2869. return -1;
  2870. switch (p->state) {
  2871. case XML_TEXTWRITER_DTD_ELEM:
  2872. case XML_TEXTWRITER_DTD_ELEM_TEXT:
  2873. count = xmlOutputBufferWriteString(writer->out, ">");
  2874. if (count < 0)
  2875. return -1;
  2876. sum += count;
  2877. break;
  2878. default:
  2879. return -1;
  2880. }
  2881. if (writer->indent) {
  2882. count = xmlOutputBufferWriteString(writer->out, "\n");
  2883. if (count < 0)
  2884. return -1;
  2885. sum += count;
  2886. }
  2887. xmlListPopFront(writer->nodes);
  2888. return sum;
  2889. }
  2890. /**
  2891. * xmlTextWriterWriteFormatDTDElement:
  2892. * @writer: the xmlTextWriterPtr
  2893. * @name: the name of the DTD element
  2894. * @format: format string (see printf)
  2895. * @...: extra parameters for the format
  2896. *
  2897. * Write a formatted DTD element.
  2898. *
  2899. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2900. */
  2901. int XMLCDECL
  2902. xmlTextWriterWriteFormatDTDElement(xmlTextWriterPtr writer,
  2903. const xmlChar * name,
  2904. const char *format, ...)
  2905. {
  2906. int rc;
  2907. va_list ap;
  2908. va_start(ap, format);
  2909. rc = xmlTextWriterWriteVFormatDTDElement(writer, name, format, ap);
  2910. va_end(ap);
  2911. return rc;
  2912. }
  2913. /**
  2914. * xmlTextWriterWriteVFormatDTDElement:
  2915. * @writer: the xmlTextWriterPtr
  2916. * @name: the name of the DTD element
  2917. * @format: format string (see printf)
  2918. * @argptr: pointer to the first member of the variable argument list.
  2919. *
  2920. * Write a formatted DTD element.
  2921. *
  2922. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2923. */
  2924. int
  2925. xmlTextWriterWriteVFormatDTDElement(xmlTextWriterPtr writer,
  2926. const xmlChar * name,
  2927. const char *format, va_list argptr)
  2928. {
  2929. int rc;
  2930. xmlChar *buf;
  2931. if (writer == NULL)
  2932. return -1;
  2933. buf = xmlTextWriterVSprintf(format, argptr);
  2934. if (buf == NULL)
  2935. return -1;
  2936. rc = xmlTextWriterWriteDTDElement(writer, name, buf);
  2937. xmlFree(buf);
  2938. return rc;
  2939. }
  2940. /**
  2941. * xmlTextWriterWriteDTDElement:
  2942. * @writer: the xmlTextWriterPtr
  2943. * @name: the name of the DTD element
  2944. * @content: content of the element
  2945. *
  2946. * Write a DTD element.
  2947. *
  2948. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2949. */
  2950. int
  2951. xmlTextWriterWriteDTDElement(xmlTextWriterPtr writer,
  2952. const xmlChar * name, const xmlChar * content)
  2953. {
  2954. int count;
  2955. int sum;
  2956. if (content == NULL)
  2957. return -1;
  2958. sum = 0;
  2959. count = xmlTextWriterStartDTDElement(writer, name);
  2960. if (count == -1)
  2961. return -1;
  2962. sum += count;
  2963. count = xmlTextWriterWriteString(writer, content);
  2964. if (count == -1)
  2965. return -1;
  2966. sum += count;
  2967. count = xmlTextWriterEndDTDElement(writer);
  2968. if (count == -1)
  2969. return -1;
  2970. sum += count;
  2971. return sum;
  2972. }
  2973. /**
  2974. * xmlTextWriterStartDTDAttlist:
  2975. * @writer: the xmlTextWriterPtr
  2976. * @name: the name of the DTD ATTLIST
  2977. *
  2978. * Start an xml DTD ATTLIST.
  2979. *
  2980. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2981. */
  2982. int
  2983. xmlTextWriterStartDTDAttlist(xmlTextWriterPtr writer, const xmlChar * name)
  2984. {
  2985. int count;
  2986. int sum;
  2987. xmlLinkPtr lk;
  2988. xmlTextWriterStackEntry *p;
  2989. if (writer == NULL || name == NULL || *name == '\0')
  2990. return -1;
  2991. sum = 0;
  2992. lk = xmlListFront(writer->nodes);
  2993. if (lk == 0) {
  2994. return -1;
  2995. }
  2996. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  2997. if (p != 0) {
  2998. switch (p->state) {
  2999. case XML_TEXTWRITER_DTD:
  3000. count = xmlOutputBufferWriteString(writer->out, " [");
  3001. if (count < 0)
  3002. return -1;
  3003. sum += count;
  3004. if (writer->indent) {
  3005. count = xmlOutputBufferWriteString(writer->out, "\n");
  3006. if (count < 0)
  3007. return -1;
  3008. sum += count;
  3009. }
  3010. p->state = XML_TEXTWRITER_DTD_TEXT;
  3011. /* fallthrough */
  3012. case XML_TEXTWRITER_DTD_TEXT:
  3013. case XML_TEXTWRITER_NONE:
  3014. break;
  3015. default:
  3016. return -1;
  3017. }
  3018. }
  3019. p = (xmlTextWriterStackEntry *)
  3020. xmlMalloc(sizeof(xmlTextWriterStackEntry));
  3021. if (p == 0) {
  3022. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  3023. "xmlTextWriterStartDTDAttlist : out of memory!\n");
  3024. return -1;
  3025. }
  3026. p->name = xmlStrdup(name);
  3027. if (p->name == 0) {
  3028. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  3029. "xmlTextWriterStartDTDAttlist : out of memory!\n");
  3030. xmlFree(p);
  3031. return -1;
  3032. }
  3033. p->state = XML_TEXTWRITER_DTD_ATTL;
  3034. xmlListPushFront(writer->nodes, p);
  3035. if (writer->indent) {
  3036. count = xmlTextWriterWriteIndent(writer);
  3037. if (count < 0)
  3038. return -1;
  3039. sum += count;
  3040. }
  3041. count = xmlOutputBufferWriteString(writer->out, "<!ATTLIST ");
  3042. if (count < 0)
  3043. return -1;
  3044. sum += count;
  3045. count = xmlOutputBufferWriteString(writer->out, (const char *) name);
  3046. if (count < 0)
  3047. return -1;
  3048. sum += count;
  3049. return sum;
  3050. }
  3051. /**
  3052. * xmlTextWriterEndDTDAttlist:
  3053. * @writer: the xmlTextWriterPtr
  3054. *
  3055. * End an xml DTD attribute list.
  3056. *
  3057. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3058. */
  3059. int
  3060. xmlTextWriterEndDTDAttlist(xmlTextWriterPtr writer)
  3061. {
  3062. int count;
  3063. int sum;
  3064. xmlLinkPtr lk;
  3065. xmlTextWriterStackEntry *p;
  3066. if (writer == NULL)
  3067. return -1;
  3068. sum = 0;
  3069. lk = xmlListFront(writer->nodes);
  3070. if (lk == 0)
  3071. return -1;
  3072. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  3073. if (p == 0)
  3074. return -1;
  3075. switch (p->state) {
  3076. case XML_TEXTWRITER_DTD_ATTL:
  3077. case XML_TEXTWRITER_DTD_ATTL_TEXT:
  3078. count = xmlOutputBufferWriteString(writer->out, ">");
  3079. if (count < 0)
  3080. return -1;
  3081. sum += count;
  3082. break;
  3083. default:
  3084. return -1;
  3085. }
  3086. if (writer->indent) {
  3087. count = xmlOutputBufferWriteString(writer->out, "\n");
  3088. if (count < 0)
  3089. return -1;
  3090. sum += count;
  3091. }
  3092. xmlListPopFront(writer->nodes);
  3093. return sum;
  3094. }
  3095. /**
  3096. * xmlTextWriterWriteFormatDTDAttlist:
  3097. * @writer: the xmlTextWriterPtr
  3098. * @name: the name of the DTD ATTLIST
  3099. * @format: format string (see printf)
  3100. * @...: extra parameters for the format
  3101. *
  3102. * Write a formatted DTD ATTLIST.
  3103. *
  3104. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3105. */
  3106. int XMLCDECL
  3107. xmlTextWriterWriteFormatDTDAttlist(xmlTextWriterPtr writer,
  3108. const xmlChar * name,
  3109. const char *format, ...)
  3110. {
  3111. int rc;
  3112. va_list ap;
  3113. va_start(ap, format);
  3114. rc = xmlTextWriterWriteVFormatDTDAttlist(writer, name, format, ap);
  3115. va_end(ap);
  3116. return rc;
  3117. }
  3118. /**
  3119. * xmlTextWriterWriteVFormatDTDAttlist:
  3120. * @writer: the xmlTextWriterPtr
  3121. * @name: the name of the DTD ATTLIST
  3122. * @format: format string (see printf)
  3123. * @argptr: pointer to the first member of the variable argument list.
  3124. *
  3125. * Write a formatted DTD ATTLIST.
  3126. *
  3127. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3128. */
  3129. int
  3130. xmlTextWriterWriteVFormatDTDAttlist(xmlTextWriterPtr writer,
  3131. const xmlChar * name,
  3132. const char *format, va_list argptr)
  3133. {
  3134. int rc;
  3135. xmlChar *buf;
  3136. if (writer == NULL)
  3137. return -1;
  3138. buf = xmlTextWriterVSprintf(format, argptr);
  3139. if (buf == NULL)
  3140. return -1;
  3141. rc = xmlTextWriterWriteDTDAttlist(writer, name, buf);
  3142. xmlFree(buf);
  3143. return rc;
  3144. }
  3145. /**
  3146. * xmlTextWriterWriteDTDAttlist:
  3147. * @writer: the xmlTextWriterPtr
  3148. * @name: the name of the DTD ATTLIST
  3149. * @content: content of the ATTLIST
  3150. *
  3151. * Write a DTD ATTLIST.
  3152. *
  3153. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3154. */
  3155. int
  3156. xmlTextWriterWriteDTDAttlist(xmlTextWriterPtr writer,
  3157. const xmlChar * name, const xmlChar * content)
  3158. {
  3159. int count;
  3160. int sum;
  3161. if (content == NULL)
  3162. return -1;
  3163. sum = 0;
  3164. count = xmlTextWriterStartDTDAttlist(writer, name);
  3165. if (count == -1)
  3166. return -1;
  3167. sum += count;
  3168. count = xmlTextWriterWriteString(writer, content);
  3169. if (count == -1)
  3170. return -1;
  3171. sum += count;
  3172. count = xmlTextWriterEndDTDAttlist(writer);
  3173. if (count == -1)
  3174. return -1;
  3175. sum += count;
  3176. return sum;
  3177. }
  3178. /**
  3179. * xmlTextWriterStartDTDEntity:
  3180. * @writer: the xmlTextWriterPtr
  3181. * @pe: TRUE if this is a parameter entity, FALSE if not
  3182. * @name: the name of the DTD ATTLIST
  3183. *
  3184. * Start an xml DTD ATTLIST.
  3185. *
  3186. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3187. */
  3188. int
  3189. xmlTextWriterStartDTDEntity(xmlTextWriterPtr writer,
  3190. int pe, const xmlChar * name)
  3191. {
  3192. int count;
  3193. int sum;
  3194. xmlLinkPtr lk;
  3195. xmlTextWriterStackEntry *p;
  3196. if (writer == NULL || name == NULL || *name == '\0')
  3197. return -1;
  3198. sum = 0;
  3199. lk = xmlListFront(writer->nodes);
  3200. if (lk != 0) {
  3201. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  3202. if (p != 0) {
  3203. switch (p->state) {
  3204. case XML_TEXTWRITER_DTD:
  3205. count = xmlOutputBufferWriteString(writer->out, " [");
  3206. if (count < 0)
  3207. return -1;
  3208. sum += count;
  3209. if (writer->indent) {
  3210. count =
  3211. xmlOutputBufferWriteString(writer->out, "\n");
  3212. if (count < 0)
  3213. return -1;
  3214. sum += count;
  3215. }
  3216. p->state = XML_TEXTWRITER_DTD_TEXT;
  3217. /* fallthrough */
  3218. case XML_TEXTWRITER_DTD_TEXT:
  3219. case XML_TEXTWRITER_NONE:
  3220. break;
  3221. default:
  3222. return -1;
  3223. }
  3224. }
  3225. }
  3226. p = (xmlTextWriterStackEntry *)
  3227. xmlMalloc(sizeof(xmlTextWriterStackEntry));
  3228. if (p == 0) {
  3229. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  3230. "xmlTextWriterStartDTDElement : out of memory!\n");
  3231. return -1;
  3232. }
  3233. p->name = xmlStrdup(name);
  3234. if (p->name == 0) {
  3235. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  3236. "xmlTextWriterStartDTDElement : out of memory!\n");
  3237. xmlFree(p);
  3238. return -1;
  3239. }
  3240. if (pe != 0)
  3241. p->state = XML_TEXTWRITER_DTD_PENT;
  3242. else
  3243. p->state = XML_TEXTWRITER_DTD_ENTY;
  3244. xmlListPushFront(writer->nodes, p);
  3245. if (writer->indent) {
  3246. count = xmlTextWriterWriteIndent(writer);
  3247. if (count < 0)
  3248. return -1;
  3249. sum += count;
  3250. }
  3251. count = xmlOutputBufferWriteString(writer->out, "<!ENTITY ");
  3252. if (count < 0)
  3253. return -1;
  3254. sum += count;
  3255. if (pe != 0) {
  3256. count = xmlOutputBufferWriteString(writer->out, "% ");
  3257. if (count < 0)
  3258. return -1;
  3259. sum += count;
  3260. }
  3261. count = xmlOutputBufferWriteString(writer->out, (const char *) name);
  3262. if (count < 0)
  3263. return -1;
  3264. sum += count;
  3265. return sum;
  3266. }
  3267. /**
  3268. * xmlTextWriterEndDTDEntity:
  3269. * @writer: the xmlTextWriterPtr
  3270. *
  3271. * End an xml DTD entity.
  3272. *
  3273. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3274. */
  3275. int
  3276. xmlTextWriterEndDTDEntity(xmlTextWriterPtr writer)
  3277. {
  3278. int count;
  3279. int sum;
  3280. xmlLinkPtr lk;
  3281. xmlTextWriterStackEntry *p;
  3282. if (writer == NULL)
  3283. return -1;
  3284. sum = 0;
  3285. lk = xmlListFront(writer->nodes);
  3286. if (lk == 0)
  3287. return -1;
  3288. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  3289. if (p == 0)
  3290. return -1;
  3291. switch (p->state) {
  3292. case XML_TEXTWRITER_DTD_ENTY_TEXT:
  3293. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  3294. if (count < 0)
  3295. return -1;
  3296. sum += count;
  3297. case XML_TEXTWRITER_DTD_ENTY:
  3298. case XML_TEXTWRITER_DTD_PENT:
  3299. count = xmlOutputBufferWriteString(writer->out, ">");
  3300. if (count < 0)
  3301. return -1;
  3302. sum += count;
  3303. break;
  3304. default:
  3305. return -1;
  3306. }
  3307. if (writer->indent) {
  3308. count = xmlOutputBufferWriteString(writer->out, "\n");
  3309. if (count < 0)
  3310. return -1;
  3311. sum += count;
  3312. }
  3313. xmlListPopFront(writer->nodes);
  3314. return sum;
  3315. }
  3316. /**
  3317. * xmlTextWriterWriteFormatDTDInternalEntity:
  3318. * @writer: the xmlTextWriterPtr
  3319. * @pe: TRUE if this is a parameter entity, FALSE if not
  3320. * @name: the name of the DTD entity
  3321. * @format: format string (see printf)
  3322. * @...: extra parameters for the format
  3323. *
  3324. * Write a formatted DTD internal entity.
  3325. *
  3326. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3327. */
  3328. int XMLCDECL
  3329. xmlTextWriterWriteFormatDTDInternalEntity(xmlTextWriterPtr writer,
  3330. int pe,
  3331. const xmlChar * name,
  3332. const char *format, ...)
  3333. {
  3334. int rc;
  3335. va_list ap;
  3336. va_start(ap, format);
  3337. rc = xmlTextWriterWriteVFormatDTDInternalEntity(writer, pe, name,
  3338. format, ap);
  3339. va_end(ap);
  3340. return rc;
  3341. }
  3342. /**
  3343. * xmlTextWriterWriteVFormatDTDInternalEntity:
  3344. * @writer: the xmlTextWriterPtr
  3345. * @pe: TRUE if this is a parameter entity, FALSE if not
  3346. * @name: the name of the DTD entity
  3347. * @format: format string (see printf)
  3348. * @argptr: pointer to the first member of the variable argument list.
  3349. *
  3350. * Write a formatted DTD internal entity.
  3351. *
  3352. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3353. */
  3354. int
  3355. xmlTextWriterWriteVFormatDTDInternalEntity(xmlTextWriterPtr writer,
  3356. int pe,
  3357. const xmlChar * name,
  3358. const char *format,
  3359. va_list argptr)
  3360. {
  3361. int rc;
  3362. xmlChar *buf;
  3363. if (writer == NULL)
  3364. return -1;
  3365. buf = xmlTextWriterVSprintf(format, argptr);
  3366. if (buf == NULL)
  3367. return -1;
  3368. rc = xmlTextWriterWriteDTDInternalEntity(writer, pe, name, buf);
  3369. xmlFree(buf);
  3370. return rc;
  3371. }
  3372. /**
  3373. * xmlTextWriterWriteDTDEntity:
  3374. * @writer: the xmlTextWriterPtr
  3375. * @pe: TRUE if this is a parameter entity, FALSE if not
  3376. * @name: the name of the DTD entity
  3377. * @pubid: the public identifier, which is an alternative to the system identifier
  3378. * @sysid: the system identifier, which is the URI of the DTD
  3379. * @ndataid: the xml notation name.
  3380. * @content: content of the entity
  3381. *
  3382. * Write a DTD entity.
  3383. *
  3384. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3385. */
  3386. int
  3387. xmlTextWriterWriteDTDEntity(xmlTextWriterPtr writer,
  3388. int pe,
  3389. const xmlChar * name,
  3390. const xmlChar * pubid,
  3391. const xmlChar * sysid,
  3392. const xmlChar * ndataid,
  3393. const xmlChar * content)
  3394. {
  3395. if ((content == NULL) && (pubid == NULL) && (sysid == NULL))
  3396. return -1;
  3397. if ((pe != 0) && (ndataid != NULL))
  3398. return -1;
  3399. if ((pubid == NULL) && (sysid == NULL))
  3400. return xmlTextWriterWriteDTDInternalEntity(writer, pe, name,
  3401. content);
  3402. return xmlTextWriterWriteDTDExternalEntity(writer, pe, name, pubid,
  3403. sysid, ndataid);
  3404. }
  3405. /**
  3406. * xmlTextWriterWriteDTDInternalEntity:
  3407. * @writer: the xmlTextWriterPtr
  3408. * @pe: TRUE if this is a parameter entity, FALSE if not
  3409. * @name: the name of the DTD entity
  3410. * @content: content of the entity
  3411. *
  3412. * Write a DTD internal entity.
  3413. *
  3414. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3415. */
  3416. int
  3417. xmlTextWriterWriteDTDInternalEntity(xmlTextWriterPtr writer,
  3418. int pe,
  3419. const xmlChar * name,
  3420. const xmlChar * content)
  3421. {
  3422. int count;
  3423. int sum;
  3424. if ((name == NULL) || (*name == '\0') || (content == NULL))
  3425. return -1;
  3426. sum = 0;
  3427. count = xmlTextWriterStartDTDEntity(writer, pe, name);
  3428. if (count == -1)
  3429. return -1;
  3430. sum += count;
  3431. count = xmlTextWriterWriteString(writer, content);
  3432. if (count == -1)
  3433. return -1;
  3434. sum += count;
  3435. count = xmlTextWriterEndDTDEntity(writer);
  3436. if (count == -1)
  3437. return -1;
  3438. sum += count;
  3439. return sum;
  3440. }
  3441. /**
  3442. * xmlTextWriterWriteDTDExternalEntity:
  3443. * @writer: the xmlTextWriterPtr
  3444. * @pe: TRUE if this is a parameter entity, FALSE if not
  3445. * @name: the name of the DTD entity
  3446. * @pubid: the public identifier, which is an alternative to the system identifier
  3447. * @sysid: the system identifier, which is the URI of the DTD
  3448. * @ndataid: the xml notation name.
  3449. *
  3450. * Write a DTD external entity. The entity must have been started with xmlTextWriterStartDTDEntity
  3451. *
  3452. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3453. */
  3454. int
  3455. xmlTextWriterWriteDTDExternalEntity(xmlTextWriterPtr writer,
  3456. int pe,
  3457. const xmlChar * name,
  3458. const xmlChar * pubid,
  3459. const xmlChar * sysid,
  3460. const xmlChar * ndataid)
  3461. {
  3462. int count;
  3463. int sum;
  3464. if (((pubid == NULL) && (sysid == NULL)))
  3465. return -1;
  3466. if ((pe != 0) && (ndataid != NULL))
  3467. return -1;
  3468. sum = 0;
  3469. count = xmlTextWriterStartDTDEntity(writer, pe, name);
  3470. if (count == -1)
  3471. return -1;
  3472. sum += count;
  3473. count =
  3474. xmlTextWriterWriteDTDExternalEntityContents(writer, pubid, sysid,
  3475. ndataid);
  3476. if (count < 0)
  3477. return -1;
  3478. sum += count;
  3479. count = xmlTextWriterEndDTDEntity(writer);
  3480. if (count == -1)
  3481. return -1;
  3482. sum += count;
  3483. return sum;
  3484. }
  3485. /**
  3486. * xmlTextWriterWriteDTDExternalEntityContents:
  3487. * @writer: the xmlTextWriterPtr
  3488. * @pubid: the public identifier, which is an alternative to the system identifier
  3489. * @sysid: the system identifier, which is the URI of the DTD
  3490. * @ndataid: the xml notation name.
  3491. *
  3492. * Write the contents of a DTD external entity.
  3493. *
  3494. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3495. */
  3496. int
  3497. xmlTextWriterWriteDTDExternalEntityContents(xmlTextWriterPtr writer,
  3498. const xmlChar * pubid,
  3499. const xmlChar * sysid,
  3500. const xmlChar * ndataid)
  3501. {
  3502. int count;
  3503. int sum;
  3504. xmlLinkPtr lk;
  3505. xmlTextWriterStackEntry *p;
  3506. if (writer == NULL) {
  3507. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  3508. "xmlTextWriterWriteDTDExternalEntityContents: xmlTextWriterPtr invalid!\n");
  3509. return -1;
  3510. }
  3511. sum = 0;
  3512. lk = xmlListFront(writer->nodes);
  3513. if (lk == 0) {
  3514. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  3515. "xmlTextWriterWriteDTDExternalEntityContents: you must call xmlTextWriterStartDTDEntity before the call to this function!\n");
  3516. return -1;
  3517. }
  3518. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  3519. if (p == 0)
  3520. return -1;
  3521. switch (p->state) {
  3522. case XML_TEXTWRITER_DTD_ENTY:
  3523. break;
  3524. case XML_TEXTWRITER_DTD_PENT:
  3525. if (ndataid != NULL) {
  3526. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  3527. "xmlTextWriterWriteDTDExternalEntityContents: notation not allowed with parameter entities!\n");
  3528. return -1;
  3529. }
  3530. break;
  3531. default:
  3532. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  3533. "xmlTextWriterWriteDTDExternalEntityContents: you must call xmlTextWriterStartDTDEntity before the call to this function!\n");
  3534. return -1;
  3535. }
  3536. if (pubid != 0) {
  3537. if (sysid == 0) {
  3538. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  3539. "xmlTextWriterWriteDTDExternalEntityContents: system identifier needed!\n");
  3540. return -1;
  3541. }
  3542. count = xmlOutputBufferWriteString(writer->out, " PUBLIC ");
  3543. if (count < 0)
  3544. return -1;
  3545. sum += count;
  3546. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  3547. if (count < 0)
  3548. return -1;
  3549. sum += count;
  3550. count =
  3551. xmlOutputBufferWriteString(writer->out, (const char *) pubid);
  3552. if (count < 0)
  3553. return -1;
  3554. sum += count;
  3555. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  3556. if (count < 0)
  3557. return -1;
  3558. sum += count;
  3559. }
  3560. if (sysid != 0) {
  3561. if (pubid == 0) {
  3562. count = xmlOutputBufferWriteString(writer->out, " SYSTEM");
  3563. if (count < 0)
  3564. return -1;
  3565. sum += count;
  3566. }
  3567. count = xmlOutputBufferWriteString(writer->out, " ");
  3568. if (count < 0)
  3569. return -1;
  3570. sum += count;
  3571. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  3572. if (count < 0)
  3573. return -1;
  3574. sum += count;
  3575. count =
  3576. xmlOutputBufferWriteString(writer->out, (const char *) sysid);
  3577. if (count < 0)
  3578. return -1;
  3579. sum += count;
  3580. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  3581. if (count < 0)
  3582. return -1;
  3583. sum += count;
  3584. }
  3585. if (ndataid != NULL) {
  3586. count = xmlOutputBufferWriteString(writer->out, " NDATA ");
  3587. if (count < 0)
  3588. return -1;
  3589. sum += count;
  3590. count =
  3591. xmlOutputBufferWriteString(writer->out,
  3592. (const char *) ndataid);
  3593. if (count < 0)
  3594. return -1;
  3595. sum += count;
  3596. }
  3597. return sum;
  3598. }
  3599. /**
  3600. * xmlTextWriterWriteDTDNotation:
  3601. * @writer: the xmlTextWriterPtr
  3602. * @name: the name of the xml notation
  3603. * @pubid: the public identifier, which is an alternative to the system identifier
  3604. * @sysid: the system identifier, which is the URI of the DTD
  3605. *
  3606. * Write a DTD entity.
  3607. *
  3608. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3609. */
  3610. int
  3611. xmlTextWriterWriteDTDNotation(xmlTextWriterPtr writer,
  3612. const xmlChar * name,
  3613. const xmlChar * pubid, const xmlChar * sysid)
  3614. {
  3615. int count;
  3616. int sum;
  3617. xmlLinkPtr lk;
  3618. xmlTextWriterStackEntry *p;
  3619. if (writer == NULL || name == NULL || *name == '\0')
  3620. return -1;
  3621. sum = 0;
  3622. lk = xmlListFront(writer->nodes);
  3623. if (lk == 0) {
  3624. return -1;
  3625. }
  3626. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  3627. if (p != 0) {
  3628. switch (p->state) {
  3629. case XML_TEXTWRITER_DTD:
  3630. count = xmlOutputBufferWriteString(writer->out, " [");
  3631. if (count < 0)
  3632. return -1;
  3633. sum += count;
  3634. if (writer->indent) {
  3635. count = xmlOutputBufferWriteString(writer->out, "\n");
  3636. if (count < 0)
  3637. return -1;
  3638. sum += count;
  3639. }
  3640. p->state = XML_TEXTWRITER_DTD_TEXT;
  3641. /* fallthrough */
  3642. case XML_TEXTWRITER_DTD_TEXT:
  3643. break;
  3644. default:
  3645. return -1;
  3646. }
  3647. }
  3648. if (writer->indent) {
  3649. count = xmlTextWriterWriteIndent(writer);
  3650. if (count < 0)
  3651. return -1;
  3652. sum += count;
  3653. }
  3654. count = xmlOutputBufferWriteString(writer->out, "<!NOTATION ");
  3655. if (count < 0)
  3656. return -1;
  3657. sum += count;
  3658. count = xmlOutputBufferWriteString(writer->out, (const char *) name);
  3659. if (count < 0)
  3660. return -1;
  3661. sum += count;
  3662. if (pubid != 0) {
  3663. count = xmlOutputBufferWriteString(writer->out, " PUBLIC ");
  3664. if (count < 0)
  3665. return -1;
  3666. sum += count;
  3667. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  3668. if (count < 0)
  3669. return -1;
  3670. sum += count;
  3671. count =
  3672. xmlOutputBufferWriteString(writer->out, (const char *) pubid);
  3673. if (count < 0)
  3674. return -1;
  3675. sum += count;
  3676. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  3677. if (count < 0)
  3678. return -1;
  3679. sum += count;
  3680. }
  3681. if (sysid != 0) {
  3682. if (pubid == 0) {
  3683. count = xmlOutputBufferWriteString(writer->out, " SYSTEM");
  3684. if (count < 0)
  3685. return -1;
  3686. sum += count;
  3687. }
  3688. count = xmlOutputBufferWriteString(writer->out, " ");
  3689. if (count < 0)
  3690. return -1;
  3691. sum += count;
  3692. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  3693. if (count < 0)
  3694. return -1;
  3695. sum += count;
  3696. count =
  3697. xmlOutputBufferWriteString(writer->out, (const char *) sysid);
  3698. if (count < 0)
  3699. return -1;
  3700. sum += count;
  3701. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  3702. if (count < 0)
  3703. return -1;
  3704. sum += count;
  3705. }
  3706. count = xmlOutputBufferWriteString(writer->out, ">");
  3707. if (count < 0)
  3708. return -1;
  3709. sum += count;
  3710. return sum;
  3711. }
  3712. /**
  3713. * xmlTextWriterFlush:
  3714. * @writer: the xmlTextWriterPtr
  3715. *
  3716. * Flush the output buffer.
  3717. *
  3718. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3719. */
  3720. int
  3721. xmlTextWriterFlush(xmlTextWriterPtr writer)
  3722. {
  3723. int count;
  3724. if (writer == NULL)
  3725. return -1;
  3726. if (writer->out == NULL)
  3727. count = 0;
  3728. else
  3729. count = xmlOutputBufferFlush(writer->out);
  3730. return count;
  3731. }
  3732. /**
  3733. * misc
  3734. */
  3735. /**
  3736. * xmlFreeTextWriterStackEntry:
  3737. * @lk: the xmlLinkPtr
  3738. *
  3739. * Free callback for the xmlList.
  3740. */
  3741. static void
  3742. xmlFreeTextWriterStackEntry(xmlLinkPtr lk)
  3743. {
  3744. xmlTextWriterStackEntry *p;
  3745. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  3746. if (p == 0)
  3747. return;
  3748. if (p->name != 0)
  3749. xmlFree(p->name);
  3750. xmlFree(p);
  3751. }
  3752. /**
  3753. * xmlCmpTextWriterStackEntry:
  3754. * @data0: the first data
  3755. * @data1: the second data
  3756. *
  3757. * Compare callback for the xmlList.
  3758. *
  3759. * Returns -1, 0, 1
  3760. */
  3761. static int
  3762. xmlCmpTextWriterStackEntry(const void *data0, const void *data1)
  3763. {
  3764. xmlTextWriterStackEntry *p0;
  3765. xmlTextWriterStackEntry *p1;
  3766. if (data0 == data1)
  3767. return 0;
  3768. if (data0 == 0)
  3769. return -1;
  3770. if (data1 == 0)
  3771. return 1;
  3772. p0 = (xmlTextWriterStackEntry *) data0;
  3773. p1 = (xmlTextWriterStackEntry *) data1;
  3774. return xmlStrcmp(p0->name, p1->name);
  3775. }
  3776. /**
  3777. * misc
  3778. */
  3779. /**
  3780. * xmlTextWriterOutputNSDecl:
  3781. * @writer: the xmlTextWriterPtr
  3782. *
  3783. * Output the current namespace declarations.
  3784. */
  3785. static int
  3786. xmlTextWriterOutputNSDecl(xmlTextWriterPtr writer)
  3787. {
  3788. xmlLinkPtr lk;
  3789. xmlTextWriterNsStackEntry *np;
  3790. int count;
  3791. int sum;
  3792. sum = 0;
  3793. while (!xmlListEmpty(writer->nsstack)) {
  3794. xmlChar *namespaceURI = NULL;
  3795. xmlChar *prefix = NULL;
  3796. lk = xmlListFront(writer->nsstack);
  3797. np = (xmlTextWriterNsStackEntry *) xmlLinkGetData(lk);
  3798. if (np != 0) {
  3799. namespaceURI = xmlStrdup(np->uri);
  3800. prefix = xmlStrdup(np->prefix);
  3801. }
  3802. xmlListPopFront(writer->nsstack);
  3803. if (np != 0) {
  3804. count = xmlTextWriterWriteAttribute(writer, prefix, namespaceURI);
  3805. xmlFree(namespaceURI);
  3806. xmlFree(prefix);
  3807. if (count < 0) {
  3808. xmlListDelete(writer->nsstack);
  3809. writer->nsstack = NULL;
  3810. return -1;
  3811. }
  3812. sum += count;
  3813. }
  3814. }
  3815. return sum;
  3816. }
  3817. /**
  3818. * xmlFreeTextWriterNsStackEntry:
  3819. * @lk: the xmlLinkPtr
  3820. *
  3821. * Free callback for the xmlList.
  3822. */
  3823. static void
  3824. xmlFreeTextWriterNsStackEntry(xmlLinkPtr lk)
  3825. {
  3826. xmlTextWriterNsStackEntry *p;
  3827. p = (xmlTextWriterNsStackEntry *) xmlLinkGetData(lk);
  3828. if (p == 0)
  3829. return;
  3830. if (p->prefix != 0)
  3831. xmlFree(p->prefix);
  3832. if (p->uri != 0)
  3833. xmlFree(p->uri);
  3834. xmlFree(p);
  3835. }
  3836. /**
  3837. * xmlCmpTextWriterNsStackEntry:
  3838. * @data0: the first data
  3839. * @data1: the second data
  3840. *
  3841. * Compare callback for the xmlList.
  3842. *
  3843. * Returns -1, 0, 1
  3844. */
  3845. static int
  3846. xmlCmpTextWriterNsStackEntry(const void *data0, const void *data1)
  3847. {
  3848. xmlTextWriterNsStackEntry *p0;
  3849. xmlTextWriterNsStackEntry *p1;
  3850. int rc;
  3851. if (data0 == data1)
  3852. return 0;
  3853. if (data0 == 0)
  3854. return -1;
  3855. if (data1 == 0)
  3856. return 1;
  3857. p0 = (xmlTextWriterNsStackEntry *) data0;
  3858. p1 = (xmlTextWriterNsStackEntry *) data1;
  3859. rc = xmlStrcmp(p0->prefix, p1->prefix);
  3860. if ((rc != 0) || (p0->elem != p1->elem))
  3861. rc = -1;
  3862. return rc;
  3863. }
  3864. /**
  3865. * xmlTextWriterWriteDocCallback:
  3866. * @context: the xmlBufferPtr
  3867. * @str: the data to write
  3868. * @len: the length of the data
  3869. *
  3870. * Write callback for the xmlOutputBuffer with target xmlBuffer
  3871. *
  3872. * Returns -1, 0, 1
  3873. */
  3874. static int
  3875. xmlTextWriterWriteDocCallback(void *context, const xmlChar * str, int len)
  3876. {
  3877. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) context;
  3878. int rc;
  3879. if ((rc = xmlParseChunk(ctxt, (const char *) str, len, 0)) != 0) {
  3880. xmlWriterErrMsgInt(NULL, XML_ERR_INTERNAL_ERROR,
  3881. "xmlTextWriterWriteDocCallback : XML error %d !\n",
  3882. rc);
  3883. return -1;
  3884. }
  3885. return len;
  3886. }
  3887. /**
  3888. * xmlTextWriterCloseDocCallback:
  3889. * @context: the xmlBufferPtr
  3890. *
  3891. * Close callback for the xmlOutputBuffer with target xmlBuffer
  3892. *
  3893. * Returns -1, 0, 1
  3894. */
  3895. static int
  3896. xmlTextWriterCloseDocCallback(void *context)
  3897. {
  3898. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) context;
  3899. int rc;
  3900. if ((rc = xmlParseChunk(ctxt, NULL, 0, 1)) != 0) {
  3901. xmlWriterErrMsgInt(NULL, XML_ERR_INTERNAL_ERROR,
  3902. "xmlTextWriterWriteDocCallback : XML error %d !\n",
  3903. rc);
  3904. return -1;
  3905. }
  3906. return 0;
  3907. }
  3908. /**
  3909. * xmlTextWriterVSprintf:
  3910. * @format: see printf
  3911. * @argptr: pointer to the first member of the variable argument list.
  3912. *
  3913. * Utility function for formatted output
  3914. *
  3915. * Returns a new xmlChar buffer with the data or NULL on error. This buffer must be freed.
  3916. */
  3917. static xmlChar *
  3918. xmlTextWriterVSprintf(const char *format, va_list argptr)
  3919. {
  3920. int size;
  3921. int count;
  3922. xmlChar *buf;
  3923. va_list locarg;
  3924. size = BUFSIZ;
  3925. buf = (xmlChar *) xmlMalloc(size);
  3926. if (buf == NULL) {
  3927. xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
  3928. "xmlTextWriterVSprintf : out of memory!\n");
  3929. return NULL;
  3930. }
  3931. VA_COPY(locarg, argptr);
  3932. while (((count = vsnprintf((char *) buf, size, format, locarg)) < 0)
  3933. || (count == size - 1) || (count == size) || (count > size)) {
  3934. va_end(locarg);
  3935. xmlFree(buf);
  3936. size += BUFSIZ;
  3937. buf = (xmlChar *) xmlMalloc(size);
  3938. if (buf == NULL) {
  3939. xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
  3940. "xmlTextWriterVSprintf : out of memory!\n");
  3941. return NULL;
  3942. }
  3943. VA_COPY(locarg, argptr);
  3944. }
  3945. va_end(locarg);
  3946. return buf;
  3947. }
  3948. /**
  3949. * xmlTextWriterStartDocumentCallback:
  3950. * @ctx: the user data (XML parser context)
  3951. *
  3952. * called at the start of document processing.
  3953. */
  3954. static void
  3955. xmlTextWriterStartDocumentCallback(void *ctx)
  3956. {
  3957. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  3958. xmlDocPtr doc;
  3959. if (ctxt->html) {
  3960. #ifdef LIBXML_HTML_ENABLED
  3961. if (ctxt->myDoc == NULL)
  3962. ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
  3963. if (ctxt->myDoc == NULL) {
  3964. if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
  3965. ctxt->sax->error(ctxt->userData,
  3966. "SAX.startDocument(): out of memory\n");
  3967. ctxt->errNo = XML_ERR_NO_MEMORY;
  3968. ctxt->instate = XML_PARSER_EOF;
  3969. ctxt->disableSAX = 1;
  3970. return;
  3971. }
  3972. #else
  3973. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  3974. "libxml2 built without HTML support\n");
  3975. ctxt->errNo = XML_ERR_INTERNAL_ERROR;
  3976. ctxt->instate = XML_PARSER_EOF;
  3977. ctxt->disableSAX = 1;
  3978. return;
  3979. #endif
  3980. } else {
  3981. doc = ctxt->myDoc;
  3982. if (doc == NULL)
  3983. doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
  3984. if (doc != NULL) {
  3985. if (doc->children == NULL) {
  3986. if (ctxt->encoding != NULL)
  3987. doc->encoding = xmlStrdup(ctxt->encoding);
  3988. else
  3989. doc->encoding = NULL;
  3990. doc->standalone = ctxt->standalone;
  3991. }
  3992. } else {
  3993. if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
  3994. ctxt->sax->error(ctxt->userData,
  3995. "SAX.startDocument(): out of memory\n");
  3996. ctxt->errNo = XML_ERR_NO_MEMORY;
  3997. ctxt->instate = XML_PARSER_EOF;
  3998. ctxt->disableSAX = 1;
  3999. return;
  4000. }
  4001. }
  4002. if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
  4003. (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
  4004. ctxt->myDoc->URL =
  4005. xmlCanonicPath((const xmlChar *) ctxt->input->filename);
  4006. if (ctxt->myDoc->URL == NULL)
  4007. ctxt->myDoc->URL =
  4008. xmlStrdup((const xmlChar *) ctxt->input->filename);
  4009. }
  4010. }
  4011. /**
  4012. * xmlTextWriterSetIndent:
  4013. * @writer: the xmlTextWriterPtr
  4014. * @indent: do indentation?
  4015. *
  4016. * Set indentation output. indent = 0 do not indentation. indent > 0 do indentation.
  4017. *
  4018. * Returns -1 on error or 0 otherwise.
  4019. */
  4020. int
  4021. xmlTextWriterSetIndent(xmlTextWriterPtr writer, int indent)
  4022. {
  4023. if ((writer == NULL) || (indent < 0))
  4024. return -1;
  4025. writer->indent = indent;
  4026. writer->doindent = 1;
  4027. return 0;
  4028. }
  4029. /**
  4030. * xmlTextWriterSetIndentString:
  4031. * @writer: the xmlTextWriterPtr
  4032. * @str: the xmlChar string
  4033. *
  4034. * Set string indentation.
  4035. *
  4036. * Returns -1 on error or 0 otherwise.
  4037. */
  4038. int
  4039. xmlTextWriterSetIndentString(xmlTextWriterPtr writer, const xmlChar * str)
  4040. {
  4041. if ((writer == NULL) || (!str))
  4042. return -1;
  4043. if (writer->ichar != NULL)
  4044. xmlFree(writer->ichar);
  4045. writer->ichar = xmlStrdup(str);
  4046. if (!writer->ichar)
  4047. return -1;
  4048. else
  4049. return 0;
  4050. }
  4051. /**
  4052. * xmlTextWriterWriteIndent:
  4053. * @writer: the xmlTextWriterPtr
  4054. *
  4055. * Write indent string.
  4056. *
  4057. * Returns -1 on error or the number of strings written.
  4058. */
  4059. static int
  4060. xmlTextWriterWriteIndent(xmlTextWriterPtr writer)
  4061. {
  4062. int lksize;
  4063. int i;
  4064. int ret;
  4065. lksize = xmlListSize(writer->nodes);
  4066. if (lksize < 1)
  4067. return (-1); /* list is empty */
  4068. for (i = 0; i < (lksize - 1); i++) {
  4069. ret = xmlOutputBufferWriteString(writer->out,
  4070. (const char *) writer->ichar);
  4071. if (ret == -1)
  4072. return (-1);
  4073. }
  4074. return (lksize - 1);
  4075. }
  4076. /**
  4077. * xmlTextWriterHandleStateDependencies:
  4078. * @writer: the xmlTextWriterPtr
  4079. * @p: the xmlTextWriterStackEntry
  4080. *
  4081. * Write state dependent strings.
  4082. *
  4083. * Returns -1 on error or the number of characters written.
  4084. */
  4085. static int
  4086. xmlTextWriterHandleStateDependencies(xmlTextWriterPtr writer,
  4087. xmlTextWriterStackEntry * p)
  4088. {
  4089. int count;
  4090. int sum;
  4091. char extra[3];
  4092. if (writer == NULL)
  4093. return -1;
  4094. if (p == NULL)
  4095. return 0;
  4096. sum = 0;
  4097. extra[0] = extra[1] = extra[2] = '\0';
  4098. if (p != 0) {
  4099. sum = 0;
  4100. switch (p->state) {
  4101. case XML_TEXTWRITER_NAME:
  4102. /* Output namespace declarations */
  4103. count = xmlTextWriterOutputNSDecl(writer);
  4104. if (count < 0)
  4105. return -1;
  4106. sum += count;
  4107. extra[0] = '>';
  4108. p->state = XML_TEXTWRITER_TEXT;
  4109. break;
  4110. case XML_TEXTWRITER_PI:
  4111. extra[0] = ' ';
  4112. p->state = XML_TEXTWRITER_PI_TEXT;
  4113. break;
  4114. case XML_TEXTWRITER_DTD:
  4115. extra[0] = ' ';
  4116. extra[1] = '[';
  4117. p->state = XML_TEXTWRITER_DTD_TEXT;
  4118. break;
  4119. case XML_TEXTWRITER_DTD_ELEM:
  4120. extra[0] = ' ';
  4121. p->state = XML_TEXTWRITER_DTD_ELEM_TEXT;
  4122. break;
  4123. case XML_TEXTWRITER_DTD_ATTL:
  4124. extra[0] = ' ';
  4125. p->state = XML_TEXTWRITER_DTD_ATTL_TEXT;
  4126. break;
  4127. case XML_TEXTWRITER_DTD_ENTY:
  4128. case XML_TEXTWRITER_DTD_PENT:
  4129. extra[0] = ' ';
  4130. extra[1] = writer->qchar;
  4131. p->state = XML_TEXTWRITER_DTD_ENTY_TEXT;
  4132. break;
  4133. default:
  4134. break;
  4135. }
  4136. }
  4137. if (*extra != '\0') {
  4138. count = xmlOutputBufferWriteString(writer->out, extra);
  4139. if (count < 0)
  4140. return -1;
  4141. sum += count;
  4142. }
  4143. return sum;
  4144. }
  4145. #define bottom_xmlwriter
  4146. #include "elfgcchack.h"
  4147. #endif