2 * libxml.c: this modules implements the main part of the glue of the
3 * libxml2 library and the Python interpreter. It provides the
4 * entry points where an automatically generated stub is either
5 * unpractical or would not match cleanly the Python model.
7 * If compiled with MERGED_MODULES, the entry point will be used to
8 * initialize both the libxml2 and the libxslt wrappers
10 * See Copyright for the status of this software.
15 #include <fileobject.h>
16 /* #include "config.h" */
17 #include <libxml/xmlmemory.h>
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
20 #include <libxml/xpath.h>
21 #include <libxml/xmlerror.h>
22 #include <libxml/xpathInternals.h>
23 #include <libxml/xmlmemory.h>
24 #include <libxml/xmlIO.h>
25 #include "libxml_wrap.h"
26 #include "libxml2-py.h"
28 #if (defined(_MSC_VER) || defined(__MINGW32__)) && !defined(vsnprintf)
29 #define vsnprintf(b,c,f,a) _vsnprintf(b,c,f,a)
33 /* #define DEBUG_SAX */
34 /* #define DEBUG_XPATH */
35 /* #define DEBUG_ERROR */
36 /* #define DEBUG_MEMORY */
37 /* #define DEBUG_FILES */
38 /* #define DEBUG_LOADER */
40 void initlibxml2mod(void);
45 * macro to flag unimplemented blocks
48 xmlGenericError(xmlGenericErrorContext, \
49 "Unimplemented block at %s:%d\n", \
52 /************************************************************************
54 * Memory debug interface *
56 ************************************************************************/
58 extern void xmlMemFree(void *ptr);
59 extern void *xmlMemMalloc(size_t size);
60 extern void *xmlMemRealloc(void *ptr, size_t size);
61 extern char *xmlMemoryStrdup(const char *str);
63 static int libxmlMemoryDebugActivated = 0;
64 static long libxmlMemoryAllocatedBase = 0;
66 static int libxmlMemoryDebug = 0;
67 static xmlFreeFunc freeFunc = NULL;
68 static xmlMallocFunc mallocFunc = NULL;
69 static xmlReallocFunc reallocFunc = NULL;
70 static xmlStrdupFunc strdupFunc = NULL;
73 libxml_xmlDebugMemory(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
79 if (!PyArg_ParseTuple(args, (char *) "i:xmlDebugMemory", &activate))
83 printf("libxml_xmlDebugMemory(%d) called\n", activate);
87 if (libxmlMemoryDebug == 0) {
89 * First initialize the library and grab the old memory handlers
90 * and switch the library to memory debugging
92 xmlMemGet((xmlFreeFunc *) & freeFunc,
93 (xmlMallocFunc *) & mallocFunc,
94 (xmlReallocFunc *) & reallocFunc,
95 (xmlStrdupFunc *) & strdupFunc);
96 if ((freeFunc == xmlMemFree) && (mallocFunc == xmlMemMalloc) &&
97 (reallocFunc == xmlMemRealloc) &&
98 (strdupFunc == xmlMemoryStrdup)) {
99 libxmlMemoryAllocatedBase = xmlMemUsed();
101 ret = (long) xmlMemSetup(xmlMemFree, xmlMemMalloc,
102 xmlMemRealloc, xmlMemoryStrdup);
105 libxmlMemoryAllocatedBase = xmlMemUsed();
109 } else if (libxmlMemoryDebugActivated == 0) {
110 libxmlMemoryAllocatedBase = xmlMemUsed();
113 ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
115 libxmlMemoryDebug = 1;
116 libxmlMemoryDebugActivated = 1;
118 if (libxmlMemoryDebugActivated == 1)
119 ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
122 libxmlMemoryDebugActivated = 0;
125 py_retval = libxml_longWrap(ret);
130 libxml_xmlDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
131 ATTRIBUTE_UNUSED PyObject * args)
134 if (libxmlMemoryDebug != 0)
140 /************************************************************************
142 * Handling Python FILE I/O at the C level *
143 * The raw I/O attack diectly the File objects, while the *
144 * other routines address the ioWrapper instance instead *
146 ************************************************************************/
149 * xmlPythonFileCloseUnref:
150 * @context: the I/O context
152 * Close an I/O channel
155 xmlPythonFileCloseRaw (void * context) {
156 PyObject *file, *ret;
159 printf("xmlPythonFileCloseUnref\n");
161 file = (PyObject *) context;
162 if (file == NULL) return(-1);
163 ret = PyEval_CallMethod(file, (char *) "close", (char *) "()");
172 * xmlPythonFileReadRaw:
173 * @context: the I/O context
174 * @buffer: where to drop data
175 * @len: number of bytes to write
177 * Read @len bytes to @buffer from the Python file in the I/O channel
179 * Returns the number of bytes read
182 xmlPythonFileReadRaw (void * context, char * buffer, int len) {
189 printf("xmlPythonFileReadRaw: %d\n", len);
191 file = (PyObject *) context;
192 if (file == NULL) return(-1);
193 ret = PyEval_CallMethod(file, (char *) "read", (char *) "(i)", len);
195 printf("xmlPythonFileReadRaw: result is NULL\n");
197 } else if (PyString_Check(ret)) {
198 lenread = PyString_Size(ret);
199 data = PyString_AsString(ret);
201 memcpy(buffer, data, len);
203 memcpy(buffer, data, lenread);
206 printf("xmlPythonFileReadRaw: result is not a String\n");
214 * @context: the I/O context
215 * @buffer: where to drop data
216 * @len: number of bytes to write
218 * Read @len bytes to @buffer from the I/O channel.
220 * Returns the number of bytes read
223 xmlPythonFileRead (void * context, char * buffer, int len) {
230 printf("xmlPythonFileRead: %d\n", len);
232 file = (PyObject *) context;
233 if (file == NULL) return(-1);
234 ret = PyEval_CallMethod(file, (char *) "io_read", (char *) "(i)", len);
236 printf("xmlPythonFileRead: result is NULL\n");
238 } else if (PyString_Check(ret)) {
239 lenread = PyString_Size(ret);
240 data = PyString_AsString(ret);
242 memcpy(buffer, data, len);
244 memcpy(buffer, data, lenread);
247 printf("xmlPythonFileRead: result is not a String\n");
255 * @context: the I/O context
256 * @buffer: where to drop data
257 * @len: number of bytes to write
259 * Write @len bytes from @buffer to the I/O channel.
261 * Returns the number of bytes written
264 xmlPythonFileWrite (void * context, const char * buffer, int len) {
271 printf("xmlPythonFileWrite: %d\n", len);
273 file = (PyObject *) context;
274 if (file == NULL) return(-1);
275 string = PyString_FromStringAndSize(buffer, len);
276 if (string == NULL) return(-1);
277 ret = PyEval_CallMethod(file, (char *) "io_write", (char *) "(O)", string);
280 printf("xmlPythonFileWrite: result is NULL\n");
282 } else if (PyInt_Check(ret)) {
283 written = (int) PyInt_AsLong(ret);
285 } else if (ret == Py_None) {
289 printf("xmlPythonFileWrite: result is not an Int nor None\n");
296 * xmlPythonFileClose:
297 * @context: the I/O context
299 * Close an I/O channel
302 xmlPythonFileClose (void * context) {
303 PyObject *file, *ret;
306 printf("xmlPythonFileClose\n");
308 file = (PyObject *) context;
309 if (file == NULL) return(-1);
310 ret = PyEval_CallMethod(file, (char *) "io_close", (char *) "()");
318 * xmlOutputBufferCreatePythonFile:
319 * @file: a PyFile_Type
320 * @encoder: the encoding converter or NULL
322 * Create a buffered output for the progressive saving to a PyFile_Type
325 * Returns the new parser output or NULL
327 static xmlOutputBufferPtr
328 xmlOutputBufferCreatePythonFile(PyObject *file,
329 xmlCharEncodingHandlerPtr encoder) {
330 xmlOutputBufferPtr ret;
332 if (file == NULL) return(NULL);
334 ret = xmlAllocOutputBuffer(encoder);
337 /* Py_INCREF(file); */
338 ret->writecallback = xmlPythonFileWrite;
339 ret->closecallback = xmlPythonFileClose;
346 libxml_xmlCreateOutputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
350 xmlCharEncodingHandlerPtr handler = NULL;
351 xmlOutputBufferPtr buffer;
354 if (!PyArg_ParseTuple(args, (char *)"Oz:xmlOutputBufferCreate",
357 if ((encoding != NULL) && (encoding[0] != 0)) {
358 handler = xmlFindCharEncodingHandler((const char *) encoding);
360 buffer = xmlOutputBufferCreatePythonFile(file, handler);
362 printf("libxml_xmlCreateOutputBuffer: buffer == NULL\n");
363 py_retval = libxml_xmlOutputBufferPtrWrap(buffer);
369 * xmlParserInputBufferCreatePythonFile:
370 * @file: a PyFile_Type
371 * @encoder: the encoding converter or NULL
373 * Create a buffered output for the progressive saving to a PyFile_Type
376 * Returns the new parser output or NULL
378 static xmlParserInputBufferPtr
379 xmlParserInputBufferCreatePythonFile(PyObject *file,
380 xmlCharEncoding encoding) {
381 xmlParserInputBufferPtr ret;
383 if (file == NULL) return(NULL);
385 ret = xmlAllocParserInputBuffer(encoding);
388 /* Py_INCREF(file); */
389 ret->readcallback = xmlPythonFileRead;
390 ret->closecallback = xmlPythonFileClose;
397 libxml_xmlCreateInputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
401 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
402 xmlParserInputBufferPtr buffer;
405 if (!PyArg_ParseTuple(args, (char *)"Oz:xmlParserInputBufferCreate",
408 if ((encoding != NULL) && (encoding[0] != 0)) {
409 enc = xmlParseCharEncoding((const char *) encoding);
411 buffer = xmlParserInputBufferCreatePythonFile(file, enc);
413 printf("libxml_xmlParserInputBufferCreate: buffer == NULL\n");
414 py_retval = libxml_xmlParserInputBufferPtrWrap(buffer);
418 /************************************************************************
420 * Providing the resolver at the Python level *
422 ************************************************************************/
424 static xmlExternalEntityLoader defaultExternalEntityLoader = NULL;
425 static PyObject *pythonExternalEntityLoaderObjext;
427 static xmlParserInputPtr
428 pythonExternalEntityLoader(const char *URL, const char *ID,
429 xmlParserCtxtPtr ctxt) {
430 xmlParserInputPtr result = NULL;
431 if (pythonExternalEntityLoaderObjext != NULL) {
435 ctxtobj = libxml_xmlParserCtxtPtrWrap(ctxt);
437 printf("pythonExternalEntityLoader: ready to call\n");
440 ret = PyObject_CallFunction(pythonExternalEntityLoaderObjext,
441 (char *) "(ssO)", URL, ID, ctxtobj);
444 printf("pythonExternalEntityLoader: result ");
445 PyObject_Print(ret, stdout, 0);
450 if (PyObject_HasAttrString(ret, (char *) "read")) {
451 xmlParserInputBufferPtr buf;
453 buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
456 buf->readcallback = xmlPythonFileReadRaw;
457 buf->closecallback = xmlPythonFileCloseRaw;
458 result = xmlNewIOInputStream(ctxt, buf,
459 XML_CHAR_ENCODING_NONE);
462 printf("pythonExternalEntityLoader: can't read\n");
464 if (result == NULL) {
466 } else if (URL != NULL) {
467 result->filename = xmlStrdup(URL);
468 result->directory = xmlParserGetDirectory((const char *) URL);
472 if ((result == NULL) && (defaultExternalEntityLoader != NULL)) {
473 result = defaultExternalEntityLoader(URL, ID, ctxt);
479 libxml_xmlSetEntityLoader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
483 if (!PyArg_ParseTuple(args, (char *)"O:libxml_xmlSetEntityLoader",
488 printf("libxml_xmlSetEntityLoader\n");
490 if (defaultExternalEntityLoader == NULL)
491 defaultExternalEntityLoader = xmlGetExternalEntityLoader();
493 pythonExternalEntityLoaderObjext = loader;
494 xmlSetExternalEntityLoader(pythonExternalEntityLoader);
496 py_retval = PyInt_FromLong(0);
501 /************************************************************************
503 * Handling SAX/xmllib/sgmlop callback interfaces *
505 ************************************************************************/
508 pythonStartElement(void *user_data, const xmlChar * name,
509 const xmlChar ** attrs)
516 PyObject *result = NULL;
520 printf("pythonStartElement(%s) called\n", name);
522 handler = (PyObject *) user_data;
523 if (PyObject_HasAttrString(handler, (char *) "startElement"))
525 else if (PyObject_HasAttrString(handler, (char *) "start"))
529 * the xmllib interface always generate a dictionnary,
532 if ((attrs == NULL) && (type == 1)) {
535 } else if (attrs == NULL) {
539 for (i = 0; attrs[i] != NULL; i++) {
540 attrname = PyString_FromString((char *) attrs[i]);
542 if (attrs[i] != NULL) {
543 attrvalue = PyString_FromString((char *) attrs[i]);
548 PyDict_SetItem(dict, attrname, attrvalue);
553 result = PyObject_CallMethod(handler, (char *) "startElement",
554 (char *) "sO", name, dict);
556 result = PyObject_CallMethod(handler, (char *) "start",
557 (char *) "sO", name, dict);
558 if (PyErr_Occurred())
566 pythonStartDocument(void *user_data)
572 printf("pythonStartDocument() called\n");
574 handler = (PyObject *) user_data;
575 if (PyObject_HasAttrString(handler, (char *) "startDocument")) {
577 PyObject_CallMethod(handler, (char *) "startDocument", NULL);
578 if (PyErr_Occurred())
585 pythonEndDocument(void *user_data)
591 printf("pythonEndDocument() called\n");
593 handler = (PyObject *) user_data;
594 if (PyObject_HasAttrString(handler, (char *) "endDocument")) {
596 PyObject_CallMethod(handler, (char *) "endDocument", NULL);
597 if (PyErr_Occurred())
602 * The reference to the handler is released there
608 pythonEndElement(void *user_data, const xmlChar * name)
614 printf("pythonEndElement(%s) called\n", name);
616 handler = (PyObject *) user_data;
617 if (PyObject_HasAttrString(handler, (char *) "endElement")) {
618 result = PyObject_CallMethod(handler, (char *) "endElement",
620 if (PyErr_Occurred())
623 } else if (PyObject_HasAttrString(handler, (char *) "end")) {
624 result = PyObject_CallMethod(handler, (char *) "end",
626 if (PyErr_Occurred())
633 pythonReference(void *user_data, const xmlChar * name)
639 printf("pythonReference(%s) called\n", name);
641 handler = (PyObject *) user_data;
642 if (PyObject_HasAttrString(handler, (char *) "reference")) {
643 result = PyObject_CallMethod(handler, (char *) "reference",
645 if (PyErr_Occurred())
652 pythonCharacters(void *user_data, const xmlChar * ch, int len)
655 PyObject *result = NULL;
659 printf("pythonCharacters(%s, %d) called\n", ch, len);
661 handler = (PyObject *) user_data;
662 if (PyObject_HasAttrString(handler, (char *) "characters"))
664 else if (PyObject_HasAttrString(handler, (char *) "data"))
668 result = PyObject_CallMethod(handler, (char *) "characters",
669 (char *) "s#", ch, len);
671 result = PyObject_CallMethod(handler, (char *) "data",
672 (char *) "s#", ch, len);
673 if (PyErr_Occurred())
680 pythonIgnorableWhitespace(void *user_data, const xmlChar * ch, int len)
683 PyObject *result = NULL;
687 printf("pythonIgnorableWhitespace(%s, %d) called\n", ch, len);
689 handler = (PyObject *) user_data;
690 if (PyObject_HasAttrString(handler, (char *) "ignorableWhitespace"))
692 else if (PyObject_HasAttrString(handler, (char *) "data"))
697 PyObject_CallMethod(handler,
698 (char *) "ignorableWhitespace",
699 (char *) "s#", ch, len);
702 PyObject_CallMethod(handler, (char *) "data",
703 (char *) "s#", ch, len);
709 pythonProcessingInstruction(void *user_data,
710 const xmlChar * target, const xmlChar * data)
716 printf("pythonProcessingInstruction(%s, %s) called\n", target, data);
718 handler = (PyObject *) user_data;
719 if (PyObject_HasAttrString(handler, (char *) "processingInstruction")) {
720 result = PyObject_CallMethod(handler, (char *)
721 "processingInstruction",
722 (char *) "ss", target, data);
728 pythonComment(void *user_data, const xmlChar * value)
734 printf("pythonComment(%s) called\n", value);
736 handler = (PyObject *) user_data;
737 if (PyObject_HasAttrString(handler, (char *) "comment")) {
739 PyObject_CallMethod(handler, (char *) "comment", (char *) "s",
741 if (PyErr_Occurred())
748 pythonWarning(void *user_data, const char *msg, ...)
756 printf("pythonWarning(%s) called\n", msg);
758 handler = (PyObject *) user_data;
759 if (PyObject_HasAttrString(handler, (char *) "warning")) {
761 vsnprintf(buf, 1023, msg, args);
765 PyObject_CallMethod(handler, (char *) "warning", (char *) "s",
767 if (PyErr_Occurred())
774 pythonError(void *user_data, const char *msg, ...)
782 printf("pythonError(%s) called\n", msg);
784 handler = (PyObject *) user_data;
785 if (PyObject_HasAttrString(handler, (char *) "error")) {
787 vsnprintf(buf, 1023, msg, args);
791 PyObject_CallMethod(handler, (char *) "error", (char *) "s",
793 if (PyErr_Occurred())
800 pythonFatalError(void *user_data, const char *msg, ...)
808 printf("pythonFatalError(%s) called\n", msg);
810 handler = (PyObject *) user_data;
811 if (PyObject_HasAttrString(handler, (char *) "fatalError")) {
813 vsnprintf(buf, 1023, msg, args);
817 PyObject_CallMethod(handler, (char *) "fatalError",
819 if (PyErr_Occurred())
826 pythonCdataBlock(void *user_data, const xmlChar * ch, int len)
829 PyObject *result = NULL;
833 printf("pythonCdataBlock(%s, %d) called\n", ch, len);
835 handler = (PyObject *) user_data;
836 if (PyObject_HasAttrString(handler, (char *) "cdataBlock"))
838 else if (PyObject_HasAttrString(handler, (char *) "cdata"))
843 PyObject_CallMethod(handler, (char *) "cdataBlock",
844 (char *) "s#", ch, len);
847 PyObject_CallMethod(handler, (char *) "cdata",
848 (char *) "s#", ch, len);
849 if (PyErr_Occurred())
856 pythonExternalSubset(void *user_data,
857 const xmlChar * name,
858 const xmlChar * externalID, const xmlChar * systemID)
864 printf("pythonExternalSubset(%s, %s, %s) called\n",
865 name, externalID, systemID);
867 handler = (PyObject *) user_data;
868 if (PyObject_HasAttrString(handler, (char *) "externalSubset")) {
870 PyObject_CallMethod(handler, (char *) "externalSubset",
871 (char *) "sss", name, externalID,
878 pythonEntityDecl(void *user_data,
879 const xmlChar * name,
881 const xmlChar * publicId,
882 const xmlChar * systemId, xmlChar * content)
887 handler = (PyObject *) user_data;
888 if (PyObject_HasAttrString(handler, (char *) "entityDecl")) {
889 result = PyObject_CallMethod(handler, (char *) "entityDecl",
890 (char *) "sisss", name, type,
891 publicId, systemId, content);
892 if (PyErr_Occurred())
902 pythonNotationDecl(void *user_data,
903 const xmlChar * name,
904 const xmlChar * publicId, const xmlChar * systemId)
909 handler = (PyObject *) user_data;
910 if (PyObject_HasAttrString(handler, (char *) "notationDecl")) {
911 result = PyObject_CallMethod(handler, (char *) "notationDecl",
912 (char *) "sss", name, publicId,
914 if (PyErr_Occurred())
921 pythonAttributeDecl(void *user_data,
922 const xmlChar * elem,
923 const xmlChar * name,
926 const xmlChar * defaultValue, xmlEnumerationPtr tree)
931 xmlEnumerationPtr node;
935 handler = (PyObject *) user_data;
936 if (PyObject_HasAttrString(handler, (char *) "attributeDecl")) {
938 for (node = tree; node != NULL; node = node->next) {
941 nameList = PyList_New(count);
943 for (node = tree; node != NULL; node = node->next) {
944 newName = PyString_FromString((char *) node->name);
945 PyList_SetItem(nameList, count, newName);
948 result = PyObject_CallMethod(handler, (char *) "attributeDecl",
949 (char *) "ssiisO", elem, name, type,
950 def, defaultValue, nameList);
951 if (PyErr_Occurred())
953 Py_XDECREF(nameList);
959 pythonElementDecl(void *user_data,
960 const xmlChar * name,
961 int type, ATTRIBUTE_UNUSED xmlElementContentPtr content)
967 handler = (PyObject *) user_data;
968 if (PyObject_HasAttrString(handler, (char *) "elementDecl")) {
969 /* TODO: wrap in an elementContent object */
971 ("pythonElementDecl: xmlElementContentPtr wrapper missing !\n");
973 /* Py_XINCREF(Py_None); isn't the reference just borrowed ??? */
974 result = PyObject_CallMethod(handler, (char *) "elementDecl",
975 (char *) "siO", name, type, obj);
976 if (PyErr_Occurred())
983 pythonUnparsedEntityDecl(void *user_data,
984 const xmlChar * name,
985 const xmlChar * publicId,
986 const xmlChar * systemId,
987 const xmlChar * notationName)
992 handler = (PyObject *) user_data;
993 if (PyObject_HasAttrString(handler, (char *) "unparsedEntityDecl")) {
995 PyObject_CallMethod(handler, (char *) "unparsedEntityDecl",
996 (char *) "ssss", name, publicId, systemId,
998 if (PyErr_Occurred())
1005 pythonInternalSubset(void *user_data, const xmlChar * name,
1006 const xmlChar * ExternalID, const xmlChar * SystemID)
1012 printf("pythonInternalSubset(%s, %s, %s) called\n",
1013 name, ExternalID, SystemID);
1015 handler = (PyObject *) user_data;
1016 if (PyObject_HasAttrString(handler, (char *) "internalSubset")) {
1017 result = PyObject_CallMethod(handler, (char *) "internalSubset",
1018 (char *) "sss", name, ExternalID,
1020 if (PyErr_Occurred())
1026 static xmlSAXHandler pythonSaxHandler = {
1027 pythonInternalSubset,
1028 NULL, /* TODO pythonIsStandalone, */
1029 NULL, /* TODO pythonHasInternalSubset, */
1030 NULL, /* TODO pythonHasExternalSubset, */
1031 NULL, /* TODO pythonResolveEntity, */
1032 NULL, /* TODO pythonGetEntity, */
1035 pythonAttributeDecl,
1037 pythonUnparsedEntityDecl,
1038 NULL, /* OBSOLETED pythonSetDocumentLocator, */
1039 pythonStartDocument,
1045 pythonIgnorableWhitespace,
1046 pythonProcessingInstruction,
1051 NULL, /* TODO pythonGetParameterEntity, */
1053 pythonExternalSubset,
1057 /************************************************************************
1059 * Handling of specific parser context *
1061 ************************************************************************/
1064 libxml_xmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
1070 PyObject *pyobj_SAX = NULL;
1071 xmlSAXHandlerPtr SAX = NULL;
1072 xmlParserCtxtPtr ret;
1075 if (!PyArg_ParseTuple
1076 (args, (char *) "Oziz:xmlCreatePushParser", &pyobj_SAX, &chunk,
1081 printf("libxml_xmlCreatePushParser(%p, %s, %d, %s) called\n",
1082 pyobj_SAX, chunk, size, URI);
1084 if (pyobj_SAX != Py_None) {
1085 SAX = &pythonSaxHandler;
1086 Py_INCREF(pyobj_SAX);
1087 /* The reference is released in pythonEndDocument() */
1089 ret = xmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI);
1090 pyret = libxml_xmlParserCtxtPtrWrap(ret);
1095 libxml_htmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
1101 PyObject *pyobj_SAX = NULL;
1102 xmlSAXHandlerPtr SAX = NULL;
1103 xmlParserCtxtPtr ret;
1106 if (!PyArg_ParseTuple
1107 (args, (char *) "Oziz:htmlCreatePushParser", &pyobj_SAX, &chunk,
1112 printf("libxml_htmlCreatePushParser(%p, %s, %d, %s) called\n",
1113 pyobj_SAX, chunk, size, URI);
1115 if (pyobj_SAX != Py_None) {
1116 SAX = &pythonSaxHandler;
1117 Py_INCREF(pyobj_SAX);
1118 /* The reference is released in pythonEndDocument() */
1120 ret = htmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI,
1121 XML_CHAR_ENCODING_NONE);
1122 pyret = libxml_xmlParserCtxtPtrWrap(ret);
1127 libxml_xmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1131 PyObject *pyobj_SAX = NULL;
1132 xmlSAXHandlerPtr SAX = NULL;
1134 if (!PyArg_ParseTuple(args, (char *) "Osi:xmlSAXParseFile", &pyobj_SAX,
1139 printf("libxml_xmlSAXParseFile(%p, %s, %d) called\n",
1140 pyobj_SAX, URI, recover);
1142 if (pyobj_SAX == Py_None) {
1146 SAX = &pythonSaxHandler;
1147 Py_INCREF(pyobj_SAX);
1148 /* The reference is released in pythonEndDocument() */
1149 xmlSAXParseFileWithData(SAX, URI, recover, pyobj_SAX);
1155 libxml_htmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1158 const char *encoding;
1159 PyObject *pyobj_SAX = NULL;
1160 xmlSAXHandlerPtr SAX = NULL;
1162 if (!PyArg_ParseTuple
1163 (args, (char *) "Osz:htmlSAXParseFile", &pyobj_SAX, &URI,
1168 printf("libxml_htmlSAXParseFile(%p, %s, %s) called\n",
1169 pyobj_SAX, URI, encoding);
1171 if (pyobj_SAX == Py_None) {
1175 SAX = &pythonSaxHandler;
1176 Py_INCREF(pyobj_SAX);
1177 /* The reference is released in pythonEndDocument() */
1178 htmlSAXParseFile(URI, encoding, SAX, pyobj_SAX);
1183 /************************************************************************
1185 * Error message callback *
1187 ************************************************************************/
1189 static PyObject *libxml_xmlPythonErrorFuncHandler = NULL;
1190 static PyObject *libxml_xmlPythonErrorFuncCtxt = NULL;
1192 /* helper to build a xmlMalloc'ed string from a format and va_list */
1194 libxml_buildMessage(const char *msg, va_list ap)
1201 str = (char *) xmlMalloc(150);
1208 chars = vsnprintf(str, size, msg, ap);
1209 if ((chars > -1) && (chars < size))
1215 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
1226 libxml_xmlErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, const char *msg,
1236 printf("libxml_xmlErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
1240 if (libxml_xmlPythonErrorFuncHandler == NULL) {
1242 vfprintf(stdout, msg, ap);
1246 str = libxml_buildMessage(msg,ap);
1249 list = PyTuple_New(2);
1250 PyTuple_SetItem(list, 0, libxml_xmlPythonErrorFuncCtxt);
1251 Py_XINCREF(libxml_xmlPythonErrorFuncCtxt);
1252 message = libxml_charPtrWrap(str);
1253 PyTuple_SetItem(list, 1, message);
1254 result = PyEval_CallObject(libxml_xmlPythonErrorFuncHandler, list);
1261 libxml_xmlErrorInitialize(void)
1264 printf("libxml_xmlErrorInitialize() called\n");
1266 xmlSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
1270 libxml_xmlRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self,
1273 PyObject *py_retval;
1275 PyObject *pyobj_ctx;
1277 if (!PyArg_ParseTuple
1278 (args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f,
1283 printf("libxml_registerXPathFunction(%p, %p) called\n", pyobj_ctx,
1287 if (libxml_xmlPythonErrorFuncHandler != NULL) {
1288 Py_XDECREF(libxml_xmlPythonErrorFuncHandler);
1290 if (libxml_xmlPythonErrorFuncCtxt != NULL) {
1291 Py_XDECREF(libxml_xmlPythonErrorFuncCtxt);
1294 Py_XINCREF(pyobj_ctx);
1295 Py_XINCREF(pyobj_f);
1297 /* TODO: check f is a function ! */
1298 libxml_xmlPythonErrorFuncHandler = pyobj_f;
1299 libxml_xmlPythonErrorFuncCtxt = pyobj_ctx;
1301 py_retval = libxml_intWrap(1);
1306 /************************************************************************
1308 * Per parserCtxt error handler *
1310 ************************************************************************/
1316 } xmlParserCtxtPyCtxt;
1317 typedef xmlParserCtxtPyCtxt *xmlParserCtxtPyCtxtPtr;
1320 libxml_xmlParserCtxtGenericErrorFuncHandler(void *ctx, int severity, char *str)
1324 xmlParserCtxtPtr ctxt;
1325 xmlParserCtxtPyCtxtPtr pyCtxt;
1328 printf("libxml_xmlParserCtxtGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
1331 ctxt = (xmlParserCtxtPtr)ctx;
1332 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
1334 list = PyTuple_New(4);
1335 PyTuple_SetItem(list, 0, pyCtxt->arg);
1336 Py_XINCREF(pyCtxt->arg);
1337 PyTuple_SetItem(list, 1, libxml_charPtrWrap(str));
1338 PyTuple_SetItem(list, 2, libxml_intWrap(severity));
1339 PyTuple_SetItem(list, 3, Py_None);
1341 result = PyEval_CallObject(pyCtxt->f, list);
1344 /* TODO: manage for the exception to be propagated... */
1352 libxml_xmlParserCtxtErrorFuncHandler(void *ctx, const char *msg, ...)
1357 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_ERROR,libxml_buildMessage(msg,ap));
1362 libxml_xmlParserCtxtWarningFuncHandler(void *ctx, const char *msg, ...)
1367 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_WARNING,libxml_buildMessage(msg,ap));
1372 libxml_xmlParserCtxtValidityErrorFuncHandler(void *ctx, const char *msg, ...)
1377 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap));
1382 libxml_xmlParserCtxtValidityWarningFuncHandler(void *ctx, const char *msg, ...)
1387 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap));
1392 libxml_xmlParserCtxtSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1394 PyObject *py_retval;
1395 xmlParserCtxtPtr ctxt;
1396 xmlParserCtxtPyCtxtPtr pyCtxt;
1397 PyObject *pyobj_ctxt;
1399 PyObject *pyobj_arg;
1401 if (!PyArg_ParseTuple(args, (char *)"OOO:xmlParserCtxtSetErrorHandler",
1402 &pyobj_ctxt, &pyobj_f, &pyobj_arg))
1404 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1405 if (ctxt->_private == NULL) {
1406 pyCtxt = xmlMalloc(sizeof(xmlParserCtxtPyCtxt));
1407 if (pyCtxt == NULL) {
1408 py_retval = libxml_intWrap(-1);
1411 memset(pyCtxt,0,sizeof(xmlParserCtxtPyCtxt));
1412 ctxt->_private = pyCtxt;
1415 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
1417 /* TODO: check f is a function ! */
1418 Py_XDECREF(pyCtxt->f);
1419 Py_XINCREF(pyobj_f);
1420 pyCtxt->f = pyobj_f;
1421 Py_XDECREF(pyCtxt->arg);
1422 Py_XINCREF(pyobj_arg);
1423 pyCtxt->arg = pyobj_arg;
1425 if (pyobj_f != Py_None) {
1426 ctxt->sax->error = libxml_xmlParserCtxtErrorFuncHandler;
1427 ctxt->sax->warning = libxml_xmlParserCtxtWarningFuncHandler;
1428 ctxt->vctxt.error = libxml_xmlParserCtxtValidityErrorFuncHandler;
1429 ctxt->vctxt.warning = libxml_xmlParserCtxtValidityWarningFuncHandler;
1432 ctxt->sax->error = xmlParserError;
1433 ctxt->vctxt.error = xmlParserValidityError;
1434 ctxt->sax->warning = xmlParserWarning;
1435 ctxt->vctxt.warning = xmlParserValidityWarning;
1438 py_retval = libxml_intWrap(1);
1443 libxml_xmlParserCtxtGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1445 PyObject *py_retval;
1446 xmlParserCtxtPtr ctxt;
1447 xmlParserCtxtPyCtxtPtr pyCtxt;
1448 PyObject *pyobj_ctxt;
1450 if (!PyArg_ParseTuple(args, (char *)"O:xmlParserCtxtGetErrorHandler",
1453 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1454 py_retval = PyTuple_New(2);
1455 if (ctxt->_private != NULL) {
1456 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
1458 PyTuple_SetItem(py_retval, 0, pyCtxt->f);
1459 Py_XINCREF(pyCtxt->f);
1460 PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
1461 Py_XINCREF(pyCtxt->arg);
1464 /* no python error handler registered */
1465 PyTuple_SetItem(py_retval, 0, Py_None);
1466 Py_XINCREF(Py_None);
1467 PyTuple_SetItem(py_retval, 1, Py_None);
1468 Py_XINCREF(Py_None);
1474 libxml_xmlFreeParserCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
1475 xmlParserCtxtPtr ctxt;
1476 PyObject *pyobj_ctxt;
1477 xmlParserCtxtPyCtxtPtr pyCtxt;
1479 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeParserCtxt", &pyobj_ctxt))
1481 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1484 pyCtxt = (xmlParserCtxtPyCtxtPtr)((xmlParserCtxtPtr)ctxt)->_private;
1486 Py_XDECREF(pyCtxt->f);
1487 Py_XDECREF(pyCtxt->arg);
1490 xmlFreeParserCtxt(ctxt);
1497 /************************************************************************
1499 * Per xmlTextReader error handler *
1501 ************************************************************************/
1507 } xmlTextReaderPyCtxt;
1508 typedef xmlTextReaderPyCtxt *xmlTextReaderPyCtxtPtr;
1511 libxml_xmlTextReaderErrorCallback(void *arg,
1514 xmlTextReaderLocatorPtr locator)
1516 xmlTextReaderPyCtxt *pyCtxt = (xmlTextReaderPyCtxt *)arg;
1520 list = PyTuple_New(4);
1521 PyTuple_SetItem(list, 0, pyCtxt->arg);
1522 Py_XINCREF(pyCtxt->arg);
1523 PyTuple_SetItem(list, 1, libxml_charPtrConstWrap(msg));
1524 PyTuple_SetItem(list, 2, libxml_intWrap(severity));
1525 PyTuple_SetItem(list, 3, libxml_xmlTextReaderLocatorPtrWrap(locator));
1526 result = PyEval_CallObject(pyCtxt->f, list);
1529 /* TODO: manage for the exception to be propagated... */
1537 libxml_xmlTextReaderSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1539 xmlTextReaderPtr reader;
1540 xmlTextReaderPyCtxtPtr pyCtxt;
1541 xmlTextReaderErrorFunc f;
1543 PyObject *pyobj_reader;
1545 PyObject *pyobj_arg;
1546 PyObject *py_retval;
1548 if (!PyArg_ParseTuple(args, (char *)"OOO:xmlTextReaderSetErrorHandler", &pyobj_reader, &pyobj_f, &pyobj_arg))
1550 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
1551 /* clear previous error handler */
1552 xmlTextReaderGetErrorHandler(reader,&f,&arg);
1554 if (f == libxml_xmlTextReaderErrorCallback) {
1555 /* ok, it's our error handler! */
1556 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
1557 Py_XDECREF(pyCtxt->f);
1558 Py_XDECREF(pyCtxt->arg);
1563 * there already an arg, and it's not ours,
1564 * there is definitely something wrong going on here...
1565 * we don't know how to free it, so we bail out...
1567 py_retval = libxml_intWrap(-1);
1571 xmlTextReaderSetErrorHandler(reader,NULL,NULL);
1572 /* set new error handler */
1573 if (pyobj_f != Py_None)
1575 pyCtxt = (xmlTextReaderPyCtxtPtr)xmlMalloc(sizeof(xmlTextReaderPyCtxt));
1576 if (pyCtxt == NULL) {
1577 py_retval = libxml_intWrap(-1);
1580 Py_XINCREF(pyobj_f);
1581 pyCtxt->f = pyobj_f;
1582 Py_XINCREF(pyobj_arg);
1583 pyCtxt->arg = pyobj_arg;
1584 xmlTextReaderSetErrorHandler(reader,libxml_xmlTextReaderErrorCallback,pyCtxt);
1587 py_retval = libxml_intWrap(1);
1592 libxml_xmlTextReaderGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1594 xmlTextReaderPtr reader;
1595 xmlTextReaderPyCtxtPtr pyCtxt;
1596 xmlTextReaderErrorFunc f;
1598 PyObject *pyobj_reader;
1599 PyObject *py_retval;
1601 if (!PyArg_ParseTuple(args, (char *)"O:xmlTextReaderSetErrorHandler", &pyobj_reader))
1603 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
1604 xmlTextReaderGetErrorHandler(reader,&f,&arg);
1605 py_retval = PyTuple_New(2);
1606 if (f == libxml_xmlTextReaderErrorCallback) {
1607 /* ok, it's our error handler! */
1608 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
1609 PyTuple_SetItem(py_retval, 0, pyCtxt->f);
1610 Py_XINCREF(pyCtxt->f);
1611 PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
1612 Py_XINCREF(pyCtxt->arg);
1616 /* f is null or it's not our error handler */
1617 PyTuple_SetItem(py_retval, 0, Py_None);
1618 Py_XINCREF(Py_None);
1619 PyTuple_SetItem(py_retval, 1, Py_None);
1620 Py_XINCREF(Py_None);
1626 libxml_xmlFreeTextReader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
1627 xmlTextReaderPtr reader;
1628 PyObject *pyobj_reader;
1629 xmlTextReaderPyCtxtPtr pyCtxt;
1630 xmlTextReaderErrorFunc f;
1633 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeTextReader", &pyobj_reader))
1635 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
1637 xmlTextReaderGetErrorHandler(reader,&f,&arg);
1639 if (f == libxml_xmlTextReaderErrorCallback) {
1640 /* ok, it's our error handler! */
1641 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
1642 Py_XDECREF(pyCtxt->f);
1643 Py_XDECREF(pyCtxt->arg);
1647 * else, something wrong happened, because the error handler is
1648 * not owned by the python bindings...
1652 xmlFreeTextReader(reader);
1657 /************************************************************************
1659 * XPath extensions *
1661 ************************************************************************/
1663 static int libxml_xpathCallbacksInitialized = 0;
1665 typedef struct libxml_xpathCallback {
1666 xmlXPathContextPtr ctx;
1670 } libxml_xpathCallback, *libxml_xpathCallbackPtr;
1671 static libxml_xpathCallback libxml_xpathCallbacks[10];
1672 static int libxml_xpathCallbacksNb = 0;
1673 static int libxml_xpathCallbacksMax = 10;
1676 libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs)
1678 PyObject *list, *cur, *result;
1679 xmlXPathObjectPtr obj;
1680 xmlXPathContextPtr rctxt;
1681 PyObject *current_function = NULL;
1682 const xmlChar *name;
1683 const xmlChar *ns_uri;
1688 rctxt = ctxt->context;
1691 name = rctxt->function;
1692 ns_uri = rctxt->functionURI;
1694 printf("libxml_xmlXPathFuncCallback called name %s URI %s\n", name,
1699 * Find the function, it should be there it was there at lookup
1701 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
1702 if ( /* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */
1703 (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) &&
1704 (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) {
1705 current_function = libxml_xpathCallbacks[i].function;
1708 if (current_function == NULL) {
1710 ("libxml_xmlXPathFuncCallback: internal error %s not found !\n",
1715 list = PyTuple_New(nargs + 1);
1716 PyTuple_SetItem(list, 0, libxml_xmlXPathParserContextPtrWrap(ctxt));
1717 for (i = 0; i < nargs; i++) {
1718 obj = valuePop(ctxt);
1719 cur = libxml_xmlXPathObjectPtrWrap(obj);
1720 PyTuple_SetItem(list, i + 1, cur);
1722 result = PyEval_CallObject(current_function, list);
1725 obj = libxml_xmlXPathObjectPtrConvert(result);
1726 valuePush(ctxt, obj);
1729 static xmlXPathFunction
1730 libxml_xmlXPathFuncLookupFunc(void *ctxt, const xmlChar * name,
1731 const xmlChar * ns_uri)
1736 printf("libxml_xmlXPathFuncLookupFunc(%p, %s, %s) called\n",
1737 ctxt, name, ns_uri);
1740 * This is called once only. The address is then stored in the
1741 * XPath expression evaluation, the proper object to call can
1742 * then still be found using the execution context function
1743 * and functionURI fields.
1745 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
1746 if ((ctxt == libxml_xpathCallbacks[i].ctx) &&
1747 (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) &&
1748 (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) {
1749 return (libxml_xmlXPathFuncCallback);
1756 libxml_xpathCallbacksInitialize(void)
1760 if (libxml_xpathCallbacksInitialized != 0)
1764 printf("libxml_xpathCallbacksInitialized called\n");
1767 for (i = 0; i < 10; i++) {
1768 libxml_xpathCallbacks[i].ctx = NULL;
1769 libxml_xpathCallbacks[i].name = NULL;
1770 libxml_xpathCallbacks[i].ns_uri = NULL;
1771 libxml_xpathCallbacks[i].function = NULL;
1773 libxml_xpathCallbacksInitialized = 1;
1777 libxml_xmlRegisterXPathFunction(ATTRIBUTE_UNUSED PyObject * self,
1780 PyObject *py_retval;
1784 xmlXPathContextPtr ctx;
1785 PyObject *pyobj_ctx;
1789 if (!PyArg_ParseTuple
1790 (args, (char *) "OszO:registerXPathFunction", &pyobj_ctx, &name,
1794 ctx = (xmlXPathContextPtr) PyxmlXPathContext_Get(pyobj_ctx);
1795 if (libxml_xpathCallbacksInitialized == 0)
1796 libxml_xpathCallbacksInitialize();
1797 xmlXPathRegisterFuncLookup(ctx, libxml_xmlXPathFuncLookupFunc, ctx);
1799 if ((pyobj_ctx == NULL) || (name == NULL) || (pyobj_f == NULL)) {
1800 py_retval = libxml_intWrap(-1);
1804 printf("libxml_registerXPathFunction(%p, %s, %s) called\n",
1807 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
1808 if ((ctx == libxml_xpathCallbacks[i].ctx) &&
1809 (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) &&
1810 (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) {
1811 Py_XINCREF(pyobj_f);
1812 Py_XDECREF(libxml_xpathCallbacks[i].function);
1813 libxml_xpathCallbacks[i].function = pyobj_f;
1818 if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksMax) {
1819 printf("libxml_registerXPathFunction() table full\n");
1821 i = libxml_xpathCallbacksNb++;
1822 Py_XINCREF(pyobj_f);
1823 libxml_xpathCallbacks[i].ctx = ctx;
1824 libxml_xpathCallbacks[i].name = xmlStrdup(name);
1825 libxml_xpathCallbacks[i].ns_uri = xmlStrdup(ns_uri);
1826 libxml_xpathCallbacks[i].function = pyobj_f;
1830 py_retval = libxml_intWrap((int) c_retval);
1834 /************************************************************************
1836 * Global properties access *
1838 ************************************************************************/
1840 libxml_name(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1842 PyObject *resultobj, *obj;
1846 if (!PyArg_ParseTuple(args, (char *) "O:name", &obj))
1848 cur = PyxmlNode_Get(obj);
1851 printf("libxml_name: cur = %p type %d\n", cur, cur->type);
1854 switch (cur->type) {
1855 case XML_DOCUMENT_NODE:
1856 #ifdef LIBXML_DOCB_ENABLED
1857 case XML_DOCB_DOCUMENT_NODE:
1859 case XML_HTML_DOCUMENT_NODE:{
1860 xmlDocPtr doc = (xmlDocPtr) cur;
1865 case XML_ATTRIBUTE_NODE:{
1866 xmlAttrPtr attr = (xmlAttrPtr) cur;
1871 case XML_NAMESPACE_DECL:{
1872 xmlNsPtr ns = (xmlNsPtr) cur;
1881 resultobj = libxml_constxmlCharPtrWrap(res);
1887 libxml_doc(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1889 PyObject *resultobj, *obj;
1893 if (!PyArg_ParseTuple(args, (char *) "O:doc", &obj))
1895 cur = PyxmlNode_Get(obj);
1898 printf("libxml_doc: cur = %p\n", cur);
1901 switch (cur->type) {
1902 case XML_DOCUMENT_NODE:
1903 #ifdef LIBXML_DOCB_ENABLED
1904 case XML_DOCB_DOCUMENT_NODE:
1906 case XML_HTML_DOCUMENT_NODE:
1909 case XML_ATTRIBUTE_NODE:{
1910 xmlAttrPtr attr = (xmlAttrPtr) cur;
1915 case XML_NAMESPACE_DECL:
1922 resultobj = libxml_xmlDocPtrWrap(res);
1927 libxml_properties(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1929 PyObject *resultobj, *obj;
1930 xmlNodePtr cur = NULL;
1933 if (!PyArg_ParseTuple(args, (char *) "O:properties", &obj))
1935 cur = PyxmlNode_Get(obj);
1936 if (cur->type == XML_ELEMENT_NODE)
1937 res = cur->properties;
1940 resultobj = libxml_xmlAttrPtrWrap(res);
1945 libxml_next(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1947 PyObject *resultobj, *obj;
1951 if (!PyArg_ParseTuple(args, (char *) "O:next", &obj))
1953 cur = PyxmlNode_Get(obj);
1956 printf("libxml_next: cur = %p\n", cur);
1959 switch (cur->type) {
1960 case XML_DOCUMENT_NODE:
1961 #ifdef LIBXML_DOCB_ENABLED
1962 case XML_DOCB_DOCUMENT_NODE:
1964 case XML_HTML_DOCUMENT_NODE:
1967 case XML_ATTRIBUTE_NODE:{
1968 xmlAttrPtr attr = (xmlAttrPtr) cur;
1970 res = (xmlNodePtr) attr->next;
1973 case XML_NAMESPACE_DECL:{
1974 xmlNsPtr ns = (xmlNsPtr) cur;
1976 res = (xmlNodePtr) ns->next;
1984 resultobj = libxml_xmlNodePtrWrap(res);
1989 libxml_prev(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1991 PyObject *resultobj, *obj;
1995 if (!PyArg_ParseTuple(args, (char *) "O:prev", &obj))
1997 cur = PyxmlNode_Get(obj);
2000 printf("libxml_prev: cur = %p\n", cur);
2003 switch (cur->type) {
2004 case XML_DOCUMENT_NODE:
2005 #ifdef LIBXML_DOCB_ENABLED
2006 case XML_DOCB_DOCUMENT_NODE:
2008 case XML_HTML_DOCUMENT_NODE:
2011 case XML_ATTRIBUTE_NODE:{
2012 xmlAttrPtr attr = (xmlAttrPtr) cur;
2014 res = (xmlNodePtr) attr->prev;
2016 case XML_NAMESPACE_DECL:
2023 resultobj = libxml_xmlNodePtrWrap(res);
2028 libxml_children(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2030 PyObject *resultobj, *obj;
2034 if (!PyArg_ParseTuple(args, (char *) "O:children", &obj))
2036 cur = PyxmlNode_Get(obj);
2039 printf("libxml_children: cur = %p\n", cur);
2042 switch (cur->type) {
2043 case XML_ELEMENT_NODE:
2044 case XML_ENTITY_REF_NODE:
2045 case XML_ENTITY_NODE:
2047 case XML_COMMENT_NODE:
2048 case XML_DOCUMENT_NODE:
2049 #ifdef LIBXML_DOCB_ENABLED
2050 case XML_DOCB_DOCUMENT_NODE:
2052 case XML_HTML_DOCUMENT_NODE:
2054 res = cur->children;
2056 case XML_ATTRIBUTE_NODE:{
2057 xmlAttrPtr attr = (xmlAttrPtr) cur;
2059 res = attr->children;
2066 resultobj = libxml_xmlNodePtrWrap(res);
2071 libxml_last(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2073 PyObject *resultobj, *obj;
2077 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
2079 cur = PyxmlNode_Get(obj);
2082 printf("libxml_last: cur = %p\n", cur);
2085 switch (cur->type) {
2086 case XML_ELEMENT_NODE:
2087 case XML_ENTITY_REF_NODE:
2088 case XML_ENTITY_NODE:
2090 case XML_COMMENT_NODE:
2091 case XML_DOCUMENT_NODE:
2092 #ifdef LIBXML_DOCB_ENABLED
2093 case XML_DOCB_DOCUMENT_NODE:
2095 case XML_HTML_DOCUMENT_NODE:
2099 case XML_ATTRIBUTE_NODE:{
2100 xmlAttrPtr attr = (xmlAttrPtr) cur;
2108 resultobj = libxml_xmlNodePtrWrap(res);
2113 libxml_parent(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2115 PyObject *resultobj, *obj;
2119 if (!PyArg_ParseTuple(args, (char *) "O:parent", &obj))
2121 cur = PyxmlNode_Get(obj);
2124 printf("libxml_parent: cur = %p\n", cur);
2127 switch (cur->type) {
2128 case XML_DOCUMENT_NODE:
2129 case XML_HTML_DOCUMENT_NODE:
2130 #ifdef LIBXML_DOCB_ENABLED
2131 case XML_DOCB_DOCUMENT_NODE:
2135 case XML_ATTRIBUTE_NODE:{
2136 xmlAttrPtr attr = (xmlAttrPtr) cur;
2140 case XML_ENTITY_DECL:
2141 case XML_NAMESPACE_DECL:
2142 case XML_XINCLUDE_START:
2143 case XML_XINCLUDE_END:
2150 resultobj = libxml_xmlNodePtrWrap(res);
2155 libxml_type(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2157 PyObject *resultobj, *obj;
2159 const xmlChar *res = NULL;
2161 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
2163 cur = PyxmlNode_Get(obj);
2166 printf("libxml_type: cur = %p\n", cur);
2169 switch (cur->type) {
2170 case XML_ELEMENT_NODE:
2171 res = (const xmlChar *) "element";
2173 case XML_ATTRIBUTE_NODE:
2174 res = (const xmlChar *) "attribute";
2177 res = (const xmlChar *) "text";
2179 case XML_CDATA_SECTION_NODE:
2180 res = (const xmlChar *) "cdata";
2182 case XML_ENTITY_REF_NODE:
2183 res = (const xmlChar *) "entity_ref";
2185 case XML_ENTITY_NODE:
2186 res = (const xmlChar *) "entity";
2189 res = (const xmlChar *) "pi";
2191 case XML_COMMENT_NODE:
2192 res = (const xmlChar *) "comment";
2194 case XML_DOCUMENT_NODE:
2195 res = (const xmlChar *) "document_xml";
2197 case XML_DOCUMENT_TYPE_NODE:
2198 res = (const xmlChar *) "doctype";
2200 case XML_DOCUMENT_FRAG_NODE:
2201 res = (const xmlChar *) "fragment";
2203 case XML_NOTATION_NODE:
2204 res = (const xmlChar *) "notation";
2206 case XML_HTML_DOCUMENT_NODE:
2207 res = (const xmlChar *) "document_html";
2210 res = (const xmlChar *) "dtd";
2212 case XML_ELEMENT_DECL:
2213 res = (const xmlChar *) "elem_decl";
2215 case XML_ATTRIBUTE_DECL:
2216 res = (const xmlChar *) "attribute_decl";
2218 case XML_ENTITY_DECL:
2219 res = (const xmlChar *) "entity_decl";
2221 case XML_NAMESPACE_DECL:
2222 res = (const xmlChar *) "namespace";
2224 case XML_XINCLUDE_START:
2225 res = (const xmlChar *) "xinclude_start";
2227 case XML_XINCLUDE_END:
2228 res = (const xmlChar *) "xinclude_end";
2230 #ifdef LIBXML_DOCB_ENABLED
2231 case XML_DOCB_DOCUMENT_NODE:
2232 res = (const xmlChar *) "document_docbook";
2237 printf("libxml_type: cur = %p: %s\n", cur, res);
2240 resultobj = libxml_constxmlCharPtrWrap(res);
2244 /************************************************************************
2246 * Specific accessor functions *
2248 ************************************************************************/
2250 libxml_xmlNodeGetNsDefs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2252 PyObject *py_retval;
2255 PyObject *pyobj_node;
2257 if (!PyArg_ParseTuple
2258 (args, (char *) "O:xmlNodeGetNsDefs", &pyobj_node))
2260 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2262 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
2266 c_retval = node->nsDef;
2267 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
2272 libxml_xmlNodeGetNs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2274 PyObject *py_retval;
2277 PyObject *pyobj_node;
2279 if (!PyArg_ParseTuple(args, (char *) "O:xmlNodeGetNs", &pyobj_node))
2281 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2283 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
2287 c_retval = node->ns;
2288 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
2292 /************************************************************************
2294 * Serialization front-end *
2296 ************************************************************************/
2299 libxml_serializeNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2301 PyObject *py_retval = NULL;
2303 PyObject *pyobj_node;
2306 const char *encoding;
2310 if (!PyArg_ParseTuple(args, (char *) "Ozi:serializeNode", &pyobj_node,
2311 &encoding, &format))
2313 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2319 if (node->type == XML_DOCUMENT_NODE) {
2320 doc = (xmlDocPtr) node;
2321 xmlDocDumpFormatMemoryEnc(doc, &c_retval, &len,
2322 (const char *) encoding, format);
2323 py_retval = libxml_charPtrWrap((char *) c_retval);
2324 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
2325 xmlOutputBufferPtr buf;
2326 xmlCharEncodingHandlerPtr handler = NULL;
2328 doc = (xmlDocPtr) node;
2329 if (encoding != NULL)
2330 htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
2331 encoding = (const char *) htmlGetMetaEncoding(doc);
2333 if (encoding != NULL) {
2334 handler = xmlFindCharEncodingHandler(encoding);
2335 if (handler == NULL) {
2342 * Fallback to HTML or ASCII when the encoding is unspecified
2344 if (handler == NULL)
2345 handler = xmlFindCharEncodingHandler("HTML");
2346 if (handler == NULL)
2347 handler = xmlFindCharEncodingHandler("ascii");
2349 buf = xmlAllocOutputBuffer(handler);
2354 htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
2355 xmlOutputBufferFlush(buf);
2356 if (buf->conv != NULL) {
2357 len = buf->conv->use;
2358 c_retval = buf->conv->content;
2359 buf->conv->content = NULL;
2361 len = buf->buffer->use;
2362 c_retval = buf->buffer->content;
2363 buf->buffer->content = NULL;
2365 (void) xmlOutputBufferClose(buf);
2366 py_retval = libxml_charPtrWrap((char *) c_retval);
2369 if ((doc == NULL) || (doc->type == XML_DOCUMENT_NODE)) {
2370 xmlOutputBufferPtr buf;
2371 xmlCharEncodingHandlerPtr handler = NULL;
2373 if (encoding != NULL) {
2374 handler = xmlFindCharEncodingHandler(encoding);
2375 if (handler == NULL) {
2381 buf = xmlAllocOutputBuffer(handler);
2386 xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
2387 xmlOutputBufferFlush(buf);
2388 if (buf->conv != NULL) {
2389 len = buf->conv->use;
2390 c_retval = buf->conv->content;
2391 buf->conv->content = NULL;
2393 len = buf->buffer->use;
2394 c_retval = buf->buffer->content;
2395 buf->buffer->content = NULL;
2397 (void) xmlOutputBufferClose(buf);
2398 py_retval = libxml_charPtrWrap((char *) c_retval);
2399 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2400 xmlOutputBufferPtr buf;
2401 xmlCharEncodingHandlerPtr handler = NULL;
2403 if (encoding != NULL)
2404 htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
2405 encoding = (const char *) htmlGetMetaEncoding(doc);
2406 if (encoding != NULL) {
2407 handler = xmlFindCharEncodingHandler(encoding);
2408 if (handler == NULL) {
2415 * Fallback to HTML or ASCII when the encoding is unspecified
2417 if (handler == NULL)
2418 handler = xmlFindCharEncodingHandler("HTML");
2419 if (handler == NULL)
2420 handler = xmlFindCharEncodingHandler("ascii");
2422 buf = xmlAllocOutputBuffer(handler);
2427 htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
2428 xmlOutputBufferFlush(buf);
2429 if (buf->conv != NULL) {
2430 len = buf->conv->use;
2431 c_retval = buf->conv->content;
2432 buf->conv->content = NULL;
2434 len = buf->buffer->use;
2435 c_retval = buf->buffer->content;
2436 buf->buffer->content = NULL;
2438 (void) xmlOutputBufferClose(buf);
2439 py_retval = libxml_charPtrWrap((char *) c_retval);
2449 libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2451 PyObject *py_file = NULL;
2453 PyObject *pyobj_node;
2456 const char *encoding;
2459 xmlOutputBufferPtr buf;
2460 xmlCharEncodingHandlerPtr handler = NULL;
2462 if (!PyArg_ParseTuple(args, (char *) "OOzi:serializeNode", &pyobj_node,
2463 &py_file, &encoding, &format))
2465 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2468 return (PyInt_FromLong((long) -1));
2470 if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
2471 return (PyInt_FromLong((long) -1));
2473 output = PyFile_AsFile(py_file);
2474 if (output == NULL) {
2475 return (PyInt_FromLong((long) -1));
2478 if (node->type == XML_DOCUMENT_NODE) {
2479 doc = (xmlDocPtr) node;
2480 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
2481 doc = (xmlDocPtr) node;
2485 if (doc->type == XML_HTML_DOCUMENT_NODE) {
2486 if (encoding == NULL)
2487 encoding = (const char *) htmlGetMetaEncoding(doc);
2489 if (encoding != NULL) {
2490 handler = xmlFindCharEncodingHandler(encoding);
2491 if (handler == NULL) {
2492 return (PyInt_FromLong((long) -1));
2495 if (doc->type == XML_HTML_DOCUMENT_NODE) {
2496 if (handler == NULL)
2497 handler = xmlFindCharEncodingHandler("HTML");
2498 if (handler == NULL)
2499 handler = xmlFindCharEncodingHandler("ascii");
2502 buf = xmlOutputBufferCreateFile(output, handler);
2503 if (node->type == XML_DOCUMENT_NODE) {
2504 len = xmlSaveFormatFileTo(buf, doc, encoding, format);
2505 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
2506 htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
2507 len = xmlOutputBufferClose(buf);
2508 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2509 htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
2510 len = xmlOutputBufferClose(buf);
2512 xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
2513 len = xmlOutputBufferClose(buf);
2515 return (PyInt_FromLong((long) len));
2518 /************************************************************************
2522 ************************************************************************/
2524 libxml_xmlNewNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2526 PyObject *py_retval;
2530 if (!PyArg_ParseTuple(args, (char *) "s:xmlNewNode", &name))
2532 node = (xmlNodePtr) xmlNewNode(NULL, name);
2534 printf("NewNode: %s : %p\n", name, (void *) node);
2541 py_retval = libxml_xmlNodePtrWrap(node);
2545 /************************************************************************
2547 * The registration stuff *
2549 ************************************************************************/
2550 static PyMethodDef libxmlMethods[] = {
2551 #include "libxml2-export.c"
2552 {(char *) "name", libxml_name, METH_VARARGS, NULL},
2553 {(char *) "children", libxml_children, METH_VARARGS, NULL},
2554 {(char *) "properties", libxml_properties, METH_VARARGS, NULL},
2555 {(char *) "last", libxml_last, METH_VARARGS, NULL},
2556 {(char *) "prev", libxml_prev, METH_VARARGS, NULL},
2557 {(char *) "next", libxml_next, METH_VARARGS, NULL},
2558 {(char *) "parent", libxml_parent, METH_VARARGS, NULL},
2559 {(char *) "type", libxml_type, METH_VARARGS, NULL},
2560 {(char *) "doc", libxml_doc, METH_VARARGS, NULL},
2561 {(char *) "xmlNewNode", libxml_xmlNewNode, METH_VARARGS, NULL},
2562 {(char *) "serializeNode", libxml_serializeNode, METH_VARARGS, NULL},
2563 {(char *) "saveNodeTo", libxml_saveNodeTo, METH_VARARGS, NULL},
2564 {(char *) "outputBufferCreate", libxml_xmlCreateOutputBuffer, METH_VARARGS, NULL},
2565 {(char *) "inputBufferCreate", libxml_xmlCreateInputBuffer, METH_VARARGS, NULL},
2566 {(char *) "setEntityLoader", libxml_xmlSetEntityLoader, METH_VARARGS, NULL},
2567 {(char *)"xmlRegisterErrorHandler", libxml_xmlRegisterErrorHandler, METH_VARARGS, NULL },
2568 {(char *)"xmlParserCtxtSetErrorHandler", libxml_xmlParserCtxtSetErrorHandler, METH_VARARGS, NULL },
2569 {(char *)"xmlParserCtxtGetErrorHandler", libxml_xmlParserCtxtGetErrorHandler, METH_VARARGS, NULL },
2570 {(char *)"xmlFreeParserCtxt", libxml_xmlFreeParserCtxt, METH_VARARGS, NULL },
2571 {(char *)"xmlTextReaderSetErrorHandler", libxml_xmlTextReaderSetErrorHandler, METH_VARARGS, NULL },
2572 {(char *)"xmlTextReaderGetErrorHandler", libxml_xmlTextReaderGetErrorHandler, METH_VARARGS, NULL },
2573 {(char *)"xmlFreeTextReader", libxml_xmlFreeTextReader, METH_VARARGS, NULL },
2574 {NULL, NULL, 0, NULL}
2577 #ifdef MERGED_MODULES
2578 extern void initlibxsltmod(void);
2582 initlibxml2mod(void)
2584 static int initialized = 0;
2587 if (initialized != 0)
2589 xmlRegisterDefaultOutputCallbacks();
2590 xmlRegisterDefaultInputCallbacks();
2591 m = Py_InitModule((char *) "libxml2mod", libxmlMethods);
2593 libxml_xmlErrorInitialize();
2595 #ifdef MERGED_MODULES