Something went wrong. Try again.
Reactos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844/* * SAX2.c : Default SAX2 handler to build a tree. * * See Copyright for the status of this software. * * Daniel Veillard <daniel@veillard.com> */
#define IN_LIBXML#include "libxml.h"#include <stdlib.h>#include <string.h>#include <limits.h>#include <stddef.h>#include <libxml/SAX2.h>#include <libxml/xmlmemory.h>#include <libxml/tree.h>#include <libxml/parser.h>#include <libxml/parserInternals.h>#include <libxml/valid.h>#include <libxml/entities.h>#include <libxml/xmlerror.h>#include <libxml/debugXML.h>#include <libxml/xmlIO.h>#include <libxml/uri.h>#include <libxml/valid.h>#include <libxml/HTMLtree.h>
#include "private/error.h"#include "private/parser.h"#include "private/tree.h"
/** * TODO: * * macro to flag unimplemented blocks * XML_CATALOG_PREFER user env to select between system/public preferred * option. C.f. Richard Tobin <richard@cogsci.ed.ac.uk> *> Just FYI, I am using an environment variable XML_CATALOG_PREFER with *> values "system" and "public". I have made the default be "system" to *> match yours. */#define TODO \ xmlGenericError(xmlGenericErrorContext, \ "Unimplemented block at %s:%d\n", \ __FILE__, __LINE__);
/* * xmlSAX2ErrMemory: * @ctxt: an XML validation parser context * @msg: a string to accompany the error message */static void LIBXML_ATTR_FORMAT(2,0)xmlSAX2ErrMemory(xmlParserCtxtPtr ctxt, const char *msg) { xmlStructuredErrorFunc schannel = NULL; const char *str1 = "out of memory\n";
if (ctxt != NULL) { ctxt->errNo = XML_ERR_NO_MEMORY; if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC)) schannel = ctxt->sax->serror; __xmlRaiseError(schannel, ctxt->vctxt.error, ctxt->vctxt.userData, ctxt, NULL, XML_FROM_PARSER, XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0, (const char *) str1, NULL, NULL, 0, 0, msg, (const char *) str1, NULL); ctxt->errNo = XML_ERR_NO_MEMORY; ctxt->instate = XML_PARSER_EOF; ctxt->disableSAX = 1; } else { __xmlRaiseError(schannel, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0, (const char *) str1, NULL, NULL, 0, 0, msg, (const char *) str1, NULL); }}
/** * xmlValidError: * @ctxt: an XML validation parser context * @error: the error number * @msg: the error message * @str1: extra data * @str2: extra data * * Handle a validation error */static void LIBXML_ATTR_FORMAT(3,0)xmlErrValid(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *msg, const char *str1, const char *str2){ xmlStructuredErrorFunc schannel = NULL;
if ((ctxt != NULL) && (ctxt->disableSAX != 0) && (ctxt->instate == XML_PARSER_EOF)) return; if (ctxt != NULL) { ctxt->errNo = error; if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC)) schannel = ctxt->sax->serror; __xmlRaiseError(schannel, ctxt->vctxt.error, ctxt->vctxt.userData, ctxt, NULL, XML_FROM_DTD, error, XML_ERR_ERROR, NULL, 0, (const char *) str1, (const char *) str2, NULL, 0, 0, msg, (const char *) str1, (const char *) str2); ctxt->valid = 0; } else { __xmlRaiseError(schannel, NULL, NULL, ctxt, NULL, XML_FROM_DTD, error, XML_ERR_ERROR, NULL, 0, (const char *) str1, (const char *) str2, NULL, 0, 0, msg, (const char *) str1, (const char *) str2); }}
/** * xmlFatalErrMsg: * @ctxt: an XML parser context * @error: the error number * @msg: the error message * @str1: an error string * @str2: an error string * * Handle a fatal parser error, i.e. violating Well-Formedness constraints */static void LIBXML_ATTR_FORMAT(3,0)xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *msg, const xmlChar *str1, const xmlChar *str2){ if ((ctxt != NULL) && (ctxt->disableSAX != 0) && (ctxt->instate == XML_PARSER_EOF)) return; if (ctxt != NULL) ctxt->errNo = error; __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, NULL, 0, (const char *) str1, (const char *) str2, NULL, 0, 0, msg, str1, str2); if (ctxt != NULL) { ctxt->wellFormed = 0; ctxt->valid = 0; if (ctxt->recovery == 0) ctxt->disableSAX = 1; }}
/** * xmlWarnMsg: * @ctxt: an XML parser context * @error: the error number * @msg: the error message * @str1: an error string * @str2: an error string * * Handle a parser warning */static void LIBXML_ATTR_FORMAT(3,0)xmlWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *msg, const xmlChar *str1){ if ((ctxt != NULL) && (ctxt->disableSAX != 0) && (ctxt->instate == XML_PARSER_EOF)) return; if (ctxt != NULL) ctxt->errNo = error; __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_WARNING, NULL, 0, (const char *) str1, NULL, NULL, 0, 0, msg, str1);}
/** * xmlNsWarnMsg: * @ctxt: an XML parser context * @error: the error number * @msg: the error message * @str1: an error string * * Handle a namespace warning */static void LIBXML_ATTR_FORMAT(3,0)xmlNsWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *msg, const xmlChar *str1, const xmlChar *str2){ if ((ctxt != NULL) && (ctxt->disableSAX != 0) && (ctxt->instate == XML_PARSER_EOF)) return; if (ctxt != NULL) ctxt->errNo = error; __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error, XML_ERR_WARNING, NULL, 0, (const char *) str1, (const char *) str2, NULL, 0, 0, msg, str1, str2);}
/** * xmlSAX2GetPublicId: * @ctx: the user data (XML parser context) * * Provides the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN" * * Returns a xmlChar * */const xmlChar *xmlSAX2GetPublicId(void *ctx ATTRIBUTE_UNUSED){ /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */ return(NULL);}
/** * xmlSAX2GetSystemId: * @ctx: the user data (XML parser context) * * Provides the system ID, basically URL or filename e.g. * http://www.sgmlsource.com/dtds/memo.dtd * * Returns a xmlChar * */const xmlChar *xmlSAX2GetSystemId(void *ctx){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; if ((ctx == NULL) || (ctxt->input == NULL)) return(NULL); return((const xmlChar *) ctxt->input->filename);}
/** * xmlSAX2GetLineNumber: * @ctx: the user data (XML parser context) * * Provide the line number of the current parsing point. * * Returns an int */intxmlSAX2GetLineNumber(void *ctx){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; if ((ctx == NULL) || (ctxt->input == NULL)) return(0); return(ctxt->input->line);}
/** * xmlSAX2GetColumnNumber: * @ctx: the user data (XML parser context) * * Provide the column number of the current parsing point. * * Returns an int */intxmlSAX2GetColumnNumber(void *ctx){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; if ((ctx == NULL) || (ctxt->input == NULL)) return(0); return(ctxt->input->col);}
/** * xmlSAX2IsStandalone: * @ctx: the user data (XML parser context) * * Is this document tagged standalone ? * * Returns 1 if true */intxmlSAX2IsStandalone(void *ctx){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; if ((ctx == NULL) || (ctxt->myDoc == NULL)) return(0); return(ctxt->myDoc->standalone == 1);}
/** * xmlSAX2HasInternalSubset: * @ctx: the user data (XML parser context) * * Does this document has an internal subset * * Returns 1 if true */intxmlSAX2HasInternalSubset(void *ctx){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0); return(ctxt->myDoc->intSubset != NULL);}
/** * xmlSAX2HasExternalSubset: * @ctx: the user data (XML parser context) * * Does this document has an external subset * * Returns 1 if true */intxmlSAX2HasExternalSubset(void *ctx){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0); return(ctxt->myDoc->extSubset != NULL);}
/** * xmlSAX2InternalSubset: * @ctx: the user data (XML parser context) * @name: the root element name * @ExternalID: the external ID * @SystemID: the SYSTEM ID (e.g. filename or URL) * * Callback on internal subset declaration. */voidxmlSAX2InternalSubset(void *ctx, const xmlChar *name, const xmlChar *ExternalID, const xmlChar *SystemID){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlDtdPtr dtd; if (ctx == NULL) return;
if (ctxt->myDoc == NULL) return; dtd = xmlGetIntSubset(ctxt->myDoc); if (dtd != NULL) { if (ctxt->html) return; xmlUnlinkNode((xmlNodePtr) dtd); xmlFreeDtd(dtd); ctxt->myDoc->intSubset = NULL; } ctxt->myDoc->intSubset = xmlCreateIntSubset(ctxt->myDoc, name, ExternalID, SystemID); if (ctxt->myDoc->intSubset == NULL) xmlSAX2ErrMemory(ctxt, "xmlSAX2InternalSubset");}
/** * xmlSAX2ExternalSubset: * @ctx: the user data (XML parser context) * @name: the root element name * @ExternalID: the external ID * @SystemID: the SYSTEM ID (e.g. filename or URL) * * Callback on external subset declaration. */voidxmlSAX2ExternalSubset(void *ctx, const xmlChar *name, const xmlChar *ExternalID, const xmlChar *SystemID){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; if (ctx == NULL) return; if (((ExternalID != NULL) || (SystemID != NULL)) && (((ctxt->validate) || (ctxt->loadsubset != 0)) && (ctxt->wellFormed && ctxt->myDoc))) { /* * Try to fetch and parse the external subset. */ xmlParserInputPtr oldinput; int oldinputNr; int oldinputMax; xmlParserInputPtr *oldinputTab; xmlParserInputPtr input = NULL; const xmlChar *oldencoding; int oldprogressive; unsigned long consumed; size_t buffered;
/* * Ask the Entity resolver to load the damn thing */ if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL)) input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID, SystemID); if (input == NULL) { return; }
xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID);
/* * make sure we won't destroy the main document context */ oldinput = ctxt->input; oldinputNr = ctxt->inputNr; oldinputMax = ctxt->inputMax; oldinputTab = ctxt->inputTab; oldencoding = ctxt->encoding; oldprogressive = ctxt->progressive; ctxt->encoding = NULL; ctxt->progressive = 0;
ctxt->inputTab = (xmlParserInputPtr *) xmlMalloc(5 * sizeof(xmlParserInputPtr)); if (ctxt->inputTab == NULL) { xmlSAX2ErrMemory(ctxt, "xmlSAX2ExternalSubset"); xmlFreeInputStream(input); ctxt->input = oldinput; ctxt->inputNr = oldinputNr; ctxt->inputMax = oldinputMax; ctxt->inputTab = oldinputTab; ctxt->encoding = oldencoding; ctxt->progressive = oldprogressive; return; } ctxt->inputNr = 0; ctxt->inputMax = 5; ctxt->input = NULL; xmlPushInput(ctxt, input);
if (input->filename == NULL) input->filename = (char *) xmlCanonicPath(SystemID); input->line = 1; input->col = 1; input->base = ctxt->input->cur; input->cur = ctxt->input->cur; input->free = NULL;
/* * let's parse that entity knowing it's an external subset. */ xmlParseExternalSubset(ctxt, ExternalID, SystemID);
/* * Free up the external entities */
while (ctxt->inputNr > 1) xmlPopInput(ctxt);
consumed = ctxt->input->consumed; buffered = ctxt->input->cur - ctxt->input->base; if (buffered > ULONG_MAX - consumed) consumed = ULONG_MAX; else consumed += buffered; if (consumed > ULONG_MAX - ctxt->sizeentities) ctxt->sizeentities = ULONG_MAX; else ctxt->sizeentities += consumed;
xmlFreeInputStream(ctxt->input); xmlFree(ctxt->inputTab);
/* * Restore the parsing context of the main entity */ ctxt->input = oldinput; ctxt->inputNr = oldinputNr; ctxt->inputMax = oldinputMax; ctxt->inputTab = oldinputTab; if ((ctxt->encoding != NULL) && ((ctxt->dict == NULL) || (!xmlDictOwns(ctxt->dict, ctxt->encoding)))) xmlFree((xmlChar *) ctxt->encoding); ctxt->encoding = oldencoding; ctxt->progressive = oldprogressive; /* ctxt->wellFormed = oldwellFormed; */ }}
/** * xmlSAX2ResolveEntity: * @ctx: the user data (XML parser context) * @publicId: The public ID of the entity * @systemId: The system ID of the entity * * The entity loader, to control the loading of external entities, * the application can either: * - override this xmlSAX2ResolveEntity() callback in the SAX block * - or better use the xmlSetExternalEntityLoader() function to * set up it's own entity resolution routine * * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour. */xmlParserInputPtrxmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlParserInputPtr ret; xmlChar *URI; const char *base = NULL;
if (ctx == NULL) return(NULL); if (ctxt->input != NULL) base = ctxt->input->filename; if (base == NULL) base = ctxt->directory;
URI = xmlBuildURI(systemId, (const xmlChar *) base);
ret = xmlLoadExternalEntity((const char *) URI, (const char *) publicId, ctxt); if (URI != NULL) xmlFree(URI); return(ret);}
/** * xmlSAX2GetEntity: * @ctx: the user data (XML parser context) * @name: The entity name * * Get an entity by name * * Returns the xmlEntityPtr if found. */xmlEntityPtrxmlSAX2GetEntity(void *ctx, const xmlChar *name){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlEntityPtr ret = NULL;
if (ctx == NULL) return(NULL);
if (ctxt->inSubset == 0) { ret = xmlGetPredefinedEntity(name); if (ret != NULL) return(ret); } if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) { if (ctxt->inSubset == 2) { ctxt->myDoc->standalone = 0; ret = xmlGetDocEntity(ctxt->myDoc, name); ctxt->myDoc->standalone = 1; } else { ret = xmlGetDocEntity(ctxt->myDoc, name); if (ret == NULL) { ctxt->myDoc->standalone = 0; ret = xmlGetDocEntity(ctxt->myDoc, name); if (ret != NULL) { xmlFatalErrMsg(ctxt, XML_ERR_NOT_STANDALONE, "Entity(%s) document marked standalone but requires external subset\n", name, NULL); } ctxt->myDoc->standalone = 1; } } } else { ret = xmlGetDocEntity(ctxt->myDoc, name); } return(ret);}
/** * xmlSAX2GetParameterEntity: * @ctx: the user data (XML parser context) * @name: The entity name * * Get a parameter entity by name * * Returns the xmlEntityPtr if found. */xmlEntityPtrxmlSAX2GetParameterEntity(void *ctx, const xmlChar *name){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlEntityPtr ret;
if (ctx == NULL) return(NULL);
ret = xmlGetParameterEntity(ctxt->myDoc, name); return(ret);}
/** * xmlSAX2EntityDecl: * @ctx: the user data (XML parser context) * @name: the entity name * @type: the entity type * @publicId: The public ID of the entity * @systemId: The system ID of the entity * @content: the entity value (without processing). * * An entity definition has been parsed */voidxmlSAX2EntityDecl(void *ctx, const xmlChar *name, int type, const xmlChar *publicId, const xmlChar *systemId, xmlChar *content){ xmlEntityPtr ent; xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
if (ctx == NULL) return; if (ctxt->inSubset == 1) { ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId, systemId, content); if ((ent == NULL) && (ctxt->pedantic)) xmlWarnMsg(ctxt, XML_WAR_ENTITY_REDEFINED, "Entity(%s) already defined in the internal subset\n", name); if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) { xmlChar *URI; const char *base = NULL;
if (ctxt->input != NULL) base = ctxt->input->filename; if (base == NULL) base = ctxt->directory;
URI = xmlBuildURI(systemId, (const xmlChar *) base); ent->URI = URI; } } else if (ctxt->inSubset == 2) { ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId, systemId, content); if ((ent == NULL) && (ctxt->pedantic) && (ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) ctxt->sax->warning(ctxt->userData, "Entity(%s) already defined in the external subset\n", name); if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) { xmlChar *URI; const char *base = NULL;
if (ctxt->input != NULL) base = ctxt->input->filename; if (base == NULL) base = ctxt->directory;
URI = xmlBuildURI(systemId, (const xmlChar *) base); ent->URI = URI; } } else { xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING, "SAX.xmlSAX2EntityDecl(%s) called while not in subset\n", name, NULL); }}
/** * xmlSAX2AttributeDecl: * @ctx: the user data (XML parser context) * @elem: the name of the element * @fullname: the attribute name * @type: the attribute type * @def: the type of default value * @defaultValue: the attribute default value * @tree: the tree of enumerated value set * * An attribute definition has been parsed */voidxmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname, int type, int def, const xmlChar *defaultValue, xmlEnumerationPtr tree){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlAttributePtr attr; xmlChar *name = NULL, *prefix = NULL;
/* Avoid unused variable warning if features are disabled. */ (void) attr;
if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return;
if ((xmlStrEqual(fullname, BAD_CAST "xml:id")) && (type != XML_ATTRIBUTE_ID)) { /* * Raise the error but keep the validity flag */ int tmp = ctxt->valid; xmlErrValid(ctxt, XML_DTD_XMLID_TYPE, "xml:id : attribute type should be ID\n", NULL, NULL); ctxt->valid = tmp; } /* TODO: optimize name/prefix allocation */ name = xmlSplitQName(ctxt, fullname, &prefix); ctxt->vctxt.valid = 1; if (ctxt->inSubset == 1) attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem, name, prefix, (xmlAttributeType) type, (xmlAttributeDefault) def, defaultValue, tree); else if (ctxt->inSubset == 2) attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem, name, prefix, (xmlAttributeType) type, (xmlAttributeDefault) def, defaultValue, tree); else { xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, "SAX.xmlSAX2AttributeDecl(%s) called while not in subset\n", name, NULL); xmlFree(name); xmlFreeEnumeration(tree); return; }#ifdef LIBXML_VALID_ENABLED if (ctxt->vctxt.valid == 0) ctxt->valid = 0; if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) && (ctxt->myDoc->intSubset != NULL)) ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc, attr);#endif /* LIBXML_VALID_ENABLED */ if (prefix != NULL) xmlFree(prefix); if (name != NULL) xmlFree(name);}
/** * xmlSAX2ElementDecl: * @ctx: the user data (XML parser context) * @name: the element name * @type: the element type * @content: the element value tree * * An element definition has been parsed */voidxmlSAX2ElementDecl(void *ctx, const xmlChar * name, int type, xmlElementContentPtr content){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlElementPtr elem = NULL;
/* Avoid unused variable warning if features are disabled. */ (void) elem;
if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return;
if (ctxt->inSubset == 1) elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name, (xmlElementTypeVal) type, content); else if (ctxt->inSubset == 2) elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name, (xmlElementTypeVal) type, content); else { xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, "SAX.xmlSAX2ElementDecl(%s) called while not in subset\n", name, NULL); return; }#ifdef LIBXML_VALID_ENABLED if (elem == NULL) ctxt->valid = 0; if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && ctxt->myDoc->intSubset) ctxt->valid &= xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);#endif /* LIBXML_VALID_ENABLED */}
/** * xmlSAX2NotationDecl: * @ctx: the user data (XML parser context) * @name: The name of the notation * @publicId: The public ID of the entity * @systemId: The system ID of the entity * * What to do when a notation declaration has been parsed. */voidxmlSAX2NotationDecl(void *ctx, const xmlChar *name, const xmlChar *publicId, const xmlChar *systemId){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlNotationPtr nota = NULL;
/* Avoid unused variable warning if features are disabled. */ (void) nota;
if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return;
if ((publicId == NULL) && (systemId == NULL)) { xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING, "SAX.xmlSAX2NotationDecl(%s) externalID or PublicID missing\n", name, NULL); return; } else if (ctxt->inSubset == 1) nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name, publicId, systemId); else if (ctxt->inSubset == 2) nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name, publicId, systemId); else { xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING, "SAX.xmlSAX2NotationDecl(%s) called while not in subset\n", name, NULL); return; }#ifdef LIBXML_VALID_ENABLED if (nota == NULL) ctxt->valid = 0; if ((ctxt->validate) && (ctxt->wellFormed) && (ctxt->myDoc->intSubset != NULL)) ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc, nota);#endif /* LIBXML_VALID_ENABLED */}
/** * xmlSAX2UnparsedEntityDecl: * @ctx: the user data (XML parser context) * @name: The name of the entity * @publicId: The public ID of the entity * @systemId: The system ID of the entity * @notationName: the name of the notation * * What to do when an unparsed entity declaration is parsed */voidxmlSAX2UnparsedEntityDecl(void *ctx, const xmlChar *name, const xmlChar *publicId, const xmlChar *systemId, const xmlChar *notationName){ xmlEntityPtr ent; xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; if (ctx == NULL) return; if (ctxt->inSubset == 1) { ent = xmlAddDocEntity(ctxt->myDoc, name, XML_EXTERNAL_GENERAL_UNPARSED_ENTITY, publicId, systemId, notationName); if ((ent == NULL) && (ctxt->pedantic) && (ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) ctxt->sax->warning(ctxt->userData, "Entity(%s) already defined in the internal subset\n", name); if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) { xmlChar *URI; const char *base = NULL;
if (ctxt->input != NULL) base = ctxt->input->filename; if (base == NULL) base = ctxt->directory;
URI = xmlBuildURI(systemId, (const xmlChar *) base); ent->URI = URI; } } else if (ctxt->inSubset == 2) { ent = xmlAddDtdEntity(ctxt->myDoc, name, XML_EXTERNAL_GENERAL_UNPARSED_ENTITY, publicId, systemId, notationName); if ((ent == NULL) && (ctxt->pedantic) && (ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) ctxt->sax->warning(ctxt->userData, "Entity(%s) already defined in the external subset\n", name); if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) { xmlChar *URI; const char *base = NULL;
if (ctxt->input != NULL) base = ctxt->input->filename; if (base == NULL) base = ctxt->directory;
URI = xmlBuildURI(systemId, (const xmlChar *) base); ent->URI = URI; } } else { xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, "SAX.xmlSAX2UnparsedEntityDecl(%s) called while not in subset\n", name, NULL); }}
/** * xmlSAX2SetDocumentLocator: * @ctx: the user data (XML parser context) * @loc: A SAX Locator * * Receive the document locator at startup, actually xmlDefaultSAXLocator * Everything is available on the context, so this is useless in our case. */voidxmlSAX2SetDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED){}
/** * xmlSAX2StartDocument: * @ctx: the user data (XML parser context) * * called when the document start being processed. */voidxmlSAX2StartDocument(void *ctx){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlDocPtr doc;
if (ctx == NULL) return;
if (ctxt->html) {#ifdef LIBXML_HTML_ENABLED if (ctxt->myDoc == NULL) ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL); if (ctxt->myDoc == NULL) { xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument"); return; } ctxt->myDoc->properties = XML_DOC_HTML; ctxt->myDoc->parseFlags = ctxt->options;#else xmlGenericError(xmlGenericErrorContext, "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 = xmlNewDoc(ctxt->version); if (doc != NULL) { doc->properties = 0; if (ctxt->options & XML_PARSE_OLD10) doc->properties |= XML_DOC_OLD10; doc->parseFlags = ctxt->options; doc->standalone = ctxt->standalone; } else { xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument"); return; } if ((ctxt->dictNames) && (doc != NULL)) { doc->dict = ctxt->dict; xmlDictReference(doc->dict); } } if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) && (ctxt->input != NULL) && (ctxt->input->filename != NULL)) { ctxt->myDoc->URL = xmlPathToURI((const xmlChar *)ctxt->input->filename); if (ctxt->myDoc->URL == NULL) xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument"); }}
/** * xmlSAX2EndDocument: * @ctx: the user data (XML parser context) * * called when the document end has been detected. */voidxmlSAX2EndDocument(void *ctx){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlDocPtr doc;
if (ctx == NULL) return;#ifdef LIBXML_VALID_ENABLED if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && ctxt->myDoc->intSubset) ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);#endif /* LIBXML_VALID_ENABLED */
doc = ctxt->myDoc; if ((doc != NULL) && (doc->encoding == NULL)) { const xmlChar *encoding = xmlGetActualEncoding(ctxt);
if (encoding != NULL) { doc->encoding = xmlStrdup(encoding); if (doc->encoding == NULL) xmlSAX2ErrMemory(ctxt, "xmlSAX2EndDocument"); } }}
#if defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_LEGACY_ENABLED)/** * xmlNsErrMsg: * @ctxt: an XML parser context * @error: the error number * @msg: the error message * @str1: an error string * @str2: an error string * * Handle a namespace error */static void LIBXML_ATTR_FORMAT(3,0)xmlNsErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *msg, const xmlChar *str1, const xmlChar *str2){ if ((ctxt != NULL) && (ctxt->disableSAX != 0) && (ctxt->instate == XML_PARSER_EOF)) return; if (ctxt != NULL) ctxt->errNo = error; __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error, XML_ERR_ERROR, NULL, 0, (const char *) str1, (const char *) str2, NULL, 0, 0, msg, str1, str2);}
/** * xmlSAX2AttributeInternal: * @ctx: the user data (XML parser context) * @fullname: The attribute name, including namespace prefix * @value: The attribute value * @prefix: the prefix on the element node * * Handle an attribute that has been read by the parser. * The default handling is to convert the attribute into an * DOM subtree and past it in a new xmlAttr element added to * the element. */static voidxmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname, const xmlChar *value, const xmlChar *prefix ATTRIBUTE_UNUSED){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlAttrPtr ret; xmlChar *name; xmlChar *ns; xmlChar *nval; xmlNsPtr namespace;
if (ctxt->html) { name = xmlStrdup(fullname); ns = NULL; namespace = NULL; } else { /* * Split the full name into a namespace prefix and the tag name */ name = xmlSplitQName(ctxt, fullname, &ns); if ((name != NULL) && (name[0] == 0)) { if (xmlStrEqual(ns, BAD_CAST "xmlns")) { xmlNsErrMsg(ctxt, XML_ERR_NS_DECL_ERROR, "invalid namespace declaration '%s'\n", fullname, NULL); } else { xmlNsWarnMsg(ctxt, XML_WAR_NS_COLUMN, "Avoid attribute ending with ':' like '%s'\n", fullname, NULL); } if (ns != NULL) xmlFree(ns); ns = NULL; xmlFree(name); name = xmlStrdup(fullname); } } if (name == NULL) { xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement"); if (ns != NULL) xmlFree(ns); return; }
#ifdef LIBXML_HTML_ENABLED if ((ctxt->html) && (value == NULL) && (htmlIsBooleanAttr(fullname))) { nval = xmlStrdup(fullname); value = (const xmlChar *) nval; } else#endif {#ifdef LIBXML_VALID_ENABLED /* * Do the last stage of the attribute normalization * Needed for HTML too: * http://www.w3.org/TR/html4/types.html#h-6.2 */ ctxt->vctxt.valid = 1; nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt, ctxt->myDoc, ctxt->node, fullname, value); if (ctxt->vctxt.valid != 1) { ctxt->valid = 0; } if (nval != NULL) value = nval;#else nval = NULL;#endif /* LIBXML_VALID_ENABLED */ }
/* * Check whether it's a namespace definition */ if ((!ctxt->html) && (ns == NULL) && (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') && (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) { xmlNsPtr nsret; xmlChar *val;
/* Avoid unused variable warning if features are disabled. */ (void) nsret;
if (!ctxt->replaceEntities) { ctxt->depth++; val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF, 0,0,0); ctxt->depth--; if (val == NULL) { xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement"); if (name != NULL) xmlFree(name); if (nval != NULL) xmlFree(nval); return; } } else { val = (xmlChar *) value; }
if (val[0] != 0) { xmlURIPtr uri;
uri = xmlParseURI((const char *)val); if (uri == NULL) { if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) ctxt->sax->warning(ctxt->userData, "xmlns: %s not a valid URI\n", val); } else { if (uri->scheme == NULL) { if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) ctxt->sax->warning(ctxt->userData, "xmlns: URI %s is not absolute\n", val); } xmlFreeURI(uri); } }
/* a default namespace definition */ nsret = xmlNewNs(ctxt->node, val, NULL);
#ifdef LIBXML_VALID_ENABLED /* * Validate also for namespace decls, they are attributes from * an XML-1.0 perspective */ if (nsret != NULL && ctxt->validate && ctxt->wellFormed && ctxt->myDoc && ctxt->myDoc->intSubset) ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc, ctxt->node, prefix, nsret, val);#endif /* LIBXML_VALID_ENABLED */ if (name != NULL) xmlFree(name); if (nval != NULL) xmlFree(nval); if (val != value) xmlFree(val); return; } if ((!ctxt->html) && (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') && (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) { xmlNsPtr nsret; xmlChar *val;
/* Avoid unused variable warning if features are disabled. */ (void) nsret;
if (!ctxt->replaceEntities) { ctxt->depth++; val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF, 0,0,0); ctxt->depth--; if (val == NULL) { xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement"); xmlFree(ns); if (name != NULL) xmlFree(name); if (nval != NULL) xmlFree(nval); return; } } else { val = (xmlChar *) value; }
if (val[0] == 0) { xmlNsErrMsg(ctxt, XML_NS_ERR_EMPTY, "Empty namespace name for prefix %s\n", name, NULL); } if ((ctxt->pedantic != 0) && (val[0] != 0)) { xmlURIPtr uri;
uri = xmlParseURI((const char *)val); if (uri == NULL) { xmlNsWarnMsg(ctxt, XML_WAR_NS_URI, "xmlns:%s: %s not a valid URI\n", name, value); } else { if (uri->scheme == NULL) { xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE, "xmlns:%s: URI %s is not absolute\n", name, value); } xmlFreeURI(uri); } }
/* a standard namespace definition */ nsret = xmlNewNs(ctxt->node, val, name); xmlFree(ns);#ifdef LIBXML_VALID_ENABLED /* * Validate also for namespace decls, they are attributes from * an XML-1.0 perspective */ if (nsret != NULL && ctxt->validate && ctxt->wellFormed && ctxt->myDoc && ctxt->myDoc->intSubset) ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc, ctxt->node, prefix, nsret, value);#endif /* LIBXML_VALID_ENABLED */ if (name != NULL) xmlFree(name); if (nval != NULL) xmlFree(nval); if (val != value) xmlFree(val); return; }
if (ns != NULL) { namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
if (namespace == NULL) { xmlNsErrMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, "Namespace prefix %s of attribute %s is not defined\n", ns, name); } else { xmlAttrPtr prop;
prop = ctxt->node->properties; while (prop != NULL) { if (prop->ns != NULL) { if ((xmlStrEqual(name, prop->name)) && ((namespace == prop->ns) || (xmlStrEqual(namespace->href, prop->ns->href)))) { xmlNsErrMsg(ctxt, XML_ERR_ATTRIBUTE_REDEFINED, "Attribute %s in %s redefined\n", name, namespace->href); ctxt->wellFormed = 0; if (ctxt->recovery == 0) ctxt->disableSAX = 1; if (name != NULL) xmlFree(name); goto error; } } prop = prop->next; } } } else { namespace = NULL; }
/* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */ ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL); if (ret == NULL) goto error;
if ((ctxt->replaceEntities == 0) && (!ctxt->html)) { xmlNodePtr tmp;
ret->children = xmlStringGetNodeList(ctxt->myDoc, value); tmp = ret->children; while (tmp != NULL) { tmp->parent = (xmlNodePtr) ret; if (tmp->next == NULL) ret->last = tmp; tmp = tmp->next; } } else if (value != NULL) { ret->children = xmlNewDocText(ctxt->myDoc, value); ret->last = ret->children; if (ret->children != NULL) ret->children->parent = (xmlNodePtr) ret; }
#ifdef LIBXML_VALID_ENABLED if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed && ctxt->myDoc && ctxt->myDoc->intSubset) {
/* * If we don't substitute entities, the validation should be * done on a value with replaced entities anyway. */ if (!ctxt->replaceEntities) { xmlChar *val;
ctxt->depth++; val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF, 0,0,0); ctxt->depth--;
if (val == NULL) ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc, ctxt->node, ret, value); else { xmlChar *nvalnorm;
/* * Do the last stage of the attribute normalization * It need to be done twice ... it's an extra burden related * to the ability to keep xmlSAX2References in attributes */ nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc, ctxt->node, fullname, val); if (nvalnorm != NULL) { xmlFree(val); val = nvalnorm; }
ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc, ctxt->node, ret, val); xmlFree(val); } } else { ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc, ctxt->node, ret, value); } } else#endif /* LIBXML_VALID_ENABLED */ if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) && (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) || ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0))) && /* Don't create IDs containing entity references */ (ret->children != NULL) && (ret->children->type == XML_TEXT_NODE) && (ret->children->next == NULL)) { xmlChar *content = ret->children->content; /* * when validating, the ID registration is done at the attribute * validation level. Otherwise we have to do specific handling here. */ if (xmlStrEqual(fullname, BAD_CAST "xml:id")) { /* * Add the xml:id value * * Open issue: normalization of the value. */ if (xmlValidateNCName(content, 1) != 0) { xmlErrValid(ctxt, XML_DTD_XMLID_VALUE, "xml:id : attribute value %s is not an NCName\n", (const char *) content, NULL); } xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret); } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret); else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) xmlAddRef(&ctxt->vctxt, ctxt->myDoc, content, ret); }
error: if (nval != NULL) xmlFree(nval); if (ns != NULL) xmlFree(ns);}
/* * xmlCheckDefaultedAttributes: * * Check defaulted attributes from the DTD */static voidxmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name, const xmlChar *prefix, const xmlChar **atts) { xmlElementPtr elemDecl; const xmlChar *att; int internal = 1; int i;
elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix); if (elemDecl == NULL) { elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix); internal = 0; }
process_external_subset:
if (elemDecl != NULL) { xmlAttributePtr attr = elemDecl->attributes; /* * Check against defaulted attributes from the external subset * if the document is stamped as standalone */ if ((ctxt->myDoc->standalone == 1) && (ctxt->myDoc->extSubset != NULL) && (ctxt->validate)) { while (attr != NULL) { if ((attr->defaultValue != NULL) && (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset, attr->elem, attr->name, attr->prefix) == attr) && (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset, attr->elem, attr->name, attr->prefix) == NULL)) { xmlChar *fulln;
if (attr->prefix != NULL) { fulln = xmlStrdup(attr->prefix); fulln = xmlStrcat(fulln, BAD_CAST ":"); fulln = xmlStrcat(fulln, attr->name); } else { fulln = xmlStrdup(attr->name); } if (fulln == NULL) { xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement"); break; }
/* * Check that the attribute is not declared in the * serialization */ att = NULL; if (atts != NULL) { i = 0; att = atts[i]; while (att != NULL) { if (xmlStrEqual(att, fulln)) break; i += 2; att = atts[i]; } } if (att == NULL) { xmlErrValid(ctxt, XML_DTD_STANDALONE_DEFAULTED, "standalone: attribute %s on %s defaulted from external subset\n", (const char *)fulln, (const char *)attr->elem); } xmlFree(fulln); } attr = attr->nexth; } }
/* * Actually insert defaulted values when needed */ attr = elemDecl->attributes; while (attr != NULL) { /* * Make sure that attributes redefinition occurring in the * internal subset are not overridden by definitions in the * external subset. */ if (attr->defaultValue != NULL) { /* * the element should be instantiated in the tree if: * - this is a namespace prefix * - the user required for completion in the tree * like XSLT * - there isn't already an attribute definition * in the internal subset overriding it. */ if (((attr->prefix != NULL) && (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) || ((attr->prefix == NULL) && (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) || (ctxt->loadsubset & XML_COMPLETE_ATTRS)) { xmlAttributePtr tst;
tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset, attr->elem, attr->name, attr->prefix); if ((tst == attr) || (tst == NULL)) { xmlChar fn[50]; xmlChar *fulln;
fulln = xmlBuildQName(attr->name, attr->prefix, fn, 50); if (fulln == NULL) { xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement"); return; }
/* * Check that the attribute is not declared in the * serialization */ att = NULL; if (atts != NULL) { i = 0; att = atts[i]; while (att != NULL) { if (xmlStrEqual(att, fulln)) break; i += 2; att = atts[i]; } } if (att == NULL) { xmlSAX2AttributeInternal(ctxt, fulln, attr->defaultValue, prefix); } if ((fulln != fn) && (fulln != attr->name)) xmlFree(fulln); } } } attr = attr->nexth; } if (internal == 1) { elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix); internal = 0; goto process_external_subset; } }}
/** * xmlSAX2StartElement: * @ctx: the user data (XML parser context) * @fullname: The element name, including namespace prefix * @atts: An array of name/value attributes pairs, NULL terminated * * called when an opening tag has been processed. */voidxmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlNodePtr ret; xmlNodePtr parent; xmlNsPtr ns; xmlChar *name; xmlChar *prefix; const xmlChar *att; const xmlChar *value; int i;
if ((ctx == NULL) || (fullname == NULL) || (ctxt->myDoc == NULL)) return;
/* * First check on validity: */ if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) && ((ctxt->myDoc->intSubset == NULL) || ((ctxt->myDoc->intSubset->notations == NULL) && (ctxt->myDoc->intSubset->elements == NULL) && (ctxt->myDoc->intSubset->attributes == NULL) && (ctxt->myDoc->intSubset->entities == NULL)))) { xmlErrValid(ctxt, XML_ERR_NO_DTD, "Validation failed: no DTD found !", NULL, NULL); ctxt->validate = 0; }
if (ctxt->html) { prefix = NULL; name = xmlStrdup(fullname); } else { /* * Split the full name into a namespace prefix and the tag name */ name = xmlSplitQName(ctxt, fullname, &prefix); }
/* * Note : the namespace resolution is deferred until the end of the * attributes parsing, since local namespace can be defined as * an attribute at this level. */ ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL); if (ret == NULL) { if (prefix != NULL) xmlFree(prefix); xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement"); return; } ctxt->nodemem = -1; if (ctxt->linenumbers) { if (ctxt->input != NULL) { if ((unsigned) ctxt->input->line < (unsigned) USHRT_MAX) ret->line = ctxt->input->line; else ret->line = USHRT_MAX; } }
/* Initialize parent before pushing node */ parent = ctxt->node; if (parent == NULL) parent = (xmlNodePtr) ctxt->myDoc;
/* * We are parsing a new node. */ if (nodePush(ctxt, ret) < 0) { xmlUnlinkNode(ret); xmlFreeNode(ret); if (prefix != NULL) xmlFree(prefix); return; }
/* * Link the child element */ xmlAddChild(parent, ret);
if (!ctxt->html) { /* * Insert all the defaulted attributes from the DTD especially * namespaces */ if ((ctxt->myDoc->intSubset != NULL) || (ctxt->myDoc->extSubset != NULL)) { xmlCheckDefaultedAttributes(ctxt, name, prefix, atts); }
/* * process all the attributes whose name start with "xmlns" */ if (atts != NULL) { i = 0; att = atts[i++]; value = atts[i++]; while ((att != NULL) && (value != NULL)) { if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') && (att[3] == 'n') && (att[4] == 's')) xmlSAX2AttributeInternal(ctxt, att, value, prefix);
att = atts[i++]; value = atts[i++]; } }
/* * Search the namespace, note that since the attributes have been * processed, the local namespaces are available. */ ns = xmlSearchNs(ctxt->myDoc, ret, prefix); if ((ns == NULL) && (parent != NULL)) ns = xmlSearchNs(ctxt->myDoc, parent, prefix); if ((prefix != NULL) && (ns == NULL)) { ns = xmlNewNs(ret, NULL, prefix); xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, "Namespace prefix %s is not defined\n", prefix, NULL); }
/* * set the namespace node, making sure that if the default namespace * is unbound on a parent we simply keep it NULL */ if ((ns != NULL) && (ns->href != NULL) && ((ns->href[0] != 0) || (ns->prefix != NULL))) xmlSetNs(ret, ns); }
/* * process all the other attributes */ if (atts != NULL) { i = 0; att = atts[i++]; value = atts[i++]; if (ctxt->html) { while (att != NULL) { xmlSAX2AttributeInternal(ctxt, att, value, NULL); att = atts[i++]; value = atts[i++]; } } else { while ((att != NULL) && (value != NULL)) { if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') || (att[3] != 'n') || (att[4] != 's')) xmlSAX2AttributeInternal(ctxt, att, value, NULL);
/* * Next ones */ att = atts[i++]; value = atts[i++]; } } }
#ifdef LIBXML_VALID_ENABLED /* * If it's the Document root, finish the DTD validation and * check the document root element for validity */ if ((ctxt->validate) && ((ctxt->vctxt.flags & XML_VCTXT_DTD_VALIDATED) == 0)) { int chk;
chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc); if (chk <= 0) ctxt->valid = 0; if (chk < 0) ctxt->wellFormed = 0; ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); ctxt->vctxt.flags |= XML_VCTXT_DTD_VALIDATED; }#endif /* LIBXML_VALID_ENABLED */
if (prefix != NULL) xmlFree(prefix);
}
/** * xmlSAX2EndElement: * @ctx: the user data (XML parser context) * @name: The element name * * called when the end of an element has been detected. */voidxmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
if (ctx == NULL) return;
ctxt->nodemem = -1;
#ifdef LIBXML_VALID_ENABLED if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && ctxt->myDoc->intSubset) ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, ctxt->node);#endif /* LIBXML_VALID_ENABLED */
/* * end of parsing of this node. */ nodePop(ctxt);}#endif /* LIBXML_SAX1_ENABLED || LIBXML_HTML_ENABLED || LIBXML_LEGACY_ENABLED */
/* * xmlSAX2TextNode: * @ctxt: the parser context * @str: the input string * @len: the string length * * Callback for a text node * * Returns the newly allocated string or NULL if not needed or error */static xmlNodePtrxmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) { xmlNodePtr ret; const xmlChar *intern = NULL;
/* * Allocate */ if (ctxt->freeElems != NULL) { ret = ctxt->freeElems; ctxt->freeElems = ret->next; ctxt->freeElemsNr--; } else { ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); } if (ret == NULL) { xmlErrMemory(ctxt, "xmlSAX2Characters"); return(NULL); } memset(ret, 0, sizeof(xmlNode)); /* * intern the formatting blanks found between tags, or the * very short strings */ if (ctxt->dictNames) { xmlChar cur = str[len];
if ((len < (int) (2 * sizeof(void *))) && (ctxt->options & XML_PARSE_COMPACT)) { /* store the string in the node overriding properties and nsDef */ xmlChar *tmp = (xmlChar *) &(ret->properties); memcpy(tmp, str, len); tmp[len] = 0; intern = tmp; } else if ((len <= 3) && ((cur == '"') || (cur == '\'') || ((cur == '<') && (str[len + 1] != '!')))) { intern = xmlDictLookup(ctxt->dict, str, len); } else if (IS_BLANK_CH(*str) && (len < 60) && (cur == '<') && (str[len + 1] != '!')) { int i;
for (i = 1;i < len;i++) { if (!IS_BLANK_CH(str[i])) goto skip; } intern = xmlDictLookup(ctxt->dict, str, len); } }skip: ret->type = XML_TEXT_NODE;
ret->name = xmlStringText; if (intern == NULL) { ret->content = xmlStrndup(str, len); if (ret->content == NULL) { xmlSAX2ErrMemory(ctxt, "xmlSAX2TextNode"); xmlFree(ret); return(NULL); } } else ret->content = (xmlChar *) intern;
if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) xmlRegisterNodeDefaultValue(ret); return(ret);}
#ifdef LIBXML_VALID_ENABLED/* * xmlSAX2DecodeAttrEntities: * @ctxt: the parser context * @str: the input string * @len: the string length * * Remove the entities from an attribute value * * Returns the newly allocated string or NULL if not needed or error */static xmlChar *xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, const xmlChar *end) { const xmlChar *in; xmlChar *ret;
in = str; while (in < end) if (*in++ == '&') goto decode; return(NULL);decode: ctxt->depth++; ret = xmlStringLenDecodeEntities(ctxt, str, end - str, XML_SUBSTITUTE_REF, 0,0,0); ctxt->depth--; return(ret);}#endif /* LIBXML_VALID_ENABLED */
/** * xmlSAX2AttributeNs: * @ctx: the user data (XML parser context) * @localname: the local name of the attribute * @prefix: the attribute namespace prefix if available * @URI: the attribute namespace name if available * @value: Start of the attribute value * @valueend: end of the attribute value * * Handle an attribute that has been read by the parser. * The default handling is to convert the attribute into an * DOM subtree and past it in a new xmlAttr element added to * the element. * * Returns the new attribute or NULL in case of error. */static xmlAttrPtrxmlSAX2AttributeNs(xmlParserCtxtPtr ctxt, const xmlChar * localname, const xmlChar * prefix, const xmlChar * value, const xmlChar * valueend){ xmlAttrPtr ret; xmlNsPtr namespace = NULL; xmlChar *dup = NULL;
/* * Note: if prefix == NULL, the attribute is not in the default namespace */ if (prefix != NULL) { namespace = xmlParserNsLookupSax(ctxt, prefix); if ((namespace == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) { namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, prefix); } }
/* * allocate the node */ if (ctxt->freeAttrs != NULL) { ret = ctxt->freeAttrs; ctxt->freeAttrs = ret->next; ctxt->freeAttrsNr--; } else { ret = xmlMalloc(sizeof(*ret)); if (ret == NULL) { xmlSAX2ErrMemory(ctxt, NULL); return(NULL); } }
memset(ret, 0, sizeof(xmlAttr)); ret->type = XML_ATTRIBUTE_NODE;
/* * xmlParseBalancedChunkMemoryRecover had a bug that could result in * a mismatch between ctxt->node->doc and ctxt->myDoc. We use * ctxt->node->doc here, but we should somehow make sure that the * document pointers match. */
/* assert(ctxt->node->doc == ctxt->myDoc); */
ret->parent = ctxt->node; ret->doc = ctxt->node->doc; ret->ns = namespace;
if (ctxt->dictNames) ret->name = localname; else ret->name = xmlStrdup(localname);
if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
if ((ctxt->replaceEntities == 0) && (!ctxt->html)) { xmlNodePtr tmp;
/* * We know that if there is an entity reference, then * the string has been dup'ed and terminates with 0 * otherwise with ' or " */ if (*valueend != 0) { tmp = xmlSAX2TextNode(ctxt, value, valueend - value); ret->children = tmp; ret->last = tmp; if (tmp != NULL) { tmp->doc = ret->doc; tmp->parent = (xmlNodePtr) ret; } } else { ret->children = xmlStringLenGetNodeList(ctxt->myDoc, value, valueend - value); tmp = ret->children; while (tmp != NULL) { tmp->doc = ret->doc; tmp->parent = (xmlNodePtr) ret; if (tmp->next == NULL) ret->last = tmp; tmp = tmp->next; } } } else if (value != NULL) { xmlNodePtr tmp;
tmp = xmlSAX2TextNode(ctxt, value, valueend - value); ret->children = tmp; ret->last = tmp; if (tmp != NULL) { tmp->doc = ret->doc; tmp->parent = (xmlNodePtr) ret; } }
#ifdef LIBXML_VALID_ENABLED if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed && ctxt->myDoc && ctxt->myDoc->intSubset) { /* * If we don't substitute entities, the validation should be * done on a value with replaced entities anyway. */ if (!ctxt->replaceEntities) { dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend); if (dup == NULL) { if (*valueend == 0) { ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc, ctxt->node, ret, value); } else { /* * That should already be normalized. * cheaper to finally allocate here than duplicate * entry points in the full validation code */ dup = xmlStrndup(value, valueend - value);
ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc, ctxt->node, ret, dup); } } else { /* * dup now contains a string of the flattened attribute * content with entities substituted. Check if we need to * apply an extra layer of normalization. * It need to be done twice ... it's an extra burden related * to the ability to keep references in attributes */ if (ctxt->attsSpecial != NULL) { xmlChar *nvalnorm; xmlChar fn[50]; xmlChar *fullname;
fullname = xmlBuildQName(localname, prefix, fn, 50); if (fullname != NULL) { ctxt->vctxt.valid = 1; nvalnorm = xmlValidCtxtNormalizeAttributeValue( &ctxt->vctxt, ctxt->myDoc, ctxt->node, fullname, dup); if (ctxt->vctxt.valid != 1) ctxt->valid = 0;
if ((fullname != fn) && (fullname != localname)) xmlFree(fullname); if (nvalnorm != NULL) { xmlFree(dup); dup = nvalnorm; } } }
ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc, ctxt->node, ret, dup); } } else { /* * if entities already have been substituted, then * the attribute as passed is already normalized */ dup = xmlStrndup(value, valueend - value);
ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc, ctxt->node, ret, dup); } } else#endif /* LIBXML_VALID_ENABLED */ if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) && (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) || ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0))) && /* Don't create IDs containing entity references */ (ret->children != NULL) && (ret->children->type == XML_TEXT_NODE) && (ret->children->next == NULL)) { xmlChar *content = ret->children->content; /* * when validating, the ID registration is done at the attribute * validation level. Otherwise we have to do specific handling here. */ if ((prefix == ctxt->str_xml) && (localname[0] == 'i') && (localname[1] == 'd') && (localname[2] == 0)) { /* * Add the xml:id value * * Open issue: normalization of the value. */ if (xmlValidateNCName(content, 1) != 0) { xmlErrValid(ctxt, XML_DTD_XMLID_VALUE, "xml:id : attribute value %s is not an NCName\n", (const char *) content, NULL); } xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret); } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) { xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret); } else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) { xmlAddRef(&ctxt->vctxt, ctxt->myDoc, content, ret); } } if (dup != NULL) xmlFree(dup);
return(ret);}
/** * xmlSAX2StartElementNs: * @ctx: the user data (XML parser context) * @localname: the local name of the element * @prefix: the element namespace prefix if available * @URI: the element namespace name if available * @nb_namespaces: number of namespace definitions on that node * @namespaces: pointer to the array of prefix/URI pairs namespace definitions * @nb_attributes: the number of attributes on that node * @nb_defaulted: the number of defaulted attributes. * @attributes: pointer to the array of (localname/prefix/URI/value/end) * attribute values. * * SAX2 callback when an element start has been detected by the parser. * It provides the namespace information for the element, as well as * the new namespace declarations on the element. */voidxmlSAX2StartElementNs(void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI, int nb_namespaces, const xmlChar **namespaces, int nb_attributes, int nb_defaulted, const xmlChar **attributes){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlNodePtr ret; xmlNodePtr parent; xmlNsPtr last = NULL, ns; const xmlChar *uri, *pref; xmlChar *lname = NULL; int i, j;
if (ctx == NULL) return; /* * First check on validity: */ if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) && ((ctxt->myDoc->intSubset == NULL) || ((ctxt->myDoc->intSubset->notations == NULL) && (ctxt->myDoc->intSubset->elements == NULL) && (ctxt->myDoc->intSubset->attributes == NULL) && (ctxt->myDoc->intSubset->entities == NULL)))) { xmlErrValid(ctxt, XML_DTD_NO_DTD, "Validation failed: no DTD found !", NULL, NULL); ctxt->validate = 0; }
/* * Take care of the rare case of an undefined namespace prefix */ if ((prefix != NULL) && (URI == NULL)) { if (ctxt->dictNames) { const xmlChar *fullname;
fullname = xmlDictQLookup(ctxt->dict, prefix, localname); if (fullname != NULL) localname = fullname; } else { lname = xmlBuildQName(localname, prefix, NULL, 0); } } /* * allocate the node */ if (ctxt->freeElems != NULL) { ret = ctxt->freeElems; ctxt->freeElems = ret->next; ctxt->freeElemsNr--; memset(ret, 0, sizeof(xmlNode)); ret->doc = ctxt->myDoc; ret->type = XML_ELEMENT_NODE;
if (ctxt->dictNames) ret->name = localname; else { if (lname == NULL) ret->name = xmlStrdup(localname); else ret->name = lname; if (ret->name == NULL) { xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs"); xmlFree(ret); return; } } if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) xmlRegisterNodeDefaultValue(ret); } else { if (ctxt->dictNames) ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, (xmlChar *) localname, NULL); else if (lname == NULL) ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL); else ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, (xmlChar *) lname, NULL); if (ret == NULL) { xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs"); return; } } if (ctxt->linenumbers) { if (ctxt->input != NULL) { if ((unsigned) ctxt->input->line < (unsigned) USHRT_MAX) ret->line = ctxt->input->line; else ret->line = USHRT_MAX; } }
/* * Build the namespace list */ for (i = 0,j = 0;j < nb_namespaces;j++) { pref = namespaces[i++]; uri = namespaces[i++]; ns = xmlNewNs(NULL, uri, pref); if (ns != NULL) { if (last == NULL) { ret->nsDef = last = ns; } else { last->next = ns; last = ns; } if ((URI != NULL) && (prefix == pref)) ret->ns = ns; } else { /* * any out of memory error would already have been raised * but we can't be guaranteed it's the actual error due to the * API, best is to skip in this case */ continue; }
xmlParserNsUpdateSax(ctxt, pref, ns);
#ifdef LIBXML_VALID_ENABLED if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed && ctxt->myDoc && ctxt->myDoc->intSubset) { ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc, ret, prefix, ns, uri); }#endif /* LIBXML_VALID_ENABLED */ } ctxt->nodemem = -1;
/* Initialize parent before pushing node */ parent = ctxt->node; if (parent == NULL) parent = (xmlNodePtr) ctxt->myDoc;
/* * We are parsing a new node. */ if (nodePush(ctxt, ret) < 0) { xmlUnlinkNode(ret); xmlFreeNode(ret); return; }
/* * Link the child element */ xmlAddChild(parent, ret);
/* * Insert the defaulted attributes from the DTD only if requested: */ if ((nb_defaulted != 0) && ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0)) nb_attributes -= nb_defaulted;
/* * Search the namespace if it wasn't already found * Note that, if prefix is NULL, this searches for the default Ns */ if ((URI != NULL) && (ret->ns == NULL)) { ret->ns = xmlParserNsLookupSax(ctxt, prefix); if ((ret->ns == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) { ret->ns = xmlSearchNs(ctxt->myDoc, ret, prefix); } if (ret->ns == NULL) { ns = xmlNewNs(ret, NULL, prefix); if (ns == NULL) {
xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs"); return; } if (prefix != NULL) xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, "Namespace prefix %s was not found\n", prefix, NULL); else xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, "Namespace default prefix was not found\n", NULL, NULL); } }
/* * process all the other attributes */ if (nb_attributes > 0) { xmlAttrPtr prev = NULL;
for (j = 0,i = 0;i < nb_attributes;i++,j+=5) { xmlAttrPtr attr = NULL;
/* * Handle the rare case of an undefined attribute prefix */ if ((attributes[j+1] != NULL) && (attributes[j+2] == NULL)) { if (ctxt->dictNames) { const xmlChar *fullname;
fullname = xmlDictQLookup(ctxt->dict, attributes[j+1], attributes[j]); if (fullname != NULL) { attr = xmlSAX2AttributeNs(ctxt, fullname, NULL, attributes[j+3], attributes[j+4]); goto have_attr; } } else { lname = xmlBuildQName(attributes[j], attributes[j+1], NULL, 0); if (lname != NULL) { attr = xmlSAX2AttributeNs(ctxt, lname, NULL, attributes[j+3], attributes[j+4]); xmlFree(lname); goto have_attr; } } } attr = xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1], attributes[j+3], attributes[j+4]);have_attr: if (attr == NULL) continue;
/* link at the end to preserve order */ if (prev == NULL) { ctxt->node->properties = attr; } else { prev->next = attr; attr->prev = prev; }
prev = attr; } }
#ifdef LIBXML_VALID_ENABLED /* * If it's the Document root, finish the DTD validation and * check the document root element for validity */ if ((ctxt->validate) && ((ctxt->vctxt.flags & XML_VCTXT_DTD_VALIDATED) == 0)) { int chk;
chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc); if (chk <= 0) ctxt->valid = 0; if (chk < 0) ctxt->wellFormed = 0; ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); ctxt->vctxt.flags |= XML_VCTXT_DTD_VALIDATED; }#endif /* LIBXML_VALID_ENABLED */}
/** * xmlSAX2EndElementNs: * @ctx: the user data (XML parser context) * @localname: the local name of the element * @prefix: the element namespace prefix if available * @URI: the element namespace name if available * * SAX2 callback when an element end has been detected by the parser. * It provides the namespace information for the element. */voidxmlSAX2EndElementNs(void *ctx, const xmlChar * localname ATTRIBUTE_UNUSED, const xmlChar * prefix ATTRIBUTE_UNUSED, const xmlChar * URI ATTRIBUTE_UNUSED){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
if (ctx == NULL) return; ctxt->nodemem = -1;
#ifdef LIBXML_VALID_ENABLED if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && ctxt->myDoc->intSubset) ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, ctxt->node);#endif /* LIBXML_VALID_ENABLED */
/* * end of parsing of this node. */ nodePop(ctxt);}
/** * xmlSAX2Reference: * @ctx: the user data (XML parser context) * @name: The entity name * * called when an entity xmlSAX2Reference is detected. */voidxmlSAX2Reference(void *ctx, const xmlChar *name){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlNodePtr ret;
if (ctx == NULL) return; ret = xmlNewReference(ctxt->myDoc, name); if (xmlAddChild(ctxt->node, ret) == NULL) { xmlFreeNode(ret); }}
/** * xmlSAX2Text: * @ctx: the user data (XML parser context) * @ch: a xmlChar string * @len: the number of xmlChar * @type: text or cdata * * Append characters. */static voidxmlSAX2Text(xmlParserCtxtPtr ctxt, const xmlChar *ch, int len, xmlElementType type){ xmlNodePtr lastChild;
if (ctxt == NULL) return; /* * Handle the data if any. If there is no child * add it as content, otherwise if the last child is text, * concatenate it, else create a new node of type text. */
if (ctxt->node == NULL) { return; } lastChild = ctxt->node->last;
/* * Here we needed an accelerator mechanism in case of very large * elements. Use an attribute in the structure !!! */ if (lastChild == NULL) { if (type == XML_TEXT_NODE) lastChild = xmlSAX2TextNode(ctxt, ch, len); else lastChild = xmlNewCDataBlock(ctxt->myDoc, ch, len); if (lastChild != NULL) { ctxt->node->children = lastChild; ctxt->node->last = lastChild; lastChild->parent = ctxt->node; lastChild->doc = ctxt->node->doc; ctxt->nodelen = len; ctxt->nodemem = len + 1; } else { xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters"); return; } } else { int coalesceText = (lastChild != NULL) && (lastChild->type == type) && ((type != XML_TEXT_NODE) || (lastChild->name == xmlStringText)); if ((coalesceText) && (ctxt->nodemem != 0)) { /* * The whole point of maintaining nodelen and nodemem, * xmlTextConcat is too costly, i.e. compute length, * reallocate a new buffer, move data, append ch. Here * We try to minimize realloc() uses and avoid copying * and recomputing length over and over. */ if (lastChild->content == (xmlChar *)&(lastChild->properties)) { lastChild->content = xmlStrdup(lastChild->content); lastChild->properties = NULL; } else if ((ctxt->nodemem == ctxt->nodelen + 1) && (xmlDictOwns(ctxt->dict, lastChild->content))) { lastChild->content = xmlStrdup(lastChild->content); } if (lastChild->content == NULL) { xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters: xmlStrdup returned NULL"); return; } if (ctxt->nodelen > INT_MAX - len) { xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters overflow prevented"); return; } if ((ctxt->nodelen + len > XML_MAX_TEXT_LENGTH) && ((ctxt->options & XML_PARSE_HUGE) == 0)) { xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters: huge text node"); return; } if (ctxt->nodelen + len >= ctxt->nodemem) { xmlChar *newbuf; int size;
size = ctxt->nodemem > INT_MAX - len ? INT_MAX : ctxt->nodemem + len; size = size > INT_MAX / 2 ? INT_MAX : size * 2; newbuf = (xmlChar *) xmlRealloc(lastChild->content,size); if (newbuf == NULL) { xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters"); return; } ctxt->nodemem = size; lastChild->content = newbuf; } memcpy(&lastChild->content[ctxt->nodelen], ch, len); ctxt->nodelen += len; lastChild->content[ctxt->nodelen] = 0; } else if (coalesceText) { if (xmlTextConcat(lastChild, ch, len)) { xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters"); } if (ctxt->node->children != NULL) { ctxt->nodelen = xmlStrlen(lastChild->content); ctxt->nodemem = ctxt->nodelen + 1; } } else { /* Mixed content, first time */ if (type == XML_TEXT_NODE) { lastChild = xmlSAX2TextNode(ctxt, ch, len); if (lastChild != NULL) lastChild->doc = ctxt->myDoc; } else lastChild = xmlNewCDataBlock(ctxt->myDoc, ch, len); if (lastChild != NULL) { xmlAddChild(ctxt->node, lastChild); if (ctxt->node->children != NULL) { ctxt->nodelen = len; ctxt->nodemem = len + 1; } } } }
if ((lastChild != NULL) && (type == XML_TEXT_NODE) && (ctxt->linenumbers) && (ctxt->input != NULL)) { if ((unsigned) ctxt->input->line < (unsigned) USHRT_MAX) lastChild->line = ctxt->input->line; else { lastChild->line = USHRT_MAX; if (ctxt->options & XML_PARSE_BIG_LINES) lastChild->psvi = (void *) (ptrdiff_t) ctxt->input->line; } }}
/** * xmlSAX2Characters: * @ctx: the user data (XML parser context) * @ch: a xmlChar string * @len: the number of xmlChar * * receiving some chars from the parser. */voidxmlSAX2Characters(void *ctx, const xmlChar *ch, int len){ xmlSAX2Text((xmlParserCtxtPtr) ctx, ch, len, XML_TEXT_NODE);}
/** * xmlSAX2IgnorableWhitespace: * @ctx: the user data (XML parser context) * @ch: a xmlChar string * @len: the number of xmlChar * * receiving some ignorable whitespaces from the parser. * UNUSED: by default the DOM building will use xmlSAX2Characters */voidxmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED){}
/** * xmlSAX2ProcessingInstruction: * @ctx: the user data (XML parser context) * @target: the target name * @data: the PI data's * * A processing instruction has been parsed. */voidxmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target, const xmlChar *data){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlNodePtr ret; xmlNodePtr parent;
if (ctx == NULL) return; parent = ctxt->node;
ret = xmlNewDocPI(ctxt->myDoc, target, data); if (ret == NULL) return;
if (ctxt->linenumbers) { if (ctxt->input != NULL) { if ((unsigned) ctxt->input->line < (unsigned) USHRT_MAX) ret->line = ctxt->input->line; else ret->line = USHRT_MAX; } } if (ctxt->inSubset == 1) { xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret); return; } else if (ctxt->inSubset == 2) { xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret); return; } if (parent == NULL) { xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret); return; } if (parent->type == XML_ELEMENT_NODE) { xmlAddChild(parent, ret); } else { xmlAddSibling(parent, ret); }}
/** * xmlSAX2Comment: * @ctx: the user data (XML parser context) * @value: the xmlSAX2Comment content * * A xmlSAX2Comment has been parsed. */voidxmlSAX2Comment(void *ctx, const xmlChar *value){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlNodePtr ret; xmlNodePtr parent;
if (ctx == NULL) return; parent = ctxt->node; ret = xmlNewDocComment(ctxt->myDoc, value); if (ret == NULL) return; if (ctxt->linenumbers) { if (ctxt->input != NULL) { if ((unsigned) ctxt->input->line < (unsigned) USHRT_MAX) ret->line = ctxt->input->line; else ret->line = USHRT_MAX; } }
if (ctxt->inSubset == 1) { xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret); return; } else if (ctxt->inSubset == 2) { xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret); return; } if (parent == NULL) { xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret); return; } if (parent->type == XML_ELEMENT_NODE) { xmlAddChild(parent, ret); } else { xmlAddSibling(parent, ret); }}
/** * xmlSAX2CDataBlock: * @ctx: the user data (XML parser context) * @value: The pcdata content * @len: the block length * * called when a pcdata block has been parsed */voidxmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len){ xmlSAX2Text((xmlParserCtxtPtr) ctx, value, len, XML_CDATA_SECTION_NODE);}
static int xmlSAX2DefaultVersionValue = 2;
#ifdef LIBXML_SAX1_ENABLED/** * xmlSAXDefaultVersion: * @version: the version, 1 or 2 * * DEPRECATED: Use parser option XML_PARSE_SAX1. * * Set the default version of SAX used globally by the library. * By default, during initialization the default is set to 2. * Note that it is generally a better coding style to use * xmlSAXVersion() to set up the version explicitly for a given * parsing context. * * Returns the previous value in case of success and -1 in case of error. */intxmlSAXDefaultVersion(int version){ int ret = xmlSAX2DefaultVersionValue;
if ((version != 1) && (version != 2)) return(-1); xmlSAX2DefaultVersionValue = version; return(ret);}#endif /* LIBXML_SAX1_ENABLED */
/** * xmlSAXVersion: * @hdlr: the SAX handler * @version: the version, 1 or 2 * * Initialize the default XML SAX handler according to the version * * Returns 0 in case of success and -1 in case of error. */intxmlSAXVersion(xmlSAXHandler *hdlr, int version){ if (hdlr == NULL) return(-1); if (version == 2) { hdlr->startElementNs = xmlSAX2StartElementNs; hdlr->endElementNs = xmlSAX2EndElementNs; hdlr->serror = NULL; hdlr->initialized = XML_SAX2_MAGIC;#ifdef LIBXML_SAX1_ENABLED } else if (version == 1) { hdlr->initialized = 1;#endif /* LIBXML_SAX1_ENABLED */ } else return(-1);#ifdef LIBXML_SAX1_ENABLED hdlr->startElement = xmlSAX2StartElement; hdlr->endElement = xmlSAX2EndElement;#else hdlr->startElement = NULL; hdlr->endElement = NULL;#endif /* LIBXML_SAX1_ENABLED */ hdlr->internalSubset = xmlSAX2InternalSubset; hdlr->externalSubset = xmlSAX2ExternalSubset; hdlr->isStandalone = xmlSAX2IsStandalone; hdlr->hasInternalSubset = xmlSAX2HasInternalSubset; hdlr->hasExternalSubset = xmlSAX2HasExternalSubset; hdlr->resolveEntity = xmlSAX2ResolveEntity; hdlr->getEntity = xmlSAX2GetEntity; hdlr->getParameterEntity = xmlSAX2GetParameterEntity; hdlr->entityDecl = xmlSAX2EntityDecl; hdlr->attributeDecl = xmlSAX2AttributeDecl; hdlr->elementDecl = xmlSAX2ElementDecl; hdlr->notationDecl = xmlSAX2NotationDecl; hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl; hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator; hdlr->startDocument = xmlSAX2StartDocument; hdlr->endDocument = xmlSAX2EndDocument; hdlr->reference = xmlSAX2Reference; hdlr->characters = xmlSAX2Characters; hdlr->cdataBlock = xmlSAX2CDataBlock; hdlr->ignorableWhitespace = xmlSAX2Characters; hdlr->processingInstruction = xmlSAX2ProcessingInstruction; hdlr->comment = xmlSAX2Comment; hdlr->warning = xmlParserWarning; hdlr->error = xmlParserError; hdlr->fatalError = xmlParserError;
return(0);}
/** * xmlSAX2InitDefaultSAXHandler: * @hdlr: the SAX handler * @warning: flag if non-zero sets the handler warning procedure * * Initialize the default XML SAX2 handler */voidxmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning){ if ((hdlr == NULL) || (hdlr->initialized != 0)) return;
xmlSAXVersion(hdlr, xmlSAX2DefaultVersionValue); if (warning == 0) hdlr->warning = NULL; else hdlr->warning = xmlParserWarning;}
/** * xmlDefaultSAXHandlerInit: * * DEPRECATED: This function is a no-op. Call xmlInitParser to * initialize the library. * * Initialize the default SAX2 handler */voidxmlDefaultSAXHandlerInit(void){}
#ifdef LIBXML_HTML_ENABLED
/** * xmlSAX2InitHtmlDefaultSAXHandler: * @hdlr: the SAX handler * * Initialize the default HTML SAX2 handler */voidxmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr){ if ((hdlr == NULL) || (hdlr->initialized != 0)) return;
hdlr->internalSubset = xmlSAX2InternalSubset; hdlr->externalSubset = NULL; hdlr->isStandalone = NULL; hdlr->hasInternalSubset = NULL; hdlr->hasExternalSubset = NULL; hdlr->resolveEntity = NULL; hdlr->getEntity = xmlSAX2GetEntity; hdlr->getParameterEntity = NULL; hdlr->entityDecl = NULL; hdlr->attributeDecl = NULL; hdlr->elementDecl = NULL; hdlr->notationDecl = NULL; hdlr->unparsedEntityDecl = NULL; hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator; hdlr->startDocument = xmlSAX2StartDocument; hdlr->endDocument = xmlSAX2EndDocument; hdlr->startElement = xmlSAX2StartElement; hdlr->endElement = xmlSAX2EndElement; hdlr->reference = NULL; hdlr->characters = xmlSAX2Characters; hdlr->cdataBlock = xmlSAX2CDataBlock; hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace; hdlr->processingInstruction = xmlSAX2ProcessingInstruction; hdlr->comment = xmlSAX2Comment; hdlr->warning = xmlParserWarning; hdlr->error = xmlParserError; hdlr->fatalError = xmlParserError;
hdlr->initialized = 1;}
/** * htmlDefaultSAXHandlerInit: * * DEPRECATED: This function is a no-op. Call xmlInitParser to * initialize the library. */voidhtmlDefaultSAXHandlerInit(void){}
#endif /* LIBXML_HTML_ENABLED */