1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724 |
- /*
- * libxml.c: this modules implements the main part of the glue of the
- * libxml2 library and the Python interpreter. It provides the
- * entry points where an automatically generated stub is either
- * unpractical or would not match cleanly the Python model.
- *
- * If compiled with MERGED_MODULES, the entry point will be used to
- * initialize both the libxml2 and the libxslt wrappers
- *
- * See Copyright for the status of this software.
- *
- * daniel@veillard.com
- */
- #include <Python.h>
- #include <fileobject.h>
- /* #include "config.h" */
- #include <libxml/xmlmemory.h>
- #include <libxml/parser.h>
- #include <libxml/tree.h>
- #include <libxml/xpath.h>
- #include <libxml/xmlerror.h>
- #include <libxml/xpathInternals.h>
- #include <libxml/xmlmemory.h>
- #include <libxml/xmlIO.h>
- #include <libxml/c14n.h>
- #include <libxml/xmlreader.h>
- #include <libxml/xmlsave.h>
- #include "libxml_wrap.h"
- #include "libxml2-py.h"
- #if (defined(_MSC_VER) || defined(__MINGW32__)) && !defined(vsnprintf)
- #define vsnprintf(b,c,f,a) _vsnprintf(b,c,f,a)
- #elif defined(WITH_TRIO)
- #include "trio.h"
- #define vsnprintf trio_vsnprintf
- #endif
- /* #define DEBUG */
- /* #define DEBUG_SAX */
- /* #define DEBUG_XPATH */
- /* #define DEBUG_ERROR */
- /* #define DEBUG_MEMORY */
- /* #define DEBUG_FILES */
- /* #define DEBUG_LOADER */
- void initlibxml2mod(void);
- /**
- * TODO:
- *
- * macro to flag unimplemented blocks
- */
- #define TODO \
- xmlGenericError(xmlGenericErrorContext, \
- "Unimplemented block at %s:%d\n", \
- __FILE__, __LINE__);
- /*
- * the following vars are used for XPath extensions, but
- * are also referenced within the parser cleanup routine.
- */
- static int libxml_xpathCallbacksInitialized = 0;
- typedef struct libxml_xpathCallback {
- xmlXPathContextPtr ctx;
- xmlChar *name;
- xmlChar *ns_uri;
- PyObject *function;
- } libxml_xpathCallback, *libxml_xpathCallbackPtr;
- typedef libxml_xpathCallback libxml_xpathCallbackArray[];
- static int libxml_xpathCallbacksAllocd = 10;
- static libxml_xpathCallbackArray *libxml_xpathCallbacks = NULL;
- static int libxml_xpathCallbacksNb = 0;
- /************************************************************************
- * *
- * Memory debug interface *
- * *
- ************************************************************************/
- #if 0
- extern void xmlMemFree(void *ptr);
- extern void *xmlMemMalloc(size_t size);
- extern void *xmlMemRealloc(void *ptr, size_t size);
- extern char *xmlMemoryStrdup(const char *str);
- #endif
- static int libxmlMemoryDebugActivated = 0;
- static long libxmlMemoryAllocatedBase = 0;
- static int libxmlMemoryDebug = 0;
- static xmlFreeFunc freeFunc = NULL;
- static xmlMallocFunc mallocFunc = NULL;
- static xmlReallocFunc reallocFunc = NULL;
- static xmlStrdupFunc strdupFunc = NULL;
- static void
- libxml_xmlErrorInitialize(void); /* forward declare */
- PyObject *
- libxml_xmlMemoryUsed(PyObject * self ATTRIBUTE_UNUSED,
- PyObject * args ATTRIBUTE_UNUSED)
- {
- long ret;
- PyObject *py_retval;
- ret = xmlMemUsed();
- py_retval = libxml_longWrap(ret);
- return (py_retval);
- }
- PyObject *
- libxml_xmlDebugMemory(PyObject * self ATTRIBUTE_UNUSED, PyObject * args)
- {
- int activate;
- PyObject *py_retval;
- long ret;
- if (!PyArg_ParseTuple(args, (char *) "i:xmlDebugMemory", &activate))
- return (NULL);
- #ifdef DEBUG_MEMORY
- printf("libxml_xmlDebugMemory(%d) called\n", activate);
- #endif
- if (activate != 0) {
- if (libxmlMemoryDebug == 0) {
- /*
- * First initialize the library and grab the old memory handlers
- * and switch the library to memory debugging
- */
- xmlMemGet((xmlFreeFunc *) & freeFunc,
- (xmlMallocFunc *) & mallocFunc,
- (xmlReallocFunc *) & reallocFunc,
- (xmlStrdupFunc *) & strdupFunc);
- if ((freeFunc == xmlMemFree) && (mallocFunc == xmlMemMalloc) &&
- (reallocFunc == xmlMemRealloc) &&
- (strdupFunc == xmlMemoryStrdup)) {
- libxmlMemoryAllocatedBase = xmlMemUsed();
- } else {
- /*
- * cleanup first, because some memory has been
- * allocated with the non-debug malloc in xmlInitParser
- * when the python module was imported
- */
- xmlCleanupParser();
- ret = (long) xmlMemSetup(xmlMemFree, xmlMemMalloc,
- xmlMemRealloc, xmlMemoryStrdup);
- if (ret < 0)
- goto error;
- libxmlMemoryAllocatedBase = xmlMemUsed();
- /* reinitialize */
- xmlInitParser();
- libxml_xmlErrorInitialize();
- }
- ret = 0;
- } else if (libxmlMemoryDebugActivated == 0) {
- libxmlMemoryAllocatedBase = xmlMemUsed();
- ret = 0;
- } else {
- ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
- }
- libxmlMemoryDebug = 1;
- libxmlMemoryDebugActivated = 1;
- } else {
- if (libxmlMemoryDebugActivated == 1)
- ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
- else
- ret = 0;
- libxmlMemoryDebugActivated = 0;
- }
- error:
- py_retval = libxml_longWrap(ret);
- return (py_retval);
- }
- PyObject *
- libxml_xmlPythonCleanupParser(PyObject *self ATTRIBUTE_UNUSED,
- PyObject *args ATTRIBUTE_UNUSED) {
- int ix;
- long freed = -1;
- if (libxmlMemoryDebug) {
- freed = xmlMemUsed();
- }
- xmlCleanupParser();
- /*
- * Need to confirm whether we really want to do this (required for
- * memcheck) in all cases...
- */
-
- if (libxml_xpathCallbacks != NULL) { /* if ext funcs declared */
- for (ix=0; ix<libxml_xpathCallbacksNb; ix++) {
- if ((*libxml_xpathCallbacks)[ix].name != NULL)
- xmlFree((*libxml_xpathCallbacks)[ix].name);
- if ((*libxml_xpathCallbacks)[ix].ns_uri != NULL)
- xmlFree((*libxml_xpathCallbacks)[ix].ns_uri);
- }
- libxml_xpathCallbacksNb = 0;
- xmlFree(libxml_xpathCallbacks);
- libxml_xpathCallbacks = NULL;
- }
- if (libxmlMemoryDebug) {
- freed -= xmlMemUsed();
- libxmlMemoryAllocatedBase -= freed;
- if (libxmlMemoryAllocatedBase < 0)
- libxmlMemoryAllocatedBase = 0;
- }
- Py_INCREF(Py_None);
- return(Py_None);
- }
- PyObject *
- libxml_xmlDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
- ATTRIBUTE_UNUSED PyObject * args)
- {
- if (libxmlMemoryDebug != 0)
- xmlMemoryDump();
- Py_INCREF(Py_None);
- return (Py_None);
- }
- /************************************************************************
- * *
- * Handling Python FILE I/O at the C level *
- * The raw I/O attack diectly the File objects, while the *
- * other routines address the ioWrapper instance instead *
- * *
- ************************************************************************/
- /**
- * xmlPythonFileCloseUnref:
- * @context: the I/O context
- *
- * Close an I/O channel
- */
- static int
- xmlPythonFileCloseRaw (void * context) {
- PyObject *file, *ret;
- #ifdef DEBUG_FILES
- printf("xmlPythonFileCloseUnref\n");
- #endif
- file = (PyObject *) context;
- if (file == NULL) return(-1);
- ret = PyEval_CallMethod(file, (char *) "close", (char *) "()");
- if (ret != NULL) {
- Py_DECREF(ret);
- }
- Py_DECREF(file);
- return(0);
- }
- /**
- * xmlPythonFileReadRaw:
- * @context: the I/O context
- * @buffer: where to drop data
- * @len: number of bytes to write
- *
- * Read @len bytes to @buffer from the Python file in the I/O channel
- *
- * Returns the number of bytes read
- */
- static int
- xmlPythonFileReadRaw (void * context, char * buffer, int len) {
- PyObject *file;
- PyObject *ret;
- int lenread = -1;
- char *data;
- #ifdef DEBUG_FILES
- printf("xmlPythonFileReadRaw: %d\n", len);
- #endif
- file = (PyObject *) context;
- if (file == NULL) return(-1);
- ret = PyEval_CallMethod(file, (char *) "read", (char *) "(i)", len);
- if (ret == NULL) {
- printf("xmlPythonFileReadRaw: result is NULL\n");
- return(-1);
- } else if (PyString_Check(ret)) {
- lenread = PyString_Size(ret);
- data = PyString_AsString(ret);
- if (lenread > len)
- memcpy(buffer, data, len);
- else
- memcpy(buffer, data, lenread);
- Py_DECREF(ret);
- } else {
- printf("xmlPythonFileReadRaw: result is not a String\n");
- Py_DECREF(ret);
- }
- return(lenread);
- }
- /**
- * xmlPythonFileRead:
- * @context: the I/O context
- * @buffer: where to drop data
- * @len: number of bytes to write
- *
- * Read @len bytes to @buffer from the I/O channel.
- *
- * Returns the number of bytes read
- */
- static int
- xmlPythonFileRead (void * context, char * buffer, int len) {
- PyObject *file;
- PyObject *ret;
- int lenread = -1;
- char *data;
- #ifdef DEBUG_FILES
- printf("xmlPythonFileRead: %d\n", len);
- #endif
- file = (PyObject *) context;
- if (file == NULL) return(-1);
- ret = PyEval_CallMethod(file, (char *) "io_read", (char *) "(i)", len);
- if (ret == NULL) {
- printf("xmlPythonFileRead: result is NULL\n");
- return(-1);
- } else if (PyString_Check(ret)) {
- lenread = PyString_Size(ret);
- data = PyString_AsString(ret);
- if (lenread > len)
- memcpy(buffer, data, len);
- else
- memcpy(buffer, data, lenread);
- Py_DECREF(ret);
- } else {
- printf("xmlPythonFileRead: result is not a String\n");
- Py_DECREF(ret);
- }
- return(lenread);
- }
- /**
- * xmlFileWrite:
- * @context: the I/O context
- * @buffer: where to drop data
- * @len: number of bytes to write
- *
- * Write @len bytes from @buffer to the I/O channel.
- *
- * Returns the number of bytes written
- */
- static int
- xmlPythonFileWrite (void * context, const char * buffer, int len) {
- PyObject *file;
- PyObject *string;
- PyObject *ret = NULL;
- int written = -1;
- #ifdef DEBUG_FILES
- printf("xmlPythonFileWrite: %d\n", len);
- #endif
- file = (PyObject *) context;
- if (file == NULL) return(-1);
- string = PyString_FromStringAndSize(buffer, len);
- if (string == NULL) return(-1);
- if (PyObject_HasAttrString(file, (char *) "io_write")) {
- ret = PyEval_CallMethod(file, (char *) "io_write", (char *) "(O)",
- string);
- } else if (PyObject_HasAttrString(file, (char *) "write")) {
- ret = PyEval_CallMethod(file, (char *) "write", (char *) "(O)",
- string);
- }
- Py_DECREF(string);
- if (ret == NULL) {
- printf("xmlPythonFileWrite: result is NULL\n");
- return(-1);
- } else if (PyInt_Check(ret)) {
- written = (int) PyInt_AsLong(ret);
- Py_DECREF(ret);
- } else if (ret == Py_None) {
- written = len;
- Py_DECREF(ret);
- } else {
- printf("xmlPythonFileWrite: result is not an Int nor None\n");
- Py_DECREF(ret);
- }
- return(written);
- }
- /**
- * xmlPythonFileClose:
- * @context: the I/O context
- *
- * Close an I/O channel
- */
- static int
- xmlPythonFileClose (void * context) {
- PyObject *file, *ret = NULL;
- #ifdef DEBUG_FILES
- printf("xmlPythonFileClose\n");
- #endif
- file = (PyObject *) context;
- if (file == NULL) return(-1);
- if (PyObject_HasAttrString(file, (char *) "io_close")) {
- ret = PyEval_CallMethod(file, (char *) "io_close", (char *) "()");
- } else if (PyObject_HasAttrString(file, (char *) "flush")) {
- ret = PyEval_CallMethod(file, (char *) "flush", (char *) "()");
- }
- if (ret != NULL) {
- Py_DECREF(ret);
- }
- return(0);
- }
- #ifdef LIBXML_OUTPUT_ENABLED
- /**
- * xmlOutputBufferCreatePythonFile:
- * @file: a PyFile_Type
- * @encoder: the encoding converter or NULL
- *
- * Create a buffered output for the progressive saving to a PyFile_Type
- * buffered C I/O
- *
- * Returns the new parser output or NULL
- */
- static xmlOutputBufferPtr
- xmlOutputBufferCreatePythonFile(PyObject *file,
- xmlCharEncodingHandlerPtr encoder) {
- xmlOutputBufferPtr ret;
- if (file == NULL) return(NULL);
- ret = xmlAllocOutputBuffer(encoder);
- if (ret != NULL) {
- ret->context = file;
- /* Py_INCREF(file); */
- ret->writecallback = xmlPythonFileWrite;
- ret->closecallback = xmlPythonFileClose;
- }
- return(ret);
- }
- PyObject *
- libxml_xmlCreateOutputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
- PyObject *py_retval;
- PyObject *file;
- xmlChar *encoding;
- xmlCharEncodingHandlerPtr handler = NULL;
- xmlOutputBufferPtr buffer;
- if (!PyArg_ParseTuple(args, (char *)"Oz:xmlOutputBufferCreate",
- &file, &encoding))
- return(NULL);
- if ((encoding != NULL) && (encoding[0] != 0)) {
- handler = xmlFindCharEncodingHandler((const char *) encoding);
- }
- buffer = xmlOutputBufferCreatePythonFile(file, handler);
- if (buffer == NULL)
- printf("libxml_xmlCreateOutputBuffer: buffer == NULL\n");
- py_retval = libxml_xmlOutputBufferPtrWrap(buffer);
- return(py_retval);
- }
- /**
- * libxml_outputBufferGetPythonFile:
- * @buffer: the I/O buffer
- *
- * read the Python I/O from the CObject
- *
- * Returns the new parser output or NULL
- */
- static PyObject *
- libxml_outputBufferGetPythonFile(ATTRIBUTE_UNUSED PyObject *self,
- PyObject *args) {
- PyObject *buffer;
- PyObject *file;
- xmlOutputBufferPtr obj;
- if (!PyArg_ParseTuple(args, (char *)"O:outputBufferGetPythonFile",
- &buffer))
- return(NULL);
- obj = PyoutputBuffer_Get(buffer);
- if (obj == NULL) {
- fprintf(stderr,
- "outputBufferGetPythonFile: obj == NULL\n");
- Py_INCREF(Py_None);
- return(Py_None);
- }
- if (obj->closecallback != xmlPythonFileClose) {
- fprintf(stderr,
- "outputBufferGetPythonFile: not a python file wrapper\n");
- Py_INCREF(Py_None);
- return(Py_None);
- }
- file = (PyObject *) obj->context;
- if (file == NULL) {
- Py_INCREF(Py_None);
- return(Py_None);
- }
- Py_INCREF(file);
- return(file);
- }
- static PyObject *
- libxml_xmlOutputBufferClose(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
- PyObject *py_retval;
- int c_retval;
- xmlOutputBufferPtr out;
- PyObject *pyobj_out;
- if (!PyArg_ParseTuple(args, (char *)"O:xmlOutputBufferClose", &pyobj_out))
- return(NULL);
- out = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_out);
- /* Buffer may already have been destroyed elsewhere. This is harmless. */
- if (out == NULL) {
- Py_INCREF(Py_None);
- return(Py_None);
- }
- c_retval = xmlOutputBufferClose(out);
- py_retval = libxml_intWrap((int) c_retval);
- return(py_retval);
- }
- static PyObject *
- libxml_xmlOutputBufferFlush(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
- PyObject *py_retval;
- int c_retval;
- xmlOutputBufferPtr out;
- PyObject *pyobj_out;
- if (!PyArg_ParseTuple(args, (char *)"O:xmlOutputBufferFlush", &pyobj_out))
- return(NULL);
- out = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_out);
- c_retval = xmlOutputBufferFlush(out);
- py_retval = libxml_intWrap((int) c_retval);
- return(py_retval);
- }
- static PyObject *
- libxml_xmlSaveFileTo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
- PyObject *py_retval;
- int c_retval;
- xmlOutputBufferPtr buf;
- PyObject *pyobj_buf;
- xmlDocPtr cur;
- PyObject *pyobj_cur;
- char * encoding;
- if (!PyArg_ParseTuple(args, (char *)"OOz:xmlSaveFileTo", &pyobj_buf, &pyobj_cur, &encoding))
- return(NULL);
- buf = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_buf);
- cur = (xmlDocPtr) PyxmlNode_Get(pyobj_cur);
- c_retval = xmlSaveFileTo(buf, cur, encoding);
- /* xmlSaveTo() freed the memory pointed to by buf, so record that in the
- * Python object. */
- ((PyoutputBuffer_Object *)(pyobj_buf))->obj = NULL;
- py_retval = libxml_intWrap((int) c_retval);
- return(py_retval);
- }
- static PyObject *
- libxml_xmlSaveFormatFileTo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
- PyObject *py_retval;
- int c_retval;
- xmlOutputBufferPtr buf;
- PyObject *pyobj_buf;
- xmlDocPtr cur;
- PyObject *pyobj_cur;
- char * encoding;
- int format;
- if (!PyArg_ParseTuple(args, (char *)"OOzi:xmlSaveFormatFileTo", &pyobj_buf, &pyobj_cur, &encoding, &format))
- return(NULL);
- buf = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_buf);
- cur = (xmlDocPtr) PyxmlNode_Get(pyobj_cur);
- c_retval = xmlSaveFormatFileTo(buf, cur, encoding, format);
- /* xmlSaveFormatFileTo() freed the memory pointed to by buf, so record that
- * in the Python object */
- ((PyoutputBuffer_Object *)(pyobj_buf))->obj = NULL;
- py_retval = libxml_intWrap((int) c_retval);
- return(py_retval);
- }
- #endif /* LIBXML_OUTPUT_ENABLED */
- /**
- * xmlParserInputBufferCreatePythonFile:
- * @file: a PyFile_Type
- * @encoder: the encoding converter or NULL
- *
- * Create a buffered output for the progressive saving to a PyFile_Type
- * buffered C I/O
- *
- * Returns the new parser output or NULL
- */
- static xmlParserInputBufferPtr
- xmlParserInputBufferCreatePythonFile(PyObject *file,
- xmlCharEncoding encoding) {
- xmlParserInputBufferPtr ret;
- if (file == NULL) return(NULL);
- ret = xmlAllocParserInputBuffer(encoding);
- if (ret != NULL) {
- ret->context = file;
- /* Py_INCREF(file); */
- ret->readcallback = xmlPythonFileRead;
- ret->closecallback = xmlPythonFileClose;
- }
- return(ret);
- }
- PyObject *
- libxml_xmlCreateInputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
- PyObject *py_retval;
- PyObject *file;
- xmlChar *encoding;
- xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
- xmlParserInputBufferPtr buffer;
- if (!PyArg_ParseTuple(args, (char *)"Oz:xmlParserInputBufferCreate",
- &file, &encoding))
- return(NULL);
- if ((encoding != NULL) && (encoding[0] != 0)) {
- enc = xmlParseCharEncoding((const char *) encoding);
- }
- buffer = xmlParserInputBufferCreatePythonFile(file, enc);
- if (buffer == NULL)
- printf("libxml_xmlParserInputBufferCreate: buffer == NULL\n");
- py_retval = libxml_xmlParserInputBufferPtrWrap(buffer);
- return(py_retval);
- }
- /************************************************************************
- * *
- * Providing the resolver at the Python level *
- * *
- ************************************************************************/
- static xmlExternalEntityLoader defaultExternalEntityLoader = NULL;
- static PyObject *pythonExternalEntityLoaderObjext;
- static xmlParserInputPtr
- pythonExternalEntityLoader(const char *URL, const char *ID,
- xmlParserCtxtPtr ctxt) {
- xmlParserInputPtr result = NULL;
- if (pythonExternalEntityLoaderObjext != NULL) {
- PyObject *ret;
- PyObject *ctxtobj;
- ctxtobj = libxml_xmlParserCtxtPtrWrap(ctxt);
- #ifdef DEBUG_LOADER
- printf("pythonExternalEntityLoader: ready to call\n");
- #endif
- ret = PyObject_CallFunction(pythonExternalEntityLoaderObjext,
- (char *) "(ssO)", URL, ID, ctxtobj);
- Py_XDECREF(ctxtobj);
- #ifdef DEBUG_LOADER
- printf("pythonExternalEntityLoader: result ");
- PyObject_Print(ret, stderr, 0);
- printf("\n");
- #endif
- if (ret != NULL) {
- if (PyObject_HasAttrString(ret, (char *) "read")) {
- xmlParserInputBufferPtr buf;
- buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
- if (buf != NULL) {
- buf->context = ret;
- buf->readcallback = xmlPythonFileReadRaw;
- buf->closecallback = xmlPythonFileCloseRaw;
- result = xmlNewIOInputStream(ctxt, buf,
- XML_CHAR_ENCODING_NONE);
- }
- #if 0
- } else {
- if (URL != NULL)
- printf("pythonExternalEntityLoader: can't read %s\n",
- URL);
- #endif
- }
- if (result == NULL) {
- Py_DECREF(ret);
- } else if (URL != NULL) {
- result->filename = (char *) xmlStrdup((const xmlChar *)URL);
- result->directory = xmlParserGetDirectory((const char *) URL);
- }
- }
- }
- if ((result == NULL) && (defaultExternalEntityLoader != NULL)) {
- result = defaultExternalEntityLoader(URL, ID, ctxt);
- }
- return(result);
- }
- PyObject *
- libxml_xmlSetEntityLoader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
- PyObject *py_retval;
- PyObject *loader;
- if (!PyArg_ParseTuple(args, (char *)"O:libxml_xmlSetEntityLoader",
- &loader))
- return(NULL);
- #ifdef DEBUG_LOADER
- printf("libxml_xmlSetEntityLoader\n");
- #endif
- if (defaultExternalEntityLoader == NULL)
- defaultExternalEntityLoader = xmlGetExternalEntityLoader();
- pythonExternalEntityLoaderObjext = loader;
- xmlSetExternalEntityLoader(pythonExternalEntityLoader);
- py_retval = PyInt_FromLong(0);
- return(py_retval);
- }
- /************************************************************************
- * *
- * Handling SAX/xmllib/sgmlop callback interfaces *
- * *
- ************************************************************************/
- static void
- pythonStartElement(void *user_data, const xmlChar * name,
- const xmlChar ** attrs)
- {
- int i;
- PyObject *handler;
- PyObject *dict;
- PyObject *attrname;
- PyObject *attrvalue;
- PyObject *result = NULL;
- int type = 0;
- #ifdef DEBUG_SAX
- printf("pythonStartElement(%s) called\n", name);
- #endif
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "startElement"))
- type = 1;
- else if (PyObject_HasAttrString(handler, (char *) "start"))
- type = 2;
- if (type != 0) {
- /*
- * the xmllib interface always generate a dictionnary,
- * possibly empty
- */
- if ((attrs == NULL) && (type == 1)) {
- Py_XINCREF(Py_None);
- dict = Py_None;
- } else if (attrs == NULL) {
- dict = PyDict_New();
- } else {
- dict = PyDict_New();
- for (i = 0; attrs[i] != NULL; i++) {
- attrname = PyString_FromString((char *) attrs[i]);
- i++;
- if (attrs[i] != NULL) {
- attrvalue = PyString_FromString((char *) attrs[i]);
- } else {
- Py_XINCREF(Py_None);
- attrvalue = Py_None;
- }
- PyDict_SetItem(dict, attrname, attrvalue);
- Py_DECREF(attrname);
- Py_DECREF(attrvalue);
- }
- }
- if (type == 1)
- result = PyObject_CallMethod(handler, (char *) "startElement",
- (char *) "sO", name, dict);
- else if (type == 2)
- result = PyObject_CallMethod(handler, (char *) "start",
- (char *) "sO", name, dict);
- if (PyErr_Occurred())
- PyErr_Print();
- Py_XDECREF(dict);
- Py_XDECREF(result);
- }
- }
- static void
- pythonStartDocument(void *user_data)
- {
- PyObject *handler;
- PyObject *result;
- #ifdef DEBUG_SAX
- printf("pythonStartDocument() called\n");
- #endif
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "startDocument")) {
- result =
- PyObject_CallMethod(handler, (char *) "startDocument", NULL);
- if (PyErr_Occurred())
- PyErr_Print();
- Py_XDECREF(result);
- }
- }
- static void
- pythonEndDocument(void *user_data)
- {
- PyObject *handler;
- PyObject *result;
- #ifdef DEBUG_SAX
- printf("pythonEndDocument() called\n");
- #endif
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "endDocument")) {
- result =
- PyObject_CallMethod(handler, (char *) "endDocument", NULL);
- if (PyErr_Occurred())
- PyErr_Print();
- Py_XDECREF(result);
- }
- /*
- * The reference to the handler is released there
- */
- Py_XDECREF(handler);
- }
- static void
- pythonEndElement(void *user_data, const xmlChar * name)
- {
- PyObject *handler;
- PyObject *result;
- #ifdef DEBUG_SAX
- printf("pythonEndElement(%s) called\n", name);
- #endif
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "endElement")) {
- result = PyObject_CallMethod(handler, (char *) "endElement",
- (char *) "s", name);
- if (PyErr_Occurred())
- PyErr_Print();
- Py_XDECREF(result);
- } else if (PyObject_HasAttrString(handler, (char *) "end")) {
- result = PyObject_CallMethod(handler, (char *) "end",
- (char *) "s", name);
- if (PyErr_Occurred())
- PyErr_Print();
- Py_XDECREF(result);
- }
- }
- static void
- pythonReference(void *user_data, const xmlChar * name)
- {
- PyObject *handler;
- PyObject *result;
- #ifdef DEBUG_SAX
- printf("pythonReference(%s) called\n", name);
- #endif
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "reference")) {
- result = PyObject_CallMethod(handler, (char *) "reference",
- (char *) "s", name);
- if (PyErr_Occurred())
- PyErr_Print();
- Py_XDECREF(result);
- }
- }
- static void
- pythonCharacters(void *user_data, const xmlChar * ch, int len)
- {
- PyObject *handler;
- PyObject *result = NULL;
- int type = 0;
- #ifdef DEBUG_SAX
- printf("pythonCharacters(%s, %d) called\n", ch, len);
- #endif
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "characters"))
- type = 1;
- else if (PyObject_HasAttrString(handler, (char *) "data"))
- type = 2;
- if (type != 0) {
- if (type == 1)
- result = PyObject_CallMethod(handler, (char *) "characters",
- (char *) "s#", ch, len);
- else if (type == 2)
- result = PyObject_CallMethod(handler, (char *) "data",
- (char *) "s#", ch, len);
- if (PyErr_Occurred())
- PyErr_Print();
- Py_XDECREF(result);
- }
- }
- static void
- pythonIgnorableWhitespace(void *user_data, const xmlChar * ch, int len)
- {
- PyObject *handler;
- PyObject *result = NULL;
- int type = 0;
- #ifdef DEBUG_SAX
- printf("pythonIgnorableWhitespace(%s, %d) called\n", ch, len);
- #endif
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "ignorableWhitespace"))
- type = 1;
- else if (PyObject_HasAttrString(handler, (char *) "data"))
- type = 2;
- if (type != 0) {
- if (type == 1)
- result =
- PyObject_CallMethod(handler,
- (char *) "ignorableWhitespace",
- (char *) "s#", ch, len);
- else if (type == 2)
- result =
- PyObject_CallMethod(handler, (char *) "data",
- (char *) "s#", ch, len);
- Py_XDECREF(result);
- }
- }
- static void
- pythonProcessingInstruction(void *user_data,
- const xmlChar * target, const xmlChar * data)
- {
- PyObject *handler;
- PyObject *result;
- #ifdef DEBUG_SAX
- printf("pythonProcessingInstruction(%s, %s) called\n", target, data);
- #endif
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "processingInstruction")) {
- result = PyObject_CallMethod(handler, (char *)
- "processingInstruction",
- (char *) "ss", target, data);
- Py_XDECREF(result);
- }
- }
- static void
- pythonComment(void *user_data, const xmlChar * value)
- {
- PyObject *handler;
- PyObject *result;
- #ifdef DEBUG_SAX
- printf("pythonComment(%s) called\n", value);
- #endif
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "comment")) {
- result =
- PyObject_CallMethod(handler, (char *) "comment", (char *) "s",
- value);
- if (PyErr_Occurred())
- PyErr_Print();
- Py_XDECREF(result);
- }
- }
- static void
- pythonWarning(void *user_data, const char *msg, ...)
- {
- PyObject *handler;
- PyObject *result;
- va_list args;
- char buf[1024];
- #ifdef DEBUG_SAX
- printf("pythonWarning(%s) called\n", msg);
- #endif
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "warning")) {
- va_start(args, msg);
- vsnprintf(buf, 1023, msg, args);
- va_end(args);
- buf[1023] = 0;
- result =
- PyObject_CallMethod(handler, (char *) "warning", (char *) "s",
- buf);
- if (PyErr_Occurred())
- PyErr_Print();
- Py_XDECREF(result);
- }
- }
- static void
- pythonError(void *user_data, const char *msg, ...)
- {
- PyObject *handler;
- PyObject *result;
- va_list args;
- char buf[1024];
- #ifdef DEBUG_SAX
- printf("pythonError(%s) called\n", msg);
- #endif
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "error")) {
- va_start(args, msg);
- vsnprintf(buf, 1023, msg, args);
- va_end(args);
- buf[1023] = 0;
- result =
- PyObject_CallMethod(handler, (char *) "error", (char *) "s",
- buf);
- if (PyErr_Occurred())
- PyErr_Print();
- Py_XDECREF(result);
- }
- }
- static void
- pythonFatalError(void *user_data, const char *msg, ...)
- {
- PyObject *handler;
- PyObject *result;
- va_list args;
- char buf[1024];
- #ifdef DEBUG_SAX
- printf("pythonFatalError(%s) called\n", msg);
- #endif
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "fatalError")) {
- va_start(args, msg);
- vsnprintf(buf, 1023, msg, args);
- va_end(args);
- buf[1023] = 0;
- result =
- PyObject_CallMethod(handler, (char *) "fatalError",
- (char *) "s", buf);
- if (PyErr_Occurred())
- PyErr_Print();
- Py_XDECREF(result);
- }
- }
- static void
- pythonCdataBlock(void *user_data, const xmlChar * ch, int len)
- {
- PyObject *handler;
- PyObject *result = NULL;
- int type = 0;
- #ifdef DEBUG_SAX
- printf("pythonCdataBlock(%s, %d) called\n", ch, len);
- #endif
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "cdataBlock"))
- type = 1;
- else if (PyObject_HasAttrString(handler, (char *) "cdata"))
- type = 2;
- if (type != 0) {
- if (type == 1)
- result =
- PyObject_CallMethod(handler, (char *) "cdataBlock",
- (char *) "s#", ch, len);
- else if (type == 2)
- result =
- PyObject_CallMethod(handler, (char *) "cdata",
- (char *) "s#", ch, len);
- if (PyErr_Occurred())
- PyErr_Print();
- Py_XDECREF(result);
- }
- }
- static void
- pythonExternalSubset(void *user_data,
- const xmlChar * name,
- const xmlChar * externalID, const xmlChar * systemID)
- {
- PyObject *handler;
- PyObject *result;
- #ifdef DEBUG_SAX
- printf("pythonExternalSubset(%s, %s, %s) called\n",
- name, externalID, systemID);
- #endif
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "externalSubset")) {
- result =
- PyObject_CallMethod(handler, (char *) "externalSubset",
- (char *) "sss", name, externalID,
- systemID);
- Py_XDECREF(result);
- }
- }
- static void
- pythonEntityDecl(void *user_data,
- const xmlChar * name,
- int type,
- const xmlChar * publicId,
- const xmlChar * systemId, xmlChar * content)
- {
- PyObject *handler;
- PyObject *result;
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "entityDecl")) {
- result = PyObject_CallMethod(handler, (char *) "entityDecl",
- (char *) "sisss", name, type,
- publicId, systemId, content);
- if (PyErr_Occurred())
- PyErr_Print();
- Py_XDECREF(result);
- }
- }
- static void
- pythonNotationDecl(void *user_data,
- const xmlChar * name,
- const xmlChar * publicId, const xmlChar * systemId)
- {
- PyObject *handler;
- PyObject *result;
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "notationDecl")) {
- result = PyObject_CallMethod(handler, (char *) "notationDecl",
- (char *) "sss", name, publicId,
- systemId);
- if (PyErr_Occurred())
- PyErr_Print();
- Py_XDECREF(result);
- }
- }
- static void
- pythonAttributeDecl(void *user_data,
- const xmlChar * elem,
- const xmlChar * name,
- int type,
- int def,
- const xmlChar * defaultValue, xmlEnumerationPtr tree)
- {
- PyObject *handler;
- PyObject *nameList;
- PyObject *newName;
- xmlEnumerationPtr node;
- PyObject *result;
- int count;
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "attributeDecl")) {
- count = 0;
- for (node = tree; node != NULL; node = node->next) {
- count++;
- }
- nameList = PyList_New(count);
- count = 0;
- for (node = tree; node != NULL; node = node->next) {
- newName = PyString_FromString((char *) node->name);
- PyList_SetItem(nameList, count, newName);
- Py_DECREF(newName);
- count++;
- }
- result = PyObject_CallMethod(handler, (char *) "attributeDecl",
- (char *) "ssiisO", elem, name, type,
- def, defaultValue, nameList);
- if (PyErr_Occurred())
- PyErr_Print();
- Py_XDECREF(nameList);
- Py_XDECREF(result);
- }
- }
- static void
- pythonElementDecl(void *user_data,
- const xmlChar * name,
- int type, ATTRIBUTE_UNUSED xmlElementContentPtr content)
- {
- PyObject *handler;
- PyObject *obj;
- PyObject *result;
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "elementDecl")) {
- /* TODO: wrap in an elementContent object */
- printf
- ("pythonElementDecl: xmlElementContentPtr wrapper missing !\n");
- obj = Py_None;
- /* Py_XINCREF(Py_None); isn't the reference just borrowed ??? */
- result = PyObject_CallMethod(handler, (char *) "elementDecl",
- (char *) "siO", name, type, obj);
- if (PyErr_Occurred())
- PyErr_Print();
- Py_XDECREF(result);
- }
- }
- static void
- pythonUnparsedEntityDecl(void *user_data,
- const xmlChar * name,
- const xmlChar * publicId,
- const xmlChar * systemId,
- const xmlChar * notationName)
- {
- PyObject *handler;
- PyObject *result;
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "unparsedEntityDecl")) {
- result =
- PyObject_CallMethod(handler, (char *) "unparsedEntityDecl",
- (char *) "ssss", name, publicId, systemId,
- notationName);
- if (PyErr_Occurred())
- PyErr_Print();
- Py_XDECREF(result);
- }
- }
- static void
- pythonInternalSubset(void *user_data, const xmlChar * name,
- const xmlChar * ExternalID, const xmlChar * SystemID)
- {
- PyObject *handler;
- PyObject *result;
- #ifdef DEBUG_SAX
- printf("pythonInternalSubset(%s, %s, %s) called\n",
- name, ExternalID, SystemID);
- #endif
- handler = (PyObject *) user_data;
- if (PyObject_HasAttrString(handler, (char *) "internalSubset")) {
- result = PyObject_CallMethod(handler, (char *) "internalSubset",
- (char *) "sss", name, ExternalID,
- SystemID);
- if (PyErr_Occurred())
- PyErr_Print();
- Py_XDECREF(result);
- }
- }
- static xmlSAXHandler pythonSaxHandler = {
- pythonInternalSubset,
- NULL, /* TODO pythonIsStandalone, */
- NULL, /* TODO pythonHasInternalSubset, */
- NULL, /* TODO pythonHasExternalSubset, */
- NULL, /* TODO pythonResolveEntity, */
- NULL, /* TODO pythonGetEntity, */
- pythonEntityDecl,
- pythonNotationDecl,
- pythonAttributeDecl,
- pythonElementDecl,
- pythonUnparsedEntityDecl,
- NULL, /* OBSOLETED pythonSetDocumentLocator, */
- pythonStartDocument,
- pythonEndDocument,
- pythonStartElement,
- pythonEndElement,
- pythonReference,
- pythonCharacters,
- pythonIgnorableWhitespace,
- pythonProcessingInstruction,
- pythonComment,
- pythonWarning,
- pythonError,
- pythonFatalError,
- NULL, /* TODO pythonGetParameterEntity, */
- pythonCdataBlock,
- pythonExternalSubset,
- 1,
- NULL, /* TODO mograte to SAX2 */
- NULL,
- NULL,
- NULL
- };
- /************************************************************************
- * *
- * Handling of specific parser context *
- * *
- ************************************************************************/
- PyObject *
- libxml_xmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
- PyObject * args)
- {
- const char *chunk;
- int size;
- const char *URI;
- PyObject *pyobj_SAX = NULL;
- xmlSAXHandlerPtr SAX = NULL;
- xmlParserCtxtPtr ret;
- PyObject *pyret;
- if (!PyArg_ParseTuple
- (args, (char *) "Oziz:xmlCreatePushParser", &pyobj_SAX, &chunk,
- &size, &URI))
- return (NULL);
- #ifdef DEBUG
- printf("libxml_xmlCreatePushParser(%p, %s, %d, %s) called\n",
- pyobj_SAX, chunk, size, URI);
- #endif
- if (pyobj_SAX != Py_None) {
- SAX = &pythonSaxHandler;
- Py_INCREF(pyobj_SAX);
- /* The reference is released in pythonEndDocument() */
- }
- ret = xmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI);
- pyret = libxml_xmlParserCtxtPtrWrap(ret);
- return (pyret);
- }
- PyObject *
- libxml_htmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
- PyObject * args)
- {
- #ifdef LIBXML_HTML_ENABLED
- const char *chunk;
- int size;
- const char *URI;
- PyObject *pyobj_SAX = NULL;
- xmlSAXHandlerPtr SAX = NULL;
- xmlParserCtxtPtr ret;
- PyObject *pyret;
- if (!PyArg_ParseTuple
- (args, (char *) "Oziz:htmlCreatePushParser", &pyobj_SAX, &chunk,
- &size, &URI))
- return (NULL);
- #ifdef DEBUG
- printf("libxml_htmlCreatePushParser(%p, %s, %d, %s) called\n",
- pyobj_SAX, chunk, size, URI);
- #endif
- if (pyobj_SAX != Py_None) {
- SAX = &pythonSaxHandler;
- Py_INCREF(pyobj_SAX);
- /* The reference is released in pythonEndDocument() */
- }
- ret = htmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI,
- XML_CHAR_ENCODING_NONE);
- pyret = libxml_xmlParserCtxtPtrWrap(ret);
- return (pyret);
- #else
- Py_INCREF(Py_None);
- return (Py_None);
- #endif /* LIBXML_HTML_ENABLED */
- }
- PyObject *
- libxml_xmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- int recover;
- const char *URI;
- PyObject *pyobj_SAX = NULL;
- xmlSAXHandlerPtr SAX = NULL;
- if (!PyArg_ParseTuple(args, (char *) "Osi:xmlSAXParseFile", &pyobj_SAX,
- &URI, &recover))
- return (NULL);
- #ifdef DEBUG
- printf("libxml_xmlSAXParseFile(%p, %s, %d) called\n",
- pyobj_SAX, URI, recover);
- #endif
- if (pyobj_SAX == Py_None) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- SAX = &pythonSaxHandler;
- Py_INCREF(pyobj_SAX);
- /* The reference is released in pythonEndDocument() */
- xmlSAXUserParseFile(SAX, pyobj_SAX, URI);
- Py_INCREF(Py_None);
- return (Py_None);
- }
- PyObject *
- libxml_htmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- #ifdef LIBXML_HTML_ENABLED
- const char *URI;
- const char *encoding;
- PyObject *pyobj_SAX = NULL;
- xmlSAXHandlerPtr SAX = NULL;
- if (!PyArg_ParseTuple
- (args, (char *) "Osz:htmlSAXParseFile", &pyobj_SAX, &URI,
- &encoding))
- return (NULL);
- #ifdef DEBUG
- printf("libxml_htmlSAXParseFile(%p, %s, %s) called\n",
- pyobj_SAX, URI, encoding);
- #endif
- if (pyobj_SAX == Py_None) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- SAX = &pythonSaxHandler;
- Py_INCREF(pyobj_SAX);
- /* The reference is released in pythonEndDocument() */
- htmlSAXParseFile(URI, encoding, SAX, pyobj_SAX);
- Py_INCREF(Py_None);
- return (Py_None);
- #else
- Py_INCREF(Py_None);
- return (Py_None);
- #endif /* LIBXML_HTML_ENABLED */
- }
- /************************************************************************
- * *
- * Error message callback *
- * *
- ************************************************************************/
- static PyObject *libxml_xmlPythonErrorFuncHandler = NULL;
- static PyObject *libxml_xmlPythonErrorFuncCtxt = NULL;
- /* helper to build a xmlMalloc'ed string from a format and va_list */
- /*
- * disabled the loop, the repeated call to vsnprintf without reset of ap
- * in case the initial buffer was too small segfaulted on x86_64
- * we now directly vsnprintf on a large buffer.
- */
- static char *
- libxml_buildMessage(const char *msg, va_list ap)
- {
- int chars;
- char *str;
- str = (char *) xmlMalloc(1000);
- if (str == NULL)
- return NULL;
- chars = vsnprintf(str, 999, msg, ap);
- if (chars >= 998)
- str[999] = 0;
- return str;
- }
- static void
- libxml_xmlErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, const char *msg,
- ...)
- {
- va_list ap;
- PyObject *list;
- PyObject *message;
- PyObject *result;
- char str[1000];
- #ifdef DEBUG_ERROR
- printf("libxml_xmlErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
- #endif
- if (libxml_xmlPythonErrorFuncHandler == NULL) {
- va_start(ap, msg);
- vfprintf(stderr, msg, ap);
- va_end(ap);
- } else {
- va_start(ap, msg);
- if (vsnprintf(str, 999, msg, ap) >= 998)
- str[999] = 0;
- va_end(ap);
- list = PyTuple_New(2);
- PyTuple_SetItem(list, 0, libxml_xmlPythonErrorFuncCtxt);
- Py_XINCREF(libxml_xmlPythonErrorFuncCtxt);
- message = libxml_charPtrConstWrap(str);
- PyTuple_SetItem(list, 1, message);
- result = PyEval_CallObject(libxml_xmlPythonErrorFuncHandler, list);
- Py_XDECREF(list);
- Py_XDECREF(result);
- }
- }
- static void
- libxml_xmlErrorInitialize(void)
- {
- #ifdef DEBUG_ERROR
- printf("libxml_xmlErrorInitialize() called\n");
- #endif
- xmlSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
- xmlThrDefSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
- }
- static PyObject *
- libxml_xmlRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self,
- PyObject * args)
- {
- PyObject *py_retval;
- PyObject *pyobj_f;
- PyObject *pyobj_ctx;
- if (!PyArg_ParseTuple
- (args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f,
- &pyobj_ctx))
- return (NULL);
- #ifdef DEBUG_ERROR
- printf("libxml_xmlRegisterErrorHandler(%p, %p) called\n", pyobj_ctx,
- pyobj_f);
- #endif
- if (libxml_xmlPythonErrorFuncHandler != NULL) {
- Py_XDECREF(libxml_xmlPythonErrorFuncHandler);
- }
- if (libxml_xmlPythonErrorFuncCtxt != NULL) {
- Py_XDECREF(libxml_xmlPythonErrorFuncCtxt);
- }
- Py_XINCREF(pyobj_ctx);
- Py_XINCREF(pyobj_f);
- /* TODO: check f is a function ! */
- libxml_xmlPythonErrorFuncHandler = pyobj_f;
- libxml_xmlPythonErrorFuncCtxt = pyobj_ctx;
- py_retval = libxml_intWrap(1);
- return (py_retval);
- }
- /************************************************************************
- * *
- * Per parserCtxt error handler *
- * *
- ************************************************************************/
- typedef struct
- {
- PyObject *f;
- PyObject *arg;
- } xmlParserCtxtPyCtxt;
- typedef xmlParserCtxtPyCtxt *xmlParserCtxtPyCtxtPtr;
- static void
- libxml_xmlParserCtxtGenericErrorFuncHandler(void *ctx, int severity, char *str)
- {
- PyObject *list;
- PyObject *result;
- xmlParserCtxtPtr ctxt;
- xmlParserCtxtPyCtxtPtr pyCtxt;
-
- #ifdef DEBUG_ERROR
- printf("libxml_xmlParserCtxtGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
- #endif
- ctxt = (xmlParserCtxtPtr)ctx;
- pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
- list = PyTuple_New(4);
- PyTuple_SetItem(list, 0, pyCtxt->arg);
- Py_XINCREF(pyCtxt->arg);
- PyTuple_SetItem(list, 1, libxml_charPtrWrap(str));
- PyTuple_SetItem(list, 2, libxml_intWrap(severity));
- PyTuple_SetItem(list, 3, Py_None);
- Py_INCREF(Py_None);
- result = PyEval_CallObject(pyCtxt->f, list);
- if (result == NULL)
- {
- /* TODO: manage for the exception to be propagated... */
- PyErr_Print();
- }
- Py_XDECREF(list);
- Py_XDECREF(result);
- }
- static void
- libxml_xmlParserCtxtErrorFuncHandler(void *ctx, const char *msg, ...)
- {
- va_list ap;
- va_start(ap, msg);
- libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_ERROR,libxml_buildMessage(msg,ap));
- va_end(ap);
- }
- static void
- libxml_xmlParserCtxtWarningFuncHandler(void *ctx, const char *msg, ...)
- {
- va_list ap;
- va_start(ap, msg);
- libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_WARNING,libxml_buildMessage(msg,ap));
- va_end(ap);
- }
- static void
- libxml_xmlParserCtxtValidityErrorFuncHandler(void *ctx, const char *msg, ...)
- {
- va_list ap;
- va_start(ap, msg);
- libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap));
- va_end(ap);
- }
- static void
- libxml_xmlParserCtxtValidityWarningFuncHandler(void *ctx, const char *msg, ...)
- {
- va_list ap;
- va_start(ap, msg);
- libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap));
- va_end(ap);
- }
- static PyObject *
- libxml_xmlParserCtxtSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
- {
- PyObject *py_retval;
- xmlParserCtxtPtr ctxt;
- xmlParserCtxtPyCtxtPtr pyCtxt;
- PyObject *pyobj_ctxt;
- PyObject *pyobj_f;
- PyObject *pyobj_arg;
- if (!PyArg_ParseTuple(args, (char *)"OOO:xmlParserCtxtSetErrorHandler",
- &pyobj_ctxt, &pyobj_f, &pyobj_arg))
- return(NULL);
- ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
- if (ctxt->_private == NULL) {
- pyCtxt = xmlMalloc(sizeof(xmlParserCtxtPyCtxt));
- if (pyCtxt == NULL) {
- py_retval = libxml_intWrap(-1);
- return(py_retval);
- }
- memset(pyCtxt,0,sizeof(xmlParserCtxtPyCtxt));
- ctxt->_private = pyCtxt;
- }
- else {
- pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
- }
- /* TODO: check f is a function ! */
- Py_XDECREF(pyCtxt->f);
- Py_XINCREF(pyobj_f);
- pyCtxt->f = pyobj_f;
- Py_XDECREF(pyCtxt->arg);
- Py_XINCREF(pyobj_arg);
- pyCtxt->arg = pyobj_arg;
- if (pyobj_f != Py_None) {
- ctxt->sax->error = libxml_xmlParserCtxtErrorFuncHandler;
- ctxt->sax->warning = libxml_xmlParserCtxtWarningFuncHandler;
- ctxt->vctxt.error = libxml_xmlParserCtxtValidityErrorFuncHandler;
- ctxt->vctxt.warning = libxml_xmlParserCtxtValidityWarningFuncHandler;
- }
- else {
- ctxt->sax->error = xmlParserError;
- ctxt->vctxt.error = xmlParserValidityError;
- ctxt->sax->warning = xmlParserWarning;
- ctxt->vctxt.warning = xmlParserValidityWarning;
- }
- py_retval = libxml_intWrap(1);
- return(py_retval);
- }
- static PyObject *
- libxml_xmlParserCtxtGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
- {
- PyObject *py_retval;
- xmlParserCtxtPtr ctxt;
- xmlParserCtxtPyCtxtPtr pyCtxt;
- PyObject *pyobj_ctxt;
- if (!PyArg_ParseTuple(args, (char *)"O:xmlParserCtxtGetErrorHandler",
- &pyobj_ctxt))
- return(NULL);
- ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
- py_retval = PyTuple_New(2);
- if (ctxt->_private != NULL) {
- pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
- PyTuple_SetItem(py_retval, 0, pyCtxt->f);
- Py_XINCREF(pyCtxt->f);
- PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
- Py_XINCREF(pyCtxt->arg);
- }
- else {
- /* no python error handler registered */
- PyTuple_SetItem(py_retval, 0, Py_None);
- Py_XINCREF(Py_None);
- PyTuple_SetItem(py_retval, 1, Py_None);
- Py_XINCREF(Py_None);
- }
- return(py_retval);
- }
- static PyObject *
- libxml_xmlFreeParserCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
- xmlParserCtxtPtr ctxt;
- PyObject *pyobj_ctxt;
- xmlParserCtxtPyCtxtPtr pyCtxt;
- if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeParserCtxt", &pyobj_ctxt))
- return(NULL);
- ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
- if (ctxt != NULL) {
- pyCtxt = (xmlParserCtxtPyCtxtPtr)((xmlParserCtxtPtr)ctxt)->_private;
- if (pyCtxt) {
- Py_XDECREF(pyCtxt->f);
- Py_XDECREF(pyCtxt->arg);
- xmlFree(pyCtxt);
- }
- xmlFreeParserCtxt(ctxt);
- }
- Py_INCREF(Py_None);
- return(Py_None);
- }
- /***
- * xmlValidCtxt stuff
- */
- typedef struct
- {
- PyObject *warn;
- PyObject *error;
- PyObject *arg;
- } xmlValidCtxtPyCtxt;
- typedef xmlValidCtxtPyCtxt *xmlValidCtxtPyCtxtPtr;
- static void
- libxml_xmlValidCtxtGenericErrorFuncHandler(void *ctx, int severity, char *str)
- {
- PyObject *list;
- PyObject *result;
- xmlValidCtxtPyCtxtPtr pyCtxt;
-
- #ifdef DEBUG_ERROR
- printf("libxml_xmlValidCtxtGenericErrorFuncHandler(%p, %d, %s, ...) called\n", ctx, severity, str);
- #endif
- pyCtxt = (xmlValidCtxtPyCtxtPtr)ctx;
-
- list = PyTuple_New(2);
- PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
- PyTuple_SetItem(list, 1, pyCtxt->arg);
- Py_XINCREF(pyCtxt->arg);
- result = PyEval_CallObject(pyCtxt->error, list);
- if (result == NULL)
- {
- /* TODO: manage for the exception to be propagated... */
- PyErr_Print();
- }
- Py_XDECREF(list);
- Py_XDECREF(result);
- }
- static void
- libxml_xmlValidCtxtGenericWarningFuncHandler(void *ctx, int severity, char *str)
- {
- PyObject *list;
- PyObject *result;
- xmlValidCtxtPyCtxtPtr pyCtxt;
-
- #ifdef DEBUG_ERROR
- printf("libxml_xmlValidCtxtGenericWarningFuncHandler(%p, %d, %s, ...) called\n", ctx, severity, str);
- #endif
- pyCtxt = (xmlValidCtxtPyCtxtPtr)ctx;
- list = PyTuple_New(2);
- PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
- PyTuple_SetItem(list, 1, pyCtxt->arg);
- Py_XINCREF(pyCtxt->arg);
- result = PyEval_CallObject(pyCtxt->warn, list);
- if (result == NULL)
- {
- /* TODO: manage for the exception to be propagated... */
- PyErr_Print();
- }
- Py_XDECREF(list);
- Py_XDECREF(result);
- }
- static void
- libxml_xmlValidCtxtErrorFuncHandler(void *ctx, const char *msg, ...)
- {
- va_list ap;
- va_start(ap, msg);
- libxml_xmlValidCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap));
- va_end(ap);
- }
- static void
- libxml_xmlValidCtxtWarningFuncHandler(void *ctx, const char *msg, ...)
- {
- va_list ap;
- va_start(ap, msg);
- libxml_xmlValidCtxtGenericWarningFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap));
- va_end(ap);
- }
- static PyObject *
- libxml_xmlSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- PyObject *py_retval;
- PyObject *pyobj_error;
- PyObject *pyobj_warn;
- PyObject *pyobj_ctx;
- PyObject *pyobj_arg = Py_None;
- xmlValidCtxtPtr ctxt;
- xmlValidCtxtPyCtxtPtr pyCtxt;
- if (!PyArg_ParseTuple
- (args, (char *) "OOO|O:xmlSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
- return (NULL);
- #ifdef DEBUG_ERROR
- printf("libxml_xmlSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
- #endif
- ctxt = PyValidCtxt_Get(pyobj_ctx);
- pyCtxt = xmlMalloc(sizeof(xmlValidCtxtPyCtxt));
- if (pyCtxt == NULL) {
- py_retval = libxml_intWrap(-1);
- return(py_retval);
- }
- memset(pyCtxt, 0, sizeof(xmlValidCtxtPyCtxt));
-
- /* TODO: check warn and error is a function ! */
- Py_XDECREF(pyCtxt->error);
- Py_XINCREF(pyobj_error);
- pyCtxt->error = pyobj_error;
-
- Py_XDECREF(pyCtxt->warn);
- Py_XINCREF(pyobj_warn);
- pyCtxt->warn = pyobj_warn;
-
- Py_XDECREF(pyCtxt->arg);
- Py_XINCREF(pyobj_arg);
- pyCtxt->arg = pyobj_arg;
- ctxt->error = libxml_xmlValidCtxtErrorFuncHandler;
- ctxt->warning = libxml_xmlValidCtxtWarningFuncHandler;
- ctxt->userData = pyCtxt;
- py_retval = libxml_intWrap(1);
- return (py_retval);
- }
- static PyObject *
- libxml_xmlFreeValidCtxt(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
- xmlValidCtxtPtr cur;
- xmlValidCtxtPyCtxtPtr pyCtxt;
- PyObject *pyobj_cur;
- if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeValidCtxt", &pyobj_cur))
- return(NULL);
- cur = (xmlValidCtxtPtr) PyValidCtxt_Get(pyobj_cur);
- pyCtxt = (xmlValidCtxtPyCtxtPtr)(cur->userData);
- if (pyCtxt != NULL)
- {
- Py_XDECREF(pyCtxt->error);
- Py_XDECREF(pyCtxt->warn);
- Py_XDECREF(pyCtxt->arg);
- xmlFree(pyCtxt);
- }
- xmlFreeValidCtxt(cur);
- Py_INCREF(Py_None);
- return(Py_None);
- }
- #ifdef LIBXML_READER_ENABLED
- /************************************************************************
- * *
- * Per xmlTextReader error handler *
- * *
- ************************************************************************/
- typedef struct
- {
- PyObject *f;
- PyObject *arg;
- } xmlTextReaderPyCtxt;
- typedef xmlTextReaderPyCtxt *xmlTextReaderPyCtxtPtr;
- static void
- libxml_xmlTextReaderErrorCallback(void *arg,
- const char *msg,
- int severity,
- xmlTextReaderLocatorPtr locator)
- {
- xmlTextReaderPyCtxt *pyCtxt = (xmlTextReaderPyCtxt *)arg;
- PyObject *list;
- PyObject *result;
-
- list = PyTuple_New(4);
- PyTuple_SetItem(list, 0, pyCtxt->arg);
- Py_XINCREF(pyCtxt->arg);
- PyTuple_SetItem(list, 1, libxml_charPtrConstWrap(msg));
- PyTuple_SetItem(list, 2, libxml_intWrap(severity));
- PyTuple_SetItem(list, 3, libxml_xmlTextReaderLocatorPtrWrap(locator));
- result = PyEval_CallObject(pyCtxt->f, list);
- if (result == NULL)
- {
- /* TODO: manage for the exception to be propagated... */
- PyErr_Print();
- }
- Py_XDECREF(list);
- Py_XDECREF(result);
- }
- static PyObject *
- libxml_xmlTextReaderSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
- {
- xmlTextReaderPtr reader;
- xmlTextReaderPyCtxtPtr pyCtxt;
- xmlTextReaderErrorFunc f;
- void *arg;
- PyObject *pyobj_reader;
- PyObject *pyobj_f;
- PyObject *pyobj_arg;
- PyObject *py_retval;
- if (!PyArg_ParseTuple(args, (char *)"OOO:xmlTextReaderSetErrorHandler", &pyobj_reader, &pyobj_f, &pyobj_arg))
- return(NULL);
- reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
- /* clear previous error handler */
- xmlTextReaderGetErrorHandler(reader,&f,&arg);
- if (arg != NULL) {
- if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) {
- /* ok, it's our error handler! */
- pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
- Py_XDECREF(pyCtxt->f);
- Py_XDECREF(pyCtxt->arg);
- xmlFree(pyCtxt);
- }
- else {
- /*
- * there already an arg, and it's not ours,
- * there is definitely something wrong going on here...
- * we don't know how to free it, so we bail out...
- */
- py_retval = libxml_intWrap(-1);
- return(py_retval);
- }
- }
- xmlTextReaderSetErrorHandler(reader,NULL,NULL);
- /* set new error handler */
- if (pyobj_f != Py_None)
- {
- pyCtxt = (xmlTextReaderPyCtxtPtr)xmlMalloc(sizeof(xmlTextReaderPyCtxt));
- if (pyCtxt == NULL) {
- py_retval = libxml_intWrap(-1);
- return(py_retval);
- }
- Py_XINCREF(pyobj_f);
- pyCtxt->f = pyobj_f;
- Py_XINCREF(pyobj_arg);
- pyCtxt->arg = pyobj_arg;
- xmlTextReaderSetErrorHandler(reader,
- (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback,
- pyCtxt);
- }
- py_retval = libxml_intWrap(1);
- return(py_retval);
- }
- static PyObject *
- libxml_xmlTextReaderGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
- {
- xmlTextReaderPtr reader;
- xmlTextReaderPyCtxtPtr pyCtxt;
- xmlTextReaderErrorFunc f;
- void *arg;
- PyObject *pyobj_reader;
- PyObject *py_retval;
- if (!PyArg_ParseTuple(args, (char *)"O:xmlTextReaderSetErrorHandler", &pyobj_reader))
- return(NULL);
- reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
- xmlTextReaderGetErrorHandler(reader,&f,&arg);
- py_retval = PyTuple_New(2);
- if (f == (xmlTextReaderErrorFunc)libxml_xmlTextReaderErrorCallback) {
- /* ok, it's our error handler! */
- pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
- PyTuple_SetItem(py_retval, 0, pyCtxt->f);
- Py_XINCREF(pyCtxt->f);
- PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
- Py_XINCREF(pyCtxt->arg);
- }
- else
- {
- /* f is null or it's not our error handler */
- PyTuple_SetItem(py_retval, 0, Py_None);
- Py_XINCREF(Py_None);
- PyTuple_SetItem(py_retval, 1, Py_None);
- Py_XINCREF(Py_None);
- }
- return(py_retval);
- }
- static PyObject *
- libxml_xmlFreeTextReader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
- xmlTextReaderPtr reader;
- PyObject *pyobj_reader;
- xmlTextReaderPyCtxtPtr pyCtxt;
- xmlTextReaderErrorFunc f;
- void *arg;
- if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeTextReader", &pyobj_reader))
- return(NULL);
- if (!PyCObject_Check(pyobj_reader)) {
- Py_INCREF(Py_None);
- return(Py_None);
- }
- reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
- if (reader == NULL) {
- Py_INCREF(Py_None);
- return(Py_None);
- }
- xmlTextReaderGetErrorHandler(reader,&f,&arg);
- if (arg != NULL) {
- if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) {
- /* ok, it's our error handler! */
- pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
- Py_XDECREF(pyCtxt->f);
- Py_XDECREF(pyCtxt->arg);
- xmlFree(pyCtxt);
- }
- /*
- * else, something wrong happened, because the error handler is
- * not owned by the python bindings...
- */
- }
- xmlFreeTextReader(reader);
- Py_INCREF(Py_None);
- return(Py_None);
- }
- #endif
- /************************************************************************
- * *
- * XPath extensions *
- * *
- ************************************************************************/
- static void
- libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs)
- {
- PyObject *list, *cur, *result;
- xmlXPathObjectPtr obj;
- xmlXPathContextPtr rctxt;
- PyObject *current_function = NULL;
- const xmlChar *name;
- const xmlChar *ns_uri;
- int i;
- if (ctxt == NULL)
- return;
- rctxt = ctxt->context;
- if (rctxt == NULL)
- return;
- name = rctxt->function;
- ns_uri = rctxt->functionURI;
- #ifdef DEBUG_XPATH
- printf("libxml_xmlXPathFuncCallback called name %s URI %s\n", name,
- ns_uri);
- #endif
- /*
- * Find the function, it should be there it was there at lookup
- */
- for (i = 0; i < libxml_xpathCallbacksNb; i++) {
- if ( /* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */
- (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
- (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
- current_function = (*libxml_xpathCallbacks)[i].function;
- }
- }
- if (current_function == NULL) {
- printf
- ("libxml_xmlXPathFuncCallback: internal error %s not found !\n",
- name);
- return;
- }
- list = PyTuple_New(nargs + 1);
- PyTuple_SetItem(list, 0, libxml_xmlXPathParserContextPtrWrap(ctxt));
- for (i = nargs - 1; i >= 0; i--) {
- obj = valuePop(ctxt);
- cur = libxml_xmlXPathObjectPtrWrap(obj);
- PyTuple_SetItem(list, i + 1, cur);
- }
- result = PyEval_CallObject(current_function, list);
- Py_DECREF(list);
- obj = libxml_xmlXPathObjectPtrConvert(result);
- valuePush(ctxt, obj);
- }
- static xmlXPathFunction
- libxml_xmlXPathFuncLookupFunc(void *ctxt, const xmlChar * name,
- const xmlChar * ns_uri)
- {
- int i;
- #ifdef DEBUG_XPATH
- printf("libxml_xmlXPathFuncLookupFunc(%p, %s, %s) called\n",
- ctxt, name, ns_uri);
- #endif
- /*
- * This is called once only. The address is then stored in the
- * XPath expression evaluation, the proper object to call can
- * then still be found using the execution context function
- * and functionURI fields.
- */
- for (i = 0; i < libxml_xpathCallbacksNb; i++) {
- if ((ctxt == (*libxml_xpathCallbacks)[i].ctx) &&
- (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
- (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
- return (libxml_xmlXPathFuncCallback);
- }
- }
- return (NULL);
- }
- static void
- libxml_xpathCallbacksInitialize(void)
- {
- int i;
- if (libxml_xpathCallbacksInitialized != 0)
- return;
- #ifdef DEBUG_XPATH
- printf("libxml_xpathCallbacksInitialized called\n");
- #endif
- libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlMalloc(
- libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
- for (i = 0; i < libxml_xpathCallbacksAllocd; i++) {
- (*libxml_xpathCallbacks)[i].ctx = NULL;
- (*libxml_xpathCallbacks)[i].name = NULL;
- (*libxml_xpathCallbacks)[i].ns_uri = NULL;
- (*libxml_xpathCallbacks)[i].function = NULL;
- }
- libxml_xpathCallbacksInitialized = 1;
- }
- PyObject *
- libxml_xmlRegisterXPathFunction(ATTRIBUTE_UNUSED PyObject * self,
- PyObject * args)
- {
- PyObject *py_retval;
- int c_retval = 0;
- xmlChar *name;
- xmlChar *ns_uri;
- xmlXPathContextPtr ctx;
- PyObject *pyobj_ctx;
- PyObject *pyobj_f;
- int i;
- if (!PyArg_ParseTuple
- (args, (char *) "OszO:registerXPathFunction", &pyobj_ctx, &name,
- &ns_uri, &pyobj_f))
- return (NULL);
- ctx = (xmlXPathContextPtr) PyxmlXPathContext_Get(pyobj_ctx);
- if (libxml_xpathCallbacksInitialized == 0)
- libxml_xpathCallbacksInitialize();
- xmlXPathRegisterFuncLookup(ctx, libxml_xmlXPathFuncLookupFunc, ctx);
- if ((pyobj_ctx == NULL) || (name == NULL) || (pyobj_f == NULL)) {
- py_retval = libxml_intWrap(-1);
- return (py_retval);
- }
- #ifdef DEBUG_XPATH
- printf("libxml_registerXPathFunction(%p, %s, %s) called\n",
- ctx, name, ns_uri);
- #endif
- for (i = 0; i < libxml_xpathCallbacksNb; i++) {
- if ((ctx == (*libxml_xpathCallbacks)[i].ctx) &&
- (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
- (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
- Py_XINCREF(pyobj_f);
- Py_XDECREF((*libxml_xpathCallbacks)[i].function);
- (*libxml_xpathCallbacks)[i].function = pyobj_f;
- c_retval = 1;
- goto done;
- }
- }
- if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksAllocd) {
- libxml_xpathCallbacksAllocd+=10;
- libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlRealloc(
- libxml_xpathCallbacks,
- libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
- }
- i = libxml_xpathCallbacksNb++;
- Py_XINCREF(pyobj_f);
- (*libxml_xpathCallbacks)[i].ctx = ctx;
- (*libxml_xpathCallbacks)[i].name = xmlStrdup(name);
- (*libxml_xpathCallbacks)[i].ns_uri = xmlStrdup(ns_uri);
- (*libxml_xpathCallbacks)[i].function = pyobj_f;
- c_retval = 1;
-
- done:
- py_retval = libxml_intWrap((int) c_retval);
- return (py_retval);
- }
- /************************************************************************
- * *
- * Global properties access *
- * *
- ************************************************************************/
- static PyObject *
- libxml_name(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- PyObject *resultobj, *obj;
- xmlNodePtr cur;
- const xmlChar *res;
- if (!PyArg_ParseTuple(args, (char *) "O:name", &obj))
- return NULL;
- cur = PyxmlNode_Get(obj);
- #ifdef DEBUG
- printf("libxml_name: cur = %p type %d\n", cur, cur->type);
- #endif
- switch (cur->type) {
- case XML_DOCUMENT_NODE:
- #ifdef LIBXML_DOCB_ENABLED
- case XML_DOCB_DOCUMENT_NODE:
- #endif
- case XML_HTML_DOCUMENT_NODE:{
- xmlDocPtr doc = (xmlDocPtr) cur;
- res = doc->URL;
- break;
- }
- case XML_ATTRIBUTE_NODE:{
- xmlAttrPtr attr = (xmlAttrPtr) cur;
- res = attr->name;
- break;
- }
- case XML_NAMESPACE_DECL:{
- xmlNsPtr ns = (xmlNsPtr) cur;
- res = ns->prefix;
- break;
- }
- default:
- res = cur->name;
- break;
- }
- resultobj = libxml_constxmlCharPtrWrap(res);
- return resultobj;
- }
- static PyObject *
- libxml_doc(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- PyObject *resultobj, *obj;
- xmlNodePtr cur;
- xmlDocPtr res;
- if (!PyArg_ParseTuple(args, (char *) "O:doc", &obj))
- return NULL;
- cur = PyxmlNode_Get(obj);
- #ifdef DEBUG
- printf("libxml_doc: cur = %p\n", cur);
- #endif
- switch (cur->type) {
- case XML_DOCUMENT_NODE:
- #ifdef LIBXML_DOCB_ENABLED
- case XML_DOCB_DOCUMENT_NODE:
- #endif
- case XML_HTML_DOCUMENT_NODE:
- res = NULL;
- break;
- case XML_ATTRIBUTE_NODE:{
- xmlAttrPtr attr = (xmlAttrPtr) cur;
- res = attr->doc;
- break;
- }
- case XML_NAMESPACE_DECL:
- res = NULL;
- break;
- default:
- res = cur->doc;
- break;
- }
- resultobj = libxml_xmlDocPtrWrap(res);
- return resultobj;
- }
- static PyObject *
- libxml_properties(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- PyObject *resultobj, *obj;
- xmlNodePtr cur;
- xmlAttrPtr res;
- if (!PyArg_ParseTuple(args, (char *) "O:properties", &obj))
- return NULL;
- cur = PyxmlNode_Get(obj);
- if ((cur != NULL) && (cur->type == XML_ELEMENT_NODE))
- res = cur->properties;
- else
- res = NULL;
- resultobj = libxml_xmlAttrPtrWrap(res);
- return resultobj;
- }
- static PyObject *
- libxml_next(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- PyObject *resultobj, *obj;
- xmlNodePtr cur;
- xmlNodePtr res;
- if (!PyArg_ParseTuple(args, (char *) "O:next", &obj))
- return NULL;
- cur = PyxmlNode_Get(obj);
- #ifdef DEBUG
- printf("libxml_next: cur = %p\n", cur);
- #endif
- switch (cur->type) {
- case XML_DOCUMENT_NODE:
- #ifdef LIBXML_DOCB_ENABLED
- case XML_DOCB_DOCUMENT_NODE:
- #endif
- case XML_HTML_DOCUMENT_NODE:
- res = NULL;
- break;
- case XML_ATTRIBUTE_NODE:{
- xmlAttrPtr attr = (xmlAttrPtr) cur;
- res = (xmlNodePtr) attr->next;
- break;
- }
- case XML_NAMESPACE_DECL:{
- xmlNsPtr ns = (xmlNsPtr) cur;
- res = (xmlNodePtr) ns->next;
- break;
- }
- default:
- res = cur->next;
- break;
- }
- resultobj = libxml_xmlNodePtrWrap(res);
- return resultobj;
- }
- static PyObject *
- libxml_prev(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- PyObject *resultobj, *obj;
- xmlNodePtr cur;
- xmlNodePtr res;
- if (!PyArg_ParseTuple(args, (char *) "O:prev", &obj))
- return NULL;
- cur = PyxmlNode_Get(obj);
- #ifdef DEBUG
- printf("libxml_prev: cur = %p\n", cur);
- #endif
- switch (cur->type) {
- case XML_DOCUMENT_NODE:
- #ifdef LIBXML_DOCB_ENABLED
- case XML_DOCB_DOCUMENT_NODE:
- #endif
- case XML_HTML_DOCUMENT_NODE:
- res = NULL;
- break;
- case XML_ATTRIBUTE_NODE:{
- xmlAttrPtr attr = (xmlAttrPtr) cur;
- res = (xmlNodePtr) attr->prev;
- }
- break;
- case XML_NAMESPACE_DECL:
- res = NULL;
- break;
- default:
- res = cur->prev;
- break;
- }
- resultobj = libxml_xmlNodePtrWrap(res);
- return resultobj;
- }
- static PyObject *
- libxml_children(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- PyObject *resultobj, *obj;
- xmlNodePtr cur;
- xmlNodePtr res;
- if (!PyArg_ParseTuple(args, (char *) "O:children", &obj))
- return NULL;
- cur = PyxmlNode_Get(obj);
- #ifdef DEBUG
- printf("libxml_children: cur = %p\n", cur);
- #endif
- switch (cur->type) {
- case XML_ELEMENT_NODE:
- case XML_ENTITY_REF_NODE:
- case XML_ENTITY_NODE:
- case XML_PI_NODE:
- case XML_COMMENT_NODE:
- case XML_DOCUMENT_NODE:
- #ifdef LIBXML_DOCB_ENABLED
- case XML_DOCB_DOCUMENT_NODE:
- #endif
- case XML_HTML_DOCUMENT_NODE:
- case XML_DTD_NODE:
- res = cur->children;
- break;
- case XML_ATTRIBUTE_NODE:{
- xmlAttrPtr attr = (xmlAttrPtr) cur;
- res = attr->children;
- break;
- }
- default:
- res = NULL;
- break;
- }
- resultobj = libxml_xmlNodePtrWrap(res);
- return resultobj;
- }
- static PyObject *
- libxml_last(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- PyObject *resultobj, *obj;
- xmlNodePtr cur;
- xmlNodePtr res;
- if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
- return NULL;
- cur = PyxmlNode_Get(obj);
- #ifdef DEBUG
- printf("libxml_last: cur = %p\n", cur);
- #endif
- switch (cur->type) {
- case XML_ELEMENT_NODE:
- case XML_ENTITY_REF_NODE:
- case XML_ENTITY_NODE:
- case XML_PI_NODE:
- case XML_COMMENT_NODE:
- case XML_DOCUMENT_NODE:
- #ifdef LIBXML_DOCB_ENABLED
- case XML_DOCB_DOCUMENT_NODE:
- #endif
- case XML_HTML_DOCUMENT_NODE:
- case XML_DTD_NODE:
- res = cur->last;
- break;
- case XML_ATTRIBUTE_NODE:{
- xmlAttrPtr attr = (xmlAttrPtr) cur;
- res = attr->last;
- }
- default:
- res = NULL;
- break;
- }
- resultobj = libxml_xmlNodePtrWrap(res);
- return resultobj;
- }
- static PyObject *
- libxml_parent(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- PyObject *resultobj, *obj;
- xmlNodePtr cur;
- xmlNodePtr res;
- if (!PyArg_ParseTuple(args, (char *) "O:parent", &obj))
- return NULL;
- cur = PyxmlNode_Get(obj);
- #ifdef DEBUG
- printf("libxml_parent: cur = %p\n", cur);
- #endif
- switch (cur->type) {
- case XML_DOCUMENT_NODE:
- case XML_HTML_DOCUMENT_NODE:
- #ifdef LIBXML_DOCB_ENABLED
- case XML_DOCB_DOCUMENT_NODE:
- #endif
- res = NULL;
- break;
- case XML_ATTRIBUTE_NODE:{
- xmlAttrPtr attr = (xmlAttrPtr) cur;
- res = attr->parent;
- }
- break;
- case XML_ENTITY_DECL:
- case XML_NAMESPACE_DECL:
- case XML_XINCLUDE_START:
- case XML_XINCLUDE_END:
- res = NULL;
- break;
- default:
- res = cur->parent;
- break;
- }
- resultobj = libxml_xmlNodePtrWrap(res);
- return resultobj;
- }
- static PyObject *
- libxml_type(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- PyObject *resultobj, *obj;
- xmlNodePtr cur;
- const xmlChar *res = NULL;
- if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
- return NULL;
- cur = PyxmlNode_Get(obj);
- #ifdef DEBUG
- printf("libxml_type: cur = %p\n", cur);
- #endif
- switch (cur->type) {
- case XML_ELEMENT_NODE:
- res = (const xmlChar *) "element";
- break;
- case XML_ATTRIBUTE_NODE:
- res = (const xmlChar *) "attribute";
- break;
- case XML_TEXT_NODE:
- res = (const xmlChar *) "text";
- break;
- case XML_CDATA_SECTION_NODE:
- res = (const xmlChar *) "cdata";
- break;
- case XML_ENTITY_REF_NODE:
- res = (const xmlChar *) "entity_ref";
- break;
- case XML_ENTITY_NODE:
- res = (const xmlChar *) "entity";
- break;
- case XML_PI_NODE:
- res = (const xmlChar *) "pi";
- break;
- case XML_COMMENT_NODE:
- res = (const xmlChar *) "comment";
- break;
- case XML_DOCUMENT_NODE:
- res = (const xmlChar *) "document_xml";
- break;
- case XML_DOCUMENT_TYPE_NODE:
- res = (const xmlChar *) "doctype";
- break;
- case XML_DOCUMENT_FRAG_NODE:
- res = (const xmlChar *) "fragment";
- break;
- case XML_NOTATION_NODE:
- res = (const xmlChar *) "notation";
- break;
- case XML_HTML_DOCUMENT_NODE:
- res = (const xmlChar *) "document_html";
- break;
- case XML_DTD_NODE:
- res = (const xmlChar *) "dtd";
- break;
- case XML_ELEMENT_DECL:
- res = (const xmlChar *) "elem_decl";
- break;
- case XML_ATTRIBUTE_DECL:
- res = (const xmlChar *) "attribute_decl";
- break;
- case XML_ENTITY_DECL:
- res = (const xmlChar *) "entity_decl";
- break;
- case XML_NAMESPACE_DECL:
- res = (const xmlChar *) "namespace";
- break;
- case XML_XINCLUDE_START:
- res = (const xmlChar *) "xinclude_start";
- break;
- case XML_XINCLUDE_END:
- res = (const xmlChar *) "xinclude_end";
- break;
- #ifdef LIBXML_DOCB_ENABLED
- case XML_DOCB_DOCUMENT_NODE:
- res = (const xmlChar *) "document_docbook";
- break;
- #endif
- }
- #ifdef DEBUG
- printf("libxml_type: cur = %p: %s\n", cur, res);
- #endif
- resultobj = libxml_constxmlCharPtrWrap(res);
- return resultobj;
- }
- /************************************************************************
- * *
- * Specific accessor functions *
- * *
- ************************************************************************/
- PyObject *
- libxml_xmlNodeGetNsDefs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- PyObject *py_retval;
- xmlNsPtr c_retval;
- xmlNodePtr node;
- PyObject *pyobj_node;
- if (!PyArg_ParseTuple
- (args, (char *) "O:xmlNodeGetNsDefs", &pyobj_node))
- return (NULL);
- node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
- if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- c_retval = node->nsDef;
- py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
- return (py_retval);
- }
- PyObject *
- libxml_xmlNodeRemoveNsDef(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- PyObject *py_retval;
- xmlNsPtr ns, prev;
- xmlNodePtr node;
- PyObject *pyobj_node;
- xmlChar *href;
- xmlNsPtr c_retval;
-
- if (!PyArg_ParseTuple
- (args, (char *) "Oz:xmlNodeRemoveNsDef", &pyobj_node, &href))
- return (NULL);
- node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
- ns = NULL;
- if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- if (href == NULL) {
- ns = node->nsDef;
- node->nsDef = NULL;
- c_retval = 0;
- }
- else {
- prev = NULL;
- ns = node->nsDef;
- while (ns != NULL) {
- if (xmlStrEqual(ns->href, href)) {
- if (prev != NULL)
- prev->next = ns->next;
- else
- node->nsDef = ns->next;
- ns->next = NULL;
- c_retval = 0;
- break;
- }
- prev = ns;
- ns = ns->next;
- }
- }
- c_retval = ns;
- py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
- return (py_retval);
- }
- PyObject *
- libxml_xmlNodeGetNs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- PyObject *py_retval;
- xmlNsPtr c_retval;
- xmlNodePtr node;
- PyObject *pyobj_node;
- if (!PyArg_ParseTuple(args, (char *) "O:xmlNodeGetNs", &pyobj_node))
- return (NULL);
- node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
- if ((node == NULL) ||
- ((node->type != XML_ELEMENT_NODE) &&
- (node->type != XML_ATTRIBUTE_NODE))) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- c_retval = node->ns;
- py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
- return (py_retval);
- }
- #ifdef LIBXML_OUTPUT_ENABLED
- /************************************************************************
- * *
- * Serialization front-end *
- * *
- ************************************************************************/
- static PyObject *
- libxml_serializeNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- PyObject *py_retval = NULL;
- xmlChar *c_retval;
- PyObject *pyobj_node;
- xmlNodePtr node;
- xmlDocPtr doc;
- const char *encoding;
- int format;
- int len;
- xmlSaveCtxtPtr ctxt;
- xmlBufferPtr buf;
- int options = 0;
- if (!PyArg_ParseTuple(args, (char *) "Ozi:serializeNode", &pyobj_node,
- &encoding, &format))
- return (NULL);
- node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
- if (node == NULL) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- if (node->type == XML_DOCUMENT_NODE) {
- doc = (xmlDocPtr) node;
- node = NULL;
- #ifdef LIBXML_HTML_ENABLED
- } else if (node->type == XML_HTML_DOCUMENT_NODE) {
- doc = (xmlDocPtr) node;
- node = NULL;
- #endif
- } else {
- if (node->type == XML_NAMESPACE_DECL)
- doc = NULL;
- else
- doc = node->doc;
- if ((doc == NULL) || (doc->type == XML_DOCUMENT_NODE)) {
- #ifdef LIBXML_HTML_ENABLED
- } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
- #endif /* LIBXML_HTML_ENABLED */
- } else {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- }
- buf = xmlBufferCreate();
- if (buf == NULL) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- if (format) options |= XML_SAVE_FORMAT;
- ctxt = xmlSaveToBuffer(buf, encoding, options);
- if (ctxt == NULL) {
- xmlBufferFree(buf);
- Py_INCREF(Py_None);
- return (Py_None);
- }
- if (node == NULL)
- xmlSaveDoc(ctxt, doc);
- else
- xmlSaveTree(ctxt, node);
- xmlSaveClose(ctxt);
- c_retval = buf->content;
- buf->content = NULL;
- xmlBufferFree(buf);
- py_retval = libxml_charPtrWrap((char *) c_retval);
- return (py_retval);
- }
- static PyObject *
- libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- PyObject *py_file = NULL;
- FILE *output;
- PyObject *pyobj_node;
- xmlNodePtr node;
- xmlDocPtr doc;
- const char *encoding;
- int format;
- int len;
- xmlOutputBufferPtr buf;
- xmlCharEncodingHandlerPtr handler = NULL;
- if (!PyArg_ParseTuple(args, (char *) "OOzi:serializeNode", &pyobj_node,
- &py_file, &encoding, &format))
- return (NULL);
- node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
- if (node == NULL) {
- return (PyInt_FromLong((long) -1));
- }
- if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
- return (PyInt_FromLong((long) -1));
- }
- output = PyFile_AsFile(py_file);
- if (output == NULL) {
- return (PyInt_FromLong((long) -1));
- }
- if (node->type == XML_DOCUMENT_NODE) {
- doc = (xmlDocPtr) node;
- } else if (node->type == XML_HTML_DOCUMENT_NODE) {
- doc = (xmlDocPtr) node;
- } else {
- doc = node->doc;
- }
- #ifdef LIBXML_HTML_ENABLED
- if (doc->type == XML_HTML_DOCUMENT_NODE) {
- if (encoding == NULL)
- encoding = (const char *) htmlGetMetaEncoding(doc);
- }
- #endif /* LIBXML_HTML_ENABLED */
- if (encoding != NULL) {
- handler = xmlFindCharEncodingHandler(encoding);
- if (handler == NULL) {
- return (PyInt_FromLong((long) -1));
- }
- }
- if (doc->type == XML_HTML_DOCUMENT_NODE) {
- if (handler == NULL)
- handler = xmlFindCharEncodingHandler("HTML");
- if (handler == NULL)
- handler = xmlFindCharEncodingHandler("ascii");
- }
- buf = xmlOutputBufferCreateFile(output, handler);
- if (node->type == XML_DOCUMENT_NODE) {
- len = xmlSaveFormatFileTo(buf, doc, encoding, format);
- #ifdef LIBXML_HTML_ENABLED
- } else if (node->type == XML_HTML_DOCUMENT_NODE) {
- htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
- len = xmlOutputBufferClose(buf);
- } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
- htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
- len = xmlOutputBufferClose(buf);
- #endif /* LIBXML_HTML_ENABLED */
- } else {
- xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
- len = xmlOutputBufferClose(buf);
- }
- return (PyInt_FromLong((long) len));
- }
- #endif /* LIBXML_OUTPUT_ENABLED */
- /************************************************************************
- * *
- * Extra stuff *
- * *
- ************************************************************************/
- PyObject *
- libxml_xmlNewNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- PyObject *py_retval;
- xmlChar *name;
- xmlNodePtr node;
- if (!PyArg_ParseTuple(args, (char *) "s:xmlNewNode", &name))
- return (NULL);
- node = (xmlNodePtr) xmlNewNode(NULL, name);
- #ifdef DEBUG
- printf("NewNode: %s : %p\n", name, (void *) node);
- #endif
- if (node == NULL) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- py_retval = libxml_xmlNodePtrWrap(node);
- return (py_retval);
- }
- /************************************************************************
- * *
- * Local Catalog stuff *
- * *
- ************************************************************************/
- static PyObject *
- libxml_addLocalCatalog(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- xmlChar *URL;
- xmlParserCtxtPtr ctxt;
- PyObject *pyobj_ctxt;
- if (!PyArg_ParseTuple(args, (char *)"Os:addLocalCatalog", &pyobj_ctxt, &URL))
- return(NULL);
- ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
- if (URL != NULL) {
- ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL);
- }
- #ifdef DEBUG
- printf("LocalCatalog: %s\n", URL);
- #endif
- Py_INCREF(Py_None);
- return (Py_None);
- }
- #ifdef LIBXML_SCHEMAS_ENABLED
- /************************************************************************
- * *
- * RelaxNG error handler registration *
- * *
- ************************************************************************/
- typedef struct
- {
- PyObject *warn;
- PyObject *error;
- PyObject *arg;
- } xmlRelaxNGValidCtxtPyCtxt;
- typedef xmlRelaxNGValidCtxtPyCtxt *xmlRelaxNGValidCtxtPyCtxtPtr;
- static void
- libxml_xmlRelaxNGValidityGenericErrorFuncHandler(void *ctx, char *str)
- {
- PyObject *list;
- PyObject *result;
- xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
-
- #ifdef DEBUG_ERROR
- printf("libxml_xmlRelaxNGValidityGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
- #endif
- pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx;
- list = PyTuple_New(2);
- PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
- PyTuple_SetItem(list, 1, pyCtxt->arg);
- Py_XINCREF(pyCtxt->arg);
- result = PyEval_CallObject(pyCtxt->error, list);
- if (result == NULL)
- {
- /* TODO: manage for the exception to be propagated... */
- PyErr_Print();
- }
- Py_XDECREF(list);
- Py_XDECREF(result);
- }
- static void
- libxml_xmlRelaxNGValidityGenericWarningFuncHandler(void *ctx, char *str)
- {
- PyObject *list;
- PyObject *result;
- xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
-
- #ifdef DEBUG_ERROR
- printf("libxml_xmlRelaxNGValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str);
- #endif
- pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx;
- list = PyTuple_New(2);
- PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
- PyTuple_SetItem(list, 1, pyCtxt->arg);
- Py_XINCREF(pyCtxt->arg);
- result = PyEval_CallObject(pyCtxt->warn, list);
- if (result == NULL)
- {
- /* TODO: manage for the exception to be propagated... */
- PyErr_Print();
- }
- Py_XDECREF(list);
- Py_XDECREF(result);
- }
- static void
- libxml_xmlRelaxNGValidityErrorFunc(void *ctx, const char *msg, ...)
- {
- va_list ap;
- va_start(ap, msg);
- libxml_xmlRelaxNGValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap));
- va_end(ap);
- }
- static void
- libxml_xmlRelaxNGValidityWarningFunc(void *ctx, const char *msg, ...)
- {
- va_list ap;
- va_start(ap, msg);
- libxml_xmlRelaxNGValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap));
- va_end(ap);
- }
- static PyObject *
- libxml_xmlRelaxNGSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- PyObject *py_retval;
- PyObject *pyobj_error;
- PyObject *pyobj_warn;
- PyObject *pyobj_ctx;
- PyObject *pyobj_arg = Py_None;
- xmlRelaxNGValidCtxtPtr ctxt;
- xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
- if (!PyArg_ParseTuple
- (args, (char *) "OOO|O:xmlRelaxNGSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
- return (NULL);
- #ifdef DEBUG_ERROR
- printf("libxml_xmlRelaxNGSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
- #endif
- ctxt = PyrelaxNgValidCtxt_Get(pyobj_ctx);
- if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1)
- {
- py_retval = libxml_intWrap(-1);
- return(py_retval);
- }
-
- if (pyCtxt == NULL)
- {
- /* first time to set the error handlers */
- pyCtxt = xmlMalloc(sizeof(xmlRelaxNGValidCtxtPyCtxt));
- if (pyCtxt == NULL) {
- py_retval = libxml_intWrap(-1);
- return(py_retval);
- }
- memset(pyCtxt, 0, sizeof(xmlRelaxNGValidCtxtPyCtxt));
- }
-
- /* TODO: check warn and error is a function ! */
- Py_XDECREF(pyCtxt->error);
- Py_XINCREF(pyobj_error);
- pyCtxt->error = pyobj_error;
-
- Py_XDECREF(pyCtxt->warn);
- Py_XINCREF(pyobj_warn);
- pyCtxt->warn = pyobj_warn;
-
- Py_XDECREF(pyCtxt->arg);
- Py_XINCREF(pyobj_arg);
- pyCtxt->arg = pyobj_arg;
- xmlRelaxNGSetValidErrors(ctxt, &libxml_xmlRelaxNGValidityErrorFunc, &libxml_xmlRelaxNGValidityWarningFunc, pyCtxt);
- py_retval = libxml_intWrap(1);
- return (py_retval);
- }
- static PyObject *
- libxml_xmlRelaxNGFreeValidCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
- xmlRelaxNGValidCtxtPtr ctxt;
- xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
- PyObject *pyobj_ctxt;
- if (!PyArg_ParseTuple(args, (char *)"O:xmlRelaxNGFreeValidCtxt", &pyobj_ctxt))
- return(NULL);
- ctxt = (xmlRelaxNGValidCtxtPtr) PyrelaxNgValidCtxt_Get(pyobj_ctxt);
- if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0)
- {
- if (pyCtxt != NULL)
- {
- Py_XDECREF(pyCtxt->error);
- Py_XDECREF(pyCtxt->warn);
- Py_XDECREF(pyCtxt->arg);
- xmlFree(pyCtxt);
- }
- }
-
- xmlRelaxNGFreeValidCtxt(ctxt);
- Py_INCREF(Py_None);
- return(Py_None);
- }
- typedef struct
- {
- PyObject *warn;
- PyObject *error;
- PyObject *arg;
- } xmlSchemaValidCtxtPyCtxt;
- typedef xmlSchemaValidCtxtPyCtxt *xmlSchemaValidCtxtPyCtxtPtr;
- static void
- libxml_xmlSchemaValidityGenericErrorFuncHandler(void *ctx, char *str)
- {
- PyObject *list;
- PyObject *result;
- xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
- #ifdef DEBUG_ERROR
- printf("libxml_xmlSchemaValiditiyGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
- #endif
- pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx;
- list = PyTuple_New(2);
- PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
- PyTuple_SetItem(list, 1, pyCtxt->arg);
- Py_XINCREF(pyCtxt->arg);
- result = PyEval_CallObject(pyCtxt->error, list);
- if (result == NULL)
- {
- /* TODO: manage for the exception to be propagated... */
- PyErr_Print();
- }
- Py_XDECREF(list);
- Py_XDECREF(result);
- }
- static void
- libxml_xmlSchemaValidityGenericWarningFuncHandler(void *ctx, char *str)
- {
- PyObject *list;
- PyObject *result;
- xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
- #ifdef DEBUG_ERROR
- printf("libxml_xmlSchemaValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str);
- #endif
-
- pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx;
- list = PyTuple_New(2);
- PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
- PyTuple_SetItem(list, 1, pyCtxt->arg);
- Py_XINCREF(pyCtxt->arg);
- result = PyEval_CallObject(pyCtxt->warn, list);
- if (result == NULL)
- {
- /* TODO: manage for the exception to be propagated... */
- PyErr_Print();
- }
- Py_XDECREF(list);
- Py_XDECREF(result);
- }
- static void
- libxml_xmlSchemaValidityErrorFunc(void *ctx, const char *msg, ...)
- {
- va_list ap;
-
- va_start(ap, msg);
- libxml_xmlSchemaValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap));
- va_end(ap);
- }
- static void
- libxml_xmlSchemaValidityWarningFunc(void *ctx, const char *msg, ...)
- {
- va_list ap;
- va_start(ap, msg);
- libxml_xmlSchemaValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap));
- va_end(ap);
- }
- PyObject *
- libxml_xmlSchemaSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- PyObject *py_retval;
- PyObject *pyobj_error;
- PyObject *pyobj_warn;
- PyObject *pyobj_ctx;
- PyObject *pyobj_arg = Py_None;
- xmlSchemaValidCtxtPtr ctxt;
- xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
- if (!PyArg_ParseTuple
- (args, (char *) "OOO|O:xmlSchemaSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
- return (NULL);
- #ifdef DEBUG_ERROR
- printf("libxml_xmlSchemaSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
- #endif
- ctxt = PySchemaValidCtxt_Get(pyobj_ctx);
- if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1)
- {
- py_retval = libxml_intWrap(-1);
- return(py_retval);
- }
- if (pyCtxt == NULL)
- {
- /* first time to set the error handlers */
- pyCtxt = xmlMalloc(sizeof(xmlSchemaValidCtxtPyCtxt));
- if (pyCtxt == NULL) {
- py_retval = libxml_intWrap(-1);
- return(py_retval);
- }
- memset(pyCtxt, 0, sizeof(xmlSchemaValidCtxtPyCtxt));
- }
- /* TODO: check warn and error is a function ! */
- Py_XDECREF(pyCtxt->error);
- Py_XINCREF(pyobj_error);
- pyCtxt->error = pyobj_error;
- Py_XDECREF(pyCtxt->warn);
- Py_XINCREF(pyobj_warn);
- pyCtxt->warn = pyobj_warn;
- Py_XDECREF(pyCtxt->arg);
- Py_XINCREF(pyobj_arg);
- pyCtxt->arg = pyobj_arg;
- xmlSchemaSetValidErrors(ctxt, &libxml_xmlSchemaValidityErrorFunc, &libxml_xmlSchemaValidityWarningFunc, pyCtxt);
- py_retval = libxml_intWrap(1);
- return(py_retval);
- }
- static PyObject *
- libxml_xmlSchemaFreeValidCtxt(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
- {
- xmlSchemaValidCtxtPtr ctxt;
- xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
- PyObject *pyobj_ctxt;
- if (!PyArg_ParseTuple(args, (char *)"O:xmlSchemaFreeValidCtxt", &pyobj_ctxt))
- return(NULL);
- ctxt = (xmlSchemaValidCtxtPtr) PySchemaValidCtxt_Get(pyobj_ctxt);
- if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0)
- {
- if (pyCtxt != NULL)
- {
- Py_XDECREF(pyCtxt->error);
- Py_XDECREF(pyCtxt->warn);
- Py_XDECREF(pyCtxt->arg);
- xmlFree(pyCtxt);
- }
- }
- xmlSchemaFreeValidCtxt(ctxt);
- Py_INCREF(Py_None);
- return(Py_None);
- }
- #endif
- #ifdef LIBXML_C14N_ENABLED
- #ifdef LIBXML_OUTPUT_ENABLED
- /************************************************************************
- * *
- * XML Canonicalization c14n *
- * *
- ************************************************************************/
- static int
- PyxmlNodeSet_Convert(PyObject *py_nodeset, xmlNodeSetPtr *result)
- {
- xmlNodeSetPtr nodeSet;
- int is_tuple = 0;
- if (PyTuple_Check(py_nodeset))
- is_tuple = 1;
- else if (PyList_Check(py_nodeset))
- is_tuple = 0;
- else if (py_nodeset == Py_None) {
- *result = NULL;
- return 0;
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "must be a tuple or list of nodes.");
- return -1;
- }
- nodeSet = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet));
- if (nodeSet == NULL) {
- PyErr_SetString(PyExc_MemoryError, "");
- return -1;
- }
- nodeSet->nodeNr = 0;
- nodeSet->nodeMax = (is_tuple
- ? PyTuple_GET_SIZE(py_nodeset)
- : PyList_GET_SIZE(py_nodeset));
- nodeSet->nodeTab
- = (xmlNodePtr *) xmlMalloc (nodeSet->nodeMax
- * sizeof(xmlNodePtr));
- if (nodeSet->nodeTab == NULL) {
- xmlFree(nodeSet);
- PyErr_SetString(PyExc_MemoryError, "");
- return -1;
- }
- memset(nodeSet->nodeTab, 0 ,
- nodeSet->nodeMax * sizeof(xmlNodePtr));
- {
- int idx;
- for (idx=0; idx < nodeSet->nodeMax; ++idx) {
- xmlNodePtr pynode =
- PyxmlNode_Get (is_tuple
- ? PyTuple_GET_ITEM(py_nodeset, idx)
- : PyList_GET_ITEM(py_nodeset, idx));
- if (pynode)
- nodeSet->nodeTab[nodeSet->nodeNr++] = pynode;
- }
- }
- *result = nodeSet;
- return 0;
- }
- static int
- PystringSet_Convert(PyObject *py_strings, xmlChar *** result)
- {
- /* NOTE: the array should be freed, but the strings are shared
- with the python strings and so must not be freed. */
- xmlChar ** strings;
- int is_tuple = 0;
- int count;
- int init_index = 0;
- if (PyTuple_Check(py_strings))
- is_tuple = 1;
- else if (PyList_Check(py_strings))
- is_tuple = 0;
- else if (py_strings == Py_None) {
- *result = NULL;
- return 0;
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "must be a tuple or list of strings.");
- return -1;
- }
- count = (is_tuple
- ? PyTuple_GET_SIZE(py_strings)
- : PyList_GET_SIZE(py_strings));
- strings = (xmlChar **) xmlMalloc(sizeof(xmlChar *) * count);
- if (strings == NULL) {
- PyErr_SetString(PyExc_MemoryError, "");
- return -1;
- }
- memset(strings, 0 , sizeof(xmlChar *) * count);
- {
- int idx;
- for (idx=0; idx < count; ++idx) {
- char* s = PyString_AsString
- (is_tuple
- ? PyTuple_GET_ITEM(py_strings, idx)
- : PyList_GET_ITEM(py_strings, idx));
- if (s)
- strings[init_index++] = (xmlChar *)s;
- else {
- xmlFree(strings);
- PyErr_SetString(PyExc_TypeError,
- "must be a tuple or list of strings.");
- return -1;
- }
- }
- }
- *result = strings;
- return 0;
- }
- static PyObject *
- libxml_C14NDocDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
- PyObject * args)
- {
- PyObject *py_retval = NULL;
- PyObject *pyobj_doc;
- PyObject *pyobj_nodes;
- int exclusive;
- PyObject *pyobj_prefixes;
- int with_comments;
- xmlDocPtr doc;
- xmlNodeSetPtr nodes;
- xmlChar **prefixes = NULL;
- xmlChar *doc_txt;
- int result;
- if (!PyArg_ParseTuple(args, (char *) "OOiOi:C14NDocDumpMemory",
- &pyobj_doc,
- &pyobj_nodes,
- &exclusive,
- &pyobj_prefixes,
- &with_comments))
- return (NULL);
- doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
- if (!doc) {
- PyErr_SetString(PyExc_TypeError, "bad document.");
- return NULL;
- }
- result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes);
- if (result < 0) return NULL;
- if (exclusive) {
- result = PystringSet_Convert(pyobj_prefixes, &prefixes);
- if (result < 0) {
- if (nodes) {
- xmlFree(nodes->nodeTab);
- xmlFree(nodes);
- }
- return NULL;
- }
- }
- result = xmlC14NDocDumpMemory(doc,
- nodes,
- exclusive,
- prefixes,
- with_comments,
- &doc_txt);
- if (nodes) {
- xmlFree(nodes->nodeTab);
- xmlFree(nodes);
- }
- if (prefixes) {
- xmlChar ** idx = prefixes;
- while (*idx) xmlFree(*(idx++));
- xmlFree(prefixes);
- }
- if (result < 0) {
- PyErr_SetString(PyExc_Exception,
- "libxml2 xmlC14NDocDumpMemory failure.");
- return NULL;
- }
- else {
- py_retval = PyString_FromStringAndSize((const char *) doc_txt,
- result);
- xmlFree(doc_txt);
- return py_retval;
- }
- }
- static PyObject *
- libxml_C14NDocSaveTo(ATTRIBUTE_UNUSED PyObject * self,
- PyObject * args)
- {
- PyObject *pyobj_doc;
- PyObject *py_file;
- PyObject *pyobj_nodes;
- int exclusive;
- PyObject *pyobj_prefixes;
- int with_comments;
- xmlDocPtr doc;
- xmlNodeSetPtr nodes;
- xmlChar **prefixes = NULL;
- FILE * output;
- xmlOutputBufferPtr buf;
- int result;
- int len;
- if (!PyArg_ParseTuple(args, (char *) "OOiOiO:C14NDocSaveTo",
- &pyobj_doc,
- &pyobj_nodes,
- &exclusive,
- &pyobj_prefixes,
- &with_comments,
- &py_file))
- return (NULL);
- doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
- if (!doc) {
- PyErr_SetString(PyExc_TypeError, "bad document.");
- return NULL;
- }
- if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
- PyErr_SetString(PyExc_TypeError, "bad file.");
- return NULL;
- }
- output = PyFile_AsFile(py_file);
- if (output == NULL) {
- PyErr_SetString(PyExc_TypeError, "bad file.");
- return NULL;
- }
- buf = xmlOutputBufferCreateFile(output, NULL);
- result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes);
- if (result < 0) return NULL;
- if (exclusive) {
- result = PystringSet_Convert(pyobj_prefixes, &prefixes);
- if (result < 0) {
- if (nodes) {
- xmlFree(nodes->nodeTab);
- xmlFree(nodes);
- }
- return NULL;
- }
- }
- result = xmlC14NDocSaveTo(doc,
- nodes,
- exclusive,
- prefixes,
- with_comments,
- buf);
- if (nodes) {
- xmlFree(nodes->nodeTab);
- xmlFree(nodes);
- }
- if (prefixes) {
- xmlChar ** idx = prefixes;
- while (*idx) xmlFree(*(idx++));
- xmlFree(prefixes);
- }
- len = xmlOutputBufferClose(buf);
- if (result < 0) {
- PyErr_SetString(PyExc_Exception,
- "libxml2 xmlC14NDocSaveTo failure.");
- return NULL;
- }
- else
- return PyInt_FromLong((long) len);
- }
- #endif
- #endif
- static PyObject *
- libxml_getObjDesc(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
- PyObject *obj;
- char *str;
- if (!PyArg_ParseTuple(args, (char *)"O:getObjDesc", &obj))
- return NULL;
- str = PyCObject_GetDesc(obj);
- return Py_BuildValue((char *)"s", str);
- }
- static PyObject *
- libxml_compareNodesEqual(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
-
- PyObject *py_node1, *py_node2;
- xmlNodePtr node1, node2;
- if (!PyArg_ParseTuple(args, (char *)"OO:compareNodesEqual",
- &py_node1, &py_node2))
- return NULL;
- /* To compare two node objects, we compare their pointer addresses */
- node1 = PyxmlNode_Get(py_node1);
- node2 = PyxmlNode_Get(py_node2);
- if ( node1 == node2 )
- return Py_BuildValue((char *)"i", 1);
- else
- return Py_BuildValue((char *)"i", 0);
-
- }
- static PyObject *
- libxml_nodeHash(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
- PyObject *py_node1;
- xmlNodePtr node1;
- if (!PyArg_ParseTuple(args, (char *)"O:nodeHash", &py_node1))
- return NULL;
- /* For simplicity, we use the node pointer address as a hash value */
- node1 = PyxmlNode_Get(py_node1);
- return PyLong_FromVoidPtr(node1);
- }
- /************************************************************************
- * *
- * The registration stuff *
- * *
- ************************************************************************/
- static PyMethodDef libxmlMethods[] = {
- #include "libxml2-export.c"
- {(char *) "name", libxml_name, METH_VARARGS, NULL},
- {(char *) "children", libxml_children, METH_VARARGS, NULL},
- {(char *) "properties", libxml_properties, METH_VARARGS, NULL},
- {(char *) "last", libxml_last, METH_VARARGS, NULL},
- {(char *) "prev", libxml_prev, METH_VARARGS, NULL},
- {(char *) "next", libxml_next, METH_VARARGS, NULL},
- {(char *) "parent", libxml_parent, METH_VARARGS, NULL},
- {(char *) "type", libxml_type, METH_VARARGS, NULL},
- {(char *) "doc", libxml_doc, METH_VARARGS, NULL},
- {(char *) "xmlNewNode", libxml_xmlNewNode, METH_VARARGS, NULL},
- {(char *) "xmlNodeRemoveNsDef", libxml_xmlNodeRemoveNsDef, METH_VARARGS, NULL},
- {(char *)"xmlSetValidErrors", libxml_xmlSetValidErrors, METH_VARARGS, NULL},
- {(char *)"xmlFreeValidCtxt", libxml_xmlFreeValidCtxt, METH_VARARGS, NULL},
- #ifdef LIBXML_OUTPUT_ENABLED
- {(char *) "serializeNode", libxml_serializeNode, METH_VARARGS, NULL},
- {(char *) "saveNodeTo", libxml_saveNodeTo, METH_VARARGS, NULL},
- {(char *) "outputBufferCreate", libxml_xmlCreateOutputBuffer, METH_VARARGS, NULL},
- {(char *) "outputBufferGetPythonFile", libxml_outputBufferGetPythonFile, METH_VARARGS, NULL},
- {(char *) "xmlOutputBufferClose", libxml_xmlOutputBufferClose, METH_VARARGS, NULL},
- { (char *)"xmlOutputBufferFlush", libxml_xmlOutputBufferFlush, METH_VARARGS, NULL },
- { (char *)"xmlSaveFileTo", libxml_xmlSaveFileTo, METH_VARARGS, NULL },
- { (char *)"xmlSaveFormatFileTo", libxml_xmlSaveFormatFileTo, METH_VARARGS, NULL },
- #endif /* LIBXML_OUTPUT_ENABLED */
- {(char *) "inputBufferCreate", libxml_xmlCreateInputBuffer, METH_VARARGS, NULL},
- {(char *) "setEntityLoader", libxml_xmlSetEntityLoader, METH_VARARGS, NULL},
- {(char *)"xmlRegisterErrorHandler", libxml_xmlRegisterErrorHandler, METH_VARARGS, NULL },
- {(char *)"xmlParserCtxtSetErrorHandler", libxml_xmlParserCtxtSetErrorHandler, METH_VARARGS, NULL },
- {(char *)"xmlParserCtxtGetErrorHandler", libxml_xmlParserCtxtGetErrorHandler, METH_VARARGS, NULL },
- {(char *)"xmlFreeParserCtxt", libxml_xmlFreeParserCtxt, METH_VARARGS, NULL },
- #ifdef LIBXML_READER_ENABLED
- {(char *)"xmlTextReaderSetErrorHandler", libxml_xmlTextReaderSetErrorHandler, METH_VARARGS, NULL },
- {(char *)"xmlTextReaderGetErrorHandler", libxml_xmlTextReaderGetErrorHandler, METH_VARARGS, NULL },
- {(char *)"xmlFreeTextReader", libxml_xmlFreeTextReader, METH_VARARGS, NULL },
- #endif
- {(char *)"addLocalCatalog", libxml_addLocalCatalog, METH_VARARGS, NULL },
- #ifdef LIBXML_SCHEMAS_ENABLED
- {(char *)"xmlRelaxNGSetValidErrors", libxml_xmlRelaxNGSetValidErrors, METH_VARARGS, NULL},
- {(char *)"xmlRelaxNGFreeValidCtxt", libxml_xmlRelaxNGFreeValidCtxt, METH_VARARGS, NULL},
- {(char *)"xmlSchemaSetValidErrors", libxml_xmlSchemaSetValidErrors, METH_VARARGS, NULL},
- {(char *)"xmlSchemaFreeValidCtxt", libxml_xmlSchemaFreeValidCtxt, METH_VARARGS, NULL},
- #endif
- #ifdef LIBXML_C14N_ENABLED
- #ifdef LIBXML_OUTPUT_ENABLED
- {(char *)"xmlC14NDocDumpMemory", libxml_C14NDocDumpMemory, METH_VARARGS, NULL},
- {(char *)"xmlC14NDocSaveTo", libxml_C14NDocSaveTo, METH_VARARGS, NULL},
- #endif
- #endif
- {(char *) "getObjDesc", libxml_getObjDesc, METH_VARARGS, NULL},
- {(char *) "compareNodesEqual", libxml_compareNodesEqual, METH_VARARGS, NULL},
- {(char *) "nodeHash", libxml_nodeHash, METH_VARARGS, NULL},
- {NULL, NULL, 0, NULL}
- };
- #ifdef MERGED_MODULES
- extern void initlibxsltmod(void);
- #endif
- void
- initlibxml2mod(void)
- {
- static int initialized = 0;
- if (initialized != 0)
- return;
- /* intialize the python extension module */
- Py_InitModule((char *) "libxml2mod", libxmlMethods);
- /* initialize libxml2 */
- xmlInitParser();
- libxml_xmlErrorInitialize();
- initialized = 1;
- #ifdef MERGED_MODULES
- initlibxsltmod();
- #endif
- }
|