SAX2.c 85 KB

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