Ai
1 Star 0 Fork 0

liuyuyan6100/libxml2

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
xmlwriter.c 122.09 KB
一键复制 编辑 原始数据 按行查看 历史
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742
/*
* xmlwriter.c: XML text writer implementation
*
* For license and disclaimer see the license and disclaimer of
* libxml2.
*
* alfred@mickautsch.de
*/
#define IN_LIBXML
#include "libxml.h"
#include <string.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/uri.h>
#include <libxml/HTMLtree.h>
#include <libxml/SAX2.h>
#ifdef LIBXML_WRITER_ENABLED
#include <libxml/xmlwriter.h>
#include "private/buf.h"
#include "private/enc.h"
#include "private/error.h"
#include "private/save.h"
#define B64LINELEN 72
#define B64CRLF "\r\n"
/*
* The following VA_COPY was coded following an example in
* the Samba project. It may not be sufficient for some
* esoteric implementations of va_list but (hopefully) will
* be sufficient for libxml2.
*/
#ifndef VA_COPY
#ifdef HAVE_VA_COPY
#define VA_COPY(dest, src) va_copy(dest, src)
#else
#ifdef HAVE___VA_COPY
#define VA_COPY(dest,src) __va_copy(dest, src)
#else
#ifndef VA_LIST_IS_ARRAY
#define VA_COPY(dest,src) (dest) = (src)
#else
#include <string.h>
#define VA_COPY(dest,src) memcpy((char *)(dest),(char *)(src),sizeof(va_list))
#endif
#endif
#endif
#endif
/*
* Types are kept private
*/
typedef enum {
XML_TEXTWRITER_NONE = 0,
XML_TEXTWRITER_NAME,
XML_TEXTWRITER_ATTRIBUTE,
XML_TEXTWRITER_TEXT,
XML_TEXTWRITER_PI,
XML_TEXTWRITER_PI_TEXT,
XML_TEXTWRITER_CDATA,
XML_TEXTWRITER_DTD,
XML_TEXTWRITER_DTD_TEXT,
XML_TEXTWRITER_DTD_ELEM,
XML_TEXTWRITER_DTD_ELEM_TEXT,
XML_TEXTWRITER_DTD_ATTL,
XML_TEXTWRITER_DTD_ATTL_TEXT,
XML_TEXTWRITER_DTD_ENTY, /* entity */
XML_TEXTWRITER_DTD_ENTY_TEXT,
XML_TEXTWRITER_DTD_PENT, /* parameter entity */
XML_TEXTWRITER_COMMENT
} xmlTextWriterState;
typedef struct _xmlTextWriterStackEntry xmlTextWriterStackEntry;
struct _xmlTextWriterStackEntry {
xmlChar *name;
xmlTextWriterState state;
};
typedef struct _xmlTextWriterNsStackEntry xmlTextWriterNsStackEntry;
struct _xmlTextWriterNsStackEntry {
xmlChar *prefix;
xmlChar *uri;
xmlLinkPtr elem;
};
struct _xmlTextWriter {
xmlOutputBufferPtr out; /* output buffer */
xmlListPtr nodes; /* element name stack */
xmlListPtr nsstack; /* name spaces stack */
int level;
int indent; /* enable indent */
int doindent; /* internal indent flag */
xmlChar *ichar; /* indent character */
char qchar; /* character used for quoting attribute values */
xmlParserCtxtPtr ctxt;
int no_doc_free;
xmlDocPtr doc;
};
static void xmlFreeTextWriterStackEntry(xmlLinkPtr lk);
static int xmlCmpTextWriterStackEntry(const void *data0,
const void *data1);
static int xmlTextWriterOutputNSDecl(xmlTextWriterPtr writer);
static void xmlFreeTextWriterNsStackEntry(xmlLinkPtr lk);
static int xmlCmpTextWriterNsStackEntry(const void *data0,
const void *data1);
static int xmlTextWriterWriteDocCallback(void *context,
const char *str, int len);
static int xmlTextWriterCloseDocCallback(void *context);
static xmlChar *xmlTextWriterVSprintf(const char *format, va_list argptr) LIBXML_ATTR_FORMAT(1,0);
static int xmlOutputBufferWriteBase64(xmlOutputBufferPtr out, int len,
const unsigned char *data);
static void xmlTextWriterStartDocumentCallback(void *ctx);
static int xmlTextWriterWriteIndent(xmlTextWriterPtr writer);
static int
xmlTextWriterHandleStateDependencies(xmlTextWriterPtr writer,
xmlTextWriterStackEntry * p);
/**
* xmlWriterErrMsg:
* @ctxt: a writer context
* @error: the error number
* @msg: the error message
*
* Handle a writer error
*/
static void
xmlWriterErrMsg(xmlTextWriterPtr ctxt, xmlParserErrors error,
const char *msg)
{
if (ctxt != NULL) {
__xmlRaiseError(NULL, NULL, NULL, ctxt->ctxt,
NULL, XML_FROM_WRITER, error, XML_ERR_FATAL,
NULL, 0, NULL, NULL, NULL, 0, 0, "%s", msg);
} else {
__xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_WRITER, error,
XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, "%s", msg);
}
}
/**
* xmlWriterErrMsgInt:
* @ctxt: a writer context
* @error: the error number
* @msg: the error message
* @val: an int
*
* Handle a writer error
*/
static void LIBXML_ATTR_FORMAT(3,0)
xmlWriterErrMsgInt(xmlTextWriterPtr ctxt, xmlParserErrors error,
const char *msg, int val)
{
if (ctxt != NULL) {
__xmlRaiseError(NULL, NULL, NULL, ctxt->ctxt,
NULL, XML_FROM_WRITER, error, XML_ERR_FATAL,
NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
} else {
__xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_WRITER, error,
XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
}
}
/**
* xmlNewTextWriter:
* @out: an xmlOutputBufferPtr
*
* Create a new xmlNewTextWriter structure using an xmlOutputBufferPtr
* NOTE: the @out parameter will be deallocated when the writer is closed
* (if the call succeed.)
*
* Returns the new xmlTextWriterPtr or NULL in case of error
*/
xmlTextWriterPtr
xmlNewTextWriter(xmlOutputBufferPtr out)
{
xmlTextWriterPtr ret;
ret = (xmlTextWriterPtr) xmlMalloc(sizeof(xmlTextWriter));
if (ret == NULL) {
xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
"xmlNewTextWriter : out of memory!\n");
return NULL;
}
memset(ret, 0, sizeof(xmlTextWriter));
ret->nodes = xmlListCreate(xmlFreeTextWriterStackEntry,
xmlCmpTextWriterStackEntry);
if (ret->nodes == NULL) {
xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
"xmlNewTextWriter : out of memory!\n");
xmlFree(ret);
return NULL;
}
ret->nsstack = xmlListCreate(xmlFreeTextWriterNsStackEntry,
xmlCmpTextWriterNsStackEntry);
if (ret->nsstack == NULL) {
xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
"xmlNewTextWriter : out of memory!\n");
xmlListDelete(ret->nodes);
xmlFree(ret);
return NULL;
}
ret->out = out;
ret->ichar = xmlStrdup(BAD_CAST " ");
ret->qchar = '"';
if (!ret->ichar) {
xmlListDelete(ret->nodes);
xmlListDelete(ret->nsstack);
xmlFree(ret);
xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
"xmlNewTextWriter : out of memory!\n");
return NULL;
}
ret->doc = xmlNewDoc(NULL);
ret->no_doc_free = 0;
return ret;
}
/**
* xmlNewTextWriterFilename:
* @uri: the URI of the resource for the output
* @compression: compress the output?
*
* Create a new xmlNewTextWriter structure with @uri as output
*
* Returns the new xmlTextWriterPtr or NULL in case of error
*/
xmlTextWriterPtr
xmlNewTextWriterFilename(const char *uri, int compression)
{
xmlTextWriterPtr ret;
xmlOutputBufferPtr out;
out = xmlOutputBufferCreateFilename(uri, NULL, compression);
if (out == NULL) {
xmlWriterErrMsg(NULL, XML_IO_EIO,
"xmlNewTextWriterFilename : cannot open uri\n");
return NULL;
}
ret = xmlNewTextWriter(out);
if (ret == NULL) {
xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
"xmlNewTextWriterFilename : out of memory!\n");
xmlOutputBufferClose(out);
return NULL;
}
ret->indent = 0;
ret->doindent = 0;
return ret;
}
/**
* xmlNewTextWriterMemory:
* @buf: xmlBufferPtr
* @compression: compress the output?
*
* Create a new xmlNewTextWriter structure with @buf as output
* TODO: handle compression
*
* Returns the new xmlTextWriterPtr or NULL in case of error
*/
xmlTextWriterPtr
xmlNewTextWriterMemory(xmlBufferPtr buf, int compression ATTRIBUTE_UNUSED)
{
xmlTextWriterPtr ret;
xmlOutputBufferPtr out;
/*::todo handle compression */
out = xmlOutputBufferCreateBuffer(buf, NULL);
if (out == NULL) {
xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
"xmlNewTextWriterMemory : out of memory!\n");
return NULL;
}
ret = xmlNewTextWriter(out);
if (ret == NULL) {
xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
"xmlNewTextWriterMemory : out of memory!\n");
xmlOutputBufferClose(out);
return NULL;
}
return ret;
}
/**
* xmlNewTextWriterPushParser:
* @ctxt: xmlParserCtxtPtr to hold the new XML document tree
* @compression: compress the output?
*
* Create a new xmlNewTextWriter structure with @ctxt as output
* NOTE: the @ctxt context will be freed with the resulting writer
* (if the call succeeds).
* TODO: handle compression
*
* Returns the new xmlTextWriterPtr or NULL in case of error
*/
xmlTextWriterPtr
xmlNewTextWriterPushParser(xmlParserCtxtPtr ctxt,
int compression ATTRIBUTE_UNUSED)
{
xmlTextWriterPtr ret;
xmlOutputBufferPtr out;
if (ctxt == NULL) {
xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
"xmlNewTextWriterPushParser : invalid context!\n");
return NULL;
}
out = xmlOutputBufferCreateIO(xmlTextWriterWriteDocCallback,
xmlTextWriterCloseDocCallback,
(void *) ctxt, NULL);
if (out == NULL) {
xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
"xmlNewTextWriterPushParser : error at xmlOutputBufferCreateIO!\n");
return NULL;
}
ret = xmlNewTextWriter(out);
if (ret == NULL) {
xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
"xmlNewTextWriterPushParser : error at xmlNewTextWriter!\n");
xmlOutputBufferClose(out);
return NULL;
}
ret->ctxt = ctxt;
return ret;
}
/**
* xmlNewTextWriterDoc:
* @doc: address of a xmlDocPtr to hold the new XML document tree
* @compression: compress the output?
*
* Create a new xmlNewTextWriter structure with @*doc as output
*
* Returns the new xmlTextWriterPtr or NULL in case of error
*/
xmlTextWriterPtr
xmlNewTextWriterDoc(xmlDocPtr * doc, int compression)
{
xmlTextWriterPtr ret;
xmlSAXHandler saxHandler;
xmlParserCtxtPtr ctxt;
memset(&saxHandler, '\0', sizeof(saxHandler));
xmlSAX2InitDefaultSAXHandler(&saxHandler, 1);
saxHandler.startDocument = xmlTextWriterStartDocumentCallback;
saxHandler.startElement = xmlSAX2StartElement;
saxHandler.endElement = xmlSAX2EndElement;
ctxt = xmlCreatePushParserCtxt(&saxHandler, NULL, NULL, 0, NULL);
if (ctxt == NULL) {
xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
"xmlNewTextWriterDoc : error at xmlCreatePushParserCtxt!\n");
return NULL;
}
/*
* For some reason this seems to completely break if node names
* are interned.
*/
ctxt->dictNames = 0;
ctxt->myDoc = xmlNewDoc(BAD_CAST XML_DEFAULT_VERSION);
if (ctxt->myDoc == NULL) {
xmlFreeParserCtxt(ctxt);
xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
"xmlNewTextWriterDoc : error at xmlNewDoc!\n");
return NULL;
}
ret = xmlNewTextWriterPushParser(ctxt, compression);
if (ret == NULL) {
xmlFreeDoc(ctxt->myDoc);
xmlFreeParserCtxt(ctxt);
xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
"xmlNewTextWriterDoc : error at xmlNewTextWriterPushParser!\n");
return NULL;
}
xmlSetDocCompressMode(ctxt->myDoc, compression);
if (doc != NULL) {
*doc = ctxt->myDoc;
ret->no_doc_free = 1;
}
return ret;
}
/**
* xmlNewTextWriterTree:
* @doc: xmlDocPtr
* @node: xmlNodePtr or NULL for doc->children
* @compression: compress the output?
*
* Create a new xmlNewTextWriter structure with @doc as output
* starting at @node
*
* Returns the new xmlTextWriterPtr or NULL in case of error
*/
xmlTextWriterPtr
xmlNewTextWriterTree(xmlDocPtr doc, xmlNodePtr node, int compression)
{
xmlTextWriterPtr ret;
xmlSAXHandler saxHandler;
xmlParserCtxtPtr ctxt;
if (doc == NULL) {
xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
"xmlNewTextWriterTree : invalid document tree!\n");
return NULL;
}
memset(&saxHandler, '\0', sizeof(saxHandler));
xmlSAX2InitDefaultSAXHandler(&saxHandler, 1);
saxHandler.startDocument = xmlTextWriterStartDocumentCallback;
saxHandler.startElement = xmlSAX2StartElement;
saxHandler.endElement = xmlSAX2EndElement;
ctxt = xmlCreatePushParserCtxt(&saxHandler, NULL, NULL, 0, NULL);
if (ctxt == NULL) {
xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
"xmlNewTextWriterDoc : error at xmlCreatePushParserCtxt!\n");
return NULL;
}
/*
* For some reason this seems to completely break if node names
* are interned.
*/
ctxt->dictNames = 0;
ret = xmlNewTextWriterPushParser(ctxt, compression);
if (ret == NULL) {
xmlFreeParserCtxt(ctxt);
xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
"xmlNewTextWriterDoc : error at xmlNewTextWriterPushParser!\n");
return NULL;
}
ctxt->myDoc = doc;
ctxt->node = node;
ret->no_doc_free = 1;
xmlSetDocCompressMode(doc, compression);
return ret;
}
/**
* xmlFreeTextWriter:
* @writer: the xmlTextWriterPtr
*
* Deallocate all the resources associated to the writer
*/
void
xmlFreeTextWriter(xmlTextWriterPtr writer)
{
if (writer == NULL)
return;
if (writer->out != NULL)
xmlOutputBufferClose(writer->out);
if (writer->nodes != NULL)
xmlListDelete(writer->nodes);
if (writer->nsstack != NULL)
xmlListDelete(writer->nsstack);
if (writer->ctxt != NULL) {
if ((writer->ctxt->myDoc != NULL) && (writer->no_doc_free == 0)) {
xmlFreeDoc(writer->ctxt->myDoc);
writer->ctxt->myDoc = NULL;
}
xmlFreeParserCtxt(writer->ctxt);
}
if (writer->doc != NULL)
xmlFreeDoc(writer->doc);
if (writer->ichar != NULL)
xmlFree(writer->ichar);
xmlFree(writer);
}
/**
* xmlTextWriterStartDocument:
* @writer: the xmlTextWriterPtr
* @version: the xml version ("1.0") or NULL for default ("1.0")
* @encoding: the encoding or NULL for default
* @standalone: "yes" or "no" or NULL for default
*
* Start a new xml document
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterStartDocument(xmlTextWriterPtr writer, const char *version,
const char *encoding, const char *standalone)
{
int count;
int sum;
xmlLinkPtr lk;
xmlCharEncodingHandlerPtr encoder;
if ((writer == NULL) || (writer->out == NULL)) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterStartDocument : invalid writer!\n");
return -1;
}
lk = xmlListFront(writer->nodes);
if ((lk != NULL) && (xmlLinkGetData(lk) != NULL)) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterStartDocument : not allowed in this context!\n");
return -1;
}
encoder = NULL;
if (encoding != NULL) {
encoder = xmlFindCharEncodingHandler(encoding);
if (encoder == NULL) {
xmlWriterErrMsg(writer, XML_ERR_UNSUPPORTED_ENCODING,
"xmlTextWriterStartDocument : unsupported encoding\n");
return -1;
}
}
writer->out->encoder = encoder;
if (encoder != NULL) {
if (writer->out->conv == NULL) {
writer->out->conv = xmlBufCreateSize(4000);
}
xmlCharEncOutput(writer->out, 1);
if ((writer->doc != NULL) && (writer->doc->encoding == NULL))
writer->doc->encoding = xmlStrdup((xmlChar *)writer->out->encoder->name);
} else
writer->out->conv = NULL;
sum = 0;
count = xmlOutputBufferWriteString(writer->out, "<?xml version=");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
if (version != 0)
count = xmlOutputBufferWriteString(writer->out, version);
else
count = xmlOutputBufferWriteString(writer->out, "1.0");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
if (writer->out->encoder != 0) {
count = xmlOutputBufferWriteString(writer->out, " encoding=");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
count =
xmlOutputBufferWriteString(writer->out,
writer->out->encoder->name);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
}
if (standalone != 0) {
count = xmlOutputBufferWriteString(writer->out, " standalone=");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWriteString(writer->out, standalone);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
}
count = xmlOutputBufferWriteString(writer->out, "?>\n");
if (count < 0)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterEndDocument:
* @writer: the xmlTextWriterPtr
*
* End an xml document. All open elements are closed, and
* the content is flushed to the output.
*
* Returns the bytes written or -1 in case of error
*/
int
xmlTextWriterEndDocument(xmlTextWriterPtr writer)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterEndDocument : invalid writer!\n");
return -1;
}
sum = 0;
while ((lk = xmlListFront(writer->nodes)) != NULL) {
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p == 0)
break;
switch (p->state) {
case XML_TEXTWRITER_NAME:
case XML_TEXTWRITER_ATTRIBUTE:
case XML_TEXTWRITER_TEXT:
count = xmlTextWriterEndElement(writer);
if (count < 0)
return -1;
sum += count;
break;
case XML_TEXTWRITER_PI:
case XML_TEXTWRITER_PI_TEXT:
count = xmlTextWriterEndPI(writer);
if (count < 0)
return -1;
sum += count;
break;
case XML_TEXTWRITER_CDATA:
count = xmlTextWriterEndCDATA(writer);
if (count < 0)
return -1;
sum += count;
break;
case XML_TEXTWRITER_DTD:
case XML_TEXTWRITER_DTD_TEXT:
case XML_TEXTWRITER_DTD_ELEM:
case XML_TEXTWRITER_DTD_ELEM_TEXT:
case XML_TEXTWRITER_DTD_ATTL:
case XML_TEXTWRITER_DTD_ATTL_TEXT:
case XML_TEXTWRITER_DTD_ENTY:
case XML_TEXTWRITER_DTD_ENTY_TEXT:
case XML_TEXTWRITER_DTD_PENT:
count = xmlTextWriterEndDTD(writer);
if (count < 0)
return -1;
sum += count;
break;
case XML_TEXTWRITER_COMMENT:
count = xmlTextWriterEndComment(writer);
if (count < 0)
return -1;
sum += count;
break;
default:
break;
}
}
if (!writer->indent) {
count = xmlOutputBufferWriteString(writer->out, "\n");
if (count < 0)
return -1;
sum += count;
}
count = xmlTextWriterFlush(writer);
if (count < 0)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterStartComment:
* @writer: the xmlTextWriterPtr
*
* Start an xml comment.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterStartComment(xmlTextWriterPtr writer)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterStartComment : invalid writer!\n");
return -1;
}
sum = 0;
lk = xmlListFront(writer->nodes);
if (lk != 0) {
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p != 0) {
switch (p->state) {
case XML_TEXTWRITER_TEXT:
case XML_TEXTWRITER_NONE:
break;
case XML_TEXTWRITER_NAME:
/* Output namespace declarations */
count = xmlTextWriterOutputNSDecl(writer);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWriteString(writer->out, ">");
if (count < 0)
return -1;
sum += count;
if (writer->indent) {
count =
xmlOutputBufferWriteString(writer->out, "\n");
if (count < 0)
return -1;
sum += count;
}
p->state = XML_TEXTWRITER_TEXT;
break;
default:
return -1;
}
}
}
p = (xmlTextWriterStackEntry *)
xmlMalloc(sizeof(xmlTextWriterStackEntry));
if (p == 0) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartElement : out of memory!\n");
return -1;
}
p->name = NULL;
p->state = XML_TEXTWRITER_COMMENT;
xmlListPushFront(writer->nodes, p);
if (writer->indent) {
count = xmlTextWriterWriteIndent(writer);
if (count < 0)
return -1;
sum += count;
}
count = xmlOutputBufferWriteString(writer->out, "<!--");
if (count < 0)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterEndComment:
* @writer: the xmlTextWriterPtr
*
* End the current xml comment.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterEndComment(xmlTextWriterPtr writer)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterEndComment : invalid writer!\n");
return -1;
}
lk = xmlListFront(writer->nodes);
if (lk == 0) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterEndComment : not allowed in this context!\n");
return -1;
}
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p == 0)
return -1;
sum = 0;
switch (p->state) {
case XML_TEXTWRITER_COMMENT:
count = xmlOutputBufferWriteString(writer->out, "-->");
if (count < 0)
return -1;
sum += count;
break;
default:
return -1;
}
if (writer->indent) {
count = xmlOutputBufferWriteString(writer->out, "\n");
if (count < 0)
return -1;
sum += count;
}
xmlListPopFront(writer->nodes);
return sum;
}
/**
* xmlTextWriterWriteFormatComment:
* @writer: the xmlTextWriterPtr
* @format: format string (see printf)
* @...: extra parameters for the format
*
* Write an xml comment.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteFormatComment(xmlTextWriterPtr writer,
const char *format, ...)
{
int rc;
va_list ap;
va_start(ap, format);
rc = xmlTextWriterWriteVFormatComment(writer, format, ap);
va_end(ap);
return rc;
}
/**
* xmlTextWriterWriteVFormatComment:
* @writer: the xmlTextWriterPtr
* @format: format string (see printf)
* @argptr: pointer to the first member of the variable argument list.
*
* Write an xml comment.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteVFormatComment(xmlTextWriterPtr writer,
const char *format, va_list argptr)
{
int rc;
xmlChar *buf;
if (writer == NULL) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterWriteVFormatComment : invalid writer!\n");
return -1;
}
buf = xmlTextWriterVSprintf(format, argptr);
if (buf == NULL)
return -1;
rc = xmlTextWriterWriteComment(writer, buf);
xmlFree(buf);
return rc;
}
/**
* xmlTextWriterWriteComment:
* @writer: the xmlTextWriterPtr
* @content: comment string
*
* Write an xml comment.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteComment(xmlTextWriterPtr writer, const xmlChar * content)
{
int count;
int sum;
sum = 0;
count = xmlTextWriterStartComment(writer);
if (count < 0)
return -1;
sum += count;
count = xmlTextWriterWriteString(writer, content);
if (count < 0)
return -1;
sum += count;
count = xmlTextWriterEndComment(writer);
if (count < 0)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterStartElement:
* @writer: the xmlTextWriterPtr
* @name: element name
*
* Start an xml element.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterStartElement(xmlTextWriterPtr writer, const xmlChar * name)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if ((writer == NULL) || (name == NULL) || (*name == '\0'))
return -1;
sum = 0;
lk = xmlListFront(writer->nodes);
if (lk != 0) {
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p != 0) {
switch (p->state) {
case XML_TEXTWRITER_PI:
case XML_TEXTWRITER_PI_TEXT:
return -1;
case XML_TEXTWRITER_NONE:
break;
case XML_TEXTWRITER_ATTRIBUTE:
count = xmlTextWriterEndAttribute(writer);
if (count < 0)
return -1;
sum += count;
/* fallthrough */
case XML_TEXTWRITER_NAME:
/* Output namespace declarations */
count = xmlTextWriterOutputNSDecl(writer);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWriteString(writer->out, ">");
if (count < 0)
return -1;
sum += count;
if (writer->indent)
count =
xmlOutputBufferWriteString(writer->out, "\n");
p->state = XML_TEXTWRITER_TEXT;
break;
default:
break;
}
}
}
p = (xmlTextWriterStackEntry *)
xmlMalloc(sizeof(xmlTextWriterStackEntry));
if (p == 0) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartElement : out of memory!\n");
return -1;
}
p->name = xmlStrdup(name);
if (p->name == 0) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartElement : out of memory!\n");
xmlFree(p);
return -1;
}
p->state = XML_TEXTWRITER_NAME;
xmlListPushFront(writer->nodes, p);
if (writer->indent) {
count = xmlTextWriterWriteIndent(writer);
sum += count;
}
count = xmlOutputBufferWriteString(writer->out, "<");
if (count < 0)
return -1;
sum += count;
count =
xmlOutputBufferWriteString(writer->out, (const char *) p->name);
if (count < 0)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterStartElementNS:
* @writer: the xmlTextWriterPtr
* @prefix: namespace prefix or NULL
* @name: element local name
* @namespaceURI: namespace URI or NULL
*
* Start an xml element with namespace support.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterStartElementNS(xmlTextWriterPtr writer,
const xmlChar * prefix, const xmlChar * name,
const xmlChar * namespaceURI)
{
int count;
int sum;
xmlChar *buf;
if ((writer == NULL) || (name == NULL) || (*name == '\0'))
return -1;
buf = NULL;
if (prefix != 0) {
buf = xmlStrdup(prefix);
buf = xmlStrcat(buf, BAD_CAST ":");
}
buf = xmlStrcat(buf, name);
sum = 0;
count = xmlTextWriterStartElement(writer, buf);
xmlFree(buf);
if (count < 0)
return -1;
sum += count;
if (namespaceURI != 0) {
xmlTextWriterNsStackEntry *p = (xmlTextWriterNsStackEntry *)
xmlMalloc(sizeof(xmlTextWriterNsStackEntry));
if (p == 0) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartElementNS : out of memory!\n");
return -1;
}
buf = xmlStrdup(BAD_CAST "xmlns");
if (prefix != 0) {
buf = xmlStrcat(buf, BAD_CAST ":");
buf = xmlStrcat(buf, prefix);
}
p->prefix = buf;
p->uri = xmlStrdup(namespaceURI);
if (p->uri == 0) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartElementNS : out of memory!\n");
xmlFree(p);
return -1;
}
p->elem = xmlListFront(writer->nodes);
xmlListPushFront(writer->nsstack, p);
}
return sum;
}
/**
* xmlTextWriterEndElement:
* @writer: the xmlTextWriterPtr
*
* End the current xml element.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterEndElement(xmlTextWriterPtr writer)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL)
return -1;
lk = xmlListFront(writer->nodes);
if (lk == 0) {
xmlListDelete(writer->nsstack);
writer->nsstack = NULL;
return -1;
}
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p == 0) {
xmlListDelete(writer->nsstack);
writer->nsstack = NULL;
return -1;
}
sum = 0;
switch (p->state) {
case XML_TEXTWRITER_ATTRIBUTE:
count = xmlTextWriterEndAttribute(writer);
if (count < 0) {
xmlListDelete(writer->nsstack);
writer->nsstack = NULL;
return -1;
}
sum += count;
/* fallthrough */
case XML_TEXTWRITER_NAME:
/* Output namespace declarations */
count = xmlTextWriterOutputNSDecl(writer);
if (count < 0)
return -1;
sum += count;
if (writer->indent) /* next element needs indent */
writer->doindent = 1;
count = xmlOutputBufferWriteString(writer->out, "/>");
if (count < 0)
return -1;
sum += count;
break;
case XML_TEXTWRITER_TEXT:
if ((writer->indent) && (writer->doindent)) {
count = xmlTextWriterWriteIndent(writer);
sum += count;
writer->doindent = 1;
} else
writer->doindent = 1;
count = xmlOutputBufferWriteString(writer->out, "</");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWriteString(writer->out,
(const char *) p->name);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWriteString(writer->out, ">");
if (count < 0)
return -1;
sum += count;
break;
default:
return -1;
}
if (writer->indent) {
count = xmlOutputBufferWriteString(writer->out, "\n");
sum += count;
}
xmlListPopFront(writer->nodes);
return sum;
}
/**
* xmlTextWriterFullEndElement:
* @writer: the xmlTextWriterPtr
*
* End the current xml element. Writes an end tag even if the element is empty
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterFullEndElement(xmlTextWriterPtr writer)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL)
return -1;
lk = xmlListFront(writer->nodes);
if (lk == 0)
return -1;
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p == 0)
return -1;
sum = 0;
switch (p->state) {
case XML_TEXTWRITER_ATTRIBUTE:
count = xmlTextWriterEndAttribute(writer);
if (count < 0)
return -1;
sum += count;
/* fallthrough */
case XML_TEXTWRITER_NAME:
/* Output namespace declarations */
count = xmlTextWriterOutputNSDecl(writer);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWriteString(writer->out, ">");
if (count < 0)
return -1;
sum += count;
if (writer->indent)
writer->doindent = 0;
/* fallthrough */
case XML_TEXTWRITER_TEXT:
if ((writer->indent) && (writer->doindent)) {
count = xmlTextWriterWriteIndent(writer);
sum += count;
writer->doindent = 1;
} else
writer->doindent = 1;
count = xmlOutputBufferWriteString(writer->out, "</");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWriteString(writer->out,
(const char *) p->name);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWriteString(writer->out, ">");
if (count < 0)
return -1;
sum += count;
break;
default:
return -1;
}
if (writer->indent) {
count = xmlOutputBufferWriteString(writer->out, "\n");
sum += count;
}
xmlListPopFront(writer->nodes);
return sum;
}
/**
* xmlTextWriterWriteFormatRaw:
* @writer: the xmlTextWriterPtr
* @format: format string (see printf)
* @...: extra parameters for the format
*
* Write a formatted raw xml text.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteFormatRaw(xmlTextWriterPtr writer, const char *format,
...)
{
int rc;
va_list ap;
va_start(ap, format);
rc = xmlTextWriterWriteVFormatRaw(writer, format, ap);
va_end(ap);
return rc;
}
/**
* xmlTextWriterWriteVFormatRaw:
* @writer: the xmlTextWriterPtr
* @format: format string (see printf)
* @argptr: pointer to the first member of the variable argument list.
*
* Write a formatted raw xml text.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteVFormatRaw(xmlTextWriterPtr writer, const char *format,
va_list argptr)
{
int rc;
xmlChar *buf;
if (writer == NULL)
return -1;
buf = xmlTextWriterVSprintf(format, argptr);
if (buf == NULL)
return -1;
rc = xmlTextWriterWriteRaw(writer, buf);
xmlFree(buf);
return rc;
}
/**
* xmlTextWriterWriteRawLen:
* @writer: the xmlTextWriterPtr
* @content: text string
* @len: length of the text string
*
* Write an xml text.
* TODO: what about entities and special chars??
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteRawLen(xmlTextWriterPtr writer, const xmlChar * content,
int len)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterWriteRawLen : invalid writer!\n");
return -1;
}
if ((content == NULL) || (len < 0)) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterWriteRawLen : invalid content!\n");
return -1;
}
sum = 0;
lk = xmlListFront(writer->nodes);
if (lk != 0) {
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
count = xmlTextWriterHandleStateDependencies(writer, p);
if (count < 0)
return -1;
sum += count;
}
if (writer->indent)
writer->doindent = 0;
if (content != NULL) {
count =
xmlOutputBufferWrite(writer->out, len, (const char *) content);
if (count < 0)
return -1;
sum += count;
}
return sum;
}
/**
* xmlTextWriterWriteRaw:
* @writer: the xmlTextWriterPtr
* @content: text string
*
* Write a raw xml text.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteRaw(xmlTextWriterPtr writer, const xmlChar * content)
{
return xmlTextWriterWriteRawLen(writer, content, xmlStrlen(content));
}
/**
* xmlTextWriterWriteFormatString:
* @writer: the xmlTextWriterPtr
* @format: format string (see printf)
* @...: extra parameters for the format
*
* Write a formatted xml text.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteFormatString(xmlTextWriterPtr writer, const char *format,
...)
{
int rc;
va_list ap;
if ((writer == NULL) || (format == NULL))
return -1;
va_start(ap, format);
rc = xmlTextWriterWriteVFormatString(writer, format, ap);
va_end(ap);
return rc;
}
/**
* xmlTextWriterWriteVFormatString:
* @writer: the xmlTextWriterPtr
* @format: format string (see printf)
* @argptr: pointer to the first member of the variable argument list.
*
* Write a formatted xml text.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteVFormatString(xmlTextWriterPtr writer,
const char *format, va_list argptr)
{
int rc;
xmlChar *buf;
if ((writer == NULL) || (format == NULL))
return -1;
buf = xmlTextWriterVSprintf(format, argptr);
if (buf == NULL)
return -1;
rc = xmlTextWriterWriteString(writer, buf);
xmlFree(buf);
return rc;
}
/**
* xmlTextWriterWriteString:
* @writer: the xmlTextWriterPtr
* @content: text string
*
* Write an xml text.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteString(xmlTextWriterPtr writer, const xmlChar * content)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
xmlChar *buf;
if ((writer == NULL) || (content == NULL))
return -1;
sum = 0;
buf = (xmlChar *) content;
lk = xmlListFront(writer->nodes);
if (lk != 0) {
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p != 0) {
switch (p->state) {
case XML_TEXTWRITER_NAME:
case XML_TEXTWRITER_TEXT:
#if 0
buf = NULL;
xmlOutputBufferWriteEscape(writer->out, content, NULL);
#endif
buf = xmlEncodeSpecialChars(NULL, content);
break;
case XML_TEXTWRITER_ATTRIBUTE:
buf = NULL;
xmlBufAttrSerializeTxtContent(writer->out->buffer,
writer->doc, NULL, content);
break;
default:
break;
}
}
}
if (buf != NULL) {
count = xmlTextWriterWriteRaw(writer, buf);
if (buf != content) /* buf was allocated by us, so free it */
xmlFree(buf);
if (count < 0)
return -1;
sum += count;
}
return sum;
}
/**
* xmlOutputBufferWriteBase64:
* @out: the xmlOutputBufferPtr
* @data: binary data
* @len: the number of bytes to encode
*
* Write base64 encoded data to an xmlOutputBuffer.
* Adapted from John Walker's base64.c (http://www.fourmilab.ch/).
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
static int
xmlOutputBufferWriteBase64(xmlOutputBufferPtr out, int len,
const unsigned char *data)
{
static const unsigned char dtable[64] =
{'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9','+','/'};
int i;
int linelen;
int count;
int sum;
if ((out == NULL) || (len < 0) || (data == NULL))
return(-1);
linelen = 0;
sum = 0;
i = 0;
while (1) {
unsigned char igroup[3];
unsigned char ogroup[4];
int c;
int n;
igroup[0] = igroup[1] = igroup[2] = 0;
for (n = 0; n < 3 && i < len; n++, i++) {
c = data[i];
igroup[n] = (unsigned char) c;
}
if (n > 0) {
ogroup[0] = dtable[igroup[0] >> 2];
ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
ogroup[2] =
dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
ogroup[3] = dtable[igroup[2] & 0x3F];
if (n < 3) {
ogroup[3] = '=';
if (n < 2) {
ogroup[2] = '=';
}
}
if (linelen >= B64LINELEN) {
count = xmlOutputBufferWrite(out, 2, B64CRLF);
if (count == -1)
return -1;
sum += count;
linelen = 0;
}
count = xmlOutputBufferWrite(out, 4, (const char *) ogroup);
if (count == -1)
return -1;
sum += count;
linelen += 4;
}
if (i >= len)
break;
}
return sum;
}
/**
* xmlTextWriterWriteBase64:
* @writer: the xmlTextWriterPtr
* @data: binary data
* @start: the position within the data of the first byte to encode
* @len: the number of bytes to encode
*
* Write an base64 encoded xml text.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteBase64(xmlTextWriterPtr writer, const char *data,
int start, int len)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if ((writer == NULL) || (data == NULL) || (start < 0) || (len < 0))
return -1;
sum = 0;
lk = xmlListFront(writer->nodes);
if (lk != 0) {
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p != 0) {
count = xmlTextWriterHandleStateDependencies(writer, p);
if (count < 0)
return -1;
sum += count;
}
}
if (writer->indent)
writer->doindent = 0;
count =
xmlOutputBufferWriteBase64(writer->out, len,
(unsigned char *) data + start);
if (count < 0)
return -1;
sum += count;
return sum;
}
/**
* xmlOutputBufferWriteBinHex:
* @out: the xmlOutputBufferPtr
* @data: binary data
* @len: the number of bytes to encode
*
* Write hqx encoded data to an xmlOutputBuffer.
* ::todo
*
* Returns the bytes written (may be 0 because of buffering)
* or -1 in case of error
*/
static int
xmlOutputBufferWriteBinHex(xmlOutputBufferPtr out,
int len, const unsigned char *data)
{
int count;
int sum;
static const char hex[16] =
{'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int i;
if ((out == NULL) || (data == NULL) || (len < 0)) {
return -1;
}
sum = 0;
for (i = 0; i < len; i++) {
count =
xmlOutputBufferWrite(out, 1,
(const char *) &hex[data[i] >> 4]);
if (count == -1)
return -1;
sum += count;
count =
xmlOutputBufferWrite(out, 1,
(const char *) &hex[data[i] & 0xF]);
if (count == -1)
return -1;
sum += count;
}
return sum;
}
/**
* xmlTextWriterWriteBinHex:
* @writer: the xmlTextWriterPtr
* @data: binary data
* @start: the position within the data of the first byte to encode
* @len: the number of bytes to encode
*
* Write a BinHex encoded xml text.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteBinHex(xmlTextWriterPtr writer, const char *data,
int start, int len)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if ((writer == NULL) || (data == NULL) || (start < 0) || (len < 0))
return -1;
sum = 0;
lk = xmlListFront(writer->nodes);
if (lk != 0) {
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p != 0) {
count = xmlTextWriterHandleStateDependencies(writer, p);
if (count < 0)
return -1;
sum += count;
}
}
if (writer->indent)
writer->doindent = 0;
count =
xmlOutputBufferWriteBinHex(writer->out, len,
(unsigned char *) data + start);
if (count < 0)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterStartAttribute:
* @writer: the xmlTextWriterPtr
* @name: element name
*
* Start an xml attribute.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterStartAttribute(xmlTextWriterPtr writer, const xmlChar * name)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if ((writer == NULL) || (name == NULL) || (*name == '\0'))
return -1;
sum = 0;
lk = xmlListFront(writer->nodes);
if (lk == 0)
return -1;
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p == 0)
return -1;
switch (p->state) {
case XML_TEXTWRITER_ATTRIBUTE:
count = xmlTextWriterEndAttribute(writer);
if (count < 0)
return -1;
sum += count;
/* fallthrough */
case XML_TEXTWRITER_NAME:
count = xmlOutputBufferWriteString(writer->out, " ");
if (count < 0)
return -1;
sum += count;
count =
xmlOutputBufferWriteString(writer->out,
(const char *) name);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWriteString(writer->out, "=");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
p->state = XML_TEXTWRITER_ATTRIBUTE;
break;
default:
return -1;
}
return sum;
}
/**
* xmlTextWriterStartAttributeNS:
* @writer: the xmlTextWriterPtr
* @prefix: namespace prefix or NULL
* @name: element local name
* @namespaceURI: namespace URI or NULL
*
* Start an xml attribute with namespace support.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterStartAttributeNS(xmlTextWriterPtr writer,
const xmlChar * prefix, const xmlChar * name,
const xmlChar * namespaceURI)
{
int count;
int sum;
xmlChar *buf;
xmlTextWriterNsStackEntry *p;
if ((writer == NULL) || (name == NULL) || (*name == '\0'))
return -1;
/* Handle namespace first in case of error */
if (namespaceURI != 0) {
xmlTextWriterNsStackEntry nsentry, *curns;
buf = xmlStrdup(BAD_CAST "xmlns");
if (prefix != 0) {
buf = xmlStrcat(buf, BAD_CAST ":");
buf = xmlStrcat(buf, prefix);
}
nsentry.prefix = buf;
nsentry.uri = (xmlChar *)namespaceURI;
nsentry.elem = xmlListFront(writer->nodes);
curns = (xmlTextWriterNsStackEntry *)xmlListSearch(writer->nsstack,
(void *)&nsentry);
if ((curns != NULL)) {
xmlFree(buf);
if (xmlStrcmp(curns->uri, namespaceURI) == 0) {
/* Namespace already defined on element skip */
buf = NULL;
} else {
/* Prefix mismatch so error out */
return -1;
}
}
/* Do not add namespace decl to list - it is already there */
if (buf != NULL) {
p = (xmlTextWriterNsStackEntry *)
xmlMalloc(sizeof(xmlTextWriterNsStackEntry));
if (p == 0) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartAttributeNS : out of memory!\n");
return -1;
}
p->prefix = buf;
p->uri = xmlStrdup(namespaceURI);
if (p->uri == 0) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartAttributeNS : out of memory!\n");
xmlFree(p);
return -1;
}
p->elem = xmlListFront(writer->nodes);
xmlListPushFront(writer->nsstack, p);
}
}
buf = NULL;
if (prefix != 0) {
buf = xmlStrdup(prefix);
buf = xmlStrcat(buf, BAD_CAST ":");
}
buf = xmlStrcat(buf, name);
sum = 0;
count = xmlTextWriterStartAttribute(writer, buf);
xmlFree(buf);
if (count < 0)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterEndAttribute:
* @writer: the xmlTextWriterPtr
*
* End the current xml element.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterEndAttribute(xmlTextWriterPtr writer)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL)
return -1;
lk = xmlListFront(writer->nodes);
if (lk == 0) {
return -1;
}
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p == 0) {
return -1;
}
sum = 0;
switch (p->state) {
case XML_TEXTWRITER_ATTRIBUTE:
p->state = XML_TEXTWRITER_NAME;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0) {
return -1;
}
sum += count;
break;
default:
return -1;
}
return sum;
}
/**
* xmlTextWriterWriteFormatAttribute:
* @writer: the xmlTextWriterPtr
* @name: attribute name
* @format: format string (see printf)
* @...: extra parameters for the format
*
* Write a formatted xml attribute.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteFormatAttribute(xmlTextWriterPtr writer,
const xmlChar * name, const char *format,
...)
{
int rc;
va_list ap;
va_start(ap, format);
rc = xmlTextWriterWriteVFormatAttribute(writer, name, format, ap);
va_end(ap);
return rc;
}
/**
* xmlTextWriterWriteVFormatAttribute:
* @writer: the xmlTextWriterPtr
* @name: attribute name
* @format: format string (see printf)
* @argptr: pointer to the first member of the variable argument list.
*
* Write a formatted xml attribute.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteVFormatAttribute(xmlTextWriterPtr writer,
const xmlChar * name,
const char *format, va_list argptr)
{
int rc;
xmlChar *buf;
if (writer == NULL)
return -1;
buf = xmlTextWriterVSprintf(format, argptr);
if (buf == NULL)
return -1;
rc = xmlTextWriterWriteAttribute(writer, name, buf);
xmlFree(buf);
return rc;
}
/**
* xmlTextWriterWriteAttribute:
* @writer: the xmlTextWriterPtr
* @name: attribute name
* @content: attribute content
*
* Write an xml attribute.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteAttribute(xmlTextWriterPtr writer, const xmlChar * name,
const xmlChar * content)
{
int count;
int sum;
sum = 0;
count = xmlTextWriterStartAttribute(writer, name);
if (count < 0)
return -1;
sum += count;
count = xmlTextWriterWriteString(writer, content);
if (count < 0)
return -1;
sum += count;
count = xmlTextWriterEndAttribute(writer);
if (count < 0)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterWriteFormatAttributeNS:
* @writer: the xmlTextWriterPtr
* @prefix: namespace prefix
* @name: attribute local name
* @namespaceURI: namespace URI
* @format: format string (see printf)
* @...: extra parameters for the format
*
* Write a formatted xml attribute.with namespace support
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteFormatAttributeNS(xmlTextWriterPtr writer,
const xmlChar * prefix,
const xmlChar * name,
const xmlChar * namespaceURI,
const char *format, ...)
{
int rc;
va_list ap;
va_start(ap, format);
rc = xmlTextWriterWriteVFormatAttributeNS(writer, prefix, name,
namespaceURI, format, ap);
va_end(ap);
return rc;
}
/**
* xmlTextWriterWriteVFormatAttributeNS:
* @writer: the xmlTextWriterPtr
* @prefix: namespace prefix
* @name: attribute local name
* @namespaceURI: namespace URI
* @format: format string (see printf)
* @argptr: pointer to the first member of the variable argument list.
*
* Write a formatted xml attribute.with namespace support
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteVFormatAttributeNS(xmlTextWriterPtr writer,
const xmlChar * prefix,
const xmlChar * name,
const xmlChar * namespaceURI,
const char *format, va_list argptr)
{
int rc;
xmlChar *buf;
if (writer == NULL)
return -1;
buf = xmlTextWriterVSprintf(format, argptr);
if (buf == NULL)
return -1;
rc = xmlTextWriterWriteAttributeNS(writer, prefix, name, namespaceURI,
buf);
xmlFree(buf);
return rc;
}
/**
* xmlTextWriterWriteAttributeNS:
* @writer: the xmlTextWriterPtr
* @prefix: namespace prefix
* @name: attribute local name
* @namespaceURI: namespace URI
* @content: attribute content
*
* Write an xml attribute.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteAttributeNS(xmlTextWriterPtr writer,
const xmlChar * prefix, const xmlChar * name,
const xmlChar * namespaceURI,
const xmlChar * content)
{
int count;
int sum;
if ((writer == NULL) || (name == NULL) || (*name == '\0'))
return -1;
sum = 0;
count = xmlTextWriterStartAttributeNS(writer, prefix, name, namespaceURI);
if (count < 0)
return -1;
sum += count;
count = xmlTextWriterWriteString(writer, content);
if (count < 0)
return -1;
sum += count;
count = xmlTextWriterEndAttribute(writer);
if (count < 0)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterWriteFormatElement:
* @writer: the xmlTextWriterPtr
* @name: element name
* @format: format string (see printf)
* @...: extra parameters for the format
*
* Write a formatted xml element.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteFormatElement(xmlTextWriterPtr writer,
const xmlChar * name, const char *format,
...)
{
int rc;
va_list ap;
va_start(ap, format);
rc = xmlTextWriterWriteVFormatElement(writer, name, format, ap);
va_end(ap);
return rc;
}
/**
* xmlTextWriterWriteVFormatElement:
* @writer: the xmlTextWriterPtr
* @name: element name
* @format: format string (see printf)
* @argptr: pointer to the first member of the variable argument list.
*
* Write a formatted xml element.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteVFormatElement(xmlTextWriterPtr writer,
const xmlChar * name, const char *format,
va_list argptr)
{
int rc;
xmlChar *buf;
if (writer == NULL)
return -1;
buf = xmlTextWriterVSprintf(format, argptr);
if (buf == NULL)
return -1;
rc = xmlTextWriterWriteElement(writer, name, buf);
xmlFree(buf);
return rc;
}
/**
* xmlTextWriterWriteElement:
* @writer: the xmlTextWriterPtr
* @name: element name
* @content: element content
*
* Write an xml element.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteElement(xmlTextWriterPtr writer, const xmlChar * name,
const xmlChar * content)
{
int count;
int sum;
sum = 0;
count = xmlTextWriterStartElement(writer, name);
if (count == -1)
return -1;
sum += count;
if (content != NULL) {
count = xmlTextWriterWriteString(writer, content);
if (count == -1)
return -1;
sum += count;
}
count = xmlTextWriterEndElement(writer);
if (count == -1)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterWriteFormatElementNS:
* @writer: the xmlTextWriterPtr
* @prefix: namespace prefix
* @name: element local name
* @namespaceURI: namespace URI
* @format: format string (see printf)
* @...: extra parameters for the format
*
* Write a formatted xml element with namespace support.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteFormatElementNS(xmlTextWriterPtr writer,
const xmlChar * prefix,
const xmlChar * name,
const xmlChar * namespaceURI,
const char *format, ...)
{
int rc;
va_list ap;
va_start(ap, format);
rc = xmlTextWriterWriteVFormatElementNS(writer, prefix, name,
namespaceURI, format, ap);
va_end(ap);
return rc;
}
/**
* xmlTextWriterWriteVFormatElementNS:
* @writer: the xmlTextWriterPtr
* @prefix: namespace prefix
* @name: element local name
* @namespaceURI: namespace URI
* @format: format string (see printf)
* @argptr: pointer to the first member of the variable argument list.
*
* Write a formatted xml element with namespace support.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteVFormatElementNS(xmlTextWriterPtr writer,
const xmlChar * prefix,
const xmlChar * name,
const xmlChar * namespaceURI,
const char *format, va_list argptr)
{
int rc;
xmlChar *buf;
if (writer == NULL)
return -1;
buf = xmlTextWriterVSprintf(format, argptr);
if (buf == NULL)
return -1;
rc = xmlTextWriterWriteElementNS(writer, prefix, name, namespaceURI,
buf);
xmlFree(buf);
return rc;
}
/**
* xmlTextWriterWriteElementNS:
* @writer: the xmlTextWriterPtr
* @prefix: namespace prefix
* @name: element local name
* @namespaceURI: namespace URI
* @content: element content
*
* Write an xml element with namespace support.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteElementNS(xmlTextWriterPtr writer,
const xmlChar * prefix, const xmlChar * name,
const xmlChar * namespaceURI,
const xmlChar * content)
{
int count;
int sum;
if ((writer == NULL) || (name == NULL) || (*name == '\0'))
return -1;
sum = 0;
count =
xmlTextWriterStartElementNS(writer, prefix, name, namespaceURI);
if (count < 0)
return -1;
sum += count;
count = xmlTextWriterWriteString(writer, content);
if (count == -1)
return -1;
sum += count;
count = xmlTextWriterEndElement(writer);
if (count == -1)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterStartPI:
* @writer: the xmlTextWriterPtr
* @target: PI target
*
* Start an xml PI.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterStartPI(xmlTextWriterPtr writer, const xmlChar * target)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if ((writer == NULL) || (target == NULL) || (*target == '\0'))
return -1;
if (xmlStrcasecmp(target, (const xmlChar *) "xml") == 0) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterStartPI : target name [Xx][Mm][Ll] is reserved for xml standardization!\n");
return -1;
}
sum = 0;
lk = xmlListFront(writer->nodes);
if (lk != 0) {
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p != 0) {
switch (p->state) {
case XML_TEXTWRITER_ATTRIBUTE:
count = xmlTextWriterEndAttribute(writer);
if (count < 0)
return -1;
sum += count;
/* fallthrough */
case XML_TEXTWRITER_NAME:
/* Output namespace declarations */
count = xmlTextWriterOutputNSDecl(writer);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWriteString(writer->out, ">");
if (count < 0)
return -1;
sum += count;
p->state = XML_TEXTWRITER_TEXT;
break;
case XML_TEXTWRITER_NONE:
case XML_TEXTWRITER_TEXT:
case XML_TEXTWRITER_DTD:
break;
case XML_TEXTWRITER_PI:
case XML_TEXTWRITER_PI_TEXT:
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterStartPI : nested PI!\n");
return -1;
default:
return -1;
}
}
}
p = (xmlTextWriterStackEntry *)
xmlMalloc(sizeof(xmlTextWriterStackEntry));
if (p == 0) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartPI : out of memory!\n");
return -1;
}
p->name = xmlStrdup(target);
if (p->name == 0) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartPI : out of memory!\n");
xmlFree(p);
return -1;
}
p->state = XML_TEXTWRITER_PI;
xmlListPushFront(writer->nodes, p);
count = xmlOutputBufferWriteString(writer->out, "<?");
if (count < 0)
return -1;
sum += count;
count =
xmlOutputBufferWriteString(writer->out, (const char *) p->name);
if (count < 0)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterEndPI:
* @writer: the xmlTextWriterPtr
*
* End the current xml PI.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterEndPI(xmlTextWriterPtr writer)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL)
return -1;
lk = xmlListFront(writer->nodes);
if (lk == 0)
return 0;
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p == 0)
return 0;
sum = 0;
switch (p->state) {
case XML_TEXTWRITER_PI:
case XML_TEXTWRITER_PI_TEXT:
count = xmlOutputBufferWriteString(writer->out, "?>");
if (count < 0)
return -1;
sum += count;
break;
default:
return -1;
}
if (writer->indent) {
count = xmlOutputBufferWriteString(writer->out, "\n");
if (count < 0)
return -1;
sum += count;
}
xmlListPopFront(writer->nodes);
return sum;
}
/**
* xmlTextWriterWriteFormatPI:
* @writer: the xmlTextWriterPtr
* @target: PI target
* @format: format string (see printf)
* @...: extra parameters for the format
*
* Write a formatted PI.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteFormatPI(xmlTextWriterPtr writer, const xmlChar * target,
const char *format, ...)
{
int rc;
va_list ap;
va_start(ap, format);
rc = xmlTextWriterWriteVFormatPI(writer, target, format, ap);
va_end(ap);
return rc;
}
/**
* xmlTextWriterWriteVFormatPI:
* @writer: the xmlTextWriterPtr
* @target: PI target
* @format: format string (see printf)
* @argptr: pointer to the first member of the variable argument list.
*
* Write a formatted xml PI.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteVFormatPI(xmlTextWriterPtr writer,
const xmlChar * target, const char *format,
va_list argptr)
{
int rc;
xmlChar *buf;
if (writer == NULL)
return -1;
buf = xmlTextWriterVSprintf(format, argptr);
if (buf == NULL)
return -1;
rc = xmlTextWriterWritePI(writer, target, buf);
xmlFree(buf);
return rc;
}
/**
* xmlTextWriterWritePI:
* @writer: the xmlTextWriterPtr
* @target: PI target
* @content: PI content
*
* Write an xml PI.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWritePI(xmlTextWriterPtr writer, const xmlChar * target,
const xmlChar * content)
{
int count;
int sum;
sum = 0;
count = xmlTextWriterStartPI(writer, target);
if (count == -1)
return -1;
sum += count;
if (content != 0) {
count = xmlTextWriterWriteString(writer, content);
if (count == -1)
return -1;
sum += count;
}
count = xmlTextWriterEndPI(writer);
if (count == -1)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterStartCDATA:
* @writer: the xmlTextWriterPtr
*
* Start an xml CDATA section.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterStartCDATA(xmlTextWriterPtr writer)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL)
return -1;
sum = 0;
lk = xmlListFront(writer->nodes);
if (lk != 0) {
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p != 0) {
switch (p->state) {
case XML_TEXTWRITER_NONE:
case XML_TEXTWRITER_TEXT:
case XML_TEXTWRITER_PI:
case XML_TEXTWRITER_PI_TEXT:
break;
case XML_TEXTWRITER_ATTRIBUTE:
count = xmlTextWriterEndAttribute(writer);
if (count < 0)
return -1;
sum += count;
/* fallthrough */
case XML_TEXTWRITER_NAME:
/* Output namespace declarations */
count = xmlTextWriterOutputNSDecl(writer);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWriteString(writer->out, ">");
if (count < 0)
return -1;
sum += count;
p->state = XML_TEXTWRITER_TEXT;
break;
case XML_TEXTWRITER_CDATA:
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterStartCDATA : CDATA not allowed in this context!\n");
return -1;
default:
return -1;
}
}
}
p = (xmlTextWriterStackEntry *)
xmlMalloc(sizeof(xmlTextWriterStackEntry));
if (p == 0) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartCDATA : out of memory!\n");
return -1;
}
p->name = NULL;
p->state = XML_TEXTWRITER_CDATA;
xmlListPushFront(writer->nodes, p);
count = xmlOutputBufferWriteString(writer->out, "<![CDATA[");
if (count < 0)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterEndCDATA:
* @writer: the xmlTextWriterPtr
*
* End an xml CDATA section.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterEndCDATA(xmlTextWriterPtr writer)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL)
return -1;
lk = xmlListFront(writer->nodes);
if (lk == 0)
return -1;
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p == 0)
return -1;
sum = 0;
switch (p->state) {
case XML_TEXTWRITER_CDATA:
count = xmlOutputBufferWriteString(writer->out, "]]>");
if (count < 0)
return -1;
sum += count;
break;
default:
return -1;
}
xmlListPopFront(writer->nodes);
return sum;
}
/**
* xmlTextWriterWriteFormatCDATA:
* @writer: the xmlTextWriterPtr
* @format: format string (see printf)
* @...: extra parameters for the format
*
* Write a formatted xml CDATA.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteFormatCDATA(xmlTextWriterPtr writer, const char *format,
...)
{
int rc;
va_list ap;
va_start(ap, format);
rc = xmlTextWriterWriteVFormatCDATA(writer, format, ap);
va_end(ap);
return rc;
}
/**
* xmlTextWriterWriteVFormatCDATA:
* @writer: the xmlTextWriterPtr
* @format: format string (see printf)
* @argptr: pointer to the first member of the variable argument list.
*
* Write a formatted xml CDATA.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteVFormatCDATA(xmlTextWriterPtr writer, const char *format,
va_list argptr)
{
int rc;
xmlChar *buf;
if (writer == NULL)
return -1;
buf = xmlTextWriterVSprintf(format, argptr);
if (buf == NULL)
return -1;
rc = xmlTextWriterWriteCDATA(writer, buf);
xmlFree(buf);
return rc;
}
/**
* xmlTextWriterWriteCDATA:
* @writer: the xmlTextWriterPtr
* @content: CDATA content
*
* Write an xml CDATA.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteCDATA(xmlTextWriterPtr writer, const xmlChar * content)
{
int count;
int sum;
sum = 0;
count = xmlTextWriterStartCDATA(writer);
if (count == -1)
return -1;
sum += count;
if (content != 0) {
count = xmlTextWriterWriteString(writer, content);
if (count == -1)
return -1;
sum += count;
}
count = xmlTextWriterEndCDATA(writer);
if (count == -1)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterStartDTD:
* @writer: the xmlTextWriterPtr
* @name: the name of the DTD
* @pubid: the public identifier, which is an alternative to the system identifier
* @sysid: the system identifier, which is the URI of the DTD
*
* Start an xml DTD.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterStartDTD(xmlTextWriterPtr writer,
const xmlChar * name,
const xmlChar * pubid, const xmlChar * sysid)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL || name == NULL || *name == '\0')
return -1;
sum = 0;
lk = xmlListFront(writer->nodes);
if ((lk != NULL) && (xmlLinkGetData(lk) != NULL)) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterStartDTD : DTD allowed only in prolog!\n");
return -1;
}
p = (xmlTextWriterStackEntry *)
xmlMalloc(sizeof(xmlTextWriterStackEntry));
if (p == 0) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartDTD : out of memory!\n");
return -1;
}
p->name = xmlStrdup(name);
if (p->name == 0) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartDTD : out of memory!\n");
xmlFree(p);
return -1;
}
p->state = XML_TEXTWRITER_DTD;
xmlListPushFront(writer->nodes, p);
count = xmlOutputBufferWriteString(writer->out, "<!DOCTYPE ");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWriteString(writer->out, (const char *) name);
if (count < 0)
return -1;
sum += count;
if (pubid != 0) {
if (sysid == 0) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterStartDTD : system identifier needed!\n");
return -1;
}
if (writer->indent)
count = xmlOutputBufferWrite(writer->out, 1, "\n");
else
count = xmlOutputBufferWrite(writer->out, 1, " ");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWriteString(writer->out, "PUBLIC ");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
count =
xmlOutputBufferWriteString(writer->out, (const char *) pubid);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
}
if (sysid != 0) {
if (pubid == 0) {
if (writer->indent)
count = xmlOutputBufferWrite(writer->out, 1, "\n");
else
count = xmlOutputBufferWrite(writer->out, 1, " ");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWriteString(writer->out, "SYSTEM ");
if (count < 0)
return -1;
sum += count;
} else {
if (writer->indent)
count = xmlOutputBufferWriteString(writer->out, "\n ");
else
count = xmlOutputBufferWrite(writer->out, 1, " ");
if (count < 0)
return -1;
sum += count;
}
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
count =
xmlOutputBufferWriteString(writer->out, (const char *) sysid);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
}
return sum;
}
/**
* xmlTextWriterEndDTD:
* @writer: the xmlTextWriterPtr
*
* End an xml DTD.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterEndDTD(xmlTextWriterPtr writer)
{
int loop;
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL)
return -1;
sum = 0;
loop = 1;
while (loop) {
lk = xmlListFront(writer->nodes);
if (lk == NULL)
break;
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p == 0)
break;
switch (p->state) {
case XML_TEXTWRITER_DTD_TEXT:
count = xmlOutputBufferWriteString(writer->out, "]");
if (count < 0)
return -1;
sum += count;
/* fallthrough */
case XML_TEXTWRITER_DTD:
count = xmlOutputBufferWriteString(writer->out, ">");
if (writer->indent) {
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWriteString(writer->out, "\n");
}
xmlListPopFront(writer->nodes);
break;
case XML_TEXTWRITER_DTD_ELEM:
case XML_TEXTWRITER_DTD_ELEM_TEXT:
count = xmlTextWriterEndDTDElement(writer);
break;
case XML_TEXTWRITER_DTD_ATTL:
case XML_TEXTWRITER_DTD_ATTL_TEXT:
count = xmlTextWriterEndDTDAttlist(writer);
break;
case XML_TEXTWRITER_DTD_ENTY:
case XML_TEXTWRITER_DTD_PENT:
case XML_TEXTWRITER_DTD_ENTY_TEXT:
count = xmlTextWriterEndDTDEntity(writer);
break;
case XML_TEXTWRITER_COMMENT:
count = xmlTextWriterEndComment(writer);
break;
default:
loop = 0;
continue;
}
if (count < 0)
return -1;
sum += count;
}
return sum;
}
/**
* xmlTextWriterWriteFormatDTD:
* @writer: the xmlTextWriterPtr
* @name: the name of the DTD
* @pubid: the public identifier, which is an alternative to the system identifier
* @sysid: the system identifier, which is the URI of the DTD
* @format: format string (see printf)
* @...: extra parameters for the format
*
* Write a DTD with a formatted markup declarations part.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteFormatDTD(xmlTextWriterPtr writer,
const xmlChar * name,
const xmlChar * pubid,
const xmlChar * sysid, const char *format, ...)
{
int rc;
va_list ap;
va_start(ap, format);
rc = xmlTextWriterWriteVFormatDTD(writer, name, pubid, sysid, format,
ap);
va_end(ap);
return rc;
}
/**
* xmlTextWriterWriteVFormatDTD:
* @writer: the xmlTextWriterPtr
* @name: the name of the DTD
* @pubid: the public identifier, which is an alternative to the system identifier
* @sysid: the system identifier, which is the URI of the DTD
* @format: format string (see printf)
* @argptr: pointer to the first member of the variable argument list.
*
* Write a DTD with a formatted markup declarations part.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteVFormatDTD(xmlTextWriterPtr writer,
const xmlChar * name,
const xmlChar * pubid,
const xmlChar * sysid,
const char *format, va_list argptr)
{
int rc;
xmlChar *buf;
if (writer == NULL)
return -1;
buf = xmlTextWriterVSprintf(format, argptr);
if (buf == NULL)
return -1;
rc = xmlTextWriterWriteDTD(writer, name, pubid, sysid, buf);
xmlFree(buf);
return rc;
}
/**
* xmlTextWriterWriteDTD:
* @writer: the xmlTextWriterPtr
* @name: the name of the DTD
* @pubid: the public identifier, which is an alternative to the system identifier
* @sysid: the system identifier, which is the URI of the DTD
* @subset: string content of the DTD
*
* Write a DTD.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteDTD(xmlTextWriterPtr writer,
const xmlChar * name,
const xmlChar * pubid,
const xmlChar * sysid, const xmlChar * subset)
{
int count;
int sum;
sum = 0;
count = xmlTextWriterStartDTD(writer, name, pubid, sysid);
if (count == -1)
return -1;
sum += count;
if (subset != 0) {
count = xmlTextWriterWriteString(writer, subset);
if (count == -1)
return -1;
sum += count;
}
count = xmlTextWriterEndDTD(writer);
if (count == -1)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterStartDTDElement:
* @writer: the xmlTextWriterPtr
* @name: the name of the DTD element
*
* Start an xml DTD element.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterStartDTDElement(xmlTextWriterPtr writer, const xmlChar * name)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL || name == NULL || *name == '\0')
return -1;
sum = 0;
lk = xmlListFront(writer->nodes);
if (lk == 0) {
return -1;
}
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p != 0) {
switch (p->state) {
case XML_TEXTWRITER_DTD:
count = xmlOutputBufferWriteString(writer->out, " [");
if (count < 0)
return -1;
sum += count;
if (writer->indent) {
count = xmlOutputBufferWriteString(writer->out, "\n");
if (count < 0)
return -1;
sum += count;
}
p->state = XML_TEXTWRITER_DTD_TEXT;
/* fallthrough */
case XML_TEXTWRITER_DTD_TEXT:
case XML_TEXTWRITER_NONE:
break;
default:
return -1;
}
}
p = (xmlTextWriterStackEntry *)
xmlMalloc(sizeof(xmlTextWriterStackEntry));
if (p == 0) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartDTDElement : out of memory!\n");
return -1;
}
p->name = xmlStrdup(name);
if (p->name == 0) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartDTDElement : out of memory!\n");
xmlFree(p);
return -1;
}
p->state = XML_TEXTWRITER_DTD_ELEM;
xmlListPushFront(writer->nodes, p);
if (writer->indent) {
count = xmlTextWriterWriteIndent(writer);
if (count < 0)
return -1;
sum += count;
}
count = xmlOutputBufferWriteString(writer->out, "<!ELEMENT ");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWriteString(writer->out, (const char *) name);
if (count < 0)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterEndDTDElement:
* @writer: the xmlTextWriterPtr
*
* End an xml DTD element.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterEndDTDElement(xmlTextWriterPtr writer)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL)
return -1;
sum = 0;
lk = xmlListFront(writer->nodes);
if (lk == 0)
return -1;
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p == 0)
return -1;
switch (p->state) {
case XML_TEXTWRITER_DTD_ELEM:
case XML_TEXTWRITER_DTD_ELEM_TEXT:
count = xmlOutputBufferWriteString(writer->out, ">");
if (count < 0)
return -1;
sum += count;
break;
default:
return -1;
}
if (writer->indent) {
count = xmlOutputBufferWriteString(writer->out, "\n");
if (count < 0)
return -1;
sum += count;
}
xmlListPopFront(writer->nodes);
return sum;
}
/**
* xmlTextWriterWriteFormatDTDElement:
* @writer: the xmlTextWriterPtr
* @name: the name of the DTD element
* @format: format string (see printf)
* @...: extra parameters for the format
*
* Write a formatted DTD element.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteFormatDTDElement(xmlTextWriterPtr writer,
const xmlChar * name,
const char *format, ...)
{
int rc;
va_list ap;
va_start(ap, format);
rc = xmlTextWriterWriteVFormatDTDElement(writer, name, format, ap);
va_end(ap);
return rc;
}
/**
* xmlTextWriterWriteVFormatDTDElement:
* @writer: the xmlTextWriterPtr
* @name: the name of the DTD element
* @format: format string (see printf)
* @argptr: pointer to the first member of the variable argument list.
*
* Write a formatted DTD element.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteVFormatDTDElement(xmlTextWriterPtr writer,
const xmlChar * name,
const char *format, va_list argptr)
{
int rc;
xmlChar *buf;
if (writer == NULL)
return -1;
buf = xmlTextWriterVSprintf(format, argptr);
if (buf == NULL)
return -1;
rc = xmlTextWriterWriteDTDElement(writer, name, buf);
xmlFree(buf);
return rc;
}
/**
* xmlTextWriterWriteDTDElement:
* @writer: the xmlTextWriterPtr
* @name: the name of the DTD element
* @content: content of the element
*
* Write a DTD element.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteDTDElement(xmlTextWriterPtr writer,
const xmlChar * name, const xmlChar * content)
{
int count;
int sum;
if (content == NULL)
return -1;
sum = 0;
count = xmlTextWriterStartDTDElement(writer, name);
if (count == -1)
return -1;
sum += count;
count = xmlTextWriterWriteString(writer, content);
if (count == -1)
return -1;
sum += count;
count = xmlTextWriterEndDTDElement(writer);
if (count == -1)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterStartDTDAttlist:
* @writer: the xmlTextWriterPtr
* @name: the name of the DTD ATTLIST
*
* Start an xml DTD ATTLIST.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterStartDTDAttlist(xmlTextWriterPtr writer, const xmlChar * name)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL || name == NULL || *name == '\0')
return -1;
sum = 0;
lk = xmlListFront(writer->nodes);
if (lk == 0) {
return -1;
}
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p != 0) {
switch (p->state) {
case XML_TEXTWRITER_DTD:
count = xmlOutputBufferWriteString(writer->out, " [");
if (count < 0)
return -1;
sum += count;
if (writer->indent) {
count = xmlOutputBufferWriteString(writer->out, "\n");
if (count < 0)
return -1;
sum += count;
}
p->state = XML_TEXTWRITER_DTD_TEXT;
/* fallthrough */
case XML_TEXTWRITER_DTD_TEXT:
case XML_TEXTWRITER_NONE:
break;
default:
return -1;
}
}
p = (xmlTextWriterStackEntry *)
xmlMalloc(sizeof(xmlTextWriterStackEntry));
if (p == 0) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartDTDAttlist : out of memory!\n");
return -1;
}
p->name = xmlStrdup(name);
if (p->name == 0) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartDTDAttlist : out of memory!\n");
xmlFree(p);
return -1;
}
p->state = XML_TEXTWRITER_DTD_ATTL;
xmlListPushFront(writer->nodes, p);
if (writer->indent) {
count = xmlTextWriterWriteIndent(writer);
if (count < 0)
return -1;
sum += count;
}
count = xmlOutputBufferWriteString(writer->out, "<!ATTLIST ");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWriteString(writer->out, (const char *) name);
if (count < 0)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterEndDTDAttlist:
* @writer: the xmlTextWriterPtr
*
* End an xml DTD attribute list.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterEndDTDAttlist(xmlTextWriterPtr writer)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL)
return -1;
sum = 0;
lk = xmlListFront(writer->nodes);
if (lk == 0)
return -1;
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p == 0)
return -1;
switch (p->state) {
case XML_TEXTWRITER_DTD_ATTL:
case XML_TEXTWRITER_DTD_ATTL_TEXT:
count = xmlOutputBufferWriteString(writer->out, ">");
if (count < 0)
return -1;
sum += count;
break;
default:
return -1;
}
if (writer->indent) {
count = xmlOutputBufferWriteString(writer->out, "\n");
if (count < 0)
return -1;
sum += count;
}
xmlListPopFront(writer->nodes);
return sum;
}
/**
* xmlTextWriterWriteFormatDTDAttlist:
* @writer: the xmlTextWriterPtr
* @name: the name of the DTD ATTLIST
* @format: format string (see printf)
* @...: extra parameters for the format
*
* Write a formatted DTD ATTLIST.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteFormatDTDAttlist(xmlTextWriterPtr writer,
const xmlChar * name,
const char *format, ...)
{
int rc;
va_list ap;
va_start(ap, format);
rc = xmlTextWriterWriteVFormatDTDAttlist(writer, name, format, ap);
va_end(ap);
return rc;
}
/**
* xmlTextWriterWriteVFormatDTDAttlist:
* @writer: the xmlTextWriterPtr
* @name: the name of the DTD ATTLIST
* @format: format string (see printf)
* @argptr: pointer to the first member of the variable argument list.
*
* Write a formatted DTD ATTLIST.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteVFormatDTDAttlist(xmlTextWriterPtr writer,
const xmlChar * name,
const char *format, va_list argptr)
{
int rc;
xmlChar *buf;
if (writer == NULL)
return -1;
buf = xmlTextWriterVSprintf(format, argptr);
if (buf == NULL)
return -1;
rc = xmlTextWriterWriteDTDAttlist(writer, name, buf);
xmlFree(buf);
return rc;
}
/**
* xmlTextWriterWriteDTDAttlist:
* @writer: the xmlTextWriterPtr
* @name: the name of the DTD ATTLIST
* @content: content of the ATTLIST
*
* Write a DTD ATTLIST.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteDTDAttlist(xmlTextWriterPtr writer,
const xmlChar * name, const xmlChar * content)
{
int count;
int sum;
if (content == NULL)
return -1;
sum = 0;
count = xmlTextWriterStartDTDAttlist(writer, name);
if (count == -1)
return -1;
sum += count;
count = xmlTextWriterWriteString(writer, content);
if (count == -1)
return -1;
sum += count;
count = xmlTextWriterEndDTDAttlist(writer);
if (count == -1)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterStartDTDEntity:
* @writer: the xmlTextWriterPtr
* @pe: TRUE if this is a parameter entity, FALSE if not
* @name: the name of the DTD ATTLIST
*
* Start an xml DTD ATTLIST.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterStartDTDEntity(xmlTextWriterPtr writer,
int pe, const xmlChar * name)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL || name == NULL || *name == '\0')
return -1;
sum = 0;
lk = xmlListFront(writer->nodes);
if (lk != 0) {
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p != 0) {
switch (p->state) {
case XML_TEXTWRITER_DTD:
count = xmlOutputBufferWriteString(writer->out, " [");
if (count < 0)
return -1;
sum += count;
if (writer->indent) {
count =
xmlOutputBufferWriteString(writer->out, "\n");
if (count < 0)
return -1;
sum += count;
}
p->state = XML_TEXTWRITER_DTD_TEXT;
/* fallthrough */
case XML_TEXTWRITER_DTD_TEXT:
case XML_TEXTWRITER_NONE:
break;
default:
return -1;
}
}
}
p = (xmlTextWriterStackEntry *)
xmlMalloc(sizeof(xmlTextWriterStackEntry));
if (p == 0) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartDTDElement : out of memory!\n");
return -1;
}
p->name = xmlStrdup(name);
if (p->name == 0) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartDTDElement : out of memory!\n");
xmlFree(p);
return -1;
}
if (pe != 0)
p->state = XML_TEXTWRITER_DTD_PENT;
else
p->state = XML_TEXTWRITER_DTD_ENTY;
xmlListPushFront(writer->nodes, p);
if (writer->indent) {
count = xmlTextWriterWriteIndent(writer);
if (count < 0)
return -1;
sum += count;
}
count = xmlOutputBufferWriteString(writer->out, "<!ENTITY ");
if (count < 0)
return -1;
sum += count;
if (pe != 0) {
count = xmlOutputBufferWriteString(writer->out, "% ");
if (count < 0)
return -1;
sum += count;
}
count = xmlOutputBufferWriteString(writer->out, (const char *) name);
if (count < 0)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterEndDTDEntity:
* @writer: the xmlTextWriterPtr
*
* End an xml DTD entity.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterEndDTDEntity(xmlTextWriterPtr writer)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL)
return -1;
sum = 0;
lk = xmlListFront(writer->nodes);
if (lk == 0)
return -1;
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p == 0)
return -1;
switch (p->state) {
case XML_TEXTWRITER_DTD_ENTY_TEXT:
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
/* Falls through. */
case XML_TEXTWRITER_DTD_ENTY:
case XML_TEXTWRITER_DTD_PENT:
count = xmlOutputBufferWriteString(writer->out, ">");
if (count < 0)
return -1;
sum += count;
break;
default:
return -1;
}
if (writer->indent) {
count = xmlOutputBufferWriteString(writer->out, "\n");
if (count < 0)
return -1;
sum += count;
}
xmlListPopFront(writer->nodes);
return sum;
}
/**
* xmlTextWriterWriteFormatDTDInternalEntity:
* @writer: the xmlTextWriterPtr
* @pe: TRUE if this is a parameter entity, FALSE if not
* @name: the name of the DTD entity
* @format: format string (see printf)
* @...: extra parameters for the format
*
* Write a formatted DTD internal entity.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteFormatDTDInternalEntity(xmlTextWriterPtr writer,
int pe,
const xmlChar * name,
const char *format, ...)
{
int rc;
va_list ap;
va_start(ap, format);
rc = xmlTextWriterWriteVFormatDTDInternalEntity(writer, pe, name,
format, ap);
va_end(ap);
return rc;
}
/**
* xmlTextWriterWriteVFormatDTDInternalEntity:
* @writer: the xmlTextWriterPtr
* @pe: TRUE if this is a parameter entity, FALSE if not
* @name: the name of the DTD entity
* @format: format string (see printf)
* @argptr: pointer to the first member of the variable argument list.
*
* Write a formatted DTD internal entity.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteVFormatDTDInternalEntity(xmlTextWriterPtr writer,
int pe,
const xmlChar * name,
const char *format,
va_list argptr)
{
int rc;
xmlChar *buf;
if (writer == NULL)
return -1;
buf = xmlTextWriterVSprintf(format, argptr);
if (buf == NULL)
return -1;
rc = xmlTextWriterWriteDTDInternalEntity(writer, pe, name, buf);
xmlFree(buf);
return rc;
}
/**
* xmlTextWriterWriteDTDEntity:
* @writer: the xmlTextWriterPtr
* @pe: TRUE if this is a parameter entity, FALSE if not
* @name: the name of the DTD entity
* @pubid: the public identifier, which is an alternative to the system identifier
* @sysid: the system identifier, which is the URI of the DTD
* @ndataid: the xml notation name.
* @content: content of the entity
*
* Write a DTD entity.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteDTDEntity(xmlTextWriterPtr writer,
int pe,
const xmlChar * name,
const xmlChar * pubid,
const xmlChar * sysid,
const xmlChar * ndataid,
const xmlChar * content)
{
if ((content == NULL) && (pubid == NULL) && (sysid == NULL))
return -1;
if ((pe != 0) && (ndataid != NULL))
return -1;
if ((pubid == NULL) && (sysid == NULL))
return xmlTextWriterWriteDTDInternalEntity(writer, pe, name,
content);
return xmlTextWriterWriteDTDExternalEntity(writer, pe, name, pubid,
sysid, ndataid);
}
/**
* xmlTextWriterWriteDTDInternalEntity:
* @writer: the xmlTextWriterPtr
* @pe: TRUE if this is a parameter entity, FALSE if not
* @name: the name of the DTD entity
* @content: content of the entity
*
* Write a DTD internal entity.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteDTDInternalEntity(xmlTextWriterPtr writer,
int pe,
const xmlChar * name,
const xmlChar * content)
{
int count;
int sum;
if ((name == NULL) || (*name == '\0') || (content == NULL))
return -1;
sum = 0;
count = xmlTextWriterStartDTDEntity(writer, pe, name);
if (count == -1)
return -1;
sum += count;
count = xmlTextWriterWriteString(writer, content);
if (count == -1)
return -1;
sum += count;
count = xmlTextWriterEndDTDEntity(writer);
if (count == -1)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterWriteDTDExternalEntity:
* @writer: the xmlTextWriterPtr
* @pe: TRUE if this is a parameter entity, FALSE if not
* @name: the name of the DTD entity
* @pubid: the public identifier, which is an alternative to the system identifier
* @sysid: the system identifier, which is the URI of the DTD
* @ndataid: the xml notation name.
*
* Write a DTD external entity. The entity must have been started with xmlTextWriterStartDTDEntity
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteDTDExternalEntity(xmlTextWriterPtr writer,
int pe,
const xmlChar * name,
const xmlChar * pubid,
const xmlChar * sysid,
const xmlChar * ndataid)
{
int count;
int sum;
if (((pubid == NULL) && (sysid == NULL)))
return -1;
if ((pe != 0) && (ndataid != NULL))
return -1;
sum = 0;
count = xmlTextWriterStartDTDEntity(writer, pe, name);
if (count == -1)
return -1;
sum += count;
count =
xmlTextWriterWriteDTDExternalEntityContents(writer, pubid, sysid,
ndataid);
if (count < 0)
return -1;
sum += count;
count = xmlTextWriterEndDTDEntity(writer);
if (count == -1)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterWriteDTDExternalEntityContents:
* @writer: the xmlTextWriterPtr
* @pubid: the public identifier, which is an alternative to the system identifier
* @sysid: the system identifier, which is the URI of the DTD
* @ndataid: the xml notation name.
*
* Write the contents of a DTD external entity.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteDTDExternalEntityContents(xmlTextWriterPtr writer,
const xmlChar * pubid,
const xmlChar * sysid,
const xmlChar * ndataid)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterWriteDTDExternalEntityContents: xmlTextWriterPtr invalid!\n");
return -1;
}
sum = 0;
lk = xmlListFront(writer->nodes);
if (lk == 0) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterWriteDTDExternalEntityContents: you must call xmlTextWriterStartDTDEntity before the call to this function!\n");
return -1;
}
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p == 0)
return -1;
switch (p->state) {
case XML_TEXTWRITER_DTD_ENTY:
break;
case XML_TEXTWRITER_DTD_PENT:
if (ndataid != NULL) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterWriteDTDExternalEntityContents: notation not allowed with parameter entities!\n");
return -1;
}
break;
default:
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterWriteDTDExternalEntityContents: you must call xmlTextWriterStartDTDEntity before the call to this function!\n");
return -1;
}
if (pubid != 0) {
if (sysid == 0) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterWriteDTDExternalEntityContents: system identifier needed!\n");
return -1;
}
count = xmlOutputBufferWriteString(writer->out, " PUBLIC ");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
count =
xmlOutputBufferWriteString(writer->out, (const char *) pubid);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
}
if (sysid != 0) {
if (pubid == 0) {
count = xmlOutputBufferWriteString(writer->out, " SYSTEM");
if (count < 0)
return -1;
sum += count;
}
count = xmlOutputBufferWriteString(writer->out, " ");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
count =
xmlOutputBufferWriteString(writer->out, (const char *) sysid);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
}
if (ndataid != NULL) {
count = xmlOutputBufferWriteString(writer->out, " NDATA ");
if (count < 0)
return -1;
sum += count;
count =
xmlOutputBufferWriteString(writer->out,
(const char *) ndataid);
if (count < 0)
return -1;
sum += count;
}
return sum;
}
/**
* xmlTextWriterWriteDTDNotation:
* @writer: the xmlTextWriterPtr
* @name: the name of the xml notation
* @pubid: the public identifier, which is an alternative to the system identifier
* @sysid: the system identifier, which is the URI of the DTD
*
* Write a DTD entity.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteDTDNotation(xmlTextWriterPtr writer,
const xmlChar * name,
const xmlChar * pubid, const xmlChar * sysid)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL || name == NULL || *name == '\0')
return -1;
sum = 0;
lk = xmlListFront(writer->nodes);
if (lk == 0) {
return -1;
}
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p != 0) {
switch (p->state) {
case XML_TEXTWRITER_DTD:
count = xmlOutputBufferWriteString(writer->out, " [");
if (count < 0)
return -1;
sum += count;
if (writer->indent) {
count = xmlOutputBufferWriteString(writer->out, "\n");
if (count < 0)
return -1;
sum += count;
}
p->state = XML_TEXTWRITER_DTD_TEXT;
/* fallthrough */
case XML_TEXTWRITER_DTD_TEXT:
break;
default:
return -1;
}
}
if (writer->indent) {
count = xmlTextWriterWriteIndent(writer);
if (count < 0)
return -1;
sum += count;
}
count = xmlOutputBufferWriteString(writer->out, "<!NOTATION ");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWriteString(writer->out, (const char *) name);
if (count < 0)
return -1;
sum += count;
if (pubid != 0) {
count = xmlOutputBufferWriteString(writer->out, " PUBLIC ");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
count =
xmlOutputBufferWriteString(writer->out, (const char *) pubid);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
}
if (sysid != 0) {
if (pubid == 0) {
count = xmlOutputBufferWriteString(writer->out, " SYSTEM");
if (count < 0)
return -1;
sum += count;
}
count = xmlOutputBufferWriteString(writer->out, " ");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
count =
xmlOutputBufferWriteString(writer->out, (const char *) sysid);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
}
count = xmlOutputBufferWriteString(writer->out, ">");
if (count < 0)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterFlush:
* @writer: the xmlTextWriterPtr
*
* Flush the output buffer.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterFlush(xmlTextWriterPtr writer)
{
int count;
if (writer == NULL)
return -1;
if (writer->out == NULL)
count = 0;
else
count = xmlOutputBufferFlush(writer->out);
return count;
}
/**
* misc
*/
/**
* xmlFreeTextWriterStackEntry:
* @lk: the xmlLinkPtr
*
* Free callback for the xmlList.
*/
static void
xmlFreeTextWriterStackEntry(xmlLinkPtr lk)
{
xmlTextWriterStackEntry *p;
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p == 0)
return;
if (p->name != 0)
xmlFree(p->name);
xmlFree(p);
}
/**
* xmlCmpTextWriterStackEntry:
* @data0: the first data
* @data1: the second data
*
* Compare callback for the xmlList.
*
* Returns -1, 0, 1
*/
static int
xmlCmpTextWriterStackEntry(const void *data0, const void *data1)
{
xmlTextWriterStackEntry *p0;
xmlTextWriterStackEntry *p1;
if (data0 == data1)
return 0;
if (data0 == 0)
return -1;
if (data1 == 0)
return 1;
p0 = (xmlTextWriterStackEntry *) data0;
p1 = (xmlTextWriterStackEntry *) data1;
return xmlStrcmp(p0->name, p1->name);
}
/**
* misc
*/
/**
* xmlTextWriterOutputNSDecl:
* @writer: the xmlTextWriterPtr
*
* Output the current namespace declarations.
*/
static int
xmlTextWriterOutputNSDecl(xmlTextWriterPtr writer)
{
xmlLinkPtr lk;
xmlTextWriterNsStackEntry *np;
int count;
int sum;
sum = 0;
while (!xmlListEmpty(writer->nsstack)) {
xmlChar *namespaceURI = NULL;
xmlChar *prefix = NULL;
lk = xmlListFront(writer->nsstack);
np = (xmlTextWriterNsStackEntry *) xmlLinkGetData(lk);
if (np != 0) {
namespaceURI = xmlStrdup(np->uri);
prefix = xmlStrdup(np->prefix);
}
xmlListPopFront(writer->nsstack);
if (np != 0) {
count = xmlTextWriterWriteAttribute(writer, prefix, namespaceURI);
xmlFree(namespaceURI);
xmlFree(prefix);
if (count < 0) {
xmlListDelete(writer->nsstack);
writer->nsstack = NULL;
return -1;
}
sum += count;
}
}
return sum;
}
/**
* xmlFreeTextWriterNsStackEntry:
* @lk: the xmlLinkPtr
*
* Free callback for the xmlList.
*/
static void
xmlFreeTextWriterNsStackEntry(xmlLinkPtr lk)
{
xmlTextWriterNsStackEntry *p;
p = (xmlTextWriterNsStackEntry *) xmlLinkGetData(lk);
if (p == 0)
return;
if (p->prefix != 0)
xmlFree(p->prefix);
if (p->uri != 0)
xmlFree(p->uri);
xmlFree(p);
}
/**
* xmlCmpTextWriterNsStackEntry:
* @data0: the first data
* @data1: the second data
*
* Compare callback for the xmlList.
*
* Returns -1, 0, 1
*/
static int
xmlCmpTextWriterNsStackEntry(const void *data0, const void *data1)
{
xmlTextWriterNsStackEntry *p0;
xmlTextWriterNsStackEntry *p1;
int rc;
if (data0 == data1)
return 0;
if (data0 == 0)
return -1;
if (data1 == 0)
return 1;
p0 = (xmlTextWriterNsStackEntry *) data0;
p1 = (xmlTextWriterNsStackEntry *) data1;
rc = xmlStrcmp(p0->prefix, p1->prefix);
if ((rc != 0) || (p0->elem != p1->elem))
rc = -1;
return rc;
}
/**
* xmlTextWriterWriteDocCallback:
* @context: the xmlBufferPtr
* @str: the data to write
* @len: the length of the data
*
* Write callback for the xmlOutputBuffer with target xmlBuffer
*
* Returns -1, 0, 1
*/
static int
xmlTextWriterWriteDocCallback(void *context, const char *str, int len)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) context;
int rc;
if ((rc = xmlParseChunk(ctxt, str, len, 0)) != 0) {
xmlWriterErrMsgInt(NULL, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterWriteDocCallback : XML error %d !\n",
rc);
return -1;
}
return len;
}
/**
* xmlTextWriterCloseDocCallback:
* @context: the xmlBufferPtr
*
* Close callback for the xmlOutputBuffer with target xmlBuffer
*
* Returns -1, 0, 1
*/
static int
xmlTextWriterCloseDocCallback(void *context)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) context;
int rc;
if ((rc = xmlParseChunk(ctxt, NULL, 0, 1)) != 0) {
xmlWriterErrMsgInt(NULL, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterCloseDocCallback : XML error %d !\n",
rc);
return -1;
}
return 0;
}
/**
* xmlTextWriterVSprintf:
* @format: see printf
* @argptr: pointer to the first member of the variable argument list.
*
* Utility function for formatted output
*
* Returns a new xmlChar buffer with the data or NULL on error. This buffer must be freed.
*/
static xmlChar *
xmlTextWriterVSprintf(const char *format, va_list argptr)
{
int size;
int count;
xmlChar *buf;
va_list locarg;
size = BUFSIZ;
buf = (xmlChar *) xmlMalloc(size);
if (buf == NULL) {
xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
"xmlTextWriterVSprintf : out of memory!\n");
return NULL;
}
VA_COPY(locarg, argptr);
while (((count = vsnprintf((char *) buf, size, format, locarg)) < 0)
|| (count == size - 1) || (count == size) || (count > size)) {
va_end(locarg);
xmlFree(buf);
size += BUFSIZ;
buf = (xmlChar *) xmlMalloc(size);
if (buf == NULL) {
xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
"xmlTextWriterVSprintf : out of memory!\n");
return NULL;
}
VA_COPY(locarg, argptr);
}
va_end(locarg);
return buf;
}
/**
* xmlTextWriterStartDocumentCallback:
* @ctx: the user data (XML parser context)
*
* called at the start of document processing.
*/
static void
xmlTextWriterStartDocumentCallback(void *ctx)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlDocPtr doc;
if (ctxt->html) {
#ifdef LIBXML_HTML_ENABLED
if (ctxt->myDoc == NULL)
ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
if (ctxt->myDoc == NULL) {
if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
ctxt->sax->error(ctxt->userData,
"SAX.startDocument(): out of memory\n");
ctxt->errNo = XML_ERR_NO_MEMORY;
ctxt->instate = XML_PARSER_EOF;
ctxt->disableSAX = 1;
return;
}
#else
xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
"libxml2 built without HTML support\n");
ctxt->errNo = XML_ERR_INTERNAL_ERROR;
ctxt->instate = XML_PARSER_EOF;
ctxt->disableSAX = 1;
return;
#endif
} else {
doc = ctxt->myDoc;
if (doc == NULL)
doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
if (doc != NULL) {
if (doc->children == NULL) {
if (ctxt->encoding != NULL)
doc->encoding = xmlStrdup(ctxt->encoding);
else
doc->encoding = NULL;
doc->standalone = ctxt->standalone;
}
} else {
if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
ctxt->sax->error(ctxt->userData,
"SAX.startDocument(): out of memory\n");
ctxt->errNo = XML_ERR_NO_MEMORY;
ctxt->instate = XML_PARSER_EOF;
ctxt->disableSAX = 1;
return;
}
}
if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
(ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
ctxt->myDoc->URL =
xmlCanonicPath((const xmlChar *) ctxt->input->filename);
if (ctxt->myDoc->URL == NULL)
ctxt->myDoc->URL =
xmlStrdup((const xmlChar *) ctxt->input->filename);
}
}
/**
* xmlTextWriterSetIndent:
* @writer: the xmlTextWriterPtr
* @indent: do indentation?
*
* Set indentation output. indent = 0 do not indentation. indent > 0 do indentation.
*
* Returns -1 on error or 0 otherwise.
*/
int
xmlTextWriterSetIndent(xmlTextWriterPtr writer, int indent)
{
if ((writer == NULL) || (indent < 0))
return -1;
writer->indent = indent;
writer->doindent = 1;
return 0;
}
/**
* xmlTextWriterSetIndentString:
* @writer: the xmlTextWriterPtr
* @str: the xmlChar string
*
* Set string indentation.
*
* Returns -1 on error or 0 otherwise.
*/
int
xmlTextWriterSetIndentString(xmlTextWriterPtr writer, const xmlChar * str)
{
if ((writer == NULL) || (!str))
return -1;
if (writer->ichar != NULL)
xmlFree(writer->ichar);
writer->ichar = xmlStrdup(str);
if (!writer->ichar)
return -1;
else
return 0;
}
/**
* xmlTextWriterSetQuoteChar:
* @writer: the xmlTextWriterPtr
* @quotechar: the quote character
*
* Set the character used for quoting attributes.
*
* Returns -1 on error or 0 otherwise.
*/
int
xmlTextWriterSetQuoteChar(xmlTextWriterPtr writer, xmlChar quotechar)
{
if ((writer == NULL) || ((quotechar != '\'') && (quotechar != '"')))
return -1;
writer->qchar = quotechar;
return 0;
}
/**
* xmlTextWriterWriteIndent:
* @writer: the xmlTextWriterPtr
*
* Write indent string.
*
* Returns -1 on error or the number of strings written.
*/
static int
xmlTextWriterWriteIndent(xmlTextWriterPtr writer)
{
int lksize;
int i;
int ret;
lksize = xmlListSize(writer->nodes);
if (lksize < 1)
return (-1); /* list is empty */
for (i = 0; i < (lksize - 1); i++) {
ret = xmlOutputBufferWriteString(writer->out,
(const char *) writer->ichar);
if (ret == -1)
return (-1);
}
return (lksize - 1);
}
/**
* xmlTextWriterHandleStateDependencies:
* @writer: the xmlTextWriterPtr
* @p: the xmlTextWriterStackEntry
*
* Write state dependent strings.
*
* Returns -1 on error or the number of characters written.
*/
static int
xmlTextWriterHandleStateDependencies(xmlTextWriterPtr writer,
xmlTextWriterStackEntry * p)
{
int count;
int sum;
char extra[3];
if (writer == NULL)
return -1;
if (p == NULL)
return 0;
sum = 0;
extra[0] = extra[1] = extra[2] = '\0';
if (p != 0) {
sum = 0;
switch (p->state) {
case XML_TEXTWRITER_NAME:
/* Output namespace declarations */
count = xmlTextWriterOutputNSDecl(writer);
if (count < 0)
return -1;
sum += count;
extra[0] = '>';
p->state = XML_TEXTWRITER_TEXT;
break;
case XML_TEXTWRITER_PI:
extra[0] = ' ';
p->state = XML_TEXTWRITER_PI_TEXT;
break;
case XML_TEXTWRITER_DTD:
extra[0] = ' ';
extra[1] = '[';
p->state = XML_TEXTWRITER_DTD_TEXT;
break;
case XML_TEXTWRITER_DTD_ELEM:
extra[0] = ' ';
p->state = XML_TEXTWRITER_DTD_ELEM_TEXT;
break;
case XML_TEXTWRITER_DTD_ATTL:
extra[0] = ' ';
p->state = XML_TEXTWRITER_DTD_ATTL_TEXT;
break;
case XML_TEXTWRITER_DTD_ENTY:
case XML_TEXTWRITER_DTD_PENT:
extra[0] = ' ';
extra[1] = writer->qchar;
p->state = XML_TEXTWRITER_DTD_ENTY_TEXT;
break;
default:
break;
}
}
if (*extra != '\0') {
count = xmlOutputBufferWriteString(writer->out, extra);
if (count < 0)
return -1;
sum += count;
}
return sum;
}
#endif
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/liuyuyan6100/libxml2.git
git@gitee.com:liuyuyan6100/libxml2.git
liuyuyan6100
libxml2
libxml2
master

搜索帮助