blob: 8ed74bbfe90937820acae77bf61a79da809c04a6 [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 Veillardd2897fd2002-01-30 16:37:32 +000026#include "libxml_wrap.h"
Daniel Veillard96fe0952002-01-30 20:52:23 +000027#include "libxml2-py.h"
Daniel Veillardd2897fd2002-01-30 16:37:32 +000028
Daniel Veillard0d132cf2002-12-23 14:43:32 +000029#if (defined(_MSC_VER) || defined(__MINGW32__)) && !defined(vsnprintf)
30#define vsnprintf(b,c,f,a) _vsnprintf(b,c,f,a)
Daniel Veillarde4fa2932003-03-26 00:38:10 +000031#elif defined(WITH_TRIO)
32#include "trio.h"
33#define vsnprintf trio_vsnprintf
Daniel Veillard0d132cf2002-12-23 14:43:32 +000034#endif
35
Daniel Veillardd2897fd2002-01-30 16:37:32 +000036/* #define DEBUG */
Daniel Veillard797a5652002-02-12 13:46:21 +000037/* #define DEBUG_SAX */
Daniel Veillarda7340c82002-02-01 17:56:45 +000038/* #define DEBUG_XPATH */
Daniel Veillard5d819032002-02-02 21:49:17 +000039/* #define DEBUG_ERROR */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000040/* #define DEBUG_MEMORY */
Daniel Veillardc6d4a932002-09-12 15:00:57 +000041/* #define DEBUG_FILES */
42/* #define DEBUG_LOADER */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000043
Daniel Veillardd2379012002-03-15 22:24:56 +000044void initlibxml2mod(void);
45
Daniel Veillardc6d4a932002-09-12 15:00:57 +000046/**
47 * TODO:
48 *
49 * macro to flag unimplemented blocks
50 */
51#define TODO \
52 xmlGenericError(xmlGenericErrorContext, \
53 "Unimplemented block at %s:%d\n", \
54 __FILE__, __LINE__);
William M. Brack8e2cc6f2004-07-03 23:28:52 +000055/*
56 * the following vars are used for XPath extensions, but
57 * are also referenced within the parser cleanup routine.
58 */
59static int libxml_xpathCallbacksInitialized = 0;
60
61typedef struct libxml_xpathCallback {
62 xmlXPathContextPtr ctx;
63 xmlChar *name;
64 xmlChar *ns_uri;
65 PyObject *function;
66} libxml_xpathCallback, *libxml_xpathCallbackPtr;
67typedef libxml_xpathCallback libxml_xpathCallbackArray[];
68static int libxml_xpathCallbacksAllocd = 10;
69static libxml_xpathCallbackArray *libxml_xpathCallbacks = NULL;
70static int libxml_xpathCallbacksNb = 0;
Daniel Veillardc6d4a932002-09-12 15:00:57 +000071
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000072/************************************************************************
73 * *
74 * Memory debug interface *
75 * *
76 ************************************************************************/
77
Daniel Veillardc2664642003-07-29 20:44:53 +000078#if 0
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000079extern void xmlMemFree(void *ptr);
80extern void *xmlMemMalloc(size_t size);
Daniel Veillardd2379012002-03-15 22:24:56 +000081extern void *xmlMemRealloc(void *ptr, size_t size);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000082extern char *xmlMemoryStrdup(const char *str);
Daniel Veillardc2664642003-07-29 20:44:53 +000083#endif
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000084
85static int libxmlMemoryDebugActivated = 0;
86static long libxmlMemoryAllocatedBase = 0;
87
88static int libxmlMemoryDebug = 0;
89static xmlFreeFunc freeFunc = NULL;
90static xmlMallocFunc mallocFunc = NULL;
91static xmlReallocFunc reallocFunc = NULL;
92static xmlStrdupFunc strdupFunc = NULL;
93
Daniel Veillardf93a8662004-07-01 12:56:30 +000094static void
95libxml_xmlErrorInitialize(void); /* forward declare */
96
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000097PyObject *
William M. Brackc68d78d2004-07-16 10:39:30 +000098libxml_xmlMemoryUsed(PyObject * self ATTRIBUTE_UNUSED,
99 PyObject * args ATTRIBUTE_UNUSED)
Daniel Veillard529233c2004-07-02 12:23:44 +0000100{
101 long ret;
102 PyObject *py_retval;
103
104 ret = xmlMemUsed();
105
106 py_retval = libxml_longWrap(ret);
107 return (py_retval);
108}
109
110PyObject *
William M. Brackc68d78d2004-07-16 10:39:30 +0000111libxml_xmlDebugMemory(PyObject * self ATTRIBUTE_UNUSED, PyObject * args)
Daniel Veillardd2379012002-03-15 22:24:56 +0000112{
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000113 int activate;
114 PyObject *py_retval;
115 long ret;
116
Daniel Veillardd2379012002-03-15 22:24:56 +0000117 if (!PyArg_ParseTuple(args, (char *) "i:xmlDebugMemory", &activate))
118 return (NULL);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000119
120#ifdef DEBUG_MEMORY
121 printf("libxml_xmlDebugMemory(%d) called\n", activate);
122#endif
123
124 if (activate != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000125 if (libxmlMemoryDebug == 0) {
126 /*
127 * First initialize the library and grab the old memory handlers
128 * and switch the library to memory debugging
129 */
130 xmlMemGet((xmlFreeFunc *) & freeFunc,
131 (xmlMallocFunc *) & mallocFunc,
132 (xmlReallocFunc *) & reallocFunc,
133 (xmlStrdupFunc *) & strdupFunc);
134 if ((freeFunc == xmlMemFree) && (mallocFunc == xmlMemMalloc) &&
135 (reallocFunc == xmlMemRealloc) &&
136 (strdupFunc == xmlMemoryStrdup)) {
137 libxmlMemoryAllocatedBase = xmlMemUsed();
138 } else {
Daniel Veillardf93a8662004-07-01 12:56:30 +0000139 /*
140 * cleanup first, because some memory has been
141 * allocated with the non-debug malloc in xmlInitParser
142 * when the python module was imported
143 */
144 xmlCleanupParser();
Daniel Veillardd2379012002-03-15 22:24:56 +0000145 ret = (long) xmlMemSetup(xmlMemFree, xmlMemMalloc,
146 xmlMemRealloc, xmlMemoryStrdup);
147 if (ret < 0)
148 goto error;
149 libxmlMemoryAllocatedBase = xmlMemUsed();
Daniel Veillardf93a8662004-07-01 12:56:30 +0000150 /* reinitialize */
151 xmlInitParser();
152 libxml_xmlErrorInitialize();
Daniel Veillardd2379012002-03-15 22:24:56 +0000153 }
Daniel Veillardd2379012002-03-15 22:24:56 +0000154 ret = 0;
155 } else if (libxmlMemoryDebugActivated == 0) {
156 libxmlMemoryAllocatedBase = xmlMemUsed();
157 ret = 0;
158 } else {
159 ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
160 }
161 libxmlMemoryDebug = 1;
162 libxmlMemoryDebugActivated = 1;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000163 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +0000164 if (libxmlMemoryDebugActivated == 1)
165 ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
166 else
167 ret = 0;
168 libxmlMemoryDebugActivated = 0;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000169 }
Daniel Veillardd2379012002-03-15 22:24:56 +0000170 error:
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000171 py_retval = libxml_longWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +0000172 return (py_retval);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000173}
174
175PyObject *
Daniel Veillardf93a8662004-07-01 12:56:30 +0000176libxml_xmlPythonCleanupParser(PyObject *self ATTRIBUTE_UNUSED,
177 PyObject *args ATTRIBUTE_UNUSED) {
178
William M. Brack8e2cc6f2004-07-03 23:28:52 +0000179 int ix;
180 long freed = -1;
Daniel Veillardf93a8662004-07-01 12:56:30 +0000181
182 if (libxmlMemoryDebug) {
183 freed = xmlMemUsed();
184 }
185
186 xmlCleanupParser();
William M. Brack8e2cc6f2004-07-03 23:28:52 +0000187 /*
188 * Need to confirm whether we really want to do this (required for
189 * memcheck) in all cases...
190 */
191
192 if (libxml_xpathCallbacks != NULL) { /* if ext funcs declared */
193 for (ix=0; ix<libxml_xpathCallbacksNb; ix++) {
194 if ((*libxml_xpathCallbacks)[ix].name != NULL)
195 xmlFree((*libxml_xpathCallbacks)[ix].name);
196 if ((*libxml_xpathCallbacks)[ix].ns_uri != NULL)
197 xmlFree((*libxml_xpathCallbacks)[ix].ns_uri);
198 }
199 libxml_xpathCallbacksNb = 0;
200 xmlFree(libxml_xpathCallbacks);
201 libxml_xpathCallbacks = NULL;
202 }
Daniel Veillardf93a8662004-07-01 12:56:30 +0000203
204 if (libxmlMemoryDebug) {
205 freed -= xmlMemUsed();
206 libxmlMemoryAllocatedBase -= freed;
207 if (libxmlMemoryAllocatedBase < 0)
208 libxmlMemoryAllocatedBase = 0;
209 }
210
211 Py_INCREF(Py_None);
212 return(Py_None);
213}
214
215PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +0000216libxml_xmlDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
217 ATTRIBUTE_UNUSED PyObject * args)
218{
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000219
220 if (libxmlMemoryDebug != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +0000221 xmlMemoryDump();
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000222 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +0000223 return (Py_None);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000224}
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000225
226/************************************************************************
227 * *
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000228 * Handling Python FILE I/O at the C level *
229 * The raw I/O attack diectly the File objects, while the *
230 * other routines address the ioWrapper instance instead *
231 * *
232 ************************************************************************/
233
234/**
235 * xmlPythonFileCloseUnref:
236 * @context: the I/O context
237 *
238 * Close an I/O channel
239 */
240static int
241xmlPythonFileCloseRaw (void * context) {
242 PyObject *file, *ret;
243
244#ifdef DEBUG_FILES
245 printf("xmlPythonFileCloseUnref\n");
246#endif
247 file = (PyObject *) context;
248 if (file == NULL) return(-1);
Daniel Veillard118aed72002-09-24 14:13:13 +0000249 ret = PyEval_CallMethod(file, (char *) "close", (char *) "()");
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000250 if (ret != NULL) {
251 Py_DECREF(ret);
252 }
253 Py_DECREF(file);
254 return(0);
255}
256
257/**
258 * xmlPythonFileReadRaw:
259 * @context: the I/O context
260 * @buffer: where to drop data
261 * @len: number of bytes to write
262 *
263 * Read @len bytes to @buffer from the Python file in the I/O channel
264 *
265 * Returns the number of bytes read
266 */
267static int
268xmlPythonFileReadRaw (void * context, char * buffer, int len) {
269 PyObject *file;
270 PyObject *ret;
271 int lenread = -1;
272 char *data;
273
274#ifdef DEBUG_FILES
275 printf("xmlPythonFileReadRaw: %d\n", len);
276#endif
277 file = (PyObject *) context;
278 if (file == NULL) return(-1);
Daniel Veillard118aed72002-09-24 14:13:13 +0000279 ret = PyEval_CallMethod(file, (char *) "read", (char *) "(i)", len);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000280 if (ret == NULL) {
281 printf("xmlPythonFileReadRaw: result is NULL\n");
282 return(-1);
283 } else if (PyString_Check(ret)) {
284 lenread = PyString_Size(ret);
285 data = PyString_AsString(ret);
286 if (lenread > len)
287 memcpy(buffer, data, len);
288 else
289 memcpy(buffer, data, lenread);
290 Py_DECREF(ret);
291 } else {
292 printf("xmlPythonFileReadRaw: result is not a String\n");
293 Py_DECREF(ret);
294 }
295 return(lenread);
296}
297
298/**
299 * xmlPythonFileRead:
300 * @context: the I/O context
301 * @buffer: where to drop data
302 * @len: number of bytes to write
303 *
304 * Read @len bytes to @buffer from the I/O channel.
305 *
306 * Returns the number of bytes read
307 */
308static int
309xmlPythonFileRead (void * context, char * buffer, int len) {
310 PyObject *file;
311 PyObject *ret;
312 int lenread = -1;
313 char *data;
314
315#ifdef DEBUG_FILES
316 printf("xmlPythonFileRead: %d\n", len);
317#endif
318 file = (PyObject *) context;
319 if (file == NULL) return(-1);
Daniel Veillard118aed72002-09-24 14:13:13 +0000320 ret = PyEval_CallMethod(file, (char *) "io_read", (char *) "(i)", len);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000321 if (ret == NULL) {
322 printf("xmlPythonFileRead: result is NULL\n");
323 return(-1);
324 } else if (PyString_Check(ret)) {
325 lenread = PyString_Size(ret);
326 data = PyString_AsString(ret);
327 if (lenread > len)
328 memcpy(buffer, data, len);
329 else
330 memcpy(buffer, data, lenread);
331 Py_DECREF(ret);
332 } else {
333 printf("xmlPythonFileRead: result is not a String\n");
334 Py_DECREF(ret);
335 }
336 return(lenread);
337}
338
339/**
340 * xmlFileWrite:
341 * @context: the I/O context
342 * @buffer: where to drop data
343 * @len: number of bytes to write
344 *
345 * Write @len bytes from @buffer to the I/O channel.
346 *
347 * Returns the number of bytes written
348 */
349static int
350xmlPythonFileWrite (void * context, const char * buffer, int len) {
351 PyObject *file;
352 PyObject *string;
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000353 PyObject *ret = NULL;
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000354 int written = -1;
355
356#ifdef DEBUG_FILES
357 printf("xmlPythonFileWrite: %d\n", len);
358#endif
359 file = (PyObject *) context;
360 if (file == NULL) return(-1);
361 string = PyString_FromStringAndSize(buffer, len);
362 if (string == NULL) return(-1);
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000363 if (PyObject_HasAttrString(file, (char *) "io_write")) {
364 ret = PyEval_CallMethod(file, (char *) "io_write", (char *) "(O)",
365 string);
366 } else if (PyObject_HasAttrString(file, (char *) "write")) {
367 ret = PyEval_CallMethod(file, (char *) "write", (char *) "(O)",
368 string);
369 }
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000370 Py_DECREF(string);
371 if (ret == NULL) {
372 printf("xmlPythonFileWrite: result is NULL\n");
373 return(-1);
374 } else if (PyInt_Check(ret)) {
375 written = (int) PyInt_AsLong(ret);
376 Py_DECREF(ret);
377 } else if (ret == Py_None) {
378 written = len;
379 Py_DECREF(ret);
380 } else {
381 printf("xmlPythonFileWrite: result is not an Int nor None\n");
382 Py_DECREF(ret);
383 }
384 return(written);
385}
386
387/**
388 * xmlPythonFileClose:
389 * @context: the I/O context
390 *
391 * Close an I/O channel
392 */
393static int
394xmlPythonFileClose (void * context) {
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000395 PyObject *file, *ret = NULL;
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000396
397#ifdef DEBUG_FILES
398 printf("xmlPythonFileClose\n");
399#endif
400 file = (PyObject *) context;
401 if (file == NULL) return(-1);
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000402 if (PyObject_HasAttrString(file, (char *) "io_close")) {
403 ret = PyEval_CallMethod(file, (char *) "io_close", (char *) "()");
404 } else if (PyObject_HasAttrString(file, (char *) "flush")) {
405 ret = PyEval_CallMethod(file, (char *) "flush", (char *) "()");
406 }
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000407 if (ret != NULL) {
408 Py_DECREF(ret);
409 }
410 return(0);
411}
412
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000413#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000414/**
415 * xmlOutputBufferCreatePythonFile:
416 * @file: a PyFile_Type
417 * @encoder: the encoding converter or NULL
418 *
419 * Create a buffered output for the progressive saving to a PyFile_Type
420 * buffered C I/O
421 *
422 * Returns the new parser output or NULL
423 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000424static xmlOutputBufferPtr
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000425xmlOutputBufferCreatePythonFile(PyObject *file,
426 xmlCharEncodingHandlerPtr encoder) {
427 xmlOutputBufferPtr ret;
428
429 if (file == NULL) return(NULL);
430
431 ret = xmlAllocOutputBuffer(encoder);
432 if (ret != NULL) {
433 ret->context = file;
434 /* Py_INCREF(file); */
435 ret->writecallback = xmlPythonFileWrite;
436 ret->closecallback = xmlPythonFileClose;
437 }
438
439 return(ret);
440}
441
442PyObject *
443libxml_xmlCreateOutputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
444 PyObject *py_retval;
445 PyObject *file;
446 xmlChar *encoding;
447 xmlCharEncodingHandlerPtr handler = NULL;
448 xmlOutputBufferPtr buffer;
449
450
451 if (!PyArg_ParseTuple(args, (char *)"Oz:xmlOutputBufferCreate",
452 &file, &encoding))
453 return(NULL);
454 if ((encoding != NULL) && (encoding[0] != 0)) {
Daniel Veillard118aed72002-09-24 14:13:13 +0000455 handler = xmlFindCharEncodingHandler((const char *) encoding);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000456 }
457 buffer = xmlOutputBufferCreatePythonFile(file, handler);
458 if (buffer == NULL)
459 printf("libxml_xmlCreateOutputBuffer: buffer == NULL\n");
460 py_retval = libxml_xmlOutputBufferPtrWrap(buffer);
461 return(py_retval);
462}
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000463
464/**
465 * libxml_outputBufferGetPythonFile:
466 * @buffer: the I/O buffer
467 *
468 * read the Python I/O from the CObject
469 *
470 * Returns the new parser output or NULL
471 */
472static PyObject *
473libxml_outputBufferGetPythonFile(ATTRIBUTE_UNUSED PyObject *self,
474 PyObject *args) {
475 PyObject *buffer;
476 PyObject *file;
477 xmlOutputBufferPtr obj;
478
479 if (!PyArg_ParseTuple(args, (char *)"O:outputBufferGetPythonFile",
480 &buffer))
481 return(NULL);
482
483 obj = PyoutputBuffer_Get(buffer);
484 if (obj == NULL) {
485 fprintf(stderr,
486 "outputBufferGetPythonFile: obj == NULL\n");
487 Py_INCREF(Py_None);
488 return(Py_None);
489 }
490 if (obj->closecallback != xmlPythonFileClose) {
491 fprintf(stderr,
492 "outputBufferGetPythonFile: not a python file wrapper\n");
493 Py_INCREF(Py_None);
494 return(Py_None);
495 }
496 file = (PyObject *) obj->context;
497 if (file == NULL) {
498 Py_INCREF(Py_None);
499 return(Py_None);
500 }
501 Py_INCREF(file);
502 return(file);
503}
504
Daniel Veillardd5e198a2004-03-09 09:03:28 +0000505static PyObject *
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000506libxml_xmlOutputBufferClose(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
507 PyObject *py_retval;
508 int c_retval;
509 xmlOutputBufferPtr out;
510 PyObject *pyobj_out;
511
512 if (!PyArg_ParseTuple(args, (char *)"O:xmlOutputBufferClose", &pyobj_out))
513 return(NULL);
514 out = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_out);
Daniel Veillard263ec862004-10-04 10:26:54 +0000515 /* Buffer may already have been destroyed elsewhere. This is harmless. */
516 if (out == NULL) {
517 Py_INCREF(Py_None);
518 return(Py_None);
519 }
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000520
521 c_retval = xmlOutputBufferClose(out);
522 py_retval = libxml_intWrap((int) c_retval);
523 return(py_retval);
524}
525
Daniel Veillardd5e198a2004-03-09 09:03:28 +0000526static PyObject *
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000527libxml_xmlOutputBufferFlush(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
528 PyObject *py_retval;
529 int c_retval;
530 xmlOutputBufferPtr out;
531 PyObject *pyobj_out;
532
533 if (!PyArg_ParseTuple(args, (char *)"O:xmlOutputBufferFlush", &pyobj_out))
534 return(NULL);
535 out = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_out);
536
537 c_retval = xmlOutputBufferFlush(out);
538 py_retval = libxml_intWrap((int) c_retval);
539 return(py_retval);
540}
Daniel Veillard263ec862004-10-04 10:26:54 +0000541
542static PyObject *
543libxml_xmlSaveFileTo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
544 PyObject *py_retval;
545 int c_retval;
546 xmlOutputBufferPtr buf;
547 PyObject *pyobj_buf;
548 xmlDocPtr cur;
549 PyObject *pyobj_cur;
550 char * encoding;
551
552 if (!PyArg_ParseTuple(args, (char *)"OOz:xmlSaveFileTo", &pyobj_buf, &pyobj_cur, &encoding))
553 return(NULL);
554 buf = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_buf);
555 cur = (xmlDocPtr) PyxmlNode_Get(pyobj_cur);
556
557 c_retval = xmlSaveFileTo(buf, cur, encoding);
558 /* xmlSaveTo() freed the memory pointed to by buf, so record that in the
559 * Python object. */
560 ((PyoutputBuffer_Object *)(pyobj_buf))->obj = NULL;
561 py_retval = libxml_intWrap((int) c_retval);
562 return(py_retval);
563}
564
565static PyObject *
566libxml_xmlSaveFormatFileTo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
567 PyObject *py_retval;
568 int c_retval;
569 xmlOutputBufferPtr buf;
570 PyObject *pyobj_buf;
571 xmlDocPtr cur;
572 PyObject *pyobj_cur;
573 char * encoding;
574 int format;
575
576 if (!PyArg_ParseTuple(args, (char *)"OOzi:xmlSaveFormatFileTo", &pyobj_buf, &pyobj_cur, &encoding, &format))
577 return(NULL);
578 buf = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_buf);
579 cur = (xmlDocPtr) PyxmlNode_Get(pyobj_cur);
580
581 c_retval = xmlSaveFormatFileTo(buf, cur, encoding, format);
582 /* xmlSaveFormatFileTo() freed the memory pointed to by buf, so record that
583 * in the Python object */
584 ((PyoutputBuffer_Object *)(pyobj_buf))->obj = NULL;
585 py_retval = libxml_intWrap((int) c_retval);
586 return(py_retval);
587}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000588#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000589
590
591/**
592 * xmlParserInputBufferCreatePythonFile:
593 * @file: a PyFile_Type
594 * @encoder: the encoding converter or NULL
595 *
596 * Create a buffered output for the progressive saving to a PyFile_Type
597 * buffered C I/O
598 *
599 * Returns the new parser output or NULL
600 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000601static xmlParserInputBufferPtr
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000602xmlParserInputBufferCreatePythonFile(PyObject *file,
603 xmlCharEncoding encoding) {
604 xmlParserInputBufferPtr ret;
605
606 if (file == NULL) return(NULL);
607
608 ret = xmlAllocParserInputBuffer(encoding);
609 if (ret != NULL) {
610 ret->context = file;
611 /* Py_INCREF(file); */
612 ret->readcallback = xmlPythonFileRead;
613 ret->closecallback = xmlPythonFileClose;
614 }
615
616 return(ret);
617}
618
619PyObject *
620libxml_xmlCreateInputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
621 PyObject *py_retval;
622 PyObject *file;
623 xmlChar *encoding;
624 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
625 xmlParserInputBufferPtr buffer;
626
627
628 if (!PyArg_ParseTuple(args, (char *)"Oz:xmlParserInputBufferCreate",
629 &file, &encoding))
630 return(NULL);
631 if ((encoding != NULL) && (encoding[0] != 0)) {
Daniel Veillard118aed72002-09-24 14:13:13 +0000632 enc = xmlParseCharEncoding((const char *) encoding);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000633 }
634 buffer = xmlParserInputBufferCreatePythonFile(file, enc);
635 if (buffer == NULL)
636 printf("libxml_xmlParserInputBufferCreate: buffer == NULL\n");
637 py_retval = libxml_xmlParserInputBufferPtrWrap(buffer);
638 return(py_retval);
639}
640
641/************************************************************************
642 * *
643 * Providing the resolver at the Python level *
644 * *
645 ************************************************************************/
646
647static xmlExternalEntityLoader defaultExternalEntityLoader = NULL;
648static PyObject *pythonExternalEntityLoaderObjext;
649
650static xmlParserInputPtr
651pythonExternalEntityLoader(const char *URL, const char *ID,
652 xmlParserCtxtPtr ctxt) {
653 xmlParserInputPtr result = NULL;
654 if (pythonExternalEntityLoaderObjext != NULL) {
655 PyObject *ret;
656 PyObject *ctxtobj;
657
658 ctxtobj = libxml_xmlParserCtxtPtrWrap(ctxt);
659#ifdef DEBUG_LOADER
660 printf("pythonExternalEntityLoader: ready to call\n");
661#endif
662
663 ret = PyObject_CallFunction(pythonExternalEntityLoaderObjext,
Daniel Veillard118aed72002-09-24 14:13:13 +0000664 (char *) "(ssO)", URL, ID, ctxtobj);
Daniel Veillarde4a07e72003-01-14 14:40:25 +0000665 Py_XDECREF(ctxtobj);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000666#ifdef DEBUG_LOADER
667 printf("pythonExternalEntityLoader: result ");
Daniel Veillard007d51e2003-09-17 20:07:28 +0000668 PyObject_Print(ret, stderr, 0);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000669 printf("\n");
670#endif
671
672 if (ret != NULL) {
Daniel Veillard118aed72002-09-24 14:13:13 +0000673 if (PyObject_HasAttrString(ret, (char *) "read")) {
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000674 xmlParserInputBufferPtr buf;
675
676 buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
677 if (buf != NULL) {
678 buf->context = ret;
679 buf->readcallback = xmlPythonFileReadRaw;
680 buf->closecallback = xmlPythonFileCloseRaw;
681 result = xmlNewIOInputStream(ctxt, buf,
682 XML_CHAR_ENCODING_NONE);
683 }
Daniel Veillard4ea89f02005-07-29 10:12:45 +0000684#if 0
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000685 } else {
Daniel Veillard4ea89f02005-07-29 10:12:45 +0000686 if (URL != NULL)
687 printf("pythonExternalEntityLoader: can't read %s\n",
688 URL);
689#endif
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000690 }
691 if (result == NULL) {
692 Py_DECREF(ret);
Daniel Veillardc64b8e92003-02-24 11:47:13 +0000693 } else if (URL != NULL) {
William M. Brackc1939562003-08-05 15:52:22 +0000694 result->filename = (char *) xmlStrdup((const xmlChar *)URL);
Daniel Veillardc64b8e92003-02-24 11:47:13 +0000695 result->directory = xmlParserGetDirectory((const char *) URL);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000696 }
697 }
698 }
699 if ((result == NULL) && (defaultExternalEntityLoader != NULL)) {
700 result = defaultExternalEntityLoader(URL, ID, ctxt);
701 }
702 return(result);
703}
704
705PyObject *
706libxml_xmlSetEntityLoader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
707 PyObject *py_retval;
708 PyObject *loader;
709
710 if (!PyArg_ParseTuple(args, (char *)"O:libxml_xmlSetEntityLoader",
711 &loader))
712 return(NULL);
713
714#ifdef DEBUG_LOADER
715 printf("libxml_xmlSetEntityLoader\n");
716#endif
717 if (defaultExternalEntityLoader == NULL)
718 defaultExternalEntityLoader = xmlGetExternalEntityLoader();
719
720 pythonExternalEntityLoaderObjext = loader;
721 xmlSetExternalEntityLoader(pythonExternalEntityLoader);
722
723 py_retval = PyInt_FromLong(0);
724 return(py_retval);
725}
726
727
728/************************************************************************
729 * *
Daniel Veillard3ce52572002-02-03 15:08:05 +0000730 * Handling SAX/xmllib/sgmlop callback interfaces *
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000731 * *
732 ************************************************************************/
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000733
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000734static void
735pythonStartElement(void *user_data, const xmlChar * name,
736 const xmlChar ** attrs)
737{
738 int i;
739 PyObject *handler;
740 PyObject *dict;
741 PyObject *attrname;
742 PyObject *attrvalue;
Daniel Veillardd2379012002-03-15 22:24:56 +0000743 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000744 int type = 0;
745
Daniel Veillard797a5652002-02-12 13:46:21 +0000746#ifdef DEBUG_SAX
747 printf("pythonStartElement(%s) called\n", name);
748#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000749 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000750 if (PyObject_HasAttrString(handler, (char *) "startElement"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000751 type = 1;
Daniel Veillardd2379012002-03-15 22:24:56 +0000752 else if (PyObject_HasAttrString(handler, (char *) "start"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000753 type = 2;
754 if (type != 0) {
755 /*
756 * the xmllib interface always generate a dictionnary,
757 * possibly empty
758 */
759 if ((attrs == NULL) && (type == 1)) {
760 Py_XINCREF(Py_None);
761 dict = Py_None;
Daniel Veillardd2379012002-03-15 22:24:56 +0000762 } else if (attrs == NULL) {
763 dict = PyDict_New();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000764 } else {
765 dict = PyDict_New();
766 for (i = 0; attrs[i] != NULL; i++) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000767 attrname = PyString_FromString((char *) attrs[i]);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000768 i++;
769 if (attrs[i] != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000770 attrvalue = PyString_FromString((char *) attrs[i]);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000771 } else {
772 Py_XINCREF(Py_None);
773 attrvalue = Py_None;
774 }
775 PyDict_SetItem(dict, attrname, attrvalue);
776 }
777 }
778
779 if (type == 1)
Daniel Veillardd2379012002-03-15 22:24:56 +0000780 result = PyObject_CallMethod(handler, (char *) "startElement",
781 (char *) "sO", name, dict);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000782 else if (type == 2)
Daniel Veillardd2379012002-03-15 22:24:56 +0000783 result = PyObject_CallMethod(handler, (char *) "start",
784 (char *) "sO", name, dict);
785 if (PyErr_Occurred())
786 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000787 Py_XDECREF(dict);
788 Py_XDECREF(result);
789 }
790}
791
792static void
793pythonStartDocument(void *user_data)
794{
795 PyObject *handler;
796 PyObject *result;
797
Daniel Veillard797a5652002-02-12 13:46:21 +0000798#ifdef DEBUG_SAX
799 printf("pythonStartDocument() called\n");
800#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000801 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000802 if (PyObject_HasAttrString(handler, (char *) "startDocument")) {
803 result =
804 PyObject_CallMethod(handler, (char *) "startDocument", NULL);
805 if (PyErr_Occurred())
806 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000807 Py_XDECREF(result);
808 }
809}
810
811static void
812pythonEndDocument(void *user_data)
813{
814 PyObject *handler;
815 PyObject *result;
816
Daniel Veillard797a5652002-02-12 13:46:21 +0000817#ifdef DEBUG_SAX
818 printf("pythonEndDocument() called\n");
819#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000820 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000821 if (PyObject_HasAttrString(handler, (char *) "endDocument")) {
822 result =
823 PyObject_CallMethod(handler, (char *) "endDocument", NULL);
824 if (PyErr_Occurred())
825 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000826 Py_XDECREF(result);
827 }
828 /*
829 * The reference to the handler is released there
830 */
831 Py_XDECREF(handler);
832}
833
834static void
835pythonEndElement(void *user_data, const xmlChar * name)
836{
837 PyObject *handler;
838 PyObject *result;
839
Daniel Veillard797a5652002-02-12 13:46:21 +0000840#ifdef DEBUG_SAX
841 printf("pythonEndElement(%s) called\n", name);
842#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000843 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000844 if (PyObject_HasAttrString(handler, (char *) "endElement")) {
845 result = PyObject_CallMethod(handler, (char *) "endElement",
846 (char *) "s", name);
847 if (PyErr_Occurred())
848 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000849 Py_XDECREF(result);
Daniel Veillardd2379012002-03-15 22:24:56 +0000850 } else if (PyObject_HasAttrString(handler, (char *) "end")) {
851 result = PyObject_CallMethod(handler, (char *) "end",
852 (char *) "s", name);
853 if (PyErr_Occurred())
854 PyErr_Print();
Daniel Veillard797a5652002-02-12 13:46:21 +0000855 Py_XDECREF(result);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000856 }
857}
858
859static void
860pythonReference(void *user_data, const xmlChar * name)
861{
862 PyObject *handler;
863 PyObject *result;
864
Daniel Veillard797a5652002-02-12 13:46:21 +0000865#ifdef DEBUG_SAX
866 printf("pythonReference(%s) called\n", name);
867#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000868 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000869 if (PyObject_HasAttrString(handler, (char *) "reference")) {
870 result = PyObject_CallMethod(handler, (char *) "reference",
871 (char *) "s", name);
872 if (PyErr_Occurred())
873 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000874 Py_XDECREF(result);
875 }
876}
877
878static void
879pythonCharacters(void *user_data, const xmlChar * ch, int len)
880{
881 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +0000882 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000883 int type = 0;
884
Daniel Veillard797a5652002-02-12 13:46:21 +0000885#ifdef DEBUG_SAX
886 printf("pythonCharacters(%s, %d) called\n", ch, len);
887#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000888 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000889 if (PyObject_HasAttrString(handler, (char *) "characters"))
890 type = 1;
891 else if (PyObject_HasAttrString(handler, (char *) "data"))
892 type = 2;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000893 if (type != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000894 if (type == 1)
895 result = PyObject_CallMethod(handler, (char *) "characters",
896 (char *) "s#", ch, len);
897 else if (type == 2)
898 result = PyObject_CallMethod(handler, (char *) "data",
899 (char *) "s#", ch, len);
900 if (PyErr_Occurred())
901 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000902 Py_XDECREF(result);
903 }
904}
905
906static void
907pythonIgnorableWhitespace(void *user_data, const xmlChar * ch, int len)
908{
909 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +0000910 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000911 int type = 0;
912
Daniel Veillard797a5652002-02-12 13:46:21 +0000913#ifdef DEBUG_SAX
914 printf("pythonIgnorableWhitespace(%s, %d) called\n", ch, len);
915#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000916 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000917 if (PyObject_HasAttrString(handler, (char *) "ignorableWhitespace"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000918 type = 1;
Daniel Veillardd2379012002-03-15 22:24:56 +0000919 else if (PyObject_HasAttrString(handler, (char *) "data"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000920 type = 2;
921 if (type != 0) {
922 if (type == 1)
923 result =
Daniel Veillardd2379012002-03-15 22:24:56 +0000924 PyObject_CallMethod(handler,
925 (char *) "ignorableWhitespace",
926 (char *) "s#", ch, len);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000927 else if (type == 2)
Daniel Veillardd2379012002-03-15 22:24:56 +0000928 result =
929 PyObject_CallMethod(handler, (char *) "data",
930 (char *) "s#", ch, len);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000931 Py_XDECREF(result);
932 }
933}
934
935static void
936pythonProcessingInstruction(void *user_data,
937 const xmlChar * target, const xmlChar * data)
938{
939 PyObject *handler;
940 PyObject *result;
941
Daniel Veillard797a5652002-02-12 13:46:21 +0000942#ifdef DEBUG_SAX
943 printf("pythonProcessingInstruction(%s, %s) called\n", target, data);
944#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000945 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000946 if (PyObject_HasAttrString(handler, (char *) "processingInstruction")) {
947 result = PyObject_CallMethod(handler, (char *)
948 "processingInstruction",
949 (char *) "ss", target, data);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000950 Py_XDECREF(result);
951 }
952}
953
954static void
955pythonComment(void *user_data, const xmlChar * value)
956{
957 PyObject *handler;
958 PyObject *result;
959
Daniel Veillard797a5652002-02-12 13:46:21 +0000960#ifdef DEBUG_SAX
961 printf("pythonComment(%s) called\n", value);
962#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000963 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000964 if (PyObject_HasAttrString(handler, (char *) "comment")) {
965 result =
966 PyObject_CallMethod(handler, (char *) "comment", (char *) "s",
967 value);
968 if (PyErr_Occurred())
969 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000970 Py_XDECREF(result);
971 }
972}
973
974static void
975pythonWarning(void *user_data, const char *msg, ...)
976{
977 PyObject *handler;
978 PyObject *result;
979 va_list args;
980 char buf[1024];
981
Daniel Veillard797a5652002-02-12 13:46:21 +0000982#ifdef DEBUG_SAX
983 printf("pythonWarning(%s) called\n", msg);
984#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000985 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000986 if (PyObject_HasAttrString(handler, (char *) "warning")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000987 va_start(args, msg);
988 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +0000989 va_end(args);
990 buf[1023] = 0;
991 result =
992 PyObject_CallMethod(handler, (char *) "warning", (char *) "s",
993 buf);
994 if (PyErr_Occurred())
995 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000996 Py_XDECREF(result);
997 }
998}
999
1000static void
1001pythonError(void *user_data, const char *msg, ...)
1002{
1003 PyObject *handler;
1004 PyObject *result;
1005 va_list args;
1006 char buf[1024];
1007
Daniel Veillard797a5652002-02-12 13:46:21 +00001008#ifdef DEBUG_SAX
1009 printf("pythonError(%s) called\n", msg);
1010#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001011 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001012 if (PyObject_HasAttrString(handler, (char *) "error")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001013 va_start(args, msg);
1014 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +00001015 va_end(args);
1016 buf[1023] = 0;
1017 result =
1018 PyObject_CallMethod(handler, (char *) "error", (char *) "s",
1019 buf);
1020 if (PyErr_Occurred())
1021 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001022 Py_XDECREF(result);
1023 }
1024}
1025
1026static void
1027pythonFatalError(void *user_data, const char *msg, ...)
1028{
1029 PyObject *handler;
1030 PyObject *result;
1031 va_list args;
1032 char buf[1024];
1033
Daniel Veillard797a5652002-02-12 13:46:21 +00001034#ifdef DEBUG_SAX
1035 printf("pythonFatalError(%s) called\n", msg);
1036#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001037 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001038 if (PyObject_HasAttrString(handler, (char *) "fatalError")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001039 va_start(args, msg);
1040 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +00001041 va_end(args);
1042 buf[1023] = 0;
1043 result =
1044 PyObject_CallMethod(handler, (char *) "fatalError",
1045 (char *) "s", buf);
1046 if (PyErr_Occurred())
1047 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001048 Py_XDECREF(result);
1049 }
1050}
1051
1052static void
1053pythonCdataBlock(void *user_data, const xmlChar * ch, int len)
1054{
1055 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +00001056 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001057 int type = 0;
1058
Daniel Veillard797a5652002-02-12 13:46:21 +00001059#ifdef DEBUG_SAX
1060 printf("pythonCdataBlock(%s, %d) called\n", ch, len);
1061#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001062 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001063 if (PyObject_HasAttrString(handler, (char *) "cdataBlock"))
1064 type = 1;
1065 else if (PyObject_HasAttrString(handler, (char *) "cdata"))
1066 type = 2;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001067 if (type != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001068 if (type == 1)
1069 result =
1070 PyObject_CallMethod(handler, (char *) "cdataBlock",
1071 (char *) "s#", ch, len);
1072 else if (type == 2)
1073 result =
1074 PyObject_CallMethod(handler, (char *) "cdata",
1075 (char *) "s#", ch, len);
1076 if (PyErr_Occurred())
1077 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001078 Py_XDECREF(result);
1079 }
1080}
1081
1082static void
1083pythonExternalSubset(void *user_data,
1084 const xmlChar * name,
1085 const xmlChar * externalID, const xmlChar * systemID)
1086{
1087 PyObject *handler;
1088 PyObject *result;
1089
Daniel Veillard797a5652002-02-12 13:46:21 +00001090#ifdef DEBUG_SAX
1091 printf("pythonExternalSubset(%s, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001092 name, externalID, systemID);
Daniel Veillard797a5652002-02-12 13:46:21 +00001093#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001094 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001095 if (PyObject_HasAttrString(handler, (char *) "externalSubset")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001096 result =
Daniel Veillardd2379012002-03-15 22:24:56 +00001097 PyObject_CallMethod(handler, (char *) "externalSubset",
1098 (char *) "sss", name, externalID,
1099 systemID);
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001100 Py_XDECREF(result);
1101 }
1102}
1103
1104static void
1105pythonEntityDecl(void *user_data,
1106 const xmlChar * name,
1107 int type,
1108 const xmlChar * publicId,
1109 const xmlChar * systemId, xmlChar * content)
1110{
1111 PyObject *handler;
1112 PyObject *result;
1113
1114 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001115 if (PyObject_HasAttrString(handler, (char *) "entityDecl")) {
1116 result = PyObject_CallMethod(handler, (char *) "entityDecl",
1117 (char *) "sisss", name, type,
1118 publicId, systemId, content);
1119 if (PyErr_Occurred())
1120 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001121 Py_XDECREF(result);
1122 }
1123}
1124
1125
1126
1127static void
1128
1129pythonNotationDecl(void *user_data,
1130 const xmlChar * name,
1131 const xmlChar * publicId, const xmlChar * systemId)
1132{
1133 PyObject *handler;
1134 PyObject *result;
1135
1136 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001137 if (PyObject_HasAttrString(handler, (char *) "notationDecl")) {
1138 result = PyObject_CallMethod(handler, (char *) "notationDecl",
1139 (char *) "sss", name, publicId,
1140 systemId);
1141 if (PyErr_Occurred())
1142 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001143 Py_XDECREF(result);
1144 }
1145}
1146
1147static void
1148pythonAttributeDecl(void *user_data,
1149 const xmlChar * elem,
1150 const xmlChar * name,
1151 int type,
1152 int def,
Daniel Veillardd2379012002-03-15 22:24:56 +00001153 const xmlChar * defaultValue, xmlEnumerationPtr tree)
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001154{
1155 PyObject *handler;
1156 PyObject *nameList;
1157 PyObject *newName;
1158 xmlEnumerationPtr node;
1159 PyObject *result;
1160 int count;
1161
1162 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001163 if (PyObject_HasAttrString(handler, (char *) "attributeDecl")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001164 count = 0;
1165 for (node = tree; node != NULL; node = node->next) {
1166 count++;
1167 }
1168 nameList = PyList_New(count);
1169 count = 0;
1170 for (node = tree; node != NULL; node = node->next) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001171 newName = PyString_FromString((char *) node->name);
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001172 PyList_SetItem(nameList, count, newName);
1173 count++;
1174 }
Daniel Veillardd2379012002-03-15 22:24:56 +00001175 result = PyObject_CallMethod(handler, (char *) "attributeDecl",
1176 (char *) "ssiisO", elem, name, type,
1177 def, defaultValue, nameList);
1178 if (PyErr_Occurred())
1179 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001180 Py_XDECREF(nameList);
1181 Py_XDECREF(result);
1182 }
1183}
1184
1185static void
1186pythonElementDecl(void *user_data,
1187 const xmlChar * name,
Daniel Veillardd2379012002-03-15 22:24:56 +00001188 int type, ATTRIBUTE_UNUSED xmlElementContentPtr content)
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001189{
1190 PyObject *handler;
1191 PyObject *obj;
1192 PyObject *result;
1193
1194 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001195 if (PyObject_HasAttrString(handler, (char *) "elementDecl")) {
1196 /* TODO: wrap in an elementContent object */
1197 printf
1198 ("pythonElementDecl: xmlElementContentPtr wrapper missing !\n");
1199 obj = Py_None;
1200 /* Py_XINCREF(Py_None); isn't the reference just borrowed ??? */
1201 result = PyObject_CallMethod(handler, (char *) "elementDecl",
1202 (char *) "siO", name, type, obj);
1203 if (PyErr_Occurred())
1204 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001205 Py_XDECREF(result);
1206 }
1207}
1208
1209static void
1210pythonUnparsedEntityDecl(void *user_data,
1211 const xmlChar * name,
1212 const xmlChar * publicId,
1213 const xmlChar * systemId,
1214 const xmlChar * notationName)
1215{
1216 PyObject *handler;
1217 PyObject *result;
1218
1219 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001220 if (PyObject_HasAttrString(handler, (char *) "unparsedEntityDecl")) {
1221 result =
1222 PyObject_CallMethod(handler, (char *) "unparsedEntityDecl",
1223 (char *) "ssss", name, publicId, systemId,
1224 notationName);
1225 if (PyErr_Occurred())
1226 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001227 Py_XDECREF(result);
1228 }
1229}
1230
1231static void
1232pythonInternalSubset(void *user_data, const xmlChar * name,
1233 const xmlChar * ExternalID, const xmlChar * SystemID)
1234{
1235 PyObject *handler;
1236 PyObject *result;
1237
Daniel Veillard797a5652002-02-12 13:46:21 +00001238#ifdef DEBUG_SAX
1239 printf("pythonInternalSubset(%s, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001240 name, ExternalID, SystemID);
Daniel Veillard797a5652002-02-12 13:46:21 +00001241#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001242 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001243 if (PyObject_HasAttrString(handler, (char *) "internalSubset")) {
1244 result = PyObject_CallMethod(handler, (char *) "internalSubset",
1245 (char *) "sss", name, ExternalID,
1246 SystemID);
1247 if (PyErr_Occurred())
1248 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001249 Py_XDECREF(result);
1250 }
1251}
1252
1253static xmlSAXHandler pythonSaxHandler = {
1254 pythonInternalSubset,
Daniel Veillardd2379012002-03-15 22:24:56 +00001255 NULL, /* TODO pythonIsStandalone, */
1256 NULL, /* TODO pythonHasInternalSubset, */
1257 NULL, /* TODO pythonHasExternalSubset, */
1258 NULL, /* TODO pythonResolveEntity, */
1259 NULL, /* TODO pythonGetEntity, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001260 pythonEntityDecl,
1261 pythonNotationDecl,
1262 pythonAttributeDecl,
1263 pythonElementDecl,
1264 pythonUnparsedEntityDecl,
Daniel Veillardd2379012002-03-15 22:24:56 +00001265 NULL, /* OBSOLETED pythonSetDocumentLocator, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001266 pythonStartDocument,
1267 pythonEndDocument,
1268 pythonStartElement,
1269 pythonEndElement,
1270 pythonReference,
1271 pythonCharacters,
1272 pythonIgnorableWhitespace,
1273 pythonProcessingInstruction,
1274 pythonComment,
1275 pythonWarning,
1276 pythonError,
1277 pythonFatalError,
Daniel Veillardd2379012002-03-15 22:24:56 +00001278 NULL, /* TODO pythonGetParameterEntity, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001279 pythonCdataBlock,
1280 pythonExternalSubset,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001281 1,
1282 NULL, /* TODO mograte to SAX2 */
1283 NULL,
William M. Brack871611b2003-10-18 04:53:14 +00001284 NULL,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001285 NULL
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001286};
Daniel Veillard3ce52572002-02-03 15:08:05 +00001287
1288/************************************************************************
1289 * *
1290 * Handling of specific parser context *
1291 * *
1292 ************************************************************************/
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001293
1294PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001295libxml_xmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
1296 PyObject * args)
1297{
1298 const char *chunk;
Daniel Veillard3ce52572002-02-03 15:08:05 +00001299 int size;
Daniel Veillardd2379012002-03-15 22:24:56 +00001300 const char *URI;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001301 PyObject *pyobj_SAX = NULL;
Daniel Veillard3ce52572002-02-03 15:08:05 +00001302 xmlSAXHandlerPtr SAX = NULL;
Daniel Veillard3ce52572002-02-03 15:08:05 +00001303 xmlParserCtxtPtr ret;
1304 PyObject *pyret;
Daniel Veillard96fe0952002-01-30 20:52:23 +00001305
Daniel Veillardd2379012002-03-15 22:24:56 +00001306 if (!PyArg_ParseTuple
1307 (args, (char *) "Oziz:xmlCreatePushParser", &pyobj_SAX, &chunk,
1308 &size, &URI))
1309 return (NULL);
Daniel Veillard3ce52572002-02-03 15:08:05 +00001310
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001311#ifdef DEBUG
Daniel Veillard3ce52572002-02-03 15:08:05 +00001312 printf("libxml_xmlCreatePushParser(%p, %s, %d, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001313 pyobj_SAX, chunk, size, URI);
Daniel Veillard96fe0952002-01-30 20:52:23 +00001314#endif
Daniel Veillard3ce52572002-02-03 15:08:05 +00001315 if (pyobj_SAX != Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001316 SAX = &pythonSaxHandler;
1317 Py_INCREF(pyobj_SAX);
1318 /* The reference is released in pythonEndDocument() */
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001319 }
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001320 ret = xmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI);
Daniel Veillard3ce52572002-02-03 15:08:05 +00001321 pyret = libxml_xmlParserCtxtPtrWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +00001322 return (pyret);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001323}
Daniel Veillard5d819032002-02-02 21:49:17 +00001324
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001325PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001326libxml_htmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
1327 PyObject * args)
1328{
Daniel Veillard656ce942004-04-30 23:11:45 +00001329#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001330 const char *chunk;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001331 int size;
Daniel Veillardd2379012002-03-15 22:24:56 +00001332 const char *URI;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001333 PyObject *pyobj_SAX = NULL;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001334 xmlSAXHandlerPtr SAX = NULL;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001335 xmlParserCtxtPtr ret;
1336 PyObject *pyret;
1337
Daniel Veillardd2379012002-03-15 22:24:56 +00001338 if (!PyArg_ParseTuple
1339 (args, (char *) "Oziz:htmlCreatePushParser", &pyobj_SAX, &chunk,
1340 &size, &URI))
1341 return (NULL);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001342
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001343#ifdef DEBUG
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001344 printf("libxml_htmlCreatePushParser(%p, %s, %d, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001345 pyobj_SAX, chunk, size, URI);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001346#endif
1347 if (pyobj_SAX != Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001348 SAX = &pythonSaxHandler;
1349 Py_INCREF(pyobj_SAX);
1350 /* The reference is released in pythonEndDocument() */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001351 }
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001352 ret = htmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI,
Daniel Veillardd2379012002-03-15 22:24:56 +00001353 XML_CHAR_ENCODING_NONE);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001354 pyret = libxml_xmlParserCtxtPtrWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +00001355 return (pyret);
Daniel Veillard656ce942004-04-30 23:11:45 +00001356#else
1357 Py_INCREF(Py_None);
1358 return (Py_None);
1359#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001360}
1361
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001362PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001363libxml_xmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1364{
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001365 int recover;
Daniel Veillardd2379012002-03-15 22:24:56 +00001366 const char *URI;
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001367 PyObject *pyobj_SAX = NULL;
1368 xmlSAXHandlerPtr SAX = NULL;
1369
Daniel Veillardd2379012002-03-15 22:24:56 +00001370 if (!PyArg_ParseTuple(args, (char *) "Osi:xmlSAXParseFile", &pyobj_SAX,
1371 &URI, &recover))
1372 return (NULL);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001373
1374#ifdef DEBUG
1375 printf("libxml_xmlSAXParseFile(%p, %s, %d) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001376 pyobj_SAX, URI, recover);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001377#endif
1378 if (pyobj_SAX == Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001379 Py_INCREF(Py_None);
1380 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001381 }
1382 SAX = &pythonSaxHandler;
1383 Py_INCREF(pyobj_SAX);
1384 /* The reference is released in pythonEndDocument() */
Daniel Veillardf2531af2005-03-31 11:06:29 +00001385 xmlSAXUserParseFile(SAX, pyobj_SAX, URI);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001386 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +00001387 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001388}
1389
1390PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001391libxml_htmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1392{
Daniel Veillard656ce942004-04-30 23:11:45 +00001393#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001394 const char *URI;
1395 const char *encoding;
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001396 PyObject *pyobj_SAX = NULL;
1397 xmlSAXHandlerPtr SAX = NULL;
1398
Daniel Veillardd2379012002-03-15 22:24:56 +00001399 if (!PyArg_ParseTuple
1400 (args, (char *) "Osz:htmlSAXParseFile", &pyobj_SAX, &URI,
1401 &encoding))
1402 return (NULL);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001403
1404#ifdef DEBUG
1405 printf("libxml_htmlSAXParseFile(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001406 pyobj_SAX, URI, encoding);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001407#endif
1408 if (pyobj_SAX == Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001409 Py_INCREF(Py_None);
1410 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001411 }
1412 SAX = &pythonSaxHandler;
1413 Py_INCREF(pyobj_SAX);
1414 /* The reference is released in pythonEndDocument() */
1415 htmlSAXParseFile(URI, encoding, SAX, pyobj_SAX);
1416 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +00001417 return (Py_None);
Daniel Veillard656ce942004-04-30 23:11:45 +00001418#else
1419 Py_INCREF(Py_None);
1420 return (Py_None);
1421#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001422}
1423
Daniel Veillard5d819032002-02-02 21:49:17 +00001424/************************************************************************
1425 * *
1426 * Error message callback *
1427 * *
1428 ************************************************************************/
1429
1430static PyObject *libxml_xmlPythonErrorFuncHandler = NULL;
1431static PyObject *libxml_xmlPythonErrorFuncCtxt = NULL;
1432
Daniel Veillarde6227e02003-01-14 11:42:39 +00001433/* helper to build a xmlMalloc'ed string from a format and va_list */
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001434/*
1435 * disabled the loop, the repeated call to vsnprintf without reset of ap
1436 * in case the initial buffer was too small segfaulted on x86_64
1437 * we now directly vsnprintf on a large buffer.
1438 */
Daniel Veillarde6227e02003-01-14 11:42:39 +00001439static char *
1440libxml_buildMessage(const char *msg, va_list ap)
Daniel Veillardd2379012002-03-15 22:24:56 +00001441{
Daniel Veillardd2379012002-03-15 22:24:56 +00001442 int chars;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001443 char *str;
1444
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001445 str = (char *) xmlMalloc(1000);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001446 if (str == NULL)
1447 return NULL;
1448
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001449 chars = vsnprintf(str, 999, msg, ap);
1450 if (chars >= 998)
1451 str[999] = 0;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001452
1453 return str;
1454}
1455
1456static void
1457libxml_xmlErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, const char *msg,
1458 ...)
1459{
Daniel Veillardd2379012002-03-15 22:24:56 +00001460 va_list ap;
Daniel Veillard5d819032002-02-02 21:49:17 +00001461 PyObject *list;
1462 PyObject *message;
1463 PyObject *result;
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001464 char str[1000];
Daniel Veillard5d819032002-02-02 21:49:17 +00001465
1466#ifdef DEBUG_ERROR
1467 printf("libxml_xmlErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
1468#endif
1469
1470
1471 if (libxml_xmlPythonErrorFuncHandler == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001472 va_start(ap, msg);
Daniel Veillard007d51e2003-09-17 20:07:28 +00001473 vfprintf(stderr, msg, ap);
Daniel Veillardd2379012002-03-15 22:24:56 +00001474 va_end(ap);
Daniel Veillard5d819032002-02-02 21:49:17 +00001475 } else {
Daniel Veillarde6227e02003-01-14 11:42:39 +00001476 va_start(ap, msg);
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001477 if (vsnprintf(str, 999, msg, ap) >= 998)
1478 str[999] = 0;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001479 va_end(ap);
Daniel Veillard5d819032002-02-02 21:49:17 +00001480
Daniel Veillardd2379012002-03-15 22:24:56 +00001481 list = PyTuple_New(2);
1482 PyTuple_SetItem(list, 0, libxml_xmlPythonErrorFuncCtxt);
1483 Py_XINCREF(libxml_xmlPythonErrorFuncCtxt);
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001484 message = libxml_charPtrConstWrap(str);
Daniel Veillardd2379012002-03-15 22:24:56 +00001485 PyTuple_SetItem(list, 1, message);
1486 result = PyEval_CallObject(libxml_xmlPythonErrorFuncHandler, list);
1487 Py_XDECREF(list);
1488 Py_XDECREF(result);
Daniel Veillard5d819032002-02-02 21:49:17 +00001489 }
1490}
1491
1492static void
Daniel Veillardd2379012002-03-15 22:24:56 +00001493libxml_xmlErrorInitialize(void)
1494{
Daniel Veillard5d819032002-02-02 21:49:17 +00001495#ifdef DEBUG_ERROR
1496 printf("libxml_xmlErrorInitialize() called\n");
1497#endif
1498 xmlSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
Daniel Veillard781ac8b2003-05-15 22:11:36 +00001499 xmlThrDefSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
Daniel Veillard5d819032002-02-02 21:49:17 +00001500}
1501
Daniel Veillardc2664642003-07-29 20:44:53 +00001502static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001503libxml_xmlRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self,
1504 PyObject * args)
1505{
Daniel Veillard5d819032002-02-02 21:49:17 +00001506 PyObject *py_retval;
1507 PyObject *pyobj_f;
1508 PyObject *pyobj_ctx;
1509
Daniel Veillardd2379012002-03-15 22:24:56 +00001510 if (!PyArg_ParseTuple
1511 (args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f,
1512 &pyobj_ctx))
1513 return (NULL);
Daniel Veillard5d819032002-02-02 21:49:17 +00001514
1515#ifdef DEBUG_ERROR
William M. Brack8e2cc6f2004-07-03 23:28:52 +00001516 printf("libxml_xmlRegisterErrorHandler(%p, %p) called\n", pyobj_ctx,
Daniel Veillardd2379012002-03-15 22:24:56 +00001517 pyobj_f);
Daniel Veillard5d819032002-02-02 21:49:17 +00001518#endif
1519
1520 if (libxml_xmlPythonErrorFuncHandler != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001521 Py_XDECREF(libxml_xmlPythonErrorFuncHandler);
Daniel Veillard5d819032002-02-02 21:49:17 +00001522 }
1523 if (libxml_xmlPythonErrorFuncCtxt != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001524 Py_XDECREF(libxml_xmlPythonErrorFuncCtxt);
Daniel Veillard5d819032002-02-02 21:49:17 +00001525 }
1526
1527 Py_XINCREF(pyobj_ctx);
1528 Py_XINCREF(pyobj_f);
1529
1530 /* TODO: check f is a function ! */
1531 libxml_xmlPythonErrorFuncHandler = pyobj_f;
1532 libxml_xmlPythonErrorFuncCtxt = pyobj_ctx;
1533
1534 py_retval = libxml_intWrap(1);
Daniel Veillardd2379012002-03-15 22:24:56 +00001535 return (py_retval);
Daniel Veillard5d819032002-02-02 21:49:17 +00001536}
Daniel Veillardd2379012002-03-15 22:24:56 +00001537
Daniel Veillarde6227e02003-01-14 11:42:39 +00001538
1539/************************************************************************
1540 * *
1541 * Per parserCtxt error handler *
1542 * *
1543 ************************************************************************/
1544
Daniel Veillard417be3a2003-01-20 21:26:34 +00001545typedef struct
Daniel Veillarde6227e02003-01-14 11:42:39 +00001546{
Daniel Veillard417be3a2003-01-20 21:26:34 +00001547 PyObject *f;
1548 PyObject *arg;
1549} xmlParserCtxtPyCtxt;
1550typedef xmlParserCtxtPyCtxt *xmlParserCtxtPyCtxtPtr;
1551
1552static void
1553libxml_xmlParserCtxtGenericErrorFuncHandler(void *ctx, int severity, char *str)
1554{
Daniel Veillarde6227e02003-01-14 11:42:39 +00001555 PyObject *list;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001556 PyObject *result;
Daniel Veillard417be3a2003-01-20 21:26:34 +00001557 xmlParserCtxtPtr ctxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001558 xmlParserCtxtPyCtxtPtr pyCtxt;
1559
1560#ifdef DEBUG_ERROR
Daniel Veillard850ce9b2004-11-10 11:55:47 +00001561 printf("libxml_xmlParserCtxtGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001562#endif
1563
Daniel Veillard417be3a2003-01-20 21:26:34 +00001564 ctxt = (xmlParserCtxtPtr)ctx;
1565 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001566
Daniel Veillard417be3a2003-01-20 21:26:34 +00001567 list = PyTuple_New(4);
1568 PyTuple_SetItem(list, 0, pyCtxt->arg);
1569 Py_XINCREF(pyCtxt->arg);
1570 PyTuple_SetItem(list, 1, libxml_charPtrWrap(str));
1571 PyTuple_SetItem(list, 2, libxml_intWrap(severity));
1572 PyTuple_SetItem(list, 3, Py_None);
1573 Py_INCREF(Py_None);
1574 result = PyEval_CallObject(pyCtxt->f, list);
1575 if (result == NULL)
1576 {
1577 /* TODO: manage for the exception to be propagated... */
1578 PyErr_Print();
Daniel Veillarde6227e02003-01-14 11:42:39 +00001579 }
Daniel Veillard417be3a2003-01-20 21:26:34 +00001580 Py_XDECREF(list);
1581 Py_XDECREF(result);
1582}
1583
1584static void
1585libxml_xmlParserCtxtErrorFuncHandler(void *ctx, const char *msg, ...)
1586{
1587 va_list ap;
1588
1589 va_start(ap, msg);
1590 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_ERROR,libxml_buildMessage(msg,ap));
1591 va_end(ap);
1592}
1593
1594static void
1595libxml_xmlParserCtxtWarningFuncHandler(void *ctx, const char *msg, ...)
1596{
1597 va_list ap;
1598
1599 va_start(ap, msg);
1600 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_WARNING,libxml_buildMessage(msg,ap));
1601 va_end(ap);
1602}
1603
1604static void
1605libxml_xmlParserCtxtValidityErrorFuncHandler(void *ctx, const char *msg, ...)
1606{
1607 va_list ap;
1608
1609 va_start(ap, msg);
1610 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap));
1611 va_end(ap);
1612}
1613
1614static void
1615libxml_xmlParserCtxtValidityWarningFuncHandler(void *ctx, const char *msg, ...)
1616{
1617 va_list ap;
1618
1619 va_start(ap, msg);
1620 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap));
1621 va_end(ap);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001622}
1623
Daniel Veillardc2664642003-07-29 20:44:53 +00001624static PyObject *
Daniel Veillard417be3a2003-01-20 21:26:34 +00001625libxml_xmlParserCtxtSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
Daniel Veillarde6227e02003-01-14 11:42:39 +00001626{
1627 PyObject *py_retval;
1628 xmlParserCtxtPtr ctxt;
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001629 xmlParserCtxtPyCtxtPtr pyCtxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001630 PyObject *pyobj_ctxt;
1631 PyObject *pyobj_f;
1632 PyObject *pyobj_arg;
1633
Daniel Veillard417be3a2003-01-20 21:26:34 +00001634 if (!PyArg_ParseTuple(args, (char *)"OOO:xmlParserCtxtSetErrorHandler",
Daniel Veillarde6227e02003-01-14 11:42:39 +00001635 &pyobj_ctxt, &pyobj_f, &pyobj_arg))
1636 return(NULL);
1637 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1638 if (ctxt->_private == NULL) {
Daniel Veillarde6227e02003-01-14 11:42:39 +00001639 pyCtxt = xmlMalloc(sizeof(xmlParserCtxtPyCtxt));
1640 if (pyCtxt == NULL) {
1641 py_retval = libxml_intWrap(-1);
1642 return(py_retval);
1643 }
1644 memset(pyCtxt,0,sizeof(xmlParserCtxtPyCtxt));
1645 ctxt->_private = pyCtxt;
1646 }
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001647 else {
Daniel Veillard417be3a2003-01-20 21:26:34 +00001648 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001649 }
Daniel Veillarde6227e02003-01-14 11:42:39 +00001650 /* TODO: check f is a function ! */
Daniel Veillard417be3a2003-01-20 21:26:34 +00001651 Py_XDECREF(pyCtxt->f);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001652 Py_XINCREF(pyobj_f);
Daniel Veillard417be3a2003-01-20 21:26:34 +00001653 pyCtxt->f = pyobj_f;
1654 Py_XDECREF(pyCtxt->arg);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001655 Py_XINCREF(pyobj_arg);
Daniel Veillard417be3a2003-01-20 21:26:34 +00001656 pyCtxt->arg = pyobj_arg;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001657
Daniel Veillard417be3a2003-01-20 21:26:34 +00001658 if (pyobj_f != Py_None) {
1659 ctxt->sax->error = libxml_xmlParserCtxtErrorFuncHandler;
1660 ctxt->sax->warning = libxml_xmlParserCtxtWarningFuncHandler;
1661 ctxt->vctxt.error = libxml_xmlParserCtxtValidityErrorFuncHandler;
1662 ctxt->vctxt.warning = libxml_xmlParserCtxtValidityWarningFuncHandler;
1663 }
1664 else {
1665 ctxt->sax->error = xmlParserError;
1666 ctxt->vctxt.error = xmlParserValidityError;
1667 ctxt->sax->warning = xmlParserWarning;
1668 ctxt->vctxt.warning = xmlParserValidityWarning;
1669 }
Daniel Veillarde6227e02003-01-14 11:42:39 +00001670
1671 py_retval = libxml_intWrap(1);
1672 return(py_retval);
1673}
1674
Daniel Veillardc2664642003-07-29 20:44:53 +00001675static PyObject *
Daniel Veillard417be3a2003-01-20 21:26:34 +00001676libxml_xmlParserCtxtGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
Daniel Veillarde6227e02003-01-14 11:42:39 +00001677{
1678 PyObject *py_retval;
1679 xmlParserCtxtPtr ctxt;
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001680 xmlParserCtxtPyCtxtPtr pyCtxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001681 PyObject *pyobj_ctxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001682
Daniel Veillard417be3a2003-01-20 21:26:34 +00001683 if (!PyArg_ParseTuple(args, (char *)"O:xmlParserCtxtGetErrorHandler",
1684 &pyobj_ctxt))
Daniel Veillarde6227e02003-01-14 11:42:39 +00001685 return(NULL);
1686 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
Daniel Veillard417be3a2003-01-20 21:26:34 +00001687 py_retval = PyTuple_New(2);
1688 if (ctxt->_private != NULL) {
1689 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
1690
1691 PyTuple_SetItem(py_retval, 0, pyCtxt->f);
1692 Py_XINCREF(pyCtxt->f);
1693 PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
1694 Py_XINCREF(pyCtxt->arg);
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001695 }
1696 else {
Daniel Veillard417be3a2003-01-20 21:26:34 +00001697 /* no python error handler registered */
1698 PyTuple_SetItem(py_retval, 0, Py_None);
1699 Py_XINCREF(Py_None);
1700 PyTuple_SetItem(py_retval, 1, Py_None);
1701 Py_XINCREF(Py_None);
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001702 }
Daniel Veillarde6227e02003-01-14 11:42:39 +00001703 return(py_retval);
1704}
1705
Daniel Veillardc2664642003-07-29 20:44:53 +00001706static PyObject *
Daniel Veillard417be3a2003-01-20 21:26:34 +00001707libxml_xmlFreeParserCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
1708 xmlParserCtxtPtr ctxt;
1709 PyObject *pyobj_ctxt;
1710 xmlParserCtxtPyCtxtPtr pyCtxt;
1711
1712 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeParserCtxt", &pyobj_ctxt))
1713 return(NULL);
1714 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1715
1716 if (ctxt != NULL) {
1717 pyCtxt = (xmlParserCtxtPyCtxtPtr)((xmlParserCtxtPtr)ctxt)->_private;
1718 if (pyCtxt) {
1719 Py_XDECREF(pyCtxt->f);
1720 Py_XDECREF(pyCtxt->arg);
1721 xmlFree(pyCtxt);
1722 }
1723 xmlFreeParserCtxt(ctxt);
1724 }
1725
1726 Py_INCREF(Py_None);
1727 return(Py_None);
1728}
1729
Daniel Veillard850ce9b2004-11-10 11:55:47 +00001730/***
1731 * xmlValidCtxt stuff
1732 */
1733
1734typedef struct
1735{
1736 PyObject *warn;
1737 PyObject *error;
1738 PyObject *arg;
1739} xmlValidCtxtPyCtxt;
1740typedef xmlValidCtxtPyCtxt *xmlValidCtxtPyCtxtPtr;
1741
1742static void
1743libxml_xmlValidCtxtGenericErrorFuncHandler(void *ctx, int severity, char *str)
1744{
1745 PyObject *list;
1746 PyObject *result;
1747 xmlValidCtxtPyCtxtPtr pyCtxt;
1748
1749#ifdef DEBUG_ERROR
1750 printf("libxml_xmlValidCtxtGenericErrorFuncHandler(%p, %d, %s, ...) called\n", ctx, severity, str);
1751#endif
1752
1753 pyCtxt = (xmlValidCtxtPyCtxtPtr)ctx;
1754
1755 list = PyTuple_New(2);
1756 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
1757 PyTuple_SetItem(list, 1, pyCtxt->arg);
1758 Py_XINCREF(pyCtxt->arg);
1759 result = PyEval_CallObject(pyCtxt->error, list);
1760 if (result == NULL)
1761 {
1762 /* TODO: manage for the exception to be propagated... */
1763 PyErr_Print();
1764 }
1765 Py_XDECREF(list);
1766 Py_XDECREF(result);
1767}
1768
1769static void
1770libxml_xmlValidCtxtGenericWarningFuncHandler(void *ctx, int severity, char *str)
1771{
1772 PyObject *list;
1773 PyObject *result;
1774 xmlValidCtxtPyCtxtPtr pyCtxt;
1775
1776#ifdef DEBUG_ERROR
1777 printf("libxml_xmlValidCtxtGenericWarningFuncHandler(%p, %d, %s, ...) called\n", ctx, severity, str);
1778#endif
1779
1780 pyCtxt = (xmlValidCtxtPyCtxtPtr)ctx;
1781
1782 list = PyTuple_New(2);
1783 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
1784 PyTuple_SetItem(list, 1, pyCtxt->arg);
1785 Py_XINCREF(pyCtxt->arg);
1786 result = PyEval_CallObject(pyCtxt->warn, list);
1787 if (result == NULL)
1788 {
1789 /* TODO: manage for the exception to be propagated... */
1790 PyErr_Print();
1791 }
1792 Py_XDECREF(list);
1793 Py_XDECREF(result);
1794}
1795
1796static void
1797libxml_xmlValidCtxtErrorFuncHandler(void *ctx, const char *msg, ...)
1798{
1799 va_list ap;
1800
1801 va_start(ap, msg);
1802 libxml_xmlValidCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap));
1803 va_end(ap);
1804}
1805
1806static void
1807libxml_xmlValidCtxtWarningFuncHandler(void *ctx, const char *msg, ...)
1808{
1809 va_list ap;
1810
1811 va_start(ap, msg);
1812 libxml_xmlValidCtxtGenericWarningFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap));
1813 va_end(ap);
1814}
1815
1816static PyObject *
1817libxml_xmlSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1818{
1819 PyObject *py_retval;
1820 PyObject *pyobj_error;
1821 PyObject *pyobj_warn;
1822 PyObject *pyobj_ctx;
1823 PyObject *pyobj_arg = Py_None;
1824 xmlValidCtxtPtr ctxt;
1825 xmlValidCtxtPyCtxtPtr pyCtxt;
1826
1827 if (!PyArg_ParseTuple
1828 (args, (char *) "OOO|O:xmlSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
1829 return (NULL);
1830
1831#ifdef DEBUG_ERROR
1832 printf("libxml_xmlSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
1833#endif
1834
1835 ctxt = PyValidCtxt_Get(pyobj_ctx);
1836 pyCtxt = xmlMalloc(sizeof(xmlValidCtxtPyCtxt));
1837 if (pyCtxt == NULL) {
1838 py_retval = libxml_intWrap(-1);
1839 return(py_retval);
1840 }
1841 memset(pyCtxt, 0, sizeof(xmlValidCtxtPyCtxt));
1842
1843
1844 /* TODO: check warn and error is a function ! */
1845 Py_XDECREF(pyCtxt->error);
1846 Py_XINCREF(pyobj_error);
1847 pyCtxt->error = pyobj_error;
1848
1849 Py_XDECREF(pyCtxt->warn);
1850 Py_XINCREF(pyobj_warn);
1851 pyCtxt->warn = pyobj_warn;
1852
1853 Py_XDECREF(pyCtxt->arg);
1854 Py_XINCREF(pyobj_arg);
1855 pyCtxt->arg = pyobj_arg;
1856
1857 ctxt->error = libxml_xmlValidCtxtErrorFuncHandler;
1858 ctxt->warning = libxml_xmlValidCtxtWarningFuncHandler;
1859 ctxt->userData = pyCtxt;
1860
1861 py_retval = libxml_intWrap(1);
1862 return (py_retval);
1863}
1864
Daniel Veillard25c90c52005-03-02 10:47:41 +00001865
Daniel Veillardbb8502c2005-03-30 07:40:35 +00001866static PyObject *
Daniel Veillard25c90c52005-03-02 10:47:41 +00001867libxml_xmlFreeValidCtxt(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1868 xmlValidCtxtPtr cur;
1869 xmlValidCtxtPyCtxtPtr pyCtxt;
1870 PyObject *pyobj_cur;
1871
1872 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeValidCtxt", &pyobj_cur))
1873 return(NULL);
1874 cur = (xmlValidCtxtPtr) PyValidCtxt_Get(pyobj_cur);
1875
1876 pyCtxt = (xmlValidCtxtPyCtxtPtr)(cur->userData);
1877 if (pyCtxt != NULL)
1878 {
1879 Py_XDECREF(pyCtxt->error);
1880 Py_XDECREF(pyCtxt->warn);
1881 Py_XDECREF(pyCtxt->arg);
1882 xmlFree(pyCtxt);
1883 }
1884
1885 xmlFreeValidCtxt(cur);
1886 Py_INCREF(Py_None);
1887 return(Py_None);
1888}
1889
Daniel Veillarda7340c82002-02-01 17:56:45 +00001890/************************************************************************
1891 * *
Daniel Veillard26f70262003-01-16 22:45:08 +00001892 * Per xmlTextReader error handler *
1893 * *
1894 ************************************************************************/
1895
1896typedef struct
1897{
1898 PyObject *f;
1899 PyObject *arg;
1900} xmlTextReaderPyCtxt;
1901typedef xmlTextReaderPyCtxt *xmlTextReaderPyCtxtPtr;
1902
1903static void
1904libxml_xmlTextReaderErrorCallback(void *arg,
1905 const char *msg,
Daniel Veillard417be3a2003-01-20 21:26:34 +00001906 int severity,
1907 xmlTextReaderLocatorPtr locator)
Daniel Veillard26f70262003-01-16 22:45:08 +00001908{
1909 xmlTextReaderPyCtxt *pyCtxt = (xmlTextReaderPyCtxt *)arg;
1910 PyObject *list;
1911 PyObject *result;
1912
Daniel Veillard417be3a2003-01-20 21:26:34 +00001913 list = PyTuple_New(4);
Daniel Veillard26f70262003-01-16 22:45:08 +00001914 PyTuple_SetItem(list, 0, pyCtxt->arg);
1915 Py_XINCREF(pyCtxt->arg);
1916 PyTuple_SetItem(list, 1, libxml_charPtrConstWrap(msg));
Daniel Veillard417be3a2003-01-20 21:26:34 +00001917 PyTuple_SetItem(list, 2, libxml_intWrap(severity));
1918 PyTuple_SetItem(list, 3, libxml_xmlTextReaderLocatorPtrWrap(locator));
Daniel Veillard26f70262003-01-16 22:45:08 +00001919 result = PyEval_CallObject(pyCtxt->f, list);
1920 if (result == NULL)
1921 {
Daniel Veillard417be3a2003-01-20 21:26:34 +00001922 /* TODO: manage for the exception to be propagated... */
Daniel Veillard26f70262003-01-16 22:45:08 +00001923 PyErr_Print();
1924 }
1925 Py_XDECREF(list);
1926 Py_XDECREF(result);
1927}
1928
Daniel Veillardc2664642003-07-29 20:44:53 +00001929static PyObject *
Daniel Veillard26f70262003-01-16 22:45:08 +00001930libxml_xmlTextReaderSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1931{
1932 xmlTextReaderPtr reader;
1933 xmlTextReaderPyCtxtPtr pyCtxt;
1934 xmlTextReaderErrorFunc f;
1935 void *arg;
1936 PyObject *pyobj_reader;
1937 PyObject *pyobj_f;
1938 PyObject *pyobj_arg;
1939 PyObject *py_retval;
1940
1941 if (!PyArg_ParseTuple(args, (char *)"OOO:xmlTextReaderSetErrorHandler", &pyobj_reader, &pyobj_f, &pyobj_arg))
1942 return(NULL);
1943 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
1944 /* clear previous error handler */
1945 xmlTextReaderGetErrorHandler(reader,&f,&arg);
1946 if (arg != NULL) {
Daniel Veillardc2664642003-07-29 20:44:53 +00001947 if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) {
Daniel Veillard26f70262003-01-16 22:45:08 +00001948 /* ok, it's our error handler! */
1949 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
1950 Py_XDECREF(pyCtxt->f);
1951 Py_XDECREF(pyCtxt->arg);
1952 xmlFree(pyCtxt);
1953 }
1954 else {
1955 /*
1956 * there already an arg, and it's not ours,
1957 * there is definitely something wrong going on here...
1958 * we don't know how to free it, so we bail out...
1959 */
1960 py_retval = libxml_intWrap(-1);
1961 return(py_retval);
1962 }
1963 }
1964 xmlTextReaderSetErrorHandler(reader,NULL,NULL);
1965 /* set new error handler */
1966 if (pyobj_f != Py_None)
1967 {
1968 pyCtxt = (xmlTextReaderPyCtxtPtr)xmlMalloc(sizeof(xmlTextReaderPyCtxt));
1969 if (pyCtxt == NULL) {
1970 py_retval = libxml_intWrap(-1);
1971 return(py_retval);
1972 }
1973 Py_XINCREF(pyobj_f);
1974 pyCtxt->f = pyobj_f;
1975 Py_XINCREF(pyobj_arg);
1976 pyCtxt->arg = pyobj_arg;
Daniel Veillardc2664642003-07-29 20:44:53 +00001977 xmlTextReaderSetErrorHandler(reader,
1978 (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback,
1979 pyCtxt);
Daniel Veillard26f70262003-01-16 22:45:08 +00001980 }
1981
1982 py_retval = libxml_intWrap(1);
1983 return(py_retval);
1984}
1985
Daniel Veillardc2664642003-07-29 20:44:53 +00001986static PyObject *
Daniel Veillard26f70262003-01-16 22:45:08 +00001987libxml_xmlTextReaderGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1988{
1989 xmlTextReaderPtr reader;
1990 xmlTextReaderPyCtxtPtr pyCtxt;
1991 xmlTextReaderErrorFunc f;
1992 void *arg;
1993 PyObject *pyobj_reader;
1994 PyObject *py_retval;
1995
1996 if (!PyArg_ParseTuple(args, (char *)"O:xmlTextReaderSetErrorHandler", &pyobj_reader))
1997 return(NULL);
1998 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
1999 xmlTextReaderGetErrorHandler(reader,&f,&arg);
2000 py_retval = PyTuple_New(2);
Daniel Veillardc2664642003-07-29 20:44:53 +00002001 if (f == (xmlTextReaderErrorFunc)libxml_xmlTextReaderErrorCallback) {
Daniel Veillard26f70262003-01-16 22:45:08 +00002002 /* ok, it's our error handler! */
2003 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
2004 PyTuple_SetItem(py_retval, 0, pyCtxt->f);
2005 Py_XINCREF(pyCtxt->f);
2006 PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
2007 Py_XINCREF(pyCtxt->arg);
2008 }
2009 else
2010 {
2011 /* f is null or it's not our error handler */
2012 PyTuple_SetItem(py_retval, 0, Py_None);
2013 Py_XINCREF(Py_None);
2014 PyTuple_SetItem(py_retval, 1, Py_None);
2015 Py_XINCREF(Py_None);
2016 }
2017 return(py_retval);
2018}
2019
Daniel Veillardc2664642003-07-29 20:44:53 +00002020static PyObject *
Daniel Veillard26f70262003-01-16 22:45:08 +00002021libxml_xmlFreeTextReader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
2022 xmlTextReaderPtr reader;
2023 PyObject *pyobj_reader;
2024 xmlTextReaderPyCtxtPtr pyCtxt;
2025 xmlTextReaderErrorFunc f;
2026 void *arg;
2027
Daniel Veillard157fee02003-10-31 10:36:03 +00002028 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeTextReader", &pyobj_reader))
2029 return(NULL);
2030 if (!PyCObject_Check(pyobj_reader)) {
Daniel Veillardbb3ba322003-10-30 13:12:43 +00002031 Py_INCREF(Py_None);
2032 return(Py_None);
2033 }
Daniel Veillard26f70262003-01-16 22:45:08 +00002034 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
Daniel Veillardbb3ba322003-10-30 13:12:43 +00002035 if (reader == NULL) {
2036 Py_INCREF(Py_None);
2037 return(Py_None);
2038 }
Daniel Veillard26f70262003-01-16 22:45:08 +00002039
2040 xmlTextReaderGetErrorHandler(reader,&f,&arg);
2041 if (arg != NULL) {
Daniel Veillardc2664642003-07-29 20:44:53 +00002042 if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) {
Daniel Veillard26f70262003-01-16 22:45:08 +00002043 /* ok, it's our error handler! */
2044 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
2045 Py_XDECREF(pyCtxt->f);
2046 Py_XDECREF(pyCtxt->arg);
2047 xmlFree(pyCtxt);
2048 }
2049 /*
2050 * else, something wrong happened, because the error handler is
2051 * not owned by the python bindings...
2052 */
2053 }
2054
2055 xmlFreeTextReader(reader);
2056 Py_INCREF(Py_None);
2057 return(Py_None);
2058}
2059
2060/************************************************************************
2061 * *
Daniel Veillarda7340c82002-02-01 17:56:45 +00002062 * XPath extensions *
2063 * *
2064 ************************************************************************/
2065
Daniel Veillarda7340c82002-02-01 17:56:45 +00002066static void
Daniel Veillardd2379012002-03-15 22:24:56 +00002067libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs)
2068{
Daniel Veillarda7340c82002-02-01 17:56:45 +00002069 PyObject *list, *cur, *result;
2070 xmlXPathObjectPtr obj;
Daniel Veillard70cab352002-02-06 16:06:58 +00002071 xmlXPathContextPtr rctxt;
2072 PyObject *current_function = NULL;
2073 const xmlChar *name;
2074 const xmlChar *ns_uri;
Daniel Veillarda7340c82002-02-01 17:56:45 +00002075 int i;
2076
Daniel Veillard70cab352002-02-06 16:06:58 +00002077 if (ctxt == NULL)
Daniel Veillardd2379012002-03-15 22:24:56 +00002078 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00002079 rctxt = ctxt->context;
2080 if (rctxt == NULL)
Daniel Veillardd2379012002-03-15 22:24:56 +00002081 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00002082 name = rctxt->function;
2083 ns_uri = rctxt->functionURI;
Daniel Veillarda7340c82002-02-01 17:56:45 +00002084#ifdef DEBUG_XPATH
Daniel Veillardd2379012002-03-15 22:24:56 +00002085 printf("libxml_xmlXPathFuncCallback called name %s URI %s\n", name,
2086 ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002087#endif
2088
Daniel Veillard70cab352002-02-06 16:06:58 +00002089 /*
2090 * Find the function, it should be there it was there at lookup
2091 */
Daniel Veillardd2379012002-03-15 22:24:56 +00002092 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
2093 if ( /* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002094 (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
2095 (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
2096 current_function = (*libxml_xpathCallbacks)[i].function;
Daniel Veillardd2379012002-03-15 22:24:56 +00002097 }
Daniel Veillard70cab352002-02-06 16:06:58 +00002098 }
2099 if (current_function == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002100 printf
2101 ("libxml_xmlXPathFuncCallback: internal error %s not found !\n",
2102 name);
2103 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00002104 }
2105
Daniel Veillardc575b992002-02-08 13:28:40 +00002106 list = PyTuple_New(nargs + 1);
2107 PyTuple_SetItem(list, 0, libxml_xmlXPathParserContextPtrWrap(ctxt));
Daniel Veillard05349ab2004-01-25 20:01:35 +00002108 for (i = nargs - 1; i >= 0; i--) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002109 obj = valuePop(ctxt);
2110 cur = libxml_xmlXPathObjectPtrWrap(obj);
2111 PyTuple_SetItem(list, i + 1, cur);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002112 }
2113 result = PyEval_CallObject(current_function, list);
2114 Py_DECREF(list);
2115
2116 obj = libxml_xmlXPathObjectPtrConvert(result);
2117 valuePush(ctxt, obj);
2118}
2119
2120static xmlXPathFunction
Daniel Veillardd2379012002-03-15 22:24:56 +00002121libxml_xmlXPathFuncLookupFunc(void *ctxt, const xmlChar * name,
2122 const xmlChar * ns_uri)
2123{
Daniel Veillarda7340c82002-02-01 17:56:45 +00002124 int i;
Daniel Veillardd2379012002-03-15 22:24:56 +00002125
Daniel Veillarda7340c82002-02-01 17:56:45 +00002126#ifdef DEBUG_XPATH
2127 printf("libxml_xmlXPathFuncLookupFunc(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00002128 ctxt, name, ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002129#endif
Daniel Veillard70cab352002-02-06 16:06:58 +00002130 /*
2131 * This is called once only. The address is then stored in the
2132 * XPath expression evaluation, the proper object to call can
2133 * then still be found using the execution context function
2134 * and functionURI fields.
2135 */
Daniel Veillardd2379012002-03-15 22:24:56 +00002136 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002137 if ((ctxt == (*libxml_xpathCallbacks)[i].ctx) &&
2138 (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
2139 (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002140 return (libxml_xmlXPathFuncCallback);
2141 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00002142 }
Daniel Veillardd2379012002-03-15 22:24:56 +00002143 return (NULL);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002144}
2145
2146static void
Daniel Veillardd2379012002-03-15 22:24:56 +00002147libxml_xpathCallbacksInitialize(void)
2148{
Daniel Veillarda7340c82002-02-01 17:56:45 +00002149 int i;
2150
2151 if (libxml_xpathCallbacksInitialized != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00002152 return;
Daniel Veillarda7340c82002-02-01 17:56:45 +00002153
2154#ifdef DEBUG_XPATH
2155 printf("libxml_xpathCallbacksInitialized called\n");
2156#endif
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002157 libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlMalloc(
2158 libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
Daniel Veillarda7340c82002-02-01 17:56:45 +00002159
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002160 for (i = 0; i < libxml_xpathCallbacksAllocd; i++) {
2161 (*libxml_xpathCallbacks)[i].ctx = NULL;
2162 (*libxml_xpathCallbacks)[i].name = NULL;
2163 (*libxml_xpathCallbacks)[i].ns_uri = NULL;
2164 (*libxml_xpathCallbacks)[i].function = NULL;
Daniel Veillarda7340c82002-02-01 17:56:45 +00002165 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00002166 libxml_xpathCallbacksInitialized = 1;
2167}
2168
2169PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002170libxml_xmlRegisterXPathFunction(ATTRIBUTE_UNUSED PyObject * self,
2171 PyObject * args)
2172{
Daniel Veillarda7340c82002-02-01 17:56:45 +00002173 PyObject *py_retval;
2174 int c_retval = 0;
2175 xmlChar *name;
2176 xmlChar *ns_uri;
2177 xmlXPathContextPtr ctx;
2178 PyObject *pyobj_ctx;
2179 PyObject *pyobj_f;
2180 int i;
2181
Daniel Veillardd2379012002-03-15 22:24:56 +00002182 if (!PyArg_ParseTuple
2183 (args, (char *) "OszO:registerXPathFunction", &pyobj_ctx, &name,
2184 &ns_uri, &pyobj_f))
2185 return (NULL);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002186
2187 ctx = (xmlXPathContextPtr) PyxmlXPathContext_Get(pyobj_ctx);
2188 if (libxml_xpathCallbacksInitialized == 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00002189 libxml_xpathCallbacksInitialize();
Daniel Veillarda7340c82002-02-01 17:56:45 +00002190 xmlXPathRegisterFuncLookup(ctx, libxml_xmlXPathFuncLookupFunc, ctx);
2191
2192 if ((pyobj_ctx == NULL) || (name == NULL) || (pyobj_f == NULL)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002193 py_retval = libxml_intWrap(-1);
2194 return (py_retval);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002195 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00002196#ifdef DEBUG_XPATH
2197 printf("libxml_registerXPathFunction(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00002198 ctx, name, ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002199#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002200 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002201 if ((ctx == (*libxml_xpathCallbacks)[i].ctx) &&
2202 (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
2203 (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002204 Py_XINCREF(pyobj_f);
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002205 Py_XDECREF((*libxml_xpathCallbacks)[i].function);
2206 (*libxml_xpathCallbacks)[i].function = pyobj_f;
Daniel Veillardd2379012002-03-15 22:24:56 +00002207 c_retval = 1;
2208 goto done;
2209 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00002210 }
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002211 if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksAllocd) {
2212 libxml_xpathCallbacksAllocd+=10;
2213 libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlRealloc(
2214 libxml_xpathCallbacks,
2215 libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
2216 }
2217 i = libxml_xpathCallbacksNb++;
2218 Py_XINCREF(pyobj_f);
2219 (*libxml_xpathCallbacks)[i].ctx = ctx;
2220 (*libxml_xpathCallbacks)[i].name = xmlStrdup(name);
2221 (*libxml_xpathCallbacks)[i].ns_uri = xmlStrdup(ns_uri);
2222 (*libxml_xpathCallbacks)[i].function = pyobj_f;
Daniel Veillardd2379012002-03-15 22:24:56 +00002223 c_retval = 1;
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002224
Daniel Veillardd2379012002-03-15 22:24:56 +00002225 done:
Daniel Veillarda7340c82002-02-01 17:56:45 +00002226 py_retval = libxml_intWrap((int) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00002227 return (py_retval);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002228}
2229
Daniel Veillard1971ee22002-01-31 20:29:19 +00002230/************************************************************************
2231 * *
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002232 * Global properties access *
2233 * *
2234 ************************************************************************/
2235static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002236libxml_name(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002237{
2238 PyObject *resultobj, *obj;
2239 xmlNodePtr cur;
2240 const xmlChar *res;
2241
Daniel Veillardd2379012002-03-15 22:24:56 +00002242 if (!PyArg_ParseTuple(args, (char *) "O:name", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002243 return NULL;
2244 cur = PyxmlNode_Get(obj);
2245
2246#ifdef DEBUG
2247 printf("libxml_name: cur = %p type %d\n", cur, cur->type);
2248#endif
2249
Daniel Veillardd2379012002-03-15 22:24:56 +00002250 switch (cur->type) {
2251 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002252#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002253 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002254#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002255 case XML_HTML_DOCUMENT_NODE:{
2256 xmlDocPtr doc = (xmlDocPtr) cur;
2257
2258 res = doc->URL;
2259 break;
2260 }
2261 case XML_ATTRIBUTE_NODE:{
2262 xmlAttrPtr attr = (xmlAttrPtr) cur;
2263
2264 res = attr->name;
2265 break;
2266 }
2267 case XML_NAMESPACE_DECL:{
2268 xmlNsPtr ns = (xmlNsPtr) cur;
2269
2270 res = ns->prefix;
2271 break;
2272 }
2273 default:
2274 res = cur->name;
2275 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002276 }
Daniel Veillard1971ee22002-01-31 20:29:19 +00002277 resultobj = libxml_constxmlCharPtrWrap(res);
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002278
2279 return resultobj;
2280}
2281
2282static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002283libxml_doc(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002284{
2285 PyObject *resultobj, *obj;
2286 xmlNodePtr cur;
2287 xmlDocPtr res;
2288
Daniel Veillardd2379012002-03-15 22:24:56 +00002289 if (!PyArg_ParseTuple(args, (char *) "O:doc", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002290 return NULL;
2291 cur = PyxmlNode_Get(obj);
2292
2293#ifdef DEBUG
2294 printf("libxml_doc: cur = %p\n", cur);
2295#endif
2296
Daniel Veillardd2379012002-03-15 22:24:56 +00002297 switch (cur->type) {
2298 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002299#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002300 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002301#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002302 case XML_HTML_DOCUMENT_NODE:
2303 res = NULL;
2304 break;
2305 case XML_ATTRIBUTE_NODE:{
2306 xmlAttrPtr attr = (xmlAttrPtr) cur;
2307
2308 res = attr->doc;
2309 break;
2310 }
2311 case XML_NAMESPACE_DECL:
2312 res = NULL;
2313 break;
2314 default:
2315 res = cur->doc;
2316 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002317 }
2318 resultobj = libxml_xmlDocPtrWrap(res);
2319 return resultobj;
2320}
2321
2322static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002323libxml_properties(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002324{
2325 PyObject *resultobj, *obj;
2326 xmlNodePtr cur = NULL;
2327 xmlAttrPtr res;
2328
Daniel Veillardd2379012002-03-15 22:24:56 +00002329 if (!PyArg_ParseTuple(args, (char *) "O:properties", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002330 return NULL;
2331 cur = PyxmlNode_Get(obj);
2332 if (cur->type == XML_ELEMENT_NODE)
Daniel Veillardd2379012002-03-15 22:24:56 +00002333 res = cur->properties;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002334 else
Daniel Veillardd2379012002-03-15 22:24:56 +00002335 res = NULL;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002336 resultobj = libxml_xmlAttrPtrWrap(res);
2337 return resultobj;
2338}
2339
2340static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002341libxml_next(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002342{
2343 PyObject *resultobj, *obj;
2344 xmlNodePtr cur;
2345 xmlNodePtr res;
2346
Daniel Veillardd2379012002-03-15 22:24:56 +00002347 if (!PyArg_ParseTuple(args, (char *) "O:next", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002348 return NULL;
2349 cur = PyxmlNode_Get(obj);
2350
2351#ifdef DEBUG
2352 printf("libxml_next: cur = %p\n", cur);
2353#endif
2354
Daniel Veillardd2379012002-03-15 22:24:56 +00002355 switch (cur->type) {
2356 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002357#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002358 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002359#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002360 case XML_HTML_DOCUMENT_NODE:
2361 res = NULL;
2362 break;
2363 case XML_ATTRIBUTE_NODE:{
2364 xmlAttrPtr attr = (xmlAttrPtr) cur;
2365
2366 res = (xmlNodePtr) attr->next;
2367 break;
2368 }
2369 case XML_NAMESPACE_DECL:{
2370 xmlNsPtr ns = (xmlNsPtr) cur;
2371
2372 res = (xmlNodePtr) ns->next;
2373 break;
2374 }
2375 default:
2376 res = cur->next;
2377 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002378
2379 }
2380 resultobj = libxml_xmlNodePtrWrap(res);
2381 return resultobj;
2382}
2383
2384static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002385libxml_prev(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002386{
2387 PyObject *resultobj, *obj;
2388 xmlNodePtr cur;
2389 xmlNodePtr res;
2390
Daniel Veillardd2379012002-03-15 22:24:56 +00002391 if (!PyArg_ParseTuple(args, (char *) "O:prev", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002392 return NULL;
2393 cur = PyxmlNode_Get(obj);
2394
2395#ifdef DEBUG
2396 printf("libxml_prev: cur = %p\n", cur);
2397#endif
2398
Daniel Veillardd2379012002-03-15 22:24:56 +00002399 switch (cur->type) {
2400 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002401#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002402 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002403#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002404 case XML_HTML_DOCUMENT_NODE:
2405 res = NULL;
2406 break;
2407 case XML_ATTRIBUTE_NODE:{
2408 xmlAttrPtr attr = (xmlAttrPtr) cur;
2409
Daniel Veillardfaa35ff2002-11-24 13:53:43 +00002410 res = (xmlNodePtr) attr->prev;
Daniel Veillardd2379012002-03-15 22:24:56 +00002411 }
2412 case XML_NAMESPACE_DECL:
2413 res = NULL;
2414 break;
2415 default:
Daniel Veillardfaa35ff2002-11-24 13:53:43 +00002416 res = cur->prev;
Daniel Veillardd2379012002-03-15 22:24:56 +00002417 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002418 }
2419 resultobj = libxml_xmlNodePtrWrap(res);
2420 return resultobj;
2421}
2422
2423static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002424libxml_children(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002425{
2426 PyObject *resultobj, *obj;
2427 xmlNodePtr cur;
2428 xmlNodePtr res;
2429
Daniel Veillardd2379012002-03-15 22:24:56 +00002430 if (!PyArg_ParseTuple(args, (char *) "O:children", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002431 return NULL;
2432 cur = PyxmlNode_Get(obj);
2433
2434#ifdef DEBUG
2435 printf("libxml_children: cur = %p\n", cur);
2436#endif
2437
Daniel Veillardd2379012002-03-15 22:24:56 +00002438 switch (cur->type) {
2439 case XML_ELEMENT_NODE:
2440 case XML_ENTITY_REF_NODE:
2441 case XML_ENTITY_NODE:
2442 case XML_PI_NODE:
2443 case XML_COMMENT_NODE:
2444 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002445#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002446 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002447#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002448 case XML_HTML_DOCUMENT_NODE:
2449 case XML_DTD_NODE:
2450 res = cur->children;
2451 break;
2452 case XML_ATTRIBUTE_NODE:{
2453 xmlAttrPtr attr = (xmlAttrPtr) cur;
2454
2455 res = attr->children;
2456 break;
2457 }
2458 default:
2459 res = NULL;
2460 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002461 }
2462 resultobj = libxml_xmlNodePtrWrap(res);
2463 return resultobj;
2464}
2465
2466static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002467libxml_last(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002468{
2469 PyObject *resultobj, *obj;
2470 xmlNodePtr cur;
2471 xmlNodePtr res;
2472
Daniel Veillardd2379012002-03-15 22:24:56 +00002473 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002474 return NULL;
2475 cur = PyxmlNode_Get(obj);
2476
2477#ifdef DEBUG
2478 printf("libxml_last: cur = %p\n", cur);
2479#endif
2480
Daniel Veillardd2379012002-03-15 22:24:56 +00002481 switch (cur->type) {
2482 case XML_ELEMENT_NODE:
2483 case XML_ENTITY_REF_NODE:
2484 case XML_ENTITY_NODE:
2485 case XML_PI_NODE:
2486 case XML_COMMENT_NODE:
2487 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002488#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002489 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002490#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002491 case XML_HTML_DOCUMENT_NODE:
2492 case XML_DTD_NODE:
2493 res = cur->last;
2494 break;
2495 case XML_ATTRIBUTE_NODE:{
2496 xmlAttrPtr attr = (xmlAttrPtr) cur;
2497
2498 res = attr->last;
2499 }
2500 default:
2501 res = NULL;
2502 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002503 }
2504 resultobj = libxml_xmlNodePtrWrap(res);
2505 return resultobj;
2506}
2507
2508static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002509libxml_parent(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002510{
2511 PyObject *resultobj, *obj;
2512 xmlNodePtr cur;
2513 xmlNodePtr res;
2514
Daniel Veillardd2379012002-03-15 22:24:56 +00002515 if (!PyArg_ParseTuple(args, (char *) "O:parent", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002516 return NULL;
2517 cur = PyxmlNode_Get(obj);
2518
2519#ifdef DEBUG
2520 printf("libxml_parent: cur = %p\n", cur);
2521#endif
2522
Daniel Veillardd2379012002-03-15 22:24:56 +00002523 switch (cur->type) {
2524 case XML_DOCUMENT_NODE:
2525 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002526#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002527 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002528#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002529 res = NULL;
2530 break;
2531 case XML_ATTRIBUTE_NODE:{
2532 xmlAttrPtr attr = (xmlAttrPtr) cur;
2533
2534 res = attr->parent;
2535 }
2536 case XML_ENTITY_DECL:
2537 case XML_NAMESPACE_DECL:
2538 case XML_XINCLUDE_START:
2539 case XML_XINCLUDE_END:
2540 res = NULL;
2541 break;
2542 default:
2543 res = cur->parent;
2544 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002545 }
2546 resultobj = libxml_xmlNodePtrWrap(res);
2547 return resultobj;
2548}
2549
2550static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002551libxml_type(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002552{
2553 PyObject *resultobj, *obj;
2554 xmlNodePtr cur;
Daniel Veillardd2379012002-03-15 22:24:56 +00002555 const xmlChar *res = NULL;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002556
Daniel Veillardd2379012002-03-15 22:24:56 +00002557 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002558 return NULL;
2559 cur = PyxmlNode_Get(obj);
2560
2561#ifdef DEBUG
2562 printf("libxml_type: cur = %p\n", cur);
2563#endif
2564
Daniel Veillardd2379012002-03-15 22:24:56 +00002565 switch (cur->type) {
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002566 case XML_ELEMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002567 res = (const xmlChar *) "element";
2568 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002569 case XML_ATTRIBUTE_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002570 res = (const xmlChar *) "attribute";
2571 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002572 case XML_TEXT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002573 res = (const xmlChar *) "text";
2574 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002575 case XML_CDATA_SECTION_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002576 res = (const xmlChar *) "cdata";
2577 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002578 case XML_ENTITY_REF_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002579 res = (const xmlChar *) "entity_ref";
2580 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002581 case XML_ENTITY_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002582 res = (const xmlChar *) "entity";
2583 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002584 case XML_PI_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002585 res = (const xmlChar *) "pi";
2586 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002587 case XML_COMMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002588 res = (const xmlChar *) "comment";
2589 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002590 case XML_DOCUMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002591 res = (const xmlChar *) "document_xml";
2592 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002593 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002594 res = (const xmlChar *) "doctype";
2595 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002596 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002597 res = (const xmlChar *) "fragment";
2598 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002599 case XML_NOTATION_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002600 res = (const xmlChar *) "notation";
2601 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002602 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002603 res = (const xmlChar *) "document_html";
2604 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002605 case XML_DTD_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002606 res = (const xmlChar *) "dtd";
2607 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002608 case XML_ELEMENT_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002609 res = (const xmlChar *) "elem_decl";
2610 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002611 case XML_ATTRIBUTE_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002612 res = (const xmlChar *) "attribute_decl";
2613 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002614 case XML_ENTITY_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002615 res = (const xmlChar *) "entity_decl";
2616 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002617 case XML_NAMESPACE_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002618 res = (const xmlChar *) "namespace";
2619 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002620 case XML_XINCLUDE_START:
Daniel Veillardd2379012002-03-15 22:24:56 +00002621 res = (const xmlChar *) "xinclude_start";
2622 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002623 case XML_XINCLUDE_END:
Daniel Veillardd2379012002-03-15 22:24:56 +00002624 res = (const xmlChar *) "xinclude_end";
2625 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002626#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002627 case XML_DOCB_DOCUMENT_NODE:
2628 res = (const xmlChar *) "document_docbook";
2629 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002630#endif
2631 }
2632#ifdef DEBUG
2633 printf("libxml_type: cur = %p: %s\n", cur, res);
2634#endif
2635
Daniel Veillard1971ee22002-01-31 20:29:19 +00002636 resultobj = libxml_constxmlCharPtrWrap(res);
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002637 return resultobj;
2638}
2639
2640/************************************************************************
2641 * *
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002642 * Specific accessor functions *
2643 * *
2644 ************************************************************************/
2645PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002646libxml_xmlNodeGetNsDefs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2647{
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002648 PyObject *py_retval;
2649 xmlNsPtr c_retval;
2650 xmlNodePtr node;
2651 PyObject *pyobj_node;
2652
Daniel Veillardd2379012002-03-15 22:24:56 +00002653 if (!PyArg_ParseTuple
2654 (args, (char *) "O:xmlNodeGetNsDefs", &pyobj_node))
2655 return (NULL);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002656 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2657
2658 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002659 Py_INCREF(Py_None);
2660 return (Py_None);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002661 }
2662 c_retval = node->nsDef;
2663 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00002664 return (py_retval);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002665}
2666
2667PyObject *
Daniel Veillardf9cf6f52005-04-12 01:02:29 +00002668libxml_xmlNodeRemoveNsDef(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2669{
2670 PyObject *py_retval;
2671 xmlNsPtr ns, prev;
2672 xmlNodePtr node;
2673 PyObject *pyobj_node;
2674 xmlChar *href;
2675 xmlNsPtr c_retval;
2676
2677 if (!PyArg_ParseTuple
2678 (args, (char *) "Oz:xmlNodeRemoveNsDef", &pyobj_node, &href))
2679 return (NULL);
2680 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2681 ns = NULL;
2682
2683 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
2684 Py_INCREF(Py_None);
2685 return (Py_None);
2686 }
2687
2688 if (href == NULL) {
2689 ns = node->nsDef;
2690 node->nsDef = NULL;
2691 c_retval = 0;
2692 }
2693 else {
2694 prev = NULL;
2695 ns = node->nsDef;
2696 while (ns != NULL) {
2697 if (xmlStrEqual(ns->href, href)) {
2698 if (prev != NULL)
2699 prev->next = ns->next;
2700 else
2701 node->nsDef = ns->next;
2702 ns->next = NULL;
2703 c_retval = 0;
2704 break;
2705 }
2706 prev = ns;
2707 ns = ns->next;
2708 }
2709 }
2710
2711 c_retval = ns;
2712 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
2713 return (py_retval);
2714}
2715
2716PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002717libxml_xmlNodeGetNs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2718{
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002719 PyObject *py_retval;
2720 xmlNsPtr c_retval;
2721 xmlNodePtr node;
2722 PyObject *pyobj_node;
2723
Daniel Veillardd2379012002-03-15 22:24:56 +00002724 if (!PyArg_ParseTuple(args, (char *) "O:xmlNodeGetNs", &pyobj_node))
2725 return (NULL);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002726 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2727
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002728 if ((node == NULL) ||
2729 ((node->type != XML_ELEMENT_NODE) &&
2730 (node->type != XML_ATTRIBUTE_NODE))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002731 Py_INCREF(Py_None);
2732 return (Py_None);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002733 }
2734 c_retval = node->ns;
2735 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00002736 return (py_retval);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002737}
2738
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002739#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002740/************************************************************************
2741 * *
Daniel Veillard1e774382002-03-06 17:35:40 +00002742 * Serialization front-end *
2743 * *
2744 ************************************************************************/
2745
Daniel Veillardd2379012002-03-15 22:24:56 +00002746static PyObject *
2747libxml_serializeNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2748{
Daniel Veillard1e774382002-03-06 17:35:40 +00002749 PyObject *py_retval = NULL;
2750 xmlChar *c_retval;
2751 PyObject *pyobj_node;
2752 xmlNodePtr node;
2753 xmlDocPtr doc;
Daniel Veillardd2379012002-03-15 22:24:56 +00002754 const char *encoding;
Daniel Veillard1e774382002-03-06 17:35:40 +00002755 int format;
2756 int len;
2757
Daniel Veillardd2379012002-03-15 22:24:56 +00002758 if (!PyArg_ParseTuple(args, (char *) "Ozi:serializeNode", &pyobj_node,
2759 &encoding, &format))
2760 return (NULL);
Daniel Veillard1e774382002-03-06 17:35:40 +00002761 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2762
2763 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002764 Py_INCREF(Py_None);
2765 return (Py_None);
Daniel Veillard1e774382002-03-06 17:35:40 +00002766 }
2767 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002768 doc = (xmlDocPtr) node;
2769 xmlDocDumpFormatMemoryEnc(doc, &c_retval, &len,
2770 (const char *) encoding, format);
2771 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard656ce942004-04-30 23:11:45 +00002772#ifdef LIBXML_HTML_ENABLED
Daniel Veillard1e774382002-03-06 17:35:40 +00002773 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002774 xmlOutputBufferPtr buf;
2775 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00002776
Daniel Veillardd2379012002-03-15 22:24:56 +00002777 doc = (xmlDocPtr) node;
2778 if (encoding != NULL)
2779 htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
2780 encoding = (const char *) htmlGetMetaEncoding(doc);
Daniel Veillard1e774382002-03-06 17:35:40 +00002781
Daniel Veillardd2379012002-03-15 22:24:56 +00002782 if (encoding != NULL) {
2783 handler = xmlFindCharEncodingHandler(encoding);
2784 if (handler == NULL) {
2785 Py_INCREF(Py_None);
2786 return (Py_None);
2787 }
2788 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002789
Daniel Veillardd2379012002-03-15 22:24:56 +00002790 /*
2791 * Fallback to HTML or ASCII when the encoding is unspecified
2792 */
2793 if (handler == NULL)
2794 handler = xmlFindCharEncodingHandler("HTML");
2795 if (handler == NULL)
2796 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00002797
Daniel Veillardd2379012002-03-15 22:24:56 +00002798 buf = xmlAllocOutputBuffer(handler);
2799 if (buf == NULL) {
2800 Py_INCREF(Py_None);
2801 return (Py_None);
2802 }
2803 htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
2804 xmlOutputBufferFlush(buf);
2805 if (buf->conv != NULL) {
2806 len = buf->conv->use;
2807 c_retval = buf->conv->content;
2808 buf->conv->content = NULL;
2809 } else {
2810 len = buf->buffer->use;
2811 c_retval = buf->buffer->content;
2812 buf->buffer->content = NULL;
2813 }
2814 (void) xmlOutputBufferClose(buf);
2815 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard656ce942004-04-30 23:11:45 +00002816#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard1e774382002-03-06 17:35:40 +00002817 } else {
William M. Brack95af5942004-02-08 04:12:49 +00002818 if (node->type == XML_NAMESPACE_DECL)
2819 doc = NULL;
2820 else
2821 doc = node->doc;
Daniel Veillarda8c0adb2002-11-17 22:37:35 +00002822 if ((doc == NULL) || (doc->type == XML_DOCUMENT_NODE)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002823 xmlOutputBufferPtr buf;
2824 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00002825
Daniel Veillardd2379012002-03-15 22:24:56 +00002826 if (encoding != NULL) {
2827 handler = xmlFindCharEncodingHandler(encoding);
2828 if (handler == NULL) {
2829 Py_INCREF(Py_None);
2830 return (Py_None);
2831 }
2832 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002833
Daniel Veillardd2379012002-03-15 22:24:56 +00002834 buf = xmlAllocOutputBuffer(handler);
2835 if (buf == NULL) {
2836 Py_INCREF(Py_None);
2837 return (Py_None);
2838 }
2839 xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
2840 xmlOutputBufferFlush(buf);
2841 if (buf->conv != NULL) {
2842 len = buf->conv->use;
2843 c_retval = buf->conv->content;
2844 buf->conv->content = NULL;
2845 } else {
2846 len = buf->buffer->use;
2847 c_retval = buf->buffer->content;
2848 buf->buffer->content = NULL;
2849 }
2850 (void) xmlOutputBufferClose(buf);
2851 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard656ce942004-04-30 23:11:45 +00002852#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002853 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2854 xmlOutputBufferPtr buf;
2855 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00002856
Daniel Veillardd2379012002-03-15 22:24:56 +00002857 if (encoding != NULL)
2858 htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
2859 encoding = (const char *) htmlGetMetaEncoding(doc);
2860 if (encoding != NULL) {
2861 handler = xmlFindCharEncodingHandler(encoding);
2862 if (handler == NULL) {
2863 Py_INCREF(Py_None);
2864 return (Py_None);
2865 }
2866 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002867
Daniel Veillardd2379012002-03-15 22:24:56 +00002868 /*
2869 * Fallback to HTML or ASCII when the encoding is unspecified
2870 */
2871 if (handler == NULL)
2872 handler = xmlFindCharEncodingHandler("HTML");
2873 if (handler == NULL)
2874 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00002875
Daniel Veillardd2379012002-03-15 22:24:56 +00002876 buf = xmlAllocOutputBuffer(handler);
2877 if (buf == NULL) {
2878 Py_INCREF(Py_None);
2879 return (Py_None);
2880 }
2881 htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
2882 xmlOutputBufferFlush(buf);
2883 if (buf->conv != NULL) {
2884 len = buf->conv->use;
2885 c_retval = buf->conv->content;
2886 buf->conv->content = NULL;
2887 } else {
2888 len = buf->buffer->use;
2889 c_retval = buf->buffer->content;
2890 buf->buffer->content = NULL;
2891 }
2892 (void) xmlOutputBufferClose(buf);
2893 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard656ce942004-04-30 23:11:45 +00002894#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2379012002-03-15 22:24:56 +00002895 } else {
2896 Py_INCREF(Py_None);
2897 return (Py_None);
2898 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002899 }
Daniel Veillardd2379012002-03-15 22:24:56 +00002900 return (py_retval);
Daniel Veillard1e774382002-03-06 17:35:40 +00002901}
2902
Daniel Veillardd2379012002-03-15 22:24:56 +00002903static PyObject *
2904libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2905{
Daniel Veillard1e774382002-03-06 17:35:40 +00002906 PyObject *py_file = NULL;
2907 FILE *output;
2908 PyObject *pyobj_node;
2909 xmlNodePtr node;
2910 xmlDocPtr doc;
Daniel Veillardd2379012002-03-15 22:24:56 +00002911 const char *encoding;
Daniel Veillard1e774382002-03-06 17:35:40 +00002912 int format;
2913 int len;
2914 xmlOutputBufferPtr buf;
2915 xmlCharEncodingHandlerPtr handler = NULL;
2916
Daniel Veillardd2379012002-03-15 22:24:56 +00002917 if (!PyArg_ParseTuple(args, (char *) "OOzi:serializeNode", &pyobj_node,
2918 &py_file, &encoding, &format))
2919 return (NULL);
Daniel Veillard1e774382002-03-06 17:35:40 +00002920 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2921
2922 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002923 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002924 }
2925 if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002926 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002927 }
2928 output = PyFile_AsFile(py_file);
2929 if (output == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002930 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002931 }
2932
2933 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002934 doc = (xmlDocPtr) node;
Daniel Veillard1e774382002-03-06 17:35:40 +00002935 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002936 doc = (xmlDocPtr) node;
Daniel Veillard1e774382002-03-06 17:35:40 +00002937 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00002938 doc = node->doc;
Daniel Veillard1e774382002-03-06 17:35:40 +00002939 }
Daniel Veillard656ce942004-04-30 23:11:45 +00002940#ifdef LIBXML_HTML_ENABLED
Daniel Veillard1e774382002-03-06 17:35:40 +00002941 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002942 if (encoding == NULL)
2943 encoding = (const char *) htmlGetMetaEncoding(doc);
Daniel Veillard1e774382002-03-06 17:35:40 +00002944 }
Daniel Veillard656ce942004-04-30 23:11:45 +00002945#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard1e774382002-03-06 17:35:40 +00002946 if (encoding != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002947 handler = xmlFindCharEncodingHandler(encoding);
2948 if (handler == NULL) {
2949 return (PyInt_FromLong((long) -1));
2950 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002951 }
2952 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002953 if (handler == NULL)
2954 handler = xmlFindCharEncodingHandler("HTML");
2955 if (handler == NULL)
2956 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00002957 }
2958
2959 buf = xmlOutputBufferCreateFile(output, handler);
2960 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002961 len = xmlSaveFormatFileTo(buf, doc, encoding, format);
Daniel Veillard656ce942004-04-30 23:11:45 +00002962#ifdef LIBXML_HTML_ENABLED
Daniel Veillard1e774382002-03-06 17:35:40 +00002963 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002964 htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
2965 len = xmlOutputBufferClose(buf);
Daniel Veillard1e774382002-03-06 17:35:40 +00002966 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002967 htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
2968 len = xmlOutputBufferClose(buf);
Daniel Veillard656ce942004-04-30 23:11:45 +00002969#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard1e774382002-03-06 17:35:40 +00002970 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00002971 xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
2972 len = xmlOutputBufferClose(buf);
Daniel Veillard1e774382002-03-06 17:35:40 +00002973 }
Daniel Veillardd2379012002-03-15 22:24:56 +00002974 return (PyInt_FromLong((long) len));
Daniel Veillard1e774382002-03-06 17:35:40 +00002975}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002976#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard1e774382002-03-06 17:35:40 +00002977
2978/************************************************************************
2979 * *
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002980 * Extra stuff *
2981 * *
2982 ************************************************************************/
2983PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002984libxml_xmlNewNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2985{
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002986 PyObject *py_retval;
Daniel Veillardd2379012002-03-15 22:24:56 +00002987 xmlChar *name;
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002988 xmlNodePtr node;
2989
Daniel Veillardd2379012002-03-15 22:24:56 +00002990 if (!PyArg_ParseTuple(args, (char *) "s:xmlNewNode", &name))
2991 return (NULL);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002992 node = (xmlNodePtr) xmlNewNode(NULL, name);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00002993#ifdef DEBUG
Daniel Veillardd2379012002-03-15 22:24:56 +00002994 printf("NewNode: %s : %p\n", name, (void *) node);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00002995#endif
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002996
2997 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002998 Py_INCREF(Py_None);
2999 return (Py_None);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00003000 }
3001 py_retval = libxml_xmlNodePtrWrap(node);
Daniel Veillardd2379012002-03-15 22:24:56 +00003002 return (py_retval);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00003003}
3004
Daniel Veillard54396242003-04-23 07:36:50 +00003005
3006/************************************************************************
3007 * *
3008 * Local Catalog stuff *
3009 * *
3010 ************************************************************************/
3011static PyObject *
3012libxml_addLocalCatalog(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3013{
3014 xmlChar *URL;
3015 xmlParserCtxtPtr ctxt;
3016 PyObject *pyobj_ctxt;
3017
3018 if (!PyArg_ParseTuple(args, (char *)"Os:addLocalCatalog", &pyobj_ctxt, &URL))
3019 return(NULL);
3020
3021 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
3022
3023 if (URL != NULL) {
3024 ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL);
3025 }
3026
3027#ifdef DEBUG
3028 printf("LocalCatalog: %s\n", URL);
3029#endif
3030
3031 Py_INCREF(Py_None);
3032 return (Py_None);
3033}
3034
Daniel Veillardc2664642003-07-29 20:44:53 +00003035#ifdef LIBXML_SCHEMAS_ENABLED
3036
3037/************************************************************************
3038 * *
3039 * RelaxNG error handler registration *
3040 * *
3041 ************************************************************************/
3042
3043typedef struct
3044{
3045 PyObject *warn;
3046 PyObject *error;
3047 PyObject *arg;
3048} xmlRelaxNGValidCtxtPyCtxt;
3049typedef xmlRelaxNGValidCtxtPyCtxt *xmlRelaxNGValidCtxtPyCtxtPtr;
3050
3051static void
3052libxml_xmlRelaxNGValidityGenericErrorFuncHandler(void *ctx, char *str)
3053{
3054 PyObject *list;
3055 PyObject *result;
3056 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3057
3058#ifdef DEBUG_ERROR
3059 printf("libxml_xmlRelaxNGValidityGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
3060#endif
3061
3062 pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx;
3063
3064 list = PyTuple_New(2);
3065 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3066 PyTuple_SetItem(list, 1, pyCtxt->arg);
3067 Py_XINCREF(pyCtxt->arg);
3068 result = PyEval_CallObject(pyCtxt->error, list);
3069 if (result == NULL)
3070 {
3071 /* TODO: manage for the exception to be propagated... */
3072 PyErr_Print();
3073 }
3074 Py_XDECREF(list);
3075 Py_XDECREF(result);
3076}
3077
3078static void
3079libxml_xmlRelaxNGValidityGenericWarningFuncHandler(void *ctx, char *str)
3080{
3081 PyObject *list;
3082 PyObject *result;
3083 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3084
3085#ifdef DEBUG_ERROR
3086 printf("libxml_xmlRelaxNGValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str);
3087#endif
3088
3089 pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx;
3090
3091 list = PyTuple_New(2);
3092 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3093 PyTuple_SetItem(list, 1, pyCtxt->arg);
3094 Py_XINCREF(pyCtxt->arg);
3095 result = PyEval_CallObject(pyCtxt->warn, list);
3096 if (result == NULL)
3097 {
3098 /* TODO: manage for the exception to be propagated... */
3099 PyErr_Print();
3100 }
3101 Py_XDECREF(list);
3102 Py_XDECREF(result);
3103}
3104
3105static void
3106libxml_xmlRelaxNGValidityErrorFunc(void *ctx, const char *msg, ...)
3107{
3108 va_list ap;
3109
3110 va_start(ap, msg);
3111 libxml_xmlRelaxNGValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap));
3112 va_end(ap);
3113}
3114
3115static void
3116libxml_xmlRelaxNGValidityWarningFunc(void *ctx, const char *msg, ...)
3117{
3118 va_list ap;
3119
3120 va_start(ap, msg);
3121 libxml_xmlRelaxNGValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap));
3122 va_end(ap);
3123}
3124
3125static PyObject *
3126libxml_xmlRelaxNGSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3127{
3128 PyObject *py_retval;
3129 PyObject *pyobj_error;
3130 PyObject *pyobj_warn;
3131 PyObject *pyobj_ctx;
3132 PyObject *pyobj_arg = Py_None;
3133 xmlRelaxNGValidCtxtPtr ctxt;
3134 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3135
3136 if (!PyArg_ParseTuple
3137 (args, (char *) "OOO|O:xmlRelaxNGSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
3138 return (NULL);
3139
3140#ifdef DEBUG_ERROR
3141 printf("libxml_xmlRelaxNGSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
3142#endif
3143
3144 ctxt = PyrelaxNgValidCtxt_Get(pyobj_ctx);
William M. Brackc1939562003-08-05 15:52:22 +00003145 if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1)
Daniel Veillardc2664642003-07-29 20:44:53 +00003146 {
3147 py_retval = libxml_intWrap(-1);
3148 return(py_retval);
3149 }
3150
3151 if (pyCtxt == NULL)
3152 {
3153 /* first time to set the error handlers */
3154 pyCtxt = xmlMalloc(sizeof(xmlRelaxNGValidCtxtPyCtxt));
3155 if (pyCtxt == NULL) {
3156 py_retval = libxml_intWrap(-1);
3157 return(py_retval);
3158 }
3159 memset(pyCtxt, 0, sizeof(xmlRelaxNGValidCtxtPyCtxt));
3160 }
3161
3162 /* TODO: check warn and error is a function ! */
3163 Py_XDECREF(pyCtxt->error);
3164 Py_XINCREF(pyobj_error);
3165 pyCtxt->error = pyobj_error;
3166
3167 Py_XDECREF(pyCtxt->warn);
3168 Py_XINCREF(pyobj_warn);
3169 pyCtxt->warn = pyobj_warn;
3170
3171 Py_XDECREF(pyCtxt->arg);
3172 Py_XINCREF(pyobj_arg);
3173 pyCtxt->arg = pyobj_arg;
3174
3175 xmlRelaxNGSetValidErrors(ctxt, &libxml_xmlRelaxNGValidityErrorFunc, &libxml_xmlRelaxNGValidityWarningFunc, pyCtxt);
3176
3177 py_retval = libxml_intWrap(1);
3178 return (py_retval);
3179}
3180
3181static PyObject *
3182libxml_xmlRelaxNGFreeValidCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
3183 xmlRelaxNGValidCtxtPtr ctxt;
3184 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3185 PyObject *pyobj_ctxt;
3186
3187 if (!PyArg_ParseTuple(args, (char *)"O:xmlRelaxNGFreeValidCtxt", &pyobj_ctxt))
3188 return(NULL);
3189 ctxt = (xmlRelaxNGValidCtxtPtr) PyrelaxNgValidCtxt_Get(pyobj_ctxt);
3190
William M. Brackc1939562003-08-05 15:52:22 +00003191 if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0)
Daniel Veillardc2664642003-07-29 20:44:53 +00003192 {
3193 if (pyCtxt != NULL)
3194 {
3195 Py_XDECREF(pyCtxt->error);
3196 Py_XDECREF(pyCtxt->warn);
3197 Py_XDECREF(pyCtxt->arg);
3198 xmlFree(pyCtxt);
3199 }
3200 }
3201
3202 xmlRelaxNGFreeValidCtxt(ctxt);
3203 Py_INCREF(Py_None);
3204 return(Py_None);
3205}
3206
Daniel Veillard259f0df2004-08-18 09:13:18 +00003207typedef struct
3208{
3209 PyObject *warn;
3210 PyObject *error;
3211 PyObject *arg;
3212} xmlSchemaValidCtxtPyCtxt;
3213typedef xmlSchemaValidCtxtPyCtxt *xmlSchemaValidCtxtPyCtxtPtr;
3214
3215static void
3216libxml_xmlSchemaValidityGenericErrorFuncHandler(void *ctx, char *str)
3217{
3218 PyObject *list;
3219 PyObject *result;
3220 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3221
3222#ifdef DEBUG_ERROR
3223 printf("libxml_xmlSchemaValiditiyGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
3224#endif
3225
3226 pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx;
3227
3228 list = PyTuple_New(2);
3229 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3230 PyTuple_SetItem(list, 1, pyCtxt->arg);
3231 Py_XINCREF(pyCtxt->arg);
3232 result = PyEval_CallObject(pyCtxt->error, list);
3233 if (result == NULL)
3234 {
3235 /* TODO: manage for the exception to be propagated... */
3236 PyErr_Print();
3237 }
3238 Py_XDECREF(list);
3239 Py_XDECREF(result);
3240}
3241
3242static void
3243libxml_xmlSchemaValidityGenericWarningFuncHandler(void *ctx, char *str)
3244{
3245 PyObject *list;
3246 PyObject *result;
3247 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3248
3249#ifdef DEBUG_ERROR
3250 printf("libxml_xmlSchemaValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str);
3251#endif
3252
3253 pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx;
3254
3255 list = PyTuple_New(2);
3256 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3257 PyTuple_SetItem(list, 1, pyCtxt->arg);
3258 Py_XINCREF(pyCtxt->arg);
3259 result = PyEval_CallObject(pyCtxt->warn, list);
3260 if (result == NULL)
3261 {
3262 /* TODO: manage for the exception to be propagated... */
3263 PyErr_Print();
3264 }
3265 Py_XDECREF(list);
3266 Py_XDECREF(result);
3267}
3268
3269static void
3270libxml_xmlSchemaValidityErrorFunc(void *ctx, const char *msg, ...)
3271{
3272 va_list ap;
3273
3274 va_start(ap, msg);
3275 libxml_xmlSchemaValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap));
3276 va_end(ap);
3277}
3278
3279static void
3280libxml_xmlSchemaValidityWarningFunc(void *ctx, const char *msg, ...)
3281{
3282 va_list ap;
3283
3284 va_start(ap, msg);
3285 libxml_xmlSchemaValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap));
3286 va_end(ap);
3287}
3288
Daniel Veillard6ebf3c42004-08-22 13:11:39 +00003289PyObject *
Daniel Veillard259f0df2004-08-18 09:13:18 +00003290libxml_xmlSchemaSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3291{
3292 PyObject *py_retval;
3293 PyObject *pyobj_error;
3294 PyObject *pyobj_warn;
3295 PyObject *pyobj_ctx;
3296 PyObject *pyobj_arg = Py_None;
3297 xmlSchemaValidCtxtPtr ctxt;
3298 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3299
3300 if (!PyArg_ParseTuple
3301 (args, (char *) "OOO|O:xmlSchemaSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
3302 return (NULL);
3303
3304#ifdef DEBUG_ERROR
3305 printf("libxml_xmlSchemaSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
3306#endif
3307
3308 ctxt = PySchemaValidCtxt_Get(pyobj_ctx);
3309 if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1)
3310 {
3311 py_retval = libxml_intWrap(-1);
3312 return(py_retval);
3313 }
3314
3315 if (pyCtxt == NULL)
3316 {
3317 /* first time to set the error handlers */
3318 pyCtxt = xmlMalloc(sizeof(xmlSchemaValidCtxtPyCtxt));
3319 if (pyCtxt == NULL) {
3320 py_retval = libxml_intWrap(-1);
3321 return(py_retval);
3322 }
3323 memset(pyCtxt, 0, sizeof(xmlSchemaValidCtxtPyCtxt));
3324 }
3325
3326 /* TODO: check warn and error is a function ! */
3327 Py_XDECREF(pyCtxt->error);
3328 Py_XINCREF(pyobj_error);
3329 pyCtxt->error = pyobj_error;
3330
3331 Py_XDECREF(pyCtxt->warn);
3332 Py_XINCREF(pyobj_warn);
3333 pyCtxt->warn = pyobj_warn;
3334
3335 Py_XDECREF(pyCtxt->arg);
3336 Py_XINCREF(pyobj_arg);
3337 pyCtxt->arg = pyobj_arg;
3338
3339 xmlSchemaSetValidErrors(ctxt, &libxml_xmlSchemaValidityErrorFunc, &libxml_xmlSchemaValidityWarningFunc, pyCtxt);
3340
3341 py_retval = libxml_intWrap(1);
3342 return(py_retval);
3343}
3344
Daniel Veillardbb8502c2005-03-30 07:40:35 +00003345static PyObject *
Daniel Veillard259f0df2004-08-18 09:13:18 +00003346libxml_xmlSchemaFreeValidCtxt(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3347{
3348 xmlSchemaValidCtxtPtr ctxt;
3349 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3350 PyObject *pyobj_ctxt;
3351
3352 if (!PyArg_ParseTuple(args, (char *)"O:xmlSchemaFreeValidCtxt", &pyobj_ctxt))
3353 return(NULL);
3354 ctxt = (xmlSchemaValidCtxtPtr) PySchemaValidCtxt_Get(pyobj_ctxt);
3355
3356 if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0)
3357 {
3358 if (pyCtxt != NULL)
3359 {
3360 Py_XDECREF(pyCtxt->error);
3361 Py_XDECREF(pyCtxt->warn);
3362 Py_XDECREF(pyCtxt->arg);
3363 xmlFree(pyCtxt);
3364 }
3365 }
3366
3367 xmlSchemaFreeValidCtxt(ctxt);
3368 Py_INCREF(Py_None);
3369 return(Py_None);
3370}
3371
Daniel Veillardc2664642003-07-29 20:44:53 +00003372#endif
3373
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003374#ifdef LIBXML_C14N_ENABLED
3375#ifdef LIBXML_OUTPUT_ENABLED
3376
3377/************************************************************************
3378 * *
3379 * XML Canonicalization c14n *
3380 * *
3381 ************************************************************************/
3382
3383static int
3384PyxmlNodeSet_Convert(PyObject *py_nodeset, xmlNodeSetPtr *result)
3385{
3386 xmlNodeSetPtr nodeSet;
3387 int is_tuple = 0;
3388
3389 if (PyTuple_Check(py_nodeset))
3390 is_tuple = 1;
3391 else if (PyList_Check(py_nodeset))
3392 is_tuple = 0;
3393 else if (py_nodeset == Py_None) {
3394 *result = NULL;
3395 return 0;
3396 }
3397 else {
3398 PyErr_SetString(PyExc_TypeError,
3399 "must be a tuple or list of nodes.");
3400 return -1;
3401 }
3402
3403 nodeSet = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet));
3404 if (nodeSet == NULL) {
3405 PyErr_SetString(PyExc_MemoryError, "");
3406 return -1;
3407 }
3408
3409 nodeSet->nodeNr = 0;
3410 nodeSet->nodeMax = (is_tuple
3411 ? PyTuple_GET_SIZE(py_nodeset)
3412 : PyList_GET_SIZE(py_nodeset));
3413 nodeSet->nodeTab
3414 = (xmlNodePtr *) xmlMalloc (nodeSet->nodeMax
3415 * sizeof(xmlNodePtr));
3416 if (nodeSet->nodeTab == NULL) {
3417 xmlFree(nodeSet);
3418 PyErr_SetString(PyExc_MemoryError, "");
3419 return -1;
3420 }
3421 memset(nodeSet->nodeTab, 0 ,
3422 nodeSet->nodeMax * sizeof(xmlNodePtr));
3423
3424 {
3425 int idx;
3426 for (idx=0; idx < nodeSet->nodeMax; ++idx) {
3427 xmlNodePtr pynode =
3428 PyxmlNode_Get (is_tuple
3429 ? PyTuple_GET_ITEM(py_nodeset, idx)
3430 : PyList_GET_ITEM(py_nodeset, idx));
3431 if (pynode)
3432 nodeSet->nodeTab[nodeSet->nodeNr++] = pynode;
3433 }
3434 }
3435 *result = nodeSet;
3436 return 0;
3437}
3438
3439static int
3440PystringSet_Convert(PyObject *py_strings, xmlChar *** result)
3441{
3442 /* NOTE: the array should be freed, but the strings are shared
3443 with the python strings and so must not be freed. */
3444
3445 xmlChar ** strings;
3446 int is_tuple = 0;
3447 int count;
3448 int init_index = 0;
3449
3450 if (PyTuple_Check(py_strings))
3451 is_tuple = 1;
3452 else if (PyList_Check(py_strings))
3453 is_tuple = 0;
3454 else if (py_strings == Py_None) {
3455 *result = NULL;
3456 return 0;
3457 }
3458 else {
3459 PyErr_SetString(PyExc_TypeError,
3460 "must be a tuple or list of strings.");
3461 return -1;
3462 }
3463
3464 count = (is_tuple
3465 ? PyTuple_GET_SIZE(py_strings)
3466 : PyList_GET_SIZE(py_strings));
3467
3468 strings = (xmlChar **) xmlMalloc(sizeof(xmlChar *) * count);
3469
3470 if (strings == NULL) {
3471 PyErr_SetString(PyExc_MemoryError, "");
3472 return -1;
3473 }
3474
3475 memset(strings, 0 , sizeof(xmlChar *) * count);
3476
3477 {
3478 int idx;
3479 for (idx=0; idx < count; ++idx) {
3480 char* s = PyString_AsString
3481 (is_tuple
3482 ? PyTuple_GET_ITEM(py_strings, idx)
3483 : PyList_GET_ITEM(py_strings, idx));
3484 if (s)
3485 strings[init_index++] = (xmlChar *)s;
3486 else {
3487 xmlFree(strings);
3488 PyErr_SetString(PyExc_TypeError,
3489 "must be a tuple or list of strings.");
3490 return -1;
3491 }
3492 }
3493 }
3494
3495 *result = strings;
3496 return 0;
3497}
3498
3499static PyObject *
3500libxml_C14NDocDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
3501 PyObject * args)
3502{
3503 PyObject *py_retval = NULL;
3504
3505 PyObject *pyobj_doc;
3506 PyObject *pyobj_nodes;
3507 int exclusive;
3508 PyObject *pyobj_prefixes;
3509 int with_comments;
3510
3511 xmlDocPtr doc;
3512 xmlNodeSetPtr nodes;
3513 xmlChar **prefixes = NULL;
3514 xmlChar *doc_txt;
3515
3516 int result;
3517
3518 if (!PyArg_ParseTuple(args, (char *) "OOiOi:C14NDocDumpMemory",
3519 &pyobj_doc,
3520 &pyobj_nodes,
3521 &exclusive,
3522 &pyobj_prefixes,
3523 &with_comments))
3524 return (NULL);
3525
3526 doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
3527 if (!doc) {
3528 PyErr_SetString(PyExc_TypeError, "bad document.");
3529 return NULL;
3530 }
3531
3532 result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes);
3533 if (result < 0) return NULL;
3534
3535 if (exclusive) {
3536 result = PystringSet_Convert(pyobj_prefixes, &prefixes);
3537 if (result < 0) {
3538 if (nodes) {
3539 xmlFree(nodes->nodeTab);
3540 xmlFree(nodes);
3541 }
3542 return NULL;
3543 }
3544 }
3545
3546 result = xmlC14NDocDumpMemory(doc,
3547 nodes,
3548 exclusive,
3549 prefixes,
3550 with_comments,
3551 &doc_txt);
3552
3553 if (nodes) {
3554 xmlFree(nodes->nodeTab);
3555 xmlFree(nodes);
3556 }
3557 if (prefixes) {
3558 xmlChar ** idx = prefixes;
3559 while (*idx) xmlFree(*(idx++));
3560 xmlFree(prefixes);
3561 }
3562
3563 if (result < 0) {
3564 PyErr_SetString(PyExc_Exception,
3565 "libxml2 xmlC14NDocDumpMemory failure.");
3566 return NULL;
3567 }
3568 else {
3569 py_retval = PyString_FromStringAndSize((const char *) doc_txt,
3570 result);
3571 xmlFree(doc_txt);
3572 return py_retval;
3573 }
3574}
3575
3576static PyObject *
3577libxml_C14NDocSaveTo(ATTRIBUTE_UNUSED PyObject * self,
3578 PyObject * args)
3579{
3580 PyObject *pyobj_doc;
3581 PyObject *py_file;
3582 PyObject *pyobj_nodes;
3583 int exclusive;
3584 PyObject *pyobj_prefixes;
3585 int with_comments;
3586
3587 xmlDocPtr doc;
3588 xmlNodeSetPtr nodes;
3589 xmlChar **prefixes = NULL;
3590 FILE * output;
3591 xmlOutputBufferPtr buf;
3592
3593 int result;
3594 int len;
3595
3596 if (!PyArg_ParseTuple(args, (char *) "OOiOiO:C14NDocSaveTo",
3597 &pyobj_doc,
3598 &pyobj_nodes,
3599 &exclusive,
3600 &pyobj_prefixes,
3601 &with_comments,
3602 &py_file))
3603 return (NULL);
3604
3605 doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
3606 if (!doc) {
3607 PyErr_SetString(PyExc_TypeError, "bad document.");
3608 return NULL;
3609 }
3610
3611 if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
3612 PyErr_SetString(PyExc_TypeError, "bad file.");
3613 return NULL;
3614 }
3615 output = PyFile_AsFile(py_file);
3616 if (output == NULL) {
3617 PyErr_SetString(PyExc_TypeError, "bad file.");
3618 return NULL;
3619 }
3620 buf = xmlOutputBufferCreateFile(output, NULL);
3621
3622 result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes);
3623 if (result < 0) return NULL;
3624
3625 if (exclusive) {
3626 result = PystringSet_Convert(pyobj_prefixes, &prefixes);
3627 if (result < 0) {
3628 if (nodes) {
3629 xmlFree(nodes->nodeTab);
3630 xmlFree(nodes);
3631 }
3632 return NULL;
3633 }
3634 }
3635
3636 result = xmlC14NDocSaveTo(doc,
3637 nodes,
3638 exclusive,
3639 prefixes,
3640 with_comments,
3641 buf);
3642
3643 if (nodes) {
3644 xmlFree(nodes->nodeTab);
3645 xmlFree(nodes);
3646 }
3647 if (prefixes) {
3648 xmlChar ** idx = prefixes;
3649 while (*idx) xmlFree(*(idx++));
3650 xmlFree(prefixes);
3651 }
3652
3653 len = xmlOutputBufferClose(buf);
3654
3655 if (result < 0) {
3656 PyErr_SetString(PyExc_Exception,
3657 "libxml2 xmlC14NDocSaveTo failure.");
3658 return NULL;
3659 }
3660 else
3661 return PyInt_FromLong((long) len);
3662}
3663
3664#endif
3665#endif
3666
William M. Brackc68d78d2004-07-16 10:39:30 +00003667static PyObject *
3668libxml_getObjDesc(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003669
William M. Brackc68d78d2004-07-16 10:39:30 +00003670 PyObject *obj;
3671 char *str;
3672
3673 if (!PyArg_ParseTuple(args, (char *)"O:getObjDesc", &obj))
3674 return NULL;
3675 str = PyCObject_GetDesc(obj);
3676 return Py_BuildValue((char *)"s", str);
3677}
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003678
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00003679/************************************************************************
3680 * *
Daniel Veillardd2897fd2002-01-30 16:37:32 +00003681 * The registration stuff *
3682 * *
3683 ************************************************************************/
3684static PyMethodDef libxmlMethods[] = {
Daniel Veillard96fe0952002-01-30 20:52:23 +00003685#include "libxml2-export.c"
Daniel Veillardd2379012002-03-15 22:24:56 +00003686 {(char *) "name", libxml_name, METH_VARARGS, NULL},
3687 {(char *) "children", libxml_children, METH_VARARGS, NULL},
3688 {(char *) "properties", libxml_properties, METH_VARARGS, NULL},
3689 {(char *) "last", libxml_last, METH_VARARGS, NULL},
3690 {(char *) "prev", libxml_prev, METH_VARARGS, NULL},
3691 {(char *) "next", libxml_next, METH_VARARGS, NULL},
3692 {(char *) "parent", libxml_parent, METH_VARARGS, NULL},
3693 {(char *) "type", libxml_type, METH_VARARGS, NULL},
3694 {(char *) "doc", libxml_doc, METH_VARARGS, NULL},
3695 {(char *) "xmlNewNode", libxml_xmlNewNode, METH_VARARGS, NULL},
Daniel Veillardf9cf6f52005-04-12 01:02:29 +00003696 {(char *) "xmlNodeRemoveNsDef", libxml_xmlNodeRemoveNsDef, METH_VARARGS, NULL},
Daniel Veillard850ce9b2004-11-10 11:55:47 +00003697 {(char *)"xmlSetValidErrors", libxml_xmlSetValidErrors, METH_VARARGS, NULL},
Daniel Veillard25c90c52005-03-02 10:47:41 +00003698 {(char *)"xmlFreeValidCtxt", libxml_xmlFreeValidCtxt, METH_VARARGS, NULL},
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003699#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00003700 {(char *) "serializeNode", libxml_serializeNode, METH_VARARGS, NULL},
3701 {(char *) "saveNodeTo", libxml_saveNodeTo, METH_VARARGS, NULL},
Daniel Veillardc6d4a932002-09-12 15:00:57 +00003702 {(char *) "outputBufferCreate", libxml_xmlCreateOutputBuffer, METH_VARARGS, NULL},
Daniel Veillard6cbd6c02003-12-04 12:31:49 +00003703 {(char *) "outputBufferGetPythonFile", libxml_outputBufferGetPythonFile, METH_VARARGS, NULL},
3704 {(char *) "xmlOutputBufferClose", libxml_xmlOutputBufferClose, METH_VARARGS, NULL},
3705 { (char *)"xmlOutputBufferFlush", libxml_xmlOutputBufferFlush, METH_VARARGS, NULL },
Daniel Veillard263ec862004-10-04 10:26:54 +00003706 { (char *)"xmlSaveFileTo", libxml_xmlSaveFileTo, METH_VARARGS, NULL },
3707 { (char *)"xmlSaveFormatFileTo", libxml_xmlSaveFormatFileTo, METH_VARARGS, NULL },
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003708#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardc6d4a932002-09-12 15:00:57 +00003709 {(char *) "inputBufferCreate", libxml_xmlCreateInputBuffer, METH_VARARGS, NULL},
3710 {(char *) "setEntityLoader", libxml_xmlSetEntityLoader, METH_VARARGS, NULL},
Daniel Veillard3e20a292003-01-10 13:14:40 +00003711 {(char *)"xmlRegisterErrorHandler", libxml_xmlRegisterErrorHandler, METH_VARARGS, NULL },
Daniel Veillard417be3a2003-01-20 21:26:34 +00003712 {(char *)"xmlParserCtxtSetErrorHandler", libxml_xmlParserCtxtSetErrorHandler, METH_VARARGS, NULL },
3713 {(char *)"xmlParserCtxtGetErrorHandler", libxml_xmlParserCtxtGetErrorHandler, METH_VARARGS, NULL },
Daniel Veillarde6227e02003-01-14 11:42:39 +00003714 {(char *)"xmlFreeParserCtxt", libxml_xmlFreeParserCtxt, METH_VARARGS, NULL },
Daniel Veillard26f70262003-01-16 22:45:08 +00003715 {(char *)"xmlTextReaderSetErrorHandler", libxml_xmlTextReaderSetErrorHandler, METH_VARARGS, NULL },
3716 {(char *)"xmlTextReaderGetErrorHandler", libxml_xmlTextReaderGetErrorHandler, METH_VARARGS, NULL },
3717 {(char *)"xmlFreeTextReader", libxml_xmlFreeTextReader, METH_VARARGS, NULL },
Daniel Veillard54396242003-04-23 07:36:50 +00003718 {(char *)"addLocalCatalog", libxml_addLocalCatalog, METH_VARARGS, NULL },
Daniel Veillardc2664642003-07-29 20:44:53 +00003719#ifdef LIBXML_SCHEMAS_ENABLED
3720 {(char *)"xmlRelaxNGSetValidErrors", libxml_xmlRelaxNGSetValidErrors, METH_VARARGS, NULL},
3721 {(char *)"xmlRelaxNGFreeValidCtxt", libxml_xmlRelaxNGFreeValidCtxt, METH_VARARGS, NULL},
Daniel Veillardeff45a92004-10-29 12:10:55 +00003722 {(char *)"xmlSchemaSetValidErrors", libxml_xmlSchemaSetValidErrors, METH_VARARGS, NULL},
Daniel Veillardbb8502c2005-03-30 07:40:35 +00003723 {(char *)"xmlSchemaFreeValidCtxt", libxml_xmlSchemaFreeValidCtxt, METH_VARARGS, NULL},
Daniel Veillardc2664642003-07-29 20:44:53 +00003724#endif
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003725#ifdef LIBXML_C14N_ENABLED
3726#ifdef LIBXML_OUTPUT_ENABLED
3727 {(char *)"xmlC14NDocDumpMemory", libxml_C14NDocDumpMemory, METH_VARARGS, NULL},
3728 {(char *)"xmlC14NDocSaveTo", libxml_C14NDocSaveTo, METH_VARARGS, NULL},
3729#endif
3730#endif
William M. Brackc68d78d2004-07-16 10:39:30 +00003731 {(char *) "getObjDesc", libxml_getObjDesc, METH_VARARGS, NULL},
Daniel Veillardd2379012002-03-15 22:24:56 +00003732 {NULL, NULL, 0, NULL}
Daniel Veillardd2897fd2002-01-30 16:37:32 +00003733};
3734
Daniel Veillard0fea6f42002-02-22 22:51:13 +00003735#ifdef MERGED_MODULES
Daniel Veillard6361da02002-02-23 10:10:33 +00003736extern void initlibxsltmod(void);
Daniel Veillard0fea6f42002-02-22 22:51:13 +00003737#endif
3738
Daniel Veillardd2379012002-03-15 22:24:56 +00003739void
3740initlibxml2mod(void)
3741{
Daniel Veillardaf43f632002-03-08 15:05:20 +00003742 static int initialized = 0;
Daniel Veillardaf43f632002-03-08 15:05:20 +00003743
3744 if (initialized != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00003745 return;
William M. Brack8e2cc6f2004-07-03 23:28:52 +00003746
Daniel Veillardf93a8662004-07-01 12:56:30 +00003747 /* intialize the python extension module */
3748 Py_InitModule((char *) "libxml2mod", libxmlMethods);
3749
3750 /* initialize libxml2 */
3751 xmlInitParser();
Daniel Veillard5d819032002-02-02 21:49:17 +00003752 libxml_xmlErrorInitialize();
Daniel Veillard6361da02002-02-23 10:10:33 +00003753
Daniel Veillardf93a8662004-07-01 12:56:30 +00003754 initialized = 1;
3755
Daniel Veillard0fea6f42002-02-22 22:51:13 +00003756#ifdef MERGED_MODULES
3757 initlibxsltmod();
3758#endif
Daniel Veillardd2897fd2002-01-30 16:37:32 +00003759}