SAX2.c 84 KB

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