SAX2.c 84 KB

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