SAX2.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036
  1. /*
  2. * SAX2.c : Default SAX2 handler to build a tree.
  3. *
  4. * See Copyright for the status of this software.
  5. *
  6. * Daniel Veillard <daniel@veillard.com>
  7. */
  8. #define IN_LIBXML
  9. #include "libxml.h"
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <limits.h>
  13. #include <stddef.h>
  14. #include <libxml/xmlmemory.h>
  15. #include <libxml/tree.h>
  16. #include <libxml/parser.h>
  17. #include <libxml/parserInternals.h>
  18. #include <libxml/valid.h>
  19. #include <libxml/entities.h>
  20. #include <libxml/xmlerror.h>
  21. #include <libxml/debugXML.h>
  22. #include <libxml/xmlIO.h>
  23. #include <libxml/SAX.h>
  24. #include <libxml/uri.h>
  25. #include <libxml/valid.h>
  26. #include <libxml/HTMLtree.h>
  27. #include <libxml/globals.h>
  28. /* Define SIZE_T_MAX unless defined through <limits.h>. */
  29. #ifndef SIZE_T_MAX
  30. # define SIZE_T_MAX ((size_t)-1)
  31. #endif /* !SIZE_T_MAX */
  32. /* #define DEBUG_SAX2 */
  33. /* #define DEBUG_SAX2_TREE */
  34. /**
  35. * TODO:
  36. *
  37. * macro to flag unimplemented blocks
  38. * XML_CATALOG_PREFER user env to select between system/public preferred
  39. * option. C.f. Richard Tobin <richard@cogsci.ed.ac.uk>
  40. *> Just FYI, I am using an environment variable XML_CATALOG_PREFER with
  41. *> values "system" and "public". I have made the default be "system" to
  42. *> match yours.
  43. */
  44. #define TODO \
  45. xmlGenericError(xmlGenericErrorContext, \
  46. "Unimplemented block at %s:%d\n", \
  47. __FILE__, __LINE__);
  48. /*
  49. * xmlSAX2ErrMemory:
  50. * @ctxt: an XML validation parser context
  51. * @msg: a string to accompany the error message
  52. */
  53. static void LIBXML_ATTR_FORMAT(2,0)
  54. xmlSAX2ErrMemory(xmlParserCtxtPtr ctxt, const char *msg) {
  55. xmlStructuredErrorFunc schannel = NULL;
  56. const char *str1 = "out of memory\n";
  57. if (ctxt != NULL) {
  58. ctxt->errNo = XML_ERR_NO_MEMORY;
  59. if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
  60. schannel = ctxt->sax->serror;
  61. __xmlRaiseError(schannel,
  62. ctxt->vctxt.error, ctxt->vctxt.userData,
  63. ctxt, NULL, XML_FROM_PARSER, XML_ERR_NO_MEMORY,
  64. XML_ERR_ERROR, NULL, 0, (const char *) str1,
  65. NULL, NULL, 0, 0,
  66. msg, (const char *) str1, NULL);
  67. ctxt->errNo = XML_ERR_NO_MEMORY;
  68. ctxt->instate = XML_PARSER_EOF;
  69. ctxt->disableSAX = 1;
  70. } else {
  71. __xmlRaiseError(schannel,
  72. NULL, NULL,
  73. ctxt, NULL, XML_FROM_PARSER, XML_ERR_NO_MEMORY,
  74. XML_ERR_ERROR, NULL, 0, (const char *) str1,
  75. NULL, NULL, 0, 0,
  76. msg, (const char *) str1, NULL);
  77. }
  78. }
  79. /**
  80. * xmlValidError:
  81. * @ctxt: an XML validation parser context
  82. * @error: the error number
  83. * @msg: the error message
  84. * @str1: extra data
  85. * @str2: extra data
  86. *
  87. * Handle a validation error
  88. */
  89. static void LIBXML_ATTR_FORMAT(3,0)
  90. xmlErrValid(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  91. const char *msg, const char *str1, const char *str2)
  92. {
  93. xmlStructuredErrorFunc schannel = NULL;
  94. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  95. (ctxt->instate == XML_PARSER_EOF))
  96. return;
  97. if (ctxt != NULL) {
  98. ctxt->errNo = error;
  99. if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
  100. schannel = ctxt->sax->serror;
  101. __xmlRaiseError(schannel,
  102. ctxt->vctxt.error, ctxt->vctxt.userData,
  103. ctxt, NULL, XML_FROM_DTD, error,
  104. XML_ERR_ERROR, NULL, 0, (const char *) str1,
  105. (const char *) str2, NULL, 0, 0,
  106. msg, (const char *) str1, (const char *) str2);
  107. ctxt->valid = 0;
  108. } else {
  109. __xmlRaiseError(schannel,
  110. NULL, NULL,
  111. ctxt, NULL, XML_FROM_DTD, error,
  112. XML_ERR_ERROR, NULL, 0, (const char *) str1,
  113. (const char *) str2, NULL, 0, 0,
  114. msg, (const char *) str1, (const char *) str2);
  115. }
  116. }
  117. /**
  118. * xmlFatalErrMsg:
  119. * @ctxt: an XML parser context
  120. * @error: the error number
  121. * @msg: the error message
  122. * @str1: an error string
  123. * @str2: an error string
  124. *
  125. * Handle a fatal parser error, i.e. violating Well-Formedness constraints
  126. */
  127. static void LIBXML_ATTR_FORMAT(3,0)
  128. xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  129. const char *msg, const xmlChar *str1, const xmlChar *str2)
  130. {
  131. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  132. (ctxt->instate == XML_PARSER_EOF))
  133. return;
  134. if (ctxt != NULL)
  135. ctxt->errNo = error;
  136. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
  137. XML_ERR_FATAL, NULL, 0,
  138. (const char *) str1, (const char *) str2,
  139. NULL, 0, 0, msg, str1, str2);
  140. if (ctxt != NULL) {
  141. ctxt->wellFormed = 0;
  142. ctxt->valid = 0;
  143. if (ctxt->recovery == 0)
  144. ctxt->disableSAX = 1;
  145. }
  146. }
  147. /**
  148. * xmlWarnMsg:
  149. * @ctxt: an XML parser context
  150. * @error: the error number
  151. * @msg: the error message
  152. * @str1: an error string
  153. * @str2: an error string
  154. *
  155. * Handle a parser warning
  156. */
  157. static void LIBXML_ATTR_FORMAT(3,0)
  158. xmlWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  159. const char *msg, const xmlChar *str1)
  160. {
  161. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  162. (ctxt->instate == XML_PARSER_EOF))
  163. return;
  164. if (ctxt != NULL)
  165. ctxt->errNo = error;
  166. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
  167. XML_ERR_WARNING, NULL, 0,
  168. (const char *) str1, NULL,
  169. NULL, 0, 0, msg, str1);
  170. }
  171. /**
  172. * xmlNsErrMsg:
  173. * @ctxt: an XML parser context
  174. * @error: the error number
  175. * @msg: the error message
  176. * @str1: an error string
  177. * @str2: an error string
  178. *
  179. * Handle a namespace error
  180. */
  181. static void LIBXML_ATTR_FORMAT(3,0)
  182. xmlNsErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  183. const char *msg, const xmlChar *str1, const xmlChar *str2)
  184. {
  185. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  186. (ctxt->instate == XML_PARSER_EOF))
  187. return;
  188. if (ctxt != NULL)
  189. ctxt->errNo = error;
  190. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
  191. XML_ERR_ERROR, NULL, 0,
  192. (const char *) str1, (const char *) str2,
  193. NULL, 0, 0, msg, str1, str2);
  194. }
  195. /**
  196. * xmlNsWarnMsg:
  197. * @ctxt: an XML parser context
  198. * @error: the error number
  199. * @msg: the error message
  200. * @str1: an error string
  201. *
  202. * Handle a namespace warning
  203. */
  204. static void LIBXML_ATTR_FORMAT(3,0)
  205. xmlNsWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  206. const char *msg, const xmlChar *str1, const xmlChar *str2)
  207. {
  208. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  209. (ctxt->instate == XML_PARSER_EOF))
  210. return;
  211. if (ctxt != NULL)
  212. ctxt->errNo = error;
  213. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
  214. XML_ERR_WARNING, NULL, 0,
  215. (const char *) str1, (const char *) str2,
  216. NULL, 0, 0, msg, str1, str2);
  217. }
  218. /**
  219. * xmlSAX2GetPublicId:
  220. * @ctx: the user data (XML parser context)
  221. *
  222. * Provides the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN"
  223. *
  224. * Returns a xmlChar *
  225. */
  226. const xmlChar *
  227. xmlSAX2GetPublicId(void *ctx ATTRIBUTE_UNUSED)
  228. {
  229. /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
  230. return(NULL);
  231. }
  232. /**
  233. * xmlSAX2GetSystemId:
  234. * @ctx: the user data (XML parser context)
  235. *
  236. * Provides the system ID, basically URL or filename e.g.
  237. * http://www.sgmlsource.com/dtds/memo.dtd
  238. *
  239. * Returns a xmlChar *
  240. */
  241. const xmlChar *
  242. xmlSAX2GetSystemId(void *ctx)
  243. {
  244. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  245. if ((ctx == NULL) || (ctxt->input == NULL)) return(NULL);
  246. return((const xmlChar *) ctxt->input->filename);
  247. }
  248. /**
  249. * xmlSAX2GetLineNumber:
  250. * @ctx: the user data (XML parser context)
  251. *
  252. * Provide the line number of the current parsing point.
  253. *
  254. * Returns an int
  255. */
  256. int
  257. xmlSAX2GetLineNumber(void *ctx)
  258. {
  259. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  260. if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
  261. return(ctxt->input->line);
  262. }
  263. /**
  264. * xmlSAX2GetColumnNumber:
  265. * @ctx: the user data (XML parser context)
  266. *
  267. * Provide the column number of the current parsing point.
  268. *
  269. * Returns an int
  270. */
  271. int
  272. xmlSAX2GetColumnNumber(void *ctx)
  273. {
  274. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  275. if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
  276. return(ctxt->input->col);
  277. }
  278. /**
  279. * xmlSAX2IsStandalone:
  280. * @ctx: the user data (XML parser context)
  281. *
  282. * Is this document tagged standalone ?
  283. *
  284. * Returns 1 if true
  285. */
  286. int
  287. xmlSAX2IsStandalone(void *ctx)
  288. {
  289. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  290. if ((ctx == NULL) || (ctxt->myDoc == NULL)) return(0);
  291. return(ctxt->myDoc->standalone == 1);
  292. }
  293. /**
  294. * xmlSAX2HasInternalSubset:
  295. * @ctx: the user data (XML parser context)
  296. *
  297. * Does this document has an internal subset
  298. *
  299. * Returns 1 if true
  300. */
  301. int
  302. xmlSAX2HasInternalSubset(void *ctx)
  303. {
  304. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  305. if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
  306. return(ctxt->myDoc->intSubset != NULL);
  307. }
  308. /**
  309. * xmlSAX2HasExternalSubset:
  310. * @ctx: the user data (XML parser context)
  311. *
  312. * Does this document has an external subset
  313. *
  314. * Returns 1 if true
  315. */
  316. int
  317. xmlSAX2HasExternalSubset(void *ctx)
  318. {
  319. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  320. if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
  321. return(ctxt->myDoc->extSubset != NULL);
  322. }
  323. /**
  324. * xmlSAX2InternalSubset:
  325. * @ctx: the user data (XML parser context)
  326. * @name: the root element name
  327. * @ExternalID: the external ID
  328. * @SystemID: the SYSTEM ID (e.g. filename or URL)
  329. *
  330. * Callback on internal subset declaration.
  331. */
  332. void
  333. xmlSAX2InternalSubset(void *ctx, const xmlChar *name,
  334. const xmlChar *ExternalID, const xmlChar *SystemID)
  335. {
  336. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  337. xmlDtdPtr dtd;
  338. if (ctx == NULL) return;
  339. #ifdef DEBUG_SAX
  340. xmlGenericError(xmlGenericErrorContext,
  341. "SAX.xmlSAX2InternalSubset(%s, %s, %s)\n",
  342. name, ExternalID, SystemID);
  343. #endif
  344. if (ctxt->myDoc == NULL)
  345. return;
  346. dtd = xmlGetIntSubset(ctxt->myDoc);
  347. if (dtd != NULL) {
  348. if (ctxt->html)
  349. return;
  350. xmlUnlinkNode((xmlNodePtr) dtd);
  351. xmlFreeDtd(dtd);
  352. ctxt->myDoc->intSubset = NULL;
  353. }
  354. ctxt->myDoc->intSubset =
  355. xmlCreateIntSubset(ctxt->myDoc, name, ExternalID, SystemID);
  356. if (ctxt->myDoc->intSubset == NULL)
  357. xmlSAX2ErrMemory(ctxt, "xmlSAX2InternalSubset");
  358. }
  359. /**
  360. * xmlSAX2ExternalSubset:
  361. * @ctx: the user data (XML parser context)
  362. * @name: the root element name
  363. * @ExternalID: the external ID
  364. * @SystemID: the SYSTEM ID (e.g. filename or URL)
  365. *
  366. * Callback on external subset declaration.
  367. */
  368. void
  369. xmlSAX2ExternalSubset(void *ctx, const xmlChar *name,
  370. const xmlChar *ExternalID, const xmlChar *SystemID)
  371. {
  372. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  373. if (ctx == NULL) return;
  374. #ifdef DEBUG_SAX
  375. xmlGenericError(xmlGenericErrorContext,
  376. "SAX.xmlSAX2ExternalSubset(%s, %s, %s)\n",
  377. name, ExternalID, SystemID);
  378. #endif
  379. if (((ExternalID != NULL) || (SystemID != NULL)) &&
  380. (((ctxt->validate) || (ctxt->loadsubset != 0)) &&
  381. (ctxt->wellFormed && ctxt->myDoc))) {
  382. /*
  383. * Try to fetch and parse the external subset.
  384. */
  385. xmlParserInputPtr oldinput;
  386. int oldinputNr;
  387. int oldinputMax;
  388. xmlParserInputPtr *oldinputTab;
  389. xmlParserInputPtr input = NULL;
  390. xmlCharEncoding enc;
  391. int oldcharset;
  392. const xmlChar *oldencoding;
  393. /*
  394. * Ask the Entity resolver to load the damn thing
  395. */
  396. if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
  397. input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID,
  398. SystemID);
  399. if (input == NULL) {
  400. return;
  401. }
  402. xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID);
  403. /*
  404. * make sure we won't destroy the main document context
  405. */
  406. oldinput = ctxt->input;
  407. oldinputNr = ctxt->inputNr;
  408. oldinputMax = ctxt->inputMax;
  409. oldinputTab = ctxt->inputTab;
  410. oldcharset = ctxt->charset;
  411. oldencoding = ctxt->encoding;
  412. ctxt->encoding = NULL;
  413. ctxt->inputTab = (xmlParserInputPtr *)
  414. xmlMalloc(5 * sizeof(xmlParserInputPtr));
  415. if (ctxt->inputTab == NULL) {
  416. xmlSAX2ErrMemory(ctxt, "xmlSAX2ExternalSubset");
  417. ctxt->input = oldinput;
  418. ctxt->inputNr = oldinputNr;
  419. ctxt->inputMax = oldinputMax;
  420. ctxt->inputTab = oldinputTab;
  421. ctxt->charset = oldcharset;
  422. ctxt->encoding = oldencoding;
  423. return;
  424. }
  425. ctxt->inputNr = 0;
  426. ctxt->inputMax = 5;
  427. ctxt->input = NULL;
  428. xmlPushInput(ctxt, input);
  429. /*
  430. * On the fly encoding conversion if needed
  431. */
  432. if (ctxt->input->length >= 4) {
  433. enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
  434. xmlSwitchEncoding(ctxt, enc);
  435. }
  436. if (input->filename == NULL)
  437. input->filename = (char *) xmlCanonicPath(SystemID);
  438. input->line = 1;
  439. input->col = 1;
  440. input->base = ctxt->input->cur;
  441. input->cur = ctxt->input->cur;
  442. input->free = NULL;
  443. /*
  444. * let's parse that entity knowing it's an external subset.
  445. */
  446. xmlParseExternalSubset(ctxt, ExternalID, SystemID);
  447. /*
  448. * Free up the external entities
  449. */
  450. while (ctxt->inputNr > 1)
  451. xmlPopInput(ctxt);
  452. xmlFreeInputStream(ctxt->input);
  453. xmlFree(ctxt->inputTab);
  454. /*
  455. * Restore the parsing context of the main entity
  456. */
  457. ctxt->input = oldinput;
  458. ctxt->inputNr = oldinputNr;
  459. ctxt->inputMax = oldinputMax;
  460. ctxt->inputTab = oldinputTab;
  461. ctxt->charset = oldcharset;
  462. if ((ctxt->encoding != NULL) &&
  463. ((ctxt->dict == NULL) ||
  464. (!xmlDictOwns(ctxt->dict, ctxt->encoding))))
  465. xmlFree((xmlChar *) ctxt->encoding);
  466. ctxt->encoding = oldencoding;
  467. /* ctxt->wellFormed = oldwellFormed; */
  468. }
  469. }
  470. /**
  471. * xmlSAX2ResolveEntity:
  472. * @ctx: the user data (XML parser context)
  473. * @publicId: The public ID of the entity
  474. * @systemId: The system ID of the entity
  475. *
  476. * The entity loader, to control the loading of external entities,
  477. * the application can either:
  478. * - override this xmlSAX2ResolveEntity() callback in the SAX block
  479. * - or better use the xmlSetExternalEntityLoader() function to
  480. * set up it's own entity resolution routine
  481. *
  482. * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
  483. */
  484. xmlParserInputPtr
  485. xmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
  486. {
  487. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  488. xmlParserInputPtr ret;
  489. xmlChar *URI;
  490. const char *base = NULL;
  491. if (ctx == NULL) return(NULL);
  492. if (ctxt->input != NULL)
  493. base = ctxt->input->filename;
  494. if (base == NULL)
  495. base = ctxt->directory;
  496. URI = xmlBuildURI(systemId, (const xmlChar *) base);
  497. #ifdef DEBUG_SAX
  498. xmlGenericError(xmlGenericErrorContext,
  499. "SAX.xmlSAX2ResolveEntity(%s, %s)\n", publicId, systemId);
  500. #endif
  501. ret = xmlLoadExternalEntity((const char *) URI,
  502. (const char *) publicId, ctxt);
  503. if (URI != NULL)
  504. xmlFree(URI);
  505. return(ret);
  506. }
  507. /**
  508. * xmlSAX2GetEntity:
  509. * @ctx: the user data (XML parser context)
  510. * @name: The entity name
  511. *
  512. * Get an entity by name
  513. *
  514. * Returns the xmlEntityPtr if found.
  515. */
  516. xmlEntityPtr
  517. xmlSAX2GetEntity(void *ctx, const xmlChar *name)
  518. {
  519. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  520. xmlEntityPtr ret = NULL;
  521. if (ctx == NULL) return(NULL);
  522. #ifdef DEBUG_SAX
  523. xmlGenericError(xmlGenericErrorContext,
  524. "SAX.xmlSAX2GetEntity(%s)\n", name);
  525. #endif
  526. if (ctxt->inSubset == 0) {
  527. ret = xmlGetPredefinedEntity(name);
  528. if (ret != NULL)
  529. return(ret);
  530. }
  531. if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) {
  532. if (ctxt->inSubset == 2) {
  533. ctxt->myDoc->standalone = 0;
  534. ret = xmlGetDocEntity(ctxt->myDoc, name);
  535. ctxt->myDoc->standalone = 1;
  536. } else {
  537. ret = xmlGetDocEntity(ctxt->myDoc, name);
  538. if (ret == NULL) {
  539. ctxt->myDoc->standalone = 0;
  540. ret = xmlGetDocEntity(ctxt->myDoc, name);
  541. if (ret != NULL) {
  542. xmlFatalErrMsg(ctxt, XML_ERR_NOT_STANDALONE,
  543. "Entity(%s) document marked standalone but requires external subset\n",
  544. name, NULL);
  545. }
  546. ctxt->myDoc->standalone = 1;
  547. }
  548. }
  549. } else {
  550. ret = xmlGetDocEntity(ctxt->myDoc, name);
  551. }
  552. return(ret);
  553. }
  554. /**
  555. * xmlSAX2GetParameterEntity:
  556. * @ctx: the user data (XML parser context)
  557. * @name: The entity name
  558. *
  559. * Get a parameter entity by name
  560. *
  561. * Returns the xmlEntityPtr if found.
  562. */
  563. xmlEntityPtr
  564. xmlSAX2GetParameterEntity(void *ctx, const xmlChar *name)
  565. {
  566. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  567. xmlEntityPtr ret;
  568. if (ctx == NULL) return(NULL);
  569. #ifdef DEBUG_SAX
  570. xmlGenericError(xmlGenericErrorContext,
  571. "SAX.xmlSAX2GetParameterEntity(%s)\n", name);
  572. #endif
  573. ret = xmlGetParameterEntity(ctxt->myDoc, name);
  574. return(ret);
  575. }
  576. /**
  577. * xmlSAX2EntityDecl:
  578. * @ctx: the user data (XML parser context)
  579. * @name: the entity name
  580. * @type: the entity type
  581. * @publicId: The public ID of the entity
  582. * @systemId: The system ID of the entity
  583. * @content: the entity value (without processing).
  584. *
  585. * An entity definition has been parsed
  586. */
  587. void
  588. xmlSAX2EntityDecl(void *ctx, const xmlChar *name, int type,
  589. const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
  590. {
  591. xmlEntityPtr ent;
  592. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  593. if (ctx == NULL) return;
  594. #ifdef DEBUG_SAX
  595. xmlGenericError(xmlGenericErrorContext,
  596. "SAX.xmlSAX2EntityDecl(%s, %d, %s, %s, %s)\n",
  597. name, type, publicId, systemId, content);
  598. #endif
  599. if (ctxt->inSubset == 1) {
  600. ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
  601. systemId, content);
  602. if ((ent == NULL) && (ctxt->pedantic))
  603. xmlWarnMsg(ctxt, XML_WAR_ENTITY_REDEFINED,
  604. "Entity(%s) already defined in the internal subset\n",
  605. name);
  606. if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
  607. xmlChar *URI;
  608. const char *base = NULL;
  609. if (ctxt->input != NULL)
  610. base = ctxt->input->filename;
  611. if (base == NULL)
  612. base = ctxt->directory;
  613. URI = xmlBuildURI(systemId, (const xmlChar *) base);
  614. ent->URI = URI;
  615. }
  616. } else if (ctxt->inSubset == 2) {
  617. ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
  618. systemId, content);
  619. if ((ent == NULL) && (ctxt->pedantic) &&
  620. (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
  621. ctxt->sax->warning(ctxt->userData,
  622. "Entity(%s) already defined in the external subset\n", name);
  623. if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
  624. xmlChar *URI;
  625. const char *base = NULL;
  626. if (ctxt->input != NULL)
  627. base = ctxt->input->filename;
  628. if (base == NULL)
  629. base = ctxt->directory;
  630. URI = xmlBuildURI(systemId, (const xmlChar *) base);
  631. ent->URI = URI;
  632. }
  633. } else {
  634. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
  635. "SAX.xmlSAX2EntityDecl(%s) called while not in subset\n",
  636. name, NULL);
  637. }
  638. }
  639. /**
  640. * xmlSAX2AttributeDecl:
  641. * @ctx: the user data (XML parser context)
  642. * @elem: the name of the element
  643. * @fullname: the attribute name
  644. * @type: the attribute type
  645. * @def: the type of default value
  646. * @defaultValue: the attribute default value
  647. * @tree: the tree of enumerated value set
  648. *
  649. * An attribute definition has been parsed
  650. */
  651. void
  652. xmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
  653. int type, int def, const xmlChar *defaultValue,
  654. xmlEnumerationPtr tree)
  655. {
  656. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  657. xmlAttributePtr attr;
  658. xmlChar *name = NULL, *prefix = NULL;
  659. if ((ctxt == NULL) || (ctxt->myDoc == NULL))
  660. return;
  661. #ifdef DEBUG_SAX
  662. xmlGenericError(xmlGenericErrorContext,
  663. "SAX.xmlSAX2AttributeDecl(%s, %s, %d, %d, %s, ...)\n",
  664. elem, fullname, type, def, defaultValue);
  665. #endif
  666. if ((xmlStrEqual(fullname, BAD_CAST "xml:id")) &&
  667. (type != XML_ATTRIBUTE_ID)) {
  668. /*
  669. * Raise the error but keep the validity flag
  670. */
  671. int tmp = ctxt->valid;
  672. xmlErrValid(ctxt, XML_DTD_XMLID_TYPE,
  673. "xml:id : attribute type should be ID\n", NULL, NULL);
  674. ctxt->valid = tmp;
  675. }
  676. /* TODO: optimize name/prefix allocation */
  677. name = xmlSplitQName(ctxt, fullname, &prefix);
  678. ctxt->vctxt.valid = 1;
  679. if (ctxt->inSubset == 1)
  680. attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
  681. name, prefix, (xmlAttributeType) type,
  682. (xmlAttributeDefault) def, defaultValue, tree);
  683. else if (ctxt->inSubset == 2)
  684. attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
  685. name, prefix, (xmlAttributeType) type,
  686. (xmlAttributeDefault) def, defaultValue, tree);
  687. else {
  688. xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
  689. "SAX.xmlSAX2AttributeDecl(%s) called while not in subset\n",
  690. name, NULL);
  691. xmlFreeEnumeration(tree);
  692. return;
  693. }
  694. #ifdef LIBXML_VALID_ENABLED
  695. if (ctxt->vctxt.valid == 0)
  696. ctxt->valid = 0;
  697. if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
  698. (ctxt->myDoc->intSubset != NULL))
  699. ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
  700. attr);
  701. #endif /* LIBXML_VALID_ENABLED */
  702. if (prefix != NULL)
  703. xmlFree(prefix);
  704. if (name != NULL)
  705. xmlFree(name);
  706. }
  707. /**
  708. * xmlSAX2ElementDecl:
  709. * @ctx: the user data (XML parser context)
  710. * @name: the element name
  711. * @type: the element type
  712. * @content: the element value tree
  713. *
  714. * An element definition has been parsed
  715. */
  716. void
  717. xmlSAX2ElementDecl(void *ctx, const xmlChar * name, int type,
  718. xmlElementContentPtr content)
  719. {
  720. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  721. xmlElementPtr elem = NULL;
  722. if ((ctxt == NULL) || (ctxt->myDoc == NULL))
  723. return;
  724. #ifdef DEBUG_SAX
  725. xmlGenericError(xmlGenericErrorContext,
  726. "SAX.xmlSAX2ElementDecl(%s, %d, ...)\n", name, type);
  727. #endif
  728. if (ctxt->inSubset == 1)
  729. elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
  730. name, (xmlElementTypeVal) type, content);
  731. else if (ctxt->inSubset == 2)
  732. elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
  733. name, (xmlElementTypeVal) type, content);
  734. else {
  735. xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
  736. "SAX.xmlSAX2ElementDecl(%s) called while not in subset\n",
  737. name, NULL);
  738. return;
  739. }
  740. #ifdef LIBXML_VALID_ENABLED
  741. if (elem == NULL)
  742. ctxt->valid = 0;
  743. if (ctxt->validate && ctxt->wellFormed &&
  744. ctxt->myDoc && ctxt->myDoc->intSubset)
  745. ctxt->valid &=
  746. xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
  747. #endif /* LIBXML_VALID_ENABLED */
  748. }
  749. /**
  750. * xmlSAX2NotationDecl:
  751. * @ctx: the user data (XML parser context)
  752. * @name: The name of the notation
  753. * @publicId: The public ID of the entity
  754. * @systemId: The system ID of the entity
  755. *
  756. * What to do when a notation declaration has been parsed.
  757. */
  758. void
  759. xmlSAX2NotationDecl(void *ctx, const xmlChar *name,
  760. const xmlChar *publicId, const xmlChar *systemId)
  761. {
  762. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  763. xmlNotationPtr nota = NULL;
  764. if ((ctxt == NULL) || (ctxt->myDoc == NULL))
  765. return;
  766. #ifdef DEBUG_SAX
  767. xmlGenericError(xmlGenericErrorContext,
  768. "SAX.xmlSAX2NotationDecl(%s, %s, %s)\n", name, publicId, systemId);
  769. #endif
  770. if ((publicId == NULL) && (systemId == NULL)) {
  771. xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
  772. "SAX.xmlSAX2NotationDecl(%s) externalID or PublicID missing\n",
  773. name, NULL);
  774. return;
  775. } else if (ctxt->inSubset == 1)
  776. nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
  777. publicId, systemId);
  778. else if (ctxt->inSubset == 2)
  779. nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
  780. publicId, systemId);
  781. else {
  782. xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
  783. "SAX.xmlSAX2NotationDecl(%s) called while not in subset\n",
  784. name, NULL);
  785. return;
  786. }
  787. #ifdef LIBXML_VALID_ENABLED
  788. if (nota == NULL) ctxt->valid = 0;
  789. if ((ctxt->validate) && (ctxt->wellFormed) &&
  790. (ctxt->myDoc->intSubset != NULL))
  791. ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
  792. nota);
  793. #endif /* LIBXML_VALID_ENABLED */
  794. }
  795. /**
  796. * xmlSAX2UnparsedEntityDecl:
  797. * @ctx: the user data (XML parser context)
  798. * @name: The name of the entity
  799. * @publicId: The public ID of the entity
  800. * @systemId: The system ID of the entity
  801. * @notationName: the name of the notation
  802. *
  803. * What to do when an unparsed entity declaration is parsed
  804. */
  805. void
  806. xmlSAX2UnparsedEntityDecl(void *ctx, const xmlChar *name,
  807. const xmlChar *publicId, const xmlChar *systemId,
  808. const xmlChar *notationName)
  809. {
  810. xmlEntityPtr ent;
  811. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  812. if (ctx == NULL) return;
  813. #ifdef DEBUG_SAX
  814. xmlGenericError(xmlGenericErrorContext,
  815. "SAX.xmlSAX2UnparsedEntityDecl(%s, %s, %s, %s)\n",
  816. name, publicId, systemId, notationName);
  817. #endif
  818. if (ctxt->inSubset == 1) {
  819. ent = xmlAddDocEntity(ctxt->myDoc, name,
  820. XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
  821. publicId, systemId, notationName);
  822. if ((ent == NULL) && (ctxt->pedantic) &&
  823. (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
  824. ctxt->sax->warning(ctxt->userData,
  825. "Entity(%s) already defined in the internal subset\n", name);
  826. if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
  827. xmlChar *URI;
  828. const char *base = NULL;
  829. if (ctxt->input != NULL)
  830. base = ctxt->input->filename;
  831. if (base == NULL)
  832. base = ctxt->directory;
  833. URI = xmlBuildURI(systemId, (const xmlChar *) base);
  834. ent->URI = URI;
  835. }
  836. } else if (ctxt->inSubset == 2) {
  837. ent = xmlAddDtdEntity(ctxt->myDoc, name,
  838. XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
  839. publicId, systemId, notationName);
  840. if ((ent == NULL) && (ctxt->pedantic) &&
  841. (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
  842. ctxt->sax->warning(ctxt->userData,
  843. "Entity(%s) already defined in the external subset\n", name);
  844. if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
  845. xmlChar *URI;
  846. const char *base = NULL;
  847. if (ctxt->input != NULL)
  848. base = ctxt->input->filename;
  849. if (base == NULL)
  850. base = ctxt->directory;
  851. URI = xmlBuildURI(systemId, (const xmlChar *) base);
  852. ent->URI = URI;
  853. }
  854. } else {
  855. xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
  856. "SAX.xmlSAX2UnparsedEntityDecl(%s) called while not in subset\n",
  857. name, NULL);
  858. }
  859. }
  860. /**
  861. * xmlSAX2SetDocumentLocator:
  862. * @ctx: the user data (XML parser context)
  863. * @loc: A SAX Locator
  864. *
  865. * Receive the document locator at startup, actually xmlDefaultSAXLocator
  866. * Everything is available on the context, so this is useless in our case.
  867. */
  868. void
  869. xmlSAX2SetDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
  870. {
  871. /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
  872. #ifdef DEBUG_SAX
  873. xmlGenericError(xmlGenericErrorContext,
  874. "SAX.xmlSAX2SetDocumentLocator()\n");
  875. #endif
  876. }
  877. /**
  878. * xmlSAX2StartDocument:
  879. * @ctx: the user data (XML parser context)
  880. *
  881. * called when the document start being processed.
  882. */
  883. void
  884. xmlSAX2StartDocument(void *ctx)
  885. {
  886. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  887. xmlDocPtr doc;
  888. if (ctx == NULL) return;
  889. #ifdef DEBUG_SAX
  890. xmlGenericError(xmlGenericErrorContext,
  891. "SAX.xmlSAX2StartDocument()\n");
  892. #endif
  893. if (ctxt->html) {
  894. #ifdef LIBXML_HTML_ENABLED
  895. if (ctxt->myDoc == NULL)
  896. ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
  897. if (ctxt->myDoc == NULL) {
  898. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
  899. return;
  900. }
  901. ctxt->myDoc->properties = XML_DOC_HTML;
  902. ctxt->myDoc->parseFlags = ctxt->options;
  903. #else
  904. xmlGenericError(xmlGenericErrorContext,
  905. "libxml2 built without HTML support\n");
  906. ctxt->errNo = XML_ERR_INTERNAL_ERROR;
  907. ctxt->instate = XML_PARSER_EOF;
  908. ctxt->disableSAX = 1;
  909. return;
  910. #endif
  911. } else {
  912. doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
  913. if (doc != NULL) {
  914. doc->properties = 0;
  915. if (ctxt->options & XML_PARSE_OLD10)
  916. doc->properties |= XML_DOC_OLD10;
  917. doc->parseFlags = ctxt->options;
  918. if (ctxt->encoding != NULL)
  919. doc->encoding = xmlStrdup(ctxt->encoding);
  920. else
  921. doc->encoding = NULL;
  922. doc->standalone = ctxt->standalone;
  923. } else {
  924. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
  925. return;
  926. }
  927. if ((ctxt->dictNames) && (doc != NULL)) {
  928. doc->dict = ctxt->dict;
  929. xmlDictReference(doc->dict);
  930. }
  931. }
  932. if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
  933. (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
  934. ctxt->myDoc->URL = xmlPathToURI((const xmlChar *)ctxt->input->filename);
  935. if (ctxt->myDoc->URL == NULL)
  936. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
  937. }
  938. }
  939. /**
  940. * xmlSAX2EndDocument:
  941. * @ctx: the user data (XML parser context)
  942. *
  943. * called when the document end has been detected.
  944. */
  945. void
  946. xmlSAX2EndDocument(void *ctx)
  947. {
  948. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  949. #ifdef DEBUG_SAX
  950. xmlGenericError(xmlGenericErrorContext,
  951. "SAX.xmlSAX2EndDocument()\n");
  952. #endif
  953. if (ctx == NULL) return;
  954. #ifdef LIBXML_VALID_ENABLED
  955. if (ctxt->validate && ctxt->wellFormed &&
  956. ctxt->myDoc && ctxt->myDoc->intSubset)
  957. ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
  958. #endif /* LIBXML_VALID_ENABLED */
  959. /*
  960. * Grab the encoding if it was added on-the-fly
  961. */
  962. if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
  963. (ctxt->myDoc->encoding == NULL)) {
  964. ctxt->myDoc->encoding = ctxt->encoding;
  965. ctxt->encoding = NULL;
  966. }
  967. if ((ctxt->inputTab != NULL) &&
  968. (ctxt->inputNr > 0) && (ctxt->inputTab[0] != NULL) &&
  969. (ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
  970. (ctxt->myDoc->encoding == NULL)) {
  971. ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
  972. }
  973. if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
  974. (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
  975. ctxt->myDoc->charset = ctxt->charset;
  976. }
  977. }
  978. #if defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_DOCB_ENABLED) || defined(LIBXML_LEGACY_ENABLED)
  979. /**
  980. * xmlSAX2AttributeInternal:
  981. * @ctx: the user data (XML parser context)
  982. * @fullname: The attribute name, including namespace prefix
  983. * @value: The attribute value
  984. * @prefix: the prefix on the element node
  985. *
  986. * Handle an attribute that has been read by the parser.
  987. * The default handling is to convert the attribute into an
  988. * DOM subtree and past it in a new xmlAttr element added to
  989. * the element.
  990. */
  991. static void
  992. xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
  993. const xmlChar *value, const xmlChar *prefix ATTRIBUTE_UNUSED)
  994. {
  995. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  996. xmlAttrPtr ret;
  997. xmlChar *name;
  998. xmlChar *ns;
  999. xmlChar *nval;
  1000. xmlNsPtr namespace;
  1001. if (ctxt->html) {
  1002. name = xmlStrdup(fullname);
  1003. ns = NULL;
  1004. namespace = NULL;
  1005. } else {
  1006. /*
  1007. * Split the full name into a namespace prefix and the tag name
  1008. */
  1009. name = xmlSplitQName(ctxt, fullname, &ns);
  1010. if ((name != NULL) && (name[0] == 0)) {
  1011. if (xmlStrEqual(ns, BAD_CAST "xmlns")) {
  1012. xmlNsErrMsg(ctxt, XML_ERR_NS_DECL_ERROR,
  1013. "invalid namespace declaration '%s'\n",
  1014. fullname, NULL);
  1015. } else {
  1016. xmlNsWarnMsg(ctxt, XML_WAR_NS_COLUMN,
  1017. "Avoid attribute ending with ':' like '%s'\n",
  1018. fullname, NULL);
  1019. }
  1020. if (ns != NULL)
  1021. xmlFree(ns);
  1022. ns = NULL;
  1023. xmlFree(name);
  1024. name = xmlStrdup(fullname);
  1025. }
  1026. }
  1027. if (name == NULL) {
  1028. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
  1029. if (ns != NULL)
  1030. xmlFree(ns);
  1031. return;
  1032. }
  1033. #ifdef LIBXML_HTML_ENABLED
  1034. if ((ctxt->html) &&
  1035. (value == NULL) && (htmlIsBooleanAttr(fullname))) {
  1036. nval = xmlStrdup(fullname);
  1037. value = (const xmlChar *) nval;
  1038. } else
  1039. #endif
  1040. {
  1041. #ifdef LIBXML_VALID_ENABLED
  1042. /*
  1043. * Do the last stage of the attribute normalization
  1044. * Needed for HTML too:
  1045. * http://www.w3.org/TR/html4/types.html#h-6.2
  1046. */
  1047. ctxt->vctxt.valid = 1;
  1048. nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
  1049. ctxt->myDoc, ctxt->node,
  1050. fullname, value);
  1051. if (ctxt->vctxt.valid != 1) {
  1052. ctxt->valid = 0;
  1053. }
  1054. if (nval != NULL)
  1055. value = nval;
  1056. #else
  1057. nval = NULL;
  1058. #endif /* LIBXML_VALID_ENABLED */
  1059. }
  1060. /*
  1061. * Check whether it's a namespace definition
  1062. */
  1063. if ((!ctxt->html) && (ns == NULL) &&
  1064. (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
  1065. (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
  1066. xmlNsPtr nsret;
  1067. xmlChar *val;
  1068. if (!ctxt->replaceEntities) {
  1069. ctxt->depth++;
  1070. val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
  1071. 0,0,0);
  1072. ctxt->depth--;
  1073. if (val == NULL) {
  1074. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
  1075. if (name != NULL)
  1076. xmlFree(name);
  1077. if (nval != NULL)
  1078. xmlFree(nval);
  1079. return;
  1080. }
  1081. } else {
  1082. val = (xmlChar *) value;
  1083. }
  1084. if (val[0] != 0) {
  1085. xmlURIPtr uri;
  1086. uri = xmlParseURI((const char *)val);
  1087. if (uri == NULL) {
  1088. if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
  1089. ctxt->sax->warning(ctxt->userData,
  1090. "xmlns: %s not a valid URI\n", val);
  1091. } else {
  1092. if (uri->scheme == NULL) {
  1093. if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
  1094. ctxt->sax->warning(ctxt->userData,
  1095. "xmlns: URI %s is not absolute\n", val);
  1096. }
  1097. xmlFreeURI(uri);
  1098. }
  1099. }
  1100. /* a default namespace definition */
  1101. nsret = xmlNewNs(ctxt->node, val, NULL);
  1102. #ifdef LIBXML_VALID_ENABLED
  1103. /*
  1104. * Validate also for namespace decls, they are attributes from
  1105. * an XML-1.0 perspective
  1106. */
  1107. if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
  1108. ctxt->myDoc && ctxt->myDoc->intSubset)
  1109. ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
  1110. ctxt->node, prefix, nsret, val);
  1111. #endif /* LIBXML_VALID_ENABLED */
  1112. if (name != NULL)
  1113. xmlFree(name);
  1114. if (nval != NULL)
  1115. xmlFree(nval);
  1116. if (val != value)
  1117. xmlFree(val);
  1118. return;
  1119. }
  1120. if ((!ctxt->html) &&
  1121. (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
  1122. (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
  1123. xmlNsPtr nsret;
  1124. xmlChar *val;
  1125. if (!ctxt->replaceEntities) {
  1126. ctxt->depth++;
  1127. val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
  1128. 0,0,0);
  1129. ctxt->depth--;
  1130. if (val == NULL) {
  1131. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
  1132. xmlFree(ns);
  1133. if (name != NULL)
  1134. xmlFree(name);
  1135. if (nval != NULL)
  1136. xmlFree(nval);
  1137. return;
  1138. }
  1139. } else {
  1140. val = (xmlChar *) value;
  1141. }
  1142. if (val[0] == 0) {
  1143. xmlNsErrMsg(ctxt, XML_NS_ERR_EMPTY,
  1144. "Empty namespace name for prefix %s\n", name, NULL);
  1145. }
  1146. if ((ctxt->pedantic != 0) && (val[0] != 0)) {
  1147. xmlURIPtr uri;
  1148. uri = xmlParseURI((const char *)val);
  1149. if (uri == NULL) {
  1150. xmlNsWarnMsg(ctxt, XML_WAR_NS_URI,
  1151. "xmlns:%s: %s not a valid URI\n", name, value);
  1152. } else {
  1153. if (uri->scheme == NULL) {
  1154. xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
  1155. "xmlns:%s: URI %s is not absolute\n", name, value);
  1156. }
  1157. xmlFreeURI(uri);
  1158. }
  1159. }
  1160. /* a standard namespace definition */
  1161. nsret = xmlNewNs(ctxt->node, val, name);
  1162. xmlFree(ns);
  1163. #ifdef LIBXML_VALID_ENABLED
  1164. /*
  1165. * Validate also for namespace decls, they are attributes from
  1166. * an XML-1.0 perspective
  1167. */
  1168. if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
  1169. ctxt->myDoc && ctxt->myDoc->intSubset)
  1170. ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
  1171. ctxt->node, prefix, nsret, value);
  1172. #endif /* LIBXML_VALID_ENABLED */
  1173. if (name != NULL)
  1174. xmlFree(name);
  1175. if (nval != NULL)
  1176. xmlFree(nval);
  1177. if (val != value)
  1178. xmlFree(val);
  1179. return;
  1180. }
  1181. if (ns != NULL) {
  1182. namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
  1183. if (namespace == NULL) {
  1184. xmlNsErrMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
  1185. "Namespace prefix %s of attribute %s is not defined\n",
  1186. ns, name);
  1187. } else {
  1188. xmlAttrPtr prop;
  1189. prop = ctxt->node->properties;
  1190. while (prop != NULL) {
  1191. if (prop->ns != NULL) {
  1192. if ((xmlStrEqual(name, prop->name)) &&
  1193. ((namespace == prop->ns) ||
  1194. (xmlStrEqual(namespace->href, prop->ns->href)))) {
  1195. xmlNsErrMsg(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
  1196. "Attribute %s in %s redefined\n",
  1197. name, namespace->href);
  1198. ctxt->wellFormed = 0;
  1199. if (ctxt->recovery == 0) ctxt->disableSAX = 1;
  1200. if (name != NULL)
  1201. xmlFree(name);
  1202. goto error;
  1203. }
  1204. }
  1205. prop = prop->next;
  1206. }
  1207. }
  1208. } else {
  1209. namespace = NULL;
  1210. }
  1211. /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
  1212. ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
  1213. if (ret != NULL) {
  1214. if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
  1215. xmlNodePtr tmp;
  1216. ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
  1217. tmp = ret->children;
  1218. while (tmp != NULL) {
  1219. tmp->parent = (xmlNodePtr) ret;
  1220. if (tmp->next == NULL)
  1221. ret->last = tmp;
  1222. tmp = tmp->next;
  1223. }
  1224. } else if (value != NULL) {
  1225. ret->children = xmlNewDocText(ctxt->myDoc, value);
  1226. ret->last = ret->children;
  1227. if (ret->children != NULL)
  1228. ret->children->parent = (xmlNodePtr) ret;
  1229. }
  1230. }
  1231. #ifdef LIBXML_VALID_ENABLED
  1232. if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
  1233. ctxt->myDoc && ctxt->myDoc->intSubset) {
  1234. /*
  1235. * If we don't substitute entities, the validation should be
  1236. * done on a value with replaced entities anyway.
  1237. */
  1238. if (!ctxt->replaceEntities) {
  1239. xmlChar *val;
  1240. ctxt->depth++;
  1241. val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
  1242. 0,0,0);
  1243. ctxt->depth--;
  1244. if (val == NULL)
  1245. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
  1246. ctxt->myDoc, ctxt->node, ret, value);
  1247. else {
  1248. xmlChar *nvalnorm;
  1249. /*
  1250. * Do the last stage of the attribute normalization
  1251. * It need to be done twice ... it's an extra burden related
  1252. * to the ability to keep xmlSAX2References in attributes
  1253. */
  1254. nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
  1255. ctxt->node, fullname, val);
  1256. if (nvalnorm != NULL) {
  1257. xmlFree(val);
  1258. val = nvalnorm;
  1259. }
  1260. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
  1261. ctxt->myDoc, ctxt->node, ret, val);
  1262. xmlFree(val);
  1263. }
  1264. } else {
  1265. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
  1266. ctxt->node, ret, value);
  1267. }
  1268. } else
  1269. #endif /* LIBXML_VALID_ENABLED */
  1270. if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
  1271. (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
  1272. ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
  1273. /*
  1274. * when validating, the ID registration is done at the attribute
  1275. * validation level. Otherwise we have to do specific handling here.
  1276. */
  1277. if (xmlStrEqual(fullname, BAD_CAST "xml:id")) {
  1278. /*
  1279. * Add the xml:id value
  1280. *
  1281. * Open issue: normalization of the value.
  1282. */
  1283. if (xmlValidateNCName(value, 1) != 0) {
  1284. xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
  1285. "xml:id : attribute value %s is not an NCName\n",
  1286. (const char *) value, NULL);
  1287. }
  1288. xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
  1289. } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
  1290. xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
  1291. else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
  1292. xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
  1293. }
  1294. error:
  1295. if (nval != NULL)
  1296. xmlFree(nval);
  1297. if (ns != NULL)
  1298. xmlFree(ns);
  1299. }
  1300. /*
  1301. * xmlCheckDefaultedAttributes:
  1302. *
  1303. * Check defaulted attributes from the DTD
  1304. */
  1305. static void
  1306. xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
  1307. const xmlChar *prefix, const xmlChar **atts) {
  1308. xmlElementPtr elemDecl;
  1309. const xmlChar *att;
  1310. int internal = 1;
  1311. int i;
  1312. elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
  1313. if (elemDecl == NULL) {
  1314. elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
  1315. internal = 0;
  1316. }
  1317. process_external_subset:
  1318. if (elemDecl != NULL) {
  1319. xmlAttributePtr attr = elemDecl->attributes;
  1320. /*
  1321. * Check against defaulted attributes from the external subset
  1322. * if the document is stamped as standalone
  1323. */
  1324. if ((ctxt->myDoc->standalone == 1) &&
  1325. (ctxt->myDoc->extSubset != NULL) &&
  1326. (ctxt->validate)) {
  1327. while (attr != NULL) {
  1328. if ((attr->defaultValue != NULL) &&
  1329. (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
  1330. attr->elem, attr->name,
  1331. attr->prefix) == attr) &&
  1332. (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
  1333. attr->elem, attr->name,
  1334. attr->prefix) == NULL)) {
  1335. xmlChar *fulln;
  1336. if (attr->prefix != NULL) {
  1337. fulln = xmlStrdup(attr->prefix);
  1338. fulln = xmlStrcat(fulln, BAD_CAST ":");
  1339. fulln = xmlStrcat(fulln, attr->name);
  1340. } else {
  1341. fulln = xmlStrdup(attr->name);
  1342. }
  1343. if (fulln == NULL) {
  1344. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
  1345. break;
  1346. }
  1347. /*
  1348. * Check that the attribute is not declared in the
  1349. * serialization
  1350. */
  1351. att = NULL;
  1352. if (atts != NULL) {
  1353. i = 0;
  1354. att = atts[i];
  1355. while (att != NULL) {
  1356. if (xmlStrEqual(att, fulln))
  1357. break;
  1358. i += 2;
  1359. att = atts[i];
  1360. }
  1361. }
  1362. if (att == NULL) {
  1363. xmlErrValid(ctxt, XML_DTD_STANDALONE_DEFAULTED,
  1364. "standalone: attribute %s on %s defaulted from external subset\n",
  1365. (const char *)fulln,
  1366. (const char *)attr->elem);
  1367. }
  1368. xmlFree(fulln);
  1369. }
  1370. attr = attr->nexth;
  1371. }
  1372. }
  1373. /*
  1374. * Actually insert defaulted values when needed
  1375. */
  1376. attr = elemDecl->attributes;
  1377. while (attr != NULL) {
  1378. /*
  1379. * Make sure that attributes redefinition occurring in the
  1380. * internal subset are not overridden by definitions in the
  1381. * external subset.
  1382. */
  1383. if (attr->defaultValue != NULL) {
  1384. /*
  1385. * the element should be instantiated in the tree if:
  1386. * - this is a namespace prefix
  1387. * - the user required for completion in the tree
  1388. * like XSLT
  1389. * - there isn't already an attribute definition
  1390. * in the internal subset overriding it.
  1391. */
  1392. if (((attr->prefix != NULL) &&
  1393. (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
  1394. ((attr->prefix == NULL) &&
  1395. (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
  1396. (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
  1397. xmlAttributePtr tst;
  1398. tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
  1399. attr->elem, attr->name,
  1400. attr->prefix);
  1401. if ((tst == attr) || (tst == NULL)) {
  1402. xmlChar fn[50];
  1403. xmlChar *fulln;
  1404. fulln = xmlBuildQName(attr->name, attr->prefix, fn, 50);
  1405. if (fulln == NULL) {
  1406. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
  1407. return;
  1408. }
  1409. /*
  1410. * Check that the attribute is not declared in the
  1411. * serialization
  1412. */
  1413. att = NULL;
  1414. if (atts != NULL) {
  1415. i = 0;
  1416. att = atts[i];
  1417. while (att != NULL) {
  1418. if (xmlStrEqual(att, fulln))
  1419. break;
  1420. i += 2;
  1421. att = atts[i];
  1422. }
  1423. }
  1424. if (att == NULL) {
  1425. xmlSAX2AttributeInternal(ctxt, fulln,
  1426. attr->defaultValue, prefix);
  1427. }
  1428. if ((fulln != fn) && (fulln != attr->name))
  1429. xmlFree(fulln);
  1430. }
  1431. }
  1432. }
  1433. attr = attr->nexth;
  1434. }
  1435. if (internal == 1) {
  1436. elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
  1437. name, prefix);
  1438. internal = 0;
  1439. goto process_external_subset;
  1440. }
  1441. }
  1442. }
  1443. /**
  1444. * xmlSAX2StartElement:
  1445. * @ctx: the user data (XML parser context)
  1446. * @fullname: The element name, including namespace prefix
  1447. * @atts: An array of name/value attributes pairs, NULL terminated
  1448. *
  1449. * called when an opening tag has been processed.
  1450. */
  1451. void
  1452. xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
  1453. {
  1454. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  1455. xmlNodePtr ret;
  1456. xmlNodePtr parent;
  1457. xmlNsPtr ns;
  1458. xmlChar *name;
  1459. xmlChar *prefix;
  1460. const xmlChar *att;
  1461. const xmlChar *value;
  1462. int i;
  1463. if ((ctx == NULL) || (fullname == NULL) || (ctxt->myDoc == NULL)) return;
  1464. parent = ctxt->node;
  1465. #ifdef DEBUG_SAX
  1466. xmlGenericError(xmlGenericErrorContext,
  1467. "SAX.xmlSAX2StartElement(%s)\n", fullname);
  1468. #endif
  1469. /*
  1470. * First check on validity:
  1471. */
  1472. if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
  1473. ((ctxt->myDoc->intSubset == NULL) ||
  1474. ((ctxt->myDoc->intSubset->notations == NULL) &&
  1475. (ctxt->myDoc->intSubset->elements == NULL) &&
  1476. (ctxt->myDoc->intSubset->attributes == NULL) &&
  1477. (ctxt->myDoc->intSubset->entities == NULL)))) {
  1478. xmlErrValid(ctxt, XML_ERR_NO_DTD,
  1479. "Validation failed: no DTD found !", NULL, NULL);
  1480. ctxt->validate = 0;
  1481. }
  1482. /*
  1483. * Split the full name into a namespace prefix and the tag name
  1484. */
  1485. name = xmlSplitQName(ctxt, fullname, &prefix);
  1486. /*
  1487. * Note : the namespace resolution is deferred until the end of the
  1488. * attributes parsing, since local namespace can be defined as
  1489. * an attribute at this level.
  1490. */
  1491. ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
  1492. if (ret == NULL) {
  1493. if (prefix != NULL)
  1494. xmlFree(prefix);
  1495. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
  1496. return;
  1497. }
  1498. if (ctxt->myDoc->children == NULL) {
  1499. #ifdef DEBUG_SAX_TREE
  1500. xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
  1501. #endif
  1502. xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
  1503. } else if (parent == NULL) {
  1504. parent = ctxt->myDoc->children;
  1505. }
  1506. ctxt->nodemem = -1;
  1507. if (ctxt->linenumbers) {
  1508. if (ctxt->input != NULL) {
  1509. if (ctxt->input->line < 65535)
  1510. ret->line = (short) ctxt->input->line;
  1511. else
  1512. ret->line = 65535;
  1513. }
  1514. }
  1515. /*
  1516. * We are parsing a new node.
  1517. */
  1518. #ifdef DEBUG_SAX_TREE
  1519. xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
  1520. #endif
  1521. if (nodePush(ctxt, ret) < 0) {
  1522. xmlUnlinkNode(ret);
  1523. xmlFreeNode(ret);
  1524. if (prefix != NULL)
  1525. xmlFree(prefix);
  1526. return;
  1527. }
  1528. /*
  1529. * Link the child element
  1530. */
  1531. if (parent != NULL) {
  1532. if (parent->type == XML_ELEMENT_NODE) {
  1533. #ifdef DEBUG_SAX_TREE
  1534. xmlGenericError(xmlGenericErrorContext,
  1535. "adding child %s to %s\n", name, parent->name);
  1536. #endif
  1537. xmlAddChild(parent, ret);
  1538. } else {
  1539. #ifdef DEBUG_SAX_TREE
  1540. xmlGenericError(xmlGenericErrorContext,
  1541. "adding sibling %s to ", name);
  1542. xmlDebugDumpOneNode(stderr, parent, 0);
  1543. #endif
  1544. xmlAddSibling(parent, ret);
  1545. }
  1546. }
  1547. if (!ctxt->html) {
  1548. /*
  1549. * Insert all the defaulted attributes from the DTD especially
  1550. * namespaces
  1551. */
  1552. if ((ctxt->myDoc->intSubset != NULL) ||
  1553. (ctxt->myDoc->extSubset != NULL)) {
  1554. xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
  1555. }
  1556. /*
  1557. * process all the attributes whose name start with "xmlns"
  1558. */
  1559. if (atts != NULL) {
  1560. i = 0;
  1561. att = atts[i++];
  1562. value = atts[i++];
  1563. while ((att != NULL) && (value != NULL)) {
  1564. if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
  1565. (att[3] == 'n') && (att[4] == 's'))
  1566. xmlSAX2AttributeInternal(ctxt, att, value, prefix);
  1567. att = atts[i++];
  1568. value = atts[i++];
  1569. }
  1570. }
  1571. /*
  1572. * Search the namespace, note that since the attributes have been
  1573. * processed, the local namespaces are available.
  1574. */
  1575. ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
  1576. if ((ns == NULL) && (parent != NULL))
  1577. ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
  1578. if ((prefix != NULL) && (ns == NULL)) {
  1579. ns = xmlNewNs(ret, NULL, prefix);
  1580. xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
  1581. "Namespace prefix %s is not defined\n",
  1582. prefix, NULL);
  1583. }
  1584. /*
  1585. * set the namespace node, making sure that if the default namespace
  1586. * is unbound on a parent we simply keep it NULL
  1587. */
  1588. if ((ns != NULL) && (ns->href != NULL) &&
  1589. ((ns->href[0] != 0) || (ns->prefix != NULL)))
  1590. xmlSetNs(ret, ns);
  1591. }
  1592. /*
  1593. * process all the other attributes
  1594. */
  1595. if (atts != NULL) {
  1596. i = 0;
  1597. att = atts[i++];
  1598. value = atts[i++];
  1599. if (ctxt->html) {
  1600. while (att != NULL) {
  1601. xmlSAX2AttributeInternal(ctxt, att, value, NULL);
  1602. att = atts[i++];
  1603. value = atts[i++];
  1604. }
  1605. } else {
  1606. while ((att != NULL) && (value != NULL)) {
  1607. if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
  1608. (att[3] != 'n') || (att[4] != 's'))
  1609. xmlSAX2AttributeInternal(ctxt, att, value, NULL);
  1610. /*
  1611. * Next ones
  1612. */
  1613. att = atts[i++];
  1614. value = atts[i++];
  1615. }
  1616. }
  1617. }
  1618. #ifdef LIBXML_VALID_ENABLED
  1619. /*
  1620. * If it's the Document root, finish the DTD validation and
  1621. * check the document root element for validity
  1622. */
  1623. if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
  1624. int chk;
  1625. chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
  1626. if (chk <= 0)
  1627. ctxt->valid = 0;
  1628. if (chk < 0)
  1629. ctxt->wellFormed = 0;
  1630. ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
  1631. ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
  1632. }
  1633. #endif /* LIBXML_VALID_ENABLED */
  1634. if (prefix != NULL)
  1635. xmlFree(prefix);
  1636. }
  1637. /**
  1638. * xmlSAX2EndElement:
  1639. * @ctx: the user data (XML parser context)
  1640. * @name: The element name
  1641. *
  1642. * called when the end of an element has been detected.
  1643. */
  1644. void
  1645. xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
  1646. {
  1647. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  1648. xmlNodePtr cur;
  1649. if (ctx == NULL) return;
  1650. cur = ctxt->node;
  1651. #ifdef DEBUG_SAX
  1652. if (name == NULL)
  1653. xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(NULL)\n");
  1654. else
  1655. xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(%s)\n", name);
  1656. #endif
  1657. /* Capture end position and add node */
  1658. if (cur != NULL && ctxt->record_info) {
  1659. ctxt->nodeInfo->end_pos = ctxt->input->cur - ctxt->input->base;
  1660. ctxt->nodeInfo->end_line = ctxt->input->line;
  1661. ctxt->nodeInfo->node = cur;
  1662. xmlParserAddNodeInfo(ctxt, ctxt->nodeInfo);
  1663. }
  1664. ctxt->nodemem = -1;
  1665. #ifdef LIBXML_VALID_ENABLED
  1666. if (ctxt->validate && ctxt->wellFormed &&
  1667. ctxt->myDoc && ctxt->myDoc->intSubset)
  1668. ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
  1669. cur);
  1670. #endif /* LIBXML_VALID_ENABLED */
  1671. /*
  1672. * end of parsing of this node.
  1673. */
  1674. #ifdef DEBUG_SAX_TREE
  1675. xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
  1676. #endif
  1677. nodePop(ctxt);
  1678. }
  1679. #endif /* LIBXML_SAX1_ENABLED || LIBXML_HTML_ENABLED || LIBXML_LEGACY_ENABLED */
  1680. /*
  1681. * xmlSAX2TextNode:
  1682. * @ctxt: the parser context
  1683. * @str: the input string
  1684. * @len: the string length
  1685. *
  1686. * Callback for a text node
  1687. *
  1688. * Returns the newly allocated string or NULL if not needed or error
  1689. */
  1690. static xmlNodePtr
  1691. xmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
  1692. xmlNodePtr ret;
  1693. const xmlChar *intern = NULL;
  1694. /*
  1695. * Allocate
  1696. */
  1697. if (ctxt->freeElems != NULL) {
  1698. ret = ctxt->freeElems;
  1699. ctxt->freeElems = ret->next;
  1700. ctxt->freeElemsNr--;
  1701. } else {
  1702. ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
  1703. }
  1704. if (ret == NULL) {
  1705. xmlErrMemory(ctxt, "xmlSAX2Characters");
  1706. return(NULL);
  1707. }
  1708. memset(ret, 0, sizeof(xmlNode));
  1709. /*
  1710. * intern the formatting blanks found between tags, or the
  1711. * very short strings
  1712. */
  1713. if (ctxt->dictNames) {
  1714. xmlChar cur = str[len];
  1715. if ((len < (int) (2 * sizeof(void *))) &&
  1716. (ctxt->options & XML_PARSE_COMPACT)) {
  1717. /* store the string in the node overriding properties and nsDef */
  1718. xmlChar *tmp = (xmlChar *) &(ret->properties);
  1719. memcpy(tmp, str, len);
  1720. tmp[len] = 0;
  1721. intern = tmp;
  1722. } else if ((len <= 3) && ((cur == '"') || (cur == '\'') ||
  1723. ((cur == '<') && (str[len + 1] != '!')))) {
  1724. intern = xmlDictLookup(ctxt->dict, str, len);
  1725. } else if (IS_BLANK_CH(*str) && (len < 60) && (cur == '<') &&
  1726. (str[len + 1] != '!')) {
  1727. int i;
  1728. for (i = 1;i < len;i++) {
  1729. if (!IS_BLANK_CH(str[i])) goto skip;
  1730. }
  1731. intern = xmlDictLookup(ctxt->dict, str, len);
  1732. }
  1733. }
  1734. skip:
  1735. ret->type = XML_TEXT_NODE;
  1736. ret->name = xmlStringText;
  1737. if (intern == NULL) {
  1738. ret->content = xmlStrndup(str, len);
  1739. if (ret->content == NULL) {
  1740. xmlSAX2ErrMemory(ctxt, "xmlSAX2TextNode");
  1741. xmlFree(ret);
  1742. return(NULL);
  1743. }
  1744. } else
  1745. ret->content = (xmlChar *) intern;
  1746. if (ctxt->linenumbers) {
  1747. if (ctxt->input != NULL) {
  1748. if (ctxt->input->line < 65535)
  1749. ret->line = (short) ctxt->input->line;
  1750. else {
  1751. ret->line = 65535;
  1752. if (ctxt->options & XML_PARSE_BIG_LINES)
  1753. ret->psvi = (void *) (ptrdiff_t) ctxt->input->line;
  1754. }
  1755. }
  1756. }
  1757. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  1758. xmlRegisterNodeDefaultValue(ret);
  1759. return(ret);
  1760. }
  1761. #ifdef LIBXML_VALID_ENABLED
  1762. /*
  1763. * xmlSAX2DecodeAttrEntities:
  1764. * @ctxt: the parser context
  1765. * @str: the input string
  1766. * @len: the string length
  1767. *
  1768. * Remove the entities from an attribute value
  1769. *
  1770. * Returns the newly allocated string or NULL if not needed or error
  1771. */
  1772. static xmlChar *
  1773. xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str,
  1774. const xmlChar *end) {
  1775. const xmlChar *in;
  1776. xmlChar *ret;
  1777. in = str;
  1778. while (in < end)
  1779. if (*in++ == '&')
  1780. goto decode;
  1781. return(NULL);
  1782. decode:
  1783. ctxt->depth++;
  1784. ret = xmlStringLenDecodeEntities(ctxt, str, end - str,
  1785. XML_SUBSTITUTE_REF, 0,0,0);
  1786. ctxt->depth--;
  1787. return(ret);
  1788. }
  1789. #endif /* LIBXML_VALID_ENABLED */
  1790. /**
  1791. * xmlSAX2AttributeNs:
  1792. * @ctx: the user data (XML parser context)
  1793. * @localname: the local name of the attribute
  1794. * @prefix: the attribute namespace prefix if available
  1795. * @URI: the attribute namespace name if available
  1796. * @value: Start of the attribute value
  1797. * @valueend: end of the attribute value
  1798. *
  1799. * Handle an attribute that has been read by the parser.
  1800. * The default handling is to convert the attribute into an
  1801. * DOM subtree and past it in a new xmlAttr element added to
  1802. * the element.
  1803. */
  1804. static void
  1805. xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
  1806. const xmlChar * localname,
  1807. const xmlChar * prefix,
  1808. const xmlChar * value,
  1809. const xmlChar * valueend)
  1810. {
  1811. xmlAttrPtr ret;
  1812. xmlNsPtr namespace = NULL;
  1813. xmlChar *dup = NULL;
  1814. /*
  1815. * Note: if prefix == NULL, the attribute is not in the default namespace
  1816. */
  1817. if (prefix != NULL)
  1818. namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, prefix);
  1819. /*
  1820. * allocate the node
  1821. */
  1822. if (ctxt->freeAttrs != NULL) {
  1823. ret = ctxt->freeAttrs;
  1824. ctxt->freeAttrs = ret->next;
  1825. ctxt->freeAttrsNr--;
  1826. memset(ret, 0, sizeof(xmlAttr));
  1827. ret->type = XML_ATTRIBUTE_NODE;
  1828. ret->parent = ctxt->node;
  1829. ret->doc = ctxt->myDoc;
  1830. ret->ns = namespace;
  1831. if (ctxt->dictNames)
  1832. ret->name = localname;
  1833. else
  1834. ret->name = xmlStrdup(localname);
  1835. /* link at the end to preserve order, TODO speed up with a last */
  1836. if (ctxt->node->properties == NULL) {
  1837. ctxt->node->properties = ret;
  1838. } else {
  1839. xmlAttrPtr prev = ctxt->node->properties;
  1840. while (prev->next != NULL) prev = prev->next;
  1841. prev->next = ret;
  1842. ret->prev = prev;
  1843. }
  1844. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  1845. xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
  1846. } else {
  1847. if (ctxt->dictNames)
  1848. ret = xmlNewNsPropEatName(ctxt->node, namespace,
  1849. (xmlChar *) localname, NULL);
  1850. else
  1851. ret = xmlNewNsProp(ctxt->node, namespace, localname, NULL);
  1852. if (ret == NULL) {
  1853. xmlErrMemory(ctxt, "xmlSAX2AttributeNs");
  1854. return;
  1855. }
  1856. }
  1857. if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
  1858. xmlNodePtr tmp;
  1859. /*
  1860. * We know that if there is an entity reference, then
  1861. * the string has been dup'ed and terminates with 0
  1862. * otherwise with ' or "
  1863. */
  1864. if (*valueend != 0) {
  1865. tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
  1866. ret->children = tmp;
  1867. ret->last = tmp;
  1868. if (tmp != NULL) {
  1869. tmp->doc = ret->doc;
  1870. tmp->parent = (xmlNodePtr) ret;
  1871. }
  1872. } else {
  1873. ret->children = xmlStringLenGetNodeList(ctxt->myDoc, value,
  1874. valueend - value);
  1875. tmp = ret->children;
  1876. while (tmp != NULL) {
  1877. tmp->doc = ret->doc;
  1878. tmp->parent = (xmlNodePtr) ret;
  1879. if (tmp->next == NULL)
  1880. ret->last = tmp;
  1881. tmp = tmp->next;
  1882. }
  1883. }
  1884. } else if (value != NULL) {
  1885. xmlNodePtr tmp;
  1886. tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
  1887. ret->children = tmp;
  1888. ret->last = tmp;
  1889. if (tmp != NULL) {
  1890. tmp->doc = ret->doc;
  1891. tmp->parent = (xmlNodePtr) ret;
  1892. }
  1893. }
  1894. #ifdef LIBXML_VALID_ENABLED
  1895. if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
  1896. ctxt->myDoc && ctxt->myDoc->intSubset) {
  1897. /*
  1898. * If we don't substitute entities, the validation should be
  1899. * done on a value with replaced entities anyway.
  1900. */
  1901. if (!ctxt->replaceEntities) {
  1902. dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend);
  1903. if (dup == NULL) {
  1904. if (*valueend == 0) {
  1905. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
  1906. ctxt->myDoc, ctxt->node, ret, value);
  1907. } else {
  1908. /*
  1909. * That should already be normalized.
  1910. * cheaper to finally allocate here than duplicate
  1911. * entry points in the full validation code
  1912. */
  1913. dup = xmlStrndup(value, valueend - value);
  1914. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
  1915. ctxt->myDoc, ctxt->node, ret, dup);
  1916. }
  1917. } else {
  1918. /*
  1919. * dup now contains a string of the flattened attribute
  1920. * content with entities substituted. Check if we need to
  1921. * apply an extra layer of normalization.
  1922. * It need to be done twice ... it's an extra burden related
  1923. * to the ability to keep references in attributes
  1924. */
  1925. if (ctxt->attsSpecial != NULL) {
  1926. xmlChar *nvalnorm;
  1927. xmlChar fn[50];
  1928. xmlChar *fullname;
  1929. fullname = xmlBuildQName(localname, prefix, fn, 50);
  1930. if (fullname != NULL) {
  1931. ctxt->vctxt.valid = 1;
  1932. nvalnorm = xmlValidCtxtNormalizeAttributeValue(
  1933. &ctxt->vctxt, ctxt->myDoc,
  1934. ctxt->node, fullname, dup);
  1935. if (ctxt->vctxt.valid != 1)
  1936. ctxt->valid = 0;
  1937. if ((fullname != fn) && (fullname != localname))
  1938. xmlFree(fullname);
  1939. if (nvalnorm != NULL) {
  1940. xmlFree(dup);
  1941. dup = nvalnorm;
  1942. }
  1943. }
  1944. }
  1945. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
  1946. ctxt->myDoc, ctxt->node, ret, dup);
  1947. }
  1948. } else {
  1949. /*
  1950. * if entities already have been substituted, then
  1951. * the attribute as passed is already normalized
  1952. */
  1953. dup = xmlStrndup(value, valueend - value);
  1954. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
  1955. ctxt->myDoc, ctxt->node, ret, dup);
  1956. }
  1957. } else
  1958. #endif /* LIBXML_VALID_ENABLED */
  1959. if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
  1960. (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
  1961. ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
  1962. /*
  1963. * when validating, the ID registration is done at the attribute
  1964. * validation level. Otherwise we have to do specific handling here.
  1965. */
  1966. if ((prefix == ctxt->str_xml) &&
  1967. (localname[0] == 'i') && (localname[1] == 'd') &&
  1968. (localname[2] == 0)) {
  1969. /*
  1970. * Add the xml:id value
  1971. *
  1972. * Open issue: normalization of the value.
  1973. */
  1974. if (dup == NULL)
  1975. dup = xmlStrndup(value, valueend - value);
  1976. #if defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_DOCB_ENABLED) || defined(LIBXML_LEGACY_ENABLED)
  1977. #ifdef LIBXML_VALID_ENABLED
  1978. if (xmlValidateNCName(dup, 1) != 0) {
  1979. xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
  1980. "xml:id : attribute value %s is not an NCName\n",
  1981. (const char *) dup, NULL);
  1982. }
  1983. #endif
  1984. #endif
  1985. xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
  1986. } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) {
  1987. /* might be worth duplicate entry points and not copy */
  1988. if (dup == NULL)
  1989. dup = xmlStrndup(value, valueend - value);
  1990. xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
  1991. } else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) {
  1992. if (dup == NULL)
  1993. dup = xmlStrndup(value, valueend - value);
  1994. xmlAddRef(&ctxt->vctxt, ctxt->myDoc, dup, ret);
  1995. }
  1996. }
  1997. if (dup != NULL)
  1998. xmlFree(dup);
  1999. }
  2000. /**
  2001. * xmlSAX2StartElementNs:
  2002. * @ctx: the user data (XML parser context)
  2003. * @localname: the local name of the element
  2004. * @prefix: the element namespace prefix if available
  2005. * @URI: the element namespace name if available
  2006. * @nb_namespaces: number of namespace definitions on that node
  2007. * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
  2008. * @nb_attributes: the number of attributes on that node
  2009. * @nb_defaulted: the number of defaulted attributes.
  2010. * @attributes: pointer to the array of (localname/prefix/URI/value/end)
  2011. * attribute values.
  2012. *
  2013. * SAX2 callback when an element start has been detected by the parser.
  2014. * It provides the namespace information for the element, as well as
  2015. * the new namespace declarations on the element.
  2016. */
  2017. void
  2018. xmlSAX2StartElementNs(void *ctx,
  2019. const xmlChar *localname,
  2020. const xmlChar *prefix,
  2021. const xmlChar *URI,
  2022. int nb_namespaces,
  2023. const xmlChar **namespaces,
  2024. int nb_attributes,
  2025. int nb_defaulted,
  2026. const xmlChar **attributes)
  2027. {
  2028. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  2029. xmlNodePtr ret;
  2030. xmlNodePtr parent;
  2031. xmlNsPtr last = NULL, ns;
  2032. const xmlChar *uri, *pref;
  2033. xmlChar *lname = NULL;
  2034. int i, j;
  2035. if (ctx == NULL) return;
  2036. parent = ctxt->node;
  2037. /*
  2038. * First check on validity:
  2039. */
  2040. if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
  2041. ((ctxt->myDoc->intSubset == NULL) ||
  2042. ((ctxt->myDoc->intSubset->notations == NULL) &&
  2043. (ctxt->myDoc->intSubset->elements == NULL) &&
  2044. (ctxt->myDoc->intSubset->attributes == NULL) &&
  2045. (ctxt->myDoc->intSubset->entities == NULL)))) {
  2046. xmlErrValid(ctxt, XML_DTD_NO_DTD,
  2047. "Validation failed: no DTD found !", NULL, NULL);
  2048. ctxt->validate = 0;
  2049. }
  2050. /*
  2051. * Take care of the rare case of an undefined namespace prefix
  2052. */
  2053. if ((prefix != NULL) && (URI == NULL)) {
  2054. if (ctxt->dictNames) {
  2055. const xmlChar *fullname;
  2056. fullname = xmlDictQLookup(ctxt->dict, prefix, localname);
  2057. if (fullname != NULL)
  2058. localname = fullname;
  2059. } else {
  2060. lname = xmlBuildQName(localname, prefix, NULL, 0);
  2061. }
  2062. }
  2063. /*
  2064. * allocate the node
  2065. */
  2066. if (ctxt->freeElems != NULL) {
  2067. ret = ctxt->freeElems;
  2068. ctxt->freeElems = ret->next;
  2069. ctxt->freeElemsNr--;
  2070. memset(ret, 0, sizeof(xmlNode));
  2071. ret->doc = ctxt->myDoc;
  2072. ret->type = XML_ELEMENT_NODE;
  2073. if (ctxt->dictNames)
  2074. ret->name = localname;
  2075. else {
  2076. if (lname == NULL)
  2077. ret->name = xmlStrdup(localname);
  2078. else
  2079. ret->name = lname;
  2080. if (ret->name == NULL) {
  2081. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
  2082. return;
  2083. }
  2084. }
  2085. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  2086. xmlRegisterNodeDefaultValue(ret);
  2087. } else {
  2088. if (ctxt->dictNames)
  2089. ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
  2090. (xmlChar *) localname, NULL);
  2091. else if (lname == NULL)
  2092. ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL);
  2093. else
  2094. ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
  2095. (xmlChar *) lname, NULL);
  2096. if (ret == NULL) {
  2097. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
  2098. return;
  2099. }
  2100. }
  2101. if (ctxt->linenumbers) {
  2102. if (ctxt->input != NULL) {
  2103. if (ctxt->input->line < 65535)
  2104. ret->line = (short) ctxt->input->line;
  2105. else
  2106. ret->line = 65535;
  2107. }
  2108. }
  2109. if (parent == NULL) {
  2110. xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
  2111. }
  2112. /*
  2113. * Build the namespace list
  2114. */
  2115. for (i = 0,j = 0;j < nb_namespaces;j++) {
  2116. pref = namespaces[i++];
  2117. uri = namespaces[i++];
  2118. ns = xmlNewNs(NULL, uri, pref);
  2119. if (ns != NULL) {
  2120. if (last == NULL) {
  2121. ret->nsDef = last = ns;
  2122. } else {
  2123. last->next = ns;
  2124. last = ns;
  2125. }
  2126. if ((URI != NULL) && (prefix == pref))
  2127. ret->ns = ns;
  2128. } else {
  2129. /*
  2130. * any out of memory error would already have been raised
  2131. * but we can't be guaranteed it's the actual error due to the
  2132. * API, best is to skip in this case
  2133. */
  2134. continue;
  2135. }
  2136. #ifdef LIBXML_VALID_ENABLED
  2137. if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
  2138. ctxt->myDoc && ctxt->myDoc->intSubset) {
  2139. ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
  2140. ret, prefix, ns, uri);
  2141. }
  2142. #endif /* LIBXML_VALID_ENABLED */
  2143. }
  2144. ctxt->nodemem = -1;
  2145. /*
  2146. * We are parsing a new node.
  2147. */
  2148. if (nodePush(ctxt, ret) < 0) {
  2149. xmlUnlinkNode(ret);
  2150. xmlFreeNode(ret);
  2151. return;
  2152. }
  2153. /*
  2154. * Link the child element
  2155. */
  2156. if (parent != NULL) {
  2157. if (parent->type == XML_ELEMENT_NODE) {
  2158. xmlAddChild(parent, ret);
  2159. } else {
  2160. xmlAddSibling(parent, ret);
  2161. }
  2162. }
  2163. /*
  2164. * Insert the defaulted attributes from the DTD only if requested:
  2165. */
  2166. if ((nb_defaulted != 0) &&
  2167. ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0))
  2168. nb_attributes -= nb_defaulted;
  2169. /*
  2170. * Search the namespace if it wasn't already found
  2171. * Note that, if prefix is NULL, this searches for the default Ns
  2172. */
  2173. if ((URI != NULL) && (ret->ns == NULL)) {
  2174. ret->ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
  2175. if ((ret->ns == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
  2176. ret->ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
  2177. }
  2178. if (ret->ns == NULL) {
  2179. ns = xmlNewNs(ret, NULL, prefix);
  2180. if (ns == NULL) {
  2181. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
  2182. return;
  2183. }
  2184. if (prefix != NULL)
  2185. xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
  2186. "Namespace prefix %s was not found\n",
  2187. prefix, NULL);
  2188. else
  2189. xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
  2190. "Namespace default prefix was not found\n",
  2191. NULL, NULL);
  2192. }
  2193. }
  2194. /*
  2195. * process all the other attributes
  2196. */
  2197. if (nb_attributes > 0) {
  2198. for (j = 0,i = 0;i < nb_attributes;i++,j+=5) {
  2199. /*
  2200. * Handle the rare case of an undefined attribute prefix
  2201. */
  2202. if ((attributes[j+1] != NULL) && (attributes[j+2] == NULL)) {
  2203. if (ctxt->dictNames) {
  2204. const xmlChar *fullname;
  2205. fullname = xmlDictQLookup(ctxt->dict, attributes[j+1],
  2206. attributes[j]);
  2207. if (fullname != NULL) {
  2208. xmlSAX2AttributeNs(ctxt, fullname, NULL,
  2209. attributes[j+3], attributes[j+4]);
  2210. continue;
  2211. }
  2212. } else {
  2213. lname = xmlBuildQName(attributes[j], attributes[j+1],
  2214. NULL, 0);
  2215. if (lname != NULL) {
  2216. xmlSAX2AttributeNs(ctxt, lname, NULL,
  2217. attributes[j+3], attributes[j+4]);
  2218. xmlFree(lname);
  2219. continue;
  2220. }
  2221. }
  2222. }
  2223. xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
  2224. attributes[j+3], attributes[j+4]);
  2225. }
  2226. }
  2227. #ifdef LIBXML_VALID_ENABLED
  2228. /*
  2229. * If it's the Document root, finish the DTD validation and
  2230. * check the document root element for validity
  2231. */
  2232. if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
  2233. int chk;
  2234. chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
  2235. if (chk <= 0)
  2236. ctxt->valid = 0;
  2237. if (chk < 0)
  2238. ctxt->wellFormed = 0;
  2239. ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
  2240. ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
  2241. }
  2242. #endif /* LIBXML_VALID_ENABLED */
  2243. }
  2244. /**
  2245. * xmlSAX2EndElementNs:
  2246. * @ctx: the user data (XML parser context)
  2247. * @localname: the local name of the element
  2248. * @prefix: the element namespace prefix if available
  2249. * @URI: the element namespace name if available
  2250. *
  2251. * SAX2 callback when an element end has been detected by the parser.
  2252. * It provides the namespace information for the element.
  2253. */
  2254. void
  2255. xmlSAX2EndElementNs(void *ctx,
  2256. const xmlChar * localname ATTRIBUTE_UNUSED,
  2257. const xmlChar * prefix ATTRIBUTE_UNUSED,
  2258. const xmlChar * URI ATTRIBUTE_UNUSED)
  2259. {
  2260. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  2261. xmlParserNodeInfo node_info;
  2262. xmlNodePtr cur;
  2263. if (ctx == NULL) return;
  2264. cur = ctxt->node;
  2265. /* Capture end position and add node */
  2266. if ((ctxt->record_info) && (cur != NULL)) {
  2267. node_info.end_pos = ctxt->input->cur - ctxt->input->base;
  2268. node_info.end_line = ctxt->input->line;
  2269. node_info.node = cur;
  2270. xmlParserAddNodeInfo(ctxt, &node_info);
  2271. }
  2272. ctxt->nodemem = -1;
  2273. #ifdef LIBXML_VALID_ENABLED
  2274. if (ctxt->validate && ctxt->wellFormed &&
  2275. ctxt->myDoc && ctxt->myDoc->intSubset)
  2276. ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, cur);
  2277. #endif /* LIBXML_VALID_ENABLED */
  2278. /*
  2279. * end of parsing of this node.
  2280. */
  2281. nodePop(ctxt);
  2282. }
  2283. /**
  2284. * xmlSAX2Reference:
  2285. * @ctx: the user data (XML parser context)
  2286. * @name: The entity name
  2287. *
  2288. * called when an entity xmlSAX2Reference is detected.
  2289. */
  2290. void
  2291. xmlSAX2Reference(void *ctx, const xmlChar *name)
  2292. {
  2293. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  2294. xmlNodePtr ret;
  2295. if (ctx == NULL) return;
  2296. #ifdef DEBUG_SAX
  2297. xmlGenericError(xmlGenericErrorContext,
  2298. "SAX.xmlSAX2Reference(%s)\n", name);
  2299. #endif
  2300. if (name[0] == '#')
  2301. ret = xmlNewCharRef(ctxt->myDoc, name);
  2302. else
  2303. ret = xmlNewReference(ctxt->myDoc, name);
  2304. #ifdef DEBUG_SAX_TREE
  2305. xmlGenericError(xmlGenericErrorContext,
  2306. "add xmlSAX2Reference %s to %s \n", name, ctxt->node->name);
  2307. #endif
  2308. if (xmlAddChild(ctxt->node, ret) == NULL) {
  2309. xmlFreeNode(ret);
  2310. }
  2311. }
  2312. /**
  2313. * xmlSAX2Text:
  2314. * @ctx: the user data (XML parser context)
  2315. * @ch: a xmlChar string
  2316. * @len: the number of xmlChar
  2317. * @type: text or cdata
  2318. *
  2319. * Append characters.
  2320. */
  2321. static void
  2322. xmlSAX2Text(xmlParserCtxtPtr ctxt, const xmlChar *ch, int len,
  2323. xmlElementType type)
  2324. {
  2325. xmlNodePtr lastChild;
  2326. if (ctxt == NULL) return;
  2327. #ifdef DEBUG_SAX
  2328. xmlGenericError(xmlGenericErrorContext,
  2329. "SAX.xmlSAX2Characters(%.30s, %d)\n", ch, len);
  2330. #endif
  2331. /*
  2332. * Handle the data if any. If there is no child
  2333. * add it as content, otherwise if the last child is text,
  2334. * concatenate it, else create a new node of type text.
  2335. */
  2336. if (ctxt->node == NULL) {
  2337. #ifdef DEBUG_SAX_TREE
  2338. xmlGenericError(xmlGenericErrorContext,
  2339. "add chars: ctxt->node == NULL !\n");
  2340. #endif
  2341. return;
  2342. }
  2343. lastChild = ctxt->node->last;
  2344. #ifdef DEBUG_SAX_TREE
  2345. xmlGenericError(xmlGenericErrorContext,
  2346. "add chars to %s \n", ctxt->node->name);
  2347. #endif
  2348. /*
  2349. * Here we needed an accelerator mechanism in case of very large
  2350. * elements. Use an attribute in the structure !!!
  2351. */
  2352. if (lastChild == NULL) {
  2353. if (type == XML_TEXT_NODE)
  2354. lastChild = xmlSAX2TextNode(ctxt, ch, len);
  2355. else
  2356. lastChild = xmlNewCDataBlock(ctxt->myDoc, ch, len);
  2357. if (lastChild != NULL) {
  2358. ctxt->node->children = lastChild;
  2359. ctxt->node->last = lastChild;
  2360. lastChild->parent = ctxt->node;
  2361. lastChild->doc = ctxt->node->doc;
  2362. ctxt->nodelen = len;
  2363. ctxt->nodemem = len + 1;
  2364. } else {
  2365. xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
  2366. return;
  2367. }
  2368. } else {
  2369. int coalesceText = (lastChild != NULL) &&
  2370. (lastChild->type == type) &&
  2371. ((type != XML_TEXT_NODE) ||
  2372. (lastChild->name == xmlStringText));
  2373. if ((coalesceText) && (ctxt->nodemem != 0)) {
  2374. /*
  2375. * The whole point of maintaining nodelen and nodemem,
  2376. * xmlTextConcat is too costly, i.e. compute length,
  2377. * reallocate a new buffer, move data, append ch. Here
  2378. * We try to minimize realloc() uses and avoid copying
  2379. * and recomputing length over and over.
  2380. */
  2381. if (lastChild->content == (xmlChar *)&(lastChild->properties)) {
  2382. lastChild->content = xmlStrdup(lastChild->content);
  2383. lastChild->properties = NULL;
  2384. } else if ((ctxt->nodemem == ctxt->nodelen + 1) &&
  2385. (xmlDictOwns(ctxt->dict, lastChild->content))) {
  2386. lastChild->content = xmlStrdup(lastChild->content);
  2387. }
  2388. if (lastChild->content == NULL) {
  2389. xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters: xmlStrdup returned NULL");
  2390. return;
  2391. }
  2392. if (((size_t)ctxt->nodelen + (size_t)len > XML_MAX_TEXT_LENGTH) &&
  2393. ((ctxt->options & XML_PARSE_HUGE) == 0)) {
  2394. xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters: huge text node");
  2395. return;
  2396. }
  2397. if ((size_t)ctxt->nodelen > SIZE_T_MAX - (size_t)len ||
  2398. (size_t)ctxt->nodemem + (size_t)len > SIZE_T_MAX / 2) {
  2399. xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters overflow prevented");
  2400. return;
  2401. }
  2402. if (ctxt->nodelen + len >= ctxt->nodemem) {
  2403. xmlChar *newbuf;
  2404. size_t size;
  2405. size = ctxt->nodemem + len;
  2406. size *= 2;
  2407. newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
  2408. if (newbuf == NULL) {
  2409. xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
  2410. return;
  2411. }
  2412. ctxt->nodemem = size;
  2413. lastChild->content = newbuf;
  2414. }
  2415. memcpy(&lastChild->content[ctxt->nodelen], ch, len);
  2416. ctxt->nodelen += len;
  2417. lastChild->content[ctxt->nodelen] = 0;
  2418. } else if (coalesceText) {
  2419. if (xmlTextConcat(lastChild, ch, len)) {
  2420. xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
  2421. }
  2422. if (ctxt->node->children != NULL) {
  2423. ctxt->nodelen = xmlStrlen(lastChild->content);
  2424. ctxt->nodemem = ctxt->nodelen + 1;
  2425. }
  2426. } else {
  2427. /* Mixed content, first time */
  2428. if (type == XML_TEXT_NODE)
  2429. lastChild = xmlSAX2TextNode(ctxt, ch, len);
  2430. else
  2431. lastChild = xmlNewCDataBlock(ctxt->myDoc, ch, len);
  2432. if (lastChild != NULL) {
  2433. xmlAddChild(ctxt->node, lastChild);
  2434. if (ctxt->node->children != NULL) {
  2435. ctxt->nodelen = len;
  2436. ctxt->nodemem = len + 1;
  2437. }
  2438. }
  2439. }
  2440. }
  2441. }
  2442. /**
  2443. * xmlSAX2Characters:
  2444. * @ctx: the user data (XML parser context)
  2445. * @ch: a xmlChar string
  2446. * @len: the number of xmlChar
  2447. *
  2448. * receiving some chars from the parser.
  2449. */
  2450. void
  2451. xmlSAX2Characters(void *ctx, const xmlChar *ch, int len)
  2452. {
  2453. xmlSAX2Text((xmlParserCtxtPtr) ctx, ch, len, XML_TEXT_NODE);
  2454. }
  2455. /**
  2456. * xmlSAX2IgnorableWhitespace:
  2457. * @ctx: the user data (XML parser context)
  2458. * @ch: a xmlChar string
  2459. * @len: the number of xmlChar
  2460. *
  2461. * receiving some ignorable whitespaces from the parser.
  2462. * UNUSED: by default the DOM building will use xmlSAX2Characters
  2463. */
  2464. void
  2465. xmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
  2466. {
  2467. /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
  2468. #ifdef DEBUG_SAX
  2469. xmlGenericError(xmlGenericErrorContext,
  2470. "SAX.xmlSAX2IgnorableWhitespace(%.30s, %d)\n", ch, len);
  2471. #endif
  2472. }
  2473. /**
  2474. * xmlSAX2ProcessingInstruction:
  2475. * @ctx: the user data (XML parser context)
  2476. * @target: the target name
  2477. * @data: the PI data's
  2478. *
  2479. * A processing instruction has been parsed.
  2480. */
  2481. void
  2482. xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target,
  2483. const xmlChar *data)
  2484. {
  2485. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  2486. xmlNodePtr ret;
  2487. xmlNodePtr parent;
  2488. if (ctx == NULL) return;
  2489. parent = ctxt->node;
  2490. #ifdef DEBUG_SAX
  2491. xmlGenericError(xmlGenericErrorContext,
  2492. "SAX.xmlSAX2ProcessingInstruction(%s, %s)\n", target, data);
  2493. #endif
  2494. ret = xmlNewDocPI(ctxt->myDoc, target, data);
  2495. if (ret == NULL) return;
  2496. if (ctxt->linenumbers) {
  2497. if (ctxt->input != NULL) {
  2498. if (ctxt->input->line < 65535)
  2499. ret->line = (short) ctxt->input->line;
  2500. else
  2501. ret->line = 65535;
  2502. }
  2503. }
  2504. if (ctxt->inSubset == 1) {
  2505. xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
  2506. return;
  2507. } else if (ctxt->inSubset == 2) {
  2508. xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
  2509. return;
  2510. }
  2511. if (parent == NULL) {
  2512. #ifdef DEBUG_SAX_TREE
  2513. xmlGenericError(xmlGenericErrorContext,
  2514. "Setting PI %s as root\n", target);
  2515. #endif
  2516. xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
  2517. return;
  2518. }
  2519. if (parent->type == XML_ELEMENT_NODE) {
  2520. #ifdef DEBUG_SAX_TREE
  2521. xmlGenericError(xmlGenericErrorContext,
  2522. "adding PI %s child to %s\n", target, parent->name);
  2523. #endif
  2524. xmlAddChild(parent, ret);
  2525. } else {
  2526. #ifdef DEBUG_SAX_TREE
  2527. xmlGenericError(xmlGenericErrorContext,
  2528. "adding PI %s sibling to ", target);
  2529. xmlDebugDumpOneNode(stderr, parent, 0);
  2530. #endif
  2531. xmlAddSibling(parent, ret);
  2532. }
  2533. }
  2534. /**
  2535. * xmlSAX2Comment:
  2536. * @ctx: the user data (XML parser context)
  2537. * @value: the xmlSAX2Comment content
  2538. *
  2539. * A xmlSAX2Comment has been parsed.
  2540. */
  2541. void
  2542. xmlSAX2Comment(void *ctx, const xmlChar *value)
  2543. {
  2544. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  2545. xmlNodePtr ret;
  2546. xmlNodePtr parent;
  2547. if (ctx == NULL) return;
  2548. parent = ctxt->node;
  2549. #ifdef DEBUG_SAX
  2550. xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2Comment(%s)\n", value);
  2551. #endif
  2552. ret = xmlNewDocComment(ctxt->myDoc, value);
  2553. if (ret == NULL) return;
  2554. if (ctxt->linenumbers) {
  2555. if (ctxt->input != NULL) {
  2556. if (ctxt->input->line < 65535)
  2557. ret->line = (short) ctxt->input->line;
  2558. else
  2559. ret->line = 65535;
  2560. }
  2561. }
  2562. if (ctxt->inSubset == 1) {
  2563. xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
  2564. return;
  2565. } else if (ctxt->inSubset == 2) {
  2566. xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
  2567. return;
  2568. }
  2569. if (parent == NULL) {
  2570. #ifdef DEBUG_SAX_TREE
  2571. xmlGenericError(xmlGenericErrorContext,
  2572. "Setting xmlSAX2Comment as root\n");
  2573. #endif
  2574. xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
  2575. return;
  2576. }
  2577. if (parent->type == XML_ELEMENT_NODE) {
  2578. #ifdef DEBUG_SAX_TREE
  2579. xmlGenericError(xmlGenericErrorContext,
  2580. "adding xmlSAX2Comment child to %s\n", parent->name);
  2581. #endif
  2582. xmlAddChild(parent, ret);
  2583. } else {
  2584. #ifdef DEBUG_SAX_TREE
  2585. xmlGenericError(xmlGenericErrorContext,
  2586. "adding xmlSAX2Comment sibling to ");
  2587. xmlDebugDumpOneNode(stderr, parent, 0);
  2588. #endif
  2589. xmlAddSibling(parent, ret);
  2590. }
  2591. }
  2592. /**
  2593. * xmlSAX2CDataBlock:
  2594. * @ctx: the user data (XML parser context)
  2595. * @value: The pcdata content
  2596. * @len: the block length
  2597. *
  2598. * called when a pcdata block has been parsed
  2599. */
  2600. void
  2601. xmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len)
  2602. {
  2603. xmlSAX2Text((xmlParserCtxtPtr) ctx, value, len, XML_CDATA_SECTION_NODE);
  2604. }
  2605. static int xmlSAX2DefaultVersionValue = 2;
  2606. #ifdef LIBXML_SAX1_ENABLED
  2607. /**
  2608. * xmlSAXDefaultVersion:
  2609. * @version: the version, 1 or 2
  2610. *
  2611. * Set the default version of SAX used globally by the library.
  2612. * By default, during initialization the default is set to 2.
  2613. * Note that it is generally a better coding style to use
  2614. * xmlSAXVersion() to set up the version explicitly for a given
  2615. * parsing context.
  2616. *
  2617. * Returns the previous value in case of success and -1 in case of error.
  2618. */
  2619. int
  2620. xmlSAXDefaultVersion(int version)
  2621. {
  2622. int ret = xmlSAX2DefaultVersionValue;
  2623. if ((version != 1) && (version != 2))
  2624. return(-1);
  2625. xmlSAX2DefaultVersionValue = version;
  2626. return(ret);
  2627. }
  2628. #endif /* LIBXML_SAX1_ENABLED */
  2629. /**
  2630. * xmlSAXVersion:
  2631. * @hdlr: the SAX handler
  2632. * @version: the version, 1 or 2
  2633. *
  2634. * Initialize the default XML SAX handler according to the version
  2635. *
  2636. * Returns 0 in case of success and -1 in case of error.
  2637. */
  2638. int
  2639. xmlSAXVersion(xmlSAXHandler *hdlr, int version)
  2640. {
  2641. if (hdlr == NULL) return(-1);
  2642. if (version == 2) {
  2643. hdlr->startElement = NULL;
  2644. hdlr->endElement = NULL;
  2645. hdlr->startElementNs = xmlSAX2StartElementNs;
  2646. hdlr->endElementNs = xmlSAX2EndElementNs;
  2647. hdlr->serror = NULL;
  2648. hdlr->initialized = XML_SAX2_MAGIC;
  2649. #ifdef LIBXML_SAX1_ENABLED
  2650. } else if (version == 1) {
  2651. hdlr->startElement = xmlSAX2StartElement;
  2652. hdlr->endElement = xmlSAX2EndElement;
  2653. hdlr->initialized = 1;
  2654. #endif /* LIBXML_SAX1_ENABLED */
  2655. } else
  2656. return(-1);
  2657. hdlr->internalSubset = xmlSAX2InternalSubset;
  2658. hdlr->externalSubset = xmlSAX2ExternalSubset;
  2659. hdlr->isStandalone = xmlSAX2IsStandalone;
  2660. hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
  2661. hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
  2662. hdlr->resolveEntity = xmlSAX2ResolveEntity;
  2663. hdlr->getEntity = xmlSAX2GetEntity;
  2664. hdlr->getParameterEntity = xmlSAX2GetParameterEntity;
  2665. hdlr->entityDecl = xmlSAX2EntityDecl;
  2666. hdlr->attributeDecl = xmlSAX2AttributeDecl;
  2667. hdlr->elementDecl = xmlSAX2ElementDecl;
  2668. hdlr->notationDecl = xmlSAX2NotationDecl;
  2669. hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl;
  2670. hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
  2671. hdlr->startDocument = xmlSAX2StartDocument;
  2672. hdlr->endDocument = xmlSAX2EndDocument;
  2673. hdlr->reference = xmlSAX2Reference;
  2674. hdlr->characters = xmlSAX2Characters;
  2675. hdlr->cdataBlock = xmlSAX2CDataBlock;
  2676. hdlr->ignorableWhitespace = xmlSAX2Characters;
  2677. hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
  2678. hdlr->comment = xmlSAX2Comment;
  2679. hdlr->warning = xmlParserWarning;
  2680. hdlr->error = xmlParserError;
  2681. hdlr->fatalError = xmlParserError;
  2682. return(0);
  2683. }
  2684. /**
  2685. * xmlSAX2InitDefaultSAXHandler:
  2686. * @hdlr: the SAX handler
  2687. * @warning: flag if non-zero sets the handler warning procedure
  2688. *
  2689. * Initialize the default XML SAX2 handler
  2690. */
  2691. void
  2692. xmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
  2693. {
  2694. if ((hdlr == NULL) || (hdlr->initialized != 0))
  2695. return;
  2696. xmlSAXVersion(hdlr, xmlSAX2DefaultVersionValue);
  2697. if (warning == 0)
  2698. hdlr->warning = NULL;
  2699. else
  2700. hdlr->warning = xmlParserWarning;
  2701. }
  2702. /**
  2703. * xmlDefaultSAXHandlerInit:
  2704. *
  2705. * Initialize the default SAX2 handler
  2706. */
  2707. void
  2708. xmlDefaultSAXHandlerInit(void)
  2709. {
  2710. #ifdef LIBXML_SAX1_ENABLED
  2711. xmlSAXVersion((xmlSAXHandlerPtr) &xmlDefaultSAXHandler, 1);
  2712. #endif /* LIBXML_SAX1_ENABLED */
  2713. }
  2714. #ifdef LIBXML_HTML_ENABLED
  2715. /**
  2716. * xmlSAX2InitHtmlDefaultSAXHandler:
  2717. * @hdlr: the SAX handler
  2718. *
  2719. * Initialize the default HTML SAX2 handler
  2720. */
  2721. void
  2722. xmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
  2723. {
  2724. if ((hdlr == NULL) || (hdlr->initialized != 0))
  2725. return;
  2726. hdlr->internalSubset = xmlSAX2InternalSubset;
  2727. hdlr->externalSubset = NULL;
  2728. hdlr->isStandalone = NULL;
  2729. hdlr->hasInternalSubset = NULL;
  2730. hdlr->hasExternalSubset = NULL;
  2731. hdlr->resolveEntity = NULL;
  2732. hdlr->getEntity = xmlSAX2GetEntity;
  2733. hdlr->getParameterEntity = NULL;
  2734. hdlr->entityDecl = NULL;
  2735. hdlr->attributeDecl = NULL;
  2736. hdlr->elementDecl = NULL;
  2737. hdlr->notationDecl = NULL;
  2738. hdlr->unparsedEntityDecl = NULL;
  2739. hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
  2740. hdlr->startDocument = xmlSAX2StartDocument;
  2741. hdlr->endDocument = xmlSAX2EndDocument;
  2742. hdlr->startElement = xmlSAX2StartElement;
  2743. hdlr->endElement = xmlSAX2EndElement;
  2744. hdlr->reference = NULL;
  2745. hdlr->characters = xmlSAX2Characters;
  2746. hdlr->cdataBlock = xmlSAX2CDataBlock;
  2747. hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
  2748. hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
  2749. hdlr->comment = xmlSAX2Comment;
  2750. hdlr->warning = xmlParserWarning;
  2751. hdlr->error = xmlParserError;
  2752. hdlr->fatalError = xmlParserError;
  2753. hdlr->initialized = 1;
  2754. }
  2755. /**
  2756. * htmlDefaultSAXHandlerInit:
  2757. *
  2758. * Initialize the default SAX handler
  2759. */
  2760. void
  2761. htmlDefaultSAXHandlerInit(void)
  2762. {
  2763. xmlSAX2InitHtmlDefaultSAXHandler((xmlSAXHandlerPtr) &htmlDefaultSAXHandler);
  2764. }
  2765. #endif /* LIBXML_HTML_ENABLED */
  2766. #ifdef LIBXML_DOCB_ENABLED
  2767. /**
  2768. * xmlSAX2InitDocbDefaultSAXHandler:
  2769. * @hdlr: the SAX handler
  2770. *
  2771. * Initialize the default DocBook SAX2 handler
  2772. */
  2773. void
  2774. xmlSAX2InitDocbDefaultSAXHandler(xmlSAXHandler *hdlr)
  2775. {
  2776. if ((hdlr == NULL) || (hdlr->initialized != 0))
  2777. return;
  2778. hdlr->internalSubset = xmlSAX2InternalSubset;
  2779. hdlr->externalSubset = NULL;
  2780. hdlr->isStandalone = xmlSAX2IsStandalone;
  2781. hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
  2782. hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
  2783. hdlr->resolveEntity = xmlSAX2ResolveEntity;
  2784. hdlr->getEntity = xmlSAX2GetEntity;
  2785. hdlr->getParameterEntity = NULL;
  2786. hdlr->entityDecl = xmlSAX2EntityDecl;
  2787. hdlr->attributeDecl = NULL;
  2788. hdlr->elementDecl = NULL;
  2789. hdlr->notationDecl = NULL;
  2790. hdlr->unparsedEntityDecl = NULL;
  2791. hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
  2792. hdlr->startDocument = xmlSAX2StartDocument;
  2793. hdlr->endDocument = xmlSAX2EndDocument;
  2794. hdlr->startElement = xmlSAX2StartElement;
  2795. hdlr->endElement = xmlSAX2EndElement;
  2796. hdlr->reference = xmlSAX2Reference;
  2797. hdlr->characters = xmlSAX2Characters;
  2798. hdlr->cdataBlock = NULL;
  2799. hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
  2800. hdlr->processingInstruction = NULL;
  2801. hdlr->comment = xmlSAX2Comment;
  2802. hdlr->warning = xmlParserWarning;
  2803. hdlr->error = xmlParserError;
  2804. hdlr->fatalError = xmlParserError;
  2805. hdlr->initialized = 1;
  2806. }
  2807. /**
  2808. * docbDefaultSAXHandlerInit:
  2809. *
  2810. * Initialize the default SAX handler
  2811. */
  2812. void
  2813. docbDefaultSAXHandlerInit(void)
  2814. {
  2815. xmlSAX2InitDocbDefaultSAXHandler((xmlSAXHandlerPtr) &docbDefaultSAXHandler);
  2816. }
  2817. #endif /* LIBXML_DOCB_ENABLED */
  2818. #define bottom_SAX2
  2819. #include "elfgcchack.h"