SAX2.c 81 KB

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