SAX2.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945
  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_HTML_ENABLED
  1032. if ((ctxt->html) &&
  1033. (value == NULL) && (htmlIsBooleanAttr(fullname))) {
  1034. nval = xmlStrdup(fullname);
  1035. value = (const xmlChar *) nval;
  1036. } else
  1037. #endif
  1038. {
  1039. #ifdef LIBXML_VALID_ENABLED
  1040. /*
  1041. * Do the last stage of the attribute normalization
  1042. * Needed for HTML too:
  1043. * http://www.w3.org/TR/html4/types.html#h-6.2
  1044. */
  1045. ctxt->vctxt.valid = 1;
  1046. nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
  1047. ctxt->myDoc, ctxt->node,
  1048. fullname, value);
  1049. if (ctxt->vctxt.valid != 1) {
  1050. ctxt->valid = 0;
  1051. }
  1052. if (nval != NULL)
  1053. value = nval;
  1054. #else
  1055. nval = NULL;
  1056. #endif /* LIBXML_VALID_ENABLED */
  1057. }
  1058. /*
  1059. * Check whether it's a namespace definition
  1060. */
  1061. if ((!ctxt->html) && (ns == NULL) &&
  1062. (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
  1063. (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
  1064. xmlNsPtr nsret;
  1065. xmlChar *val;
  1066. if (!ctxt->replaceEntities) {
  1067. ctxt->depth++;
  1068. val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
  1069. 0,0,0);
  1070. ctxt->depth--;
  1071. } else {
  1072. val = (xmlChar *) value;
  1073. }
  1074. if (val[0] != 0) {
  1075. xmlURIPtr uri;
  1076. uri = xmlParseURI((const char *)val);
  1077. if (uri == NULL) {
  1078. if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
  1079. ctxt->sax->warning(ctxt->userData,
  1080. "xmlns: %s not a valid URI\n", val);
  1081. } else {
  1082. if (uri->scheme == NULL) {
  1083. if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
  1084. ctxt->sax->warning(ctxt->userData,
  1085. "xmlns: URI %s is not absolute\n", val);
  1086. }
  1087. xmlFreeURI(uri);
  1088. }
  1089. }
  1090. /* a default namespace definition */
  1091. nsret = xmlNewNs(ctxt->node, val, NULL);
  1092. #ifdef LIBXML_VALID_ENABLED
  1093. /*
  1094. * Validate also for namespace decls, they are attributes from
  1095. * an XML-1.0 perspective
  1096. */
  1097. if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
  1098. ctxt->myDoc && ctxt->myDoc->intSubset)
  1099. ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
  1100. ctxt->node, prefix, nsret, val);
  1101. #endif /* LIBXML_VALID_ENABLED */
  1102. if (name != NULL)
  1103. xmlFree(name);
  1104. if (nval != NULL)
  1105. xmlFree(nval);
  1106. if (val != value)
  1107. xmlFree(val);
  1108. return;
  1109. }
  1110. if ((!ctxt->html) &&
  1111. (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
  1112. (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
  1113. xmlNsPtr nsret;
  1114. xmlChar *val;
  1115. if (!ctxt->replaceEntities) {
  1116. ctxt->depth++;
  1117. val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
  1118. 0,0,0);
  1119. ctxt->depth--;
  1120. if (val == NULL) {
  1121. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
  1122. xmlFree(ns);
  1123. if (name != NULL)
  1124. xmlFree(name);
  1125. return;
  1126. }
  1127. } else {
  1128. val = (xmlChar *) value;
  1129. }
  1130. if (val[0] == 0) {
  1131. xmlNsErrMsg(ctxt, XML_NS_ERR_EMPTY,
  1132. "Empty namespace name for prefix %s\n", name, NULL);
  1133. }
  1134. if ((ctxt->pedantic != 0) && (val[0] != 0)) {
  1135. xmlURIPtr uri;
  1136. uri = xmlParseURI((const char *)val);
  1137. if (uri == NULL) {
  1138. xmlNsWarnMsg(ctxt, XML_WAR_NS_URI,
  1139. "xmlns:%s: %s not a valid URI\n", name, value);
  1140. } else {
  1141. if (uri->scheme == NULL) {
  1142. xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
  1143. "xmlns:%s: URI %s is not absolute\n", name, value);
  1144. }
  1145. xmlFreeURI(uri);
  1146. }
  1147. }
  1148. /* a standard namespace definition */
  1149. nsret = xmlNewNs(ctxt->node, val, name);
  1150. xmlFree(ns);
  1151. #ifdef LIBXML_VALID_ENABLED
  1152. /*
  1153. * Validate also for namespace decls, they are attributes from
  1154. * an XML-1.0 perspective
  1155. */
  1156. if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
  1157. ctxt->myDoc && ctxt->myDoc->intSubset)
  1158. ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
  1159. ctxt->node, prefix, nsret, value);
  1160. #endif /* LIBXML_VALID_ENABLED */
  1161. if (name != NULL)
  1162. xmlFree(name);
  1163. if (nval != NULL)
  1164. xmlFree(nval);
  1165. if (val != value)
  1166. xmlFree(val);
  1167. return;
  1168. }
  1169. if (ns != NULL) {
  1170. namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
  1171. if (namespace == NULL) {
  1172. xmlNsErrMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
  1173. "Namespace prefix %s of attribute %s is not defined\n",
  1174. ns, name);
  1175. } else {
  1176. xmlAttrPtr prop;
  1177. prop = ctxt->node->properties;
  1178. while (prop != NULL) {
  1179. if (prop->ns != NULL) {
  1180. if ((xmlStrEqual(name, prop->name)) &&
  1181. ((namespace == prop->ns) ||
  1182. (xmlStrEqual(namespace->href, prop->ns->href)))) {
  1183. xmlNsErrMsg(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
  1184. "Attribute %s in %s redefined\n",
  1185. name, namespace->href);
  1186. ctxt->wellFormed = 0;
  1187. if (ctxt->recovery == 0) ctxt->disableSAX = 1;
  1188. goto error;
  1189. }
  1190. }
  1191. prop = prop->next;
  1192. }
  1193. }
  1194. } else {
  1195. namespace = NULL;
  1196. }
  1197. /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
  1198. ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
  1199. if (ret != NULL) {
  1200. if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
  1201. xmlNodePtr tmp;
  1202. ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
  1203. tmp = ret->children;
  1204. while (tmp != NULL) {
  1205. tmp->parent = (xmlNodePtr) ret;
  1206. if (tmp->next == NULL)
  1207. ret->last = tmp;
  1208. tmp = tmp->next;
  1209. }
  1210. } else if (value != NULL) {
  1211. ret->children = xmlNewDocText(ctxt->myDoc, value);
  1212. ret->last = ret->children;
  1213. if (ret->children != NULL)
  1214. ret->children->parent = (xmlNodePtr) ret;
  1215. }
  1216. }
  1217. #ifdef LIBXML_VALID_ENABLED
  1218. if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
  1219. ctxt->myDoc && ctxt->myDoc->intSubset) {
  1220. /*
  1221. * If we don't substitute entities, the validation should be
  1222. * done on a value with replaced entities anyway.
  1223. */
  1224. if (!ctxt->replaceEntities) {
  1225. xmlChar *val;
  1226. ctxt->depth++;
  1227. val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
  1228. 0,0,0);
  1229. ctxt->depth--;
  1230. if (val == NULL)
  1231. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
  1232. ctxt->myDoc, ctxt->node, ret, value);
  1233. else {
  1234. xmlChar *nvalnorm;
  1235. /*
  1236. * Do the last stage of the attribute normalization
  1237. * It need to be done twice ... it's an extra burden related
  1238. * to the ability to keep xmlSAX2References in attributes
  1239. */
  1240. nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
  1241. ctxt->node, fullname, val);
  1242. if (nvalnorm != NULL) {
  1243. xmlFree(val);
  1244. val = nvalnorm;
  1245. }
  1246. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
  1247. ctxt->myDoc, ctxt->node, ret, val);
  1248. xmlFree(val);
  1249. }
  1250. } else {
  1251. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
  1252. ctxt->node, ret, value);
  1253. }
  1254. } else
  1255. #endif /* LIBXML_VALID_ENABLED */
  1256. if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
  1257. (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
  1258. ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
  1259. /*
  1260. * when validating, the ID registration is done at the attribute
  1261. * validation level. Otherwise we have to do specific handling here.
  1262. */
  1263. if (xmlStrEqual(fullname, BAD_CAST "xml:id")) {
  1264. /*
  1265. * Add the xml:id value
  1266. *
  1267. * Open issue: normalization of the value.
  1268. */
  1269. if (xmlValidateNCName(value, 1) != 0) {
  1270. xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
  1271. "xml:id : attribute value %s is not an NCName\n",
  1272. (const char *) value, NULL);
  1273. }
  1274. xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
  1275. } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
  1276. xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
  1277. else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
  1278. xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
  1279. }
  1280. error:
  1281. if (nval != NULL)
  1282. xmlFree(nval);
  1283. if (ns != NULL)
  1284. xmlFree(ns);
  1285. }
  1286. /*
  1287. * xmlCheckDefaultedAttributes:
  1288. *
  1289. * Check defaulted attributes from the DTD
  1290. */
  1291. static void
  1292. xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
  1293. const xmlChar *prefix, const xmlChar **atts) {
  1294. xmlElementPtr elemDecl;
  1295. const xmlChar *att;
  1296. int internal = 1;
  1297. int i;
  1298. elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
  1299. if (elemDecl == NULL) {
  1300. elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
  1301. internal = 0;
  1302. }
  1303. process_external_subset:
  1304. if (elemDecl != NULL) {
  1305. xmlAttributePtr attr = elemDecl->attributes;
  1306. /*
  1307. * Check against defaulted attributes from the external subset
  1308. * if the document is stamped as standalone
  1309. */
  1310. if ((ctxt->myDoc->standalone == 1) &&
  1311. (ctxt->myDoc->extSubset != NULL) &&
  1312. (ctxt->validate)) {
  1313. while (attr != NULL) {
  1314. if ((attr->defaultValue != NULL) &&
  1315. (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
  1316. attr->elem, attr->name,
  1317. attr->prefix) == attr) &&
  1318. (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
  1319. attr->elem, attr->name,
  1320. attr->prefix) == NULL)) {
  1321. xmlChar *fulln;
  1322. if (attr->prefix != NULL) {
  1323. fulln = xmlStrdup(attr->prefix);
  1324. fulln = xmlStrcat(fulln, BAD_CAST ":");
  1325. fulln = xmlStrcat(fulln, attr->name);
  1326. } else {
  1327. fulln = xmlStrdup(attr->name);
  1328. }
  1329. if (fulln == NULL) {
  1330. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
  1331. break;
  1332. }
  1333. /*
  1334. * Check that the attribute is not declared in the
  1335. * serialization
  1336. */
  1337. att = NULL;
  1338. if (atts != NULL) {
  1339. i = 0;
  1340. att = atts[i];
  1341. while (att != NULL) {
  1342. if (xmlStrEqual(att, fulln))
  1343. break;
  1344. i += 2;
  1345. att = atts[i];
  1346. }
  1347. }
  1348. if (att == NULL) {
  1349. xmlErrValid(ctxt, XML_DTD_STANDALONE_DEFAULTED,
  1350. "standalone: attribute %s on %s defaulted from external subset\n",
  1351. (const char *)fulln,
  1352. (const char *)attr->elem);
  1353. }
  1354. xmlFree(fulln);
  1355. }
  1356. attr = attr->nexth;
  1357. }
  1358. }
  1359. /*
  1360. * Actually insert defaulted values when needed
  1361. */
  1362. attr = elemDecl->attributes;
  1363. while (attr != NULL) {
  1364. /*
  1365. * Make sure that attributes redefinition occuring in the
  1366. * internal subset are not overriden by definitions in the
  1367. * external subset.
  1368. */
  1369. if (attr->defaultValue != NULL) {
  1370. /*
  1371. * the element should be instantiated in the tree if:
  1372. * - this is a namespace prefix
  1373. * - the user required for completion in the tree
  1374. * like XSLT
  1375. * - there isn't already an attribute definition
  1376. * in the internal subset overriding it.
  1377. */
  1378. if (((attr->prefix != NULL) &&
  1379. (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
  1380. ((attr->prefix == NULL) &&
  1381. (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
  1382. (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
  1383. xmlAttributePtr tst;
  1384. tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
  1385. attr->elem, attr->name,
  1386. attr->prefix);
  1387. if ((tst == attr) || (tst == NULL)) {
  1388. xmlChar fn[50];
  1389. xmlChar *fulln;
  1390. fulln = xmlBuildQName(attr->name, attr->prefix, fn, 50);
  1391. if (fulln == NULL) {
  1392. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
  1393. return;
  1394. }
  1395. /*
  1396. * Check that the attribute is not declared in the
  1397. * serialization
  1398. */
  1399. att = NULL;
  1400. if (atts != NULL) {
  1401. i = 0;
  1402. att = atts[i];
  1403. while (att != NULL) {
  1404. if (xmlStrEqual(att, fulln))
  1405. break;
  1406. i += 2;
  1407. att = atts[i];
  1408. }
  1409. }
  1410. if (att == NULL) {
  1411. xmlSAX2AttributeInternal(ctxt, fulln,
  1412. attr->defaultValue, prefix);
  1413. }
  1414. if ((fulln != fn) && (fulln != attr->name))
  1415. xmlFree(fulln);
  1416. }
  1417. }
  1418. }
  1419. attr = attr->nexth;
  1420. }
  1421. if (internal == 1) {
  1422. elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
  1423. name, prefix);
  1424. internal = 0;
  1425. goto process_external_subset;
  1426. }
  1427. }
  1428. }
  1429. /**
  1430. * xmlSAX2StartElement:
  1431. * @ctx: the user data (XML parser context)
  1432. * @fullname: The element name, including namespace prefix
  1433. * @atts: An array of name/value attributes pairs, NULL terminated
  1434. *
  1435. * called when an opening tag has been processed.
  1436. */
  1437. void
  1438. xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
  1439. {
  1440. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  1441. xmlNodePtr ret;
  1442. xmlNodePtr parent;
  1443. xmlNsPtr ns;
  1444. xmlChar *name;
  1445. xmlChar *prefix;
  1446. const xmlChar *att;
  1447. const xmlChar *value;
  1448. int i;
  1449. if ((ctx == NULL) || (fullname == NULL) || (ctxt->myDoc == NULL)) return;
  1450. parent = ctxt->node;
  1451. #ifdef DEBUG_SAX
  1452. xmlGenericError(xmlGenericErrorContext,
  1453. "SAX.xmlSAX2StartElement(%s)\n", fullname);
  1454. #endif
  1455. /*
  1456. * First check on validity:
  1457. */
  1458. if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
  1459. ((ctxt->myDoc->intSubset == NULL) ||
  1460. ((ctxt->myDoc->intSubset->notations == NULL) &&
  1461. (ctxt->myDoc->intSubset->elements == NULL) &&
  1462. (ctxt->myDoc->intSubset->attributes == NULL) &&
  1463. (ctxt->myDoc->intSubset->entities == NULL)))) {
  1464. xmlErrValid(ctxt, XML_ERR_NO_DTD,
  1465. "Validation failed: no DTD found !", NULL, NULL);
  1466. ctxt->validate = 0;
  1467. }
  1468. /*
  1469. * Split the full name into a namespace prefix and the tag name
  1470. */
  1471. name = xmlSplitQName(ctxt, fullname, &prefix);
  1472. /*
  1473. * Note : the namespace resolution is deferred until the end of the
  1474. * attributes parsing, since local namespace can be defined as
  1475. * an attribute at this level.
  1476. */
  1477. ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
  1478. if (ret == NULL) {
  1479. if (prefix != NULL)
  1480. xmlFree(prefix);
  1481. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
  1482. return;
  1483. }
  1484. if (ctxt->myDoc->children == NULL) {
  1485. #ifdef DEBUG_SAX_TREE
  1486. xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
  1487. #endif
  1488. xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
  1489. } else if (parent == NULL) {
  1490. parent = ctxt->myDoc->children;
  1491. }
  1492. ctxt->nodemem = -1;
  1493. if (ctxt->linenumbers) {
  1494. if (ctxt->input != NULL) {
  1495. if (ctxt->input->line < 65535)
  1496. ret->line = (short) ctxt->input->line;
  1497. else
  1498. ret->line = 65535;
  1499. }
  1500. }
  1501. /*
  1502. * We are parsing a new node.
  1503. */
  1504. #ifdef DEBUG_SAX_TREE
  1505. xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
  1506. #endif
  1507. nodePush(ctxt, ret);
  1508. /*
  1509. * Link the child element
  1510. */
  1511. if (parent != NULL) {
  1512. if (parent->type == XML_ELEMENT_NODE) {
  1513. #ifdef DEBUG_SAX_TREE
  1514. xmlGenericError(xmlGenericErrorContext,
  1515. "adding child %s to %s\n", name, parent->name);
  1516. #endif
  1517. xmlAddChild(parent, ret);
  1518. } else {
  1519. #ifdef DEBUG_SAX_TREE
  1520. xmlGenericError(xmlGenericErrorContext,
  1521. "adding sibling %s to ", name);
  1522. xmlDebugDumpOneNode(stderr, parent, 0);
  1523. #endif
  1524. xmlAddSibling(parent, ret);
  1525. }
  1526. }
  1527. /*
  1528. * Insert all the defaulted attributes from the DTD especially namespaces
  1529. */
  1530. if ((!ctxt->html) &&
  1531. ((ctxt->myDoc->intSubset != NULL) ||
  1532. (ctxt->myDoc->extSubset != NULL))) {
  1533. xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
  1534. }
  1535. /*
  1536. * process all the attributes whose name start with "xmlns"
  1537. */
  1538. if (atts != NULL) {
  1539. i = 0;
  1540. att = atts[i++];
  1541. value = atts[i++];
  1542. if (!ctxt->html) {
  1543. while ((att != NULL) && (value != NULL)) {
  1544. if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
  1545. (att[3] == 'n') && (att[4] == 's'))
  1546. xmlSAX2AttributeInternal(ctxt, att, value, prefix);
  1547. att = atts[i++];
  1548. value = atts[i++];
  1549. }
  1550. }
  1551. }
  1552. /*
  1553. * Search the namespace, note that since the attributes have been
  1554. * processed, the local namespaces are available.
  1555. */
  1556. ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
  1557. if ((ns == NULL) && (parent != NULL))
  1558. ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
  1559. if ((prefix != NULL) && (ns == NULL)) {
  1560. ns = xmlNewNs(ret, NULL, prefix);
  1561. xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
  1562. "Namespace prefix %s is not defined\n",
  1563. prefix, NULL);
  1564. }
  1565. /*
  1566. * set the namespace node, making sure that if the default namspace
  1567. * is unbound on a parent we simply kee it NULL
  1568. */
  1569. if ((ns != NULL) && (ns->href != NULL) &&
  1570. ((ns->href[0] != 0) || (ns->prefix != NULL)))
  1571. xmlSetNs(ret, ns);
  1572. /*
  1573. * process all the other attributes
  1574. */
  1575. if (atts != NULL) {
  1576. i = 0;
  1577. att = atts[i++];
  1578. value = atts[i++];
  1579. if (ctxt->html) {
  1580. while (att != NULL) {
  1581. xmlSAX2AttributeInternal(ctxt, att, value, NULL);
  1582. att = atts[i++];
  1583. value = atts[i++];
  1584. }
  1585. } else {
  1586. while ((att != NULL) && (value != NULL)) {
  1587. if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
  1588. (att[3] != 'n') || (att[4] != 's'))
  1589. xmlSAX2AttributeInternal(ctxt, att, value, NULL);
  1590. /*
  1591. * Next ones
  1592. */
  1593. att = atts[i++];
  1594. value = atts[i++];
  1595. }
  1596. }
  1597. }
  1598. #ifdef LIBXML_VALID_ENABLED
  1599. /*
  1600. * If it's the Document root, finish the DTD validation and
  1601. * check the document root element for validity
  1602. */
  1603. if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
  1604. int chk;
  1605. chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
  1606. if (chk <= 0)
  1607. ctxt->valid = 0;
  1608. if (chk < 0)
  1609. ctxt->wellFormed = 0;
  1610. ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
  1611. ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
  1612. }
  1613. #endif /* LIBXML_VALID_ENABLED */
  1614. if (prefix != NULL)
  1615. xmlFree(prefix);
  1616. }
  1617. /**
  1618. * xmlSAX2EndElement:
  1619. * @ctx: the user data (XML parser context)
  1620. * @name: The element name
  1621. *
  1622. * called when the end of an element has been detected.
  1623. */
  1624. void
  1625. xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
  1626. {
  1627. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  1628. xmlParserNodeInfo node_info;
  1629. xmlNodePtr cur;
  1630. if (ctx == NULL) return;
  1631. cur = ctxt->node;
  1632. #ifdef DEBUG_SAX
  1633. if (name == NULL)
  1634. xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(NULL)\n");
  1635. else
  1636. xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(%s)\n", name);
  1637. #endif
  1638. /* Capture end position and add node */
  1639. if (cur != NULL && ctxt->record_info) {
  1640. node_info.end_pos = ctxt->input->cur - ctxt->input->base;
  1641. node_info.end_line = ctxt->input->line;
  1642. node_info.node = cur;
  1643. xmlParserAddNodeInfo(ctxt, &node_info);
  1644. }
  1645. ctxt->nodemem = -1;
  1646. #ifdef LIBXML_VALID_ENABLED
  1647. if (ctxt->validate && ctxt->wellFormed &&
  1648. ctxt->myDoc && ctxt->myDoc->intSubset)
  1649. ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
  1650. cur);
  1651. #endif /* LIBXML_VALID_ENABLED */
  1652. /*
  1653. * end of parsing of this node.
  1654. */
  1655. #ifdef DEBUG_SAX_TREE
  1656. xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
  1657. #endif
  1658. nodePop(ctxt);
  1659. }
  1660. #endif /* LIBXML_SAX1_ENABLED || LIBXML_HTML_ENABLE */
  1661. /*
  1662. * xmlSAX2TextNode:
  1663. * @ctxt: the parser context
  1664. * @str: the input string
  1665. * @len: the string length
  1666. *
  1667. * Remove the entities from an attribute value
  1668. *
  1669. * Returns the newly allocated string or NULL if not needed or error
  1670. */
  1671. static xmlNodePtr
  1672. xmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
  1673. xmlNodePtr ret;
  1674. const xmlChar *intern = NULL;
  1675. /*
  1676. * Allocate
  1677. */
  1678. if (ctxt->freeElems != NULL) {
  1679. ret = ctxt->freeElems;
  1680. ctxt->freeElems = ret->next;
  1681. ctxt->freeElemsNr--;
  1682. } else {
  1683. ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
  1684. }
  1685. if (ret == NULL) {
  1686. xmlErrMemory(ctxt, "xmlSAX2Characters");
  1687. return(NULL);
  1688. }
  1689. memset(ret, 0, sizeof(xmlNode));
  1690. /*
  1691. * intern the formatting blanks found between tags, or the
  1692. * very short strings
  1693. */
  1694. if (ctxt->dictNames) {
  1695. xmlChar cur = str[len];
  1696. if ((len < (int) (2 * sizeof(void *))) &&
  1697. (ctxt->options & XML_PARSE_COMPACT)) {
  1698. /* store the string in the node overrithing properties and nsDef */
  1699. xmlChar *tmp = (xmlChar *) &(ret->properties);
  1700. memcpy(tmp, str, len);
  1701. tmp[len] = 0;
  1702. intern = tmp;
  1703. } else if ((len <= 3) && ((cur == '"') || (cur == '\'') ||
  1704. ((cur == '<') && (str[len + 1] != '!')))) {
  1705. intern = xmlDictLookup(ctxt->dict, str, len);
  1706. } else if (IS_BLANK_CH(*str) && (len < 60) && (cur == '<') &&
  1707. (str[len + 1] != '!')) {
  1708. int i;
  1709. for (i = 1;i < len;i++) {
  1710. if (!IS_BLANK_CH(str[i])) goto skip;
  1711. }
  1712. intern = xmlDictLookup(ctxt->dict, str, len);
  1713. }
  1714. }
  1715. skip:
  1716. ret->type = XML_TEXT_NODE;
  1717. ret->name = xmlStringText;
  1718. if (intern == NULL) {
  1719. ret->content = xmlStrndup(str, len);
  1720. if (ret->content == NULL) {
  1721. xmlSAX2ErrMemory(ctxt, "xmlSAX2TextNode");
  1722. xmlFree(ret);
  1723. return(NULL);
  1724. }
  1725. } else
  1726. ret->content = (xmlChar *) intern;
  1727. if (ctxt->input != NULL)
  1728. ret->line = ctxt->input->line;
  1729. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  1730. xmlRegisterNodeDefaultValue(ret);
  1731. return(ret);
  1732. }
  1733. #ifdef LIBXML_VALID_ENABLED
  1734. /*
  1735. * xmlSAX2DecodeAttrEntities:
  1736. * @ctxt: the parser context
  1737. * @str: the input string
  1738. * @len: the string length
  1739. *
  1740. * Remove the entities from an attribute value
  1741. *
  1742. * Returns the newly allocated string or NULL if not needed or error
  1743. */
  1744. static xmlChar *
  1745. xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str,
  1746. const xmlChar *end) {
  1747. const xmlChar *in;
  1748. xmlChar *ret;
  1749. in = str;
  1750. while (in < end)
  1751. if (*in++ == '&')
  1752. goto decode;
  1753. return(NULL);
  1754. decode:
  1755. ctxt->depth++;
  1756. ret = xmlStringLenDecodeEntities(ctxt, str, end - str,
  1757. XML_SUBSTITUTE_REF, 0,0,0);
  1758. ctxt->depth--;
  1759. return(ret);
  1760. }
  1761. #endif /* LIBXML_VALID_ENABLED */
  1762. /**
  1763. * xmlSAX2AttributeNs:
  1764. * @ctx: the user data (XML parser context)
  1765. * @localname: the local name of the attribute
  1766. * @prefix: the attribute namespace prefix if available
  1767. * @URI: the attribute namespace name if available
  1768. * @value: Start of the attribute value
  1769. * @valueend: end of the attribute value
  1770. *
  1771. * Handle an attribute that has been read by the parser.
  1772. * The default handling is to convert the attribute into an
  1773. * DOM subtree and past it in a new xmlAttr element added to
  1774. * the element.
  1775. */
  1776. static void
  1777. xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
  1778. const xmlChar * localname,
  1779. const xmlChar * prefix,
  1780. const xmlChar * value,
  1781. const xmlChar * valueend)
  1782. {
  1783. xmlAttrPtr ret;
  1784. xmlNsPtr namespace = NULL;
  1785. xmlChar *dup = NULL;
  1786. /*
  1787. * Note: if prefix == NULL, the attribute is not in the default namespace
  1788. */
  1789. if (prefix != NULL)
  1790. namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, prefix);
  1791. /*
  1792. * allocate the node
  1793. */
  1794. if (ctxt->freeAttrs != NULL) {
  1795. ret = ctxt->freeAttrs;
  1796. ctxt->freeAttrs = ret->next;
  1797. ctxt->freeAttrsNr--;
  1798. memset(ret, 0, sizeof(xmlAttr));
  1799. ret->type = XML_ATTRIBUTE_NODE;
  1800. ret->parent = ctxt->node;
  1801. ret->doc = ctxt->myDoc;
  1802. ret->ns = namespace;
  1803. if (ctxt->dictNames)
  1804. ret->name = localname;
  1805. else
  1806. ret->name = xmlStrdup(localname);
  1807. /* link at the end to preserv order, TODO speed up with a last */
  1808. if (ctxt->node->properties == NULL) {
  1809. ctxt->node->properties = ret;
  1810. } else {
  1811. xmlAttrPtr prev = ctxt->node->properties;
  1812. while (prev->next != NULL) prev = prev->next;
  1813. prev->next = ret;
  1814. ret->prev = prev;
  1815. }
  1816. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  1817. xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
  1818. } else {
  1819. if (ctxt->dictNames)
  1820. ret = xmlNewNsPropEatName(ctxt->node, namespace,
  1821. (xmlChar *) localname, NULL);
  1822. else
  1823. ret = xmlNewNsProp(ctxt->node, namespace, localname, NULL);
  1824. if (ret == NULL) {
  1825. xmlErrMemory(ctxt, "xmlSAX2AttributeNs");
  1826. return;
  1827. }
  1828. }
  1829. if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
  1830. xmlNodePtr tmp;
  1831. /*
  1832. * We know that if there is an entity reference, then
  1833. * the string has been dup'ed and terminates with 0
  1834. * otherwise with ' or "
  1835. */
  1836. if (*valueend != 0) {
  1837. tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
  1838. ret->children = tmp;
  1839. ret->last = tmp;
  1840. if (tmp != NULL) {
  1841. tmp->doc = ret->doc;
  1842. tmp->parent = (xmlNodePtr) ret;
  1843. }
  1844. } else {
  1845. ret->children = xmlStringLenGetNodeList(ctxt->myDoc, value,
  1846. valueend - value);
  1847. tmp = ret->children;
  1848. while (tmp != NULL) {
  1849. tmp->doc = ret->doc;
  1850. tmp->parent = (xmlNodePtr) ret;
  1851. if (tmp->next == NULL)
  1852. ret->last = tmp;
  1853. tmp = tmp->next;
  1854. }
  1855. }
  1856. } else if (value != NULL) {
  1857. xmlNodePtr tmp;
  1858. tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
  1859. ret->children = tmp;
  1860. ret->last = tmp;
  1861. if (tmp != NULL) {
  1862. tmp->doc = ret->doc;
  1863. tmp->parent = (xmlNodePtr) ret;
  1864. }
  1865. }
  1866. #ifdef LIBXML_VALID_ENABLED
  1867. if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
  1868. ctxt->myDoc && ctxt->myDoc->intSubset) {
  1869. /*
  1870. * If we don't substitute entities, the validation should be
  1871. * done on a value with replaced entities anyway.
  1872. */
  1873. if (!ctxt->replaceEntities) {
  1874. dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend);
  1875. if (dup == NULL) {
  1876. if (*valueend == 0) {
  1877. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
  1878. ctxt->myDoc, ctxt->node, ret, value);
  1879. } else {
  1880. /*
  1881. * That should already be normalized.
  1882. * cheaper to finally allocate here than duplicate
  1883. * entry points in the full validation code
  1884. */
  1885. dup = xmlStrndup(value, valueend - value);
  1886. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
  1887. ctxt->myDoc, ctxt->node, ret, dup);
  1888. }
  1889. } else {
  1890. /*
  1891. * dup now contains a string of the flattened attribute
  1892. * content with entities substitued. Check if we need to
  1893. * apply an extra layer of normalization.
  1894. * It need to be done twice ... it's an extra burden related
  1895. * to the ability to keep references in attributes
  1896. */
  1897. if (ctxt->attsSpecial != NULL) {
  1898. xmlChar *nvalnorm;
  1899. xmlChar fn[50];
  1900. xmlChar *fullname;
  1901. fullname = xmlBuildQName(localname, prefix, fn, 50);
  1902. if (fullname != NULL) {
  1903. ctxt->vctxt.valid = 1;
  1904. nvalnorm = xmlValidCtxtNormalizeAttributeValue(
  1905. &ctxt->vctxt, ctxt->myDoc,
  1906. ctxt->node, fullname, dup);
  1907. if (ctxt->vctxt.valid != 1)
  1908. ctxt->valid = 0;
  1909. if ((fullname != fn) && (fullname != localname))
  1910. xmlFree(fullname);
  1911. if (nvalnorm != NULL) {
  1912. xmlFree(dup);
  1913. dup = nvalnorm;
  1914. }
  1915. }
  1916. }
  1917. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
  1918. ctxt->myDoc, ctxt->node, ret, dup);
  1919. }
  1920. } else {
  1921. /*
  1922. * if entities already have been substitued, then
  1923. * the attribute as passed is already normalized
  1924. */
  1925. dup = xmlStrndup(value, valueend - value);
  1926. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
  1927. ctxt->myDoc, ctxt->node, ret, dup);
  1928. }
  1929. } else
  1930. #endif /* LIBXML_VALID_ENABLED */
  1931. if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
  1932. (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
  1933. ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
  1934. /*
  1935. * when validating, the ID registration is done at the attribute
  1936. * validation level. Otherwise we have to do specific handling here.
  1937. */
  1938. if ((prefix == ctxt->str_xml) &&
  1939. (localname[0] == 'i') && (localname[1] == 'd') &&
  1940. (localname[2] == 0)) {
  1941. /*
  1942. * Add the xml:id value
  1943. *
  1944. * Open issue: normalization of the value.
  1945. */
  1946. if (dup == NULL)
  1947. dup = xmlStrndup(value, valueend - value);
  1948. #ifdef LIBXML_VALID_ENABLED
  1949. if (xmlValidateNCName(dup, 1) != 0) {
  1950. xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
  1951. "xml:id : attribute value %s is not an NCName\n",
  1952. (const char *) dup, NULL);
  1953. }
  1954. #endif
  1955. xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
  1956. } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) {
  1957. /* might be worth duplicate entry points and not copy */
  1958. if (dup == NULL)
  1959. dup = xmlStrndup(value, valueend - value);
  1960. xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
  1961. } else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) {
  1962. if (dup == NULL)
  1963. dup = xmlStrndup(value, valueend - value);
  1964. xmlAddRef(&ctxt->vctxt, ctxt->myDoc, dup, ret);
  1965. }
  1966. }
  1967. if (dup != NULL)
  1968. xmlFree(dup);
  1969. }
  1970. /**
  1971. * xmlSAX2StartElementNs:
  1972. * @ctx: the user data (XML parser context)
  1973. * @localname: the local name of the element
  1974. * @prefix: the element namespace prefix if available
  1975. * @URI: the element namespace name if available
  1976. * @nb_namespaces: number of namespace definitions on that node
  1977. * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
  1978. * @nb_attributes: the number of attributes on that node
  1979. * @nb_defaulted: the number of defaulted attributes.
  1980. * @attributes: pointer to the array of (localname/prefix/URI/value/end)
  1981. * attribute values.
  1982. *
  1983. * SAX2 callback when an element start has been detected by the parser.
  1984. * It provides the namespace informations for the element, as well as
  1985. * the new namespace declarations on the element.
  1986. */
  1987. void
  1988. xmlSAX2StartElementNs(void *ctx,
  1989. const xmlChar *localname,
  1990. const xmlChar *prefix,
  1991. const xmlChar *URI,
  1992. int nb_namespaces,
  1993. const xmlChar **namespaces,
  1994. int nb_attributes,
  1995. int nb_defaulted,
  1996. const xmlChar **attributes)
  1997. {
  1998. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  1999. xmlNodePtr ret;
  2000. xmlNodePtr parent;
  2001. xmlNsPtr last = NULL, ns;
  2002. const xmlChar *uri, *pref;
  2003. int i, j;
  2004. if (ctx == NULL) return;
  2005. parent = ctxt->node;
  2006. /*
  2007. * First check on validity:
  2008. */
  2009. if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
  2010. ((ctxt->myDoc->intSubset == NULL) ||
  2011. ((ctxt->myDoc->intSubset->notations == NULL) &&
  2012. (ctxt->myDoc->intSubset->elements == NULL) &&
  2013. (ctxt->myDoc->intSubset->attributes == NULL) &&
  2014. (ctxt->myDoc->intSubset->entities == NULL)))) {
  2015. xmlErrValid(ctxt, XML_ERR_NO_DTD,
  2016. "Validation failed: no DTD found !", NULL, NULL);
  2017. ctxt->validate = 0;
  2018. }
  2019. /*
  2020. * allocate the node
  2021. */
  2022. if (ctxt->freeElems != NULL) {
  2023. ret = ctxt->freeElems;
  2024. ctxt->freeElems = ret->next;
  2025. ctxt->freeElemsNr--;
  2026. memset(ret, 0, sizeof(xmlNode));
  2027. ret->type = XML_ELEMENT_NODE;
  2028. if (ctxt->dictNames)
  2029. ret->name = localname;
  2030. else {
  2031. ret->name = xmlStrdup(localname);
  2032. if (ret->name == NULL) {
  2033. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
  2034. return;
  2035. }
  2036. }
  2037. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  2038. xmlRegisterNodeDefaultValue(ret);
  2039. } else {
  2040. if (ctxt->dictNames)
  2041. ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
  2042. (xmlChar *) localname, NULL);
  2043. else
  2044. ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL);
  2045. if (ret == NULL) {
  2046. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
  2047. return;
  2048. }
  2049. }
  2050. if (ctxt->linenumbers) {
  2051. if (ctxt->input != NULL) {
  2052. if (ctxt->input->line < 65535)
  2053. ret->line = (short) ctxt->input->line;
  2054. else
  2055. ret->line = 65535;
  2056. }
  2057. }
  2058. if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
  2059. xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
  2060. }
  2061. /*
  2062. * Build the namespace list
  2063. */
  2064. for (i = 0,j = 0;j < nb_namespaces;j++) {
  2065. pref = namespaces[i++];
  2066. uri = namespaces[i++];
  2067. ns = xmlNewNs(NULL, uri, pref);
  2068. if (ns != NULL) {
  2069. if (last == NULL) {
  2070. ret->nsDef = last = ns;
  2071. } else {
  2072. last->next = ns;
  2073. last = ns;
  2074. }
  2075. if ((URI != NULL) && (prefix == pref))
  2076. ret->ns = ns;
  2077. } else {
  2078. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
  2079. return;
  2080. }
  2081. #ifdef LIBXML_VALID_ENABLED
  2082. if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
  2083. ctxt->myDoc && ctxt->myDoc->intSubset) {
  2084. ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
  2085. ret, prefix, ns, uri);
  2086. }
  2087. #endif /* LIBXML_VALID_ENABLED */
  2088. }
  2089. ctxt->nodemem = -1;
  2090. /*
  2091. * We are parsing a new node.
  2092. */
  2093. nodePush(ctxt, ret);
  2094. /*
  2095. * Link the child element
  2096. */
  2097. if (parent != NULL) {
  2098. if (parent->type == XML_ELEMENT_NODE) {
  2099. xmlAddChild(parent, ret);
  2100. } else {
  2101. xmlAddSibling(parent, ret);
  2102. }
  2103. }
  2104. /*
  2105. * Insert the defaulted attributes from the DTD only if requested:
  2106. */
  2107. if ((nb_defaulted != 0) &&
  2108. ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0))
  2109. nb_attributes -= nb_defaulted;
  2110. /*
  2111. * Search the namespace if it wasn't already found
  2112. * Note that, if prefix is NULL, this searches for the default Ns
  2113. */
  2114. if ((URI != NULL) && (ret->ns == NULL)) {
  2115. ret->ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
  2116. if ((ret->ns == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
  2117. ret->ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
  2118. }
  2119. if (ret->ns == NULL) {
  2120. ns = xmlNewNs(ret, NULL, prefix);
  2121. if (ns == NULL) {
  2122. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
  2123. return;
  2124. }
  2125. if (prefix != NULL)
  2126. xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
  2127. "Namespace prefix %s was not found\n",
  2128. prefix, NULL);
  2129. else
  2130. xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
  2131. "Namespace default prefix was not found\n",
  2132. NULL, NULL);
  2133. }
  2134. }
  2135. /*
  2136. * process all the other attributes
  2137. */
  2138. if (nb_attributes > 0) {
  2139. for (j = 0,i = 0;i < nb_attributes;i++,j+=5) {
  2140. xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
  2141. attributes[j+3], attributes[j+4]);
  2142. }
  2143. }
  2144. #ifdef LIBXML_VALID_ENABLED
  2145. /*
  2146. * If it's the Document root, finish the DTD validation and
  2147. * check the document root element for validity
  2148. */
  2149. if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
  2150. int chk;
  2151. chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
  2152. if (chk <= 0)
  2153. ctxt->valid = 0;
  2154. if (chk < 0)
  2155. ctxt->wellFormed = 0;
  2156. ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
  2157. ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
  2158. }
  2159. #endif /* LIBXML_VALID_ENABLED */
  2160. }
  2161. /**
  2162. * xmlSAX2EndElementNs:
  2163. * @ctx: the user data (XML parser context)
  2164. * @localname: the local name of the element
  2165. * @prefix: the element namespace prefix if available
  2166. * @URI: the element namespace name if available
  2167. *
  2168. * SAX2 callback when an element end has been detected by the parser.
  2169. * It provides the namespace informations for the element.
  2170. */
  2171. void
  2172. xmlSAX2EndElementNs(void *ctx,
  2173. const xmlChar * localname ATTRIBUTE_UNUSED,
  2174. const xmlChar * prefix ATTRIBUTE_UNUSED,
  2175. const xmlChar * URI ATTRIBUTE_UNUSED)
  2176. {
  2177. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  2178. xmlParserNodeInfo node_info;
  2179. xmlNodePtr cur;
  2180. if (ctx == NULL) return;
  2181. cur = ctxt->node;
  2182. /* Capture end position and add node */
  2183. if ((ctxt->record_info) && (cur != NULL)) {
  2184. node_info.end_pos = ctxt->input->cur - ctxt->input->base;
  2185. node_info.end_line = ctxt->input->line;
  2186. node_info.node = cur;
  2187. xmlParserAddNodeInfo(ctxt, &node_info);
  2188. }
  2189. ctxt->nodemem = -1;
  2190. #ifdef LIBXML_VALID_ENABLED
  2191. if (ctxt->validate && ctxt->wellFormed &&
  2192. ctxt->myDoc && ctxt->myDoc->intSubset)
  2193. ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, cur);
  2194. #endif /* LIBXML_VALID_ENABLED */
  2195. /*
  2196. * end of parsing of this node.
  2197. */
  2198. nodePop(ctxt);
  2199. }
  2200. /**
  2201. * xmlSAX2Reference:
  2202. * @ctx: the user data (XML parser context)
  2203. * @name: The entity name
  2204. *
  2205. * called when an entity xmlSAX2Reference is detected.
  2206. */
  2207. void
  2208. xmlSAX2Reference(void *ctx, const xmlChar *name)
  2209. {
  2210. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  2211. xmlNodePtr ret;
  2212. if (ctx == NULL) return;
  2213. #ifdef DEBUG_SAX
  2214. xmlGenericError(xmlGenericErrorContext,
  2215. "SAX.xmlSAX2Reference(%s)\n", name);
  2216. #endif
  2217. if (name[0] == '#')
  2218. ret = xmlNewCharRef(ctxt->myDoc, name);
  2219. else
  2220. ret = xmlNewReference(ctxt->myDoc, name);
  2221. #ifdef DEBUG_SAX_TREE
  2222. xmlGenericError(xmlGenericErrorContext,
  2223. "add xmlSAX2Reference %s to %s \n", name, ctxt->node->name);
  2224. #endif
  2225. if (xmlAddChild(ctxt->node, ret) == NULL) {
  2226. xmlFreeNode(ret);
  2227. }
  2228. }
  2229. /**
  2230. * xmlSAX2Characters:
  2231. * @ctx: the user data (XML parser context)
  2232. * @ch: a xmlChar string
  2233. * @len: the number of xmlChar
  2234. *
  2235. * receiving some chars from the parser.
  2236. */
  2237. void
  2238. xmlSAX2Characters(void *ctx, const xmlChar *ch, int len)
  2239. {
  2240. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  2241. xmlNodePtr lastChild;
  2242. if (ctx == NULL) return;
  2243. #ifdef DEBUG_SAX
  2244. xmlGenericError(xmlGenericErrorContext,
  2245. "SAX.xmlSAX2Characters(%.30s, %d)\n", ch, len);
  2246. #endif
  2247. /*
  2248. * Handle the data if any. If there is no child
  2249. * add it as content, otherwise if the last child is text,
  2250. * concatenate it, else create a new node of type text.
  2251. */
  2252. if (ctxt->node == NULL) {
  2253. #ifdef DEBUG_SAX_TREE
  2254. xmlGenericError(xmlGenericErrorContext,
  2255. "add chars: ctxt->node == NULL !\n");
  2256. #endif
  2257. return;
  2258. }
  2259. lastChild = ctxt->node->last;
  2260. #ifdef DEBUG_SAX_TREE
  2261. xmlGenericError(xmlGenericErrorContext,
  2262. "add chars to %s \n", ctxt->node->name);
  2263. #endif
  2264. /*
  2265. * Here we needed an accelerator mechanism in case of very large
  2266. * elements. Use an attribute in the structure !!!
  2267. */
  2268. if (lastChild == NULL) {
  2269. lastChild = xmlSAX2TextNode(ctxt, ch, len);
  2270. if (lastChild != NULL) {
  2271. ctxt->node->children = lastChild;
  2272. ctxt->node->last = lastChild;
  2273. lastChild->parent = ctxt->node;
  2274. lastChild->doc = ctxt->node->doc;
  2275. ctxt->nodelen = len;
  2276. ctxt->nodemem = len + 1;
  2277. } else {
  2278. xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
  2279. return;
  2280. }
  2281. } else {
  2282. int coalesceText = (lastChild != NULL) &&
  2283. (lastChild->type == XML_TEXT_NODE) &&
  2284. (lastChild->name == xmlStringText);
  2285. if ((coalesceText) && (ctxt->nodemem != 0)) {
  2286. /*
  2287. * The whole point of maintaining nodelen and nodemem,
  2288. * xmlTextConcat is too costly, i.e. compute length,
  2289. * reallocate a new buffer, move data, append ch. Here
  2290. * We try to minimaze realloc() uses and avoid copying
  2291. * and recomputing length over and over.
  2292. */
  2293. if (lastChild->content == (xmlChar *)&(lastChild->properties)) {
  2294. lastChild->content = xmlStrdup(lastChild->content);
  2295. lastChild->properties = NULL;
  2296. } else if ((ctxt->nodemem == ctxt->nodelen + 1) &&
  2297. (xmlDictOwns(ctxt->dict, lastChild->content))) {
  2298. lastChild->content = xmlStrdup(lastChild->content);
  2299. }
  2300. if (((size_t)ctxt->nodelen + (size_t)len > XML_MAX_TEXT_LENGTH) &&
  2301. ((ctxt->options & XML_PARSE_HUGE) == 0)) {
  2302. xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters: huge text node");
  2303. return;
  2304. }
  2305. if ((size_t)ctxt->nodelen > SIZE_T_MAX - (size_t)len ||
  2306. (size_t)ctxt->nodemem + (size_t)len > SIZE_T_MAX / 2) {
  2307. xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters overflow prevented");
  2308. return;
  2309. }
  2310. if (ctxt->nodelen + len >= ctxt->nodemem) {
  2311. xmlChar *newbuf;
  2312. size_t size;
  2313. size = ctxt->nodemem + len;
  2314. size *= 2;
  2315. newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
  2316. if (newbuf == NULL) {
  2317. xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
  2318. return;
  2319. }
  2320. ctxt->nodemem = size;
  2321. lastChild->content = newbuf;
  2322. }
  2323. memcpy(&lastChild->content[ctxt->nodelen], ch, len);
  2324. ctxt->nodelen += len;
  2325. lastChild->content[ctxt->nodelen] = 0;
  2326. } else if (coalesceText) {
  2327. if (xmlTextConcat(lastChild, ch, len)) {
  2328. xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
  2329. }
  2330. if (ctxt->node->children != NULL) {
  2331. ctxt->nodelen = xmlStrlen(lastChild->content);
  2332. ctxt->nodemem = ctxt->nodelen + 1;
  2333. }
  2334. } else {
  2335. /* Mixed content, first time */
  2336. lastChild = xmlSAX2TextNode(ctxt, ch, len);
  2337. if (lastChild != NULL) {
  2338. xmlAddChild(ctxt->node, lastChild);
  2339. if (ctxt->node->children != NULL) {
  2340. ctxt->nodelen = len;
  2341. ctxt->nodemem = len + 1;
  2342. }
  2343. }
  2344. }
  2345. }
  2346. }
  2347. /**
  2348. * xmlSAX2IgnorableWhitespace:
  2349. * @ctx: the user data (XML parser context)
  2350. * @ch: a xmlChar string
  2351. * @len: the number of xmlChar
  2352. *
  2353. * receiving some ignorable whitespaces from the parser.
  2354. * UNUSED: by default the DOM building will use xmlSAX2Characters
  2355. */
  2356. void
  2357. xmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
  2358. {
  2359. /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
  2360. #ifdef DEBUG_SAX
  2361. xmlGenericError(xmlGenericErrorContext,
  2362. "SAX.xmlSAX2IgnorableWhitespace(%.30s, %d)\n", ch, len);
  2363. #endif
  2364. }
  2365. /**
  2366. * xmlSAX2ProcessingInstruction:
  2367. * @ctx: the user data (XML parser context)
  2368. * @target: the target name
  2369. * @data: the PI data's
  2370. *
  2371. * A processing instruction has been parsed.
  2372. */
  2373. void
  2374. xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target,
  2375. const xmlChar *data)
  2376. {
  2377. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  2378. xmlNodePtr ret;
  2379. xmlNodePtr parent;
  2380. if (ctx == NULL) return;
  2381. parent = ctxt->node;
  2382. #ifdef DEBUG_SAX
  2383. xmlGenericError(xmlGenericErrorContext,
  2384. "SAX.xmlSAX2ProcessingInstruction(%s, %s)\n", target, data);
  2385. #endif
  2386. ret = xmlNewDocPI(ctxt->myDoc, target, data);
  2387. if (ret == NULL) return;
  2388. if (ctxt->linenumbers) {
  2389. if (ctxt->input != NULL) {
  2390. if (ctxt->input->line < 65535)
  2391. ret->line = (short) ctxt->input->line;
  2392. else
  2393. ret->line = 65535;
  2394. }
  2395. }
  2396. if (ctxt->inSubset == 1) {
  2397. xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
  2398. return;
  2399. } else if (ctxt->inSubset == 2) {
  2400. xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
  2401. return;
  2402. }
  2403. if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
  2404. #ifdef DEBUG_SAX_TREE
  2405. xmlGenericError(xmlGenericErrorContext,
  2406. "Setting PI %s as root\n", target);
  2407. #endif
  2408. xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
  2409. return;
  2410. }
  2411. if (parent->type == XML_ELEMENT_NODE) {
  2412. #ifdef DEBUG_SAX_TREE
  2413. xmlGenericError(xmlGenericErrorContext,
  2414. "adding PI %s child to %s\n", target, parent->name);
  2415. #endif
  2416. xmlAddChild(parent, ret);
  2417. } else {
  2418. #ifdef DEBUG_SAX_TREE
  2419. xmlGenericError(xmlGenericErrorContext,
  2420. "adding PI %s sibling to ", target);
  2421. xmlDebugDumpOneNode(stderr, parent, 0);
  2422. #endif
  2423. xmlAddSibling(parent, ret);
  2424. }
  2425. }
  2426. /**
  2427. * xmlSAX2Comment:
  2428. * @ctx: the user data (XML parser context)
  2429. * @value: the xmlSAX2Comment content
  2430. *
  2431. * A xmlSAX2Comment has been parsed.
  2432. */
  2433. void
  2434. xmlSAX2Comment(void *ctx, const xmlChar *value)
  2435. {
  2436. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  2437. xmlNodePtr ret;
  2438. xmlNodePtr parent;
  2439. if (ctx == NULL) return;
  2440. parent = ctxt->node;
  2441. #ifdef DEBUG_SAX
  2442. xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2Comment(%s)\n", value);
  2443. #endif
  2444. ret = xmlNewDocComment(ctxt->myDoc, value);
  2445. if (ret == NULL) return;
  2446. if (ctxt->linenumbers) {
  2447. if (ctxt->input != NULL) {
  2448. if (ctxt->input->line < 65535)
  2449. ret->line = (short) ctxt->input->line;
  2450. else
  2451. ret->line = 65535;
  2452. }
  2453. }
  2454. if (ctxt->inSubset == 1) {
  2455. xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
  2456. return;
  2457. } else if (ctxt->inSubset == 2) {
  2458. xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
  2459. return;
  2460. }
  2461. if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
  2462. #ifdef DEBUG_SAX_TREE
  2463. xmlGenericError(xmlGenericErrorContext,
  2464. "Setting xmlSAX2Comment as root\n");
  2465. #endif
  2466. xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
  2467. return;
  2468. }
  2469. if (parent->type == XML_ELEMENT_NODE) {
  2470. #ifdef DEBUG_SAX_TREE
  2471. xmlGenericError(xmlGenericErrorContext,
  2472. "adding xmlSAX2Comment child to %s\n", parent->name);
  2473. #endif
  2474. xmlAddChild(parent, ret);
  2475. } else {
  2476. #ifdef DEBUG_SAX_TREE
  2477. xmlGenericError(xmlGenericErrorContext,
  2478. "adding xmlSAX2Comment sibling to ");
  2479. xmlDebugDumpOneNode(stderr, parent, 0);
  2480. #endif
  2481. xmlAddSibling(parent, ret);
  2482. }
  2483. }
  2484. /**
  2485. * xmlSAX2CDataBlock:
  2486. * @ctx: the user data (XML parser context)
  2487. * @value: The pcdata content
  2488. * @len: the block length
  2489. *
  2490. * called when a pcdata block has been parsed
  2491. */
  2492. void
  2493. xmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len)
  2494. {
  2495. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  2496. xmlNodePtr ret, lastChild;
  2497. if (ctx == NULL) return;
  2498. #ifdef DEBUG_SAX
  2499. xmlGenericError(xmlGenericErrorContext,
  2500. "SAX.pcdata(%.10s, %d)\n", value, len);
  2501. #endif
  2502. lastChild = xmlGetLastChild(ctxt->node);
  2503. #ifdef DEBUG_SAX_TREE
  2504. xmlGenericError(xmlGenericErrorContext,
  2505. "add chars to %s \n", ctxt->node->name);
  2506. #endif
  2507. if ((lastChild != NULL) &&
  2508. (lastChild->type == XML_CDATA_SECTION_NODE)) {
  2509. xmlTextConcat(lastChild, value, len);
  2510. } else {
  2511. ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
  2512. xmlAddChild(ctxt->node, ret);
  2513. }
  2514. }
  2515. static int xmlSAX2DefaultVersionValue = 2;
  2516. #ifdef LIBXML_SAX1_ENABLED
  2517. /**
  2518. * xmlSAXDefaultVersion:
  2519. * @version: the version, 1 or 2
  2520. *
  2521. * Set the default version of SAX used globally by the library.
  2522. * By default, during initialization the default is set to 2.
  2523. * Note that it is generally a better coding style to use
  2524. * xmlSAXVersion() to set up the version explicitly for a given
  2525. * parsing context.
  2526. *
  2527. * Returns the previous value in case of success and -1 in case of error.
  2528. */
  2529. int
  2530. xmlSAXDefaultVersion(int version)
  2531. {
  2532. int ret = xmlSAX2DefaultVersionValue;
  2533. if ((version != 1) && (version != 2))
  2534. return(-1);
  2535. xmlSAX2DefaultVersionValue = version;
  2536. return(ret);
  2537. }
  2538. #endif /* LIBXML_SAX1_ENABLED */
  2539. /**
  2540. * xmlSAXVersion:
  2541. * @hdlr: the SAX handler
  2542. * @version: the version, 1 or 2
  2543. *
  2544. * Initialize the default XML SAX handler according to the version
  2545. *
  2546. * Returns 0 in case of success and -1 in case of error.
  2547. */
  2548. int
  2549. xmlSAXVersion(xmlSAXHandler *hdlr, int version)
  2550. {
  2551. if (hdlr == NULL) return(-1);
  2552. if (version == 2) {
  2553. hdlr->startElement = NULL;
  2554. hdlr->endElement = NULL;
  2555. hdlr->startElementNs = xmlSAX2StartElementNs;
  2556. hdlr->endElementNs = xmlSAX2EndElementNs;
  2557. hdlr->serror = NULL;
  2558. hdlr->initialized = XML_SAX2_MAGIC;
  2559. #ifdef LIBXML_SAX1_ENABLED
  2560. } else if (version == 1) {
  2561. hdlr->startElement = xmlSAX2StartElement;
  2562. hdlr->endElement = xmlSAX2EndElement;
  2563. hdlr->initialized = 1;
  2564. #endif /* LIBXML_SAX1_ENABLED */
  2565. } else
  2566. return(-1);
  2567. hdlr->internalSubset = xmlSAX2InternalSubset;
  2568. hdlr->externalSubset = xmlSAX2ExternalSubset;
  2569. hdlr->isStandalone = xmlSAX2IsStandalone;
  2570. hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
  2571. hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
  2572. hdlr->resolveEntity = xmlSAX2ResolveEntity;
  2573. hdlr->getEntity = xmlSAX2GetEntity;
  2574. hdlr->getParameterEntity = xmlSAX2GetParameterEntity;
  2575. hdlr->entityDecl = xmlSAX2EntityDecl;
  2576. hdlr->attributeDecl = xmlSAX2AttributeDecl;
  2577. hdlr->elementDecl = xmlSAX2ElementDecl;
  2578. hdlr->notationDecl = xmlSAX2NotationDecl;
  2579. hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl;
  2580. hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
  2581. hdlr->startDocument = xmlSAX2StartDocument;
  2582. hdlr->endDocument = xmlSAX2EndDocument;
  2583. hdlr->reference = xmlSAX2Reference;
  2584. hdlr->characters = xmlSAX2Characters;
  2585. hdlr->cdataBlock = xmlSAX2CDataBlock;
  2586. hdlr->ignorableWhitespace = xmlSAX2Characters;
  2587. hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
  2588. hdlr->comment = xmlSAX2Comment;
  2589. hdlr->warning = xmlParserWarning;
  2590. hdlr->error = xmlParserError;
  2591. hdlr->fatalError = xmlParserError;
  2592. return(0);
  2593. }
  2594. /**
  2595. * xmlSAX2InitDefaultSAXHandler:
  2596. * @hdlr: the SAX handler
  2597. * @warning: flag if non-zero sets the handler warning procedure
  2598. *
  2599. * Initialize the default XML SAX2 handler
  2600. */
  2601. void
  2602. xmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
  2603. {
  2604. if ((hdlr == NULL) || (hdlr->initialized != 0))
  2605. return;
  2606. xmlSAXVersion(hdlr, xmlSAX2DefaultVersionValue);
  2607. if (warning == 0)
  2608. hdlr->warning = NULL;
  2609. else
  2610. hdlr->warning = xmlParserWarning;
  2611. }
  2612. /**
  2613. * xmlDefaultSAXHandlerInit:
  2614. *
  2615. * Initialize the default SAX2 handler
  2616. */
  2617. void
  2618. xmlDefaultSAXHandlerInit(void)
  2619. {
  2620. #ifdef LIBXML_SAX1_ENABLED
  2621. xmlSAXVersion((xmlSAXHandlerPtr) &xmlDefaultSAXHandler, 1);
  2622. #endif /* LIBXML_SAX1_ENABLED */
  2623. }
  2624. #ifdef LIBXML_HTML_ENABLED
  2625. /**
  2626. * xmlSAX2InitHtmlDefaultSAXHandler:
  2627. * @hdlr: the SAX handler
  2628. *
  2629. * Initialize the default HTML SAX2 handler
  2630. */
  2631. void
  2632. xmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
  2633. {
  2634. if ((hdlr == NULL) || (hdlr->initialized != 0))
  2635. return;
  2636. hdlr->internalSubset = xmlSAX2InternalSubset;
  2637. hdlr->externalSubset = NULL;
  2638. hdlr->isStandalone = NULL;
  2639. hdlr->hasInternalSubset = NULL;
  2640. hdlr->hasExternalSubset = NULL;
  2641. hdlr->resolveEntity = NULL;
  2642. hdlr->getEntity = xmlSAX2GetEntity;
  2643. hdlr->getParameterEntity = NULL;
  2644. hdlr->entityDecl = NULL;
  2645. hdlr->attributeDecl = NULL;
  2646. hdlr->elementDecl = NULL;
  2647. hdlr->notationDecl = NULL;
  2648. hdlr->unparsedEntityDecl = NULL;
  2649. hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
  2650. hdlr->startDocument = xmlSAX2StartDocument;
  2651. hdlr->endDocument = xmlSAX2EndDocument;
  2652. hdlr->startElement = xmlSAX2StartElement;
  2653. hdlr->endElement = xmlSAX2EndElement;
  2654. hdlr->reference = NULL;
  2655. hdlr->characters = xmlSAX2Characters;
  2656. hdlr->cdataBlock = xmlSAX2CDataBlock;
  2657. hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
  2658. hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
  2659. hdlr->comment = xmlSAX2Comment;
  2660. hdlr->warning = xmlParserWarning;
  2661. hdlr->error = xmlParserError;
  2662. hdlr->fatalError = xmlParserError;
  2663. hdlr->initialized = 1;
  2664. }
  2665. /**
  2666. * htmlDefaultSAXHandlerInit:
  2667. *
  2668. * Initialize the default SAX handler
  2669. */
  2670. void
  2671. htmlDefaultSAXHandlerInit(void)
  2672. {
  2673. xmlSAX2InitHtmlDefaultSAXHandler((xmlSAXHandlerPtr) &htmlDefaultSAXHandler);
  2674. }
  2675. #endif /* LIBXML_HTML_ENABLED */
  2676. #ifdef LIBXML_DOCB_ENABLED
  2677. /**
  2678. * xmlSAX2InitDocbDefaultSAXHandler:
  2679. * @hdlr: the SAX handler
  2680. *
  2681. * Initialize the default DocBook SAX2 handler
  2682. */
  2683. void
  2684. xmlSAX2InitDocbDefaultSAXHandler(xmlSAXHandler *hdlr)
  2685. {
  2686. if ((hdlr == NULL) || (hdlr->initialized != 0))
  2687. return;
  2688. hdlr->internalSubset = xmlSAX2InternalSubset;
  2689. hdlr->externalSubset = NULL;
  2690. hdlr->isStandalone = xmlSAX2IsStandalone;
  2691. hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
  2692. hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
  2693. hdlr->resolveEntity = xmlSAX2ResolveEntity;
  2694. hdlr->getEntity = xmlSAX2GetEntity;
  2695. hdlr->getParameterEntity = NULL;
  2696. hdlr->entityDecl = xmlSAX2EntityDecl;
  2697. hdlr->attributeDecl = NULL;
  2698. hdlr->elementDecl = NULL;
  2699. hdlr->notationDecl = NULL;
  2700. hdlr->unparsedEntityDecl = NULL;
  2701. hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
  2702. hdlr->startDocument = xmlSAX2StartDocument;
  2703. hdlr->endDocument = xmlSAX2EndDocument;
  2704. hdlr->startElement = xmlSAX2StartElement;
  2705. hdlr->endElement = xmlSAX2EndElement;
  2706. hdlr->reference = xmlSAX2Reference;
  2707. hdlr->characters = xmlSAX2Characters;
  2708. hdlr->cdataBlock = NULL;
  2709. hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
  2710. hdlr->processingInstruction = NULL;
  2711. hdlr->comment = xmlSAX2Comment;
  2712. hdlr->warning = xmlParserWarning;
  2713. hdlr->error = xmlParserError;
  2714. hdlr->fatalError = xmlParserError;
  2715. hdlr->initialized = 1;
  2716. }
  2717. /**
  2718. * docbDefaultSAXHandlerInit:
  2719. *
  2720. * Initialize the default SAX handler
  2721. */
  2722. void
  2723. docbDefaultSAXHandlerInit(void)
  2724. {
  2725. xmlSAX2InitDocbDefaultSAXHandler((xmlSAXHandlerPtr) &docbDefaultSAXHandler);
  2726. }
  2727. #endif /* LIBXML_DOCB_ENABLED */
  2728. #define bottom_SAX2
  2729. #include "elfgcchack.h"