blob: b52099490603c8205f9bed45aae998e7614f82fc [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 }
684 } else {
685 printf("pythonExternalEntityLoader: can't read\n");
686 }
687 if (result == NULL) {
688 Py_DECREF(ret);
Daniel Veillardc64b8e92003-02-24 11:47:13 +0000689 } else if (URL != NULL) {
William M. Brackc1939562003-08-05 15:52:22 +0000690 result->filename = (char *) xmlStrdup((const xmlChar *)URL);
Daniel Veillardc64b8e92003-02-24 11:47:13 +0000691 result->directory = xmlParserGetDirectory((const char *) URL);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000692 }
693 }
694 }
695 if ((result == NULL) && (defaultExternalEntityLoader != NULL)) {
696 result = defaultExternalEntityLoader(URL, ID, ctxt);
697 }
698 return(result);
699}
700
701PyObject *
702libxml_xmlSetEntityLoader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
703 PyObject *py_retval;
704 PyObject *loader;
705
706 if (!PyArg_ParseTuple(args, (char *)"O:libxml_xmlSetEntityLoader",
707 &loader))
708 return(NULL);
709
710#ifdef DEBUG_LOADER
711 printf("libxml_xmlSetEntityLoader\n");
712#endif
713 if (defaultExternalEntityLoader == NULL)
714 defaultExternalEntityLoader = xmlGetExternalEntityLoader();
715
716 pythonExternalEntityLoaderObjext = loader;
717 xmlSetExternalEntityLoader(pythonExternalEntityLoader);
718
719 py_retval = PyInt_FromLong(0);
720 return(py_retval);
721}
722
723
724/************************************************************************
725 * *
Daniel Veillard3ce52572002-02-03 15:08:05 +0000726 * Handling SAX/xmllib/sgmlop callback interfaces *
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000727 * *
728 ************************************************************************/
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000729
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000730static void
731pythonStartElement(void *user_data, const xmlChar * name,
732 const xmlChar ** attrs)
733{
734 int i;
735 PyObject *handler;
736 PyObject *dict;
737 PyObject *attrname;
738 PyObject *attrvalue;
Daniel Veillardd2379012002-03-15 22:24:56 +0000739 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000740 int type = 0;
741
Daniel Veillard797a5652002-02-12 13:46:21 +0000742#ifdef DEBUG_SAX
743 printf("pythonStartElement(%s) called\n", name);
744#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000745 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000746 if (PyObject_HasAttrString(handler, (char *) "startElement"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000747 type = 1;
Daniel Veillardd2379012002-03-15 22:24:56 +0000748 else if (PyObject_HasAttrString(handler, (char *) "start"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000749 type = 2;
750 if (type != 0) {
751 /*
752 * the xmllib interface always generate a dictionnary,
753 * possibly empty
754 */
755 if ((attrs == NULL) && (type == 1)) {
756 Py_XINCREF(Py_None);
757 dict = Py_None;
Daniel Veillardd2379012002-03-15 22:24:56 +0000758 } else if (attrs == NULL) {
759 dict = PyDict_New();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000760 } else {
761 dict = PyDict_New();
762 for (i = 0; attrs[i] != NULL; i++) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000763 attrname = PyString_FromString((char *) attrs[i]);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000764 i++;
765 if (attrs[i] != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000766 attrvalue = PyString_FromString((char *) attrs[i]);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000767 } else {
768 Py_XINCREF(Py_None);
769 attrvalue = Py_None;
770 }
771 PyDict_SetItem(dict, attrname, attrvalue);
772 }
773 }
774
775 if (type == 1)
Daniel Veillardd2379012002-03-15 22:24:56 +0000776 result = PyObject_CallMethod(handler, (char *) "startElement",
777 (char *) "sO", name, dict);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000778 else if (type == 2)
Daniel Veillardd2379012002-03-15 22:24:56 +0000779 result = PyObject_CallMethod(handler, (char *) "start",
780 (char *) "sO", name, dict);
781 if (PyErr_Occurred())
782 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000783 Py_XDECREF(dict);
784 Py_XDECREF(result);
785 }
786}
787
788static void
789pythonStartDocument(void *user_data)
790{
791 PyObject *handler;
792 PyObject *result;
793
Daniel Veillard797a5652002-02-12 13:46:21 +0000794#ifdef DEBUG_SAX
795 printf("pythonStartDocument() called\n");
796#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000797 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000798 if (PyObject_HasAttrString(handler, (char *) "startDocument")) {
799 result =
800 PyObject_CallMethod(handler, (char *) "startDocument", NULL);
801 if (PyErr_Occurred())
802 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000803 Py_XDECREF(result);
804 }
805}
806
807static void
808pythonEndDocument(void *user_data)
809{
810 PyObject *handler;
811 PyObject *result;
812
Daniel Veillard797a5652002-02-12 13:46:21 +0000813#ifdef DEBUG_SAX
814 printf("pythonEndDocument() called\n");
815#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000816 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000817 if (PyObject_HasAttrString(handler, (char *) "endDocument")) {
818 result =
819 PyObject_CallMethod(handler, (char *) "endDocument", NULL);
820 if (PyErr_Occurred())
821 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000822 Py_XDECREF(result);
823 }
824 /*
825 * The reference to the handler is released there
826 */
827 Py_XDECREF(handler);
828}
829
830static void
831pythonEndElement(void *user_data, const xmlChar * name)
832{
833 PyObject *handler;
834 PyObject *result;
835
Daniel Veillard797a5652002-02-12 13:46:21 +0000836#ifdef DEBUG_SAX
837 printf("pythonEndElement(%s) called\n", name);
838#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000839 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000840 if (PyObject_HasAttrString(handler, (char *) "endElement")) {
841 result = PyObject_CallMethod(handler, (char *) "endElement",
842 (char *) "s", name);
843 if (PyErr_Occurred())
844 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000845 Py_XDECREF(result);
Daniel Veillardd2379012002-03-15 22:24:56 +0000846 } else if (PyObject_HasAttrString(handler, (char *) "end")) {
847 result = PyObject_CallMethod(handler, (char *) "end",
848 (char *) "s", name);
849 if (PyErr_Occurred())
850 PyErr_Print();
Daniel Veillard797a5652002-02-12 13:46:21 +0000851 Py_XDECREF(result);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000852 }
853}
854
855static void
856pythonReference(void *user_data, const xmlChar * name)
857{
858 PyObject *handler;
859 PyObject *result;
860
Daniel Veillard797a5652002-02-12 13:46:21 +0000861#ifdef DEBUG_SAX
862 printf("pythonReference(%s) called\n", name);
863#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000864 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000865 if (PyObject_HasAttrString(handler, (char *) "reference")) {
866 result = PyObject_CallMethod(handler, (char *) "reference",
867 (char *) "s", name);
868 if (PyErr_Occurred())
869 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000870 Py_XDECREF(result);
871 }
872}
873
874static void
875pythonCharacters(void *user_data, const xmlChar * ch, int len)
876{
877 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +0000878 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000879 int type = 0;
880
Daniel Veillard797a5652002-02-12 13:46:21 +0000881#ifdef DEBUG_SAX
882 printf("pythonCharacters(%s, %d) called\n", ch, len);
883#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000884 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000885 if (PyObject_HasAttrString(handler, (char *) "characters"))
886 type = 1;
887 else if (PyObject_HasAttrString(handler, (char *) "data"))
888 type = 2;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000889 if (type != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000890 if (type == 1)
891 result = PyObject_CallMethod(handler, (char *) "characters",
892 (char *) "s#", ch, len);
893 else if (type == 2)
894 result = PyObject_CallMethod(handler, (char *) "data",
895 (char *) "s#", ch, len);
896 if (PyErr_Occurred())
897 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000898 Py_XDECREF(result);
899 }
900}
901
902static void
903pythonIgnorableWhitespace(void *user_data, const xmlChar * ch, int len)
904{
905 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +0000906 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000907 int type = 0;
908
Daniel Veillard797a5652002-02-12 13:46:21 +0000909#ifdef DEBUG_SAX
910 printf("pythonIgnorableWhitespace(%s, %d) called\n", ch, len);
911#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000912 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000913 if (PyObject_HasAttrString(handler, (char *) "ignorableWhitespace"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000914 type = 1;
Daniel Veillardd2379012002-03-15 22:24:56 +0000915 else if (PyObject_HasAttrString(handler, (char *) "data"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000916 type = 2;
917 if (type != 0) {
918 if (type == 1)
919 result =
Daniel Veillardd2379012002-03-15 22:24:56 +0000920 PyObject_CallMethod(handler,
921 (char *) "ignorableWhitespace",
922 (char *) "s#", ch, len);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000923 else if (type == 2)
Daniel Veillardd2379012002-03-15 22:24:56 +0000924 result =
925 PyObject_CallMethod(handler, (char *) "data",
926 (char *) "s#", ch, len);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000927 Py_XDECREF(result);
928 }
929}
930
931static void
932pythonProcessingInstruction(void *user_data,
933 const xmlChar * target, const xmlChar * data)
934{
935 PyObject *handler;
936 PyObject *result;
937
Daniel Veillard797a5652002-02-12 13:46:21 +0000938#ifdef DEBUG_SAX
939 printf("pythonProcessingInstruction(%s, %s) called\n", target, data);
940#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000941 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000942 if (PyObject_HasAttrString(handler, (char *) "processingInstruction")) {
943 result = PyObject_CallMethod(handler, (char *)
944 "processingInstruction",
945 (char *) "ss", target, data);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000946 Py_XDECREF(result);
947 }
948}
949
950static void
951pythonComment(void *user_data, const xmlChar * value)
952{
953 PyObject *handler;
954 PyObject *result;
955
Daniel Veillard797a5652002-02-12 13:46:21 +0000956#ifdef DEBUG_SAX
957 printf("pythonComment(%s) called\n", value);
958#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000959 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000960 if (PyObject_HasAttrString(handler, (char *) "comment")) {
961 result =
962 PyObject_CallMethod(handler, (char *) "comment", (char *) "s",
963 value);
964 if (PyErr_Occurred())
965 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000966 Py_XDECREF(result);
967 }
968}
969
970static void
971pythonWarning(void *user_data, const char *msg, ...)
972{
973 PyObject *handler;
974 PyObject *result;
975 va_list args;
976 char buf[1024];
977
Daniel Veillard797a5652002-02-12 13:46:21 +0000978#ifdef DEBUG_SAX
979 printf("pythonWarning(%s) called\n", msg);
980#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000981 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000982 if (PyObject_HasAttrString(handler, (char *) "warning")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000983 va_start(args, msg);
984 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +0000985 va_end(args);
986 buf[1023] = 0;
987 result =
988 PyObject_CallMethod(handler, (char *) "warning", (char *) "s",
989 buf);
990 if (PyErr_Occurred())
991 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000992 Py_XDECREF(result);
993 }
994}
995
996static void
997pythonError(void *user_data, const char *msg, ...)
998{
999 PyObject *handler;
1000 PyObject *result;
1001 va_list args;
1002 char buf[1024];
1003
Daniel Veillard797a5652002-02-12 13:46:21 +00001004#ifdef DEBUG_SAX
1005 printf("pythonError(%s) called\n", msg);
1006#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001007 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001008 if (PyObject_HasAttrString(handler, (char *) "error")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001009 va_start(args, msg);
1010 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +00001011 va_end(args);
1012 buf[1023] = 0;
1013 result =
1014 PyObject_CallMethod(handler, (char *) "error", (char *) "s",
1015 buf);
1016 if (PyErr_Occurred())
1017 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001018 Py_XDECREF(result);
1019 }
1020}
1021
1022static void
1023pythonFatalError(void *user_data, const char *msg, ...)
1024{
1025 PyObject *handler;
1026 PyObject *result;
1027 va_list args;
1028 char buf[1024];
1029
Daniel Veillard797a5652002-02-12 13:46:21 +00001030#ifdef DEBUG_SAX
1031 printf("pythonFatalError(%s) called\n", msg);
1032#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001033 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001034 if (PyObject_HasAttrString(handler, (char *) "fatalError")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001035 va_start(args, msg);
1036 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +00001037 va_end(args);
1038 buf[1023] = 0;
1039 result =
1040 PyObject_CallMethod(handler, (char *) "fatalError",
1041 (char *) "s", buf);
1042 if (PyErr_Occurred())
1043 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001044 Py_XDECREF(result);
1045 }
1046}
1047
1048static void
1049pythonCdataBlock(void *user_data, const xmlChar * ch, int len)
1050{
1051 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +00001052 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001053 int type = 0;
1054
Daniel Veillard797a5652002-02-12 13:46:21 +00001055#ifdef DEBUG_SAX
1056 printf("pythonCdataBlock(%s, %d) called\n", ch, len);
1057#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001058 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001059 if (PyObject_HasAttrString(handler, (char *) "cdataBlock"))
1060 type = 1;
1061 else if (PyObject_HasAttrString(handler, (char *) "cdata"))
1062 type = 2;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001063 if (type != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001064 if (type == 1)
1065 result =
1066 PyObject_CallMethod(handler, (char *) "cdataBlock",
1067 (char *) "s#", ch, len);
1068 else if (type == 2)
1069 result =
1070 PyObject_CallMethod(handler, (char *) "cdata",
1071 (char *) "s#", ch, len);
1072 if (PyErr_Occurred())
1073 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001074 Py_XDECREF(result);
1075 }
1076}
1077
1078static void
1079pythonExternalSubset(void *user_data,
1080 const xmlChar * name,
1081 const xmlChar * externalID, const xmlChar * systemID)
1082{
1083 PyObject *handler;
1084 PyObject *result;
1085
Daniel Veillard797a5652002-02-12 13:46:21 +00001086#ifdef DEBUG_SAX
1087 printf("pythonExternalSubset(%s, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001088 name, externalID, systemID);
Daniel Veillard797a5652002-02-12 13:46:21 +00001089#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001090 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001091 if (PyObject_HasAttrString(handler, (char *) "externalSubset")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001092 result =
Daniel Veillardd2379012002-03-15 22:24:56 +00001093 PyObject_CallMethod(handler, (char *) "externalSubset",
1094 (char *) "sss", name, externalID,
1095 systemID);
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001096 Py_XDECREF(result);
1097 }
1098}
1099
1100static void
1101pythonEntityDecl(void *user_data,
1102 const xmlChar * name,
1103 int type,
1104 const xmlChar * publicId,
1105 const xmlChar * systemId, xmlChar * content)
1106{
1107 PyObject *handler;
1108 PyObject *result;
1109
1110 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001111 if (PyObject_HasAttrString(handler, (char *) "entityDecl")) {
1112 result = PyObject_CallMethod(handler, (char *) "entityDecl",
1113 (char *) "sisss", name, type,
1114 publicId, systemId, content);
1115 if (PyErr_Occurred())
1116 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001117 Py_XDECREF(result);
1118 }
1119}
1120
1121
1122
1123static void
1124
1125pythonNotationDecl(void *user_data,
1126 const xmlChar * name,
1127 const xmlChar * publicId, const xmlChar * systemId)
1128{
1129 PyObject *handler;
1130 PyObject *result;
1131
1132 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001133 if (PyObject_HasAttrString(handler, (char *) "notationDecl")) {
1134 result = PyObject_CallMethod(handler, (char *) "notationDecl",
1135 (char *) "sss", name, publicId,
1136 systemId);
1137 if (PyErr_Occurred())
1138 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001139 Py_XDECREF(result);
1140 }
1141}
1142
1143static void
1144pythonAttributeDecl(void *user_data,
1145 const xmlChar * elem,
1146 const xmlChar * name,
1147 int type,
1148 int def,
Daniel Veillardd2379012002-03-15 22:24:56 +00001149 const xmlChar * defaultValue, xmlEnumerationPtr tree)
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001150{
1151 PyObject *handler;
1152 PyObject *nameList;
1153 PyObject *newName;
1154 xmlEnumerationPtr node;
1155 PyObject *result;
1156 int count;
1157
1158 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001159 if (PyObject_HasAttrString(handler, (char *) "attributeDecl")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001160 count = 0;
1161 for (node = tree; node != NULL; node = node->next) {
1162 count++;
1163 }
1164 nameList = PyList_New(count);
1165 count = 0;
1166 for (node = tree; node != NULL; node = node->next) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001167 newName = PyString_FromString((char *) node->name);
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001168 PyList_SetItem(nameList, count, newName);
1169 count++;
1170 }
Daniel Veillardd2379012002-03-15 22:24:56 +00001171 result = PyObject_CallMethod(handler, (char *) "attributeDecl",
1172 (char *) "ssiisO", elem, name, type,
1173 def, defaultValue, nameList);
1174 if (PyErr_Occurred())
1175 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001176 Py_XDECREF(nameList);
1177 Py_XDECREF(result);
1178 }
1179}
1180
1181static void
1182pythonElementDecl(void *user_data,
1183 const xmlChar * name,
Daniel Veillardd2379012002-03-15 22:24:56 +00001184 int type, ATTRIBUTE_UNUSED xmlElementContentPtr content)
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001185{
1186 PyObject *handler;
1187 PyObject *obj;
1188 PyObject *result;
1189
1190 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001191 if (PyObject_HasAttrString(handler, (char *) "elementDecl")) {
1192 /* TODO: wrap in an elementContent object */
1193 printf
1194 ("pythonElementDecl: xmlElementContentPtr wrapper missing !\n");
1195 obj = Py_None;
1196 /* Py_XINCREF(Py_None); isn't the reference just borrowed ??? */
1197 result = PyObject_CallMethod(handler, (char *) "elementDecl",
1198 (char *) "siO", name, type, obj);
1199 if (PyErr_Occurred())
1200 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001201 Py_XDECREF(result);
1202 }
1203}
1204
1205static void
1206pythonUnparsedEntityDecl(void *user_data,
1207 const xmlChar * name,
1208 const xmlChar * publicId,
1209 const xmlChar * systemId,
1210 const xmlChar * notationName)
1211{
1212 PyObject *handler;
1213 PyObject *result;
1214
1215 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001216 if (PyObject_HasAttrString(handler, (char *) "unparsedEntityDecl")) {
1217 result =
1218 PyObject_CallMethod(handler, (char *) "unparsedEntityDecl",
1219 (char *) "ssss", name, publicId, systemId,
1220 notationName);
1221 if (PyErr_Occurred())
1222 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001223 Py_XDECREF(result);
1224 }
1225}
1226
1227static void
1228pythonInternalSubset(void *user_data, const xmlChar * name,
1229 const xmlChar * ExternalID, const xmlChar * SystemID)
1230{
1231 PyObject *handler;
1232 PyObject *result;
1233
Daniel Veillard797a5652002-02-12 13:46:21 +00001234#ifdef DEBUG_SAX
1235 printf("pythonInternalSubset(%s, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001236 name, ExternalID, SystemID);
Daniel Veillard797a5652002-02-12 13:46:21 +00001237#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001238 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001239 if (PyObject_HasAttrString(handler, (char *) "internalSubset")) {
1240 result = PyObject_CallMethod(handler, (char *) "internalSubset",
1241 (char *) "sss", name, ExternalID,
1242 SystemID);
1243 if (PyErr_Occurred())
1244 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001245 Py_XDECREF(result);
1246 }
1247}
1248
1249static xmlSAXHandler pythonSaxHandler = {
1250 pythonInternalSubset,
Daniel Veillardd2379012002-03-15 22:24:56 +00001251 NULL, /* TODO pythonIsStandalone, */
1252 NULL, /* TODO pythonHasInternalSubset, */
1253 NULL, /* TODO pythonHasExternalSubset, */
1254 NULL, /* TODO pythonResolveEntity, */
1255 NULL, /* TODO pythonGetEntity, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001256 pythonEntityDecl,
1257 pythonNotationDecl,
1258 pythonAttributeDecl,
1259 pythonElementDecl,
1260 pythonUnparsedEntityDecl,
Daniel Veillardd2379012002-03-15 22:24:56 +00001261 NULL, /* OBSOLETED pythonSetDocumentLocator, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001262 pythonStartDocument,
1263 pythonEndDocument,
1264 pythonStartElement,
1265 pythonEndElement,
1266 pythonReference,
1267 pythonCharacters,
1268 pythonIgnorableWhitespace,
1269 pythonProcessingInstruction,
1270 pythonComment,
1271 pythonWarning,
1272 pythonError,
1273 pythonFatalError,
Daniel Veillardd2379012002-03-15 22:24:56 +00001274 NULL, /* TODO pythonGetParameterEntity, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001275 pythonCdataBlock,
1276 pythonExternalSubset,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001277 1,
1278 NULL, /* TODO mograte to SAX2 */
1279 NULL,
William M. Brack871611b2003-10-18 04:53:14 +00001280 NULL,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001281 NULL
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001282};
Daniel Veillard3ce52572002-02-03 15:08:05 +00001283
1284/************************************************************************
1285 * *
1286 * Handling of specific parser context *
1287 * *
1288 ************************************************************************/
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001289
1290PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001291libxml_xmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
1292 PyObject * args)
1293{
1294 const char *chunk;
Daniel Veillard3ce52572002-02-03 15:08:05 +00001295 int size;
Daniel Veillardd2379012002-03-15 22:24:56 +00001296 const char *URI;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001297 PyObject *pyobj_SAX = NULL;
Daniel Veillard3ce52572002-02-03 15:08:05 +00001298 xmlSAXHandlerPtr SAX = NULL;
Daniel Veillard3ce52572002-02-03 15:08:05 +00001299 xmlParserCtxtPtr ret;
1300 PyObject *pyret;
Daniel Veillard96fe0952002-01-30 20:52:23 +00001301
Daniel Veillardd2379012002-03-15 22:24:56 +00001302 if (!PyArg_ParseTuple
1303 (args, (char *) "Oziz:xmlCreatePushParser", &pyobj_SAX, &chunk,
1304 &size, &URI))
1305 return (NULL);
Daniel Veillard3ce52572002-02-03 15:08:05 +00001306
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001307#ifdef DEBUG
Daniel Veillard3ce52572002-02-03 15:08:05 +00001308 printf("libxml_xmlCreatePushParser(%p, %s, %d, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001309 pyobj_SAX, chunk, size, URI);
Daniel Veillard96fe0952002-01-30 20:52:23 +00001310#endif
Daniel Veillard3ce52572002-02-03 15:08:05 +00001311 if (pyobj_SAX != Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001312 SAX = &pythonSaxHandler;
1313 Py_INCREF(pyobj_SAX);
1314 /* The reference is released in pythonEndDocument() */
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001315 }
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001316 ret = xmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI);
Daniel Veillard3ce52572002-02-03 15:08:05 +00001317 pyret = libxml_xmlParserCtxtPtrWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +00001318 return (pyret);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001319}
Daniel Veillard5d819032002-02-02 21:49:17 +00001320
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001321PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001322libxml_htmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
1323 PyObject * args)
1324{
Daniel Veillard656ce942004-04-30 23:11:45 +00001325#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001326 const char *chunk;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001327 int size;
Daniel Veillardd2379012002-03-15 22:24:56 +00001328 const char *URI;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001329 PyObject *pyobj_SAX = NULL;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001330 xmlSAXHandlerPtr SAX = NULL;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001331 xmlParserCtxtPtr ret;
1332 PyObject *pyret;
1333
Daniel Veillardd2379012002-03-15 22:24:56 +00001334 if (!PyArg_ParseTuple
1335 (args, (char *) "Oziz:htmlCreatePushParser", &pyobj_SAX, &chunk,
1336 &size, &URI))
1337 return (NULL);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001338
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001339#ifdef DEBUG
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001340 printf("libxml_htmlCreatePushParser(%p, %s, %d, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001341 pyobj_SAX, chunk, size, URI);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001342#endif
1343 if (pyobj_SAX != Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001344 SAX = &pythonSaxHandler;
1345 Py_INCREF(pyobj_SAX);
1346 /* The reference is released in pythonEndDocument() */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001347 }
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001348 ret = htmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI,
Daniel Veillardd2379012002-03-15 22:24:56 +00001349 XML_CHAR_ENCODING_NONE);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001350 pyret = libxml_xmlParserCtxtPtrWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +00001351 return (pyret);
Daniel Veillard656ce942004-04-30 23:11:45 +00001352#else
1353 Py_INCREF(Py_None);
1354 return (Py_None);
1355#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001356}
1357
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001358PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001359libxml_xmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1360{
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001361 int recover;
Daniel Veillardd2379012002-03-15 22:24:56 +00001362 const char *URI;
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001363 PyObject *pyobj_SAX = NULL;
1364 xmlSAXHandlerPtr SAX = NULL;
1365
Daniel Veillardd2379012002-03-15 22:24:56 +00001366 if (!PyArg_ParseTuple(args, (char *) "Osi:xmlSAXParseFile", &pyobj_SAX,
1367 &URI, &recover))
1368 return (NULL);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001369
1370#ifdef DEBUG
1371 printf("libxml_xmlSAXParseFile(%p, %s, %d) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001372 pyobj_SAX, URI, recover);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001373#endif
1374 if (pyobj_SAX == Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001375 Py_INCREF(Py_None);
1376 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001377 }
1378 SAX = &pythonSaxHandler;
1379 Py_INCREF(pyobj_SAX);
1380 /* The reference is released in pythonEndDocument() */
1381 xmlSAXParseFileWithData(SAX, URI, recover, pyobj_SAX);
1382 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +00001383 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001384}
1385
1386PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001387libxml_htmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1388{
Daniel Veillard656ce942004-04-30 23:11:45 +00001389#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001390 const char *URI;
1391 const char *encoding;
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001392 PyObject *pyobj_SAX = NULL;
1393 xmlSAXHandlerPtr SAX = NULL;
1394
Daniel Veillardd2379012002-03-15 22:24:56 +00001395 if (!PyArg_ParseTuple
1396 (args, (char *) "Osz:htmlSAXParseFile", &pyobj_SAX, &URI,
1397 &encoding))
1398 return (NULL);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001399
1400#ifdef DEBUG
1401 printf("libxml_htmlSAXParseFile(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001402 pyobj_SAX, URI, encoding);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001403#endif
1404 if (pyobj_SAX == Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001405 Py_INCREF(Py_None);
1406 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001407 }
1408 SAX = &pythonSaxHandler;
1409 Py_INCREF(pyobj_SAX);
1410 /* The reference is released in pythonEndDocument() */
1411 htmlSAXParseFile(URI, encoding, SAX, pyobj_SAX);
1412 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +00001413 return (Py_None);
Daniel Veillard656ce942004-04-30 23:11:45 +00001414#else
1415 Py_INCREF(Py_None);
1416 return (Py_None);
1417#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001418}
1419
Daniel Veillard5d819032002-02-02 21:49:17 +00001420/************************************************************************
1421 * *
1422 * Error message callback *
1423 * *
1424 ************************************************************************/
1425
1426static PyObject *libxml_xmlPythonErrorFuncHandler = NULL;
1427static PyObject *libxml_xmlPythonErrorFuncCtxt = NULL;
1428
Daniel Veillarde6227e02003-01-14 11:42:39 +00001429/* helper to build a xmlMalloc'ed string from a format and va_list */
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001430/*
1431 * disabled the loop, the repeated call to vsnprintf without reset of ap
1432 * in case the initial buffer was too small segfaulted on x86_64
1433 * we now directly vsnprintf on a large buffer.
1434 */
Daniel Veillarde6227e02003-01-14 11:42:39 +00001435static char *
1436libxml_buildMessage(const char *msg, va_list ap)
Daniel Veillardd2379012002-03-15 22:24:56 +00001437{
Daniel Veillardd2379012002-03-15 22:24:56 +00001438 int chars;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001439 char *str;
1440
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001441 str = (char *) xmlMalloc(1000);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001442 if (str == NULL)
1443 return NULL;
1444
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001445 chars = vsnprintf(str, 999, msg, ap);
1446 if (chars >= 998)
1447 str[999] = 0;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001448
1449 return str;
1450}
1451
1452static void
1453libxml_xmlErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, const char *msg,
1454 ...)
1455{
Daniel Veillardd2379012002-03-15 22:24:56 +00001456 va_list ap;
Daniel Veillard5d819032002-02-02 21:49:17 +00001457 PyObject *list;
1458 PyObject *message;
1459 PyObject *result;
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001460 char str[1000];
Daniel Veillard5d819032002-02-02 21:49:17 +00001461
1462#ifdef DEBUG_ERROR
1463 printf("libxml_xmlErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
1464#endif
1465
1466
1467 if (libxml_xmlPythonErrorFuncHandler == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001468 va_start(ap, msg);
Daniel Veillard007d51e2003-09-17 20:07:28 +00001469 vfprintf(stderr, msg, ap);
Daniel Veillardd2379012002-03-15 22:24:56 +00001470 va_end(ap);
Daniel Veillard5d819032002-02-02 21:49:17 +00001471 } else {
Daniel Veillarde6227e02003-01-14 11:42:39 +00001472 va_start(ap, msg);
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001473 if (vsnprintf(str, 999, msg, ap) >= 998)
1474 str[999] = 0;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001475 va_end(ap);
Daniel Veillard5d819032002-02-02 21:49:17 +00001476
Daniel Veillardd2379012002-03-15 22:24:56 +00001477 list = PyTuple_New(2);
1478 PyTuple_SetItem(list, 0, libxml_xmlPythonErrorFuncCtxt);
1479 Py_XINCREF(libxml_xmlPythonErrorFuncCtxt);
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001480 message = libxml_charPtrConstWrap(str);
Daniel Veillardd2379012002-03-15 22:24:56 +00001481 PyTuple_SetItem(list, 1, message);
1482 result = PyEval_CallObject(libxml_xmlPythonErrorFuncHandler, list);
1483 Py_XDECREF(list);
1484 Py_XDECREF(result);
Daniel Veillard5d819032002-02-02 21:49:17 +00001485 }
1486}
1487
1488static void
Daniel Veillardd2379012002-03-15 22:24:56 +00001489libxml_xmlErrorInitialize(void)
1490{
Daniel Veillard5d819032002-02-02 21:49:17 +00001491#ifdef DEBUG_ERROR
1492 printf("libxml_xmlErrorInitialize() called\n");
1493#endif
1494 xmlSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
Daniel Veillard781ac8b2003-05-15 22:11:36 +00001495 xmlThrDefSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
Daniel Veillard5d819032002-02-02 21:49:17 +00001496}
1497
Daniel Veillardc2664642003-07-29 20:44:53 +00001498static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001499libxml_xmlRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self,
1500 PyObject * args)
1501{
Daniel Veillard5d819032002-02-02 21:49:17 +00001502 PyObject *py_retval;
1503 PyObject *pyobj_f;
1504 PyObject *pyobj_ctx;
1505
Daniel Veillardd2379012002-03-15 22:24:56 +00001506 if (!PyArg_ParseTuple
1507 (args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f,
1508 &pyobj_ctx))
1509 return (NULL);
Daniel Veillard5d819032002-02-02 21:49:17 +00001510
1511#ifdef DEBUG_ERROR
William M. Brack8e2cc6f2004-07-03 23:28:52 +00001512 printf("libxml_xmlRegisterErrorHandler(%p, %p) called\n", pyobj_ctx,
Daniel Veillardd2379012002-03-15 22:24:56 +00001513 pyobj_f);
Daniel Veillard5d819032002-02-02 21:49:17 +00001514#endif
1515
1516 if (libxml_xmlPythonErrorFuncHandler != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001517 Py_XDECREF(libxml_xmlPythonErrorFuncHandler);
Daniel Veillard5d819032002-02-02 21:49:17 +00001518 }
1519 if (libxml_xmlPythonErrorFuncCtxt != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001520 Py_XDECREF(libxml_xmlPythonErrorFuncCtxt);
Daniel Veillard5d819032002-02-02 21:49:17 +00001521 }
1522
1523 Py_XINCREF(pyobj_ctx);
1524 Py_XINCREF(pyobj_f);
1525
1526 /* TODO: check f is a function ! */
1527 libxml_xmlPythonErrorFuncHandler = pyobj_f;
1528 libxml_xmlPythonErrorFuncCtxt = pyobj_ctx;
1529
1530 py_retval = libxml_intWrap(1);
Daniel Veillardd2379012002-03-15 22:24:56 +00001531 return (py_retval);
Daniel Veillard5d819032002-02-02 21:49:17 +00001532}
Daniel Veillardd2379012002-03-15 22:24:56 +00001533
Daniel Veillarde6227e02003-01-14 11:42:39 +00001534
1535/************************************************************************
1536 * *
1537 * Per parserCtxt error handler *
1538 * *
1539 ************************************************************************/
1540
Daniel Veillard417be3a2003-01-20 21:26:34 +00001541typedef struct
Daniel Veillarde6227e02003-01-14 11:42:39 +00001542{
Daniel Veillard417be3a2003-01-20 21:26:34 +00001543 PyObject *f;
1544 PyObject *arg;
1545} xmlParserCtxtPyCtxt;
1546typedef xmlParserCtxtPyCtxt *xmlParserCtxtPyCtxtPtr;
1547
1548static void
1549libxml_xmlParserCtxtGenericErrorFuncHandler(void *ctx, int severity, char *str)
1550{
Daniel Veillarde6227e02003-01-14 11:42:39 +00001551 PyObject *list;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001552 PyObject *result;
Daniel Veillard417be3a2003-01-20 21:26:34 +00001553 xmlParserCtxtPtr ctxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001554 xmlParserCtxtPyCtxtPtr pyCtxt;
1555
1556#ifdef DEBUG_ERROR
Daniel Veillard850ce9b2004-11-10 11:55:47 +00001557 printf("libxml_xmlParserCtxtGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001558#endif
1559
Daniel Veillard417be3a2003-01-20 21:26:34 +00001560 ctxt = (xmlParserCtxtPtr)ctx;
1561 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001562
Daniel Veillard417be3a2003-01-20 21:26:34 +00001563 list = PyTuple_New(4);
1564 PyTuple_SetItem(list, 0, pyCtxt->arg);
1565 Py_XINCREF(pyCtxt->arg);
1566 PyTuple_SetItem(list, 1, libxml_charPtrWrap(str));
1567 PyTuple_SetItem(list, 2, libxml_intWrap(severity));
1568 PyTuple_SetItem(list, 3, Py_None);
1569 Py_INCREF(Py_None);
1570 result = PyEval_CallObject(pyCtxt->f, list);
1571 if (result == NULL)
1572 {
1573 /* TODO: manage for the exception to be propagated... */
1574 PyErr_Print();
Daniel Veillarde6227e02003-01-14 11:42:39 +00001575 }
Daniel Veillard417be3a2003-01-20 21:26:34 +00001576 Py_XDECREF(list);
1577 Py_XDECREF(result);
1578}
1579
1580static void
1581libxml_xmlParserCtxtErrorFuncHandler(void *ctx, const char *msg, ...)
1582{
1583 va_list ap;
1584
1585 va_start(ap, msg);
1586 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_ERROR,libxml_buildMessage(msg,ap));
1587 va_end(ap);
1588}
1589
1590static void
1591libxml_xmlParserCtxtWarningFuncHandler(void *ctx, const char *msg, ...)
1592{
1593 va_list ap;
1594
1595 va_start(ap, msg);
1596 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_WARNING,libxml_buildMessage(msg,ap));
1597 va_end(ap);
1598}
1599
1600static void
1601libxml_xmlParserCtxtValidityErrorFuncHandler(void *ctx, const char *msg, ...)
1602{
1603 va_list ap;
1604
1605 va_start(ap, msg);
1606 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap));
1607 va_end(ap);
1608}
1609
1610static void
1611libxml_xmlParserCtxtValidityWarningFuncHandler(void *ctx, const char *msg, ...)
1612{
1613 va_list ap;
1614
1615 va_start(ap, msg);
1616 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap));
1617 va_end(ap);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001618}
1619
Daniel Veillardc2664642003-07-29 20:44:53 +00001620static PyObject *
Daniel Veillard417be3a2003-01-20 21:26:34 +00001621libxml_xmlParserCtxtSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
Daniel Veillarde6227e02003-01-14 11:42:39 +00001622{
1623 PyObject *py_retval;
1624 xmlParserCtxtPtr ctxt;
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001625 xmlParserCtxtPyCtxtPtr pyCtxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001626 PyObject *pyobj_ctxt;
1627 PyObject *pyobj_f;
1628 PyObject *pyobj_arg;
1629
Daniel Veillard417be3a2003-01-20 21:26:34 +00001630 if (!PyArg_ParseTuple(args, (char *)"OOO:xmlParserCtxtSetErrorHandler",
Daniel Veillarde6227e02003-01-14 11:42:39 +00001631 &pyobj_ctxt, &pyobj_f, &pyobj_arg))
1632 return(NULL);
1633 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1634 if (ctxt->_private == NULL) {
Daniel Veillarde6227e02003-01-14 11:42:39 +00001635 pyCtxt = xmlMalloc(sizeof(xmlParserCtxtPyCtxt));
1636 if (pyCtxt == NULL) {
1637 py_retval = libxml_intWrap(-1);
1638 return(py_retval);
1639 }
1640 memset(pyCtxt,0,sizeof(xmlParserCtxtPyCtxt));
1641 ctxt->_private = pyCtxt;
1642 }
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001643 else {
Daniel Veillard417be3a2003-01-20 21:26:34 +00001644 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001645 }
Daniel Veillarde6227e02003-01-14 11:42:39 +00001646 /* TODO: check f is a function ! */
Daniel Veillard417be3a2003-01-20 21:26:34 +00001647 Py_XDECREF(pyCtxt->f);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001648 Py_XINCREF(pyobj_f);
Daniel Veillard417be3a2003-01-20 21:26:34 +00001649 pyCtxt->f = pyobj_f;
1650 Py_XDECREF(pyCtxt->arg);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001651 Py_XINCREF(pyobj_arg);
Daniel Veillard417be3a2003-01-20 21:26:34 +00001652 pyCtxt->arg = pyobj_arg;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001653
Daniel Veillard417be3a2003-01-20 21:26:34 +00001654 if (pyobj_f != Py_None) {
1655 ctxt->sax->error = libxml_xmlParserCtxtErrorFuncHandler;
1656 ctxt->sax->warning = libxml_xmlParserCtxtWarningFuncHandler;
1657 ctxt->vctxt.error = libxml_xmlParserCtxtValidityErrorFuncHandler;
1658 ctxt->vctxt.warning = libxml_xmlParserCtxtValidityWarningFuncHandler;
1659 }
1660 else {
1661 ctxt->sax->error = xmlParserError;
1662 ctxt->vctxt.error = xmlParserValidityError;
1663 ctxt->sax->warning = xmlParserWarning;
1664 ctxt->vctxt.warning = xmlParserValidityWarning;
1665 }
Daniel Veillarde6227e02003-01-14 11:42:39 +00001666
1667 py_retval = libxml_intWrap(1);
1668 return(py_retval);
1669}
1670
Daniel Veillardc2664642003-07-29 20:44:53 +00001671static PyObject *
Daniel Veillard417be3a2003-01-20 21:26:34 +00001672libxml_xmlParserCtxtGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
Daniel Veillarde6227e02003-01-14 11:42:39 +00001673{
1674 PyObject *py_retval;
1675 xmlParserCtxtPtr ctxt;
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001676 xmlParserCtxtPyCtxtPtr pyCtxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001677 PyObject *pyobj_ctxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001678
Daniel Veillard417be3a2003-01-20 21:26:34 +00001679 if (!PyArg_ParseTuple(args, (char *)"O:xmlParserCtxtGetErrorHandler",
1680 &pyobj_ctxt))
Daniel Veillarde6227e02003-01-14 11:42:39 +00001681 return(NULL);
1682 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
Daniel Veillard417be3a2003-01-20 21:26:34 +00001683 py_retval = PyTuple_New(2);
1684 if (ctxt->_private != NULL) {
1685 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
1686
1687 PyTuple_SetItem(py_retval, 0, pyCtxt->f);
1688 Py_XINCREF(pyCtxt->f);
1689 PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
1690 Py_XINCREF(pyCtxt->arg);
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001691 }
1692 else {
Daniel Veillard417be3a2003-01-20 21:26:34 +00001693 /* no python error handler registered */
1694 PyTuple_SetItem(py_retval, 0, Py_None);
1695 Py_XINCREF(Py_None);
1696 PyTuple_SetItem(py_retval, 1, Py_None);
1697 Py_XINCREF(Py_None);
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001698 }
Daniel Veillarde6227e02003-01-14 11:42:39 +00001699 return(py_retval);
1700}
1701
Daniel Veillardc2664642003-07-29 20:44:53 +00001702static PyObject *
Daniel Veillard417be3a2003-01-20 21:26:34 +00001703libxml_xmlFreeParserCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
1704 xmlParserCtxtPtr ctxt;
1705 PyObject *pyobj_ctxt;
1706 xmlParserCtxtPyCtxtPtr pyCtxt;
1707
1708 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeParserCtxt", &pyobj_ctxt))
1709 return(NULL);
1710 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1711
1712 if (ctxt != NULL) {
1713 pyCtxt = (xmlParserCtxtPyCtxtPtr)((xmlParserCtxtPtr)ctxt)->_private;
1714 if (pyCtxt) {
1715 Py_XDECREF(pyCtxt->f);
1716 Py_XDECREF(pyCtxt->arg);
1717 xmlFree(pyCtxt);
1718 }
1719 xmlFreeParserCtxt(ctxt);
1720 }
1721
1722 Py_INCREF(Py_None);
1723 return(Py_None);
1724}
1725
Daniel Veillard850ce9b2004-11-10 11:55:47 +00001726/***
1727 * xmlValidCtxt stuff
1728 */
1729
1730typedef struct
1731{
1732 PyObject *warn;
1733 PyObject *error;
1734 PyObject *arg;
1735} xmlValidCtxtPyCtxt;
1736typedef xmlValidCtxtPyCtxt *xmlValidCtxtPyCtxtPtr;
1737
1738static void
1739libxml_xmlValidCtxtGenericErrorFuncHandler(void *ctx, int severity, char *str)
1740{
1741 PyObject *list;
1742 PyObject *result;
1743 xmlValidCtxtPyCtxtPtr pyCtxt;
1744
1745#ifdef DEBUG_ERROR
1746 printf("libxml_xmlValidCtxtGenericErrorFuncHandler(%p, %d, %s, ...) called\n", ctx, severity, str);
1747#endif
1748
1749 pyCtxt = (xmlValidCtxtPyCtxtPtr)ctx;
1750
1751 list = PyTuple_New(2);
1752 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
1753 PyTuple_SetItem(list, 1, pyCtxt->arg);
1754 Py_XINCREF(pyCtxt->arg);
1755 result = PyEval_CallObject(pyCtxt->error, list);
1756 if (result == NULL)
1757 {
1758 /* TODO: manage for the exception to be propagated... */
1759 PyErr_Print();
1760 }
1761 Py_XDECREF(list);
1762 Py_XDECREF(result);
1763}
1764
1765static void
1766libxml_xmlValidCtxtGenericWarningFuncHandler(void *ctx, int severity, char *str)
1767{
1768 PyObject *list;
1769 PyObject *result;
1770 xmlValidCtxtPyCtxtPtr pyCtxt;
1771
1772#ifdef DEBUG_ERROR
1773 printf("libxml_xmlValidCtxtGenericWarningFuncHandler(%p, %d, %s, ...) called\n", ctx, severity, str);
1774#endif
1775
1776 pyCtxt = (xmlValidCtxtPyCtxtPtr)ctx;
1777
1778 list = PyTuple_New(2);
1779 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
1780 PyTuple_SetItem(list, 1, pyCtxt->arg);
1781 Py_XINCREF(pyCtxt->arg);
1782 result = PyEval_CallObject(pyCtxt->warn, list);
1783 if (result == NULL)
1784 {
1785 /* TODO: manage for the exception to be propagated... */
1786 PyErr_Print();
1787 }
1788 Py_XDECREF(list);
1789 Py_XDECREF(result);
1790}
1791
1792static void
1793libxml_xmlValidCtxtErrorFuncHandler(void *ctx, const char *msg, ...)
1794{
1795 va_list ap;
1796
1797 va_start(ap, msg);
1798 libxml_xmlValidCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap));
1799 va_end(ap);
1800}
1801
1802static void
1803libxml_xmlValidCtxtWarningFuncHandler(void *ctx, const char *msg, ...)
1804{
1805 va_list ap;
1806
1807 va_start(ap, msg);
1808 libxml_xmlValidCtxtGenericWarningFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap));
1809 va_end(ap);
1810}
1811
1812static PyObject *
1813libxml_xmlSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1814{
1815 PyObject *py_retval;
1816 PyObject *pyobj_error;
1817 PyObject *pyobj_warn;
1818 PyObject *pyobj_ctx;
1819 PyObject *pyobj_arg = Py_None;
1820 xmlValidCtxtPtr ctxt;
1821 xmlValidCtxtPyCtxtPtr pyCtxt;
1822
1823 if (!PyArg_ParseTuple
1824 (args, (char *) "OOO|O:xmlSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
1825 return (NULL);
1826
1827#ifdef DEBUG_ERROR
1828 printf("libxml_xmlSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
1829#endif
1830
1831 ctxt = PyValidCtxt_Get(pyobj_ctx);
1832 pyCtxt = xmlMalloc(sizeof(xmlValidCtxtPyCtxt));
1833 if (pyCtxt == NULL) {
1834 py_retval = libxml_intWrap(-1);
1835 return(py_retval);
1836 }
1837 memset(pyCtxt, 0, sizeof(xmlValidCtxtPyCtxt));
1838
1839
1840 /* TODO: check warn and error is a function ! */
1841 Py_XDECREF(pyCtxt->error);
1842 Py_XINCREF(pyobj_error);
1843 pyCtxt->error = pyobj_error;
1844
1845 Py_XDECREF(pyCtxt->warn);
1846 Py_XINCREF(pyobj_warn);
1847 pyCtxt->warn = pyobj_warn;
1848
1849 Py_XDECREF(pyCtxt->arg);
1850 Py_XINCREF(pyobj_arg);
1851 pyCtxt->arg = pyobj_arg;
1852
1853 ctxt->error = libxml_xmlValidCtxtErrorFuncHandler;
1854 ctxt->warning = libxml_xmlValidCtxtWarningFuncHandler;
1855 ctxt->userData = pyCtxt;
1856
1857 py_retval = libxml_intWrap(1);
1858 return (py_retval);
1859}
1860
Daniel Veillard25c90c52005-03-02 10:47:41 +00001861
1862PyObject *
1863libxml_xmlFreeValidCtxt(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1864 xmlValidCtxtPtr cur;
1865 xmlValidCtxtPyCtxtPtr pyCtxt;
1866 PyObject *pyobj_cur;
1867
1868 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeValidCtxt", &pyobj_cur))
1869 return(NULL);
1870 cur = (xmlValidCtxtPtr) PyValidCtxt_Get(pyobj_cur);
1871
1872 pyCtxt = (xmlValidCtxtPyCtxtPtr)(cur->userData);
1873 if (pyCtxt != NULL)
1874 {
1875 Py_XDECREF(pyCtxt->error);
1876 Py_XDECREF(pyCtxt->warn);
1877 Py_XDECREF(pyCtxt->arg);
1878 xmlFree(pyCtxt);
1879 }
1880
1881 xmlFreeValidCtxt(cur);
1882 Py_INCREF(Py_None);
1883 return(Py_None);
1884}
1885
Daniel Veillarda7340c82002-02-01 17:56:45 +00001886/************************************************************************
1887 * *
Daniel Veillard26f70262003-01-16 22:45:08 +00001888 * Per xmlTextReader error handler *
1889 * *
1890 ************************************************************************/
1891
1892typedef struct
1893{
1894 PyObject *f;
1895 PyObject *arg;
1896} xmlTextReaderPyCtxt;
1897typedef xmlTextReaderPyCtxt *xmlTextReaderPyCtxtPtr;
1898
1899static void
1900libxml_xmlTextReaderErrorCallback(void *arg,
1901 const char *msg,
Daniel Veillard417be3a2003-01-20 21:26:34 +00001902 int severity,
1903 xmlTextReaderLocatorPtr locator)
Daniel Veillard26f70262003-01-16 22:45:08 +00001904{
1905 xmlTextReaderPyCtxt *pyCtxt = (xmlTextReaderPyCtxt *)arg;
1906 PyObject *list;
1907 PyObject *result;
1908
Daniel Veillard417be3a2003-01-20 21:26:34 +00001909 list = PyTuple_New(4);
Daniel Veillard26f70262003-01-16 22:45:08 +00001910 PyTuple_SetItem(list, 0, pyCtxt->arg);
1911 Py_XINCREF(pyCtxt->arg);
1912 PyTuple_SetItem(list, 1, libxml_charPtrConstWrap(msg));
Daniel Veillard417be3a2003-01-20 21:26:34 +00001913 PyTuple_SetItem(list, 2, libxml_intWrap(severity));
1914 PyTuple_SetItem(list, 3, libxml_xmlTextReaderLocatorPtrWrap(locator));
Daniel Veillard26f70262003-01-16 22:45:08 +00001915 result = PyEval_CallObject(pyCtxt->f, list);
1916 if (result == NULL)
1917 {
Daniel Veillard417be3a2003-01-20 21:26:34 +00001918 /* TODO: manage for the exception to be propagated... */
Daniel Veillard26f70262003-01-16 22:45:08 +00001919 PyErr_Print();
1920 }
1921 Py_XDECREF(list);
1922 Py_XDECREF(result);
1923}
1924
Daniel Veillardc2664642003-07-29 20:44:53 +00001925static PyObject *
Daniel Veillard26f70262003-01-16 22:45:08 +00001926libxml_xmlTextReaderSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1927{
1928 xmlTextReaderPtr reader;
1929 xmlTextReaderPyCtxtPtr pyCtxt;
1930 xmlTextReaderErrorFunc f;
1931 void *arg;
1932 PyObject *pyobj_reader;
1933 PyObject *pyobj_f;
1934 PyObject *pyobj_arg;
1935 PyObject *py_retval;
1936
1937 if (!PyArg_ParseTuple(args, (char *)"OOO:xmlTextReaderSetErrorHandler", &pyobj_reader, &pyobj_f, &pyobj_arg))
1938 return(NULL);
1939 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
1940 /* clear previous error handler */
1941 xmlTextReaderGetErrorHandler(reader,&f,&arg);
1942 if (arg != NULL) {
Daniel Veillardc2664642003-07-29 20:44:53 +00001943 if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) {
Daniel Veillard26f70262003-01-16 22:45:08 +00001944 /* ok, it's our error handler! */
1945 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
1946 Py_XDECREF(pyCtxt->f);
1947 Py_XDECREF(pyCtxt->arg);
1948 xmlFree(pyCtxt);
1949 }
1950 else {
1951 /*
1952 * there already an arg, and it's not ours,
1953 * there is definitely something wrong going on here...
1954 * we don't know how to free it, so we bail out...
1955 */
1956 py_retval = libxml_intWrap(-1);
1957 return(py_retval);
1958 }
1959 }
1960 xmlTextReaderSetErrorHandler(reader,NULL,NULL);
1961 /* set new error handler */
1962 if (pyobj_f != Py_None)
1963 {
1964 pyCtxt = (xmlTextReaderPyCtxtPtr)xmlMalloc(sizeof(xmlTextReaderPyCtxt));
1965 if (pyCtxt == NULL) {
1966 py_retval = libxml_intWrap(-1);
1967 return(py_retval);
1968 }
1969 Py_XINCREF(pyobj_f);
1970 pyCtxt->f = pyobj_f;
1971 Py_XINCREF(pyobj_arg);
1972 pyCtxt->arg = pyobj_arg;
Daniel Veillardc2664642003-07-29 20:44:53 +00001973 xmlTextReaderSetErrorHandler(reader,
1974 (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback,
1975 pyCtxt);
Daniel Veillard26f70262003-01-16 22:45:08 +00001976 }
1977
1978 py_retval = libxml_intWrap(1);
1979 return(py_retval);
1980}
1981
Daniel Veillardc2664642003-07-29 20:44:53 +00001982static PyObject *
Daniel Veillard26f70262003-01-16 22:45:08 +00001983libxml_xmlTextReaderGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1984{
1985 xmlTextReaderPtr reader;
1986 xmlTextReaderPyCtxtPtr pyCtxt;
1987 xmlTextReaderErrorFunc f;
1988 void *arg;
1989 PyObject *pyobj_reader;
1990 PyObject *py_retval;
1991
1992 if (!PyArg_ParseTuple(args, (char *)"O:xmlTextReaderSetErrorHandler", &pyobj_reader))
1993 return(NULL);
1994 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
1995 xmlTextReaderGetErrorHandler(reader,&f,&arg);
1996 py_retval = PyTuple_New(2);
Daniel Veillardc2664642003-07-29 20:44:53 +00001997 if (f == (xmlTextReaderErrorFunc)libxml_xmlTextReaderErrorCallback) {
Daniel Veillard26f70262003-01-16 22:45:08 +00001998 /* ok, it's our error handler! */
1999 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
2000 PyTuple_SetItem(py_retval, 0, pyCtxt->f);
2001 Py_XINCREF(pyCtxt->f);
2002 PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
2003 Py_XINCREF(pyCtxt->arg);
2004 }
2005 else
2006 {
2007 /* f is null or it's not our error handler */
2008 PyTuple_SetItem(py_retval, 0, Py_None);
2009 Py_XINCREF(Py_None);
2010 PyTuple_SetItem(py_retval, 1, Py_None);
2011 Py_XINCREF(Py_None);
2012 }
2013 return(py_retval);
2014}
2015
Daniel Veillardc2664642003-07-29 20:44:53 +00002016static PyObject *
Daniel Veillard26f70262003-01-16 22:45:08 +00002017libxml_xmlFreeTextReader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
2018 xmlTextReaderPtr reader;
2019 PyObject *pyobj_reader;
2020 xmlTextReaderPyCtxtPtr pyCtxt;
2021 xmlTextReaderErrorFunc f;
2022 void *arg;
2023
Daniel Veillard157fee02003-10-31 10:36:03 +00002024 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeTextReader", &pyobj_reader))
2025 return(NULL);
2026 if (!PyCObject_Check(pyobj_reader)) {
Daniel Veillardbb3ba322003-10-30 13:12:43 +00002027 Py_INCREF(Py_None);
2028 return(Py_None);
2029 }
Daniel Veillard26f70262003-01-16 22:45:08 +00002030 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
Daniel Veillardbb3ba322003-10-30 13:12:43 +00002031 if (reader == NULL) {
2032 Py_INCREF(Py_None);
2033 return(Py_None);
2034 }
Daniel Veillard26f70262003-01-16 22:45:08 +00002035
2036 xmlTextReaderGetErrorHandler(reader,&f,&arg);
2037 if (arg != NULL) {
Daniel Veillardc2664642003-07-29 20:44:53 +00002038 if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) {
Daniel Veillard26f70262003-01-16 22:45:08 +00002039 /* ok, it's our error handler! */
2040 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
2041 Py_XDECREF(pyCtxt->f);
2042 Py_XDECREF(pyCtxt->arg);
2043 xmlFree(pyCtxt);
2044 }
2045 /*
2046 * else, something wrong happened, because the error handler is
2047 * not owned by the python bindings...
2048 */
2049 }
2050
2051 xmlFreeTextReader(reader);
2052 Py_INCREF(Py_None);
2053 return(Py_None);
2054}
2055
2056/************************************************************************
2057 * *
Daniel Veillarda7340c82002-02-01 17:56:45 +00002058 * XPath extensions *
2059 * *
2060 ************************************************************************/
2061
Daniel Veillarda7340c82002-02-01 17:56:45 +00002062static void
Daniel Veillardd2379012002-03-15 22:24:56 +00002063libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs)
2064{
Daniel Veillarda7340c82002-02-01 17:56:45 +00002065 PyObject *list, *cur, *result;
2066 xmlXPathObjectPtr obj;
Daniel Veillard70cab352002-02-06 16:06:58 +00002067 xmlXPathContextPtr rctxt;
2068 PyObject *current_function = NULL;
2069 const xmlChar *name;
2070 const xmlChar *ns_uri;
Daniel Veillarda7340c82002-02-01 17:56:45 +00002071 int i;
2072
Daniel Veillard70cab352002-02-06 16:06:58 +00002073 if (ctxt == NULL)
Daniel Veillardd2379012002-03-15 22:24:56 +00002074 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00002075 rctxt = ctxt->context;
2076 if (rctxt == NULL)
Daniel Veillardd2379012002-03-15 22:24:56 +00002077 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00002078 name = rctxt->function;
2079 ns_uri = rctxt->functionURI;
Daniel Veillarda7340c82002-02-01 17:56:45 +00002080#ifdef DEBUG_XPATH
Daniel Veillardd2379012002-03-15 22:24:56 +00002081 printf("libxml_xmlXPathFuncCallback called name %s URI %s\n", name,
2082 ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002083#endif
2084
Daniel Veillard70cab352002-02-06 16:06:58 +00002085 /*
2086 * Find the function, it should be there it was there at lookup
2087 */
Daniel Veillardd2379012002-03-15 22:24:56 +00002088 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
2089 if ( /* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002090 (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
2091 (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
2092 current_function = (*libxml_xpathCallbacks)[i].function;
Daniel Veillardd2379012002-03-15 22:24:56 +00002093 }
Daniel Veillard70cab352002-02-06 16:06:58 +00002094 }
2095 if (current_function == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002096 printf
2097 ("libxml_xmlXPathFuncCallback: internal error %s not found !\n",
2098 name);
2099 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00002100 }
2101
Daniel Veillardc575b992002-02-08 13:28:40 +00002102 list = PyTuple_New(nargs + 1);
2103 PyTuple_SetItem(list, 0, libxml_xmlXPathParserContextPtrWrap(ctxt));
Daniel Veillard05349ab2004-01-25 20:01:35 +00002104 for (i = nargs - 1; i >= 0; i--) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002105 obj = valuePop(ctxt);
2106 cur = libxml_xmlXPathObjectPtrWrap(obj);
2107 PyTuple_SetItem(list, i + 1, cur);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002108 }
2109 result = PyEval_CallObject(current_function, list);
2110 Py_DECREF(list);
2111
2112 obj = libxml_xmlXPathObjectPtrConvert(result);
2113 valuePush(ctxt, obj);
2114}
2115
2116static xmlXPathFunction
Daniel Veillardd2379012002-03-15 22:24:56 +00002117libxml_xmlXPathFuncLookupFunc(void *ctxt, const xmlChar * name,
2118 const xmlChar * ns_uri)
2119{
Daniel Veillarda7340c82002-02-01 17:56:45 +00002120 int i;
Daniel Veillardd2379012002-03-15 22:24:56 +00002121
Daniel Veillarda7340c82002-02-01 17:56:45 +00002122#ifdef DEBUG_XPATH
2123 printf("libxml_xmlXPathFuncLookupFunc(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00002124 ctxt, name, ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002125#endif
Daniel Veillard70cab352002-02-06 16:06:58 +00002126 /*
2127 * This is called once only. The address is then stored in the
2128 * XPath expression evaluation, the proper object to call can
2129 * then still be found using the execution context function
2130 * and functionURI fields.
2131 */
Daniel Veillardd2379012002-03-15 22:24:56 +00002132 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002133 if ((ctxt == (*libxml_xpathCallbacks)[i].ctx) &&
2134 (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
2135 (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002136 return (libxml_xmlXPathFuncCallback);
2137 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00002138 }
Daniel Veillardd2379012002-03-15 22:24:56 +00002139 return (NULL);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002140}
2141
2142static void
Daniel Veillardd2379012002-03-15 22:24:56 +00002143libxml_xpathCallbacksInitialize(void)
2144{
Daniel Veillarda7340c82002-02-01 17:56:45 +00002145 int i;
2146
2147 if (libxml_xpathCallbacksInitialized != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00002148 return;
Daniel Veillarda7340c82002-02-01 17:56:45 +00002149
2150#ifdef DEBUG_XPATH
2151 printf("libxml_xpathCallbacksInitialized called\n");
2152#endif
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002153 libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlMalloc(
2154 libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
Daniel Veillarda7340c82002-02-01 17:56:45 +00002155
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002156 for (i = 0; i < libxml_xpathCallbacksAllocd; i++) {
2157 (*libxml_xpathCallbacks)[i].ctx = NULL;
2158 (*libxml_xpathCallbacks)[i].name = NULL;
2159 (*libxml_xpathCallbacks)[i].ns_uri = NULL;
2160 (*libxml_xpathCallbacks)[i].function = NULL;
Daniel Veillarda7340c82002-02-01 17:56:45 +00002161 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00002162 libxml_xpathCallbacksInitialized = 1;
2163}
2164
2165PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002166libxml_xmlRegisterXPathFunction(ATTRIBUTE_UNUSED PyObject * self,
2167 PyObject * args)
2168{
Daniel Veillarda7340c82002-02-01 17:56:45 +00002169 PyObject *py_retval;
2170 int c_retval = 0;
2171 xmlChar *name;
2172 xmlChar *ns_uri;
2173 xmlXPathContextPtr ctx;
2174 PyObject *pyobj_ctx;
2175 PyObject *pyobj_f;
2176 int i;
2177
Daniel Veillardd2379012002-03-15 22:24:56 +00002178 if (!PyArg_ParseTuple
2179 (args, (char *) "OszO:registerXPathFunction", &pyobj_ctx, &name,
2180 &ns_uri, &pyobj_f))
2181 return (NULL);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002182
2183 ctx = (xmlXPathContextPtr) PyxmlXPathContext_Get(pyobj_ctx);
2184 if (libxml_xpathCallbacksInitialized == 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00002185 libxml_xpathCallbacksInitialize();
Daniel Veillarda7340c82002-02-01 17:56:45 +00002186 xmlXPathRegisterFuncLookup(ctx, libxml_xmlXPathFuncLookupFunc, ctx);
2187
2188 if ((pyobj_ctx == NULL) || (name == NULL) || (pyobj_f == NULL)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002189 py_retval = libxml_intWrap(-1);
2190 return (py_retval);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002191 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00002192#ifdef DEBUG_XPATH
2193 printf("libxml_registerXPathFunction(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00002194 ctx, name, ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002195#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002196 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002197 if ((ctx == (*libxml_xpathCallbacks)[i].ctx) &&
2198 (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
2199 (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002200 Py_XINCREF(pyobj_f);
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002201 Py_XDECREF((*libxml_xpathCallbacks)[i].function);
2202 (*libxml_xpathCallbacks)[i].function = pyobj_f;
Daniel Veillardd2379012002-03-15 22:24:56 +00002203 c_retval = 1;
2204 goto done;
2205 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00002206 }
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002207 if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksAllocd) {
2208 libxml_xpathCallbacksAllocd+=10;
2209 libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlRealloc(
2210 libxml_xpathCallbacks,
2211 libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
2212 }
2213 i = libxml_xpathCallbacksNb++;
2214 Py_XINCREF(pyobj_f);
2215 (*libxml_xpathCallbacks)[i].ctx = ctx;
2216 (*libxml_xpathCallbacks)[i].name = xmlStrdup(name);
2217 (*libxml_xpathCallbacks)[i].ns_uri = xmlStrdup(ns_uri);
2218 (*libxml_xpathCallbacks)[i].function = pyobj_f;
Daniel Veillardd2379012002-03-15 22:24:56 +00002219 c_retval = 1;
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002220
Daniel Veillardd2379012002-03-15 22:24:56 +00002221 done:
Daniel Veillarda7340c82002-02-01 17:56:45 +00002222 py_retval = libxml_intWrap((int) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00002223 return (py_retval);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002224}
2225
Daniel Veillard1971ee22002-01-31 20:29:19 +00002226/************************************************************************
2227 * *
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002228 * Global properties access *
2229 * *
2230 ************************************************************************/
2231static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002232libxml_name(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002233{
2234 PyObject *resultobj, *obj;
2235 xmlNodePtr cur;
2236 const xmlChar *res;
2237
Daniel Veillardd2379012002-03-15 22:24:56 +00002238 if (!PyArg_ParseTuple(args, (char *) "O:name", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002239 return NULL;
2240 cur = PyxmlNode_Get(obj);
2241
2242#ifdef DEBUG
2243 printf("libxml_name: cur = %p type %d\n", cur, cur->type);
2244#endif
2245
Daniel Veillardd2379012002-03-15 22:24:56 +00002246 switch (cur->type) {
2247 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002248#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002249 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002250#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002251 case XML_HTML_DOCUMENT_NODE:{
2252 xmlDocPtr doc = (xmlDocPtr) cur;
2253
2254 res = doc->URL;
2255 break;
2256 }
2257 case XML_ATTRIBUTE_NODE:{
2258 xmlAttrPtr attr = (xmlAttrPtr) cur;
2259
2260 res = attr->name;
2261 break;
2262 }
2263 case XML_NAMESPACE_DECL:{
2264 xmlNsPtr ns = (xmlNsPtr) cur;
2265
2266 res = ns->prefix;
2267 break;
2268 }
2269 default:
2270 res = cur->name;
2271 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002272 }
Daniel Veillard1971ee22002-01-31 20:29:19 +00002273 resultobj = libxml_constxmlCharPtrWrap(res);
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002274
2275 return resultobj;
2276}
2277
2278static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002279libxml_doc(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002280{
2281 PyObject *resultobj, *obj;
2282 xmlNodePtr cur;
2283 xmlDocPtr res;
2284
Daniel Veillardd2379012002-03-15 22:24:56 +00002285 if (!PyArg_ParseTuple(args, (char *) "O:doc", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002286 return NULL;
2287 cur = PyxmlNode_Get(obj);
2288
2289#ifdef DEBUG
2290 printf("libxml_doc: cur = %p\n", cur);
2291#endif
2292
Daniel Veillardd2379012002-03-15 22:24:56 +00002293 switch (cur->type) {
2294 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002295#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002296 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002297#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002298 case XML_HTML_DOCUMENT_NODE:
2299 res = NULL;
2300 break;
2301 case XML_ATTRIBUTE_NODE:{
2302 xmlAttrPtr attr = (xmlAttrPtr) cur;
2303
2304 res = attr->doc;
2305 break;
2306 }
2307 case XML_NAMESPACE_DECL:
2308 res = NULL;
2309 break;
2310 default:
2311 res = cur->doc;
2312 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002313 }
2314 resultobj = libxml_xmlDocPtrWrap(res);
2315 return resultobj;
2316}
2317
2318static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002319libxml_properties(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002320{
2321 PyObject *resultobj, *obj;
2322 xmlNodePtr cur = NULL;
2323 xmlAttrPtr res;
2324
Daniel Veillardd2379012002-03-15 22:24:56 +00002325 if (!PyArg_ParseTuple(args, (char *) "O:properties", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002326 return NULL;
2327 cur = PyxmlNode_Get(obj);
2328 if (cur->type == XML_ELEMENT_NODE)
Daniel Veillardd2379012002-03-15 22:24:56 +00002329 res = cur->properties;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002330 else
Daniel Veillardd2379012002-03-15 22:24:56 +00002331 res = NULL;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002332 resultobj = libxml_xmlAttrPtrWrap(res);
2333 return resultobj;
2334}
2335
2336static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002337libxml_next(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002338{
2339 PyObject *resultobj, *obj;
2340 xmlNodePtr cur;
2341 xmlNodePtr res;
2342
Daniel Veillardd2379012002-03-15 22:24:56 +00002343 if (!PyArg_ParseTuple(args, (char *) "O:next", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002344 return NULL;
2345 cur = PyxmlNode_Get(obj);
2346
2347#ifdef DEBUG
2348 printf("libxml_next: cur = %p\n", cur);
2349#endif
2350
Daniel Veillardd2379012002-03-15 22:24:56 +00002351 switch (cur->type) {
2352 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002353#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002354 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002355#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002356 case XML_HTML_DOCUMENT_NODE:
2357 res = NULL;
2358 break;
2359 case XML_ATTRIBUTE_NODE:{
2360 xmlAttrPtr attr = (xmlAttrPtr) cur;
2361
2362 res = (xmlNodePtr) attr->next;
2363 break;
2364 }
2365 case XML_NAMESPACE_DECL:{
2366 xmlNsPtr ns = (xmlNsPtr) cur;
2367
2368 res = (xmlNodePtr) ns->next;
2369 break;
2370 }
2371 default:
2372 res = cur->next;
2373 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002374
2375 }
2376 resultobj = libxml_xmlNodePtrWrap(res);
2377 return resultobj;
2378}
2379
2380static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002381libxml_prev(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002382{
2383 PyObject *resultobj, *obj;
2384 xmlNodePtr cur;
2385 xmlNodePtr res;
2386
Daniel Veillardd2379012002-03-15 22:24:56 +00002387 if (!PyArg_ParseTuple(args, (char *) "O:prev", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002388 return NULL;
2389 cur = PyxmlNode_Get(obj);
2390
2391#ifdef DEBUG
2392 printf("libxml_prev: cur = %p\n", cur);
2393#endif
2394
Daniel Veillardd2379012002-03-15 22:24:56 +00002395 switch (cur->type) {
2396 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002397#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002398 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002399#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002400 case XML_HTML_DOCUMENT_NODE:
2401 res = NULL;
2402 break;
2403 case XML_ATTRIBUTE_NODE:{
2404 xmlAttrPtr attr = (xmlAttrPtr) cur;
2405
Daniel Veillardfaa35ff2002-11-24 13:53:43 +00002406 res = (xmlNodePtr) attr->prev;
Daniel Veillardd2379012002-03-15 22:24:56 +00002407 }
2408 case XML_NAMESPACE_DECL:
2409 res = NULL;
2410 break;
2411 default:
Daniel Veillardfaa35ff2002-11-24 13:53:43 +00002412 res = cur->prev;
Daniel Veillardd2379012002-03-15 22:24:56 +00002413 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002414 }
2415 resultobj = libxml_xmlNodePtrWrap(res);
2416 return resultobj;
2417}
2418
2419static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002420libxml_children(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002421{
2422 PyObject *resultobj, *obj;
2423 xmlNodePtr cur;
2424 xmlNodePtr res;
2425
Daniel Veillardd2379012002-03-15 22:24:56 +00002426 if (!PyArg_ParseTuple(args, (char *) "O:children", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002427 return NULL;
2428 cur = PyxmlNode_Get(obj);
2429
2430#ifdef DEBUG
2431 printf("libxml_children: cur = %p\n", cur);
2432#endif
2433
Daniel Veillardd2379012002-03-15 22:24:56 +00002434 switch (cur->type) {
2435 case XML_ELEMENT_NODE:
2436 case XML_ENTITY_REF_NODE:
2437 case XML_ENTITY_NODE:
2438 case XML_PI_NODE:
2439 case XML_COMMENT_NODE:
2440 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002441#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002442 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002443#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002444 case XML_HTML_DOCUMENT_NODE:
2445 case XML_DTD_NODE:
2446 res = cur->children;
2447 break;
2448 case XML_ATTRIBUTE_NODE:{
2449 xmlAttrPtr attr = (xmlAttrPtr) cur;
2450
2451 res = attr->children;
2452 break;
2453 }
2454 default:
2455 res = NULL;
2456 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002457 }
2458 resultobj = libxml_xmlNodePtrWrap(res);
2459 return resultobj;
2460}
2461
2462static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002463libxml_last(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002464{
2465 PyObject *resultobj, *obj;
2466 xmlNodePtr cur;
2467 xmlNodePtr res;
2468
Daniel Veillardd2379012002-03-15 22:24:56 +00002469 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002470 return NULL;
2471 cur = PyxmlNode_Get(obj);
2472
2473#ifdef DEBUG
2474 printf("libxml_last: cur = %p\n", cur);
2475#endif
2476
Daniel Veillardd2379012002-03-15 22:24:56 +00002477 switch (cur->type) {
2478 case XML_ELEMENT_NODE:
2479 case XML_ENTITY_REF_NODE:
2480 case XML_ENTITY_NODE:
2481 case XML_PI_NODE:
2482 case XML_COMMENT_NODE:
2483 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002484#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002485 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002486#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002487 case XML_HTML_DOCUMENT_NODE:
2488 case XML_DTD_NODE:
2489 res = cur->last;
2490 break;
2491 case XML_ATTRIBUTE_NODE:{
2492 xmlAttrPtr attr = (xmlAttrPtr) cur;
2493
2494 res = attr->last;
2495 }
2496 default:
2497 res = NULL;
2498 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002499 }
2500 resultobj = libxml_xmlNodePtrWrap(res);
2501 return resultobj;
2502}
2503
2504static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002505libxml_parent(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002506{
2507 PyObject *resultobj, *obj;
2508 xmlNodePtr cur;
2509 xmlNodePtr res;
2510
Daniel Veillardd2379012002-03-15 22:24:56 +00002511 if (!PyArg_ParseTuple(args, (char *) "O:parent", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002512 return NULL;
2513 cur = PyxmlNode_Get(obj);
2514
2515#ifdef DEBUG
2516 printf("libxml_parent: cur = %p\n", cur);
2517#endif
2518
Daniel Veillardd2379012002-03-15 22:24:56 +00002519 switch (cur->type) {
2520 case XML_DOCUMENT_NODE:
2521 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002522#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002523 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002524#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002525 res = NULL;
2526 break;
2527 case XML_ATTRIBUTE_NODE:{
2528 xmlAttrPtr attr = (xmlAttrPtr) cur;
2529
2530 res = attr->parent;
2531 }
2532 case XML_ENTITY_DECL:
2533 case XML_NAMESPACE_DECL:
2534 case XML_XINCLUDE_START:
2535 case XML_XINCLUDE_END:
2536 res = NULL;
2537 break;
2538 default:
2539 res = cur->parent;
2540 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002541 }
2542 resultobj = libxml_xmlNodePtrWrap(res);
2543 return resultobj;
2544}
2545
2546static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002547libxml_type(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002548{
2549 PyObject *resultobj, *obj;
2550 xmlNodePtr cur;
Daniel Veillardd2379012002-03-15 22:24:56 +00002551 const xmlChar *res = NULL;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002552
Daniel Veillardd2379012002-03-15 22:24:56 +00002553 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002554 return NULL;
2555 cur = PyxmlNode_Get(obj);
2556
2557#ifdef DEBUG
2558 printf("libxml_type: cur = %p\n", cur);
2559#endif
2560
Daniel Veillardd2379012002-03-15 22:24:56 +00002561 switch (cur->type) {
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002562 case XML_ELEMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002563 res = (const xmlChar *) "element";
2564 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002565 case XML_ATTRIBUTE_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002566 res = (const xmlChar *) "attribute";
2567 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002568 case XML_TEXT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002569 res = (const xmlChar *) "text";
2570 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002571 case XML_CDATA_SECTION_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002572 res = (const xmlChar *) "cdata";
2573 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002574 case XML_ENTITY_REF_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002575 res = (const xmlChar *) "entity_ref";
2576 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002577 case XML_ENTITY_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002578 res = (const xmlChar *) "entity";
2579 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002580 case XML_PI_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002581 res = (const xmlChar *) "pi";
2582 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002583 case XML_COMMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002584 res = (const xmlChar *) "comment";
2585 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002586 case XML_DOCUMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002587 res = (const xmlChar *) "document_xml";
2588 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002589 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002590 res = (const xmlChar *) "doctype";
2591 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002592 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002593 res = (const xmlChar *) "fragment";
2594 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002595 case XML_NOTATION_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002596 res = (const xmlChar *) "notation";
2597 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002598 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002599 res = (const xmlChar *) "document_html";
2600 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002601 case XML_DTD_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002602 res = (const xmlChar *) "dtd";
2603 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002604 case XML_ELEMENT_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002605 res = (const xmlChar *) "elem_decl";
2606 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002607 case XML_ATTRIBUTE_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002608 res = (const xmlChar *) "attribute_decl";
2609 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002610 case XML_ENTITY_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002611 res = (const xmlChar *) "entity_decl";
2612 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002613 case XML_NAMESPACE_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002614 res = (const xmlChar *) "namespace";
2615 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002616 case XML_XINCLUDE_START:
Daniel Veillardd2379012002-03-15 22:24:56 +00002617 res = (const xmlChar *) "xinclude_start";
2618 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002619 case XML_XINCLUDE_END:
Daniel Veillardd2379012002-03-15 22:24:56 +00002620 res = (const xmlChar *) "xinclude_end";
2621 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002622#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002623 case XML_DOCB_DOCUMENT_NODE:
2624 res = (const xmlChar *) "document_docbook";
2625 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002626#endif
2627 }
2628#ifdef DEBUG
2629 printf("libxml_type: cur = %p: %s\n", cur, res);
2630#endif
2631
Daniel Veillard1971ee22002-01-31 20:29:19 +00002632 resultobj = libxml_constxmlCharPtrWrap(res);
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002633 return resultobj;
2634}
2635
2636/************************************************************************
2637 * *
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002638 * Specific accessor functions *
2639 * *
2640 ************************************************************************/
2641PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002642libxml_xmlNodeGetNsDefs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2643{
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002644 PyObject *py_retval;
2645 xmlNsPtr c_retval;
2646 xmlNodePtr node;
2647 PyObject *pyobj_node;
2648
Daniel Veillardd2379012002-03-15 22:24:56 +00002649 if (!PyArg_ParseTuple
2650 (args, (char *) "O:xmlNodeGetNsDefs", &pyobj_node))
2651 return (NULL);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002652 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2653
2654 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002655 Py_INCREF(Py_None);
2656 return (Py_None);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002657 }
2658 c_retval = node->nsDef;
2659 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00002660 return (py_retval);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002661}
2662
2663PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002664libxml_xmlNodeGetNs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2665{
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002666 PyObject *py_retval;
2667 xmlNsPtr c_retval;
2668 xmlNodePtr node;
2669 PyObject *pyobj_node;
2670
Daniel Veillardd2379012002-03-15 22:24:56 +00002671 if (!PyArg_ParseTuple(args, (char *) "O:xmlNodeGetNs", &pyobj_node))
2672 return (NULL);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002673 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2674
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002675 if ((node == NULL) ||
2676 ((node->type != XML_ELEMENT_NODE) &&
2677 (node->type != XML_ATTRIBUTE_NODE))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002678 Py_INCREF(Py_None);
2679 return (Py_None);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002680 }
2681 c_retval = node->ns;
2682 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00002683 return (py_retval);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002684}
2685
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002686#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002687/************************************************************************
2688 * *
Daniel Veillard1e774382002-03-06 17:35:40 +00002689 * Serialization front-end *
2690 * *
2691 ************************************************************************/
2692
Daniel Veillardd2379012002-03-15 22:24:56 +00002693static PyObject *
2694libxml_serializeNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2695{
Daniel Veillard1e774382002-03-06 17:35:40 +00002696 PyObject *py_retval = NULL;
2697 xmlChar *c_retval;
2698 PyObject *pyobj_node;
2699 xmlNodePtr node;
2700 xmlDocPtr doc;
Daniel Veillardd2379012002-03-15 22:24:56 +00002701 const char *encoding;
Daniel Veillard1e774382002-03-06 17:35:40 +00002702 int format;
2703 int len;
2704
Daniel Veillardd2379012002-03-15 22:24:56 +00002705 if (!PyArg_ParseTuple(args, (char *) "Ozi:serializeNode", &pyobj_node,
2706 &encoding, &format))
2707 return (NULL);
Daniel Veillard1e774382002-03-06 17:35:40 +00002708 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2709
2710 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002711 Py_INCREF(Py_None);
2712 return (Py_None);
Daniel Veillard1e774382002-03-06 17:35:40 +00002713 }
2714 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002715 doc = (xmlDocPtr) node;
2716 xmlDocDumpFormatMemoryEnc(doc, &c_retval, &len,
2717 (const char *) encoding, format);
2718 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard656ce942004-04-30 23:11:45 +00002719#ifdef LIBXML_HTML_ENABLED
Daniel Veillard1e774382002-03-06 17:35:40 +00002720 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002721 xmlOutputBufferPtr buf;
2722 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00002723
Daniel Veillardd2379012002-03-15 22:24:56 +00002724 doc = (xmlDocPtr) node;
2725 if (encoding != NULL)
2726 htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
2727 encoding = (const char *) htmlGetMetaEncoding(doc);
Daniel Veillard1e774382002-03-06 17:35:40 +00002728
Daniel Veillardd2379012002-03-15 22:24:56 +00002729 if (encoding != NULL) {
2730 handler = xmlFindCharEncodingHandler(encoding);
2731 if (handler == NULL) {
2732 Py_INCREF(Py_None);
2733 return (Py_None);
2734 }
2735 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002736
Daniel Veillardd2379012002-03-15 22:24:56 +00002737 /*
2738 * Fallback to HTML or ASCII when the encoding is unspecified
2739 */
2740 if (handler == NULL)
2741 handler = xmlFindCharEncodingHandler("HTML");
2742 if (handler == NULL)
2743 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00002744
Daniel Veillardd2379012002-03-15 22:24:56 +00002745 buf = xmlAllocOutputBuffer(handler);
2746 if (buf == NULL) {
2747 Py_INCREF(Py_None);
2748 return (Py_None);
2749 }
2750 htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
2751 xmlOutputBufferFlush(buf);
2752 if (buf->conv != NULL) {
2753 len = buf->conv->use;
2754 c_retval = buf->conv->content;
2755 buf->conv->content = NULL;
2756 } else {
2757 len = buf->buffer->use;
2758 c_retval = buf->buffer->content;
2759 buf->buffer->content = NULL;
2760 }
2761 (void) xmlOutputBufferClose(buf);
2762 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard656ce942004-04-30 23:11:45 +00002763#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard1e774382002-03-06 17:35:40 +00002764 } else {
William M. Brack95af5942004-02-08 04:12:49 +00002765 if (node->type == XML_NAMESPACE_DECL)
2766 doc = NULL;
2767 else
2768 doc = node->doc;
Daniel Veillarda8c0adb2002-11-17 22:37:35 +00002769 if ((doc == NULL) || (doc->type == XML_DOCUMENT_NODE)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002770 xmlOutputBufferPtr buf;
2771 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00002772
Daniel Veillardd2379012002-03-15 22:24:56 +00002773 if (encoding != NULL) {
2774 handler = xmlFindCharEncodingHandler(encoding);
2775 if (handler == NULL) {
2776 Py_INCREF(Py_None);
2777 return (Py_None);
2778 }
2779 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002780
Daniel Veillardd2379012002-03-15 22:24:56 +00002781 buf = xmlAllocOutputBuffer(handler);
2782 if (buf == NULL) {
2783 Py_INCREF(Py_None);
2784 return (Py_None);
2785 }
2786 xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
2787 xmlOutputBufferFlush(buf);
2788 if (buf->conv != NULL) {
2789 len = buf->conv->use;
2790 c_retval = buf->conv->content;
2791 buf->conv->content = NULL;
2792 } else {
2793 len = buf->buffer->use;
2794 c_retval = buf->buffer->content;
2795 buf->buffer->content = NULL;
2796 }
2797 (void) xmlOutputBufferClose(buf);
2798 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard656ce942004-04-30 23:11:45 +00002799#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002800 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2801 xmlOutputBufferPtr buf;
2802 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00002803
Daniel Veillardd2379012002-03-15 22:24:56 +00002804 if (encoding != NULL)
2805 htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
2806 encoding = (const char *) htmlGetMetaEncoding(doc);
2807 if (encoding != NULL) {
2808 handler = xmlFindCharEncodingHandler(encoding);
2809 if (handler == NULL) {
2810 Py_INCREF(Py_None);
2811 return (Py_None);
2812 }
2813 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002814
Daniel Veillardd2379012002-03-15 22:24:56 +00002815 /*
2816 * Fallback to HTML or ASCII when the encoding is unspecified
2817 */
2818 if (handler == NULL)
2819 handler = xmlFindCharEncodingHandler("HTML");
2820 if (handler == NULL)
2821 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00002822
Daniel Veillardd2379012002-03-15 22:24:56 +00002823 buf = xmlAllocOutputBuffer(handler);
2824 if (buf == NULL) {
2825 Py_INCREF(Py_None);
2826 return (Py_None);
2827 }
2828 htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
2829 xmlOutputBufferFlush(buf);
2830 if (buf->conv != NULL) {
2831 len = buf->conv->use;
2832 c_retval = buf->conv->content;
2833 buf->conv->content = NULL;
2834 } else {
2835 len = buf->buffer->use;
2836 c_retval = buf->buffer->content;
2837 buf->buffer->content = NULL;
2838 }
2839 (void) xmlOutputBufferClose(buf);
2840 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard656ce942004-04-30 23:11:45 +00002841#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2379012002-03-15 22:24:56 +00002842 } else {
2843 Py_INCREF(Py_None);
2844 return (Py_None);
2845 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002846 }
Daniel Veillardd2379012002-03-15 22:24:56 +00002847 return (py_retval);
Daniel Veillard1e774382002-03-06 17:35:40 +00002848}
2849
Daniel Veillardd2379012002-03-15 22:24:56 +00002850static PyObject *
2851libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2852{
Daniel Veillard1e774382002-03-06 17:35:40 +00002853 PyObject *py_file = NULL;
2854 FILE *output;
2855 PyObject *pyobj_node;
2856 xmlNodePtr node;
2857 xmlDocPtr doc;
Daniel Veillardd2379012002-03-15 22:24:56 +00002858 const char *encoding;
Daniel Veillard1e774382002-03-06 17:35:40 +00002859 int format;
2860 int len;
2861 xmlOutputBufferPtr buf;
2862 xmlCharEncodingHandlerPtr handler = NULL;
2863
Daniel Veillardd2379012002-03-15 22:24:56 +00002864 if (!PyArg_ParseTuple(args, (char *) "OOzi:serializeNode", &pyobj_node,
2865 &py_file, &encoding, &format))
2866 return (NULL);
Daniel Veillard1e774382002-03-06 17:35:40 +00002867 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2868
2869 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002870 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002871 }
2872 if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002873 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002874 }
2875 output = PyFile_AsFile(py_file);
2876 if (output == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002877 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002878 }
2879
2880 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002881 doc = (xmlDocPtr) node;
Daniel Veillard1e774382002-03-06 17:35:40 +00002882 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002883 doc = (xmlDocPtr) node;
Daniel Veillard1e774382002-03-06 17:35:40 +00002884 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00002885 doc = node->doc;
Daniel Veillard1e774382002-03-06 17:35:40 +00002886 }
Daniel Veillard656ce942004-04-30 23:11:45 +00002887#ifdef LIBXML_HTML_ENABLED
Daniel Veillard1e774382002-03-06 17:35:40 +00002888 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002889 if (encoding == NULL)
2890 encoding = (const char *) htmlGetMetaEncoding(doc);
Daniel Veillard1e774382002-03-06 17:35:40 +00002891 }
Daniel Veillard656ce942004-04-30 23:11:45 +00002892#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard1e774382002-03-06 17:35:40 +00002893 if (encoding != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002894 handler = xmlFindCharEncodingHandler(encoding);
2895 if (handler == NULL) {
2896 return (PyInt_FromLong((long) -1));
2897 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002898 }
2899 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002900 if (handler == NULL)
2901 handler = xmlFindCharEncodingHandler("HTML");
2902 if (handler == NULL)
2903 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00002904 }
2905
2906 buf = xmlOutputBufferCreateFile(output, handler);
2907 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002908 len = xmlSaveFormatFileTo(buf, doc, encoding, format);
Daniel Veillard656ce942004-04-30 23:11:45 +00002909#ifdef LIBXML_HTML_ENABLED
Daniel Veillard1e774382002-03-06 17:35:40 +00002910 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002911 htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
2912 len = xmlOutputBufferClose(buf);
Daniel Veillard1e774382002-03-06 17:35:40 +00002913 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002914 htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
2915 len = xmlOutputBufferClose(buf);
Daniel Veillard656ce942004-04-30 23:11:45 +00002916#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard1e774382002-03-06 17:35:40 +00002917 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00002918 xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
2919 len = xmlOutputBufferClose(buf);
Daniel Veillard1e774382002-03-06 17:35:40 +00002920 }
Daniel Veillardd2379012002-03-15 22:24:56 +00002921 return (PyInt_FromLong((long) len));
Daniel Veillard1e774382002-03-06 17:35:40 +00002922}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002923#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard1e774382002-03-06 17:35:40 +00002924
2925/************************************************************************
2926 * *
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002927 * Extra stuff *
2928 * *
2929 ************************************************************************/
2930PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002931libxml_xmlNewNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2932{
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002933 PyObject *py_retval;
Daniel Veillardd2379012002-03-15 22:24:56 +00002934 xmlChar *name;
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002935 xmlNodePtr node;
2936
Daniel Veillardd2379012002-03-15 22:24:56 +00002937 if (!PyArg_ParseTuple(args, (char *) "s:xmlNewNode", &name))
2938 return (NULL);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002939 node = (xmlNodePtr) xmlNewNode(NULL, name);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00002940#ifdef DEBUG
Daniel Veillardd2379012002-03-15 22:24:56 +00002941 printf("NewNode: %s : %p\n", name, (void *) node);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00002942#endif
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002943
2944 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002945 Py_INCREF(Py_None);
2946 return (Py_None);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002947 }
2948 py_retval = libxml_xmlNodePtrWrap(node);
Daniel Veillardd2379012002-03-15 22:24:56 +00002949 return (py_retval);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002950}
2951
Daniel Veillard54396242003-04-23 07:36:50 +00002952
2953/************************************************************************
2954 * *
2955 * Local Catalog stuff *
2956 * *
2957 ************************************************************************/
2958static PyObject *
2959libxml_addLocalCatalog(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2960{
2961 xmlChar *URL;
2962 xmlParserCtxtPtr ctxt;
2963 PyObject *pyobj_ctxt;
2964
2965 if (!PyArg_ParseTuple(args, (char *)"Os:addLocalCatalog", &pyobj_ctxt, &URL))
2966 return(NULL);
2967
2968 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
2969
2970 if (URL != NULL) {
2971 ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL);
2972 }
2973
2974#ifdef DEBUG
2975 printf("LocalCatalog: %s\n", URL);
2976#endif
2977
2978 Py_INCREF(Py_None);
2979 return (Py_None);
2980}
2981
Daniel Veillardc2664642003-07-29 20:44:53 +00002982#ifdef LIBXML_SCHEMAS_ENABLED
2983
2984/************************************************************************
2985 * *
2986 * RelaxNG error handler registration *
2987 * *
2988 ************************************************************************/
2989
2990typedef struct
2991{
2992 PyObject *warn;
2993 PyObject *error;
2994 PyObject *arg;
2995} xmlRelaxNGValidCtxtPyCtxt;
2996typedef xmlRelaxNGValidCtxtPyCtxt *xmlRelaxNGValidCtxtPyCtxtPtr;
2997
2998static void
2999libxml_xmlRelaxNGValidityGenericErrorFuncHandler(void *ctx, char *str)
3000{
3001 PyObject *list;
3002 PyObject *result;
3003 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3004
3005#ifdef DEBUG_ERROR
3006 printf("libxml_xmlRelaxNGValidityGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
3007#endif
3008
3009 pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx;
3010
3011 list = PyTuple_New(2);
3012 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3013 PyTuple_SetItem(list, 1, pyCtxt->arg);
3014 Py_XINCREF(pyCtxt->arg);
3015 result = PyEval_CallObject(pyCtxt->error, list);
3016 if (result == NULL)
3017 {
3018 /* TODO: manage for the exception to be propagated... */
3019 PyErr_Print();
3020 }
3021 Py_XDECREF(list);
3022 Py_XDECREF(result);
3023}
3024
3025static void
3026libxml_xmlRelaxNGValidityGenericWarningFuncHandler(void *ctx, char *str)
3027{
3028 PyObject *list;
3029 PyObject *result;
3030 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3031
3032#ifdef DEBUG_ERROR
3033 printf("libxml_xmlRelaxNGValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str);
3034#endif
3035
3036 pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx;
3037
3038 list = PyTuple_New(2);
3039 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3040 PyTuple_SetItem(list, 1, pyCtxt->arg);
3041 Py_XINCREF(pyCtxt->arg);
3042 result = PyEval_CallObject(pyCtxt->warn, list);
3043 if (result == NULL)
3044 {
3045 /* TODO: manage for the exception to be propagated... */
3046 PyErr_Print();
3047 }
3048 Py_XDECREF(list);
3049 Py_XDECREF(result);
3050}
3051
3052static void
3053libxml_xmlRelaxNGValidityErrorFunc(void *ctx, const char *msg, ...)
3054{
3055 va_list ap;
3056
3057 va_start(ap, msg);
3058 libxml_xmlRelaxNGValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap));
3059 va_end(ap);
3060}
3061
3062static void
3063libxml_xmlRelaxNGValidityWarningFunc(void *ctx, const char *msg, ...)
3064{
3065 va_list ap;
3066
3067 va_start(ap, msg);
3068 libxml_xmlRelaxNGValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap));
3069 va_end(ap);
3070}
3071
3072static PyObject *
3073libxml_xmlRelaxNGSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3074{
3075 PyObject *py_retval;
3076 PyObject *pyobj_error;
3077 PyObject *pyobj_warn;
3078 PyObject *pyobj_ctx;
3079 PyObject *pyobj_arg = Py_None;
3080 xmlRelaxNGValidCtxtPtr ctxt;
3081 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3082
3083 if (!PyArg_ParseTuple
3084 (args, (char *) "OOO|O:xmlRelaxNGSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
3085 return (NULL);
3086
3087#ifdef DEBUG_ERROR
3088 printf("libxml_xmlRelaxNGSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
3089#endif
3090
3091 ctxt = PyrelaxNgValidCtxt_Get(pyobj_ctx);
William M. Brackc1939562003-08-05 15:52:22 +00003092 if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1)
Daniel Veillardc2664642003-07-29 20:44:53 +00003093 {
3094 py_retval = libxml_intWrap(-1);
3095 return(py_retval);
3096 }
3097
3098 if (pyCtxt == NULL)
3099 {
3100 /* first time to set the error handlers */
3101 pyCtxt = xmlMalloc(sizeof(xmlRelaxNGValidCtxtPyCtxt));
3102 if (pyCtxt == NULL) {
3103 py_retval = libxml_intWrap(-1);
3104 return(py_retval);
3105 }
3106 memset(pyCtxt, 0, sizeof(xmlRelaxNGValidCtxtPyCtxt));
3107 }
3108
3109 /* TODO: check warn and error is a function ! */
3110 Py_XDECREF(pyCtxt->error);
3111 Py_XINCREF(pyobj_error);
3112 pyCtxt->error = pyobj_error;
3113
3114 Py_XDECREF(pyCtxt->warn);
3115 Py_XINCREF(pyobj_warn);
3116 pyCtxt->warn = pyobj_warn;
3117
3118 Py_XDECREF(pyCtxt->arg);
3119 Py_XINCREF(pyobj_arg);
3120 pyCtxt->arg = pyobj_arg;
3121
3122 xmlRelaxNGSetValidErrors(ctxt, &libxml_xmlRelaxNGValidityErrorFunc, &libxml_xmlRelaxNGValidityWarningFunc, pyCtxt);
3123
3124 py_retval = libxml_intWrap(1);
3125 return (py_retval);
3126}
3127
3128static PyObject *
3129libxml_xmlRelaxNGFreeValidCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
3130 xmlRelaxNGValidCtxtPtr ctxt;
3131 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3132 PyObject *pyobj_ctxt;
3133
3134 if (!PyArg_ParseTuple(args, (char *)"O:xmlRelaxNGFreeValidCtxt", &pyobj_ctxt))
3135 return(NULL);
3136 ctxt = (xmlRelaxNGValidCtxtPtr) PyrelaxNgValidCtxt_Get(pyobj_ctxt);
3137
William M. Brackc1939562003-08-05 15:52:22 +00003138 if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0)
Daniel Veillardc2664642003-07-29 20:44:53 +00003139 {
3140 if (pyCtxt != NULL)
3141 {
3142 Py_XDECREF(pyCtxt->error);
3143 Py_XDECREF(pyCtxt->warn);
3144 Py_XDECREF(pyCtxt->arg);
3145 xmlFree(pyCtxt);
3146 }
3147 }
3148
3149 xmlRelaxNGFreeValidCtxt(ctxt);
3150 Py_INCREF(Py_None);
3151 return(Py_None);
3152}
3153
Daniel Veillard259f0df2004-08-18 09:13:18 +00003154typedef struct
3155{
3156 PyObject *warn;
3157 PyObject *error;
3158 PyObject *arg;
3159} xmlSchemaValidCtxtPyCtxt;
3160typedef xmlSchemaValidCtxtPyCtxt *xmlSchemaValidCtxtPyCtxtPtr;
3161
3162static void
3163libxml_xmlSchemaValidityGenericErrorFuncHandler(void *ctx, char *str)
3164{
3165 PyObject *list;
3166 PyObject *result;
3167 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3168
3169#ifdef DEBUG_ERROR
3170 printf("libxml_xmlSchemaValiditiyGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
3171#endif
3172
3173 pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx;
3174
3175 list = PyTuple_New(2);
3176 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3177 PyTuple_SetItem(list, 1, pyCtxt->arg);
3178 Py_XINCREF(pyCtxt->arg);
3179 result = PyEval_CallObject(pyCtxt->error, list);
3180 if (result == NULL)
3181 {
3182 /* TODO: manage for the exception to be propagated... */
3183 PyErr_Print();
3184 }
3185 Py_XDECREF(list);
3186 Py_XDECREF(result);
3187}
3188
3189static void
3190libxml_xmlSchemaValidityGenericWarningFuncHandler(void *ctx, char *str)
3191{
3192 PyObject *list;
3193 PyObject *result;
3194 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3195
3196#ifdef DEBUG_ERROR
3197 printf("libxml_xmlSchemaValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str);
3198#endif
3199
3200 pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx;
3201
3202 list = PyTuple_New(2);
3203 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3204 PyTuple_SetItem(list, 1, pyCtxt->arg);
3205 Py_XINCREF(pyCtxt->arg);
3206 result = PyEval_CallObject(pyCtxt->warn, list);
3207 if (result == NULL)
3208 {
3209 /* TODO: manage for the exception to be propagated... */
3210 PyErr_Print();
3211 }
3212 Py_XDECREF(list);
3213 Py_XDECREF(result);
3214}
3215
3216static void
3217libxml_xmlSchemaValidityErrorFunc(void *ctx, const char *msg, ...)
3218{
3219 va_list ap;
3220
3221 va_start(ap, msg);
3222 libxml_xmlSchemaValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap));
3223 va_end(ap);
3224}
3225
3226static void
3227libxml_xmlSchemaValidityWarningFunc(void *ctx, const char *msg, ...)
3228{
3229 va_list ap;
3230
3231 va_start(ap, msg);
3232 libxml_xmlSchemaValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap));
3233 va_end(ap);
3234}
3235
Daniel Veillard6ebf3c42004-08-22 13:11:39 +00003236PyObject *
Daniel Veillard259f0df2004-08-18 09:13:18 +00003237libxml_xmlSchemaSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3238{
3239 PyObject *py_retval;
3240 PyObject *pyobj_error;
3241 PyObject *pyobj_warn;
3242 PyObject *pyobj_ctx;
3243 PyObject *pyobj_arg = Py_None;
3244 xmlSchemaValidCtxtPtr ctxt;
3245 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3246
3247 if (!PyArg_ParseTuple
3248 (args, (char *) "OOO|O:xmlSchemaSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
3249 return (NULL);
3250
3251#ifdef DEBUG_ERROR
3252 printf("libxml_xmlSchemaSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
3253#endif
3254
3255 ctxt = PySchemaValidCtxt_Get(pyobj_ctx);
3256 if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1)
3257 {
3258 py_retval = libxml_intWrap(-1);
3259 return(py_retval);
3260 }
3261
3262 if (pyCtxt == NULL)
3263 {
3264 /* first time to set the error handlers */
3265 pyCtxt = xmlMalloc(sizeof(xmlSchemaValidCtxtPyCtxt));
3266 if (pyCtxt == NULL) {
3267 py_retval = libxml_intWrap(-1);
3268 return(py_retval);
3269 }
3270 memset(pyCtxt, 0, sizeof(xmlSchemaValidCtxtPyCtxt));
3271 }
3272
3273 /* TODO: check warn and error is a function ! */
3274 Py_XDECREF(pyCtxt->error);
3275 Py_XINCREF(pyobj_error);
3276 pyCtxt->error = pyobj_error;
3277
3278 Py_XDECREF(pyCtxt->warn);
3279 Py_XINCREF(pyobj_warn);
3280 pyCtxt->warn = pyobj_warn;
3281
3282 Py_XDECREF(pyCtxt->arg);
3283 Py_XINCREF(pyobj_arg);
3284 pyCtxt->arg = pyobj_arg;
3285
3286 xmlSchemaSetValidErrors(ctxt, &libxml_xmlSchemaValidityErrorFunc, &libxml_xmlSchemaValidityWarningFunc, pyCtxt);
3287
3288 py_retval = libxml_intWrap(1);
3289 return(py_retval);
3290}
3291
Daniel Veillard36505562004-08-22 14:02:09 +00003292#if 0
Daniel Veillard6ebf3c42004-08-22 13:11:39 +00003293PyObject *
Daniel Veillard259f0df2004-08-18 09:13:18 +00003294libxml_xmlSchemaFreeValidCtxt(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3295{
3296 xmlSchemaValidCtxtPtr ctxt;
3297 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3298 PyObject *pyobj_ctxt;
3299
3300 if (!PyArg_ParseTuple(args, (char *)"O:xmlSchemaFreeValidCtxt", &pyobj_ctxt))
3301 return(NULL);
3302 ctxt = (xmlSchemaValidCtxtPtr) PySchemaValidCtxt_Get(pyobj_ctxt);
3303
3304 if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0)
3305 {
3306 if (pyCtxt != NULL)
3307 {
3308 Py_XDECREF(pyCtxt->error);
3309 Py_XDECREF(pyCtxt->warn);
3310 Py_XDECREF(pyCtxt->arg);
3311 xmlFree(pyCtxt);
3312 }
3313 }
3314
3315 xmlSchemaFreeValidCtxt(ctxt);
3316 Py_INCREF(Py_None);
3317 return(Py_None);
3318}
Daniel Veillard36505562004-08-22 14:02:09 +00003319#endif
Daniel Veillard259f0df2004-08-18 09:13:18 +00003320
Daniel Veillardc2664642003-07-29 20:44:53 +00003321#endif
3322
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003323#ifdef LIBXML_C14N_ENABLED
3324#ifdef LIBXML_OUTPUT_ENABLED
3325
3326/************************************************************************
3327 * *
3328 * XML Canonicalization c14n *
3329 * *
3330 ************************************************************************/
3331
3332static int
3333PyxmlNodeSet_Convert(PyObject *py_nodeset, xmlNodeSetPtr *result)
3334{
3335 xmlNodeSetPtr nodeSet;
3336 int is_tuple = 0;
3337
3338 if (PyTuple_Check(py_nodeset))
3339 is_tuple = 1;
3340 else if (PyList_Check(py_nodeset))
3341 is_tuple = 0;
3342 else if (py_nodeset == Py_None) {
3343 *result = NULL;
3344 return 0;
3345 }
3346 else {
3347 PyErr_SetString(PyExc_TypeError,
3348 "must be a tuple or list of nodes.");
3349 return -1;
3350 }
3351
3352 nodeSet = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet));
3353 if (nodeSet == NULL) {
3354 PyErr_SetString(PyExc_MemoryError, "");
3355 return -1;
3356 }
3357
3358 nodeSet->nodeNr = 0;
3359 nodeSet->nodeMax = (is_tuple
3360 ? PyTuple_GET_SIZE(py_nodeset)
3361 : PyList_GET_SIZE(py_nodeset));
3362 nodeSet->nodeTab
3363 = (xmlNodePtr *) xmlMalloc (nodeSet->nodeMax
3364 * sizeof(xmlNodePtr));
3365 if (nodeSet->nodeTab == NULL) {
3366 xmlFree(nodeSet);
3367 PyErr_SetString(PyExc_MemoryError, "");
3368 return -1;
3369 }
3370 memset(nodeSet->nodeTab, 0 ,
3371 nodeSet->nodeMax * sizeof(xmlNodePtr));
3372
3373 {
3374 int idx;
3375 for (idx=0; idx < nodeSet->nodeMax; ++idx) {
3376 xmlNodePtr pynode =
3377 PyxmlNode_Get (is_tuple
3378 ? PyTuple_GET_ITEM(py_nodeset, idx)
3379 : PyList_GET_ITEM(py_nodeset, idx));
3380 if (pynode)
3381 nodeSet->nodeTab[nodeSet->nodeNr++] = pynode;
3382 }
3383 }
3384 *result = nodeSet;
3385 return 0;
3386}
3387
3388static int
3389PystringSet_Convert(PyObject *py_strings, xmlChar *** result)
3390{
3391 /* NOTE: the array should be freed, but the strings are shared
3392 with the python strings and so must not be freed. */
3393
3394 xmlChar ** strings;
3395 int is_tuple = 0;
3396 int count;
3397 int init_index = 0;
3398
3399 if (PyTuple_Check(py_strings))
3400 is_tuple = 1;
3401 else if (PyList_Check(py_strings))
3402 is_tuple = 0;
3403 else if (py_strings == Py_None) {
3404 *result = NULL;
3405 return 0;
3406 }
3407 else {
3408 PyErr_SetString(PyExc_TypeError,
3409 "must be a tuple or list of strings.");
3410 return -1;
3411 }
3412
3413 count = (is_tuple
3414 ? PyTuple_GET_SIZE(py_strings)
3415 : PyList_GET_SIZE(py_strings));
3416
3417 strings = (xmlChar **) xmlMalloc(sizeof(xmlChar *) * count);
3418
3419 if (strings == NULL) {
3420 PyErr_SetString(PyExc_MemoryError, "");
3421 return -1;
3422 }
3423
3424 memset(strings, 0 , sizeof(xmlChar *) * count);
3425
3426 {
3427 int idx;
3428 for (idx=0; idx < count; ++idx) {
3429 char* s = PyString_AsString
3430 (is_tuple
3431 ? PyTuple_GET_ITEM(py_strings, idx)
3432 : PyList_GET_ITEM(py_strings, idx));
3433 if (s)
3434 strings[init_index++] = (xmlChar *)s;
3435 else {
3436 xmlFree(strings);
3437 PyErr_SetString(PyExc_TypeError,
3438 "must be a tuple or list of strings.");
3439 return -1;
3440 }
3441 }
3442 }
3443
3444 *result = strings;
3445 return 0;
3446}
3447
3448static PyObject *
3449libxml_C14NDocDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
3450 PyObject * args)
3451{
3452 PyObject *py_retval = NULL;
3453
3454 PyObject *pyobj_doc;
3455 PyObject *pyobj_nodes;
3456 int exclusive;
3457 PyObject *pyobj_prefixes;
3458 int with_comments;
3459
3460 xmlDocPtr doc;
3461 xmlNodeSetPtr nodes;
3462 xmlChar **prefixes = NULL;
3463 xmlChar *doc_txt;
3464
3465 int result;
3466
3467 if (!PyArg_ParseTuple(args, (char *) "OOiOi:C14NDocDumpMemory",
3468 &pyobj_doc,
3469 &pyobj_nodes,
3470 &exclusive,
3471 &pyobj_prefixes,
3472 &with_comments))
3473 return (NULL);
3474
3475 doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
3476 if (!doc) {
3477 PyErr_SetString(PyExc_TypeError, "bad document.");
3478 return NULL;
3479 }
3480
3481 result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes);
3482 if (result < 0) return NULL;
3483
3484 if (exclusive) {
3485 result = PystringSet_Convert(pyobj_prefixes, &prefixes);
3486 if (result < 0) {
3487 if (nodes) {
3488 xmlFree(nodes->nodeTab);
3489 xmlFree(nodes);
3490 }
3491 return NULL;
3492 }
3493 }
3494
3495 result = xmlC14NDocDumpMemory(doc,
3496 nodes,
3497 exclusive,
3498 prefixes,
3499 with_comments,
3500 &doc_txt);
3501
3502 if (nodes) {
3503 xmlFree(nodes->nodeTab);
3504 xmlFree(nodes);
3505 }
3506 if (prefixes) {
3507 xmlChar ** idx = prefixes;
3508 while (*idx) xmlFree(*(idx++));
3509 xmlFree(prefixes);
3510 }
3511
3512 if (result < 0) {
3513 PyErr_SetString(PyExc_Exception,
3514 "libxml2 xmlC14NDocDumpMemory failure.");
3515 return NULL;
3516 }
3517 else {
3518 py_retval = PyString_FromStringAndSize((const char *) doc_txt,
3519 result);
3520 xmlFree(doc_txt);
3521 return py_retval;
3522 }
3523}
3524
3525static PyObject *
3526libxml_C14NDocSaveTo(ATTRIBUTE_UNUSED PyObject * self,
3527 PyObject * args)
3528{
3529 PyObject *pyobj_doc;
3530 PyObject *py_file;
3531 PyObject *pyobj_nodes;
3532 int exclusive;
3533 PyObject *pyobj_prefixes;
3534 int with_comments;
3535
3536 xmlDocPtr doc;
3537 xmlNodeSetPtr nodes;
3538 xmlChar **prefixes = NULL;
3539 FILE * output;
3540 xmlOutputBufferPtr buf;
3541
3542 int result;
3543 int len;
3544
3545 if (!PyArg_ParseTuple(args, (char *) "OOiOiO:C14NDocSaveTo",
3546 &pyobj_doc,
3547 &pyobj_nodes,
3548 &exclusive,
3549 &pyobj_prefixes,
3550 &with_comments,
3551 &py_file))
3552 return (NULL);
3553
3554 doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
3555 if (!doc) {
3556 PyErr_SetString(PyExc_TypeError, "bad document.");
3557 return NULL;
3558 }
3559
3560 if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
3561 PyErr_SetString(PyExc_TypeError, "bad file.");
3562 return NULL;
3563 }
3564 output = PyFile_AsFile(py_file);
3565 if (output == NULL) {
3566 PyErr_SetString(PyExc_TypeError, "bad file.");
3567 return NULL;
3568 }
3569 buf = xmlOutputBufferCreateFile(output, NULL);
3570
3571 result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes);
3572 if (result < 0) return NULL;
3573
3574 if (exclusive) {
3575 result = PystringSet_Convert(pyobj_prefixes, &prefixes);
3576 if (result < 0) {
3577 if (nodes) {
3578 xmlFree(nodes->nodeTab);
3579 xmlFree(nodes);
3580 }
3581 return NULL;
3582 }
3583 }
3584
3585 result = xmlC14NDocSaveTo(doc,
3586 nodes,
3587 exclusive,
3588 prefixes,
3589 with_comments,
3590 buf);
3591
3592 if (nodes) {
3593 xmlFree(nodes->nodeTab);
3594 xmlFree(nodes);
3595 }
3596 if (prefixes) {
3597 xmlChar ** idx = prefixes;
3598 while (*idx) xmlFree(*(idx++));
3599 xmlFree(prefixes);
3600 }
3601
3602 len = xmlOutputBufferClose(buf);
3603
3604 if (result < 0) {
3605 PyErr_SetString(PyExc_Exception,
3606 "libxml2 xmlC14NDocSaveTo failure.");
3607 return NULL;
3608 }
3609 else
3610 return PyInt_FromLong((long) len);
3611}
3612
3613#endif
3614#endif
3615
William M. Brackc68d78d2004-07-16 10:39:30 +00003616static PyObject *
3617libxml_getObjDesc(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003618
William M. Brackc68d78d2004-07-16 10:39:30 +00003619 PyObject *obj;
3620 char *str;
3621
3622 if (!PyArg_ParseTuple(args, (char *)"O:getObjDesc", &obj))
3623 return NULL;
3624 str = PyCObject_GetDesc(obj);
3625 return Py_BuildValue((char *)"s", str);
3626}
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003627
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00003628/************************************************************************
3629 * *
Daniel Veillardd2897fd2002-01-30 16:37:32 +00003630 * The registration stuff *
3631 * *
3632 ************************************************************************/
3633static PyMethodDef libxmlMethods[] = {
Daniel Veillard96fe0952002-01-30 20:52:23 +00003634#include "libxml2-export.c"
Daniel Veillardd2379012002-03-15 22:24:56 +00003635 {(char *) "name", libxml_name, METH_VARARGS, NULL},
3636 {(char *) "children", libxml_children, METH_VARARGS, NULL},
3637 {(char *) "properties", libxml_properties, METH_VARARGS, NULL},
3638 {(char *) "last", libxml_last, METH_VARARGS, NULL},
3639 {(char *) "prev", libxml_prev, METH_VARARGS, NULL},
3640 {(char *) "next", libxml_next, METH_VARARGS, NULL},
3641 {(char *) "parent", libxml_parent, METH_VARARGS, NULL},
3642 {(char *) "type", libxml_type, METH_VARARGS, NULL},
3643 {(char *) "doc", libxml_doc, METH_VARARGS, NULL},
3644 {(char *) "xmlNewNode", libxml_xmlNewNode, METH_VARARGS, NULL},
Daniel Veillard850ce9b2004-11-10 11:55:47 +00003645 {(char *)"xmlSetValidErrors", libxml_xmlSetValidErrors, METH_VARARGS, NULL},
Daniel Veillard25c90c52005-03-02 10:47:41 +00003646 {(char *)"xmlFreeValidCtxt", libxml_xmlFreeValidCtxt, METH_VARARGS, NULL},
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003647#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00003648 {(char *) "serializeNode", libxml_serializeNode, METH_VARARGS, NULL},
3649 {(char *) "saveNodeTo", libxml_saveNodeTo, METH_VARARGS, NULL},
Daniel Veillardc6d4a932002-09-12 15:00:57 +00003650 {(char *) "outputBufferCreate", libxml_xmlCreateOutputBuffer, METH_VARARGS, NULL},
Daniel Veillard6cbd6c02003-12-04 12:31:49 +00003651 {(char *) "outputBufferGetPythonFile", libxml_outputBufferGetPythonFile, METH_VARARGS, NULL},
3652 {(char *) "xmlOutputBufferClose", libxml_xmlOutputBufferClose, METH_VARARGS, NULL},
3653 { (char *)"xmlOutputBufferFlush", libxml_xmlOutputBufferFlush, METH_VARARGS, NULL },
Daniel Veillard263ec862004-10-04 10:26:54 +00003654 { (char *)"xmlSaveFileTo", libxml_xmlSaveFileTo, METH_VARARGS, NULL },
3655 { (char *)"xmlSaveFormatFileTo", libxml_xmlSaveFormatFileTo, METH_VARARGS, NULL },
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003656#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardc6d4a932002-09-12 15:00:57 +00003657 {(char *) "inputBufferCreate", libxml_xmlCreateInputBuffer, METH_VARARGS, NULL},
3658 {(char *) "setEntityLoader", libxml_xmlSetEntityLoader, METH_VARARGS, NULL},
Daniel Veillard3e20a292003-01-10 13:14:40 +00003659 {(char *)"xmlRegisterErrorHandler", libxml_xmlRegisterErrorHandler, METH_VARARGS, NULL },
Daniel Veillard417be3a2003-01-20 21:26:34 +00003660 {(char *)"xmlParserCtxtSetErrorHandler", libxml_xmlParserCtxtSetErrorHandler, METH_VARARGS, NULL },
3661 {(char *)"xmlParserCtxtGetErrorHandler", libxml_xmlParserCtxtGetErrorHandler, METH_VARARGS, NULL },
Daniel Veillarde6227e02003-01-14 11:42:39 +00003662 {(char *)"xmlFreeParserCtxt", libxml_xmlFreeParserCtxt, METH_VARARGS, NULL },
Daniel Veillard26f70262003-01-16 22:45:08 +00003663 {(char *)"xmlTextReaderSetErrorHandler", libxml_xmlTextReaderSetErrorHandler, METH_VARARGS, NULL },
3664 {(char *)"xmlTextReaderGetErrorHandler", libxml_xmlTextReaderGetErrorHandler, METH_VARARGS, NULL },
3665 {(char *)"xmlFreeTextReader", libxml_xmlFreeTextReader, METH_VARARGS, NULL },
Daniel Veillard54396242003-04-23 07:36:50 +00003666 {(char *)"addLocalCatalog", libxml_addLocalCatalog, METH_VARARGS, NULL },
Daniel Veillardc2664642003-07-29 20:44:53 +00003667#ifdef LIBXML_SCHEMAS_ENABLED
3668 {(char *)"xmlRelaxNGSetValidErrors", libxml_xmlRelaxNGSetValidErrors, METH_VARARGS, NULL},
3669 {(char *)"xmlRelaxNGFreeValidCtxt", libxml_xmlRelaxNGFreeValidCtxt, METH_VARARGS, NULL},
Daniel Veillardeff45a92004-10-29 12:10:55 +00003670 {(char *)"xmlSchemaSetValidErrors", libxml_xmlSchemaSetValidErrors, METH_VARARGS, NULL},
Daniel Veillardc2664642003-07-29 20:44:53 +00003671#endif
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003672#ifdef LIBXML_C14N_ENABLED
3673#ifdef LIBXML_OUTPUT_ENABLED
3674 {(char *)"xmlC14NDocDumpMemory", libxml_C14NDocDumpMemory, METH_VARARGS, NULL},
3675 {(char *)"xmlC14NDocSaveTo", libxml_C14NDocSaveTo, METH_VARARGS, NULL},
3676#endif
3677#endif
William M. Brackc68d78d2004-07-16 10:39:30 +00003678 {(char *) "getObjDesc", libxml_getObjDesc, METH_VARARGS, NULL},
Daniel Veillardd2379012002-03-15 22:24:56 +00003679 {NULL, NULL, 0, NULL}
Daniel Veillardd2897fd2002-01-30 16:37:32 +00003680};
3681
Daniel Veillard0fea6f42002-02-22 22:51:13 +00003682#ifdef MERGED_MODULES
Daniel Veillard6361da02002-02-23 10:10:33 +00003683extern void initlibxsltmod(void);
Daniel Veillard0fea6f42002-02-22 22:51:13 +00003684#endif
3685
Daniel Veillardd2379012002-03-15 22:24:56 +00003686void
3687initlibxml2mod(void)
3688{
Daniel Veillardaf43f632002-03-08 15:05:20 +00003689 static int initialized = 0;
Daniel Veillardaf43f632002-03-08 15:05:20 +00003690
3691 if (initialized != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00003692 return;
William M. Brack8e2cc6f2004-07-03 23:28:52 +00003693
Daniel Veillardf93a8662004-07-01 12:56:30 +00003694 /* intialize the python extension module */
3695 Py_InitModule((char *) "libxml2mod", libxmlMethods);
3696
3697 /* initialize libxml2 */
3698 xmlInitParser();
Daniel Veillard5d819032002-02-02 21:49:17 +00003699 libxml_xmlErrorInitialize();
Daniel Veillard6361da02002-02-23 10:10:33 +00003700
Daniel Veillardf93a8662004-07-01 12:56:30 +00003701 initialized = 1;
3702
Daniel Veillard0fea6f42002-02-22 22:51:13 +00003703#ifdef MERGED_MODULES
3704 initlibxsltmod();
3705#endif
Daniel Veillardd2897fd2002-01-30 16:37:32 +00003706}