libxml.c 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724
  1. /*
  2. * libxml.c: this modules implements the main part of the glue of the
  3. * libxml2 library and the Python interpreter. It provides the
  4. * entry points where an automatically generated stub is either
  5. * unpractical or would not match cleanly the Python model.
  6. *
  7. * If compiled with MERGED_MODULES, the entry point will be used to
  8. * initialize both the libxml2 and the libxslt wrappers
  9. *
  10. * See Copyright for the status of this software.
  11. *
  12. * daniel@veillard.com
  13. */
  14. #include <Python.h>
  15. #include <fileobject.h>
  16. /* #include "config.h" */
  17. #include <libxml/xmlmemory.h>
  18. #include <libxml/parser.h>
  19. #include <libxml/tree.h>
  20. #include <libxml/xpath.h>
  21. #include <libxml/xmlerror.h>
  22. #include <libxml/xpathInternals.h>
  23. #include <libxml/xmlmemory.h>
  24. #include <libxml/xmlIO.h>
  25. #include <libxml/c14n.h>
  26. #include <libxml/xmlreader.h>
  27. #include <libxml/xmlsave.h>
  28. #include "libxml_wrap.h"
  29. #include "libxml2-py.h"
  30. #if (defined(_MSC_VER) || defined(__MINGW32__)) && !defined(vsnprintf)
  31. #define vsnprintf(b,c,f,a) _vsnprintf(b,c,f,a)
  32. #elif defined(WITH_TRIO)
  33. #include "trio.h"
  34. #define vsnprintf trio_vsnprintf
  35. #endif
  36. /* #define DEBUG */
  37. /* #define DEBUG_SAX */
  38. /* #define DEBUG_XPATH */
  39. /* #define DEBUG_ERROR */
  40. /* #define DEBUG_MEMORY */
  41. /* #define DEBUG_FILES */
  42. /* #define DEBUG_LOADER */
  43. void initlibxml2mod(void);
  44. /**
  45. * TODO:
  46. *
  47. * macro to flag unimplemented blocks
  48. */
  49. #define TODO \
  50. xmlGenericError(xmlGenericErrorContext, \
  51. "Unimplemented block at %s:%d\n", \
  52. __FILE__, __LINE__);
  53. /*
  54. * the following vars are used for XPath extensions, but
  55. * are also referenced within the parser cleanup routine.
  56. */
  57. static int libxml_xpathCallbacksInitialized = 0;
  58. typedef struct libxml_xpathCallback {
  59. xmlXPathContextPtr ctx;
  60. xmlChar *name;
  61. xmlChar *ns_uri;
  62. PyObject *function;
  63. } libxml_xpathCallback, *libxml_xpathCallbackPtr;
  64. typedef libxml_xpathCallback libxml_xpathCallbackArray[];
  65. static int libxml_xpathCallbacksAllocd = 10;
  66. static libxml_xpathCallbackArray *libxml_xpathCallbacks = NULL;
  67. static int libxml_xpathCallbacksNb = 0;
  68. /************************************************************************
  69. * *
  70. * Memory debug interface *
  71. * *
  72. ************************************************************************/
  73. #if 0
  74. extern void xmlMemFree(void *ptr);
  75. extern void *xmlMemMalloc(size_t size);
  76. extern void *xmlMemRealloc(void *ptr, size_t size);
  77. extern char *xmlMemoryStrdup(const char *str);
  78. #endif
  79. static int libxmlMemoryDebugActivated = 0;
  80. static long libxmlMemoryAllocatedBase = 0;
  81. static int libxmlMemoryDebug = 0;
  82. static xmlFreeFunc freeFunc = NULL;
  83. static xmlMallocFunc mallocFunc = NULL;
  84. static xmlReallocFunc reallocFunc = NULL;
  85. static xmlStrdupFunc strdupFunc = NULL;
  86. static void
  87. libxml_xmlErrorInitialize(void); /* forward declare */
  88. PyObject *
  89. libxml_xmlMemoryUsed(PyObject * self ATTRIBUTE_UNUSED,
  90. PyObject * args ATTRIBUTE_UNUSED)
  91. {
  92. long ret;
  93. PyObject *py_retval;
  94. ret = xmlMemUsed();
  95. py_retval = libxml_longWrap(ret);
  96. return (py_retval);
  97. }
  98. PyObject *
  99. libxml_xmlDebugMemory(PyObject * self ATTRIBUTE_UNUSED, PyObject * args)
  100. {
  101. int activate;
  102. PyObject *py_retval;
  103. long ret;
  104. if (!PyArg_ParseTuple(args, (char *) "i:xmlDebugMemory", &activate))
  105. return (NULL);
  106. #ifdef DEBUG_MEMORY
  107. printf("libxml_xmlDebugMemory(%d) called\n", activate);
  108. #endif
  109. if (activate != 0) {
  110. if (libxmlMemoryDebug == 0) {
  111. /*
  112. * First initialize the library and grab the old memory handlers
  113. * and switch the library to memory debugging
  114. */
  115. xmlMemGet((xmlFreeFunc *) & freeFunc,
  116. (xmlMallocFunc *) & mallocFunc,
  117. (xmlReallocFunc *) & reallocFunc,
  118. (xmlStrdupFunc *) & strdupFunc);
  119. if ((freeFunc == xmlMemFree) && (mallocFunc == xmlMemMalloc) &&
  120. (reallocFunc == xmlMemRealloc) &&
  121. (strdupFunc == xmlMemoryStrdup)) {
  122. libxmlMemoryAllocatedBase = xmlMemUsed();
  123. } else {
  124. /*
  125. * cleanup first, because some memory has been
  126. * allocated with the non-debug malloc in xmlInitParser
  127. * when the python module was imported
  128. */
  129. xmlCleanupParser();
  130. ret = (long) xmlMemSetup(xmlMemFree, xmlMemMalloc,
  131. xmlMemRealloc, xmlMemoryStrdup);
  132. if (ret < 0)
  133. goto error;
  134. libxmlMemoryAllocatedBase = xmlMemUsed();
  135. /* reinitialize */
  136. xmlInitParser();
  137. libxml_xmlErrorInitialize();
  138. }
  139. ret = 0;
  140. } else if (libxmlMemoryDebugActivated == 0) {
  141. libxmlMemoryAllocatedBase = xmlMemUsed();
  142. ret = 0;
  143. } else {
  144. ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
  145. }
  146. libxmlMemoryDebug = 1;
  147. libxmlMemoryDebugActivated = 1;
  148. } else {
  149. if (libxmlMemoryDebugActivated == 1)
  150. ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
  151. else
  152. ret = 0;
  153. libxmlMemoryDebugActivated = 0;
  154. }
  155. error:
  156. py_retval = libxml_longWrap(ret);
  157. return (py_retval);
  158. }
  159. PyObject *
  160. libxml_xmlPythonCleanupParser(PyObject *self ATTRIBUTE_UNUSED,
  161. PyObject *args ATTRIBUTE_UNUSED) {
  162. int ix;
  163. long freed = -1;
  164. if (libxmlMemoryDebug) {
  165. freed = xmlMemUsed();
  166. }
  167. xmlCleanupParser();
  168. /*
  169. * Need to confirm whether we really want to do this (required for
  170. * memcheck) in all cases...
  171. */
  172. if (libxml_xpathCallbacks != NULL) { /* if ext funcs declared */
  173. for (ix=0; ix<libxml_xpathCallbacksNb; ix++) {
  174. if ((*libxml_xpathCallbacks)[ix].name != NULL)
  175. xmlFree((*libxml_xpathCallbacks)[ix].name);
  176. if ((*libxml_xpathCallbacks)[ix].ns_uri != NULL)
  177. xmlFree((*libxml_xpathCallbacks)[ix].ns_uri);
  178. }
  179. libxml_xpathCallbacksNb = 0;
  180. xmlFree(libxml_xpathCallbacks);
  181. libxml_xpathCallbacks = NULL;
  182. }
  183. if (libxmlMemoryDebug) {
  184. freed -= xmlMemUsed();
  185. libxmlMemoryAllocatedBase -= freed;
  186. if (libxmlMemoryAllocatedBase < 0)
  187. libxmlMemoryAllocatedBase = 0;
  188. }
  189. Py_INCREF(Py_None);
  190. return(Py_None);
  191. }
  192. PyObject *
  193. libxml_xmlDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
  194. ATTRIBUTE_UNUSED PyObject * args)
  195. {
  196. if (libxmlMemoryDebug != 0)
  197. xmlMemoryDump();
  198. Py_INCREF(Py_None);
  199. return (Py_None);
  200. }
  201. /************************************************************************
  202. * *
  203. * Handling Python FILE I/O at the C level *
  204. * The raw I/O attack diectly the File objects, while the *
  205. * other routines address the ioWrapper instance instead *
  206. * *
  207. ************************************************************************/
  208. /**
  209. * xmlPythonFileCloseUnref:
  210. * @context: the I/O context
  211. *
  212. * Close an I/O channel
  213. */
  214. static int
  215. xmlPythonFileCloseRaw (void * context) {
  216. PyObject *file, *ret;
  217. #ifdef DEBUG_FILES
  218. printf("xmlPythonFileCloseUnref\n");
  219. #endif
  220. file = (PyObject *) context;
  221. if (file == NULL) return(-1);
  222. ret = PyEval_CallMethod(file, (char *) "close", (char *) "()");
  223. if (ret != NULL) {
  224. Py_DECREF(ret);
  225. }
  226. Py_DECREF(file);
  227. return(0);
  228. }
  229. /**
  230. * xmlPythonFileReadRaw:
  231. * @context: the I/O context
  232. * @buffer: where to drop data
  233. * @len: number of bytes to write
  234. *
  235. * Read @len bytes to @buffer from the Python file in the I/O channel
  236. *
  237. * Returns the number of bytes read
  238. */
  239. static int
  240. xmlPythonFileReadRaw (void * context, char * buffer, int len) {
  241. PyObject *file;
  242. PyObject *ret;
  243. int lenread = -1;
  244. char *data;
  245. #ifdef DEBUG_FILES
  246. printf("xmlPythonFileReadRaw: %d\n", len);
  247. #endif
  248. file = (PyObject *) context;
  249. if (file == NULL) return(-1);
  250. ret = PyEval_CallMethod(file, (char *) "read", (char *) "(i)", len);
  251. if (ret == NULL) {
  252. printf("xmlPythonFileReadRaw: result is NULL\n");
  253. return(-1);
  254. } else if (PyString_Check(ret)) {
  255. lenread = PyString_Size(ret);
  256. data = PyString_AsString(ret);
  257. if (lenread > len)
  258. memcpy(buffer, data, len);
  259. else
  260. memcpy(buffer, data, lenread);
  261. Py_DECREF(ret);
  262. } else {
  263. printf("xmlPythonFileReadRaw: result is not a String\n");
  264. Py_DECREF(ret);
  265. }
  266. return(lenread);
  267. }
  268. /**
  269. * xmlPythonFileRead:
  270. * @context: the I/O context
  271. * @buffer: where to drop data
  272. * @len: number of bytes to write
  273. *
  274. * Read @len bytes to @buffer from the I/O channel.
  275. *
  276. * Returns the number of bytes read
  277. */
  278. static int
  279. xmlPythonFileRead (void * context, char * buffer, int len) {
  280. PyObject *file;
  281. PyObject *ret;
  282. int lenread = -1;
  283. char *data;
  284. #ifdef DEBUG_FILES
  285. printf("xmlPythonFileRead: %d\n", len);
  286. #endif
  287. file = (PyObject *) context;
  288. if (file == NULL) return(-1);
  289. ret = PyEval_CallMethod(file, (char *) "io_read", (char *) "(i)", len);
  290. if (ret == NULL) {
  291. printf("xmlPythonFileRead: result is NULL\n");
  292. return(-1);
  293. } else if (PyString_Check(ret)) {
  294. lenread = PyString_Size(ret);
  295. data = PyString_AsString(ret);
  296. if (lenread > len)
  297. memcpy(buffer, data, len);
  298. else
  299. memcpy(buffer, data, lenread);
  300. Py_DECREF(ret);
  301. } else {
  302. printf("xmlPythonFileRead: result is not a String\n");
  303. Py_DECREF(ret);
  304. }
  305. return(lenread);
  306. }
  307. /**
  308. * xmlFileWrite:
  309. * @context: the I/O context
  310. * @buffer: where to drop data
  311. * @len: number of bytes to write
  312. *
  313. * Write @len bytes from @buffer to the I/O channel.
  314. *
  315. * Returns the number of bytes written
  316. */
  317. static int
  318. xmlPythonFileWrite (void * context, const char * buffer, int len) {
  319. PyObject *file;
  320. PyObject *string;
  321. PyObject *ret = NULL;
  322. int written = -1;
  323. #ifdef DEBUG_FILES
  324. printf("xmlPythonFileWrite: %d\n", len);
  325. #endif
  326. file = (PyObject *) context;
  327. if (file == NULL) return(-1);
  328. string = PyString_FromStringAndSize(buffer, len);
  329. if (string == NULL) return(-1);
  330. if (PyObject_HasAttrString(file, (char *) "io_write")) {
  331. ret = PyEval_CallMethod(file, (char *) "io_write", (char *) "(O)",
  332. string);
  333. } else if (PyObject_HasAttrString(file, (char *) "write")) {
  334. ret = PyEval_CallMethod(file, (char *) "write", (char *) "(O)",
  335. string);
  336. }
  337. Py_DECREF(string);
  338. if (ret == NULL) {
  339. printf("xmlPythonFileWrite: result is NULL\n");
  340. return(-1);
  341. } else if (PyInt_Check(ret)) {
  342. written = (int) PyInt_AsLong(ret);
  343. Py_DECREF(ret);
  344. } else if (ret == Py_None) {
  345. written = len;
  346. Py_DECREF(ret);
  347. } else {
  348. printf("xmlPythonFileWrite: result is not an Int nor None\n");
  349. Py_DECREF(ret);
  350. }
  351. return(written);
  352. }
  353. /**
  354. * xmlPythonFileClose:
  355. * @context: the I/O context
  356. *
  357. * Close an I/O channel
  358. */
  359. static int
  360. xmlPythonFileClose (void * context) {
  361. PyObject *file, *ret = NULL;
  362. #ifdef DEBUG_FILES
  363. printf("xmlPythonFileClose\n");
  364. #endif
  365. file = (PyObject *) context;
  366. if (file == NULL) return(-1);
  367. if (PyObject_HasAttrString(file, (char *) "io_close")) {
  368. ret = PyEval_CallMethod(file, (char *) "io_close", (char *) "()");
  369. } else if (PyObject_HasAttrString(file, (char *) "flush")) {
  370. ret = PyEval_CallMethod(file, (char *) "flush", (char *) "()");
  371. }
  372. if (ret != NULL) {
  373. Py_DECREF(ret);
  374. }
  375. return(0);
  376. }
  377. #ifdef LIBXML_OUTPUT_ENABLED
  378. /**
  379. * xmlOutputBufferCreatePythonFile:
  380. * @file: a PyFile_Type
  381. * @encoder: the encoding converter or NULL
  382. *
  383. * Create a buffered output for the progressive saving to a PyFile_Type
  384. * buffered C I/O
  385. *
  386. * Returns the new parser output or NULL
  387. */
  388. static xmlOutputBufferPtr
  389. xmlOutputBufferCreatePythonFile(PyObject *file,
  390. xmlCharEncodingHandlerPtr encoder) {
  391. xmlOutputBufferPtr ret;
  392. if (file == NULL) return(NULL);
  393. ret = xmlAllocOutputBuffer(encoder);
  394. if (ret != NULL) {
  395. ret->context = file;
  396. /* Py_INCREF(file); */
  397. ret->writecallback = xmlPythonFileWrite;
  398. ret->closecallback = xmlPythonFileClose;
  399. }
  400. return(ret);
  401. }
  402. PyObject *
  403. libxml_xmlCreateOutputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
  404. PyObject *py_retval;
  405. PyObject *file;
  406. xmlChar *encoding;
  407. xmlCharEncodingHandlerPtr handler = NULL;
  408. xmlOutputBufferPtr buffer;
  409. if (!PyArg_ParseTuple(args, (char *)"Oz:xmlOutputBufferCreate",
  410. &file, &encoding))
  411. return(NULL);
  412. if ((encoding != NULL) && (encoding[0] != 0)) {
  413. handler = xmlFindCharEncodingHandler((const char *) encoding);
  414. }
  415. buffer = xmlOutputBufferCreatePythonFile(file, handler);
  416. if (buffer == NULL)
  417. printf("libxml_xmlCreateOutputBuffer: buffer == NULL\n");
  418. py_retval = libxml_xmlOutputBufferPtrWrap(buffer);
  419. return(py_retval);
  420. }
  421. /**
  422. * libxml_outputBufferGetPythonFile:
  423. * @buffer: the I/O buffer
  424. *
  425. * read the Python I/O from the CObject
  426. *
  427. * Returns the new parser output or NULL
  428. */
  429. static PyObject *
  430. libxml_outputBufferGetPythonFile(ATTRIBUTE_UNUSED PyObject *self,
  431. PyObject *args) {
  432. PyObject *buffer;
  433. PyObject *file;
  434. xmlOutputBufferPtr obj;
  435. if (!PyArg_ParseTuple(args, (char *)"O:outputBufferGetPythonFile",
  436. &buffer))
  437. return(NULL);
  438. obj = PyoutputBuffer_Get(buffer);
  439. if (obj == NULL) {
  440. fprintf(stderr,
  441. "outputBufferGetPythonFile: obj == NULL\n");
  442. Py_INCREF(Py_None);
  443. return(Py_None);
  444. }
  445. if (obj->closecallback != xmlPythonFileClose) {
  446. fprintf(stderr,
  447. "outputBufferGetPythonFile: not a python file wrapper\n");
  448. Py_INCREF(Py_None);
  449. return(Py_None);
  450. }
  451. file = (PyObject *) obj->context;
  452. if (file == NULL) {
  453. Py_INCREF(Py_None);
  454. return(Py_None);
  455. }
  456. Py_INCREF(file);
  457. return(file);
  458. }
  459. static PyObject *
  460. libxml_xmlOutputBufferClose(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
  461. PyObject *py_retval;
  462. int c_retval;
  463. xmlOutputBufferPtr out;
  464. PyObject *pyobj_out;
  465. if (!PyArg_ParseTuple(args, (char *)"O:xmlOutputBufferClose", &pyobj_out))
  466. return(NULL);
  467. out = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_out);
  468. /* Buffer may already have been destroyed elsewhere. This is harmless. */
  469. if (out == NULL) {
  470. Py_INCREF(Py_None);
  471. return(Py_None);
  472. }
  473. c_retval = xmlOutputBufferClose(out);
  474. py_retval = libxml_intWrap((int) c_retval);
  475. return(py_retval);
  476. }
  477. static PyObject *
  478. libxml_xmlOutputBufferFlush(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
  479. PyObject *py_retval;
  480. int c_retval;
  481. xmlOutputBufferPtr out;
  482. PyObject *pyobj_out;
  483. if (!PyArg_ParseTuple(args, (char *)"O:xmlOutputBufferFlush", &pyobj_out))
  484. return(NULL);
  485. out = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_out);
  486. c_retval = xmlOutputBufferFlush(out);
  487. py_retval = libxml_intWrap((int) c_retval);
  488. return(py_retval);
  489. }
  490. static PyObject *
  491. libxml_xmlSaveFileTo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
  492. PyObject *py_retval;
  493. int c_retval;
  494. xmlOutputBufferPtr buf;
  495. PyObject *pyobj_buf;
  496. xmlDocPtr cur;
  497. PyObject *pyobj_cur;
  498. char * encoding;
  499. if (!PyArg_ParseTuple(args, (char *)"OOz:xmlSaveFileTo", &pyobj_buf, &pyobj_cur, &encoding))
  500. return(NULL);
  501. buf = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_buf);
  502. cur = (xmlDocPtr) PyxmlNode_Get(pyobj_cur);
  503. c_retval = xmlSaveFileTo(buf, cur, encoding);
  504. /* xmlSaveTo() freed the memory pointed to by buf, so record that in the
  505. * Python object. */
  506. ((PyoutputBuffer_Object *)(pyobj_buf))->obj = NULL;
  507. py_retval = libxml_intWrap((int) c_retval);
  508. return(py_retval);
  509. }
  510. static PyObject *
  511. libxml_xmlSaveFormatFileTo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
  512. PyObject *py_retval;
  513. int c_retval;
  514. xmlOutputBufferPtr buf;
  515. PyObject *pyobj_buf;
  516. xmlDocPtr cur;
  517. PyObject *pyobj_cur;
  518. char * encoding;
  519. int format;
  520. if (!PyArg_ParseTuple(args, (char *)"OOzi:xmlSaveFormatFileTo", &pyobj_buf, &pyobj_cur, &encoding, &format))
  521. return(NULL);
  522. buf = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_buf);
  523. cur = (xmlDocPtr) PyxmlNode_Get(pyobj_cur);
  524. c_retval = xmlSaveFormatFileTo(buf, cur, encoding, format);
  525. /* xmlSaveFormatFileTo() freed the memory pointed to by buf, so record that
  526. * in the Python object */
  527. ((PyoutputBuffer_Object *)(pyobj_buf))->obj = NULL;
  528. py_retval = libxml_intWrap((int) c_retval);
  529. return(py_retval);
  530. }
  531. #endif /* LIBXML_OUTPUT_ENABLED */
  532. /**
  533. * xmlParserInputBufferCreatePythonFile:
  534. * @file: a PyFile_Type
  535. * @encoder: the encoding converter or NULL
  536. *
  537. * Create a buffered output for the progressive saving to a PyFile_Type
  538. * buffered C I/O
  539. *
  540. * Returns the new parser output or NULL
  541. */
  542. static xmlParserInputBufferPtr
  543. xmlParserInputBufferCreatePythonFile(PyObject *file,
  544. xmlCharEncoding encoding) {
  545. xmlParserInputBufferPtr ret;
  546. if (file == NULL) return(NULL);
  547. ret = xmlAllocParserInputBuffer(encoding);
  548. if (ret != NULL) {
  549. ret->context = file;
  550. /* Py_INCREF(file); */
  551. ret->readcallback = xmlPythonFileRead;
  552. ret->closecallback = xmlPythonFileClose;
  553. }
  554. return(ret);
  555. }
  556. PyObject *
  557. libxml_xmlCreateInputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
  558. PyObject *py_retval;
  559. PyObject *file;
  560. xmlChar *encoding;
  561. xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
  562. xmlParserInputBufferPtr buffer;
  563. if (!PyArg_ParseTuple(args, (char *)"Oz:xmlParserInputBufferCreate",
  564. &file, &encoding))
  565. return(NULL);
  566. if ((encoding != NULL) && (encoding[0] != 0)) {
  567. enc = xmlParseCharEncoding((const char *) encoding);
  568. }
  569. buffer = xmlParserInputBufferCreatePythonFile(file, enc);
  570. if (buffer == NULL)
  571. printf("libxml_xmlParserInputBufferCreate: buffer == NULL\n");
  572. py_retval = libxml_xmlParserInputBufferPtrWrap(buffer);
  573. return(py_retval);
  574. }
  575. /************************************************************************
  576. * *
  577. * Providing the resolver at the Python level *
  578. * *
  579. ************************************************************************/
  580. static xmlExternalEntityLoader defaultExternalEntityLoader = NULL;
  581. static PyObject *pythonExternalEntityLoaderObjext;
  582. static xmlParserInputPtr
  583. pythonExternalEntityLoader(const char *URL, const char *ID,
  584. xmlParserCtxtPtr ctxt) {
  585. xmlParserInputPtr result = NULL;
  586. if (pythonExternalEntityLoaderObjext != NULL) {
  587. PyObject *ret;
  588. PyObject *ctxtobj;
  589. ctxtobj = libxml_xmlParserCtxtPtrWrap(ctxt);
  590. #ifdef DEBUG_LOADER
  591. printf("pythonExternalEntityLoader: ready to call\n");
  592. #endif
  593. ret = PyObject_CallFunction(pythonExternalEntityLoaderObjext,
  594. (char *) "(ssO)", URL, ID, ctxtobj);
  595. Py_XDECREF(ctxtobj);
  596. #ifdef DEBUG_LOADER
  597. printf("pythonExternalEntityLoader: result ");
  598. PyObject_Print(ret, stderr, 0);
  599. printf("\n");
  600. #endif
  601. if (ret != NULL) {
  602. if (PyObject_HasAttrString(ret, (char *) "read")) {
  603. xmlParserInputBufferPtr buf;
  604. buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
  605. if (buf != NULL) {
  606. buf->context = ret;
  607. buf->readcallback = xmlPythonFileReadRaw;
  608. buf->closecallback = xmlPythonFileCloseRaw;
  609. result = xmlNewIOInputStream(ctxt, buf,
  610. XML_CHAR_ENCODING_NONE);
  611. }
  612. #if 0
  613. } else {
  614. if (URL != NULL)
  615. printf("pythonExternalEntityLoader: can't read %s\n",
  616. URL);
  617. #endif
  618. }
  619. if (result == NULL) {
  620. Py_DECREF(ret);
  621. } else if (URL != NULL) {
  622. result->filename = (char *) xmlStrdup((const xmlChar *)URL);
  623. result->directory = xmlParserGetDirectory((const char *) URL);
  624. }
  625. }
  626. }
  627. if ((result == NULL) && (defaultExternalEntityLoader != NULL)) {
  628. result = defaultExternalEntityLoader(URL, ID, ctxt);
  629. }
  630. return(result);
  631. }
  632. PyObject *
  633. libxml_xmlSetEntityLoader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
  634. PyObject *py_retval;
  635. PyObject *loader;
  636. if (!PyArg_ParseTuple(args, (char *)"O:libxml_xmlSetEntityLoader",
  637. &loader))
  638. return(NULL);
  639. #ifdef DEBUG_LOADER
  640. printf("libxml_xmlSetEntityLoader\n");
  641. #endif
  642. if (defaultExternalEntityLoader == NULL)
  643. defaultExternalEntityLoader = xmlGetExternalEntityLoader();
  644. pythonExternalEntityLoaderObjext = loader;
  645. xmlSetExternalEntityLoader(pythonExternalEntityLoader);
  646. py_retval = PyInt_FromLong(0);
  647. return(py_retval);
  648. }
  649. /************************************************************************
  650. * *
  651. * Handling SAX/xmllib/sgmlop callback interfaces *
  652. * *
  653. ************************************************************************/
  654. static void
  655. pythonStartElement(void *user_data, const xmlChar * name,
  656. const xmlChar ** attrs)
  657. {
  658. int i;
  659. PyObject *handler;
  660. PyObject *dict;
  661. PyObject *attrname;
  662. PyObject *attrvalue;
  663. PyObject *result = NULL;
  664. int type = 0;
  665. #ifdef DEBUG_SAX
  666. printf("pythonStartElement(%s) called\n", name);
  667. #endif
  668. handler = (PyObject *) user_data;
  669. if (PyObject_HasAttrString(handler, (char *) "startElement"))
  670. type = 1;
  671. else if (PyObject_HasAttrString(handler, (char *) "start"))
  672. type = 2;
  673. if (type != 0) {
  674. /*
  675. * the xmllib interface always generate a dictionnary,
  676. * possibly empty
  677. */
  678. if ((attrs == NULL) && (type == 1)) {
  679. Py_XINCREF(Py_None);
  680. dict = Py_None;
  681. } else if (attrs == NULL) {
  682. dict = PyDict_New();
  683. } else {
  684. dict = PyDict_New();
  685. for (i = 0; attrs[i] != NULL; i++) {
  686. attrname = PyString_FromString((char *) attrs[i]);
  687. i++;
  688. if (attrs[i] != NULL) {
  689. attrvalue = PyString_FromString((char *) attrs[i]);
  690. } else {
  691. Py_XINCREF(Py_None);
  692. attrvalue = Py_None;
  693. }
  694. PyDict_SetItem(dict, attrname, attrvalue);
  695. Py_DECREF(attrname);
  696. Py_DECREF(attrvalue);
  697. }
  698. }
  699. if (type == 1)
  700. result = PyObject_CallMethod(handler, (char *) "startElement",
  701. (char *) "sO", name, dict);
  702. else if (type == 2)
  703. result = PyObject_CallMethod(handler, (char *) "start",
  704. (char *) "sO", name, dict);
  705. if (PyErr_Occurred())
  706. PyErr_Print();
  707. Py_XDECREF(dict);
  708. Py_XDECREF(result);
  709. }
  710. }
  711. static void
  712. pythonStartDocument(void *user_data)
  713. {
  714. PyObject *handler;
  715. PyObject *result;
  716. #ifdef DEBUG_SAX
  717. printf("pythonStartDocument() called\n");
  718. #endif
  719. handler = (PyObject *) user_data;
  720. if (PyObject_HasAttrString(handler, (char *) "startDocument")) {
  721. result =
  722. PyObject_CallMethod(handler, (char *) "startDocument", NULL);
  723. if (PyErr_Occurred())
  724. PyErr_Print();
  725. Py_XDECREF(result);
  726. }
  727. }
  728. static void
  729. pythonEndDocument(void *user_data)
  730. {
  731. PyObject *handler;
  732. PyObject *result;
  733. #ifdef DEBUG_SAX
  734. printf("pythonEndDocument() called\n");
  735. #endif
  736. handler = (PyObject *) user_data;
  737. if (PyObject_HasAttrString(handler, (char *) "endDocument")) {
  738. result =
  739. PyObject_CallMethod(handler, (char *) "endDocument", NULL);
  740. if (PyErr_Occurred())
  741. PyErr_Print();
  742. Py_XDECREF(result);
  743. }
  744. /*
  745. * The reference to the handler is released there
  746. */
  747. Py_XDECREF(handler);
  748. }
  749. static void
  750. pythonEndElement(void *user_data, const xmlChar * name)
  751. {
  752. PyObject *handler;
  753. PyObject *result;
  754. #ifdef DEBUG_SAX
  755. printf("pythonEndElement(%s) called\n", name);
  756. #endif
  757. handler = (PyObject *) user_data;
  758. if (PyObject_HasAttrString(handler, (char *) "endElement")) {
  759. result = PyObject_CallMethod(handler, (char *) "endElement",
  760. (char *) "s", name);
  761. if (PyErr_Occurred())
  762. PyErr_Print();
  763. Py_XDECREF(result);
  764. } else if (PyObject_HasAttrString(handler, (char *) "end")) {
  765. result = PyObject_CallMethod(handler, (char *) "end",
  766. (char *) "s", name);
  767. if (PyErr_Occurred())
  768. PyErr_Print();
  769. Py_XDECREF(result);
  770. }
  771. }
  772. static void
  773. pythonReference(void *user_data, const xmlChar * name)
  774. {
  775. PyObject *handler;
  776. PyObject *result;
  777. #ifdef DEBUG_SAX
  778. printf("pythonReference(%s) called\n", name);
  779. #endif
  780. handler = (PyObject *) user_data;
  781. if (PyObject_HasAttrString(handler, (char *) "reference")) {
  782. result = PyObject_CallMethod(handler, (char *) "reference",
  783. (char *) "s", name);
  784. if (PyErr_Occurred())
  785. PyErr_Print();
  786. Py_XDECREF(result);
  787. }
  788. }
  789. static void
  790. pythonCharacters(void *user_data, const xmlChar * ch, int len)
  791. {
  792. PyObject *handler;
  793. PyObject *result = NULL;
  794. int type = 0;
  795. #ifdef DEBUG_SAX
  796. printf("pythonCharacters(%s, %d) called\n", ch, len);
  797. #endif
  798. handler = (PyObject *) user_data;
  799. if (PyObject_HasAttrString(handler, (char *) "characters"))
  800. type = 1;
  801. else if (PyObject_HasAttrString(handler, (char *) "data"))
  802. type = 2;
  803. if (type != 0) {
  804. if (type == 1)
  805. result = PyObject_CallMethod(handler, (char *) "characters",
  806. (char *) "s#", ch, len);
  807. else if (type == 2)
  808. result = PyObject_CallMethod(handler, (char *) "data",
  809. (char *) "s#", ch, len);
  810. if (PyErr_Occurred())
  811. PyErr_Print();
  812. Py_XDECREF(result);
  813. }
  814. }
  815. static void
  816. pythonIgnorableWhitespace(void *user_data, const xmlChar * ch, int len)
  817. {
  818. PyObject *handler;
  819. PyObject *result = NULL;
  820. int type = 0;
  821. #ifdef DEBUG_SAX
  822. printf("pythonIgnorableWhitespace(%s, %d) called\n", ch, len);
  823. #endif
  824. handler = (PyObject *) user_data;
  825. if (PyObject_HasAttrString(handler, (char *) "ignorableWhitespace"))
  826. type = 1;
  827. else if (PyObject_HasAttrString(handler, (char *) "data"))
  828. type = 2;
  829. if (type != 0) {
  830. if (type == 1)
  831. result =
  832. PyObject_CallMethod(handler,
  833. (char *) "ignorableWhitespace",
  834. (char *) "s#", ch, len);
  835. else if (type == 2)
  836. result =
  837. PyObject_CallMethod(handler, (char *) "data",
  838. (char *) "s#", ch, len);
  839. Py_XDECREF(result);
  840. }
  841. }
  842. static void
  843. pythonProcessingInstruction(void *user_data,
  844. const xmlChar * target, const xmlChar * data)
  845. {
  846. PyObject *handler;
  847. PyObject *result;
  848. #ifdef DEBUG_SAX
  849. printf("pythonProcessingInstruction(%s, %s) called\n", target, data);
  850. #endif
  851. handler = (PyObject *) user_data;
  852. if (PyObject_HasAttrString(handler, (char *) "processingInstruction")) {
  853. result = PyObject_CallMethod(handler, (char *)
  854. "processingInstruction",
  855. (char *) "ss", target, data);
  856. Py_XDECREF(result);
  857. }
  858. }
  859. static void
  860. pythonComment(void *user_data, const xmlChar * value)
  861. {
  862. PyObject *handler;
  863. PyObject *result;
  864. #ifdef DEBUG_SAX
  865. printf("pythonComment(%s) called\n", value);
  866. #endif
  867. handler = (PyObject *) user_data;
  868. if (PyObject_HasAttrString(handler, (char *) "comment")) {
  869. result =
  870. PyObject_CallMethod(handler, (char *) "comment", (char *) "s",
  871. value);
  872. if (PyErr_Occurred())
  873. PyErr_Print();
  874. Py_XDECREF(result);
  875. }
  876. }
  877. static void
  878. pythonWarning(void *user_data, const char *msg, ...)
  879. {
  880. PyObject *handler;
  881. PyObject *result;
  882. va_list args;
  883. char buf[1024];
  884. #ifdef DEBUG_SAX
  885. printf("pythonWarning(%s) called\n", msg);
  886. #endif
  887. handler = (PyObject *) user_data;
  888. if (PyObject_HasAttrString(handler, (char *) "warning")) {
  889. va_start(args, msg);
  890. vsnprintf(buf, 1023, msg, args);
  891. va_end(args);
  892. buf[1023] = 0;
  893. result =
  894. PyObject_CallMethod(handler, (char *) "warning", (char *) "s",
  895. buf);
  896. if (PyErr_Occurred())
  897. PyErr_Print();
  898. Py_XDECREF(result);
  899. }
  900. }
  901. static void
  902. pythonError(void *user_data, const char *msg, ...)
  903. {
  904. PyObject *handler;
  905. PyObject *result;
  906. va_list args;
  907. char buf[1024];
  908. #ifdef DEBUG_SAX
  909. printf("pythonError(%s) called\n", msg);
  910. #endif
  911. handler = (PyObject *) user_data;
  912. if (PyObject_HasAttrString(handler, (char *) "error")) {
  913. va_start(args, msg);
  914. vsnprintf(buf, 1023, msg, args);
  915. va_end(args);
  916. buf[1023] = 0;
  917. result =
  918. PyObject_CallMethod(handler, (char *) "error", (char *) "s",
  919. buf);
  920. if (PyErr_Occurred())
  921. PyErr_Print();
  922. Py_XDECREF(result);
  923. }
  924. }
  925. static void
  926. pythonFatalError(void *user_data, const char *msg, ...)
  927. {
  928. PyObject *handler;
  929. PyObject *result;
  930. va_list args;
  931. char buf[1024];
  932. #ifdef DEBUG_SAX
  933. printf("pythonFatalError(%s) called\n", msg);
  934. #endif
  935. handler = (PyObject *) user_data;
  936. if (PyObject_HasAttrString(handler, (char *) "fatalError")) {
  937. va_start(args, msg);
  938. vsnprintf(buf, 1023, msg, args);
  939. va_end(args);
  940. buf[1023] = 0;
  941. result =
  942. PyObject_CallMethod(handler, (char *) "fatalError",
  943. (char *) "s", buf);
  944. if (PyErr_Occurred())
  945. PyErr_Print();
  946. Py_XDECREF(result);
  947. }
  948. }
  949. static void
  950. pythonCdataBlock(void *user_data, const xmlChar * ch, int len)
  951. {
  952. PyObject *handler;
  953. PyObject *result = NULL;
  954. int type = 0;
  955. #ifdef DEBUG_SAX
  956. printf("pythonCdataBlock(%s, %d) called\n", ch, len);
  957. #endif
  958. handler = (PyObject *) user_data;
  959. if (PyObject_HasAttrString(handler, (char *) "cdataBlock"))
  960. type = 1;
  961. else if (PyObject_HasAttrString(handler, (char *) "cdata"))
  962. type = 2;
  963. if (type != 0) {
  964. if (type == 1)
  965. result =
  966. PyObject_CallMethod(handler, (char *) "cdataBlock",
  967. (char *) "s#", ch, len);
  968. else if (type == 2)
  969. result =
  970. PyObject_CallMethod(handler, (char *) "cdata",
  971. (char *) "s#", ch, len);
  972. if (PyErr_Occurred())
  973. PyErr_Print();
  974. Py_XDECREF(result);
  975. }
  976. }
  977. static void
  978. pythonExternalSubset(void *user_data,
  979. const xmlChar * name,
  980. const xmlChar * externalID, const xmlChar * systemID)
  981. {
  982. PyObject *handler;
  983. PyObject *result;
  984. #ifdef DEBUG_SAX
  985. printf("pythonExternalSubset(%s, %s, %s) called\n",
  986. name, externalID, systemID);
  987. #endif
  988. handler = (PyObject *) user_data;
  989. if (PyObject_HasAttrString(handler, (char *) "externalSubset")) {
  990. result =
  991. PyObject_CallMethod(handler, (char *) "externalSubset",
  992. (char *) "sss", name, externalID,
  993. systemID);
  994. Py_XDECREF(result);
  995. }
  996. }
  997. static void
  998. pythonEntityDecl(void *user_data,
  999. const xmlChar * name,
  1000. int type,
  1001. const xmlChar * publicId,
  1002. const xmlChar * systemId, xmlChar * content)
  1003. {
  1004. PyObject *handler;
  1005. PyObject *result;
  1006. handler = (PyObject *) user_data;
  1007. if (PyObject_HasAttrString(handler, (char *) "entityDecl")) {
  1008. result = PyObject_CallMethod(handler, (char *) "entityDecl",
  1009. (char *) "sisss", name, type,
  1010. publicId, systemId, content);
  1011. if (PyErr_Occurred())
  1012. PyErr_Print();
  1013. Py_XDECREF(result);
  1014. }
  1015. }
  1016. static void
  1017. pythonNotationDecl(void *user_data,
  1018. const xmlChar * name,
  1019. const xmlChar * publicId, const xmlChar * systemId)
  1020. {
  1021. PyObject *handler;
  1022. PyObject *result;
  1023. handler = (PyObject *) user_data;
  1024. if (PyObject_HasAttrString(handler, (char *) "notationDecl")) {
  1025. result = PyObject_CallMethod(handler, (char *) "notationDecl",
  1026. (char *) "sss", name, publicId,
  1027. systemId);
  1028. if (PyErr_Occurred())
  1029. PyErr_Print();
  1030. Py_XDECREF(result);
  1031. }
  1032. }
  1033. static void
  1034. pythonAttributeDecl(void *user_data,
  1035. const xmlChar * elem,
  1036. const xmlChar * name,
  1037. int type,
  1038. int def,
  1039. const xmlChar * defaultValue, xmlEnumerationPtr tree)
  1040. {
  1041. PyObject *handler;
  1042. PyObject *nameList;
  1043. PyObject *newName;
  1044. xmlEnumerationPtr node;
  1045. PyObject *result;
  1046. int count;
  1047. handler = (PyObject *) user_data;
  1048. if (PyObject_HasAttrString(handler, (char *) "attributeDecl")) {
  1049. count = 0;
  1050. for (node = tree; node != NULL; node = node->next) {
  1051. count++;
  1052. }
  1053. nameList = PyList_New(count);
  1054. count = 0;
  1055. for (node = tree; node != NULL; node = node->next) {
  1056. newName = PyString_FromString((char *) node->name);
  1057. PyList_SetItem(nameList, count, newName);
  1058. Py_DECREF(newName);
  1059. count++;
  1060. }
  1061. result = PyObject_CallMethod(handler, (char *) "attributeDecl",
  1062. (char *) "ssiisO", elem, name, type,
  1063. def, defaultValue, nameList);
  1064. if (PyErr_Occurred())
  1065. PyErr_Print();
  1066. Py_XDECREF(nameList);
  1067. Py_XDECREF(result);
  1068. }
  1069. }
  1070. static void
  1071. pythonElementDecl(void *user_data,
  1072. const xmlChar * name,
  1073. int type, ATTRIBUTE_UNUSED xmlElementContentPtr content)
  1074. {
  1075. PyObject *handler;
  1076. PyObject *obj;
  1077. PyObject *result;
  1078. handler = (PyObject *) user_data;
  1079. if (PyObject_HasAttrString(handler, (char *) "elementDecl")) {
  1080. /* TODO: wrap in an elementContent object */
  1081. printf
  1082. ("pythonElementDecl: xmlElementContentPtr wrapper missing !\n");
  1083. obj = Py_None;
  1084. /* Py_XINCREF(Py_None); isn't the reference just borrowed ??? */
  1085. result = PyObject_CallMethod(handler, (char *) "elementDecl",
  1086. (char *) "siO", name, type, obj);
  1087. if (PyErr_Occurred())
  1088. PyErr_Print();
  1089. Py_XDECREF(result);
  1090. }
  1091. }
  1092. static void
  1093. pythonUnparsedEntityDecl(void *user_data,
  1094. const xmlChar * name,
  1095. const xmlChar * publicId,
  1096. const xmlChar * systemId,
  1097. const xmlChar * notationName)
  1098. {
  1099. PyObject *handler;
  1100. PyObject *result;
  1101. handler = (PyObject *) user_data;
  1102. if (PyObject_HasAttrString(handler, (char *) "unparsedEntityDecl")) {
  1103. result =
  1104. PyObject_CallMethod(handler, (char *) "unparsedEntityDecl",
  1105. (char *) "ssss", name, publicId, systemId,
  1106. notationName);
  1107. if (PyErr_Occurred())
  1108. PyErr_Print();
  1109. Py_XDECREF(result);
  1110. }
  1111. }
  1112. static void
  1113. pythonInternalSubset(void *user_data, const xmlChar * name,
  1114. const xmlChar * ExternalID, const xmlChar * SystemID)
  1115. {
  1116. PyObject *handler;
  1117. PyObject *result;
  1118. #ifdef DEBUG_SAX
  1119. printf("pythonInternalSubset(%s, %s, %s) called\n",
  1120. name, ExternalID, SystemID);
  1121. #endif
  1122. handler = (PyObject *) user_data;
  1123. if (PyObject_HasAttrString(handler, (char *) "internalSubset")) {
  1124. result = PyObject_CallMethod(handler, (char *) "internalSubset",
  1125. (char *) "sss", name, ExternalID,
  1126. SystemID);
  1127. if (PyErr_Occurred())
  1128. PyErr_Print();
  1129. Py_XDECREF(result);
  1130. }
  1131. }
  1132. static xmlSAXHandler pythonSaxHandler = {
  1133. pythonInternalSubset,
  1134. NULL, /* TODO pythonIsStandalone, */
  1135. NULL, /* TODO pythonHasInternalSubset, */
  1136. NULL, /* TODO pythonHasExternalSubset, */
  1137. NULL, /* TODO pythonResolveEntity, */
  1138. NULL, /* TODO pythonGetEntity, */
  1139. pythonEntityDecl,
  1140. pythonNotationDecl,
  1141. pythonAttributeDecl,
  1142. pythonElementDecl,
  1143. pythonUnparsedEntityDecl,
  1144. NULL, /* OBSOLETED pythonSetDocumentLocator, */
  1145. pythonStartDocument,
  1146. pythonEndDocument,
  1147. pythonStartElement,
  1148. pythonEndElement,
  1149. pythonReference,
  1150. pythonCharacters,
  1151. pythonIgnorableWhitespace,
  1152. pythonProcessingInstruction,
  1153. pythonComment,
  1154. pythonWarning,
  1155. pythonError,
  1156. pythonFatalError,
  1157. NULL, /* TODO pythonGetParameterEntity, */
  1158. pythonCdataBlock,
  1159. pythonExternalSubset,
  1160. 1,
  1161. NULL, /* TODO mograte to SAX2 */
  1162. NULL,
  1163. NULL,
  1164. NULL
  1165. };
  1166. /************************************************************************
  1167. * *
  1168. * Handling of specific parser context *
  1169. * *
  1170. ************************************************************************/
  1171. PyObject *
  1172. libxml_xmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
  1173. PyObject * args)
  1174. {
  1175. const char *chunk;
  1176. int size;
  1177. const char *URI;
  1178. PyObject *pyobj_SAX = NULL;
  1179. xmlSAXHandlerPtr SAX = NULL;
  1180. xmlParserCtxtPtr ret;
  1181. PyObject *pyret;
  1182. if (!PyArg_ParseTuple
  1183. (args, (char *) "Oziz:xmlCreatePushParser", &pyobj_SAX, &chunk,
  1184. &size, &URI))
  1185. return (NULL);
  1186. #ifdef DEBUG
  1187. printf("libxml_xmlCreatePushParser(%p, %s, %d, %s) called\n",
  1188. pyobj_SAX, chunk, size, URI);
  1189. #endif
  1190. if (pyobj_SAX != Py_None) {
  1191. SAX = &pythonSaxHandler;
  1192. Py_INCREF(pyobj_SAX);
  1193. /* The reference is released in pythonEndDocument() */
  1194. }
  1195. ret = xmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI);
  1196. pyret = libxml_xmlParserCtxtPtrWrap(ret);
  1197. return (pyret);
  1198. }
  1199. PyObject *
  1200. libxml_htmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
  1201. PyObject * args)
  1202. {
  1203. #ifdef LIBXML_HTML_ENABLED
  1204. const char *chunk;
  1205. int size;
  1206. const char *URI;
  1207. PyObject *pyobj_SAX = NULL;
  1208. xmlSAXHandlerPtr SAX = NULL;
  1209. xmlParserCtxtPtr ret;
  1210. PyObject *pyret;
  1211. if (!PyArg_ParseTuple
  1212. (args, (char *) "Oziz:htmlCreatePushParser", &pyobj_SAX, &chunk,
  1213. &size, &URI))
  1214. return (NULL);
  1215. #ifdef DEBUG
  1216. printf("libxml_htmlCreatePushParser(%p, %s, %d, %s) called\n",
  1217. pyobj_SAX, chunk, size, URI);
  1218. #endif
  1219. if (pyobj_SAX != Py_None) {
  1220. SAX = &pythonSaxHandler;
  1221. Py_INCREF(pyobj_SAX);
  1222. /* The reference is released in pythonEndDocument() */
  1223. }
  1224. ret = htmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI,
  1225. XML_CHAR_ENCODING_NONE);
  1226. pyret = libxml_xmlParserCtxtPtrWrap(ret);
  1227. return (pyret);
  1228. #else
  1229. Py_INCREF(Py_None);
  1230. return (Py_None);
  1231. #endif /* LIBXML_HTML_ENABLED */
  1232. }
  1233. PyObject *
  1234. libxml_xmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  1235. {
  1236. int recover;
  1237. const char *URI;
  1238. PyObject *pyobj_SAX = NULL;
  1239. xmlSAXHandlerPtr SAX = NULL;
  1240. if (!PyArg_ParseTuple(args, (char *) "Osi:xmlSAXParseFile", &pyobj_SAX,
  1241. &URI, &recover))
  1242. return (NULL);
  1243. #ifdef DEBUG
  1244. printf("libxml_xmlSAXParseFile(%p, %s, %d) called\n",
  1245. pyobj_SAX, URI, recover);
  1246. #endif
  1247. if (pyobj_SAX == Py_None) {
  1248. Py_INCREF(Py_None);
  1249. return (Py_None);
  1250. }
  1251. SAX = &pythonSaxHandler;
  1252. Py_INCREF(pyobj_SAX);
  1253. /* The reference is released in pythonEndDocument() */
  1254. xmlSAXUserParseFile(SAX, pyobj_SAX, URI);
  1255. Py_INCREF(Py_None);
  1256. return (Py_None);
  1257. }
  1258. PyObject *
  1259. libxml_htmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  1260. {
  1261. #ifdef LIBXML_HTML_ENABLED
  1262. const char *URI;
  1263. const char *encoding;
  1264. PyObject *pyobj_SAX = NULL;
  1265. xmlSAXHandlerPtr SAX = NULL;
  1266. if (!PyArg_ParseTuple
  1267. (args, (char *) "Osz:htmlSAXParseFile", &pyobj_SAX, &URI,
  1268. &encoding))
  1269. return (NULL);
  1270. #ifdef DEBUG
  1271. printf("libxml_htmlSAXParseFile(%p, %s, %s) called\n",
  1272. pyobj_SAX, URI, encoding);
  1273. #endif
  1274. if (pyobj_SAX == Py_None) {
  1275. Py_INCREF(Py_None);
  1276. return (Py_None);
  1277. }
  1278. SAX = &pythonSaxHandler;
  1279. Py_INCREF(pyobj_SAX);
  1280. /* The reference is released in pythonEndDocument() */
  1281. htmlSAXParseFile(URI, encoding, SAX, pyobj_SAX);
  1282. Py_INCREF(Py_None);
  1283. return (Py_None);
  1284. #else
  1285. Py_INCREF(Py_None);
  1286. return (Py_None);
  1287. #endif /* LIBXML_HTML_ENABLED */
  1288. }
  1289. /************************************************************************
  1290. * *
  1291. * Error message callback *
  1292. * *
  1293. ************************************************************************/
  1294. static PyObject *libxml_xmlPythonErrorFuncHandler = NULL;
  1295. static PyObject *libxml_xmlPythonErrorFuncCtxt = NULL;
  1296. /* helper to build a xmlMalloc'ed string from a format and va_list */
  1297. /*
  1298. * disabled the loop, the repeated call to vsnprintf without reset of ap
  1299. * in case the initial buffer was too small segfaulted on x86_64
  1300. * we now directly vsnprintf on a large buffer.
  1301. */
  1302. static char *
  1303. libxml_buildMessage(const char *msg, va_list ap)
  1304. {
  1305. int chars;
  1306. char *str;
  1307. str = (char *) xmlMalloc(1000);
  1308. if (str == NULL)
  1309. return NULL;
  1310. chars = vsnprintf(str, 999, msg, ap);
  1311. if (chars >= 998)
  1312. str[999] = 0;
  1313. return str;
  1314. }
  1315. static void
  1316. libxml_xmlErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, const char *msg,
  1317. ...)
  1318. {
  1319. va_list ap;
  1320. PyObject *list;
  1321. PyObject *message;
  1322. PyObject *result;
  1323. char str[1000];
  1324. #ifdef DEBUG_ERROR
  1325. printf("libxml_xmlErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
  1326. #endif
  1327. if (libxml_xmlPythonErrorFuncHandler == NULL) {
  1328. va_start(ap, msg);
  1329. vfprintf(stderr, msg, ap);
  1330. va_end(ap);
  1331. } else {
  1332. va_start(ap, msg);
  1333. if (vsnprintf(str, 999, msg, ap) >= 998)
  1334. str[999] = 0;
  1335. va_end(ap);
  1336. list = PyTuple_New(2);
  1337. PyTuple_SetItem(list, 0, libxml_xmlPythonErrorFuncCtxt);
  1338. Py_XINCREF(libxml_xmlPythonErrorFuncCtxt);
  1339. message = libxml_charPtrConstWrap(str);
  1340. PyTuple_SetItem(list, 1, message);
  1341. result = PyEval_CallObject(libxml_xmlPythonErrorFuncHandler, list);
  1342. Py_XDECREF(list);
  1343. Py_XDECREF(result);
  1344. }
  1345. }
  1346. static void
  1347. libxml_xmlErrorInitialize(void)
  1348. {
  1349. #ifdef DEBUG_ERROR
  1350. printf("libxml_xmlErrorInitialize() called\n");
  1351. #endif
  1352. xmlSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
  1353. xmlThrDefSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
  1354. }
  1355. static PyObject *
  1356. libxml_xmlRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self,
  1357. PyObject * args)
  1358. {
  1359. PyObject *py_retval;
  1360. PyObject *pyobj_f;
  1361. PyObject *pyobj_ctx;
  1362. if (!PyArg_ParseTuple
  1363. (args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f,
  1364. &pyobj_ctx))
  1365. return (NULL);
  1366. #ifdef DEBUG_ERROR
  1367. printf("libxml_xmlRegisterErrorHandler(%p, %p) called\n", pyobj_ctx,
  1368. pyobj_f);
  1369. #endif
  1370. if (libxml_xmlPythonErrorFuncHandler != NULL) {
  1371. Py_XDECREF(libxml_xmlPythonErrorFuncHandler);
  1372. }
  1373. if (libxml_xmlPythonErrorFuncCtxt != NULL) {
  1374. Py_XDECREF(libxml_xmlPythonErrorFuncCtxt);
  1375. }
  1376. Py_XINCREF(pyobj_ctx);
  1377. Py_XINCREF(pyobj_f);
  1378. /* TODO: check f is a function ! */
  1379. libxml_xmlPythonErrorFuncHandler = pyobj_f;
  1380. libxml_xmlPythonErrorFuncCtxt = pyobj_ctx;
  1381. py_retval = libxml_intWrap(1);
  1382. return (py_retval);
  1383. }
  1384. /************************************************************************
  1385. * *
  1386. * Per parserCtxt error handler *
  1387. * *
  1388. ************************************************************************/
  1389. typedef struct
  1390. {
  1391. PyObject *f;
  1392. PyObject *arg;
  1393. } xmlParserCtxtPyCtxt;
  1394. typedef xmlParserCtxtPyCtxt *xmlParserCtxtPyCtxtPtr;
  1395. static void
  1396. libxml_xmlParserCtxtGenericErrorFuncHandler(void *ctx, int severity, char *str)
  1397. {
  1398. PyObject *list;
  1399. PyObject *result;
  1400. xmlParserCtxtPtr ctxt;
  1401. xmlParserCtxtPyCtxtPtr pyCtxt;
  1402. #ifdef DEBUG_ERROR
  1403. printf("libxml_xmlParserCtxtGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
  1404. #endif
  1405. ctxt = (xmlParserCtxtPtr)ctx;
  1406. pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
  1407. list = PyTuple_New(4);
  1408. PyTuple_SetItem(list, 0, pyCtxt->arg);
  1409. Py_XINCREF(pyCtxt->arg);
  1410. PyTuple_SetItem(list, 1, libxml_charPtrWrap(str));
  1411. PyTuple_SetItem(list, 2, libxml_intWrap(severity));
  1412. PyTuple_SetItem(list, 3, Py_None);
  1413. Py_INCREF(Py_None);
  1414. result = PyEval_CallObject(pyCtxt->f, list);
  1415. if (result == NULL)
  1416. {
  1417. /* TODO: manage for the exception to be propagated... */
  1418. PyErr_Print();
  1419. }
  1420. Py_XDECREF(list);
  1421. Py_XDECREF(result);
  1422. }
  1423. static void
  1424. libxml_xmlParserCtxtErrorFuncHandler(void *ctx, const char *msg, ...)
  1425. {
  1426. va_list ap;
  1427. va_start(ap, msg);
  1428. libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_ERROR,libxml_buildMessage(msg,ap));
  1429. va_end(ap);
  1430. }
  1431. static void
  1432. libxml_xmlParserCtxtWarningFuncHandler(void *ctx, const char *msg, ...)
  1433. {
  1434. va_list ap;
  1435. va_start(ap, msg);
  1436. libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_WARNING,libxml_buildMessage(msg,ap));
  1437. va_end(ap);
  1438. }
  1439. static void
  1440. libxml_xmlParserCtxtValidityErrorFuncHandler(void *ctx, const char *msg, ...)
  1441. {
  1442. va_list ap;
  1443. va_start(ap, msg);
  1444. libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap));
  1445. va_end(ap);
  1446. }
  1447. static void
  1448. libxml_xmlParserCtxtValidityWarningFuncHandler(void *ctx, const char *msg, ...)
  1449. {
  1450. va_list ap;
  1451. va_start(ap, msg);
  1452. libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap));
  1453. va_end(ap);
  1454. }
  1455. static PyObject *
  1456. libxml_xmlParserCtxtSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
  1457. {
  1458. PyObject *py_retval;
  1459. xmlParserCtxtPtr ctxt;
  1460. xmlParserCtxtPyCtxtPtr pyCtxt;
  1461. PyObject *pyobj_ctxt;
  1462. PyObject *pyobj_f;
  1463. PyObject *pyobj_arg;
  1464. if (!PyArg_ParseTuple(args, (char *)"OOO:xmlParserCtxtSetErrorHandler",
  1465. &pyobj_ctxt, &pyobj_f, &pyobj_arg))
  1466. return(NULL);
  1467. ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
  1468. if (ctxt->_private == NULL) {
  1469. pyCtxt = xmlMalloc(sizeof(xmlParserCtxtPyCtxt));
  1470. if (pyCtxt == NULL) {
  1471. py_retval = libxml_intWrap(-1);
  1472. return(py_retval);
  1473. }
  1474. memset(pyCtxt,0,sizeof(xmlParserCtxtPyCtxt));
  1475. ctxt->_private = pyCtxt;
  1476. }
  1477. else {
  1478. pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
  1479. }
  1480. /* TODO: check f is a function ! */
  1481. Py_XDECREF(pyCtxt->f);
  1482. Py_XINCREF(pyobj_f);
  1483. pyCtxt->f = pyobj_f;
  1484. Py_XDECREF(pyCtxt->arg);
  1485. Py_XINCREF(pyobj_arg);
  1486. pyCtxt->arg = pyobj_arg;
  1487. if (pyobj_f != Py_None) {
  1488. ctxt->sax->error = libxml_xmlParserCtxtErrorFuncHandler;
  1489. ctxt->sax->warning = libxml_xmlParserCtxtWarningFuncHandler;
  1490. ctxt->vctxt.error = libxml_xmlParserCtxtValidityErrorFuncHandler;
  1491. ctxt->vctxt.warning = libxml_xmlParserCtxtValidityWarningFuncHandler;
  1492. }
  1493. else {
  1494. ctxt->sax->error = xmlParserError;
  1495. ctxt->vctxt.error = xmlParserValidityError;
  1496. ctxt->sax->warning = xmlParserWarning;
  1497. ctxt->vctxt.warning = xmlParserValidityWarning;
  1498. }
  1499. py_retval = libxml_intWrap(1);
  1500. return(py_retval);
  1501. }
  1502. static PyObject *
  1503. libxml_xmlParserCtxtGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
  1504. {
  1505. PyObject *py_retval;
  1506. xmlParserCtxtPtr ctxt;
  1507. xmlParserCtxtPyCtxtPtr pyCtxt;
  1508. PyObject *pyobj_ctxt;
  1509. if (!PyArg_ParseTuple(args, (char *)"O:xmlParserCtxtGetErrorHandler",
  1510. &pyobj_ctxt))
  1511. return(NULL);
  1512. ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
  1513. py_retval = PyTuple_New(2);
  1514. if (ctxt->_private != NULL) {
  1515. pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
  1516. PyTuple_SetItem(py_retval, 0, pyCtxt->f);
  1517. Py_XINCREF(pyCtxt->f);
  1518. PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
  1519. Py_XINCREF(pyCtxt->arg);
  1520. }
  1521. else {
  1522. /* no python error handler registered */
  1523. PyTuple_SetItem(py_retval, 0, Py_None);
  1524. Py_XINCREF(Py_None);
  1525. PyTuple_SetItem(py_retval, 1, Py_None);
  1526. Py_XINCREF(Py_None);
  1527. }
  1528. return(py_retval);
  1529. }
  1530. static PyObject *
  1531. libxml_xmlFreeParserCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
  1532. xmlParserCtxtPtr ctxt;
  1533. PyObject *pyobj_ctxt;
  1534. xmlParserCtxtPyCtxtPtr pyCtxt;
  1535. if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeParserCtxt", &pyobj_ctxt))
  1536. return(NULL);
  1537. ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
  1538. if (ctxt != NULL) {
  1539. pyCtxt = (xmlParserCtxtPyCtxtPtr)((xmlParserCtxtPtr)ctxt)->_private;
  1540. if (pyCtxt) {
  1541. Py_XDECREF(pyCtxt->f);
  1542. Py_XDECREF(pyCtxt->arg);
  1543. xmlFree(pyCtxt);
  1544. }
  1545. xmlFreeParserCtxt(ctxt);
  1546. }
  1547. Py_INCREF(Py_None);
  1548. return(Py_None);
  1549. }
  1550. /***
  1551. * xmlValidCtxt stuff
  1552. */
  1553. typedef struct
  1554. {
  1555. PyObject *warn;
  1556. PyObject *error;
  1557. PyObject *arg;
  1558. } xmlValidCtxtPyCtxt;
  1559. typedef xmlValidCtxtPyCtxt *xmlValidCtxtPyCtxtPtr;
  1560. static void
  1561. libxml_xmlValidCtxtGenericErrorFuncHandler(void *ctx, int severity, char *str)
  1562. {
  1563. PyObject *list;
  1564. PyObject *result;
  1565. xmlValidCtxtPyCtxtPtr pyCtxt;
  1566. #ifdef DEBUG_ERROR
  1567. printf("libxml_xmlValidCtxtGenericErrorFuncHandler(%p, %d, %s, ...) called\n", ctx, severity, str);
  1568. #endif
  1569. pyCtxt = (xmlValidCtxtPyCtxtPtr)ctx;
  1570. list = PyTuple_New(2);
  1571. PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
  1572. PyTuple_SetItem(list, 1, pyCtxt->arg);
  1573. Py_XINCREF(pyCtxt->arg);
  1574. result = PyEval_CallObject(pyCtxt->error, list);
  1575. if (result == NULL)
  1576. {
  1577. /* TODO: manage for the exception to be propagated... */
  1578. PyErr_Print();
  1579. }
  1580. Py_XDECREF(list);
  1581. Py_XDECREF(result);
  1582. }
  1583. static void
  1584. libxml_xmlValidCtxtGenericWarningFuncHandler(void *ctx, int severity, char *str)
  1585. {
  1586. PyObject *list;
  1587. PyObject *result;
  1588. xmlValidCtxtPyCtxtPtr pyCtxt;
  1589. #ifdef DEBUG_ERROR
  1590. printf("libxml_xmlValidCtxtGenericWarningFuncHandler(%p, %d, %s, ...) called\n", ctx, severity, str);
  1591. #endif
  1592. pyCtxt = (xmlValidCtxtPyCtxtPtr)ctx;
  1593. list = PyTuple_New(2);
  1594. PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
  1595. PyTuple_SetItem(list, 1, pyCtxt->arg);
  1596. Py_XINCREF(pyCtxt->arg);
  1597. result = PyEval_CallObject(pyCtxt->warn, list);
  1598. if (result == NULL)
  1599. {
  1600. /* TODO: manage for the exception to be propagated... */
  1601. PyErr_Print();
  1602. }
  1603. Py_XDECREF(list);
  1604. Py_XDECREF(result);
  1605. }
  1606. static void
  1607. libxml_xmlValidCtxtErrorFuncHandler(void *ctx, const char *msg, ...)
  1608. {
  1609. va_list ap;
  1610. va_start(ap, msg);
  1611. libxml_xmlValidCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap));
  1612. va_end(ap);
  1613. }
  1614. static void
  1615. libxml_xmlValidCtxtWarningFuncHandler(void *ctx, const char *msg, ...)
  1616. {
  1617. va_list ap;
  1618. va_start(ap, msg);
  1619. libxml_xmlValidCtxtGenericWarningFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap));
  1620. va_end(ap);
  1621. }
  1622. static PyObject *
  1623. libxml_xmlSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  1624. {
  1625. PyObject *py_retval;
  1626. PyObject *pyobj_error;
  1627. PyObject *pyobj_warn;
  1628. PyObject *pyobj_ctx;
  1629. PyObject *pyobj_arg = Py_None;
  1630. xmlValidCtxtPtr ctxt;
  1631. xmlValidCtxtPyCtxtPtr pyCtxt;
  1632. if (!PyArg_ParseTuple
  1633. (args, (char *) "OOO|O:xmlSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
  1634. return (NULL);
  1635. #ifdef DEBUG_ERROR
  1636. printf("libxml_xmlSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
  1637. #endif
  1638. ctxt = PyValidCtxt_Get(pyobj_ctx);
  1639. pyCtxt = xmlMalloc(sizeof(xmlValidCtxtPyCtxt));
  1640. if (pyCtxt == NULL) {
  1641. py_retval = libxml_intWrap(-1);
  1642. return(py_retval);
  1643. }
  1644. memset(pyCtxt, 0, sizeof(xmlValidCtxtPyCtxt));
  1645. /* TODO: check warn and error is a function ! */
  1646. Py_XDECREF(pyCtxt->error);
  1647. Py_XINCREF(pyobj_error);
  1648. pyCtxt->error = pyobj_error;
  1649. Py_XDECREF(pyCtxt->warn);
  1650. Py_XINCREF(pyobj_warn);
  1651. pyCtxt->warn = pyobj_warn;
  1652. Py_XDECREF(pyCtxt->arg);
  1653. Py_XINCREF(pyobj_arg);
  1654. pyCtxt->arg = pyobj_arg;
  1655. ctxt->error = libxml_xmlValidCtxtErrorFuncHandler;
  1656. ctxt->warning = libxml_xmlValidCtxtWarningFuncHandler;
  1657. ctxt->userData = pyCtxt;
  1658. py_retval = libxml_intWrap(1);
  1659. return (py_retval);
  1660. }
  1661. static PyObject *
  1662. libxml_xmlFreeValidCtxt(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
  1663. xmlValidCtxtPtr cur;
  1664. xmlValidCtxtPyCtxtPtr pyCtxt;
  1665. PyObject *pyobj_cur;
  1666. if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeValidCtxt", &pyobj_cur))
  1667. return(NULL);
  1668. cur = (xmlValidCtxtPtr) PyValidCtxt_Get(pyobj_cur);
  1669. pyCtxt = (xmlValidCtxtPyCtxtPtr)(cur->userData);
  1670. if (pyCtxt != NULL)
  1671. {
  1672. Py_XDECREF(pyCtxt->error);
  1673. Py_XDECREF(pyCtxt->warn);
  1674. Py_XDECREF(pyCtxt->arg);
  1675. xmlFree(pyCtxt);
  1676. }
  1677. xmlFreeValidCtxt(cur);
  1678. Py_INCREF(Py_None);
  1679. return(Py_None);
  1680. }
  1681. #ifdef LIBXML_READER_ENABLED
  1682. /************************************************************************
  1683. * *
  1684. * Per xmlTextReader error handler *
  1685. * *
  1686. ************************************************************************/
  1687. typedef struct
  1688. {
  1689. PyObject *f;
  1690. PyObject *arg;
  1691. } xmlTextReaderPyCtxt;
  1692. typedef xmlTextReaderPyCtxt *xmlTextReaderPyCtxtPtr;
  1693. static void
  1694. libxml_xmlTextReaderErrorCallback(void *arg,
  1695. const char *msg,
  1696. int severity,
  1697. xmlTextReaderLocatorPtr locator)
  1698. {
  1699. xmlTextReaderPyCtxt *pyCtxt = (xmlTextReaderPyCtxt *)arg;
  1700. PyObject *list;
  1701. PyObject *result;
  1702. list = PyTuple_New(4);
  1703. PyTuple_SetItem(list, 0, pyCtxt->arg);
  1704. Py_XINCREF(pyCtxt->arg);
  1705. PyTuple_SetItem(list, 1, libxml_charPtrConstWrap(msg));
  1706. PyTuple_SetItem(list, 2, libxml_intWrap(severity));
  1707. PyTuple_SetItem(list, 3, libxml_xmlTextReaderLocatorPtrWrap(locator));
  1708. result = PyEval_CallObject(pyCtxt->f, list);
  1709. if (result == NULL)
  1710. {
  1711. /* TODO: manage for the exception to be propagated... */
  1712. PyErr_Print();
  1713. }
  1714. Py_XDECREF(list);
  1715. Py_XDECREF(result);
  1716. }
  1717. static PyObject *
  1718. libxml_xmlTextReaderSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
  1719. {
  1720. xmlTextReaderPtr reader;
  1721. xmlTextReaderPyCtxtPtr pyCtxt;
  1722. xmlTextReaderErrorFunc f;
  1723. void *arg;
  1724. PyObject *pyobj_reader;
  1725. PyObject *pyobj_f;
  1726. PyObject *pyobj_arg;
  1727. PyObject *py_retval;
  1728. if (!PyArg_ParseTuple(args, (char *)"OOO:xmlTextReaderSetErrorHandler", &pyobj_reader, &pyobj_f, &pyobj_arg))
  1729. return(NULL);
  1730. reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
  1731. /* clear previous error handler */
  1732. xmlTextReaderGetErrorHandler(reader,&f,&arg);
  1733. if (arg != NULL) {
  1734. if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) {
  1735. /* ok, it's our error handler! */
  1736. pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
  1737. Py_XDECREF(pyCtxt->f);
  1738. Py_XDECREF(pyCtxt->arg);
  1739. xmlFree(pyCtxt);
  1740. }
  1741. else {
  1742. /*
  1743. * there already an arg, and it's not ours,
  1744. * there is definitely something wrong going on here...
  1745. * we don't know how to free it, so we bail out...
  1746. */
  1747. py_retval = libxml_intWrap(-1);
  1748. return(py_retval);
  1749. }
  1750. }
  1751. xmlTextReaderSetErrorHandler(reader,NULL,NULL);
  1752. /* set new error handler */
  1753. if (pyobj_f != Py_None)
  1754. {
  1755. pyCtxt = (xmlTextReaderPyCtxtPtr)xmlMalloc(sizeof(xmlTextReaderPyCtxt));
  1756. if (pyCtxt == NULL) {
  1757. py_retval = libxml_intWrap(-1);
  1758. return(py_retval);
  1759. }
  1760. Py_XINCREF(pyobj_f);
  1761. pyCtxt->f = pyobj_f;
  1762. Py_XINCREF(pyobj_arg);
  1763. pyCtxt->arg = pyobj_arg;
  1764. xmlTextReaderSetErrorHandler(reader,
  1765. (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback,
  1766. pyCtxt);
  1767. }
  1768. py_retval = libxml_intWrap(1);
  1769. return(py_retval);
  1770. }
  1771. static PyObject *
  1772. libxml_xmlTextReaderGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
  1773. {
  1774. xmlTextReaderPtr reader;
  1775. xmlTextReaderPyCtxtPtr pyCtxt;
  1776. xmlTextReaderErrorFunc f;
  1777. void *arg;
  1778. PyObject *pyobj_reader;
  1779. PyObject *py_retval;
  1780. if (!PyArg_ParseTuple(args, (char *)"O:xmlTextReaderSetErrorHandler", &pyobj_reader))
  1781. return(NULL);
  1782. reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
  1783. xmlTextReaderGetErrorHandler(reader,&f,&arg);
  1784. py_retval = PyTuple_New(2);
  1785. if (f == (xmlTextReaderErrorFunc)libxml_xmlTextReaderErrorCallback) {
  1786. /* ok, it's our error handler! */
  1787. pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
  1788. PyTuple_SetItem(py_retval, 0, pyCtxt->f);
  1789. Py_XINCREF(pyCtxt->f);
  1790. PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
  1791. Py_XINCREF(pyCtxt->arg);
  1792. }
  1793. else
  1794. {
  1795. /* f is null or it's not our error handler */
  1796. PyTuple_SetItem(py_retval, 0, Py_None);
  1797. Py_XINCREF(Py_None);
  1798. PyTuple_SetItem(py_retval, 1, Py_None);
  1799. Py_XINCREF(Py_None);
  1800. }
  1801. return(py_retval);
  1802. }
  1803. static PyObject *
  1804. libxml_xmlFreeTextReader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
  1805. xmlTextReaderPtr reader;
  1806. PyObject *pyobj_reader;
  1807. xmlTextReaderPyCtxtPtr pyCtxt;
  1808. xmlTextReaderErrorFunc f;
  1809. void *arg;
  1810. if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeTextReader", &pyobj_reader))
  1811. return(NULL);
  1812. if (!PyCObject_Check(pyobj_reader)) {
  1813. Py_INCREF(Py_None);
  1814. return(Py_None);
  1815. }
  1816. reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
  1817. if (reader == NULL) {
  1818. Py_INCREF(Py_None);
  1819. return(Py_None);
  1820. }
  1821. xmlTextReaderGetErrorHandler(reader,&f,&arg);
  1822. if (arg != NULL) {
  1823. if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) {
  1824. /* ok, it's our error handler! */
  1825. pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
  1826. Py_XDECREF(pyCtxt->f);
  1827. Py_XDECREF(pyCtxt->arg);
  1828. xmlFree(pyCtxt);
  1829. }
  1830. /*
  1831. * else, something wrong happened, because the error handler is
  1832. * not owned by the python bindings...
  1833. */
  1834. }
  1835. xmlFreeTextReader(reader);
  1836. Py_INCREF(Py_None);
  1837. return(Py_None);
  1838. }
  1839. #endif
  1840. /************************************************************************
  1841. * *
  1842. * XPath extensions *
  1843. * *
  1844. ************************************************************************/
  1845. static void
  1846. libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs)
  1847. {
  1848. PyObject *list, *cur, *result;
  1849. xmlXPathObjectPtr obj;
  1850. xmlXPathContextPtr rctxt;
  1851. PyObject *current_function = NULL;
  1852. const xmlChar *name;
  1853. const xmlChar *ns_uri;
  1854. int i;
  1855. if (ctxt == NULL)
  1856. return;
  1857. rctxt = ctxt->context;
  1858. if (rctxt == NULL)
  1859. return;
  1860. name = rctxt->function;
  1861. ns_uri = rctxt->functionURI;
  1862. #ifdef DEBUG_XPATH
  1863. printf("libxml_xmlXPathFuncCallback called name %s URI %s\n", name,
  1864. ns_uri);
  1865. #endif
  1866. /*
  1867. * Find the function, it should be there it was there at lookup
  1868. */
  1869. for (i = 0; i < libxml_xpathCallbacksNb; i++) {
  1870. if ( /* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */
  1871. (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
  1872. (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
  1873. current_function = (*libxml_xpathCallbacks)[i].function;
  1874. }
  1875. }
  1876. if (current_function == NULL) {
  1877. printf
  1878. ("libxml_xmlXPathFuncCallback: internal error %s not found !\n",
  1879. name);
  1880. return;
  1881. }
  1882. list = PyTuple_New(nargs + 1);
  1883. PyTuple_SetItem(list, 0, libxml_xmlXPathParserContextPtrWrap(ctxt));
  1884. for (i = nargs - 1; i >= 0; i--) {
  1885. obj = valuePop(ctxt);
  1886. cur = libxml_xmlXPathObjectPtrWrap(obj);
  1887. PyTuple_SetItem(list, i + 1, cur);
  1888. }
  1889. result = PyEval_CallObject(current_function, list);
  1890. Py_DECREF(list);
  1891. obj = libxml_xmlXPathObjectPtrConvert(result);
  1892. valuePush(ctxt, obj);
  1893. }
  1894. static xmlXPathFunction
  1895. libxml_xmlXPathFuncLookupFunc(void *ctxt, const xmlChar * name,
  1896. const xmlChar * ns_uri)
  1897. {
  1898. int i;
  1899. #ifdef DEBUG_XPATH
  1900. printf("libxml_xmlXPathFuncLookupFunc(%p, %s, %s) called\n",
  1901. ctxt, name, ns_uri);
  1902. #endif
  1903. /*
  1904. * This is called once only. The address is then stored in the
  1905. * XPath expression evaluation, the proper object to call can
  1906. * then still be found using the execution context function
  1907. * and functionURI fields.
  1908. */
  1909. for (i = 0; i < libxml_xpathCallbacksNb; i++) {
  1910. if ((ctxt == (*libxml_xpathCallbacks)[i].ctx) &&
  1911. (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
  1912. (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
  1913. return (libxml_xmlXPathFuncCallback);
  1914. }
  1915. }
  1916. return (NULL);
  1917. }
  1918. static void
  1919. libxml_xpathCallbacksInitialize(void)
  1920. {
  1921. int i;
  1922. if (libxml_xpathCallbacksInitialized != 0)
  1923. return;
  1924. #ifdef DEBUG_XPATH
  1925. printf("libxml_xpathCallbacksInitialized called\n");
  1926. #endif
  1927. libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlMalloc(
  1928. libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
  1929. for (i = 0; i < libxml_xpathCallbacksAllocd; i++) {
  1930. (*libxml_xpathCallbacks)[i].ctx = NULL;
  1931. (*libxml_xpathCallbacks)[i].name = NULL;
  1932. (*libxml_xpathCallbacks)[i].ns_uri = NULL;
  1933. (*libxml_xpathCallbacks)[i].function = NULL;
  1934. }
  1935. libxml_xpathCallbacksInitialized = 1;
  1936. }
  1937. PyObject *
  1938. libxml_xmlRegisterXPathFunction(ATTRIBUTE_UNUSED PyObject * self,
  1939. PyObject * args)
  1940. {
  1941. PyObject *py_retval;
  1942. int c_retval = 0;
  1943. xmlChar *name;
  1944. xmlChar *ns_uri;
  1945. xmlXPathContextPtr ctx;
  1946. PyObject *pyobj_ctx;
  1947. PyObject *pyobj_f;
  1948. int i;
  1949. if (!PyArg_ParseTuple
  1950. (args, (char *) "OszO:registerXPathFunction", &pyobj_ctx, &name,
  1951. &ns_uri, &pyobj_f))
  1952. return (NULL);
  1953. ctx = (xmlXPathContextPtr) PyxmlXPathContext_Get(pyobj_ctx);
  1954. if (libxml_xpathCallbacksInitialized == 0)
  1955. libxml_xpathCallbacksInitialize();
  1956. xmlXPathRegisterFuncLookup(ctx, libxml_xmlXPathFuncLookupFunc, ctx);
  1957. if ((pyobj_ctx == NULL) || (name == NULL) || (pyobj_f == NULL)) {
  1958. py_retval = libxml_intWrap(-1);
  1959. return (py_retval);
  1960. }
  1961. #ifdef DEBUG_XPATH
  1962. printf("libxml_registerXPathFunction(%p, %s, %s) called\n",
  1963. ctx, name, ns_uri);
  1964. #endif
  1965. for (i = 0; i < libxml_xpathCallbacksNb; i++) {
  1966. if ((ctx == (*libxml_xpathCallbacks)[i].ctx) &&
  1967. (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
  1968. (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
  1969. Py_XINCREF(pyobj_f);
  1970. Py_XDECREF((*libxml_xpathCallbacks)[i].function);
  1971. (*libxml_xpathCallbacks)[i].function = pyobj_f;
  1972. c_retval = 1;
  1973. goto done;
  1974. }
  1975. }
  1976. if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksAllocd) {
  1977. libxml_xpathCallbacksAllocd+=10;
  1978. libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlRealloc(
  1979. libxml_xpathCallbacks,
  1980. libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
  1981. }
  1982. i = libxml_xpathCallbacksNb++;
  1983. Py_XINCREF(pyobj_f);
  1984. (*libxml_xpathCallbacks)[i].ctx = ctx;
  1985. (*libxml_xpathCallbacks)[i].name = xmlStrdup(name);
  1986. (*libxml_xpathCallbacks)[i].ns_uri = xmlStrdup(ns_uri);
  1987. (*libxml_xpathCallbacks)[i].function = pyobj_f;
  1988. c_retval = 1;
  1989. done:
  1990. py_retval = libxml_intWrap((int) c_retval);
  1991. return (py_retval);
  1992. }
  1993. /************************************************************************
  1994. * *
  1995. * Global properties access *
  1996. * *
  1997. ************************************************************************/
  1998. static PyObject *
  1999. libxml_name(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  2000. {
  2001. PyObject *resultobj, *obj;
  2002. xmlNodePtr cur;
  2003. const xmlChar *res;
  2004. if (!PyArg_ParseTuple(args, (char *) "O:name", &obj))
  2005. return NULL;
  2006. cur = PyxmlNode_Get(obj);
  2007. #ifdef DEBUG
  2008. printf("libxml_name: cur = %p type %d\n", cur, cur->type);
  2009. #endif
  2010. switch (cur->type) {
  2011. case XML_DOCUMENT_NODE:
  2012. #ifdef LIBXML_DOCB_ENABLED
  2013. case XML_DOCB_DOCUMENT_NODE:
  2014. #endif
  2015. case XML_HTML_DOCUMENT_NODE:{
  2016. xmlDocPtr doc = (xmlDocPtr) cur;
  2017. res = doc->URL;
  2018. break;
  2019. }
  2020. case XML_ATTRIBUTE_NODE:{
  2021. xmlAttrPtr attr = (xmlAttrPtr) cur;
  2022. res = attr->name;
  2023. break;
  2024. }
  2025. case XML_NAMESPACE_DECL:{
  2026. xmlNsPtr ns = (xmlNsPtr) cur;
  2027. res = ns->prefix;
  2028. break;
  2029. }
  2030. default:
  2031. res = cur->name;
  2032. break;
  2033. }
  2034. resultobj = libxml_constxmlCharPtrWrap(res);
  2035. return resultobj;
  2036. }
  2037. static PyObject *
  2038. libxml_doc(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  2039. {
  2040. PyObject *resultobj, *obj;
  2041. xmlNodePtr cur;
  2042. xmlDocPtr res;
  2043. if (!PyArg_ParseTuple(args, (char *) "O:doc", &obj))
  2044. return NULL;
  2045. cur = PyxmlNode_Get(obj);
  2046. #ifdef DEBUG
  2047. printf("libxml_doc: cur = %p\n", cur);
  2048. #endif
  2049. switch (cur->type) {
  2050. case XML_DOCUMENT_NODE:
  2051. #ifdef LIBXML_DOCB_ENABLED
  2052. case XML_DOCB_DOCUMENT_NODE:
  2053. #endif
  2054. case XML_HTML_DOCUMENT_NODE:
  2055. res = NULL;
  2056. break;
  2057. case XML_ATTRIBUTE_NODE:{
  2058. xmlAttrPtr attr = (xmlAttrPtr) cur;
  2059. res = attr->doc;
  2060. break;
  2061. }
  2062. case XML_NAMESPACE_DECL:
  2063. res = NULL;
  2064. break;
  2065. default:
  2066. res = cur->doc;
  2067. break;
  2068. }
  2069. resultobj = libxml_xmlDocPtrWrap(res);
  2070. return resultobj;
  2071. }
  2072. static PyObject *
  2073. libxml_properties(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  2074. {
  2075. PyObject *resultobj, *obj;
  2076. xmlNodePtr cur;
  2077. xmlAttrPtr res;
  2078. if (!PyArg_ParseTuple(args, (char *) "O:properties", &obj))
  2079. return NULL;
  2080. cur = PyxmlNode_Get(obj);
  2081. if ((cur != NULL) && (cur->type == XML_ELEMENT_NODE))
  2082. res = cur->properties;
  2083. else
  2084. res = NULL;
  2085. resultobj = libxml_xmlAttrPtrWrap(res);
  2086. return resultobj;
  2087. }
  2088. static PyObject *
  2089. libxml_next(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  2090. {
  2091. PyObject *resultobj, *obj;
  2092. xmlNodePtr cur;
  2093. xmlNodePtr res;
  2094. if (!PyArg_ParseTuple(args, (char *) "O:next", &obj))
  2095. return NULL;
  2096. cur = PyxmlNode_Get(obj);
  2097. #ifdef DEBUG
  2098. printf("libxml_next: cur = %p\n", cur);
  2099. #endif
  2100. switch (cur->type) {
  2101. case XML_DOCUMENT_NODE:
  2102. #ifdef LIBXML_DOCB_ENABLED
  2103. case XML_DOCB_DOCUMENT_NODE:
  2104. #endif
  2105. case XML_HTML_DOCUMENT_NODE:
  2106. res = NULL;
  2107. break;
  2108. case XML_ATTRIBUTE_NODE:{
  2109. xmlAttrPtr attr = (xmlAttrPtr) cur;
  2110. res = (xmlNodePtr) attr->next;
  2111. break;
  2112. }
  2113. case XML_NAMESPACE_DECL:{
  2114. xmlNsPtr ns = (xmlNsPtr) cur;
  2115. res = (xmlNodePtr) ns->next;
  2116. break;
  2117. }
  2118. default:
  2119. res = cur->next;
  2120. break;
  2121. }
  2122. resultobj = libxml_xmlNodePtrWrap(res);
  2123. return resultobj;
  2124. }
  2125. static PyObject *
  2126. libxml_prev(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  2127. {
  2128. PyObject *resultobj, *obj;
  2129. xmlNodePtr cur;
  2130. xmlNodePtr res;
  2131. if (!PyArg_ParseTuple(args, (char *) "O:prev", &obj))
  2132. return NULL;
  2133. cur = PyxmlNode_Get(obj);
  2134. #ifdef DEBUG
  2135. printf("libxml_prev: cur = %p\n", cur);
  2136. #endif
  2137. switch (cur->type) {
  2138. case XML_DOCUMENT_NODE:
  2139. #ifdef LIBXML_DOCB_ENABLED
  2140. case XML_DOCB_DOCUMENT_NODE:
  2141. #endif
  2142. case XML_HTML_DOCUMENT_NODE:
  2143. res = NULL;
  2144. break;
  2145. case XML_ATTRIBUTE_NODE:{
  2146. xmlAttrPtr attr = (xmlAttrPtr) cur;
  2147. res = (xmlNodePtr) attr->prev;
  2148. }
  2149. break;
  2150. case XML_NAMESPACE_DECL:
  2151. res = NULL;
  2152. break;
  2153. default:
  2154. res = cur->prev;
  2155. break;
  2156. }
  2157. resultobj = libxml_xmlNodePtrWrap(res);
  2158. return resultobj;
  2159. }
  2160. static PyObject *
  2161. libxml_children(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  2162. {
  2163. PyObject *resultobj, *obj;
  2164. xmlNodePtr cur;
  2165. xmlNodePtr res;
  2166. if (!PyArg_ParseTuple(args, (char *) "O:children", &obj))
  2167. return NULL;
  2168. cur = PyxmlNode_Get(obj);
  2169. #ifdef DEBUG
  2170. printf("libxml_children: cur = %p\n", cur);
  2171. #endif
  2172. switch (cur->type) {
  2173. case XML_ELEMENT_NODE:
  2174. case XML_ENTITY_REF_NODE:
  2175. case XML_ENTITY_NODE:
  2176. case XML_PI_NODE:
  2177. case XML_COMMENT_NODE:
  2178. case XML_DOCUMENT_NODE:
  2179. #ifdef LIBXML_DOCB_ENABLED
  2180. case XML_DOCB_DOCUMENT_NODE:
  2181. #endif
  2182. case XML_HTML_DOCUMENT_NODE:
  2183. case XML_DTD_NODE:
  2184. res = cur->children;
  2185. break;
  2186. case XML_ATTRIBUTE_NODE:{
  2187. xmlAttrPtr attr = (xmlAttrPtr) cur;
  2188. res = attr->children;
  2189. break;
  2190. }
  2191. default:
  2192. res = NULL;
  2193. break;
  2194. }
  2195. resultobj = libxml_xmlNodePtrWrap(res);
  2196. return resultobj;
  2197. }
  2198. static PyObject *
  2199. libxml_last(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  2200. {
  2201. PyObject *resultobj, *obj;
  2202. xmlNodePtr cur;
  2203. xmlNodePtr res;
  2204. if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
  2205. return NULL;
  2206. cur = PyxmlNode_Get(obj);
  2207. #ifdef DEBUG
  2208. printf("libxml_last: cur = %p\n", cur);
  2209. #endif
  2210. switch (cur->type) {
  2211. case XML_ELEMENT_NODE:
  2212. case XML_ENTITY_REF_NODE:
  2213. case XML_ENTITY_NODE:
  2214. case XML_PI_NODE:
  2215. case XML_COMMENT_NODE:
  2216. case XML_DOCUMENT_NODE:
  2217. #ifdef LIBXML_DOCB_ENABLED
  2218. case XML_DOCB_DOCUMENT_NODE:
  2219. #endif
  2220. case XML_HTML_DOCUMENT_NODE:
  2221. case XML_DTD_NODE:
  2222. res = cur->last;
  2223. break;
  2224. case XML_ATTRIBUTE_NODE:{
  2225. xmlAttrPtr attr = (xmlAttrPtr) cur;
  2226. res = attr->last;
  2227. }
  2228. default:
  2229. res = NULL;
  2230. break;
  2231. }
  2232. resultobj = libxml_xmlNodePtrWrap(res);
  2233. return resultobj;
  2234. }
  2235. static PyObject *
  2236. libxml_parent(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  2237. {
  2238. PyObject *resultobj, *obj;
  2239. xmlNodePtr cur;
  2240. xmlNodePtr res;
  2241. if (!PyArg_ParseTuple(args, (char *) "O:parent", &obj))
  2242. return NULL;
  2243. cur = PyxmlNode_Get(obj);
  2244. #ifdef DEBUG
  2245. printf("libxml_parent: cur = %p\n", cur);
  2246. #endif
  2247. switch (cur->type) {
  2248. case XML_DOCUMENT_NODE:
  2249. case XML_HTML_DOCUMENT_NODE:
  2250. #ifdef LIBXML_DOCB_ENABLED
  2251. case XML_DOCB_DOCUMENT_NODE:
  2252. #endif
  2253. res = NULL;
  2254. break;
  2255. case XML_ATTRIBUTE_NODE:{
  2256. xmlAttrPtr attr = (xmlAttrPtr) cur;
  2257. res = attr->parent;
  2258. }
  2259. break;
  2260. case XML_ENTITY_DECL:
  2261. case XML_NAMESPACE_DECL:
  2262. case XML_XINCLUDE_START:
  2263. case XML_XINCLUDE_END:
  2264. res = NULL;
  2265. break;
  2266. default:
  2267. res = cur->parent;
  2268. break;
  2269. }
  2270. resultobj = libxml_xmlNodePtrWrap(res);
  2271. return resultobj;
  2272. }
  2273. static PyObject *
  2274. libxml_type(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  2275. {
  2276. PyObject *resultobj, *obj;
  2277. xmlNodePtr cur;
  2278. const xmlChar *res = NULL;
  2279. if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
  2280. return NULL;
  2281. cur = PyxmlNode_Get(obj);
  2282. #ifdef DEBUG
  2283. printf("libxml_type: cur = %p\n", cur);
  2284. #endif
  2285. switch (cur->type) {
  2286. case XML_ELEMENT_NODE:
  2287. res = (const xmlChar *) "element";
  2288. break;
  2289. case XML_ATTRIBUTE_NODE:
  2290. res = (const xmlChar *) "attribute";
  2291. break;
  2292. case XML_TEXT_NODE:
  2293. res = (const xmlChar *) "text";
  2294. break;
  2295. case XML_CDATA_SECTION_NODE:
  2296. res = (const xmlChar *) "cdata";
  2297. break;
  2298. case XML_ENTITY_REF_NODE:
  2299. res = (const xmlChar *) "entity_ref";
  2300. break;
  2301. case XML_ENTITY_NODE:
  2302. res = (const xmlChar *) "entity";
  2303. break;
  2304. case XML_PI_NODE:
  2305. res = (const xmlChar *) "pi";
  2306. break;
  2307. case XML_COMMENT_NODE:
  2308. res = (const xmlChar *) "comment";
  2309. break;
  2310. case XML_DOCUMENT_NODE:
  2311. res = (const xmlChar *) "document_xml";
  2312. break;
  2313. case XML_DOCUMENT_TYPE_NODE:
  2314. res = (const xmlChar *) "doctype";
  2315. break;
  2316. case XML_DOCUMENT_FRAG_NODE:
  2317. res = (const xmlChar *) "fragment";
  2318. break;
  2319. case XML_NOTATION_NODE:
  2320. res = (const xmlChar *) "notation";
  2321. break;
  2322. case XML_HTML_DOCUMENT_NODE:
  2323. res = (const xmlChar *) "document_html";
  2324. break;
  2325. case XML_DTD_NODE:
  2326. res = (const xmlChar *) "dtd";
  2327. break;
  2328. case XML_ELEMENT_DECL:
  2329. res = (const xmlChar *) "elem_decl";
  2330. break;
  2331. case XML_ATTRIBUTE_DECL:
  2332. res = (const xmlChar *) "attribute_decl";
  2333. break;
  2334. case XML_ENTITY_DECL:
  2335. res = (const xmlChar *) "entity_decl";
  2336. break;
  2337. case XML_NAMESPACE_DECL:
  2338. res = (const xmlChar *) "namespace";
  2339. break;
  2340. case XML_XINCLUDE_START:
  2341. res = (const xmlChar *) "xinclude_start";
  2342. break;
  2343. case XML_XINCLUDE_END:
  2344. res = (const xmlChar *) "xinclude_end";
  2345. break;
  2346. #ifdef LIBXML_DOCB_ENABLED
  2347. case XML_DOCB_DOCUMENT_NODE:
  2348. res = (const xmlChar *) "document_docbook";
  2349. break;
  2350. #endif
  2351. }
  2352. #ifdef DEBUG
  2353. printf("libxml_type: cur = %p: %s\n", cur, res);
  2354. #endif
  2355. resultobj = libxml_constxmlCharPtrWrap(res);
  2356. return resultobj;
  2357. }
  2358. /************************************************************************
  2359. * *
  2360. * Specific accessor functions *
  2361. * *
  2362. ************************************************************************/
  2363. PyObject *
  2364. libxml_xmlNodeGetNsDefs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  2365. {
  2366. PyObject *py_retval;
  2367. xmlNsPtr c_retval;
  2368. xmlNodePtr node;
  2369. PyObject *pyobj_node;
  2370. if (!PyArg_ParseTuple
  2371. (args, (char *) "O:xmlNodeGetNsDefs", &pyobj_node))
  2372. return (NULL);
  2373. node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
  2374. if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
  2375. Py_INCREF(Py_None);
  2376. return (Py_None);
  2377. }
  2378. c_retval = node->nsDef;
  2379. py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
  2380. return (py_retval);
  2381. }
  2382. PyObject *
  2383. libxml_xmlNodeRemoveNsDef(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  2384. {
  2385. PyObject *py_retval;
  2386. xmlNsPtr ns, prev;
  2387. xmlNodePtr node;
  2388. PyObject *pyobj_node;
  2389. xmlChar *href;
  2390. xmlNsPtr c_retval;
  2391. if (!PyArg_ParseTuple
  2392. (args, (char *) "Oz:xmlNodeRemoveNsDef", &pyobj_node, &href))
  2393. return (NULL);
  2394. node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
  2395. ns = NULL;
  2396. if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
  2397. Py_INCREF(Py_None);
  2398. return (Py_None);
  2399. }
  2400. if (href == NULL) {
  2401. ns = node->nsDef;
  2402. node->nsDef = NULL;
  2403. c_retval = 0;
  2404. }
  2405. else {
  2406. prev = NULL;
  2407. ns = node->nsDef;
  2408. while (ns != NULL) {
  2409. if (xmlStrEqual(ns->href, href)) {
  2410. if (prev != NULL)
  2411. prev->next = ns->next;
  2412. else
  2413. node->nsDef = ns->next;
  2414. ns->next = NULL;
  2415. c_retval = 0;
  2416. break;
  2417. }
  2418. prev = ns;
  2419. ns = ns->next;
  2420. }
  2421. }
  2422. c_retval = ns;
  2423. py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
  2424. return (py_retval);
  2425. }
  2426. PyObject *
  2427. libxml_xmlNodeGetNs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  2428. {
  2429. PyObject *py_retval;
  2430. xmlNsPtr c_retval;
  2431. xmlNodePtr node;
  2432. PyObject *pyobj_node;
  2433. if (!PyArg_ParseTuple(args, (char *) "O:xmlNodeGetNs", &pyobj_node))
  2434. return (NULL);
  2435. node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
  2436. if ((node == NULL) ||
  2437. ((node->type != XML_ELEMENT_NODE) &&
  2438. (node->type != XML_ATTRIBUTE_NODE))) {
  2439. Py_INCREF(Py_None);
  2440. return (Py_None);
  2441. }
  2442. c_retval = node->ns;
  2443. py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
  2444. return (py_retval);
  2445. }
  2446. #ifdef LIBXML_OUTPUT_ENABLED
  2447. /************************************************************************
  2448. * *
  2449. * Serialization front-end *
  2450. * *
  2451. ************************************************************************/
  2452. static PyObject *
  2453. libxml_serializeNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  2454. {
  2455. PyObject *py_retval = NULL;
  2456. xmlChar *c_retval;
  2457. PyObject *pyobj_node;
  2458. xmlNodePtr node;
  2459. xmlDocPtr doc;
  2460. const char *encoding;
  2461. int format;
  2462. int len;
  2463. xmlSaveCtxtPtr ctxt;
  2464. xmlBufferPtr buf;
  2465. int options = 0;
  2466. if (!PyArg_ParseTuple(args, (char *) "Ozi:serializeNode", &pyobj_node,
  2467. &encoding, &format))
  2468. return (NULL);
  2469. node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
  2470. if (node == NULL) {
  2471. Py_INCREF(Py_None);
  2472. return (Py_None);
  2473. }
  2474. if (node->type == XML_DOCUMENT_NODE) {
  2475. doc = (xmlDocPtr) node;
  2476. node = NULL;
  2477. #ifdef LIBXML_HTML_ENABLED
  2478. } else if (node->type == XML_HTML_DOCUMENT_NODE) {
  2479. doc = (xmlDocPtr) node;
  2480. node = NULL;
  2481. #endif
  2482. } else {
  2483. if (node->type == XML_NAMESPACE_DECL)
  2484. doc = NULL;
  2485. else
  2486. doc = node->doc;
  2487. if ((doc == NULL) || (doc->type == XML_DOCUMENT_NODE)) {
  2488. #ifdef LIBXML_HTML_ENABLED
  2489. } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
  2490. #endif /* LIBXML_HTML_ENABLED */
  2491. } else {
  2492. Py_INCREF(Py_None);
  2493. return (Py_None);
  2494. }
  2495. }
  2496. buf = xmlBufferCreate();
  2497. if (buf == NULL) {
  2498. Py_INCREF(Py_None);
  2499. return (Py_None);
  2500. }
  2501. if (format) options |= XML_SAVE_FORMAT;
  2502. ctxt = xmlSaveToBuffer(buf, encoding, options);
  2503. if (ctxt == NULL) {
  2504. xmlBufferFree(buf);
  2505. Py_INCREF(Py_None);
  2506. return (Py_None);
  2507. }
  2508. if (node == NULL)
  2509. xmlSaveDoc(ctxt, doc);
  2510. else
  2511. xmlSaveTree(ctxt, node);
  2512. xmlSaveClose(ctxt);
  2513. c_retval = buf->content;
  2514. buf->content = NULL;
  2515. xmlBufferFree(buf);
  2516. py_retval = libxml_charPtrWrap((char *) c_retval);
  2517. return (py_retval);
  2518. }
  2519. static PyObject *
  2520. libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  2521. {
  2522. PyObject *py_file = NULL;
  2523. FILE *output;
  2524. PyObject *pyobj_node;
  2525. xmlNodePtr node;
  2526. xmlDocPtr doc;
  2527. const char *encoding;
  2528. int format;
  2529. int len;
  2530. xmlOutputBufferPtr buf;
  2531. xmlCharEncodingHandlerPtr handler = NULL;
  2532. if (!PyArg_ParseTuple(args, (char *) "OOzi:serializeNode", &pyobj_node,
  2533. &py_file, &encoding, &format))
  2534. return (NULL);
  2535. node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
  2536. if (node == NULL) {
  2537. return (PyInt_FromLong((long) -1));
  2538. }
  2539. if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
  2540. return (PyInt_FromLong((long) -1));
  2541. }
  2542. output = PyFile_AsFile(py_file);
  2543. if (output == NULL) {
  2544. return (PyInt_FromLong((long) -1));
  2545. }
  2546. if (node->type == XML_DOCUMENT_NODE) {
  2547. doc = (xmlDocPtr) node;
  2548. } else if (node->type == XML_HTML_DOCUMENT_NODE) {
  2549. doc = (xmlDocPtr) node;
  2550. } else {
  2551. doc = node->doc;
  2552. }
  2553. #ifdef LIBXML_HTML_ENABLED
  2554. if (doc->type == XML_HTML_DOCUMENT_NODE) {
  2555. if (encoding == NULL)
  2556. encoding = (const char *) htmlGetMetaEncoding(doc);
  2557. }
  2558. #endif /* LIBXML_HTML_ENABLED */
  2559. if (encoding != NULL) {
  2560. handler = xmlFindCharEncodingHandler(encoding);
  2561. if (handler == NULL) {
  2562. return (PyInt_FromLong((long) -1));
  2563. }
  2564. }
  2565. if (doc->type == XML_HTML_DOCUMENT_NODE) {
  2566. if (handler == NULL)
  2567. handler = xmlFindCharEncodingHandler("HTML");
  2568. if (handler == NULL)
  2569. handler = xmlFindCharEncodingHandler("ascii");
  2570. }
  2571. buf = xmlOutputBufferCreateFile(output, handler);
  2572. if (node->type == XML_DOCUMENT_NODE) {
  2573. len = xmlSaveFormatFileTo(buf, doc, encoding, format);
  2574. #ifdef LIBXML_HTML_ENABLED
  2575. } else if (node->type == XML_HTML_DOCUMENT_NODE) {
  2576. htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
  2577. len = xmlOutputBufferClose(buf);
  2578. } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
  2579. htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
  2580. len = xmlOutputBufferClose(buf);
  2581. #endif /* LIBXML_HTML_ENABLED */
  2582. } else {
  2583. xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
  2584. len = xmlOutputBufferClose(buf);
  2585. }
  2586. return (PyInt_FromLong((long) len));
  2587. }
  2588. #endif /* LIBXML_OUTPUT_ENABLED */
  2589. /************************************************************************
  2590. * *
  2591. * Extra stuff *
  2592. * *
  2593. ************************************************************************/
  2594. PyObject *
  2595. libxml_xmlNewNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  2596. {
  2597. PyObject *py_retval;
  2598. xmlChar *name;
  2599. xmlNodePtr node;
  2600. if (!PyArg_ParseTuple(args, (char *) "s:xmlNewNode", &name))
  2601. return (NULL);
  2602. node = (xmlNodePtr) xmlNewNode(NULL, name);
  2603. #ifdef DEBUG
  2604. printf("NewNode: %s : %p\n", name, (void *) node);
  2605. #endif
  2606. if (node == NULL) {
  2607. Py_INCREF(Py_None);
  2608. return (Py_None);
  2609. }
  2610. py_retval = libxml_xmlNodePtrWrap(node);
  2611. return (py_retval);
  2612. }
  2613. /************************************************************************
  2614. * *
  2615. * Local Catalog stuff *
  2616. * *
  2617. ************************************************************************/
  2618. static PyObject *
  2619. libxml_addLocalCatalog(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  2620. {
  2621. xmlChar *URL;
  2622. xmlParserCtxtPtr ctxt;
  2623. PyObject *pyobj_ctxt;
  2624. if (!PyArg_ParseTuple(args, (char *)"Os:addLocalCatalog", &pyobj_ctxt, &URL))
  2625. return(NULL);
  2626. ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
  2627. if (URL != NULL) {
  2628. ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL);
  2629. }
  2630. #ifdef DEBUG
  2631. printf("LocalCatalog: %s\n", URL);
  2632. #endif
  2633. Py_INCREF(Py_None);
  2634. return (Py_None);
  2635. }
  2636. #ifdef LIBXML_SCHEMAS_ENABLED
  2637. /************************************************************************
  2638. * *
  2639. * RelaxNG error handler registration *
  2640. * *
  2641. ************************************************************************/
  2642. typedef struct
  2643. {
  2644. PyObject *warn;
  2645. PyObject *error;
  2646. PyObject *arg;
  2647. } xmlRelaxNGValidCtxtPyCtxt;
  2648. typedef xmlRelaxNGValidCtxtPyCtxt *xmlRelaxNGValidCtxtPyCtxtPtr;
  2649. static void
  2650. libxml_xmlRelaxNGValidityGenericErrorFuncHandler(void *ctx, char *str)
  2651. {
  2652. PyObject *list;
  2653. PyObject *result;
  2654. xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
  2655. #ifdef DEBUG_ERROR
  2656. printf("libxml_xmlRelaxNGValidityGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
  2657. #endif
  2658. pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx;
  2659. list = PyTuple_New(2);
  2660. PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
  2661. PyTuple_SetItem(list, 1, pyCtxt->arg);
  2662. Py_XINCREF(pyCtxt->arg);
  2663. result = PyEval_CallObject(pyCtxt->error, list);
  2664. if (result == NULL)
  2665. {
  2666. /* TODO: manage for the exception to be propagated... */
  2667. PyErr_Print();
  2668. }
  2669. Py_XDECREF(list);
  2670. Py_XDECREF(result);
  2671. }
  2672. static void
  2673. libxml_xmlRelaxNGValidityGenericWarningFuncHandler(void *ctx, char *str)
  2674. {
  2675. PyObject *list;
  2676. PyObject *result;
  2677. xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
  2678. #ifdef DEBUG_ERROR
  2679. printf("libxml_xmlRelaxNGValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str);
  2680. #endif
  2681. pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx;
  2682. list = PyTuple_New(2);
  2683. PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
  2684. PyTuple_SetItem(list, 1, pyCtxt->arg);
  2685. Py_XINCREF(pyCtxt->arg);
  2686. result = PyEval_CallObject(pyCtxt->warn, list);
  2687. if (result == NULL)
  2688. {
  2689. /* TODO: manage for the exception to be propagated... */
  2690. PyErr_Print();
  2691. }
  2692. Py_XDECREF(list);
  2693. Py_XDECREF(result);
  2694. }
  2695. static void
  2696. libxml_xmlRelaxNGValidityErrorFunc(void *ctx, const char *msg, ...)
  2697. {
  2698. va_list ap;
  2699. va_start(ap, msg);
  2700. libxml_xmlRelaxNGValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap));
  2701. va_end(ap);
  2702. }
  2703. static void
  2704. libxml_xmlRelaxNGValidityWarningFunc(void *ctx, const char *msg, ...)
  2705. {
  2706. va_list ap;
  2707. va_start(ap, msg);
  2708. libxml_xmlRelaxNGValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap));
  2709. va_end(ap);
  2710. }
  2711. static PyObject *
  2712. libxml_xmlRelaxNGSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  2713. {
  2714. PyObject *py_retval;
  2715. PyObject *pyobj_error;
  2716. PyObject *pyobj_warn;
  2717. PyObject *pyobj_ctx;
  2718. PyObject *pyobj_arg = Py_None;
  2719. xmlRelaxNGValidCtxtPtr ctxt;
  2720. xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
  2721. if (!PyArg_ParseTuple
  2722. (args, (char *) "OOO|O:xmlRelaxNGSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
  2723. return (NULL);
  2724. #ifdef DEBUG_ERROR
  2725. printf("libxml_xmlRelaxNGSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
  2726. #endif
  2727. ctxt = PyrelaxNgValidCtxt_Get(pyobj_ctx);
  2728. if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1)
  2729. {
  2730. py_retval = libxml_intWrap(-1);
  2731. return(py_retval);
  2732. }
  2733. if (pyCtxt == NULL)
  2734. {
  2735. /* first time to set the error handlers */
  2736. pyCtxt = xmlMalloc(sizeof(xmlRelaxNGValidCtxtPyCtxt));
  2737. if (pyCtxt == NULL) {
  2738. py_retval = libxml_intWrap(-1);
  2739. return(py_retval);
  2740. }
  2741. memset(pyCtxt, 0, sizeof(xmlRelaxNGValidCtxtPyCtxt));
  2742. }
  2743. /* TODO: check warn and error is a function ! */
  2744. Py_XDECREF(pyCtxt->error);
  2745. Py_XINCREF(pyobj_error);
  2746. pyCtxt->error = pyobj_error;
  2747. Py_XDECREF(pyCtxt->warn);
  2748. Py_XINCREF(pyobj_warn);
  2749. pyCtxt->warn = pyobj_warn;
  2750. Py_XDECREF(pyCtxt->arg);
  2751. Py_XINCREF(pyobj_arg);
  2752. pyCtxt->arg = pyobj_arg;
  2753. xmlRelaxNGSetValidErrors(ctxt, &libxml_xmlRelaxNGValidityErrorFunc, &libxml_xmlRelaxNGValidityWarningFunc, pyCtxt);
  2754. py_retval = libxml_intWrap(1);
  2755. return (py_retval);
  2756. }
  2757. static PyObject *
  2758. libxml_xmlRelaxNGFreeValidCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
  2759. xmlRelaxNGValidCtxtPtr ctxt;
  2760. xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
  2761. PyObject *pyobj_ctxt;
  2762. if (!PyArg_ParseTuple(args, (char *)"O:xmlRelaxNGFreeValidCtxt", &pyobj_ctxt))
  2763. return(NULL);
  2764. ctxt = (xmlRelaxNGValidCtxtPtr) PyrelaxNgValidCtxt_Get(pyobj_ctxt);
  2765. if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0)
  2766. {
  2767. if (pyCtxt != NULL)
  2768. {
  2769. Py_XDECREF(pyCtxt->error);
  2770. Py_XDECREF(pyCtxt->warn);
  2771. Py_XDECREF(pyCtxt->arg);
  2772. xmlFree(pyCtxt);
  2773. }
  2774. }
  2775. xmlRelaxNGFreeValidCtxt(ctxt);
  2776. Py_INCREF(Py_None);
  2777. return(Py_None);
  2778. }
  2779. typedef struct
  2780. {
  2781. PyObject *warn;
  2782. PyObject *error;
  2783. PyObject *arg;
  2784. } xmlSchemaValidCtxtPyCtxt;
  2785. typedef xmlSchemaValidCtxtPyCtxt *xmlSchemaValidCtxtPyCtxtPtr;
  2786. static void
  2787. libxml_xmlSchemaValidityGenericErrorFuncHandler(void *ctx, char *str)
  2788. {
  2789. PyObject *list;
  2790. PyObject *result;
  2791. xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
  2792. #ifdef DEBUG_ERROR
  2793. printf("libxml_xmlSchemaValiditiyGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
  2794. #endif
  2795. pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx;
  2796. list = PyTuple_New(2);
  2797. PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
  2798. PyTuple_SetItem(list, 1, pyCtxt->arg);
  2799. Py_XINCREF(pyCtxt->arg);
  2800. result = PyEval_CallObject(pyCtxt->error, list);
  2801. if (result == NULL)
  2802. {
  2803. /* TODO: manage for the exception to be propagated... */
  2804. PyErr_Print();
  2805. }
  2806. Py_XDECREF(list);
  2807. Py_XDECREF(result);
  2808. }
  2809. static void
  2810. libxml_xmlSchemaValidityGenericWarningFuncHandler(void *ctx, char *str)
  2811. {
  2812. PyObject *list;
  2813. PyObject *result;
  2814. xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
  2815. #ifdef DEBUG_ERROR
  2816. printf("libxml_xmlSchemaValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str);
  2817. #endif
  2818. pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx;
  2819. list = PyTuple_New(2);
  2820. PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
  2821. PyTuple_SetItem(list, 1, pyCtxt->arg);
  2822. Py_XINCREF(pyCtxt->arg);
  2823. result = PyEval_CallObject(pyCtxt->warn, list);
  2824. if (result == NULL)
  2825. {
  2826. /* TODO: manage for the exception to be propagated... */
  2827. PyErr_Print();
  2828. }
  2829. Py_XDECREF(list);
  2830. Py_XDECREF(result);
  2831. }
  2832. static void
  2833. libxml_xmlSchemaValidityErrorFunc(void *ctx, const char *msg, ...)
  2834. {
  2835. va_list ap;
  2836. va_start(ap, msg);
  2837. libxml_xmlSchemaValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap));
  2838. va_end(ap);
  2839. }
  2840. static void
  2841. libxml_xmlSchemaValidityWarningFunc(void *ctx, const char *msg, ...)
  2842. {
  2843. va_list ap;
  2844. va_start(ap, msg);
  2845. libxml_xmlSchemaValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap));
  2846. va_end(ap);
  2847. }
  2848. PyObject *
  2849. libxml_xmlSchemaSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  2850. {
  2851. PyObject *py_retval;
  2852. PyObject *pyobj_error;
  2853. PyObject *pyobj_warn;
  2854. PyObject *pyobj_ctx;
  2855. PyObject *pyobj_arg = Py_None;
  2856. xmlSchemaValidCtxtPtr ctxt;
  2857. xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
  2858. if (!PyArg_ParseTuple
  2859. (args, (char *) "OOO|O:xmlSchemaSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
  2860. return (NULL);
  2861. #ifdef DEBUG_ERROR
  2862. printf("libxml_xmlSchemaSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
  2863. #endif
  2864. ctxt = PySchemaValidCtxt_Get(pyobj_ctx);
  2865. if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1)
  2866. {
  2867. py_retval = libxml_intWrap(-1);
  2868. return(py_retval);
  2869. }
  2870. if (pyCtxt == NULL)
  2871. {
  2872. /* first time to set the error handlers */
  2873. pyCtxt = xmlMalloc(sizeof(xmlSchemaValidCtxtPyCtxt));
  2874. if (pyCtxt == NULL) {
  2875. py_retval = libxml_intWrap(-1);
  2876. return(py_retval);
  2877. }
  2878. memset(pyCtxt, 0, sizeof(xmlSchemaValidCtxtPyCtxt));
  2879. }
  2880. /* TODO: check warn and error is a function ! */
  2881. Py_XDECREF(pyCtxt->error);
  2882. Py_XINCREF(pyobj_error);
  2883. pyCtxt->error = pyobj_error;
  2884. Py_XDECREF(pyCtxt->warn);
  2885. Py_XINCREF(pyobj_warn);
  2886. pyCtxt->warn = pyobj_warn;
  2887. Py_XDECREF(pyCtxt->arg);
  2888. Py_XINCREF(pyobj_arg);
  2889. pyCtxt->arg = pyobj_arg;
  2890. xmlSchemaSetValidErrors(ctxt, &libxml_xmlSchemaValidityErrorFunc, &libxml_xmlSchemaValidityWarningFunc, pyCtxt);
  2891. py_retval = libxml_intWrap(1);
  2892. return(py_retval);
  2893. }
  2894. static PyObject *
  2895. libxml_xmlSchemaFreeValidCtxt(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
  2896. {
  2897. xmlSchemaValidCtxtPtr ctxt;
  2898. xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
  2899. PyObject *pyobj_ctxt;
  2900. if (!PyArg_ParseTuple(args, (char *)"O:xmlSchemaFreeValidCtxt", &pyobj_ctxt))
  2901. return(NULL);
  2902. ctxt = (xmlSchemaValidCtxtPtr) PySchemaValidCtxt_Get(pyobj_ctxt);
  2903. if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0)
  2904. {
  2905. if (pyCtxt != NULL)
  2906. {
  2907. Py_XDECREF(pyCtxt->error);
  2908. Py_XDECREF(pyCtxt->warn);
  2909. Py_XDECREF(pyCtxt->arg);
  2910. xmlFree(pyCtxt);
  2911. }
  2912. }
  2913. xmlSchemaFreeValidCtxt(ctxt);
  2914. Py_INCREF(Py_None);
  2915. return(Py_None);
  2916. }
  2917. #endif
  2918. #ifdef LIBXML_C14N_ENABLED
  2919. #ifdef LIBXML_OUTPUT_ENABLED
  2920. /************************************************************************
  2921. * *
  2922. * XML Canonicalization c14n *
  2923. * *
  2924. ************************************************************************/
  2925. static int
  2926. PyxmlNodeSet_Convert(PyObject *py_nodeset, xmlNodeSetPtr *result)
  2927. {
  2928. xmlNodeSetPtr nodeSet;
  2929. int is_tuple = 0;
  2930. if (PyTuple_Check(py_nodeset))
  2931. is_tuple = 1;
  2932. else if (PyList_Check(py_nodeset))
  2933. is_tuple = 0;
  2934. else if (py_nodeset == Py_None) {
  2935. *result = NULL;
  2936. return 0;
  2937. }
  2938. else {
  2939. PyErr_SetString(PyExc_TypeError,
  2940. "must be a tuple or list of nodes.");
  2941. return -1;
  2942. }
  2943. nodeSet = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet));
  2944. if (nodeSet == NULL) {
  2945. PyErr_SetString(PyExc_MemoryError, "");
  2946. return -1;
  2947. }
  2948. nodeSet->nodeNr = 0;
  2949. nodeSet->nodeMax = (is_tuple
  2950. ? PyTuple_GET_SIZE(py_nodeset)
  2951. : PyList_GET_SIZE(py_nodeset));
  2952. nodeSet->nodeTab
  2953. = (xmlNodePtr *) xmlMalloc (nodeSet->nodeMax
  2954. * sizeof(xmlNodePtr));
  2955. if (nodeSet->nodeTab == NULL) {
  2956. xmlFree(nodeSet);
  2957. PyErr_SetString(PyExc_MemoryError, "");
  2958. return -1;
  2959. }
  2960. memset(nodeSet->nodeTab, 0 ,
  2961. nodeSet->nodeMax * sizeof(xmlNodePtr));
  2962. {
  2963. int idx;
  2964. for (idx=0; idx < nodeSet->nodeMax; ++idx) {
  2965. xmlNodePtr pynode =
  2966. PyxmlNode_Get (is_tuple
  2967. ? PyTuple_GET_ITEM(py_nodeset, idx)
  2968. : PyList_GET_ITEM(py_nodeset, idx));
  2969. if (pynode)
  2970. nodeSet->nodeTab[nodeSet->nodeNr++] = pynode;
  2971. }
  2972. }
  2973. *result = nodeSet;
  2974. return 0;
  2975. }
  2976. static int
  2977. PystringSet_Convert(PyObject *py_strings, xmlChar *** result)
  2978. {
  2979. /* NOTE: the array should be freed, but the strings are shared
  2980. with the python strings and so must not be freed. */
  2981. xmlChar ** strings;
  2982. int is_tuple = 0;
  2983. int count;
  2984. int init_index = 0;
  2985. if (PyTuple_Check(py_strings))
  2986. is_tuple = 1;
  2987. else if (PyList_Check(py_strings))
  2988. is_tuple = 0;
  2989. else if (py_strings == Py_None) {
  2990. *result = NULL;
  2991. return 0;
  2992. }
  2993. else {
  2994. PyErr_SetString(PyExc_TypeError,
  2995. "must be a tuple or list of strings.");
  2996. return -1;
  2997. }
  2998. count = (is_tuple
  2999. ? PyTuple_GET_SIZE(py_strings)
  3000. : PyList_GET_SIZE(py_strings));
  3001. strings = (xmlChar **) xmlMalloc(sizeof(xmlChar *) * count);
  3002. if (strings == NULL) {
  3003. PyErr_SetString(PyExc_MemoryError, "");
  3004. return -1;
  3005. }
  3006. memset(strings, 0 , sizeof(xmlChar *) * count);
  3007. {
  3008. int idx;
  3009. for (idx=0; idx < count; ++idx) {
  3010. char* s = PyString_AsString
  3011. (is_tuple
  3012. ? PyTuple_GET_ITEM(py_strings, idx)
  3013. : PyList_GET_ITEM(py_strings, idx));
  3014. if (s)
  3015. strings[init_index++] = (xmlChar *)s;
  3016. else {
  3017. xmlFree(strings);
  3018. PyErr_SetString(PyExc_TypeError,
  3019. "must be a tuple or list of strings.");
  3020. return -1;
  3021. }
  3022. }
  3023. }
  3024. *result = strings;
  3025. return 0;
  3026. }
  3027. static PyObject *
  3028. libxml_C14NDocDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
  3029. PyObject * args)
  3030. {
  3031. PyObject *py_retval = NULL;
  3032. PyObject *pyobj_doc;
  3033. PyObject *pyobj_nodes;
  3034. int exclusive;
  3035. PyObject *pyobj_prefixes;
  3036. int with_comments;
  3037. xmlDocPtr doc;
  3038. xmlNodeSetPtr nodes;
  3039. xmlChar **prefixes = NULL;
  3040. xmlChar *doc_txt;
  3041. int result;
  3042. if (!PyArg_ParseTuple(args, (char *) "OOiOi:C14NDocDumpMemory",
  3043. &pyobj_doc,
  3044. &pyobj_nodes,
  3045. &exclusive,
  3046. &pyobj_prefixes,
  3047. &with_comments))
  3048. return (NULL);
  3049. doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
  3050. if (!doc) {
  3051. PyErr_SetString(PyExc_TypeError, "bad document.");
  3052. return NULL;
  3053. }
  3054. result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes);
  3055. if (result < 0) return NULL;
  3056. if (exclusive) {
  3057. result = PystringSet_Convert(pyobj_prefixes, &prefixes);
  3058. if (result < 0) {
  3059. if (nodes) {
  3060. xmlFree(nodes->nodeTab);
  3061. xmlFree(nodes);
  3062. }
  3063. return NULL;
  3064. }
  3065. }
  3066. result = xmlC14NDocDumpMemory(doc,
  3067. nodes,
  3068. exclusive,
  3069. prefixes,
  3070. with_comments,
  3071. &doc_txt);
  3072. if (nodes) {
  3073. xmlFree(nodes->nodeTab);
  3074. xmlFree(nodes);
  3075. }
  3076. if (prefixes) {
  3077. xmlChar ** idx = prefixes;
  3078. while (*idx) xmlFree(*(idx++));
  3079. xmlFree(prefixes);
  3080. }
  3081. if (result < 0) {
  3082. PyErr_SetString(PyExc_Exception,
  3083. "libxml2 xmlC14NDocDumpMemory failure.");
  3084. return NULL;
  3085. }
  3086. else {
  3087. py_retval = PyString_FromStringAndSize((const char *) doc_txt,
  3088. result);
  3089. xmlFree(doc_txt);
  3090. return py_retval;
  3091. }
  3092. }
  3093. static PyObject *
  3094. libxml_C14NDocSaveTo(ATTRIBUTE_UNUSED PyObject * self,
  3095. PyObject * args)
  3096. {
  3097. PyObject *pyobj_doc;
  3098. PyObject *py_file;
  3099. PyObject *pyobj_nodes;
  3100. int exclusive;
  3101. PyObject *pyobj_prefixes;
  3102. int with_comments;
  3103. xmlDocPtr doc;
  3104. xmlNodeSetPtr nodes;
  3105. xmlChar **prefixes = NULL;
  3106. FILE * output;
  3107. xmlOutputBufferPtr buf;
  3108. int result;
  3109. int len;
  3110. if (!PyArg_ParseTuple(args, (char *) "OOiOiO:C14NDocSaveTo",
  3111. &pyobj_doc,
  3112. &pyobj_nodes,
  3113. &exclusive,
  3114. &pyobj_prefixes,
  3115. &with_comments,
  3116. &py_file))
  3117. return (NULL);
  3118. doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
  3119. if (!doc) {
  3120. PyErr_SetString(PyExc_TypeError, "bad document.");
  3121. return NULL;
  3122. }
  3123. if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
  3124. PyErr_SetString(PyExc_TypeError, "bad file.");
  3125. return NULL;
  3126. }
  3127. output = PyFile_AsFile(py_file);
  3128. if (output == NULL) {
  3129. PyErr_SetString(PyExc_TypeError, "bad file.");
  3130. return NULL;
  3131. }
  3132. buf = xmlOutputBufferCreateFile(output, NULL);
  3133. result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes);
  3134. if (result < 0) return NULL;
  3135. if (exclusive) {
  3136. result = PystringSet_Convert(pyobj_prefixes, &prefixes);
  3137. if (result < 0) {
  3138. if (nodes) {
  3139. xmlFree(nodes->nodeTab);
  3140. xmlFree(nodes);
  3141. }
  3142. return NULL;
  3143. }
  3144. }
  3145. result = xmlC14NDocSaveTo(doc,
  3146. nodes,
  3147. exclusive,
  3148. prefixes,
  3149. with_comments,
  3150. buf);
  3151. if (nodes) {
  3152. xmlFree(nodes->nodeTab);
  3153. xmlFree(nodes);
  3154. }
  3155. if (prefixes) {
  3156. xmlChar ** idx = prefixes;
  3157. while (*idx) xmlFree(*(idx++));
  3158. xmlFree(prefixes);
  3159. }
  3160. len = xmlOutputBufferClose(buf);
  3161. if (result < 0) {
  3162. PyErr_SetString(PyExc_Exception,
  3163. "libxml2 xmlC14NDocSaveTo failure.");
  3164. return NULL;
  3165. }
  3166. else
  3167. return PyInt_FromLong((long) len);
  3168. }
  3169. #endif
  3170. #endif
  3171. static PyObject *
  3172. libxml_getObjDesc(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
  3173. PyObject *obj;
  3174. char *str;
  3175. if (!PyArg_ParseTuple(args, (char *)"O:getObjDesc", &obj))
  3176. return NULL;
  3177. str = PyCObject_GetDesc(obj);
  3178. return Py_BuildValue((char *)"s", str);
  3179. }
  3180. static PyObject *
  3181. libxml_compareNodesEqual(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
  3182. PyObject *py_node1, *py_node2;
  3183. xmlNodePtr node1, node2;
  3184. if (!PyArg_ParseTuple(args, (char *)"OO:compareNodesEqual",
  3185. &py_node1, &py_node2))
  3186. return NULL;
  3187. /* To compare two node objects, we compare their pointer addresses */
  3188. node1 = PyxmlNode_Get(py_node1);
  3189. node2 = PyxmlNode_Get(py_node2);
  3190. if ( node1 == node2 )
  3191. return Py_BuildValue((char *)"i", 1);
  3192. else
  3193. return Py_BuildValue((char *)"i", 0);
  3194. }
  3195. static PyObject *
  3196. libxml_nodeHash(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
  3197. PyObject *py_node1;
  3198. xmlNodePtr node1;
  3199. if (!PyArg_ParseTuple(args, (char *)"O:nodeHash", &py_node1))
  3200. return NULL;
  3201. /* For simplicity, we use the node pointer address as a hash value */
  3202. node1 = PyxmlNode_Get(py_node1);
  3203. return PyLong_FromVoidPtr(node1);
  3204. }
  3205. /************************************************************************
  3206. * *
  3207. * The registration stuff *
  3208. * *
  3209. ************************************************************************/
  3210. static PyMethodDef libxmlMethods[] = {
  3211. #include "libxml2-export.c"
  3212. {(char *) "name", libxml_name, METH_VARARGS, NULL},
  3213. {(char *) "children", libxml_children, METH_VARARGS, NULL},
  3214. {(char *) "properties", libxml_properties, METH_VARARGS, NULL},
  3215. {(char *) "last", libxml_last, METH_VARARGS, NULL},
  3216. {(char *) "prev", libxml_prev, METH_VARARGS, NULL},
  3217. {(char *) "next", libxml_next, METH_VARARGS, NULL},
  3218. {(char *) "parent", libxml_parent, METH_VARARGS, NULL},
  3219. {(char *) "type", libxml_type, METH_VARARGS, NULL},
  3220. {(char *) "doc", libxml_doc, METH_VARARGS, NULL},
  3221. {(char *) "xmlNewNode", libxml_xmlNewNode, METH_VARARGS, NULL},
  3222. {(char *) "xmlNodeRemoveNsDef", libxml_xmlNodeRemoveNsDef, METH_VARARGS, NULL},
  3223. {(char *)"xmlSetValidErrors", libxml_xmlSetValidErrors, METH_VARARGS, NULL},
  3224. {(char *)"xmlFreeValidCtxt", libxml_xmlFreeValidCtxt, METH_VARARGS, NULL},
  3225. #ifdef LIBXML_OUTPUT_ENABLED
  3226. {(char *) "serializeNode", libxml_serializeNode, METH_VARARGS, NULL},
  3227. {(char *) "saveNodeTo", libxml_saveNodeTo, METH_VARARGS, NULL},
  3228. {(char *) "outputBufferCreate", libxml_xmlCreateOutputBuffer, METH_VARARGS, NULL},
  3229. {(char *) "outputBufferGetPythonFile", libxml_outputBufferGetPythonFile, METH_VARARGS, NULL},
  3230. {(char *) "xmlOutputBufferClose", libxml_xmlOutputBufferClose, METH_VARARGS, NULL},
  3231. { (char *)"xmlOutputBufferFlush", libxml_xmlOutputBufferFlush, METH_VARARGS, NULL },
  3232. { (char *)"xmlSaveFileTo", libxml_xmlSaveFileTo, METH_VARARGS, NULL },
  3233. { (char *)"xmlSaveFormatFileTo", libxml_xmlSaveFormatFileTo, METH_VARARGS, NULL },
  3234. #endif /* LIBXML_OUTPUT_ENABLED */
  3235. {(char *) "inputBufferCreate", libxml_xmlCreateInputBuffer, METH_VARARGS, NULL},
  3236. {(char *) "setEntityLoader", libxml_xmlSetEntityLoader, METH_VARARGS, NULL},
  3237. {(char *)"xmlRegisterErrorHandler", libxml_xmlRegisterErrorHandler, METH_VARARGS, NULL },
  3238. {(char *)"xmlParserCtxtSetErrorHandler", libxml_xmlParserCtxtSetErrorHandler, METH_VARARGS, NULL },
  3239. {(char *)"xmlParserCtxtGetErrorHandler", libxml_xmlParserCtxtGetErrorHandler, METH_VARARGS, NULL },
  3240. {(char *)"xmlFreeParserCtxt", libxml_xmlFreeParserCtxt, METH_VARARGS, NULL },
  3241. #ifdef LIBXML_READER_ENABLED
  3242. {(char *)"xmlTextReaderSetErrorHandler", libxml_xmlTextReaderSetErrorHandler, METH_VARARGS, NULL },
  3243. {(char *)"xmlTextReaderGetErrorHandler", libxml_xmlTextReaderGetErrorHandler, METH_VARARGS, NULL },
  3244. {(char *)"xmlFreeTextReader", libxml_xmlFreeTextReader, METH_VARARGS, NULL },
  3245. #endif
  3246. {(char *)"addLocalCatalog", libxml_addLocalCatalog, METH_VARARGS, NULL },
  3247. #ifdef LIBXML_SCHEMAS_ENABLED
  3248. {(char *)"xmlRelaxNGSetValidErrors", libxml_xmlRelaxNGSetValidErrors, METH_VARARGS, NULL},
  3249. {(char *)"xmlRelaxNGFreeValidCtxt", libxml_xmlRelaxNGFreeValidCtxt, METH_VARARGS, NULL},
  3250. {(char *)"xmlSchemaSetValidErrors", libxml_xmlSchemaSetValidErrors, METH_VARARGS, NULL},
  3251. {(char *)"xmlSchemaFreeValidCtxt", libxml_xmlSchemaFreeValidCtxt, METH_VARARGS, NULL},
  3252. #endif
  3253. #ifdef LIBXML_C14N_ENABLED
  3254. #ifdef LIBXML_OUTPUT_ENABLED
  3255. {(char *)"xmlC14NDocDumpMemory", libxml_C14NDocDumpMemory, METH_VARARGS, NULL},
  3256. {(char *)"xmlC14NDocSaveTo", libxml_C14NDocSaveTo, METH_VARARGS, NULL},
  3257. #endif
  3258. #endif
  3259. {(char *) "getObjDesc", libxml_getObjDesc, METH_VARARGS, NULL},
  3260. {(char *) "compareNodesEqual", libxml_compareNodesEqual, METH_VARARGS, NULL},
  3261. {(char *) "nodeHash", libxml_nodeHash, METH_VARARGS, NULL},
  3262. {NULL, NULL, 0, NULL}
  3263. };
  3264. #ifdef MERGED_MODULES
  3265. extern void initlibxsltmod(void);
  3266. #endif
  3267. void
  3268. initlibxml2mod(void)
  3269. {
  3270. static int initialized = 0;
  3271. if (initialized != 0)
  3272. return;
  3273. /* intialize the python extension module */
  3274. Py_InitModule((char *) "libxml2mod", libxmlMethods);
  3275. /* initialize libxml2 */
  3276. xmlInitParser();
  3277. libxml_xmlErrorInitialize();
  3278. initialized = 1;
  3279. #ifdef MERGED_MODULES
  3280. initlibxsltmod();
  3281. #endif
  3282. }