SAX2.c 84 KB

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