blob: 95b3cb922076826810ee6d2291282743e18de518 [file] [log] [blame]
Daniel Veillard3ce52572002-02-03 15:08:05 +00001/*
2 * libxml.c: this modules implements the main part of the glue of the
3 * libxml2 library and the Python interpreter. It provides the
4 * entry points where an automatically generated stub is either
5 * unpractical or would not match cleanly the Python model.
6 *
Daniel Veillard0fea6f42002-02-22 22:51:13 +00007 * If compiled with MERGED_MODULES, the entry point will be used to
8 * initialize both the libxml2 and the libxslt wrappers
9 *
Daniel Veillard3ce52572002-02-03 15:08:05 +000010 * See Copyright for the status of this software.
11 *
12 * daniel@veillard.com
13 */
Daniel Veillardd2897fd2002-01-30 16:37:32 +000014#include <Python.h>
Daniel Veillardc6d4a932002-09-12 15:00:57 +000015#include <fileobject.h>
Daniel Veillarda1196ed2002-11-23 11:22:49 +000016/* #include "config.h" */
Daniel Veillardf1d0e6b2002-01-31 23:42:44 +000017#include <libxml/xmlmemory.h>
Daniel Veillardd2897fd2002-01-30 16:37:32 +000018#include <libxml/parser.h>
19#include <libxml/tree.h>
Daniel Veillarda7340c82002-02-01 17:56:45 +000020#include <libxml/xpath.h>
Daniel Veillard5d819032002-02-02 21:49:17 +000021#include <libxml/xmlerror.h>
Daniel Veillarda7340c82002-02-01 17:56:45 +000022#include <libxml/xpathInternals.h>
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000023#include <libxml/xmlmemory.h>
Daniel Veillardc6d4a932002-09-12 15:00:57 +000024#include <libxml/xmlIO.h>
Daniel Veillardd5e198a2004-03-09 09:03:28 +000025#include <libxml/c14n.h>
Daniel Veillard438ebbd2008-05-12 12:58:46 +000026#include <libxml/xmlreader.h>
Daniel Veillardd2897fd2002-01-30 16:37:32 +000027#include "libxml_wrap.h"
Daniel Veillard96fe0952002-01-30 20:52:23 +000028#include "libxml2-py.h"
Daniel Veillardd2897fd2002-01-30 16:37:32 +000029
Daniel Veillard0d132cf2002-12-23 14:43:32 +000030#if (defined(_MSC_VER) || defined(__MINGW32__)) && !defined(vsnprintf)
31#define vsnprintf(b,c,f,a) _vsnprintf(b,c,f,a)
Daniel Veillarde4fa2932003-03-26 00:38:10 +000032#elif defined(WITH_TRIO)
33#include "trio.h"
34#define vsnprintf trio_vsnprintf
Daniel Veillard0d132cf2002-12-23 14:43:32 +000035#endif
36
Daniel Veillardd2897fd2002-01-30 16:37:32 +000037/* #define DEBUG */
Daniel Veillard797a5652002-02-12 13:46:21 +000038/* #define DEBUG_SAX */
Daniel Veillarda7340c82002-02-01 17:56:45 +000039/* #define DEBUG_XPATH */
Daniel Veillard5d819032002-02-02 21:49:17 +000040/* #define DEBUG_ERROR */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000041/* #define DEBUG_MEMORY */
Daniel Veillardc6d4a932002-09-12 15:00:57 +000042/* #define DEBUG_FILES */
43/* #define DEBUG_LOADER */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000044
Daniel Veillardd2379012002-03-15 22:24:56 +000045void initlibxml2mod(void);
46
Daniel Veillardc6d4a932002-09-12 15:00:57 +000047/**
48 * TODO:
49 *
50 * macro to flag unimplemented blocks
51 */
52#define TODO \
53 xmlGenericError(xmlGenericErrorContext, \
54 "Unimplemented block at %s:%d\n", \
55 __FILE__, __LINE__);
William M. Brack8e2cc6f2004-07-03 23:28:52 +000056/*
57 * the following vars are used for XPath extensions, but
58 * are also referenced within the parser cleanup routine.
59 */
60static int libxml_xpathCallbacksInitialized = 0;
61
62typedef struct libxml_xpathCallback {
63 xmlXPathContextPtr ctx;
64 xmlChar *name;
65 xmlChar *ns_uri;
66 PyObject *function;
67} libxml_xpathCallback, *libxml_xpathCallbackPtr;
68typedef libxml_xpathCallback libxml_xpathCallbackArray[];
69static int libxml_xpathCallbacksAllocd = 10;
70static libxml_xpathCallbackArray *libxml_xpathCallbacks = NULL;
71static int libxml_xpathCallbacksNb = 0;
Daniel Veillardc6d4a932002-09-12 15:00:57 +000072
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000073/************************************************************************
74 * *
75 * Memory debug interface *
76 * *
77 ************************************************************************/
78
Daniel Veillardc2664642003-07-29 20:44:53 +000079#if 0
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000080extern void xmlMemFree(void *ptr);
81extern void *xmlMemMalloc(size_t size);
Daniel Veillardd2379012002-03-15 22:24:56 +000082extern void *xmlMemRealloc(void *ptr, size_t size);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000083extern char *xmlMemoryStrdup(const char *str);
Daniel Veillardc2664642003-07-29 20:44:53 +000084#endif
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000085
86static int libxmlMemoryDebugActivated = 0;
87static long libxmlMemoryAllocatedBase = 0;
88
89static int libxmlMemoryDebug = 0;
90static xmlFreeFunc freeFunc = NULL;
91static xmlMallocFunc mallocFunc = NULL;
92static xmlReallocFunc reallocFunc = NULL;
93static xmlStrdupFunc strdupFunc = NULL;
94
Daniel Veillardf93a8662004-07-01 12:56:30 +000095static void
96libxml_xmlErrorInitialize(void); /* forward declare */
97
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000098PyObject *
William M. Brackc68d78d2004-07-16 10:39:30 +000099libxml_xmlMemoryUsed(PyObject * self ATTRIBUTE_UNUSED,
100 PyObject * args ATTRIBUTE_UNUSED)
Daniel Veillard529233c2004-07-02 12:23:44 +0000101{
102 long ret;
103 PyObject *py_retval;
104
105 ret = xmlMemUsed();
106
107 py_retval = libxml_longWrap(ret);
108 return (py_retval);
109}
110
111PyObject *
William M. Brackc68d78d2004-07-16 10:39:30 +0000112libxml_xmlDebugMemory(PyObject * self ATTRIBUTE_UNUSED, PyObject * args)
Daniel Veillardd2379012002-03-15 22:24:56 +0000113{
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000114 int activate;
115 PyObject *py_retval;
116 long ret;
117
Daniel Veillardd2379012002-03-15 22:24:56 +0000118 if (!PyArg_ParseTuple(args, (char *) "i:xmlDebugMemory", &activate))
119 return (NULL);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000120
121#ifdef DEBUG_MEMORY
122 printf("libxml_xmlDebugMemory(%d) called\n", activate);
123#endif
124
125 if (activate != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000126 if (libxmlMemoryDebug == 0) {
127 /*
128 * First initialize the library and grab the old memory handlers
129 * and switch the library to memory debugging
130 */
131 xmlMemGet((xmlFreeFunc *) & freeFunc,
132 (xmlMallocFunc *) & mallocFunc,
133 (xmlReallocFunc *) & reallocFunc,
134 (xmlStrdupFunc *) & strdupFunc);
135 if ((freeFunc == xmlMemFree) && (mallocFunc == xmlMemMalloc) &&
136 (reallocFunc == xmlMemRealloc) &&
137 (strdupFunc == xmlMemoryStrdup)) {
138 libxmlMemoryAllocatedBase = xmlMemUsed();
139 } else {
Daniel Veillardf93a8662004-07-01 12:56:30 +0000140 /*
141 * cleanup first, because some memory has been
142 * allocated with the non-debug malloc in xmlInitParser
143 * when the python module was imported
144 */
145 xmlCleanupParser();
Daniel Veillardd2379012002-03-15 22:24:56 +0000146 ret = (long) xmlMemSetup(xmlMemFree, xmlMemMalloc,
147 xmlMemRealloc, xmlMemoryStrdup);
148 if (ret < 0)
149 goto error;
150 libxmlMemoryAllocatedBase = xmlMemUsed();
Daniel Veillardf93a8662004-07-01 12:56:30 +0000151 /* reinitialize */
152 xmlInitParser();
153 libxml_xmlErrorInitialize();
Daniel Veillardd2379012002-03-15 22:24:56 +0000154 }
Daniel Veillardd2379012002-03-15 22:24:56 +0000155 ret = 0;
156 } else if (libxmlMemoryDebugActivated == 0) {
157 libxmlMemoryAllocatedBase = xmlMemUsed();
158 ret = 0;
159 } else {
160 ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
161 }
162 libxmlMemoryDebug = 1;
163 libxmlMemoryDebugActivated = 1;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000164 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +0000165 if (libxmlMemoryDebugActivated == 1)
166 ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
167 else
168 ret = 0;
169 libxmlMemoryDebugActivated = 0;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000170 }
Daniel Veillardd2379012002-03-15 22:24:56 +0000171 error:
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000172 py_retval = libxml_longWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +0000173 return (py_retval);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000174}
175
176PyObject *
Daniel Veillardf93a8662004-07-01 12:56:30 +0000177libxml_xmlPythonCleanupParser(PyObject *self ATTRIBUTE_UNUSED,
178 PyObject *args ATTRIBUTE_UNUSED) {
179
William M. Brack8e2cc6f2004-07-03 23:28:52 +0000180 int ix;
181 long freed = -1;
Daniel Veillardf93a8662004-07-01 12:56:30 +0000182
183 if (libxmlMemoryDebug) {
184 freed = xmlMemUsed();
185 }
186
187 xmlCleanupParser();
William M. Brack8e2cc6f2004-07-03 23:28:52 +0000188 /*
189 * Need to confirm whether we really want to do this (required for
190 * memcheck) in all cases...
191 */
192
193 if (libxml_xpathCallbacks != NULL) { /* if ext funcs declared */
194 for (ix=0; ix<libxml_xpathCallbacksNb; ix++) {
195 if ((*libxml_xpathCallbacks)[ix].name != NULL)
196 xmlFree((*libxml_xpathCallbacks)[ix].name);
197 if ((*libxml_xpathCallbacks)[ix].ns_uri != NULL)
198 xmlFree((*libxml_xpathCallbacks)[ix].ns_uri);
199 }
200 libxml_xpathCallbacksNb = 0;
201 xmlFree(libxml_xpathCallbacks);
202 libxml_xpathCallbacks = NULL;
203 }
Daniel Veillardf93a8662004-07-01 12:56:30 +0000204
205 if (libxmlMemoryDebug) {
206 freed -= xmlMemUsed();
207 libxmlMemoryAllocatedBase -= freed;
208 if (libxmlMemoryAllocatedBase < 0)
209 libxmlMemoryAllocatedBase = 0;
210 }
211
212 Py_INCREF(Py_None);
213 return(Py_None);
214}
215
216PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +0000217libxml_xmlDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
218 ATTRIBUTE_UNUSED PyObject * args)
219{
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000220
221 if (libxmlMemoryDebug != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +0000222 xmlMemoryDump();
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000223 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +0000224 return (Py_None);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000225}
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000226
227/************************************************************************
228 * *
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000229 * Handling Python FILE I/O at the C level *
230 * The raw I/O attack diectly the File objects, while the *
231 * other routines address the ioWrapper instance instead *
232 * *
233 ************************************************************************/
234
235/**
236 * xmlPythonFileCloseUnref:
237 * @context: the I/O context
238 *
239 * Close an I/O channel
240 */
241static int
242xmlPythonFileCloseRaw (void * context) {
243 PyObject *file, *ret;
244
245#ifdef DEBUG_FILES
246 printf("xmlPythonFileCloseUnref\n");
247#endif
248 file = (PyObject *) context;
249 if (file == NULL) return(-1);
Daniel Veillard118aed72002-09-24 14:13:13 +0000250 ret = PyEval_CallMethod(file, (char *) "close", (char *) "()");
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000251 if (ret != NULL) {
252 Py_DECREF(ret);
253 }
254 Py_DECREF(file);
255 return(0);
256}
257
258/**
259 * xmlPythonFileReadRaw:
260 * @context: the I/O context
261 * @buffer: where to drop data
262 * @len: number of bytes to write
263 *
264 * Read @len bytes to @buffer from the Python file in the I/O channel
265 *
266 * Returns the number of bytes read
267 */
268static int
269xmlPythonFileReadRaw (void * context, char * buffer, int len) {
270 PyObject *file;
271 PyObject *ret;
272 int lenread = -1;
273 char *data;
274
275#ifdef DEBUG_FILES
276 printf("xmlPythonFileReadRaw: %d\n", len);
277#endif
278 file = (PyObject *) context;
279 if (file == NULL) return(-1);
Daniel Veillard118aed72002-09-24 14:13:13 +0000280 ret = PyEval_CallMethod(file, (char *) "read", (char *) "(i)", len);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000281 if (ret == NULL) {
282 printf("xmlPythonFileReadRaw: result is NULL\n");
283 return(-1);
284 } else if (PyString_Check(ret)) {
285 lenread = PyString_Size(ret);
286 data = PyString_AsString(ret);
287 if (lenread > len)
288 memcpy(buffer, data, len);
289 else
290 memcpy(buffer, data, lenread);
291 Py_DECREF(ret);
292 } else {
293 printf("xmlPythonFileReadRaw: result is not a String\n");
294 Py_DECREF(ret);
295 }
296 return(lenread);
297}
298
299/**
300 * xmlPythonFileRead:
301 * @context: the I/O context
302 * @buffer: where to drop data
303 * @len: number of bytes to write
304 *
305 * Read @len bytes to @buffer from the I/O channel.
306 *
307 * Returns the number of bytes read
308 */
309static int
310xmlPythonFileRead (void * context, char * buffer, int len) {
311 PyObject *file;
312 PyObject *ret;
313 int lenread = -1;
314 char *data;
315
316#ifdef DEBUG_FILES
317 printf("xmlPythonFileRead: %d\n", len);
318#endif
319 file = (PyObject *) context;
320 if (file == NULL) return(-1);
Daniel Veillard118aed72002-09-24 14:13:13 +0000321 ret = PyEval_CallMethod(file, (char *) "io_read", (char *) "(i)", len);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000322 if (ret == NULL) {
323 printf("xmlPythonFileRead: result is NULL\n");
324 return(-1);
325 } else if (PyString_Check(ret)) {
326 lenread = PyString_Size(ret);
327 data = PyString_AsString(ret);
328 if (lenread > len)
329 memcpy(buffer, data, len);
330 else
331 memcpy(buffer, data, lenread);
332 Py_DECREF(ret);
333 } else {
334 printf("xmlPythonFileRead: result is not a String\n");
335 Py_DECREF(ret);
336 }
337 return(lenread);
338}
339
340/**
341 * xmlFileWrite:
342 * @context: the I/O context
343 * @buffer: where to drop data
344 * @len: number of bytes to write
345 *
346 * Write @len bytes from @buffer to the I/O channel.
347 *
348 * Returns the number of bytes written
349 */
350static int
351xmlPythonFileWrite (void * context, const char * buffer, int len) {
352 PyObject *file;
353 PyObject *string;
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000354 PyObject *ret = NULL;
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000355 int written = -1;
356
357#ifdef DEBUG_FILES
358 printf("xmlPythonFileWrite: %d\n", len);
359#endif
360 file = (PyObject *) context;
361 if (file == NULL) return(-1);
362 string = PyString_FromStringAndSize(buffer, len);
363 if (string == NULL) return(-1);
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000364 if (PyObject_HasAttrString(file, (char *) "io_write")) {
365 ret = PyEval_CallMethod(file, (char *) "io_write", (char *) "(O)",
366 string);
367 } else if (PyObject_HasAttrString(file, (char *) "write")) {
368 ret = PyEval_CallMethod(file, (char *) "write", (char *) "(O)",
369 string);
370 }
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000371 Py_DECREF(string);
372 if (ret == NULL) {
373 printf("xmlPythonFileWrite: result is NULL\n");
374 return(-1);
375 } else if (PyInt_Check(ret)) {
376 written = (int) PyInt_AsLong(ret);
377 Py_DECREF(ret);
378 } else if (ret == Py_None) {
379 written = len;
380 Py_DECREF(ret);
381 } else {
382 printf("xmlPythonFileWrite: result is not an Int nor None\n");
383 Py_DECREF(ret);
384 }
385 return(written);
386}
387
388/**
389 * xmlPythonFileClose:
390 * @context: the I/O context
391 *
392 * Close an I/O channel
393 */
394static int
395xmlPythonFileClose (void * context) {
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000396 PyObject *file, *ret = NULL;
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000397
398#ifdef DEBUG_FILES
399 printf("xmlPythonFileClose\n");
400#endif
401 file = (PyObject *) context;
402 if (file == NULL) return(-1);
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000403 if (PyObject_HasAttrString(file, (char *) "io_close")) {
404 ret = PyEval_CallMethod(file, (char *) "io_close", (char *) "()");
405 } else if (PyObject_HasAttrString(file, (char *) "flush")) {
406 ret = PyEval_CallMethod(file, (char *) "flush", (char *) "()");
407 }
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000408 if (ret != NULL) {
409 Py_DECREF(ret);
410 }
411 return(0);
412}
413
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000414#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000415/**
416 * xmlOutputBufferCreatePythonFile:
417 * @file: a PyFile_Type
418 * @encoder: the encoding converter or NULL
419 *
420 * Create a buffered output for the progressive saving to a PyFile_Type
421 * buffered C I/O
422 *
423 * Returns the new parser output or NULL
424 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000425static xmlOutputBufferPtr
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000426xmlOutputBufferCreatePythonFile(PyObject *file,
427 xmlCharEncodingHandlerPtr encoder) {
428 xmlOutputBufferPtr ret;
429
430 if (file == NULL) return(NULL);
431
432 ret = xmlAllocOutputBuffer(encoder);
433 if (ret != NULL) {
434 ret->context = file;
435 /* Py_INCREF(file); */
436 ret->writecallback = xmlPythonFileWrite;
437 ret->closecallback = xmlPythonFileClose;
438 }
439
440 return(ret);
441}
442
443PyObject *
444libxml_xmlCreateOutputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
445 PyObject *py_retval;
446 PyObject *file;
447 xmlChar *encoding;
448 xmlCharEncodingHandlerPtr handler = NULL;
449 xmlOutputBufferPtr buffer;
450
451
452 if (!PyArg_ParseTuple(args, (char *)"Oz:xmlOutputBufferCreate",
453 &file, &encoding))
454 return(NULL);
455 if ((encoding != NULL) && (encoding[0] != 0)) {
Daniel Veillard118aed72002-09-24 14:13:13 +0000456 handler = xmlFindCharEncodingHandler((const char *) encoding);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000457 }
458 buffer = xmlOutputBufferCreatePythonFile(file, handler);
459 if (buffer == NULL)
460 printf("libxml_xmlCreateOutputBuffer: buffer == NULL\n");
461 py_retval = libxml_xmlOutputBufferPtrWrap(buffer);
462 return(py_retval);
463}
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000464
465/**
466 * libxml_outputBufferGetPythonFile:
467 * @buffer: the I/O buffer
468 *
469 * read the Python I/O from the CObject
470 *
471 * Returns the new parser output or NULL
472 */
473static PyObject *
474libxml_outputBufferGetPythonFile(ATTRIBUTE_UNUSED PyObject *self,
475 PyObject *args) {
476 PyObject *buffer;
477 PyObject *file;
478 xmlOutputBufferPtr obj;
479
480 if (!PyArg_ParseTuple(args, (char *)"O:outputBufferGetPythonFile",
481 &buffer))
482 return(NULL);
483
484 obj = PyoutputBuffer_Get(buffer);
485 if (obj == NULL) {
486 fprintf(stderr,
487 "outputBufferGetPythonFile: obj == NULL\n");
488 Py_INCREF(Py_None);
489 return(Py_None);
490 }
491 if (obj->closecallback != xmlPythonFileClose) {
492 fprintf(stderr,
493 "outputBufferGetPythonFile: not a python file wrapper\n");
494 Py_INCREF(Py_None);
495 return(Py_None);
496 }
497 file = (PyObject *) obj->context;
498 if (file == NULL) {
499 Py_INCREF(Py_None);
500 return(Py_None);
501 }
502 Py_INCREF(file);
503 return(file);
504}
505
Daniel Veillardd5e198a2004-03-09 09:03:28 +0000506static PyObject *
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000507libxml_xmlOutputBufferClose(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
508 PyObject *py_retval;
509 int c_retval;
510 xmlOutputBufferPtr out;
511 PyObject *pyobj_out;
512
513 if (!PyArg_ParseTuple(args, (char *)"O:xmlOutputBufferClose", &pyobj_out))
514 return(NULL);
515 out = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_out);
Daniel Veillard263ec862004-10-04 10:26:54 +0000516 /* Buffer may already have been destroyed elsewhere. This is harmless. */
517 if (out == NULL) {
518 Py_INCREF(Py_None);
519 return(Py_None);
520 }
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000521
522 c_retval = xmlOutputBufferClose(out);
523 py_retval = libxml_intWrap((int) c_retval);
524 return(py_retval);
525}
526
Daniel Veillardd5e198a2004-03-09 09:03:28 +0000527static PyObject *
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000528libxml_xmlOutputBufferFlush(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
529 PyObject *py_retval;
530 int c_retval;
531 xmlOutputBufferPtr out;
532 PyObject *pyobj_out;
533
534 if (!PyArg_ParseTuple(args, (char *)"O:xmlOutputBufferFlush", &pyobj_out))
535 return(NULL);
536 out = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_out);
537
538 c_retval = xmlOutputBufferFlush(out);
539 py_retval = libxml_intWrap((int) c_retval);
540 return(py_retval);
541}
Daniel Veillard263ec862004-10-04 10:26:54 +0000542
543static PyObject *
544libxml_xmlSaveFileTo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
545 PyObject *py_retval;
546 int c_retval;
547 xmlOutputBufferPtr buf;
548 PyObject *pyobj_buf;
549 xmlDocPtr cur;
550 PyObject *pyobj_cur;
551 char * encoding;
552
553 if (!PyArg_ParseTuple(args, (char *)"OOz:xmlSaveFileTo", &pyobj_buf, &pyobj_cur, &encoding))
554 return(NULL);
555 buf = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_buf);
556 cur = (xmlDocPtr) PyxmlNode_Get(pyobj_cur);
557
558 c_retval = xmlSaveFileTo(buf, cur, encoding);
559 /* xmlSaveTo() freed the memory pointed to by buf, so record that in the
560 * Python object. */
561 ((PyoutputBuffer_Object *)(pyobj_buf))->obj = NULL;
562 py_retval = libxml_intWrap((int) c_retval);
563 return(py_retval);
564}
565
566static PyObject *
567libxml_xmlSaveFormatFileTo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
568 PyObject *py_retval;
569 int c_retval;
570 xmlOutputBufferPtr buf;
571 PyObject *pyobj_buf;
572 xmlDocPtr cur;
573 PyObject *pyobj_cur;
574 char * encoding;
575 int format;
576
577 if (!PyArg_ParseTuple(args, (char *)"OOzi:xmlSaveFormatFileTo", &pyobj_buf, &pyobj_cur, &encoding, &format))
578 return(NULL);
579 buf = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_buf);
580 cur = (xmlDocPtr) PyxmlNode_Get(pyobj_cur);
581
582 c_retval = xmlSaveFormatFileTo(buf, cur, encoding, format);
583 /* xmlSaveFormatFileTo() freed the memory pointed to by buf, so record that
584 * in the Python object */
585 ((PyoutputBuffer_Object *)(pyobj_buf))->obj = NULL;
586 py_retval = libxml_intWrap((int) c_retval);
587 return(py_retval);
588}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000589#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000590
591
592/**
593 * xmlParserInputBufferCreatePythonFile:
594 * @file: a PyFile_Type
595 * @encoder: the encoding converter or NULL
596 *
597 * Create a buffered output for the progressive saving to a PyFile_Type
598 * buffered C I/O
599 *
600 * Returns the new parser output or NULL
601 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000602static xmlParserInputBufferPtr
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000603xmlParserInputBufferCreatePythonFile(PyObject *file,
604 xmlCharEncoding encoding) {
605 xmlParserInputBufferPtr ret;
606
607 if (file == NULL) return(NULL);
608
609 ret = xmlAllocParserInputBuffer(encoding);
610 if (ret != NULL) {
611 ret->context = file;
612 /* Py_INCREF(file); */
613 ret->readcallback = xmlPythonFileRead;
614 ret->closecallback = xmlPythonFileClose;
615 }
616
617 return(ret);
618}
619
620PyObject *
621libxml_xmlCreateInputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
622 PyObject *py_retval;
623 PyObject *file;
624 xmlChar *encoding;
625 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
626 xmlParserInputBufferPtr buffer;
627
628
629 if (!PyArg_ParseTuple(args, (char *)"Oz:xmlParserInputBufferCreate",
630 &file, &encoding))
631 return(NULL);
632 if ((encoding != NULL) && (encoding[0] != 0)) {
Daniel Veillard118aed72002-09-24 14:13:13 +0000633 enc = xmlParseCharEncoding((const char *) encoding);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000634 }
635 buffer = xmlParserInputBufferCreatePythonFile(file, enc);
636 if (buffer == NULL)
637 printf("libxml_xmlParserInputBufferCreate: buffer == NULL\n");
638 py_retval = libxml_xmlParserInputBufferPtrWrap(buffer);
639 return(py_retval);
640}
641
642/************************************************************************
643 * *
644 * Providing the resolver at the Python level *
645 * *
646 ************************************************************************/
647
648static xmlExternalEntityLoader defaultExternalEntityLoader = NULL;
649static PyObject *pythonExternalEntityLoaderObjext;
650
651static xmlParserInputPtr
652pythonExternalEntityLoader(const char *URL, const char *ID,
653 xmlParserCtxtPtr ctxt) {
654 xmlParserInputPtr result = NULL;
655 if (pythonExternalEntityLoaderObjext != NULL) {
656 PyObject *ret;
657 PyObject *ctxtobj;
658
659 ctxtobj = libxml_xmlParserCtxtPtrWrap(ctxt);
660#ifdef DEBUG_LOADER
661 printf("pythonExternalEntityLoader: ready to call\n");
662#endif
663
664 ret = PyObject_CallFunction(pythonExternalEntityLoaderObjext,
Daniel Veillard118aed72002-09-24 14:13:13 +0000665 (char *) "(ssO)", URL, ID, ctxtobj);
Daniel Veillarde4a07e72003-01-14 14:40:25 +0000666 Py_XDECREF(ctxtobj);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000667#ifdef DEBUG_LOADER
668 printf("pythonExternalEntityLoader: result ");
Daniel Veillard007d51e2003-09-17 20:07:28 +0000669 PyObject_Print(ret, stderr, 0);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000670 printf("\n");
671#endif
672
673 if (ret != NULL) {
Daniel Veillard118aed72002-09-24 14:13:13 +0000674 if (PyObject_HasAttrString(ret, (char *) "read")) {
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000675 xmlParserInputBufferPtr buf;
676
677 buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
678 if (buf != NULL) {
679 buf->context = ret;
680 buf->readcallback = xmlPythonFileReadRaw;
681 buf->closecallback = xmlPythonFileCloseRaw;
682 result = xmlNewIOInputStream(ctxt, buf,
683 XML_CHAR_ENCODING_NONE);
684 }
Daniel Veillard4ea89f02005-07-29 10:12:45 +0000685#if 0
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000686 } else {
Daniel Veillard4ea89f02005-07-29 10:12:45 +0000687 if (URL != NULL)
688 printf("pythonExternalEntityLoader: can't read %s\n",
689 URL);
690#endif
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000691 }
692 if (result == NULL) {
693 Py_DECREF(ret);
Daniel Veillardc64b8e92003-02-24 11:47:13 +0000694 } else if (URL != NULL) {
William M. Brackc1939562003-08-05 15:52:22 +0000695 result->filename = (char *) xmlStrdup((const xmlChar *)URL);
Daniel Veillardc64b8e92003-02-24 11:47:13 +0000696 result->directory = xmlParserGetDirectory((const char *) URL);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000697 }
698 }
699 }
700 if ((result == NULL) && (defaultExternalEntityLoader != NULL)) {
701 result = defaultExternalEntityLoader(URL, ID, ctxt);
702 }
703 return(result);
704}
705
706PyObject *
707libxml_xmlSetEntityLoader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
708 PyObject *py_retval;
709 PyObject *loader;
710
711 if (!PyArg_ParseTuple(args, (char *)"O:libxml_xmlSetEntityLoader",
712 &loader))
713 return(NULL);
714
715#ifdef DEBUG_LOADER
716 printf("libxml_xmlSetEntityLoader\n");
717#endif
718 if (defaultExternalEntityLoader == NULL)
719 defaultExternalEntityLoader = xmlGetExternalEntityLoader();
720
721 pythonExternalEntityLoaderObjext = loader;
722 xmlSetExternalEntityLoader(pythonExternalEntityLoader);
723
724 py_retval = PyInt_FromLong(0);
725 return(py_retval);
726}
727
728
729/************************************************************************
730 * *
Daniel Veillard3ce52572002-02-03 15:08:05 +0000731 * Handling SAX/xmllib/sgmlop callback interfaces *
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000732 * *
733 ************************************************************************/
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000734
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000735static void
736pythonStartElement(void *user_data, const xmlChar * name,
737 const xmlChar ** attrs)
738{
739 int i;
740 PyObject *handler;
741 PyObject *dict;
742 PyObject *attrname;
743 PyObject *attrvalue;
Daniel Veillardd2379012002-03-15 22:24:56 +0000744 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000745 int type = 0;
746
Daniel Veillard797a5652002-02-12 13:46:21 +0000747#ifdef DEBUG_SAX
748 printf("pythonStartElement(%s) called\n", name);
749#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000750 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000751 if (PyObject_HasAttrString(handler, (char *) "startElement"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000752 type = 1;
Daniel Veillardd2379012002-03-15 22:24:56 +0000753 else if (PyObject_HasAttrString(handler, (char *) "start"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000754 type = 2;
755 if (type != 0) {
756 /*
757 * the xmllib interface always generate a dictionnary,
758 * possibly empty
759 */
760 if ((attrs == NULL) && (type == 1)) {
761 Py_XINCREF(Py_None);
762 dict = Py_None;
Daniel Veillardd2379012002-03-15 22:24:56 +0000763 } else if (attrs == NULL) {
764 dict = PyDict_New();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000765 } else {
766 dict = PyDict_New();
767 for (i = 0; attrs[i] != NULL; i++) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000768 attrname = PyString_FromString((char *) attrs[i]);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000769 i++;
770 if (attrs[i] != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000771 attrvalue = PyString_FromString((char *) attrs[i]);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000772 } else {
773 Py_XINCREF(Py_None);
774 attrvalue = Py_None;
775 }
776 PyDict_SetItem(dict, attrname, attrvalue);
Daniel Veillarda3d23052007-01-09 21:24:34 +0000777 Py_DECREF(attrname);
778 Py_DECREF(attrvalue);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000779 }
780 }
781
782 if (type == 1)
Daniel Veillardd2379012002-03-15 22:24:56 +0000783 result = PyObject_CallMethod(handler, (char *) "startElement",
784 (char *) "sO", name, dict);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000785 else if (type == 2)
Daniel Veillardd2379012002-03-15 22:24:56 +0000786 result = PyObject_CallMethod(handler, (char *) "start",
787 (char *) "sO", name, dict);
788 if (PyErr_Occurred())
789 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000790 Py_XDECREF(dict);
791 Py_XDECREF(result);
792 }
793}
794
795static void
796pythonStartDocument(void *user_data)
797{
798 PyObject *handler;
799 PyObject *result;
800
Daniel Veillard797a5652002-02-12 13:46:21 +0000801#ifdef DEBUG_SAX
802 printf("pythonStartDocument() called\n");
803#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000804 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000805 if (PyObject_HasAttrString(handler, (char *) "startDocument")) {
806 result =
807 PyObject_CallMethod(handler, (char *) "startDocument", NULL);
808 if (PyErr_Occurred())
809 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000810 Py_XDECREF(result);
811 }
812}
813
814static void
815pythonEndDocument(void *user_data)
816{
817 PyObject *handler;
818 PyObject *result;
819
Daniel Veillard797a5652002-02-12 13:46:21 +0000820#ifdef DEBUG_SAX
821 printf("pythonEndDocument() called\n");
822#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000823 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000824 if (PyObject_HasAttrString(handler, (char *) "endDocument")) {
825 result =
826 PyObject_CallMethod(handler, (char *) "endDocument", NULL);
827 if (PyErr_Occurred())
828 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000829 Py_XDECREF(result);
830 }
831 /*
832 * The reference to the handler is released there
833 */
834 Py_XDECREF(handler);
835}
836
837static void
838pythonEndElement(void *user_data, const xmlChar * name)
839{
840 PyObject *handler;
841 PyObject *result;
842
Daniel Veillard797a5652002-02-12 13:46:21 +0000843#ifdef DEBUG_SAX
844 printf("pythonEndElement(%s) called\n", name);
845#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000846 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000847 if (PyObject_HasAttrString(handler, (char *) "endElement")) {
848 result = PyObject_CallMethod(handler, (char *) "endElement",
849 (char *) "s", name);
850 if (PyErr_Occurred())
851 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000852 Py_XDECREF(result);
Daniel Veillardd2379012002-03-15 22:24:56 +0000853 } else if (PyObject_HasAttrString(handler, (char *) "end")) {
854 result = PyObject_CallMethod(handler, (char *) "end",
855 (char *) "s", name);
856 if (PyErr_Occurred())
857 PyErr_Print();
Daniel Veillard797a5652002-02-12 13:46:21 +0000858 Py_XDECREF(result);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000859 }
860}
861
862static void
863pythonReference(void *user_data, const xmlChar * name)
864{
865 PyObject *handler;
866 PyObject *result;
867
Daniel Veillard797a5652002-02-12 13:46:21 +0000868#ifdef DEBUG_SAX
869 printf("pythonReference(%s) called\n", name);
870#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000871 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000872 if (PyObject_HasAttrString(handler, (char *) "reference")) {
873 result = PyObject_CallMethod(handler, (char *) "reference",
874 (char *) "s", name);
875 if (PyErr_Occurred())
876 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000877 Py_XDECREF(result);
878 }
879}
880
881static void
882pythonCharacters(void *user_data, const xmlChar * ch, int len)
883{
884 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +0000885 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000886 int type = 0;
887
Daniel Veillard797a5652002-02-12 13:46:21 +0000888#ifdef DEBUG_SAX
889 printf("pythonCharacters(%s, %d) called\n", ch, len);
890#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000891 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000892 if (PyObject_HasAttrString(handler, (char *) "characters"))
893 type = 1;
894 else if (PyObject_HasAttrString(handler, (char *) "data"))
895 type = 2;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000896 if (type != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000897 if (type == 1)
898 result = PyObject_CallMethod(handler, (char *) "characters",
899 (char *) "s#", ch, len);
900 else if (type == 2)
901 result = PyObject_CallMethod(handler, (char *) "data",
902 (char *) "s#", ch, len);
903 if (PyErr_Occurred())
904 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000905 Py_XDECREF(result);
906 }
907}
908
909static void
910pythonIgnorableWhitespace(void *user_data, const xmlChar * ch, int len)
911{
912 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +0000913 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000914 int type = 0;
915
Daniel Veillard797a5652002-02-12 13:46:21 +0000916#ifdef DEBUG_SAX
917 printf("pythonIgnorableWhitespace(%s, %d) called\n", ch, len);
918#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000919 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000920 if (PyObject_HasAttrString(handler, (char *) "ignorableWhitespace"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000921 type = 1;
Daniel Veillardd2379012002-03-15 22:24:56 +0000922 else if (PyObject_HasAttrString(handler, (char *) "data"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000923 type = 2;
924 if (type != 0) {
925 if (type == 1)
926 result =
Daniel Veillardd2379012002-03-15 22:24:56 +0000927 PyObject_CallMethod(handler,
928 (char *) "ignorableWhitespace",
929 (char *) "s#", ch, len);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000930 else if (type == 2)
Daniel Veillardd2379012002-03-15 22:24:56 +0000931 result =
932 PyObject_CallMethod(handler, (char *) "data",
933 (char *) "s#", ch, len);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000934 Py_XDECREF(result);
935 }
936}
937
938static void
939pythonProcessingInstruction(void *user_data,
940 const xmlChar * target, const xmlChar * data)
941{
942 PyObject *handler;
943 PyObject *result;
944
Daniel Veillard797a5652002-02-12 13:46:21 +0000945#ifdef DEBUG_SAX
946 printf("pythonProcessingInstruction(%s, %s) called\n", target, data);
947#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000948 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000949 if (PyObject_HasAttrString(handler, (char *) "processingInstruction")) {
950 result = PyObject_CallMethod(handler, (char *)
951 "processingInstruction",
952 (char *) "ss", target, data);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000953 Py_XDECREF(result);
954 }
955}
956
957static void
958pythonComment(void *user_data, const xmlChar * value)
959{
960 PyObject *handler;
961 PyObject *result;
962
Daniel Veillard797a5652002-02-12 13:46:21 +0000963#ifdef DEBUG_SAX
964 printf("pythonComment(%s) called\n", value);
965#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000966 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000967 if (PyObject_HasAttrString(handler, (char *) "comment")) {
968 result =
969 PyObject_CallMethod(handler, (char *) "comment", (char *) "s",
970 value);
971 if (PyErr_Occurred())
972 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000973 Py_XDECREF(result);
974 }
975}
976
977static void
978pythonWarning(void *user_data, const char *msg, ...)
979{
980 PyObject *handler;
981 PyObject *result;
982 va_list args;
983 char buf[1024];
984
Daniel Veillard797a5652002-02-12 13:46:21 +0000985#ifdef DEBUG_SAX
986 printf("pythonWarning(%s) called\n", msg);
987#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000988 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000989 if (PyObject_HasAttrString(handler, (char *) "warning")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000990 va_start(args, msg);
991 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +0000992 va_end(args);
993 buf[1023] = 0;
994 result =
995 PyObject_CallMethod(handler, (char *) "warning", (char *) "s",
996 buf);
997 if (PyErr_Occurred())
998 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000999 Py_XDECREF(result);
1000 }
1001}
1002
1003static void
1004pythonError(void *user_data, const char *msg, ...)
1005{
1006 PyObject *handler;
1007 PyObject *result;
1008 va_list args;
1009 char buf[1024];
1010
Daniel Veillard797a5652002-02-12 13:46:21 +00001011#ifdef DEBUG_SAX
1012 printf("pythonError(%s) called\n", msg);
1013#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001014 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001015 if (PyObject_HasAttrString(handler, (char *) "error")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001016 va_start(args, msg);
1017 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +00001018 va_end(args);
1019 buf[1023] = 0;
1020 result =
1021 PyObject_CallMethod(handler, (char *) "error", (char *) "s",
1022 buf);
1023 if (PyErr_Occurred())
1024 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001025 Py_XDECREF(result);
1026 }
1027}
1028
1029static void
1030pythonFatalError(void *user_data, const char *msg, ...)
1031{
1032 PyObject *handler;
1033 PyObject *result;
1034 va_list args;
1035 char buf[1024];
1036
Daniel Veillard797a5652002-02-12 13:46:21 +00001037#ifdef DEBUG_SAX
1038 printf("pythonFatalError(%s) called\n", msg);
1039#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001040 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001041 if (PyObject_HasAttrString(handler, (char *) "fatalError")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001042 va_start(args, msg);
1043 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +00001044 va_end(args);
1045 buf[1023] = 0;
1046 result =
1047 PyObject_CallMethod(handler, (char *) "fatalError",
1048 (char *) "s", buf);
1049 if (PyErr_Occurred())
1050 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001051 Py_XDECREF(result);
1052 }
1053}
1054
1055static void
1056pythonCdataBlock(void *user_data, const xmlChar * ch, int len)
1057{
1058 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +00001059 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001060 int type = 0;
1061
Daniel Veillard797a5652002-02-12 13:46:21 +00001062#ifdef DEBUG_SAX
1063 printf("pythonCdataBlock(%s, %d) called\n", ch, len);
1064#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001065 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001066 if (PyObject_HasAttrString(handler, (char *) "cdataBlock"))
1067 type = 1;
1068 else if (PyObject_HasAttrString(handler, (char *) "cdata"))
1069 type = 2;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001070 if (type != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001071 if (type == 1)
1072 result =
1073 PyObject_CallMethod(handler, (char *) "cdataBlock",
1074 (char *) "s#", ch, len);
1075 else if (type == 2)
1076 result =
1077 PyObject_CallMethod(handler, (char *) "cdata",
1078 (char *) "s#", ch, len);
1079 if (PyErr_Occurred())
1080 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001081 Py_XDECREF(result);
1082 }
1083}
1084
1085static void
1086pythonExternalSubset(void *user_data,
1087 const xmlChar * name,
1088 const xmlChar * externalID, const xmlChar * systemID)
1089{
1090 PyObject *handler;
1091 PyObject *result;
1092
Daniel Veillard797a5652002-02-12 13:46:21 +00001093#ifdef DEBUG_SAX
1094 printf("pythonExternalSubset(%s, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001095 name, externalID, systemID);
Daniel Veillard797a5652002-02-12 13:46:21 +00001096#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001097 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001098 if (PyObject_HasAttrString(handler, (char *) "externalSubset")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001099 result =
Daniel Veillardd2379012002-03-15 22:24:56 +00001100 PyObject_CallMethod(handler, (char *) "externalSubset",
1101 (char *) "sss", name, externalID,
1102 systemID);
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001103 Py_XDECREF(result);
1104 }
1105}
1106
1107static void
1108pythonEntityDecl(void *user_data,
1109 const xmlChar * name,
1110 int type,
1111 const xmlChar * publicId,
1112 const xmlChar * systemId, xmlChar * content)
1113{
1114 PyObject *handler;
1115 PyObject *result;
1116
1117 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001118 if (PyObject_HasAttrString(handler, (char *) "entityDecl")) {
1119 result = PyObject_CallMethod(handler, (char *) "entityDecl",
1120 (char *) "sisss", name, type,
1121 publicId, systemId, content);
1122 if (PyErr_Occurred())
1123 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001124 Py_XDECREF(result);
1125 }
1126}
1127
1128
1129
1130static void
1131
1132pythonNotationDecl(void *user_data,
1133 const xmlChar * name,
1134 const xmlChar * publicId, const xmlChar * systemId)
1135{
1136 PyObject *handler;
1137 PyObject *result;
1138
1139 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001140 if (PyObject_HasAttrString(handler, (char *) "notationDecl")) {
1141 result = PyObject_CallMethod(handler, (char *) "notationDecl",
1142 (char *) "sss", name, publicId,
1143 systemId);
1144 if (PyErr_Occurred())
1145 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001146 Py_XDECREF(result);
1147 }
1148}
1149
1150static void
1151pythonAttributeDecl(void *user_data,
1152 const xmlChar * elem,
1153 const xmlChar * name,
1154 int type,
1155 int def,
Daniel Veillardd2379012002-03-15 22:24:56 +00001156 const xmlChar * defaultValue, xmlEnumerationPtr tree)
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001157{
1158 PyObject *handler;
1159 PyObject *nameList;
1160 PyObject *newName;
1161 xmlEnumerationPtr node;
1162 PyObject *result;
1163 int count;
1164
1165 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001166 if (PyObject_HasAttrString(handler, (char *) "attributeDecl")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001167 count = 0;
1168 for (node = tree; node != NULL; node = node->next) {
1169 count++;
1170 }
1171 nameList = PyList_New(count);
1172 count = 0;
1173 for (node = tree; node != NULL; node = node->next) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001174 newName = PyString_FromString((char *) node->name);
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001175 PyList_SetItem(nameList, count, newName);
Daniel Veillarda3d23052007-01-09 21:24:34 +00001176 Py_DECREF(newName);
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001177 count++;
1178 }
Daniel Veillardd2379012002-03-15 22:24:56 +00001179 result = PyObject_CallMethod(handler, (char *) "attributeDecl",
1180 (char *) "ssiisO", elem, name, type,
1181 def, defaultValue, nameList);
1182 if (PyErr_Occurred())
1183 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001184 Py_XDECREF(nameList);
1185 Py_XDECREF(result);
1186 }
1187}
1188
1189static void
1190pythonElementDecl(void *user_data,
1191 const xmlChar * name,
Daniel Veillardd2379012002-03-15 22:24:56 +00001192 int type, ATTRIBUTE_UNUSED xmlElementContentPtr content)
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001193{
1194 PyObject *handler;
1195 PyObject *obj;
1196 PyObject *result;
1197
1198 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001199 if (PyObject_HasAttrString(handler, (char *) "elementDecl")) {
1200 /* TODO: wrap in an elementContent object */
1201 printf
1202 ("pythonElementDecl: xmlElementContentPtr wrapper missing !\n");
1203 obj = Py_None;
1204 /* Py_XINCREF(Py_None); isn't the reference just borrowed ??? */
1205 result = PyObject_CallMethod(handler, (char *) "elementDecl",
1206 (char *) "siO", name, type, obj);
1207 if (PyErr_Occurred())
1208 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001209 Py_XDECREF(result);
1210 }
1211}
1212
1213static void
1214pythonUnparsedEntityDecl(void *user_data,
1215 const xmlChar * name,
1216 const xmlChar * publicId,
1217 const xmlChar * systemId,
1218 const xmlChar * notationName)
1219{
1220 PyObject *handler;
1221 PyObject *result;
1222
1223 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001224 if (PyObject_HasAttrString(handler, (char *) "unparsedEntityDecl")) {
1225 result =
1226 PyObject_CallMethod(handler, (char *) "unparsedEntityDecl",
1227 (char *) "ssss", name, publicId, systemId,
1228 notationName);
1229 if (PyErr_Occurred())
1230 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001231 Py_XDECREF(result);
1232 }
1233}
1234
1235static void
1236pythonInternalSubset(void *user_data, const xmlChar * name,
1237 const xmlChar * ExternalID, const xmlChar * SystemID)
1238{
1239 PyObject *handler;
1240 PyObject *result;
1241
Daniel Veillard797a5652002-02-12 13:46:21 +00001242#ifdef DEBUG_SAX
1243 printf("pythonInternalSubset(%s, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001244 name, ExternalID, SystemID);
Daniel Veillard797a5652002-02-12 13:46:21 +00001245#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001246 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001247 if (PyObject_HasAttrString(handler, (char *) "internalSubset")) {
1248 result = PyObject_CallMethod(handler, (char *) "internalSubset",
1249 (char *) "sss", name, ExternalID,
1250 SystemID);
1251 if (PyErr_Occurred())
1252 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001253 Py_XDECREF(result);
1254 }
1255}
1256
1257static xmlSAXHandler pythonSaxHandler = {
1258 pythonInternalSubset,
Daniel Veillardd2379012002-03-15 22:24:56 +00001259 NULL, /* TODO pythonIsStandalone, */
1260 NULL, /* TODO pythonHasInternalSubset, */
1261 NULL, /* TODO pythonHasExternalSubset, */
1262 NULL, /* TODO pythonResolveEntity, */
1263 NULL, /* TODO pythonGetEntity, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001264 pythonEntityDecl,
1265 pythonNotationDecl,
1266 pythonAttributeDecl,
1267 pythonElementDecl,
1268 pythonUnparsedEntityDecl,
Daniel Veillardd2379012002-03-15 22:24:56 +00001269 NULL, /* OBSOLETED pythonSetDocumentLocator, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001270 pythonStartDocument,
1271 pythonEndDocument,
1272 pythonStartElement,
1273 pythonEndElement,
1274 pythonReference,
1275 pythonCharacters,
1276 pythonIgnorableWhitespace,
1277 pythonProcessingInstruction,
1278 pythonComment,
1279 pythonWarning,
1280 pythonError,
1281 pythonFatalError,
Daniel Veillardd2379012002-03-15 22:24:56 +00001282 NULL, /* TODO pythonGetParameterEntity, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001283 pythonCdataBlock,
1284 pythonExternalSubset,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001285 1,
1286 NULL, /* TODO mograte to SAX2 */
1287 NULL,
William M. Brack871611b2003-10-18 04:53:14 +00001288 NULL,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001289 NULL
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001290};
Daniel Veillard3ce52572002-02-03 15:08:05 +00001291
1292/************************************************************************
1293 * *
1294 * Handling of specific parser context *
1295 * *
1296 ************************************************************************/
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001297
1298PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001299libxml_xmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
1300 PyObject * args)
1301{
1302 const char *chunk;
Daniel Veillard3ce52572002-02-03 15:08:05 +00001303 int size;
Daniel Veillardd2379012002-03-15 22:24:56 +00001304 const char *URI;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001305 PyObject *pyobj_SAX = NULL;
Daniel Veillard3ce52572002-02-03 15:08:05 +00001306 xmlSAXHandlerPtr SAX = NULL;
Daniel Veillard3ce52572002-02-03 15:08:05 +00001307 xmlParserCtxtPtr ret;
1308 PyObject *pyret;
Daniel Veillard96fe0952002-01-30 20:52:23 +00001309
Daniel Veillardd2379012002-03-15 22:24:56 +00001310 if (!PyArg_ParseTuple
1311 (args, (char *) "Oziz:xmlCreatePushParser", &pyobj_SAX, &chunk,
1312 &size, &URI))
1313 return (NULL);
Daniel Veillard3ce52572002-02-03 15:08:05 +00001314
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001315#ifdef DEBUG
Daniel Veillard3ce52572002-02-03 15:08:05 +00001316 printf("libxml_xmlCreatePushParser(%p, %s, %d, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001317 pyobj_SAX, chunk, size, URI);
Daniel Veillard96fe0952002-01-30 20:52:23 +00001318#endif
Daniel Veillard3ce52572002-02-03 15:08:05 +00001319 if (pyobj_SAX != Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001320 SAX = &pythonSaxHandler;
1321 Py_INCREF(pyobj_SAX);
1322 /* The reference is released in pythonEndDocument() */
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001323 }
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001324 ret = xmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI);
Daniel Veillard3ce52572002-02-03 15:08:05 +00001325 pyret = libxml_xmlParserCtxtPtrWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +00001326 return (pyret);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001327}
Daniel Veillard5d819032002-02-02 21:49:17 +00001328
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001329PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001330libxml_htmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
1331 PyObject * args)
1332{
Daniel Veillard656ce942004-04-30 23:11:45 +00001333#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001334 const char *chunk;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001335 int size;
Daniel Veillardd2379012002-03-15 22:24:56 +00001336 const char *URI;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001337 PyObject *pyobj_SAX = NULL;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001338 xmlSAXHandlerPtr SAX = NULL;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001339 xmlParserCtxtPtr ret;
1340 PyObject *pyret;
1341
Daniel Veillardd2379012002-03-15 22:24:56 +00001342 if (!PyArg_ParseTuple
1343 (args, (char *) "Oziz:htmlCreatePushParser", &pyobj_SAX, &chunk,
1344 &size, &URI))
1345 return (NULL);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001346
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001347#ifdef DEBUG
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001348 printf("libxml_htmlCreatePushParser(%p, %s, %d, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001349 pyobj_SAX, chunk, size, URI);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001350#endif
1351 if (pyobj_SAX != Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001352 SAX = &pythonSaxHandler;
1353 Py_INCREF(pyobj_SAX);
1354 /* The reference is released in pythonEndDocument() */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001355 }
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001356 ret = htmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI,
Daniel Veillardd2379012002-03-15 22:24:56 +00001357 XML_CHAR_ENCODING_NONE);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001358 pyret = libxml_xmlParserCtxtPtrWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +00001359 return (pyret);
Daniel Veillard656ce942004-04-30 23:11:45 +00001360#else
1361 Py_INCREF(Py_None);
1362 return (Py_None);
1363#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001364}
1365
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001366PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001367libxml_xmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1368{
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001369 int recover;
Daniel Veillardd2379012002-03-15 22:24:56 +00001370 const char *URI;
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001371 PyObject *pyobj_SAX = NULL;
1372 xmlSAXHandlerPtr SAX = NULL;
1373
Daniel Veillardd2379012002-03-15 22:24:56 +00001374 if (!PyArg_ParseTuple(args, (char *) "Osi:xmlSAXParseFile", &pyobj_SAX,
1375 &URI, &recover))
1376 return (NULL);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001377
1378#ifdef DEBUG
1379 printf("libxml_xmlSAXParseFile(%p, %s, %d) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001380 pyobj_SAX, URI, recover);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001381#endif
1382 if (pyobj_SAX == Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001383 Py_INCREF(Py_None);
1384 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001385 }
1386 SAX = &pythonSaxHandler;
1387 Py_INCREF(pyobj_SAX);
1388 /* The reference is released in pythonEndDocument() */
Daniel Veillardf2531af2005-03-31 11:06:29 +00001389 xmlSAXUserParseFile(SAX, pyobj_SAX, URI);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001390 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +00001391 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001392}
1393
1394PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001395libxml_htmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1396{
Daniel Veillard656ce942004-04-30 23:11:45 +00001397#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001398 const char *URI;
1399 const char *encoding;
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001400 PyObject *pyobj_SAX = NULL;
1401 xmlSAXHandlerPtr SAX = NULL;
1402
Daniel Veillardd2379012002-03-15 22:24:56 +00001403 if (!PyArg_ParseTuple
1404 (args, (char *) "Osz:htmlSAXParseFile", &pyobj_SAX, &URI,
1405 &encoding))
1406 return (NULL);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001407
1408#ifdef DEBUG
1409 printf("libxml_htmlSAXParseFile(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001410 pyobj_SAX, URI, encoding);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001411#endif
1412 if (pyobj_SAX == Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001413 Py_INCREF(Py_None);
1414 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001415 }
1416 SAX = &pythonSaxHandler;
1417 Py_INCREF(pyobj_SAX);
1418 /* The reference is released in pythonEndDocument() */
1419 htmlSAXParseFile(URI, encoding, SAX, pyobj_SAX);
1420 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +00001421 return (Py_None);
Daniel Veillard656ce942004-04-30 23:11:45 +00001422#else
1423 Py_INCREF(Py_None);
1424 return (Py_None);
1425#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001426}
1427
Daniel Veillard5d819032002-02-02 21:49:17 +00001428/************************************************************************
1429 * *
1430 * Error message callback *
1431 * *
1432 ************************************************************************/
1433
1434static PyObject *libxml_xmlPythonErrorFuncHandler = NULL;
1435static PyObject *libxml_xmlPythonErrorFuncCtxt = NULL;
1436
Daniel Veillarde6227e02003-01-14 11:42:39 +00001437/* helper to build a xmlMalloc'ed string from a format and va_list */
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001438/*
1439 * disabled the loop, the repeated call to vsnprintf without reset of ap
1440 * in case the initial buffer was too small segfaulted on x86_64
1441 * we now directly vsnprintf on a large buffer.
1442 */
Daniel Veillarde6227e02003-01-14 11:42:39 +00001443static char *
1444libxml_buildMessage(const char *msg, va_list ap)
Daniel Veillardd2379012002-03-15 22:24:56 +00001445{
Daniel Veillardd2379012002-03-15 22:24:56 +00001446 int chars;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001447 char *str;
1448
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001449 str = (char *) xmlMalloc(1000);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001450 if (str == NULL)
1451 return NULL;
1452
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001453 chars = vsnprintf(str, 999, msg, ap);
1454 if (chars >= 998)
1455 str[999] = 0;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001456
1457 return str;
1458}
1459
1460static void
1461libxml_xmlErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, const char *msg,
1462 ...)
1463{
Daniel Veillardd2379012002-03-15 22:24:56 +00001464 va_list ap;
Daniel Veillard5d819032002-02-02 21:49:17 +00001465 PyObject *list;
1466 PyObject *message;
1467 PyObject *result;
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001468 char str[1000];
Daniel Veillard5d819032002-02-02 21:49:17 +00001469
1470#ifdef DEBUG_ERROR
1471 printf("libxml_xmlErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
1472#endif
1473
1474
1475 if (libxml_xmlPythonErrorFuncHandler == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001476 va_start(ap, msg);
Daniel Veillard007d51e2003-09-17 20:07:28 +00001477 vfprintf(stderr, msg, ap);
Daniel Veillardd2379012002-03-15 22:24:56 +00001478 va_end(ap);
Daniel Veillard5d819032002-02-02 21:49:17 +00001479 } else {
Daniel Veillarde6227e02003-01-14 11:42:39 +00001480 va_start(ap, msg);
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001481 if (vsnprintf(str, 999, msg, ap) >= 998)
1482 str[999] = 0;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001483 va_end(ap);
Daniel Veillard5d819032002-02-02 21:49:17 +00001484
Daniel Veillardd2379012002-03-15 22:24:56 +00001485 list = PyTuple_New(2);
1486 PyTuple_SetItem(list, 0, libxml_xmlPythonErrorFuncCtxt);
1487 Py_XINCREF(libxml_xmlPythonErrorFuncCtxt);
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001488 message = libxml_charPtrConstWrap(str);
Daniel Veillardd2379012002-03-15 22:24:56 +00001489 PyTuple_SetItem(list, 1, message);
1490 result = PyEval_CallObject(libxml_xmlPythonErrorFuncHandler, list);
1491 Py_XDECREF(list);
1492 Py_XDECREF(result);
Daniel Veillard5d819032002-02-02 21:49:17 +00001493 }
1494}
1495
1496static void
Daniel Veillardd2379012002-03-15 22:24:56 +00001497libxml_xmlErrorInitialize(void)
1498{
Daniel Veillard5d819032002-02-02 21:49:17 +00001499#ifdef DEBUG_ERROR
1500 printf("libxml_xmlErrorInitialize() called\n");
1501#endif
1502 xmlSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
Daniel Veillard781ac8b2003-05-15 22:11:36 +00001503 xmlThrDefSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
Daniel Veillard5d819032002-02-02 21:49:17 +00001504}
1505
Daniel Veillardc2664642003-07-29 20:44:53 +00001506static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001507libxml_xmlRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self,
1508 PyObject * args)
1509{
Daniel Veillard5d819032002-02-02 21:49:17 +00001510 PyObject *py_retval;
1511 PyObject *pyobj_f;
1512 PyObject *pyobj_ctx;
1513
Daniel Veillardd2379012002-03-15 22:24:56 +00001514 if (!PyArg_ParseTuple
1515 (args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f,
1516 &pyobj_ctx))
1517 return (NULL);
Daniel Veillard5d819032002-02-02 21:49:17 +00001518
1519#ifdef DEBUG_ERROR
William M. Brack8e2cc6f2004-07-03 23:28:52 +00001520 printf("libxml_xmlRegisterErrorHandler(%p, %p) called\n", pyobj_ctx,
Daniel Veillardd2379012002-03-15 22:24:56 +00001521 pyobj_f);
Daniel Veillard5d819032002-02-02 21:49:17 +00001522#endif
1523
1524 if (libxml_xmlPythonErrorFuncHandler != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001525 Py_XDECREF(libxml_xmlPythonErrorFuncHandler);
Daniel Veillard5d819032002-02-02 21:49:17 +00001526 }
1527 if (libxml_xmlPythonErrorFuncCtxt != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001528 Py_XDECREF(libxml_xmlPythonErrorFuncCtxt);
Daniel Veillard5d819032002-02-02 21:49:17 +00001529 }
1530
1531 Py_XINCREF(pyobj_ctx);
1532 Py_XINCREF(pyobj_f);
1533
1534 /* TODO: check f is a function ! */
1535 libxml_xmlPythonErrorFuncHandler = pyobj_f;
1536 libxml_xmlPythonErrorFuncCtxt = pyobj_ctx;
1537
1538 py_retval = libxml_intWrap(1);
Daniel Veillardd2379012002-03-15 22:24:56 +00001539 return (py_retval);
Daniel Veillard5d819032002-02-02 21:49:17 +00001540}
Daniel Veillardd2379012002-03-15 22:24:56 +00001541
Daniel Veillarde6227e02003-01-14 11:42:39 +00001542
1543/************************************************************************
1544 * *
1545 * Per parserCtxt error handler *
1546 * *
1547 ************************************************************************/
1548
Daniel Veillard417be3a2003-01-20 21:26:34 +00001549typedef struct
Daniel Veillarde6227e02003-01-14 11:42:39 +00001550{
Daniel Veillard417be3a2003-01-20 21:26:34 +00001551 PyObject *f;
1552 PyObject *arg;
1553} xmlParserCtxtPyCtxt;
1554typedef xmlParserCtxtPyCtxt *xmlParserCtxtPyCtxtPtr;
1555
1556static void
1557libxml_xmlParserCtxtGenericErrorFuncHandler(void *ctx, int severity, char *str)
1558{
Daniel Veillarde6227e02003-01-14 11:42:39 +00001559 PyObject *list;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001560 PyObject *result;
Daniel Veillard417be3a2003-01-20 21:26:34 +00001561 xmlParserCtxtPtr ctxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001562 xmlParserCtxtPyCtxtPtr pyCtxt;
1563
1564#ifdef DEBUG_ERROR
Daniel Veillard850ce9b2004-11-10 11:55:47 +00001565 printf("libxml_xmlParserCtxtGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001566#endif
1567
Daniel Veillard417be3a2003-01-20 21:26:34 +00001568 ctxt = (xmlParserCtxtPtr)ctx;
1569 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001570
Daniel Veillard417be3a2003-01-20 21:26:34 +00001571 list = PyTuple_New(4);
1572 PyTuple_SetItem(list, 0, pyCtxt->arg);
1573 Py_XINCREF(pyCtxt->arg);
1574 PyTuple_SetItem(list, 1, libxml_charPtrWrap(str));
1575 PyTuple_SetItem(list, 2, libxml_intWrap(severity));
1576 PyTuple_SetItem(list, 3, Py_None);
1577 Py_INCREF(Py_None);
1578 result = PyEval_CallObject(pyCtxt->f, list);
1579 if (result == NULL)
1580 {
1581 /* TODO: manage for the exception to be propagated... */
1582 PyErr_Print();
Daniel Veillarde6227e02003-01-14 11:42:39 +00001583 }
Daniel Veillard417be3a2003-01-20 21:26:34 +00001584 Py_XDECREF(list);
1585 Py_XDECREF(result);
1586}
1587
1588static void
1589libxml_xmlParserCtxtErrorFuncHandler(void *ctx, const char *msg, ...)
1590{
1591 va_list ap;
1592
1593 va_start(ap, msg);
1594 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_ERROR,libxml_buildMessage(msg,ap));
1595 va_end(ap);
1596}
1597
1598static void
1599libxml_xmlParserCtxtWarningFuncHandler(void *ctx, const char *msg, ...)
1600{
1601 va_list ap;
1602
1603 va_start(ap, msg);
1604 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_WARNING,libxml_buildMessage(msg,ap));
1605 va_end(ap);
1606}
1607
1608static void
1609libxml_xmlParserCtxtValidityErrorFuncHandler(void *ctx, const char *msg, ...)
1610{
1611 va_list ap;
1612
1613 va_start(ap, msg);
1614 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap));
1615 va_end(ap);
1616}
1617
1618static void
1619libxml_xmlParserCtxtValidityWarningFuncHandler(void *ctx, const char *msg, ...)
1620{
1621 va_list ap;
1622
1623 va_start(ap, msg);
1624 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap));
1625 va_end(ap);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001626}
1627
Daniel Veillardc2664642003-07-29 20:44:53 +00001628static PyObject *
Daniel Veillard417be3a2003-01-20 21:26:34 +00001629libxml_xmlParserCtxtSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
Daniel Veillarde6227e02003-01-14 11:42:39 +00001630{
1631 PyObject *py_retval;
1632 xmlParserCtxtPtr ctxt;
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001633 xmlParserCtxtPyCtxtPtr pyCtxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001634 PyObject *pyobj_ctxt;
1635 PyObject *pyobj_f;
1636 PyObject *pyobj_arg;
1637
Daniel Veillard417be3a2003-01-20 21:26:34 +00001638 if (!PyArg_ParseTuple(args, (char *)"OOO:xmlParserCtxtSetErrorHandler",
Daniel Veillarde6227e02003-01-14 11:42:39 +00001639 &pyobj_ctxt, &pyobj_f, &pyobj_arg))
1640 return(NULL);
1641 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1642 if (ctxt->_private == NULL) {
Daniel Veillarde6227e02003-01-14 11:42:39 +00001643 pyCtxt = xmlMalloc(sizeof(xmlParserCtxtPyCtxt));
1644 if (pyCtxt == NULL) {
1645 py_retval = libxml_intWrap(-1);
1646 return(py_retval);
1647 }
1648 memset(pyCtxt,0,sizeof(xmlParserCtxtPyCtxt));
1649 ctxt->_private = pyCtxt;
1650 }
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001651 else {
Daniel Veillard417be3a2003-01-20 21:26:34 +00001652 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001653 }
Daniel Veillarde6227e02003-01-14 11:42:39 +00001654 /* TODO: check f is a function ! */
Daniel Veillard417be3a2003-01-20 21:26:34 +00001655 Py_XDECREF(pyCtxt->f);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001656 Py_XINCREF(pyobj_f);
Daniel Veillard417be3a2003-01-20 21:26:34 +00001657 pyCtxt->f = pyobj_f;
1658 Py_XDECREF(pyCtxt->arg);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001659 Py_XINCREF(pyobj_arg);
Daniel Veillard417be3a2003-01-20 21:26:34 +00001660 pyCtxt->arg = pyobj_arg;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001661
Daniel Veillard417be3a2003-01-20 21:26:34 +00001662 if (pyobj_f != Py_None) {
1663 ctxt->sax->error = libxml_xmlParserCtxtErrorFuncHandler;
1664 ctxt->sax->warning = libxml_xmlParserCtxtWarningFuncHandler;
1665 ctxt->vctxt.error = libxml_xmlParserCtxtValidityErrorFuncHandler;
1666 ctxt->vctxt.warning = libxml_xmlParserCtxtValidityWarningFuncHandler;
1667 }
1668 else {
1669 ctxt->sax->error = xmlParserError;
1670 ctxt->vctxt.error = xmlParserValidityError;
1671 ctxt->sax->warning = xmlParserWarning;
1672 ctxt->vctxt.warning = xmlParserValidityWarning;
1673 }
Daniel Veillarde6227e02003-01-14 11:42:39 +00001674
1675 py_retval = libxml_intWrap(1);
1676 return(py_retval);
1677}
1678
Daniel Veillardc2664642003-07-29 20:44:53 +00001679static PyObject *
Daniel Veillard417be3a2003-01-20 21:26:34 +00001680libxml_xmlParserCtxtGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
Daniel Veillarde6227e02003-01-14 11:42:39 +00001681{
1682 PyObject *py_retval;
1683 xmlParserCtxtPtr ctxt;
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001684 xmlParserCtxtPyCtxtPtr pyCtxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001685 PyObject *pyobj_ctxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001686
Daniel Veillard417be3a2003-01-20 21:26:34 +00001687 if (!PyArg_ParseTuple(args, (char *)"O:xmlParserCtxtGetErrorHandler",
1688 &pyobj_ctxt))
Daniel Veillarde6227e02003-01-14 11:42:39 +00001689 return(NULL);
1690 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
Daniel Veillard417be3a2003-01-20 21:26:34 +00001691 py_retval = PyTuple_New(2);
1692 if (ctxt->_private != NULL) {
1693 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
1694
1695 PyTuple_SetItem(py_retval, 0, pyCtxt->f);
1696 Py_XINCREF(pyCtxt->f);
1697 PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
1698 Py_XINCREF(pyCtxt->arg);
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001699 }
1700 else {
Daniel Veillard417be3a2003-01-20 21:26:34 +00001701 /* no python error handler registered */
1702 PyTuple_SetItem(py_retval, 0, Py_None);
1703 Py_XINCREF(Py_None);
1704 PyTuple_SetItem(py_retval, 1, Py_None);
1705 Py_XINCREF(Py_None);
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001706 }
Daniel Veillarde6227e02003-01-14 11:42:39 +00001707 return(py_retval);
1708}
1709
Daniel Veillardc2664642003-07-29 20:44:53 +00001710static PyObject *
Daniel Veillard417be3a2003-01-20 21:26:34 +00001711libxml_xmlFreeParserCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
1712 xmlParserCtxtPtr ctxt;
1713 PyObject *pyobj_ctxt;
1714 xmlParserCtxtPyCtxtPtr pyCtxt;
1715
1716 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeParserCtxt", &pyobj_ctxt))
1717 return(NULL);
1718 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1719
1720 if (ctxt != NULL) {
1721 pyCtxt = (xmlParserCtxtPyCtxtPtr)((xmlParserCtxtPtr)ctxt)->_private;
1722 if (pyCtxt) {
1723 Py_XDECREF(pyCtxt->f);
1724 Py_XDECREF(pyCtxt->arg);
1725 xmlFree(pyCtxt);
1726 }
1727 xmlFreeParserCtxt(ctxt);
1728 }
1729
1730 Py_INCREF(Py_None);
1731 return(Py_None);
1732}
1733
Daniel Veillard850ce9b2004-11-10 11:55:47 +00001734/***
1735 * xmlValidCtxt stuff
1736 */
1737
1738typedef struct
1739{
1740 PyObject *warn;
1741 PyObject *error;
1742 PyObject *arg;
1743} xmlValidCtxtPyCtxt;
1744typedef xmlValidCtxtPyCtxt *xmlValidCtxtPyCtxtPtr;
1745
1746static void
1747libxml_xmlValidCtxtGenericErrorFuncHandler(void *ctx, int severity, char *str)
1748{
1749 PyObject *list;
1750 PyObject *result;
1751 xmlValidCtxtPyCtxtPtr pyCtxt;
1752
1753#ifdef DEBUG_ERROR
1754 printf("libxml_xmlValidCtxtGenericErrorFuncHandler(%p, %d, %s, ...) called\n", ctx, severity, str);
1755#endif
1756
1757 pyCtxt = (xmlValidCtxtPyCtxtPtr)ctx;
1758
1759 list = PyTuple_New(2);
1760 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
1761 PyTuple_SetItem(list, 1, pyCtxt->arg);
1762 Py_XINCREF(pyCtxt->arg);
1763 result = PyEval_CallObject(pyCtxt->error, list);
1764 if (result == NULL)
1765 {
1766 /* TODO: manage for the exception to be propagated... */
1767 PyErr_Print();
1768 }
1769 Py_XDECREF(list);
1770 Py_XDECREF(result);
1771}
1772
1773static void
1774libxml_xmlValidCtxtGenericWarningFuncHandler(void *ctx, int severity, char *str)
1775{
1776 PyObject *list;
1777 PyObject *result;
1778 xmlValidCtxtPyCtxtPtr pyCtxt;
1779
1780#ifdef DEBUG_ERROR
1781 printf("libxml_xmlValidCtxtGenericWarningFuncHandler(%p, %d, %s, ...) called\n", ctx, severity, str);
1782#endif
1783
1784 pyCtxt = (xmlValidCtxtPyCtxtPtr)ctx;
1785
1786 list = PyTuple_New(2);
1787 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
1788 PyTuple_SetItem(list, 1, pyCtxt->arg);
1789 Py_XINCREF(pyCtxt->arg);
1790 result = PyEval_CallObject(pyCtxt->warn, list);
1791 if (result == NULL)
1792 {
1793 /* TODO: manage for the exception to be propagated... */
1794 PyErr_Print();
1795 }
1796 Py_XDECREF(list);
1797 Py_XDECREF(result);
1798}
1799
1800static void
1801libxml_xmlValidCtxtErrorFuncHandler(void *ctx, const char *msg, ...)
1802{
1803 va_list ap;
1804
1805 va_start(ap, msg);
1806 libxml_xmlValidCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap));
1807 va_end(ap);
1808}
1809
1810static void
1811libxml_xmlValidCtxtWarningFuncHandler(void *ctx, const char *msg, ...)
1812{
1813 va_list ap;
1814
1815 va_start(ap, msg);
1816 libxml_xmlValidCtxtGenericWarningFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap));
1817 va_end(ap);
1818}
1819
1820static PyObject *
1821libxml_xmlSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1822{
1823 PyObject *py_retval;
1824 PyObject *pyobj_error;
1825 PyObject *pyobj_warn;
1826 PyObject *pyobj_ctx;
1827 PyObject *pyobj_arg = Py_None;
1828 xmlValidCtxtPtr ctxt;
1829 xmlValidCtxtPyCtxtPtr pyCtxt;
1830
1831 if (!PyArg_ParseTuple
1832 (args, (char *) "OOO|O:xmlSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
1833 return (NULL);
1834
1835#ifdef DEBUG_ERROR
1836 printf("libxml_xmlSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
1837#endif
1838
1839 ctxt = PyValidCtxt_Get(pyobj_ctx);
1840 pyCtxt = xmlMalloc(sizeof(xmlValidCtxtPyCtxt));
1841 if (pyCtxt == NULL) {
1842 py_retval = libxml_intWrap(-1);
1843 return(py_retval);
1844 }
1845 memset(pyCtxt, 0, sizeof(xmlValidCtxtPyCtxt));
1846
1847
1848 /* TODO: check warn and error is a function ! */
1849 Py_XDECREF(pyCtxt->error);
1850 Py_XINCREF(pyobj_error);
1851 pyCtxt->error = pyobj_error;
1852
1853 Py_XDECREF(pyCtxt->warn);
1854 Py_XINCREF(pyobj_warn);
1855 pyCtxt->warn = pyobj_warn;
1856
1857 Py_XDECREF(pyCtxt->arg);
1858 Py_XINCREF(pyobj_arg);
1859 pyCtxt->arg = pyobj_arg;
1860
1861 ctxt->error = libxml_xmlValidCtxtErrorFuncHandler;
1862 ctxt->warning = libxml_xmlValidCtxtWarningFuncHandler;
1863 ctxt->userData = pyCtxt;
1864
1865 py_retval = libxml_intWrap(1);
1866 return (py_retval);
1867}
1868
Daniel Veillard25c90c52005-03-02 10:47:41 +00001869
Daniel Veillardbb8502c2005-03-30 07:40:35 +00001870static PyObject *
Daniel Veillard25c90c52005-03-02 10:47:41 +00001871libxml_xmlFreeValidCtxt(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1872 xmlValidCtxtPtr cur;
1873 xmlValidCtxtPyCtxtPtr pyCtxt;
1874 PyObject *pyobj_cur;
1875
1876 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeValidCtxt", &pyobj_cur))
1877 return(NULL);
1878 cur = (xmlValidCtxtPtr) PyValidCtxt_Get(pyobj_cur);
1879
1880 pyCtxt = (xmlValidCtxtPyCtxtPtr)(cur->userData);
1881 if (pyCtxt != NULL)
1882 {
1883 Py_XDECREF(pyCtxt->error);
1884 Py_XDECREF(pyCtxt->warn);
1885 Py_XDECREF(pyCtxt->arg);
1886 xmlFree(pyCtxt);
1887 }
1888
1889 xmlFreeValidCtxt(cur);
1890 Py_INCREF(Py_None);
1891 return(Py_None);
1892}
1893
Daniel Veillard438ebbd2008-05-12 12:58:46 +00001894#ifdef LIBXML_READER_ENABLED
Daniel Veillarda7340c82002-02-01 17:56:45 +00001895/************************************************************************
1896 * *
Daniel Veillard26f70262003-01-16 22:45:08 +00001897 * Per xmlTextReader error handler *
1898 * *
1899 ************************************************************************/
1900
1901typedef struct
1902{
1903 PyObject *f;
1904 PyObject *arg;
1905} xmlTextReaderPyCtxt;
1906typedef xmlTextReaderPyCtxt *xmlTextReaderPyCtxtPtr;
1907
1908static void
1909libxml_xmlTextReaderErrorCallback(void *arg,
1910 const char *msg,
Daniel Veillard417be3a2003-01-20 21:26:34 +00001911 int severity,
1912 xmlTextReaderLocatorPtr locator)
Daniel Veillard26f70262003-01-16 22:45:08 +00001913{
1914 xmlTextReaderPyCtxt *pyCtxt = (xmlTextReaderPyCtxt *)arg;
1915 PyObject *list;
1916 PyObject *result;
1917
Daniel Veillard417be3a2003-01-20 21:26:34 +00001918 list = PyTuple_New(4);
Daniel Veillard26f70262003-01-16 22:45:08 +00001919 PyTuple_SetItem(list, 0, pyCtxt->arg);
1920 Py_XINCREF(pyCtxt->arg);
1921 PyTuple_SetItem(list, 1, libxml_charPtrConstWrap(msg));
Daniel Veillard417be3a2003-01-20 21:26:34 +00001922 PyTuple_SetItem(list, 2, libxml_intWrap(severity));
1923 PyTuple_SetItem(list, 3, libxml_xmlTextReaderLocatorPtrWrap(locator));
Daniel Veillard26f70262003-01-16 22:45:08 +00001924 result = PyEval_CallObject(pyCtxt->f, list);
1925 if (result == NULL)
1926 {
Daniel Veillard417be3a2003-01-20 21:26:34 +00001927 /* TODO: manage for the exception to be propagated... */
Daniel Veillard26f70262003-01-16 22:45:08 +00001928 PyErr_Print();
1929 }
1930 Py_XDECREF(list);
1931 Py_XDECREF(result);
1932}
1933
Daniel Veillardc2664642003-07-29 20:44:53 +00001934static PyObject *
Daniel Veillard26f70262003-01-16 22:45:08 +00001935libxml_xmlTextReaderSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1936{
1937 xmlTextReaderPtr reader;
1938 xmlTextReaderPyCtxtPtr pyCtxt;
1939 xmlTextReaderErrorFunc f;
1940 void *arg;
1941 PyObject *pyobj_reader;
1942 PyObject *pyobj_f;
1943 PyObject *pyobj_arg;
1944 PyObject *py_retval;
1945
1946 if (!PyArg_ParseTuple(args, (char *)"OOO:xmlTextReaderSetErrorHandler", &pyobj_reader, &pyobj_f, &pyobj_arg))
1947 return(NULL);
1948 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
1949 /* clear previous error handler */
1950 xmlTextReaderGetErrorHandler(reader,&f,&arg);
1951 if (arg != NULL) {
Daniel Veillardc2664642003-07-29 20:44:53 +00001952 if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) {
Daniel Veillard26f70262003-01-16 22:45:08 +00001953 /* ok, it's our error handler! */
1954 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
1955 Py_XDECREF(pyCtxt->f);
1956 Py_XDECREF(pyCtxt->arg);
1957 xmlFree(pyCtxt);
1958 }
1959 else {
1960 /*
1961 * there already an arg, and it's not ours,
1962 * there is definitely something wrong going on here...
1963 * we don't know how to free it, so we bail out...
1964 */
1965 py_retval = libxml_intWrap(-1);
1966 return(py_retval);
1967 }
1968 }
1969 xmlTextReaderSetErrorHandler(reader,NULL,NULL);
1970 /* set new error handler */
1971 if (pyobj_f != Py_None)
1972 {
1973 pyCtxt = (xmlTextReaderPyCtxtPtr)xmlMalloc(sizeof(xmlTextReaderPyCtxt));
1974 if (pyCtxt == NULL) {
1975 py_retval = libxml_intWrap(-1);
1976 return(py_retval);
1977 }
1978 Py_XINCREF(pyobj_f);
1979 pyCtxt->f = pyobj_f;
1980 Py_XINCREF(pyobj_arg);
1981 pyCtxt->arg = pyobj_arg;
Daniel Veillardc2664642003-07-29 20:44:53 +00001982 xmlTextReaderSetErrorHandler(reader,
1983 (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback,
1984 pyCtxt);
Daniel Veillard26f70262003-01-16 22:45:08 +00001985 }
1986
1987 py_retval = libxml_intWrap(1);
1988 return(py_retval);
1989}
1990
Daniel Veillardc2664642003-07-29 20:44:53 +00001991static PyObject *
Daniel Veillard26f70262003-01-16 22:45:08 +00001992libxml_xmlTextReaderGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1993{
1994 xmlTextReaderPtr reader;
1995 xmlTextReaderPyCtxtPtr pyCtxt;
1996 xmlTextReaderErrorFunc f;
1997 void *arg;
1998 PyObject *pyobj_reader;
1999 PyObject *py_retval;
2000
2001 if (!PyArg_ParseTuple(args, (char *)"O:xmlTextReaderSetErrorHandler", &pyobj_reader))
2002 return(NULL);
2003 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
2004 xmlTextReaderGetErrorHandler(reader,&f,&arg);
2005 py_retval = PyTuple_New(2);
Daniel Veillardc2664642003-07-29 20:44:53 +00002006 if (f == (xmlTextReaderErrorFunc)libxml_xmlTextReaderErrorCallback) {
Daniel Veillard26f70262003-01-16 22:45:08 +00002007 /* ok, it's our error handler! */
2008 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
2009 PyTuple_SetItem(py_retval, 0, pyCtxt->f);
2010 Py_XINCREF(pyCtxt->f);
2011 PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
2012 Py_XINCREF(pyCtxt->arg);
2013 }
2014 else
2015 {
2016 /* f is null or it's not our error handler */
2017 PyTuple_SetItem(py_retval, 0, Py_None);
2018 Py_XINCREF(Py_None);
2019 PyTuple_SetItem(py_retval, 1, Py_None);
2020 Py_XINCREF(Py_None);
2021 }
2022 return(py_retval);
2023}
2024
Daniel Veillardc2664642003-07-29 20:44:53 +00002025static PyObject *
Daniel Veillard26f70262003-01-16 22:45:08 +00002026libxml_xmlFreeTextReader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
2027 xmlTextReaderPtr reader;
2028 PyObject *pyobj_reader;
2029 xmlTextReaderPyCtxtPtr pyCtxt;
2030 xmlTextReaderErrorFunc f;
2031 void *arg;
2032
Daniel Veillard157fee02003-10-31 10:36:03 +00002033 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeTextReader", &pyobj_reader))
2034 return(NULL);
2035 if (!PyCObject_Check(pyobj_reader)) {
Daniel Veillardbb3ba322003-10-30 13:12:43 +00002036 Py_INCREF(Py_None);
2037 return(Py_None);
2038 }
Daniel Veillard26f70262003-01-16 22:45:08 +00002039 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
Daniel Veillardbb3ba322003-10-30 13:12:43 +00002040 if (reader == NULL) {
2041 Py_INCREF(Py_None);
2042 return(Py_None);
2043 }
Daniel Veillard26f70262003-01-16 22:45:08 +00002044
2045 xmlTextReaderGetErrorHandler(reader,&f,&arg);
2046 if (arg != NULL) {
Daniel Veillardc2664642003-07-29 20:44:53 +00002047 if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) {
Daniel Veillard26f70262003-01-16 22:45:08 +00002048 /* ok, it's our error handler! */
2049 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
2050 Py_XDECREF(pyCtxt->f);
2051 Py_XDECREF(pyCtxt->arg);
2052 xmlFree(pyCtxt);
2053 }
2054 /*
2055 * else, something wrong happened, because the error handler is
2056 * not owned by the python bindings...
2057 */
2058 }
2059
2060 xmlFreeTextReader(reader);
2061 Py_INCREF(Py_None);
2062 return(Py_None);
2063}
Daniel Veillard438ebbd2008-05-12 12:58:46 +00002064#endif
Daniel Veillard26f70262003-01-16 22:45:08 +00002065
2066/************************************************************************
2067 * *
Daniel Veillarda7340c82002-02-01 17:56:45 +00002068 * XPath extensions *
2069 * *
2070 ************************************************************************/
2071
Daniel Veillarda7340c82002-02-01 17:56:45 +00002072static void
Daniel Veillardd2379012002-03-15 22:24:56 +00002073libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs)
2074{
Daniel Veillarda7340c82002-02-01 17:56:45 +00002075 PyObject *list, *cur, *result;
2076 xmlXPathObjectPtr obj;
Daniel Veillard70cab352002-02-06 16:06:58 +00002077 xmlXPathContextPtr rctxt;
2078 PyObject *current_function = NULL;
2079 const xmlChar *name;
2080 const xmlChar *ns_uri;
Daniel Veillarda7340c82002-02-01 17:56:45 +00002081 int i;
2082
Daniel Veillard70cab352002-02-06 16:06:58 +00002083 if (ctxt == NULL)
Daniel Veillardd2379012002-03-15 22:24:56 +00002084 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00002085 rctxt = ctxt->context;
2086 if (rctxt == NULL)
Daniel Veillardd2379012002-03-15 22:24:56 +00002087 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00002088 name = rctxt->function;
2089 ns_uri = rctxt->functionURI;
Daniel Veillarda7340c82002-02-01 17:56:45 +00002090#ifdef DEBUG_XPATH
Daniel Veillardd2379012002-03-15 22:24:56 +00002091 printf("libxml_xmlXPathFuncCallback called name %s URI %s\n", name,
2092 ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002093#endif
2094
Daniel Veillard70cab352002-02-06 16:06:58 +00002095 /*
2096 * Find the function, it should be there it was there at lookup
2097 */
Daniel Veillardd2379012002-03-15 22:24:56 +00002098 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
2099 if ( /* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002100 (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
2101 (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
2102 current_function = (*libxml_xpathCallbacks)[i].function;
Daniel Veillardd2379012002-03-15 22:24:56 +00002103 }
Daniel Veillard70cab352002-02-06 16:06:58 +00002104 }
2105 if (current_function == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002106 printf
2107 ("libxml_xmlXPathFuncCallback: internal error %s not found !\n",
2108 name);
2109 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00002110 }
2111
Daniel Veillardc575b992002-02-08 13:28:40 +00002112 list = PyTuple_New(nargs + 1);
2113 PyTuple_SetItem(list, 0, libxml_xmlXPathParserContextPtrWrap(ctxt));
Daniel Veillard05349ab2004-01-25 20:01:35 +00002114 for (i = nargs - 1; i >= 0; i--) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002115 obj = valuePop(ctxt);
2116 cur = libxml_xmlXPathObjectPtrWrap(obj);
2117 PyTuple_SetItem(list, i + 1, cur);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002118 }
2119 result = PyEval_CallObject(current_function, list);
2120 Py_DECREF(list);
2121
2122 obj = libxml_xmlXPathObjectPtrConvert(result);
2123 valuePush(ctxt, obj);
2124}
2125
2126static xmlXPathFunction
Daniel Veillardd2379012002-03-15 22:24:56 +00002127libxml_xmlXPathFuncLookupFunc(void *ctxt, const xmlChar * name,
2128 const xmlChar * ns_uri)
2129{
Daniel Veillarda7340c82002-02-01 17:56:45 +00002130 int i;
Daniel Veillardd2379012002-03-15 22:24:56 +00002131
Daniel Veillarda7340c82002-02-01 17:56:45 +00002132#ifdef DEBUG_XPATH
2133 printf("libxml_xmlXPathFuncLookupFunc(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00002134 ctxt, name, ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002135#endif
Daniel Veillard70cab352002-02-06 16:06:58 +00002136 /*
2137 * This is called once only. The address is then stored in the
2138 * XPath expression evaluation, the proper object to call can
2139 * then still be found using the execution context function
2140 * and functionURI fields.
2141 */
Daniel Veillardd2379012002-03-15 22:24:56 +00002142 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002143 if ((ctxt == (*libxml_xpathCallbacks)[i].ctx) &&
2144 (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
2145 (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002146 return (libxml_xmlXPathFuncCallback);
2147 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00002148 }
Daniel Veillardd2379012002-03-15 22:24:56 +00002149 return (NULL);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002150}
2151
2152static void
Daniel Veillardd2379012002-03-15 22:24:56 +00002153libxml_xpathCallbacksInitialize(void)
2154{
Daniel Veillarda7340c82002-02-01 17:56:45 +00002155 int i;
2156
2157 if (libxml_xpathCallbacksInitialized != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00002158 return;
Daniel Veillarda7340c82002-02-01 17:56:45 +00002159
2160#ifdef DEBUG_XPATH
2161 printf("libxml_xpathCallbacksInitialized called\n");
2162#endif
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002163 libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlMalloc(
2164 libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
Daniel Veillarda7340c82002-02-01 17:56:45 +00002165
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002166 for (i = 0; i < libxml_xpathCallbacksAllocd; i++) {
2167 (*libxml_xpathCallbacks)[i].ctx = NULL;
2168 (*libxml_xpathCallbacks)[i].name = NULL;
2169 (*libxml_xpathCallbacks)[i].ns_uri = NULL;
2170 (*libxml_xpathCallbacks)[i].function = NULL;
Daniel Veillarda7340c82002-02-01 17:56:45 +00002171 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00002172 libxml_xpathCallbacksInitialized = 1;
2173}
2174
2175PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002176libxml_xmlRegisterXPathFunction(ATTRIBUTE_UNUSED PyObject * self,
2177 PyObject * args)
2178{
Daniel Veillarda7340c82002-02-01 17:56:45 +00002179 PyObject *py_retval;
2180 int c_retval = 0;
2181 xmlChar *name;
2182 xmlChar *ns_uri;
2183 xmlXPathContextPtr ctx;
2184 PyObject *pyobj_ctx;
2185 PyObject *pyobj_f;
2186 int i;
2187
Daniel Veillardd2379012002-03-15 22:24:56 +00002188 if (!PyArg_ParseTuple
2189 (args, (char *) "OszO:registerXPathFunction", &pyobj_ctx, &name,
2190 &ns_uri, &pyobj_f))
2191 return (NULL);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002192
2193 ctx = (xmlXPathContextPtr) PyxmlXPathContext_Get(pyobj_ctx);
2194 if (libxml_xpathCallbacksInitialized == 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00002195 libxml_xpathCallbacksInitialize();
Daniel Veillarda7340c82002-02-01 17:56:45 +00002196 xmlXPathRegisterFuncLookup(ctx, libxml_xmlXPathFuncLookupFunc, ctx);
2197
2198 if ((pyobj_ctx == NULL) || (name == NULL) || (pyobj_f == NULL)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002199 py_retval = libxml_intWrap(-1);
2200 return (py_retval);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002201 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00002202#ifdef DEBUG_XPATH
2203 printf("libxml_registerXPathFunction(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00002204 ctx, name, ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002205#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002206 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002207 if ((ctx == (*libxml_xpathCallbacks)[i].ctx) &&
2208 (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
2209 (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002210 Py_XINCREF(pyobj_f);
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002211 Py_XDECREF((*libxml_xpathCallbacks)[i].function);
2212 (*libxml_xpathCallbacks)[i].function = pyobj_f;
Daniel Veillardd2379012002-03-15 22:24:56 +00002213 c_retval = 1;
2214 goto done;
2215 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00002216 }
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002217 if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksAllocd) {
2218 libxml_xpathCallbacksAllocd+=10;
2219 libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlRealloc(
2220 libxml_xpathCallbacks,
2221 libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
2222 }
2223 i = libxml_xpathCallbacksNb++;
2224 Py_XINCREF(pyobj_f);
2225 (*libxml_xpathCallbacks)[i].ctx = ctx;
2226 (*libxml_xpathCallbacks)[i].name = xmlStrdup(name);
2227 (*libxml_xpathCallbacks)[i].ns_uri = xmlStrdup(ns_uri);
2228 (*libxml_xpathCallbacks)[i].function = pyobj_f;
Daniel Veillardd2379012002-03-15 22:24:56 +00002229 c_retval = 1;
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002230
Daniel Veillardd2379012002-03-15 22:24:56 +00002231 done:
Daniel Veillarda7340c82002-02-01 17:56:45 +00002232 py_retval = libxml_intWrap((int) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00002233 return (py_retval);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002234}
2235
Daniel Veillard1971ee22002-01-31 20:29:19 +00002236/************************************************************************
2237 * *
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002238 * Global properties access *
2239 * *
2240 ************************************************************************/
2241static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002242libxml_name(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002243{
2244 PyObject *resultobj, *obj;
2245 xmlNodePtr cur;
2246 const xmlChar *res;
2247
Daniel Veillardd2379012002-03-15 22:24:56 +00002248 if (!PyArg_ParseTuple(args, (char *) "O:name", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002249 return NULL;
2250 cur = PyxmlNode_Get(obj);
2251
2252#ifdef DEBUG
2253 printf("libxml_name: cur = %p type %d\n", cur, cur->type);
2254#endif
2255
Daniel Veillardd2379012002-03-15 22:24:56 +00002256 switch (cur->type) {
2257 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002258#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002259 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002260#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002261 case XML_HTML_DOCUMENT_NODE:{
2262 xmlDocPtr doc = (xmlDocPtr) cur;
2263
2264 res = doc->URL;
2265 break;
2266 }
2267 case XML_ATTRIBUTE_NODE:{
2268 xmlAttrPtr attr = (xmlAttrPtr) cur;
2269
2270 res = attr->name;
2271 break;
2272 }
2273 case XML_NAMESPACE_DECL:{
2274 xmlNsPtr ns = (xmlNsPtr) cur;
2275
2276 res = ns->prefix;
2277 break;
2278 }
2279 default:
2280 res = cur->name;
2281 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002282 }
Daniel Veillard1971ee22002-01-31 20:29:19 +00002283 resultobj = libxml_constxmlCharPtrWrap(res);
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002284
2285 return resultobj;
2286}
2287
2288static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002289libxml_doc(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002290{
2291 PyObject *resultobj, *obj;
2292 xmlNodePtr cur;
2293 xmlDocPtr res;
2294
Daniel Veillardd2379012002-03-15 22:24:56 +00002295 if (!PyArg_ParseTuple(args, (char *) "O:doc", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002296 return NULL;
2297 cur = PyxmlNode_Get(obj);
2298
2299#ifdef DEBUG
2300 printf("libxml_doc: cur = %p\n", cur);
2301#endif
2302
Daniel Veillardd2379012002-03-15 22:24:56 +00002303 switch (cur->type) {
2304 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002305#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002306 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002307#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002308 case XML_HTML_DOCUMENT_NODE:
2309 res = NULL;
2310 break;
2311 case XML_ATTRIBUTE_NODE:{
2312 xmlAttrPtr attr = (xmlAttrPtr) cur;
2313
2314 res = attr->doc;
2315 break;
2316 }
2317 case XML_NAMESPACE_DECL:
2318 res = NULL;
2319 break;
2320 default:
2321 res = cur->doc;
2322 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002323 }
2324 resultobj = libxml_xmlDocPtrWrap(res);
2325 return resultobj;
2326}
2327
2328static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002329libxml_properties(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002330{
2331 PyObject *resultobj, *obj;
Daniel Veillard2728f842006-03-09 16:49:24 +00002332 xmlNodePtr cur;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002333 xmlAttrPtr res;
2334
Daniel Veillardd2379012002-03-15 22:24:56 +00002335 if (!PyArg_ParseTuple(args, (char *) "O:properties", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002336 return NULL;
2337 cur = PyxmlNode_Get(obj);
Daniel Veillard2728f842006-03-09 16:49:24 +00002338 if ((cur != NULL) && (cur->type == XML_ELEMENT_NODE))
Daniel Veillardd2379012002-03-15 22:24:56 +00002339 res = cur->properties;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002340 else
Daniel Veillardd2379012002-03-15 22:24:56 +00002341 res = NULL;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002342 resultobj = libxml_xmlAttrPtrWrap(res);
2343 return resultobj;
2344}
2345
2346static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002347libxml_next(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002348{
2349 PyObject *resultobj, *obj;
2350 xmlNodePtr cur;
2351 xmlNodePtr res;
2352
Daniel Veillardd2379012002-03-15 22:24:56 +00002353 if (!PyArg_ParseTuple(args, (char *) "O:next", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002354 return NULL;
2355 cur = PyxmlNode_Get(obj);
2356
2357#ifdef DEBUG
2358 printf("libxml_next: cur = %p\n", cur);
2359#endif
2360
Daniel Veillardd2379012002-03-15 22:24:56 +00002361 switch (cur->type) {
2362 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002363#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002364 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002365#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002366 case XML_HTML_DOCUMENT_NODE:
2367 res = NULL;
2368 break;
2369 case XML_ATTRIBUTE_NODE:{
2370 xmlAttrPtr attr = (xmlAttrPtr) cur;
2371
2372 res = (xmlNodePtr) attr->next;
2373 break;
2374 }
2375 case XML_NAMESPACE_DECL:{
2376 xmlNsPtr ns = (xmlNsPtr) cur;
2377
2378 res = (xmlNodePtr) ns->next;
2379 break;
2380 }
2381 default:
2382 res = cur->next;
2383 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002384
2385 }
2386 resultobj = libxml_xmlNodePtrWrap(res);
2387 return resultobj;
2388}
2389
2390static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002391libxml_prev(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002392{
2393 PyObject *resultobj, *obj;
2394 xmlNodePtr cur;
2395 xmlNodePtr res;
2396
Daniel Veillardd2379012002-03-15 22:24:56 +00002397 if (!PyArg_ParseTuple(args, (char *) "O:prev", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002398 return NULL;
2399 cur = PyxmlNode_Get(obj);
2400
2401#ifdef DEBUG
2402 printf("libxml_prev: cur = %p\n", cur);
2403#endif
2404
Daniel Veillardd2379012002-03-15 22:24:56 +00002405 switch (cur->type) {
2406 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002407#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002408 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002409#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002410 case XML_HTML_DOCUMENT_NODE:
2411 res = NULL;
2412 break;
2413 case XML_ATTRIBUTE_NODE:{
2414 xmlAttrPtr attr = (xmlAttrPtr) cur;
2415
Daniel Veillardfaa35ff2002-11-24 13:53:43 +00002416 res = (xmlNodePtr) attr->prev;
Daniel Veillardd2379012002-03-15 22:24:56 +00002417 }
2418 case XML_NAMESPACE_DECL:
2419 res = NULL;
2420 break;
2421 default:
Daniel Veillardfaa35ff2002-11-24 13:53:43 +00002422 res = cur->prev;
Daniel Veillardd2379012002-03-15 22:24:56 +00002423 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002424 }
2425 resultobj = libxml_xmlNodePtrWrap(res);
2426 return resultobj;
2427}
2428
2429static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002430libxml_children(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002431{
2432 PyObject *resultobj, *obj;
2433 xmlNodePtr cur;
2434 xmlNodePtr res;
2435
Daniel Veillardd2379012002-03-15 22:24:56 +00002436 if (!PyArg_ParseTuple(args, (char *) "O:children", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002437 return NULL;
2438 cur = PyxmlNode_Get(obj);
2439
2440#ifdef DEBUG
2441 printf("libxml_children: cur = %p\n", cur);
2442#endif
2443
Daniel Veillardd2379012002-03-15 22:24:56 +00002444 switch (cur->type) {
2445 case XML_ELEMENT_NODE:
2446 case XML_ENTITY_REF_NODE:
2447 case XML_ENTITY_NODE:
2448 case XML_PI_NODE:
2449 case XML_COMMENT_NODE:
2450 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002451#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002452 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002453#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002454 case XML_HTML_DOCUMENT_NODE:
2455 case XML_DTD_NODE:
2456 res = cur->children;
2457 break;
2458 case XML_ATTRIBUTE_NODE:{
2459 xmlAttrPtr attr = (xmlAttrPtr) cur;
2460
2461 res = attr->children;
2462 break;
2463 }
2464 default:
2465 res = NULL;
2466 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002467 }
2468 resultobj = libxml_xmlNodePtrWrap(res);
2469 return resultobj;
2470}
2471
2472static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002473libxml_last(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002474{
2475 PyObject *resultobj, *obj;
2476 xmlNodePtr cur;
2477 xmlNodePtr res;
2478
Daniel Veillardd2379012002-03-15 22:24:56 +00002479 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002480 return NULL;
2481 cur = PyxmlNode_Get(obj);
2482
2483#ifdef DEBUG
2484 printf("libxml_last: cur = %p\n", cur);
2485#endif
2486
Daniel Veillardd2379012002-03-15 22:24:56 +00002487 switch (cur->type) {
2488 case XML_ELEMENT_NODE:
2489 case XML_ENTITY_REF_NODE:
2490 case XML_ENTITY_NODE:
2491 case XML_PI_NODE:
2492 case XML_COMMENT_NODE:
2493 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002494#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002495 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002496#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002497 case XML_HTML_DOCUMENT_NODE:
2498 case XML_DTD_NODE:
2499 res = cur->last;
2500 break;
2501 case XML_ATTRIBUTE_NODE:{
2502 xmlAttrPtr attr = (xmlAttrPtr) cur;
2503
2504 res = attr->last;
2505 }
2506 default:
2507 res = NULL;
2508 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002509 }
2510 resultobj = libxml_xmlNodePtrWrap(res);
2511 return resultobj;
2512}
2513
2514static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002515libxml_parent(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002516{
2517 PyObject *resultobj, *obj;
2518 xmlNodePtr cur;
2519 xmlNodePtr res;
2520
Daniel Veillardd2379012002-03-15 22:24:56 +00002521 if (!PyArg_ParseTuple(args, (char *) "O:parent", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002522 return NULL;
2523 cur = PyxmlNode_Get(obj);
2524
2525#ifdef DEBUG
2526 printf("libxml_parent: cur = %p\n", cur);
2527#endif
2528
Daniel Veillardd2379012002-03-15 22:24:56 +00002529 switch (cur->type) {
2530 case XML_DOCUMENT_NODE:
2531 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002532#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002533 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002534#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002535 res = NULL;
2536 break;
2537 case XML_ATTRIBUTE_NODE:{
2538 xmlAttrPtr attr = (xmlAttrPtr) cur;
2539
2540 res = attr->parent;
2541 }
Daniel Veillard22eda2b2005-08-01 05:20:16 +00002542 break;
Daniel Veillardd2379012002-03-15 22:24:56 +00002543 case XML_ENTITY_DECL:
2544 case XML_NAMESPACE_DECL:
2545 case XML_XINCLUDE_START:
2546 case XML_XINCLUDE_END:
2547 res = NULL;
2548 break;
2549 default:
2550 res = cur->parent;
2551 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002552 }
2553 resultobj = libxml_xmlNodePtrWrap(res);
2554 return resultobj;
2555}
2556
2557static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002558libxml_type(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002559{
2560 PyObject *resultobj, *obj;
2561 xmlNodePtr cur;
Daniel Veillardd2379012002-03-15 22:24:56 +00002562 const xmlChar *res = NULL;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002563
Daniel Veillardd2379012002-03-15 22:24:56 +00002564 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002565 return NULL;
2566 cur = PyxmlNode_Get(obj);
2567
2568#ifdef DEBUG
2569 printf("libxml_type: cur = %p\n", cur);
2570#endif
2571
Daniel Veillardd2379012002-03-15 22:24:56 +00002572 switch (cur->type) {
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002573 case XML_ELEMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002574 res = (const xmlChar *) "element";
2575 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002576 case XML_ATTRIBUTE_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002577 res = (const xmlChar *) "attribute";
2578 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002579 case XML_TEXT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002580 res = (const xmlChar *) "text";
2581 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002582 case XML_CDATA_SECTION_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002583 res = (const xmlChar *) "cdata";
2584 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002585 case XML_ENTITY_REF_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002586 res = (const xmlChar *) "entity_ref";
2587 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002588 case XML_ENTITY_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002589 res = (const xmlChar *) "entity";
2590 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002591 case XML_PI_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002592 res = (const xmlChar *) "pi";
2593 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002594 case XML_COMMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002595 res = (const xmlChar *) "comment";
2596 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002597 case XML_DOCUMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002598 res = (const xmlChar *) "document_xml";
2599 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002600 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002601 res = (const xmlChar *) "doctype";
2602 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002603 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002604 res = (const xmlChar *) "fragment";
2605 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002606 case XML_NOTATION_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002607 res = (const xmlChar *) "notation";
2608 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002609 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002610 res = (const xmlChar *) "document_html";
2611 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002612 case XML_DTD_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002613 res = (const xmlChar *) "dtd";
2614 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002615 case XML_ELEMENT_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002616 res = (const xmlChar *) "elem_decl";
2617 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002618 case XML_ATTRIBUTE_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002619 res = (const xmlChar *) "attribute_decl";
2620 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002621 case XML_ENTITY_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002622 res = (const xmlChar *) "entity_decl";
2623 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002624 case XML_NAMESPACE_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002625 res = (const xmlChar *) "namespace";
2626 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002627 case XML_XINCLUDE_START:
Daniel Veillardd2379012002-03-15 22:24:56 +00002628 res = (const xmlChar *) "xinclude_start";
2629 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002630 case XML_XINCLUDE_END:
Daniel Veillardd2379012002-03-15 22:24:56 +00002631 res = (const xmlChar *) "xinclude_end";
2632 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002633#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002634 case XML_DOCB_DOCUMENT_NODE:
2635 res = (const xmlChar *) "document_docbook";
2636 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002637#endif
2638 }
2639#ifdef DEBUG
2640 printf("libxml_type: cur = %p: %s\n", cur, res);
2641#endif
2642
Daniel Veillard1971ee22002-01-31 20:29:19 +00002643 resultobj = libxml_constxmlCharPtrWrap(res);
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002644 return resultobj;
2645}
2646
2647/************************************************************************
2648 * *
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002649 * Specific accessor functions *
2650 * *
2651 ************************************************************************/
2652PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002653libxml_xmlNodeGetNsDefs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2654{
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002655 PyObject *py_retval;
2656 xmlNsPtr c_retval;
2657 xmlNodePtr node;
2658 PyObject *pyobj_node;
2659
Daniel Veillardd2379012002-03-15 22:24:56 +00002660 if (!PyArg_ParseTuple
2661 (args, (char *) "O:xmlNodeGetNsDefs", &pyobj_node))
2662 return (NULL);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002663 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2664
2665 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002666 Py_INCREF(Py_None);
2667 return (Py_None);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002668 }
2669 c_retval = node->nsDef;
2670 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00002671 return (py_retval);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002672}
2673
2674PyObject *
Daniel Veillardf9cf6f52005-04-12 01:02:29 +00002675libxml_xmlNodeRemoveNsDef(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2676{
2677 PyObject *py_retval;
2678 xmlNsPtr ns, prev;
2679 xmlNodePtr node;
2680 PyObject *pyobj_node;
2681 xmlChar *href;
2682 xmlNsPtr c_retval;
2683
2684 if (!PyArg_ParseTuple
2685 (args, (char *) "Oz:xmlNodeRemoveNsDef", &pyobj_node, &href))
2686 return (NULL);
2687 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2688 ns = NULL;
2689
2690 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
2691 Py_INCREF(Py_None);
2692 return (Py_None);
2693 }
2694
2695 if (href == NULL) {
2696 ns = node->nsDef;
2697 node->nsDef = NULL;
2698 c_retval = 0;
2699 }
2700 else {
2701 prev = NULL;
2702 ns = node->nsDef;
2703 while (ns != NULL) {
2704 if (xmlStrEqual(ns->href, href)) {
2705 if (prev != NULL)
2706 prev->next = ns->next;
2707 else
2708 node->nsDef = ns->next;
2709 ns->next = NULL;
2710 c_retval = 0;
2711 break;
2712 }
2713 prev = ns;
2714 ns = ns->next;
2715 }
2716 }
2717
2718 c_retval = ns;
2719 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
2720 return (py_retval);
2721}
2722
2723PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002724libxml_xmlNodeGetNs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2725{
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002726 PyObject *py_retval;
2727 xmlNsPtr c_retval;
2728 xmlNodePtr node;
2729 PyObject *pyobj_node;
2730
Daniel Veillardd2379012002-03-15 22:24:56 +00002731 if (!PyArg_ParseTuple(args, (char *) "O:xmlNodeGetNs", &pyobj_node))
2732 return (NULL);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002733 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2734
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002735 if ((node == NULL) ||
2736 ((node->type != XML_ELEMENT_NODE) &&
2737 (node->type != XML_ATTRIBUTE_NODE))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002738 Py_INCREF(Py_None);
2739 return (Py_None);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002740 }
2741 c_retval = node->ns;
2742 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00002743 return (py_retval);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002744}
2745
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002746#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002747/************************************************************************
2748 * *
Daniel Veillard1e774382002-03-06 17:35:40 +00002749 * Serialization front-end *
2750 * *
2751 ************************************************************************/
2752
Daniel Veillardd2379012002-03-15 22:24:56 +00002753static PyObject *
2754libxml_serializeNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2755{
Daniel Veillard1e774382002-03-06 17:35:40 +00002756 PyObject *py_retval = NULL;
2757 xmlChar *c_retval;
2758 PyObject *pyobj_node;
2759 xmlNodePtr node;
2760 xmlDocPtr doc;
Daniel Veillardd2379012002-03-15 22:24:56 +00002761 const char *encoding;
Daniel Veillard1e774382002-03-06 17:35:40 +00002762 int format;
2763 int len;
2764
Daniel Veillardd2379012002-03-15 22:24:56 +00002765 if (!PyArg_ParseTuple(args, (char *) "Ozi:serializeNode", &pyobj_node,
2766 &encoding, &format))
2767 return (NULL);
Daniel Veillard1e774382002-03-06 17:35:40 +00002768 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2769
2770 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002771 Py_INCREF(Py_None);
2772 return (Py_None);
Daniel Veillard1e774382002-03-06 17:35:40 +00002773 }
2774 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002775 doc = (xmlDocPtr) node;
2776 xmlDocDumpFormatMemoryEnc(doc, &c_retval, &len,
2777 (const char *) encoding, format);
2778 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard656ce942004-04-30 23:11:45 +00002779#ifdef LIBXML_HTML_ENABLED
Daniel Veillard1e774382002-03-06 17:35:40 +00002780 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002781 xmlOutputBufferPtr buf;
2782 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00002783
Daniel Veillardd2379012002-03-15 22:24:56 +00002784 doc = (xmlDocPtr) node;
2785 if (encoding != NULL)
2786 htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
2787 encoding = (const char *) htmlGetMetaEncoding(doc);
Daniel Veillard1e774382002-03-06 17:35:40 +00002788
Daniel Veillardd2379012002-03-15 22:24:56 +00002789 if (encoding != NULL) {
2790 handler = xmlFindCharEncodingHandler(encoding);
2791 if (handler == NULL) {
2792 Py_INCREF(Py_None);
2793 return (Py_None);
2794 }
2795 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002796
Daniel Veillardd2379012002-03-15 22:24:56 +00002797 /*
2798 * Fallback to HTML or ASCII when the encoding is unspecified
2799 */
2800 if (handler == NULL)
2801 handler = xmlFindCharEncodingHandler("HTML");
2802 if (handler == NULL)
2803 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00002804
Daniel Veillardd2379012002-03-15 22:24:56 +00002805 buf = xmlAllocOutputBuffer(handler);
2806 if (buf == NULL) {
2807 Py_INCREF(Py_None);
2808 return (Py_None);
2809 }
2810 htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
2811 xmlOutputBufferFlush(buf);
2812 if (buf->conv != NULL) {
2813 len = buf->conv->use;
2814 c_retval = buf->conv->content;
2815 buf->conv->content = NULL;
2816 } else {
2817 len = buf->buffer->use;
2818 c_retval = buf->buffer->content;
2819 buf->buffer->content = NULL;
2820 }
2821 (void) xmlOutputBufferClose(buf);
2822 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard656ce942004-04-30 23:11:45 +00002823#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard1e774382002-03-06 17:35:40 +00002824 } else {
William M. Brack95af5942004-02-08 04:12:49 +00002825 if (node->type == XML_NAMESPACE_DECL)
2826 doc = NULL;
2827 else
2828 doc = node->doc;
Daniel Veillarda8c0adb2002-11-17 22:37:35 +00002829 if ((doc == NULL) || (doc->type == XML_DOCUMENT_NODE)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002830 xmlOutputBufferPtr buf;
2831 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00002832
Daniel Veillardd2379012002-03-15 22:24:56 +00002833 if (encoding != NULL) {
2834 handler = xmlFindCharEncodingHandler(encoding);
2835 if (handler == NULL) {
2836 Py_INCREF(Py_None);
2837 return (Py_None);
2838 }
2839 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002840
Daniel Veillardd2379012002-03-15 22:24:56 +00002841 buf = xmlAllocOutputBuffer(handler);
2842 if (buf == NULL) {
2843 Py_INCREF(Py_None);
2844 return (Py_None);
2845 }
2846 xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
2847 xmlOutputBufferFlush(buf);
2848 if (buf->conv != NULL) {
2849 len = buf->conv->use;
2850 c_retval = buf->conv->content;
2851 buf->conv->content = NULL;
2852 } else {
2853 len = buf->buffer->use;
2854 c_retval = buf->buffer->content;
2855 buf->buffer->content = NULL;
2856 }
2857 (void) xmlOutputBufferClose(buf);
2858 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard656ce942004-04-30 23:11:45 +00002859#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002860 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2861 xmlOutputBufferPtr buf;
2862 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00002863
Daniel Veillardd2379012002-03-15 22:24:56 +00002864 if (encoding != NULL)
2865 htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
2866 encoding = (const char *) htmlGetMetaEncoding(doc);
2867 if (encoding != NULL) {
2868 handler = xmlFindCharEncodingHandler(encoding);
2869 if (handler == NULL) {
2870 Py_INCREF(Py_None);
2871 return (Py_None);
2872 }
2873 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002874
Daniel Veillardd2379012002-03-15 22:24:56 +00002875 /*
2876 * Fallback to HTML or ASCII when the encoding is unspecified
2877 */
2878 if (handler == NULL)
2879 handler = xmlFindCharEncodingHandler("HTML");
2880 if (handler == NULL)
2881 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00002882
Daniel Veillardd2379012002-03-15 22:24:56 +00002883 buf = xmlAllocOutputBuffer(handler);
2884 if (buf == NULL) {
2885 Py_INCREF(Py_None);
2886 return (Py_None);
2887 }
2888 htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
2889 xmlOutputBufferFlush(buf);
2890 if (buf->conv != NULL) {
2891 len = buf->conv->use;
2892 c_retval = buf->conv->content;
2893 buf->conv->content = NULL;
2894 } else {
2895 len = buf->buffer->use;
2896 c_retval = buf->buffer->content;
2897 buf->buffer->content = NULL;
2898 }
2899 (void) xmlOutputBufferClose(buf);
2900 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard656ce942004-04-30 23:11:45 +00002901#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2379012002-03-15 22:24:56 +00002902 } else {
2903 Py_INCREF(Py_None);
2904 return (Py_None);
2905 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002906 }
Daniel Veillardd2379012002-03-15 22:24:56 +00002907 return (py_retval);
Daniel Veillard1e774382002-03-06 17:35:40 +00002908}
2909
Daniel Veillardd2379012002-03-15 22:24:56 +00002910static PyObject *
2911libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2912{
Daniel Veillard1e774382002-03-06 17:35:40 +00002913 PyObject *py_file = NULL;
2914 FILE *output;
2915 PyObject *pyobj_node;
2916 xmlNodePtr node;
2917 xmlDocPtr doc;
Daniel Veillardd2379012002-03-15 22:24:56 +00002918 const char *encoding;
Daniel Veillard1e774382002-03-06 17:35:40 +00002919 int format;
2920 int len;
2921 xmlOutputBufferPtr buf;
2922 xmlCharEncodingHandlerPtr handler = NULL;
2923
Daniel Veillardd2379012002-03-15 22:24:56 +00002924 if (!PyArg_ParseTuple(args, (char *) "OOzi:serializeNode", &pyobj_node,
2925 &py_file, &encoding, &format))
2926 return (NULL);
Daniel Veillard1e774382002-03-06 17:35:40 +00002927 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2928
2929 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002930 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002931 }
2932 if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002933 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002934 }
2935 output = PyFile_AsFile(py_file);
2936 if (output == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002937 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002938 }
2939
2940 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002941 doc = (xmlDocPtr) node;
Daniel Veillard1e774382002-03-06 17:35:40 +00002942 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002943 doc = (xmlDocPtr) node;
Daniel Veillard1e774382002-03-06 17:35:40 +00002944 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00002945 doc = node->doc;
Daniel Veillard1e774382002-03-06 17:35:40 +00002946 }
Daniel Veillard656ce942004-04-30 23:11:45 +00002947#ifdef LIBXML_HTML_ENABLED
Daniel Veillard1e774382002-03-06 17:35:40 +00002948 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002949 if (encoding == NULL)
2950 encoding = (const char *) htmlGetMetaEncoding(doc);
Daniel Veillard1e774382002-03-06 17:35:40 +00002951 }
Daniel Veillard656ce942004-04-30 23:11:45 +00002952#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard1e774382002-03-06 17:35:40 +00002953 if (encoding != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002954 handler = xmlFindCharEncodingHandler(encoding);
2955 if (handler == NULL) {
2956 return (PyInt_FromLong((long) -1));
2957 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002958 }
2959 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002960 if (handler == NULL)
2961 handler = xmlFindCharEncodingHandler("HTML");
2962 if (handler == NULL)
2963 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00002964 }
2965
2966 buf = xmlOutputBufferCreateFile(output, handler);
2967 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002968 len = xmlSaveFormatFileTo(buf, doc, encoding, format);
Daniel Veillard656ce942004-04-30 23:11:45 +00002969#ifdef LIBXML_HTML_ENABLED
Daniel Veillard1e774382002-03-06 17:35:40 +00002970 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002971 htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
2972 len = xmlOutputBufferClose(buf);
Daniel Veillard1e774382002-03-06 17:35:40 +00002973 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002974 htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
2975 len = xmlOutputBufferClose(buf);
Daniel Veillard656ce942004-04-30 23:11:45 +00002976#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard1e774382002-03-06 17:35:40 +00002977 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00002978 xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
2979 len = xmlOutputBufferClose(buf);
Daniel Veillard1e774382002-03-06 17:35:40 +00002980 }
Daniel Veillardd2379012002-03-15 22:24:56 +00002981 return (PyInt_FromLong((long) len));
Daniel Veillard1e774382002-03-06 17:35:40 +00002982}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002983#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard1e774382002-03-06 17:35:40 +00002984
2985/************************************************************************
2986 * *
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002987 * Extra stuff *
2988 * *
2989 ************************************************************************/
2990PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002991libxml_xmlNewNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2992{
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002993 PyObject *py_retval;
Daniel Veillardd2379012002-03-15 22:24:56 +00002994 xmlChar *name;
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002995 xmlNodePtr node;
2996
Daniel Veillardd2379012002-03-15 22:24:56 +00002997 if (!PyArg_ParseTuple(args, (char *) "s:xmlNewNode", &name))
2998 return (NULL);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002999 node = (xmlNodePtr) xmlNewNode(NULL, name);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00003000#ifdef DEBUG
Daniel Veillardd2379012002-03-15 22:24:56 +00003001 printf("NewNode: %s : %p\n", name, (void *) node);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00003002#endif
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00003003
3004 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00003005 Py_INCREF(Py_None);
3006 return (Py_None);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00003007 }
3008 py_retval = libxml_xmlNodePtrWrap(node);
Daniel Veillardd2379012002-03-15 22:24:56 +00003009 return (py_retval);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00003010}
3011
Daniel Veillard54396242003-04-23 07:36:50 +00003012
3013/************************************************************************
3014 * *
3015 * Local Catalog stuff *
3016 * *
3017 ************************************************************************/
3018static PyObject *
3019libxml_addLocalCatalog(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3020{
3021 xmlChar *URL;
3022 xmlParserCtxtPtr ctxt;
3023 PyObject *pyobj_ctxt;
3024
3025 if (!PyArg_ParseTuple(args, (char *)"Os:addLocalCatalog", &pyobj_ctxt, &URL))
3026 return(NULL);
3027
3028 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
3029
3030 if (URL != NULL) {
3031 ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL);
3032 }
3033
3034#ifdef DEBUG
3035 printf("LocalCatalog: %s\n", URL);
3036#endif
3037
3038 Py_INCREF(Py_None);
3039 return (Py_None);
3040}
3041
Daniel Veillardc2664642003-07-29 20:44:53 +00003042#ifdef LIBXML_SCHEMAS_ENABLED
3043
3044/************************************************************************
3045 * *
3046 * RelaxNG error handler registration *
3047 * *
3048 ************************************************************************/
3049
3050typedef struct
3051{
3052 PyObject *warn;
3053 PyObject *error;
3054 PyObject *arg;
3055} xmlRelaxNGValidCtxtPyCtxt;
3056typedef xmlRelaxNGValidCtxtPyCtxt *xmlRelaxNGValidCtxtPyCtxtPtr;
3057
3058static void
3059libxml_xmlRelaxNGValidityGenericErrorFuncHandler(void *ctx, char *str)
3060{
3061 PyObject *list;
3062 PyObject *result;
3063 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3064
3065#ifdef DEBUG_ERROR
3066 printf("libxml_xmlRelaxNGValidityGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
3067#endif
3068
3069 pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx;
3070
3071 list = PyTuple_New(2);
3072 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3073 PyTuple_SetItem(list, 1, pyCtxt->arg);
3074 Py_XINCREF(pyCtxt->arg);
3075 result = PyEval_CallObject(pyCtxt->error, list);
3076 if (result == NULL)
3077 {
3078 /* TODO: manage for the exception to be propagated... */
3079 PyErr_Print();
3080 }
3081 Py_XDECREF(list);
3082 Py_XDECREF(result);
3083}
3084
3085static void
3086libxml_xmlRelaxNGValidityGenericWarningFuncHandler(void *ctx, char *str)
3087{
3088 PyObject *list;
3089 PyObject *result;
3090 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3091
3092#ifdef DEBUG_ERROR
3093 printf("libxml_xmlRelaxNGValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str);
3094#endif
3095
3096 pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx;
3097
3098 list = PyTuple_New(2);
3099 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3100 PyTuple_SetItem(list, 1, pyCtxt->arg);
3101 Py_XINCREF(pyCtxt->arg);
3102 result = PyEval_CallObject(pyCtxt->warn, list);
3103 if (result == NULL)
3104 {
3105 /* TODO: manage for the exception to be propagated... */
3106 PyErr_Print();
3107 }
3108 Py_XDECREF(list);
3109 Py_XDECREF(result);
3110}
3111
3112static void
3113libxml_xmlRelaxNGValidityErrorFunc(void *ctx, const char *msg, ...)
3114{
3115 va_list ap;
3116
3117 va_start(ap, msg);
3118 libxml_xmlRelaxNGValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap));
3119 va_end(ap);
3120}
3121
3122static void
3123libxml_xmlRelaxNGValidityWarningFunc(void *ctx, const char *msg, ...)
3124{
3125 va_list ap;
3126
3127 va_start(ap, msg);
3128 libxml_xmlRelaxNGValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap));
3129 va_end(ap);
3130}
3131
3132static PyObject *
3133libxml_xmlRelaxNGSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3134{
3135 PyObject *py_retval;
3136 PyObject *pyobj_error;
3137 PyObject *pyobj_warn;
3138 PyObject *pyobj_ctx;
3139 PyObject *pyobj_arg = Py_None;
3140 xmlRelaxNGValidCtxtPtr ctxt;
3141 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3142
3143 if (!PyArg_ParseTuple
3144 (args, (char *) "OOO|O:xmlRelaxNGSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
3145 return (NULL);
3146
3147#ifdef DEBUG_ERROR
3148 printf("libxml_xmlRelaxNGSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
3149#endif
3150
3151 ctxt = PyrelaxNgValidCtxt_Get(pyobj_ctx);
William M. Brackc1939562003-08-05 15:52:22 +00003152 if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1)
Daniel Veillardc2664642003-07-29 20:44:53 +00003153 {
3154 py_retval = libxml_intWrap(-1);
3155 return(py_retval);
3156 }
3157
3158 if (pyCtxt == NULL)
3159 {
3160 /* first time to set the error handlers */
3161 pyCtxt = xmlMalloc(sizeof(xmlRelaxNGValidCtxtPyCtxt));
3162 if (pyCtxt == NULL) {
3163 py_retval = libxml_intWrap(-1);
3164 return(py_retval);
3165 }
3166 memset(pyCtxt, 0, sizeof(xmlRelaxNGValidCtxtPyCtxt));
3167 }
3168
3169 /* TODO: check warn and error is a function ! */
3170 Py_XDECREF(pyCtxt->error);
3171 Py_XINCREF(pyobj_error);
3172 pyCtxt->error = pyobj_error;
3173
3174 Py_XDECREF(pyCtxt->warn);
3175 Py_XINCREF(pyobj_warn);
3176 pyCtxt->warn = pyobj_warn;
3177
3178 Py_XDECREF(pyCtxt->arg);
3179 Py_XINCREF(pyobj_arg);
3180 pyCtxt->arg = pyobj_arg;
3181
3182 xmlRelaxNGSetValidErrors(ctxt, &libxml_xmlRelaxNGValidityErrorFunc, &libxml_xmlRelaxNGValidityWarningFunc, pyCtxt);
3183
3184 py_retval = libxml_intWrap(1);
3185 return (py_retval);
3186}
3187
3188static PyObject *
3189libxml_xmlRelaxNGFreeValidCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
3190 xmlRelaxNGValidCtxtPtr ctxt;
3191 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3192 PyObject *pyobj_ctxt;
3193
3194 if (!PyArg_ParseTuple(args, (char *)"O:xmlRelaxNGFreeValidCtxt", &pyobj_ctxt))
3195 return(NULL);
3196 ctxt = (xmlRelaxNGValidCtxtPtr) PyrelaxNgValidCtxt_Get(pyobj_ctxt);
3197
William M. Brackc1939562003-08-05 15:52:22 +00003198 if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0)
Daniel Veillardc2664642003-07-29 20:44:53 +00003199 {
3200 if (pyCtxt != NULL)
3201 {
3202 Py_XDECREF(pyCtxt->error);
3203 Py_XDECREF(pyCtxt->warn);
3204 Py_XDECREF(pyCtxt->arg);
3205 xmlFree(pyCtxt);
3206 }
3207 }
3208
3209 xmlRelaxNGFreeValidCtxt(ctxt);
3210 Py_INCREF(Py_None);
3211 return(Py_None);
3212}
3213
Daniel Veillard259f0df2004-08-18 09:13:18 +00003214typedef struct
3215{
3216 PyObject *warn;
3217 PyObject *error;
3218 PyObject *arg;
3219} xmlSchemaValidCtxtPyCtxt;
3220typedef xmlSchemaValidCtxtPyCtxt *xmlSchemaValidCtxtPyCtxtPtr;
3221
3222static void
3223libxml_xmlSchemaValidityGenericErrorFuncHandler(void *ctx, char *str)
3224{
3225 PyObject *list;
3226 PyObject *result;
3227 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3228
3229#ifdef DEBUG_ERROR
3230 printf("libxml_xmlSchemaValiditiyGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
3231#endif
3232
3233 pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx;
3234
3235 list = PyTuple_New(2);
3236 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3237 PyTuple_SetItem(list, 1, pyCtxt->arg);
3238 Py_XINCREF(pyCtxt->arg);
3239 result = PyEval_CallObject(pyCtxt->error, list);
3240 if (result == NULL)
3241 {
3242 /* TODO: manage for the exception to be propagated... */
3243 PyErr_Print();
3244 }
3245 Py_XDECREF(list);
3246 Py_XDECREF(result);
3247}
3248
3249static void
3250libxml_xmlSchemaValidityGenericWarningFuncHandler(void *ctx, char *str)
3251{
3252 PyObject *list;
3253 PyObject *result;
3254 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3255
3256#ifdef DEBUG_ERROR
3257 printf("libxml_xmlSchemaValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str);
3258#endif
3259
3260 pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx;
3261
3262 list = PyTuple_New(2);
3263 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3264 PyTuple_SetItem(list, 1, pyCtxt->arg);
3265 Py_XINCREF(pyCtxt->arg);
3266 result = PyEval_CallObject(pyCtxt->warn, list);
3267 if (result == NULL)
3268 {
3269 /* TODO: manage for the exception to be propagated... */
3270 PyErr_Print();
3271 }
3272 Py_XDECREF(list);
3273 Py_XDECREF(result);
3274}
3275
3276static void
3277libxml_xmlSchemaValidityErrorFunc(void *ctx, const char *msg, ...)
3278{
3279 va_list ap;
3280
3281 va_start(ap, msg);
3282 libxml_xmlSchemaValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap));
3283 va_end(ap);
3284}
3285
3286static void
3287libxml_xmlSchemaValidityWarningFunc(void *ctx, const char *msg, ...)
3288{
3289 va_list ap;
3290
3291 va_start(ap, msg);
3292 libxml_xmlSchemaValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap));
3293 va_end(ap);
3294}
3295
Daniel Veillard6ebf3c42004-08-22 13:11:39 +00003296PyObject *
Daniel Veillard259f0df2004-08-18 09:13:18 +00003297libxml_xmlSchemaSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3298{
3299 PyObject *py_retval;
3300 PyObject *pyobj_error;
3301 PyObject *pyobj_warn;
3302 PyObject *pyobj_ctx;
3303 PyObject *pyobj_arg = Py_None;
3304 xmlSchemaValidCtxtPtr ctxt;
3305 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3306
3307 if (!PyArg_ParseTuple
3308 (args, (char *) "OOO|O:xmlSchemaSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
3309 return (NULL);
3310
3311#ifdef DEBUG_ERROR
3312 printf("libxml_xmlSchemaSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
3313#endif
3314
3315 ctxt = PySchemaValidCtxt_Get(pyobj_ctx);
3316 if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1)
3317 {
3318 py_retval = libxml_intWrap(-1);
3319 return(py_retval);
3320 }
3321
3322 if (pyCtxt == NULL)
3323 {
3324 /* first time to set the error handlers */
3325 pyCtxt = xmlMalloc(sizeof(xmlSchemaValidCtxtPyCtxt));
3326 if (pyCtxt == NULL) {
3327 py_retval = libxml_intWrap(-1);
3328 return(py_retval);
3329 }
3330 memset(pyCtxt, 0, sizeof(xmlSchemaValidCtxtPyCtxt));
3331 }
3332
3333 /* TODO: check warn and error is a function ! */
3334 Py_XDECREF(pyCtxt->error);
3335 Py_XINCREF(pyobj_error);
3336 pyCtxt->error = pyobj_error;
3337
3338 Py_XDECREF(pyCtxt->warn);
3339 Py_XINCREF(pyobj_warn);
3340 pyCtxt->warn = pyobj_warn;
3341
3342 Py_XDECREF(pyCtxt->arg);
3343 Py_XINCREF(pyobj_arg);
3344 pyCtxt->arg = pyobj_arg;
3345
3346 xmlSchemaSetValidErrors(ctxt, &libxml_xmlSchemaValidityErrorFunc, &libxml_xmlSchemaValidityWarningFunc, pyCtxt);
3347
3348 py_retval = libxml_intWrap(1);
3349 return(py_retval);
3350}
3351
Daniel Veillardbb8502c2005-03-30 07:40:35 +00003352static PyObject *
Daniel Veillard259f0df2004-08-18 09:13:18 +00003353libxml_xmlSchemaFreeValidCtxt(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3354{
3355 xmlSchemaValidCtxtPtr ctxt;
3356 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3357 PyObject *pyobj_ctxt;
3358
3359 if (!PyArg_ParseTuple(args, (char *)"O:xmlSchemaFreeValidCtxt", &pyobj_ctxt))
3360 return(NULL);
3361 ctxt = (xmlSchemaValidCtxtPtr) PySchemaValidCtxt_Get(pyobj_ctxt);
3362
3363 if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0)
3364 {
3365 if (pyCtxt != NULL)
3366 {
3367 Py_XDECREF(pyCtxt->error);
3368 Py_XDECREF(pyCtxt->warn);
3369 Py_XDECREF(pyCtxt->arg);
3370 xmlFree(pyCtxt);
3371 }
3372 }
3373
3374 xmlSchemaFreeValidCtxt(ctxt);
3375 Py_INCREF(Py_None);
3376 return(Py_None);
3377}
3378
Daniel Veillardc2664642003-07-29 20:44:53 +00003379#endif
3380
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003381#ifdef LIBXML_C14N_ENABLED
3382#ifdef LIBXML_OUTPUT_ENABLED
3383
3384/************************************************************************
3385 * *
3386 * XML Canonicalization c14n *
3387 * *
3388 ************************************************************************/
3389
3390static int
3391PyxmlNodeSet_Convert(PyObject *py_nodeset, xmlNodeSetPtr *result)
3392{
3393 xmlNodeSetPtr nodeSet;
3394 int is_tuple = 0;
3395
3396 if (PyTuple_Check(py_nodeset))
3397 is_tuple = 1;
3398 else if (PyList_Check(py_nodeset))
3399 is_tuple = 0;
3400 else if (py_nodeset == Py_None) {
3401 *result = NULL;
3402 return 0;
3403 }
3404 else {
3405 PyErr_SetString(PyExc_TypeError,
3406 "must be a tuple or list of nodes.");
3407 return -1;
3408 }
3409
3410 nodeSet = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet));
3411 if (nodeSet == NULL) {
3412 PyErr_SetString(PyExc_MemoryError, "");
3413 return -1;
3414 }
3415
3416 nodeSet->nodeNr = 0;
3417 nodeSet->nodeMax = (is_tuple
3418 ? PyTuple_GET_SIZE(py_nodeset)
3419 : PyList_GET_SIZE(py_nodeset));
3420 nodeSet->nodeTab
3421 = (xmlNodePtr *) xmlMalloc (nodeSet->nodeMax
3422 * sizeof(xmlNodePtr));
3423 if (nodeSet->nodeTab == NULL) {
3424 xmlFree(nodeSet);
3425 PyErr_SetString(PyExc_MemoryError, "");
3426 return -1;
3427 }
3428 memset(nodeSet->nodeTab, 0 ,
3429 nodeSet->nodeMax * sizeof(xmlNodePtr));
3430
3431 {
3432 int idx;
3433 for (idx=0; idx < nodeSet->nodeMax; ++idx) {
3434 xmlNodePtr pynode =
3435 PyxmlNode_Get (is_tuple
3436 ? PyTuple_GET_ITEM(py_nodeset, idx)
3437 : PyList_GET_ITEM(py_nodeset, idx));
3438 if (pynode)
3439 nodeSet->nodeTab[nodeSet->nodeNr++] = pynode;
3440 }
3441 }
3442 *result = nodeSet;
3443 return 0;
3444}
3445
3446static int
3447PystringSet_Convert(PyObject *py_strings, xmlChar *** result)
3448{
3449 /* NOTE: the array should be freed, but the strings are shared
3450 with the python strings and so must not be freed. */
3451
3452 xmlChar ** strings;
3453 int is_tuple = 0;
3454 int count;
3455 int init_index = 0;
3456
3457 if (PyTuple_Check(py_strings))
3458 is_tuple = 1;
3459 else if (PyList_Check(py_strings))
3460 is_tuple = 0;
3461 else if (py_strings == Py_None) {
3462 *result = NULL;
3463 return 0;
3464 }
3465 else {
3466 PyErr_SetString(PyExc_TypeError,
3467 "must be a tuple or list of strings.");
3468 return -1;
3469 }
3470
3471 count = (is_tuple
3472 ? PyTuple_GET_SIZE(py_strings)
3473 : PyList_GET_SIZE(py_strings));
3474
3475 strings = (xmlChar **) xmlMalloc(sizeof(xmlChar *) * count);
3476
3477 if (strings == NULL) {
3478 PyErr_SetString(PyExc_MemoryError, "");
3479 return -1;
3480 }
3481
3482 memset(strings, 0 , sizeof(xmlChar *) * count);
3483
3484 {
3485 int idx;
3486 for (idx=0; idx < count; ++idx) {
3487 char* s = PyString_AsString
3488 (is_tuple
3489 ? PyTuple_GET_ITEM(py_strings, idx)
3490 : PyList_GET_ITEM(py_strings, idx));
3491 if (s)
3492 strings[init_index++] = (xmlChar *)s;
3493 else {
3494 xmlFree(strings);
3495 PyErr_SetString(PyExc_TypeError,
3496 "must be a tuple or list of strings.");
3497 return -1;
3498 }
3499 }
3500 }
3501
3502 *result = strings;
3503 return 0;
3504}
3505
3506static PyObject *
3507libxml_C14NDocDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
3508 PyObject * args)
3509{
3510 PyObject *py_retval = NULL;
3511
3512 PyObject *pyobj_doc;
3513 PyObject *pyobj_nodes;
3514 int exclusive;
3515 PyObject *pyobj_prefixes;
3516 int with_comments;
3517
3518 xmlDocPtr doc;
3519 xmlNodeSetPtr nodes;
3520 xmlChar **prefixes = NULL;
3521 xmlChar *doc_txt;
3522
3523 int result;
3524
3525 if (!PyArg_ParseTuple(args, (char *) "OOiOi:C14NDocDumpMemory",
3526 &pyobj_doc,
3527 &pyobj_nodes,
3528 &exclusive,
3529 &pyobj_prefixes,
3530 &with_comments))
3531 return (NULL);
3532
3533 doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
3534 if (!doc) {
3535 PyErr_SetString(PyExc_TypeError, "bad document.");
3536 return NULL;
3537 }
3538
3539 result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes);
3540 if (result < 0) return NULL;
3541
3542 if (exclusive) {
3543 result = PystringSet_Convert(pyobj_prefixes, &prefixes);
3544 if (result < 0) {
3545 if (nodes) {
3546 xmlFree(nodes->nodeTab);
3547 xmlFree(nodes);
3548 }
3549 return NULL;
3550 }
3551 }
3552
3553 result = xmlC14NDocDumpMemory(doc,
3554 nodes,
3555 exclusive,
3556 prefixes,
3557 with_comments,
3558 &doc_txt);
3559
3560 if (nodes) {
3561 xmlFree(nodes->nodeTab);
3562 xmlFree(nodes);
3563 }
3564 if (prefixes) {
3565 xmlChar ** idx = prefixes;
3566 while (*idx) xmlFree(*(idx++));
3567 xmlFree(prefixes);
3568 }
3569
3570 if (result < 0) {
3571 PyErr_SetString(PyExc_Exception,
3572 "libxml2 xmlC14NDocDumpMemory failure.");
3573 return NULL;
3574 }
3575 else {
3576 py_retval = PyString_FromStringAndSize((const char *) doc_txt,
3577 result);
3578 xmlFree(doc_txt);
3579 return py_retval;
3580 }
3581}
3582
3583static PyObject *
3584libxml_C14NDocSaveTo(ATTRIBUTE_UNUSED PyObject * self,
3585 PyObject * args)
3586{
3587 PyObject *pyobj_doc;
3588 PyObject *py_file;
3589 PyObject *pyobj_nodes;
3590 int exclusive;
3591 PyObject *pyobj_prefixes;
3592 int with_comments;
3593
3594 xmlDocPtr doc;
3595 xmlNodeSetPtr nodes;
3596 xmlChar **prefixes = NULL;
3597 FILE * output;
3598 xmlOutputBufferPtr buf;
3599
3600 int result;
3601 int len;
3602
3603 if (!PyArg_ParseTuple(args, (char *) "OOiOiO:C14NDocSaveTo",
3604 &pyobj_doc,
3605 &pyobj_nodes,
3606 &exclusive,
3607 &pyobj_prefixes,
3608 &with_comments,
3609 &py_file))
3610 return (NULL);
3611
3612 doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
3613 if (!doc) {
3614 PyErr_SetString(PyExc_TypeError, "bad document.");
3615 return NULL;
3616 }
3617
3618 if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
3619 PyErr_SetString(PyExc_TypeError, "bad file.");
3620 return NULL;
3621 }
3622 output = PyFile_AsFile(py_file);
3623 if (output == NULL) {
3624 PyErr_SetString(PyExc_TypeError, "bad file.");
3625 return NULL;
3626 }
3627 buf = xmlOutputBufferCreateFile(output, NULL);
3628
3629 result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes);
3630 if (result < 0) return NULL;
3631
3632 if (exclusive) {
3633 result = PystringSet_Convert(pyobj_prefixes, &prefixes);
3634 if (result < 0) {
3635 if (nodes) {
3636 xmlFree(nodes->nodeTab);
3637 xmlFree(nodes);
3638 }
3639 return NULL;
3640 }
3641 }
3642
3643 result = xmlC14NDocSaveTo(doc,
3644 nodes,
3645 exclusive,
3646 prefixes,
3647 with_comments,
3648 buf);
3649
3650 if (nodes) {
3651 xmlFree(nodes->nodeTab);
3652 xmlFree(nodes);
3653 }
3654 if (prefixes) {
3655 xmlChar ** idx = prefixes;
3656 while (*idx) xmlFree(*(idx++));
3657 xmlFree(prefixes);
3658 }
3659
3660 len = xmlOutputBufferClose(buf);
3661
3662 if (result < 0) {
3663 PyErr_SetString(PyExc_Exception,
3664 "libxml2 xmlC14NDocSaveTo failure.");
3665 return NULL;
3666 }
3667 else
3668 return PyInt_FromLong((long) len);
3669}
3670
3671#endif
3672#endif
3673
William M. Brackc68d78d2004-07-16 10:39:30 +00003674static PyObject *
3675libxml_getObjDesc(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003676
William M. Brackc68d78d2004-07-16 10:39:30 +00003677 PyObject *obj;
3678 char *str;
3679
3680 if (!PyArg_ParseTuple(args, (char *)"O:getObjDesc", &obj))
3681 return NULL;
3682 str = PyCObject_GetDesc(obj);
3683 return Py_BuildValue((char *)"s", str);
3684}
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003685
William M. Brack40cca612006-06-26 18:25:40 +00003686static PyObject *
3687libxml_compareNodesEqual(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
3688
3689 PyObject *py_node1, *py_node2;
3690 xmlNodePtr node1, node2;
3691
3692 if (!PyArg_ParseTuple(args, (char *)"OO:compareNodesEqual",
3693 &py_node1, &py_node2))
3694 return NULL;
3695 /* To compare two node objects, we compare their pointer addresses */
3696 node1 = PyxmlNode_Get(py_node1);
3697 node2 = PyxmlNode_Get(py_node2);
3698 if ( node1 == node2 )
3699 return Py_BuildValue((char *)"i", 1);
3700 else
3701 return Py_BuildValue((char *)"i", 0);
3702
3703}
3704
3705static PyObject *
3706libxml_nodeHash(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
3707
3708 PyObject *py_node1;
3709 xmlNodePtr node1;
3710
3711 if (!PyArg_ParseTuple(args, (char *)"O:nodeHash", &py_node1))
3712 return NULL;
3713 /* For simplicity, we use the node pointer address as a hash value */
3714 node1 = PyxmlNode_Get(py_node1);
3715
3716 return PyLong_FromVoidPtr(node1);
3717
3718}
3719
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00003720/************************************************************************
3721 * *
Daniel Veillardd2897fd2002-01-30 16:37:32 +00003722 * The registration stuff *
3723 * *
3724 ************************************************************************/
3725static PyMethodDef libxmlMethods[] = {
Daniel Veillard96fe0952002-01-30 20:52:23 +00003726#include "libxml2-export.c"
Daniel Veillardd2379012002-03-15 22:24:56 +00003727 {(char *) "name", libxml_name, METH_VARARGS, NULL},
3728 {(char *) "children", libxml_children, METH_VARARGS, NULL},
3729 {(char *) "properties", libxml_properties, METH_VARARGS, NULL},
3730 {(char *) "last", libxml_last, METH_VARARGS, NULL},
3731 {(char *) "prev", libxml_prev, METH_VARARGS, NULL},
3732 {(char *) "next", libxml_next, METH_VARARGS, NULL},
3733 {(char *) "parent", libxml_parent, METH_VARARGS, NULL},
3734 {(char *) "type", libxml_type, METH_VARARGS, NULL},
3735 {(char *) "doc", libxml_doc, METH_VARARGS, NULL},
3736 {(char *) "xmlNewNode", libxml_xmlNewNode, METH_VARARGS, NULL},
Daniel Veillardf9cf6f52005-04-12 01:02:29 +00003737 {(char *) "xmlNodeRemoveNsDef", libxml_xmlNodeRemoveNsDef, METH_VARARGS, NULL},
Daniel Veillard850ce9b2004-11-10 11:55:47 +00003738 {(char *)"xmlSetValidErrors", libxml_xmlSetValidErrors, METH_VARARGS, NULL},
Daniel Veillard25c90c52005-03-02 10:47:41 +00003739 {(char *)"xmlFreeValidCtxt", libxml_xmlFreeValidCtxt, METH_VARARGS, NULL},
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003740#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00003741 {(char *) "serializeNode", libxml_serializeNode, METH_VARARGS, NULL},
3742 {(char *) "saveNodeTo", libxml_saveNodeTo, METH_VARARGS, NULL},
Daniel Veillardc6d4a932002-09-12 15:00:57 +00003743 {(char *) "outputBufferCreate", libxml_xmlCreateOutputBuffer, METH_VARARGS, NULL},
Daniel Veillard6cbd6c02003-12-04 12:31:49 +00003744 {(char *) "outputBufferGetPythonFile", libxml_outputBufferGetPythonFile, METH_VARARGS, NULL},
3745 {(char *) "xmlOutputBufferClose", libxml_xmlOutputBufferClose, METH_VARARGS, NULL},
3746 { (char *)"xmlOutputBufferFlush", libxml_xmlOutputBufferFlush, METH_VARARGS, NULL },
Daniel Veillard263ec862004-10-04 10:26:54 +00003747 { (char *)"xmlSaveFileTo", libxml_xmlSaveFileTo, METH_VARARGS, NULL },
3748 { (char *)"xmlSaveFormatFileTo", libxml_xmlSaveFormatFileTo, METH_VARARGS, NULL },
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003749#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardc6d4a932002-09-12 15:00:57 +00003750 {(char *) "inputBufferCreate", libxml_xmlCreateInputBuffer, METH_VARARGS, NULL},
3751 {(char *) "setEntityLoader", libxml_xmlSetEntityLoader, METH_VARARGS, NULL},
Daniel Veillard3e20a292003-01-10 13:14:40 +00003752 {(char *)"xmlRegisterErrorHandler", libxml_xmlRegisterErrorHandler, METH_VARARGS, NULL },
Daniel Veillard417be3a2003-01-20 21:26:34 +00003753 {(char *)"xmlParserCtxtSetErrorHandler", libxml_xmlParserCtxtSetErrorHandler, METH_VARARGS, NULL },
3754 {(char *)"xmlParserCtxtGetErrorHandler", libxml_xmlParserCtxtGetErrorHandler, METH_VARARGS, NULL },
Daniel Veillarde6227e02003-01-14 11:42:39 +00003755 {(char *)"xmlFreeParserCtxt", libxml_xmlFreeParserCtxt, METH_VARARGS, NULL },
Daniel Veillard438ebbd2008-05-12 12:58:46 +00003756#ifdef LIBXML_READER_ENABLED
Daniel Veillard26f70262003-01-16 22:45:08 +00003757 {(char *)"xmlTextReaderSetErrorHandler", libxml_xmlTextReaderSetErrorHandler, METH_VARARGS, NULL },
3758 {(char *)"xmlTextReaderGetErrorHandler", libxml_xmlTextReaderGetErrorHandler, METH_VARARGS, NULL },
3759 {(char *)"xmlFreeTextReader", libxml_xmlFreeTextReader, METH_VARARGS, NULL },
Daniel Veillard438ebbd2008-05-12 12:58:46 +00003760#endif
Daniel Veillard54396242003-04-23 07:36:50 +00003761 {(char *)"addLocalCatalog", libxml_addLocalCatalog, METH_VARARGS, NULL },
Daniel Veillardc2664642003-07-29 20:44:53 +00003762#ifdef LIBXML_SCHEMAS_ENABLED
3763 {(char *)"xmlRelaxNGSetValidErrors", libxml_xmlRelaxNGSetValidErrors, METH_VARARGS, NULL},
3764 {(char *)"xmlRelaxNGFreeValidCtxt", libxml_xmlRelaxNGFreeValidCtxt, METH_VARARGS, NULL},
Daniel Veillardeff45a92004-10-29 12:10:55 +00003765 {(char *)"xmlSchemaSetValidErrors", libxml_xmlSchemaSetValidErrors, METH_VARARGS, NULL},
Daniel Veillardbb8502c2005-03-30 07:40:35 +00003766 {(char *)"xmlSchemaFreeValidCtxt", libxml_xmlSchemaFreeValidCtxt, METH_VARARGS, NULL},
Daniel Veillardc2664642003-07-29 20:44:53 +00003767#endif
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003768#ifdef LIBXML_C14N_ENABLED
3769#ifdef LIBXML_OUTPUT_ENABLED
3770 {(char *)"xmlC14NDocDumpMemory", libxml_C14NDocDumpMemory, METH_VARARGS, NULL},
3771 {(char *)"xmlC14NDocSaveTo", libxml_C14NDocSaveTo, METH_VARARGS, NULL},
3772#endif
3773#endif
William M. Brackc68d78d2004-07-16 10:39:30 +00003774 {(char *) "getObjDesc", libxml_getObjDesc, METH_VARARGS, NULL},
William M. Brack40cca612006-06-26 18:25:40 +00003775 {(char *) "compareNodesEqual", libxml_compareNodesEqual, METH_VARARGS, NULL},
3776 {(char *) "nodeHash", libxml_nodeHash, METH_VARARGS, NULL},
Daniel Veillardd2379012002-03-15 22:24:56 +00003777 {NULL, NULL, 0, NULL}
Daniel Veillardd2897fd2002-01-30 16:37:32 +00003778};
3779
Daniel Veillard0fea6f42002-02-22 22:51:13 +00003780#ifdef MERGED_MODULES
Daniel Veillard6361da02002-02-23 10:10:33 +00003781extern void initlibxsltmod(void);
Daniel Veillard0fea6f42002-02-22 22:51:13 +00003782#endif
3783
Daniel Veillardd2379012002-03-15 22:24:56 +00003784void
3785initlibxml2mod(void)
3786{
Daniel Veillardaf43f632002-03-08 15:05:20 +00003787 static int initialized = 0;
Daniel Veillardaf43f632002-03-08 15:05:20 +00003788
3789 if (initialized != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00003790 return;
William M. Brack8e2cc6f2004-07-03 23:28:52 +00003791
Daniel Veillardf93a8662004-07-01 12:56:30 +00003792 /* intialize the python extension module */
3793 Py_InitModule((char *) "libxml2mod", libxmlMethods);
3794
3795 /* initialize libxml2 */
3796 xmlInitParser();
Daniel Veillard5d819032002-02-02 21:49:17 +00003797 libxml_xmlErrorInitialize();
Daniel Veillard6361da02002-02-23 10:10:33 +00003798
Daniel Veillardf93a8662004-07-01 12:56:30 +00003799 initialized = 1;
3800
Daniel Veillard0fea6f42002-02-22 22:51:13 +00003801#ifdef MERGED_MODULES
3802 initlibxsltmod();
3803#endif
Daniel Veillardd2897fd2002-01-30 16:37:32 +00003804}