blob: 9c013a66e562c20b96a1349c67de8a9686678711 [file] [log] [blame]
Daniel Veillard3ce52572002-02-03 15:08:05 +00001/*
2 * libxml.c: this modules implements the main part of the glue of the
3 * libxml2 library and the Python interpreter. It provides the
4 * entry points where an automatically generated stub is either
5 * unpractical or would not match cleanly the Python model.
6 *
Daniel Veillard0fea6f42002-02-22 22:51:13 +00007 * If compiled with MERGED_MODULES, the entry point will be used to
8 * initialize both the libxml2 and the libxslt wrappers
9 *
Daniel Veillard3ce52572002-02-03 15:08:05 +000010 * See Copyright for the status of this software.
11 *
12 * daniel@veillard.com
13 */
Daniel Veillardd2897fd2002-01-30 16:37:32 +000014#include <Python.h>
Daniel Veillardc6d4a932002-09-12 15:00:57 +000015#include <fileobject.h>
Daniel Veillarda1196ed2002-11-23 11:22:49 +000016/* #include "config.h" */
Daniel Veillardf1d0e6b2002-01-31 23:42:44 +000017#include <libxml/xmlmemory.h>
Daniel Veillardd2897fd2002-01-30 16:37:32 +000018#include <libxml/parser.h>
19#include <libxml/tree.h>
Daniel Veillarda7340c82002-02-01 17:56:45 +000020#include <libxml/xpath.h>
Daniel Veillard5d819032002-02-02 21:49:17 +000021#include <libxml/xmlerror.h>
Daniel Veillarda7340c82002-02-01 17:56:45 +000022#include <libxml/xpathInternals.h>
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000023#include <libxml/xmlmemory.h>
Daniel Veillardc6d4a932002-09-12 15:00:57 +000024#include <libxml/xmlIO.h>
Daniel Veillardd5e198a2004-03-09 09:03:28 +000025#include <libxml/c14n.h>
Daniel Veillard438ebbd2008-05-12 12:58:46 +000026#include <libxml/xmlreader.h>
Daniel Veillardda3fee42008-09-01 13:08:57 +000027#include <libxml/xmlsave.h>
Daniel Veillardd2897fd2002-01-30 16:37:32 +000028#include "libxml_wrap.h"
Daniel Veillard96fe0952002-01-30 20:52:23 +000029#include "libxml2-py.h"
Daniel Veillardd2897fd2002-01-30 16:37:32 +000030
Roumen Petrovc3b1d092012-08-13 16:50:48 +080031#if defined(WITH_TRIO)
Daniel Veillarde4fa2932003-03-26 00:38:10 +000032#include "trio.h"
33#define vsnprintf trio_vsnprintf
Daniel Veillard0d132cf2002-12-23 14:43:32 +000034#endif
35
Daniel Veillardd2897fd2002-01-30 16:37:32 +000036/* #define DEBUG */
Daniel Veillard797a5652002-02-12 13:46:21 +000037/* #define DEBUG_SAX */
Daniel Veillarda7340c82002-02-01 17:56:45 +000038/* #define DEBUG_XPATH */
Daniel Veillard5d819032002-02-02 21:49:17 +000039/* #define DEBUG_ERROR */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000040/* #define DEBUG_MEMORY */
Daniel Veillardc6d4a932002-09-12 15:00:57 +000041/* #define DEBUG_FILES */
42/* #define DEBUG_LOADER */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000043
Daniel Veillardd2379012002-03-15 22:24:56 +000044void initlibxml2mod(void);
45
Daniel Veillardc6d4a932002-09-12 15:00:57 +000046/**
47 * TODO:
48 *
49 * macro to flag unimplemented blocks
50 */
51#define TODO \
52 xmlGenericError(xmlGenericErrorContext, \
53 "Unimplemented block at %s:%d\n", \
54 __FILE__, __LINE__);
William M. Brack8e2cc6f2004-07-03 23:28:52 +000055/*
56 * the following vars are used for XPath extensions, but
57 * are also referenced within the parser cleanup routine.
58 */
59static int libxml_xpathCallbacksInitialized = 0;
60
61typedef struct libxml_xpathCallback {
62 xmlXPathContextPtr ctx;
63 xmlChar *name;
64 xmlChar *ns_uri;
65 PyObject *function;
66} libxml_xpathCallback, *libxml_xpathCallbackPtr;
67typedef libxml_xpathCallback libxml_xpathCallbackArray[];
68static int libxml_xpathCallbacksAllocd = 10;
69static libxml_xpathCallbackArray *libxml_xpathCallbacks = NULL;
70static int libxml_xpathCallbacksNb = 0;
Daniel Veillardc6d4a932002-09-12 15:00:57 +000071
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000072/************************************************************************
73 * *
74 * Memory debug interface *
75 * *
76 ************************************************************************/
77
Daniel Veillardc2664642003-07-29 20:44:53 +000078#if 0
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000079extern void xmlMemFree(void *ptr);
80extern void *xmlMemMalloc(size_t size);
Daniel Veillardd2379012002-03-15 22:24:56 +000081extern void *xmlMemRealloc(void *ptr, size_t size);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000082extern char *xmlMemoryStrdup(const char *str);
Daniel Veillardc2664642003-07-29 20:44:53 +000083#endif
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000084
85static int libxmlMemoryDebugActivated = 0;
86static long libxmlMemoryAllocatedBase = 0;
87
88static int libxmlMemoryDebug = 0;
89static xmlFreeFunc freeFunc = NULL;
90static xmlMallocFunc mallocFunc = NULL;
91static xmlReallocFunc reallocFunc = NULL;
92static xmlStrdupFunc strdupFunc = NULL;
93
Daniel Veillardf93a8662004-07-01 12:56:30 +000094static void
95libxml_xmlErrorInitialize(void); /* forward declare */
96
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000097PyObject *
William M. Brackc68d78d2004-07-16 10:39:30 +000098libxml_xmlMemoryUsed(PyObject * self ATTRIBUTE_UNUSED,
99 PyObject * args ATTRIBUTE_UNUSED)
Daniel Veillard529233c2004-07-02 12:23:44 +0000100{
101 long ret;
102 PyObject *py_retval;
103
104 ret = xmlMemUsed();
105
106 py_retval = libxml_longWrap(ret);
107 return (py_retval);
108}
109
110PyObject *
William M. Brackc68d78d2004-07-16 10:39:30 +0000111libxml_xmlDebugMemory(PyObject * self ATTRIBUTE_UNUSED, PyObject * args)
Daniel Veillardd2379012002-03-15 22:24:56 +0000112{
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000113 int activate;
114 PyObject *py_retval;
115 long ret;
116
Daniel Veillardd2379012002-03-15 22:24:56 +0000117 if (!PyArg_ParseTuple(args, (char *) "i:xmlDebugMemory", &activate))
118 return (NULL);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000119
120#ifdef DEBUG_MEMORY
121 printf("libxml_xmlDebugMemory(%d) called\n", activate);
122#endif
123
124 if (activate != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000125 if (libxmlMemoryDebug == 0) {
126 /*
127 * First initialize the library and grab the old memory handlers
128 * and switch the library to memory debugging
129 */
130 xmlMemGet((xmlFreeFunc *) & freeFunc,
131 (xmlMallocFunc *) & mallocFunc,
132 (xmlReallocFunc *) & reallocFunc,
133 (xmlStrdupFunc *) & strdupFunc);
134 if ((freeFunc == xmlMemFree) && (mallocFunc == xmlMemMalloc) &&
135 (reallocFunc == xmlMemRealloc) &&
136 (strdupFunc == xmlMemoryStrdup)) {
137 libxmlMemoryAllocatedBase = xmlMemUsed();
138 } else {
Daniel Veillardf93a8662004-07-01 12:56:30 +0000139 /*
140 * cleanup first, because some memory has been
141 * allocated with the non-debug malloc in xmlInitParser
142 * when the python module was imported
143 */
144 xmlCleanupParser();
Daniel Veillardd2379012002-03-15 22:24:56 +0000145 ret = (long) xmlMemSetup(xmlMemFree, xmlMemMalloc,
146 xmlMemRealloc, xmlMemoryStrdup);
147 if (ret < 0)
148 goto error;
149 libxmlMemoryAllocatedBase = xmlMemUsed();
Daniel Veillardf93a8662004-07-01 12:56:30 +0000150 /* reinitialize */
151 xmlInitParser();
152 libxml_xmlErrorInitialize();
Daniel Veillardd2379012002-03-15 22:24:56 +0000153 }
Daniel Veillardd2379012002-03-15 22:24:56 +0000154 ret = 0;
155 } else if (libxmlMemoryDebugActivated == 0) {
156 libxmlMemoryAllocatedBase = xmlMemUsed();
157 ret = 0;
158 } else {
159 ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
160 }
161 libxmlMemoryDebug = 1;
162 libxmlMemoryDebugActivated = 1;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000163 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +0000164 if (libxmlMemoryDebugActivated == 1)
165 ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
166 else
167 ret = 0;
168 libxmlMemoryDebugActivated = 0;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000169 }
Daniel Veillardd2379012002-03-15 22:24:56 +0000170 error:
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000171 py_retval = libxml_longWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +0000172 return (py_retval);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000173}
174
175PyObject *
Daniel Veillardf93a8662004-07-01 12:56:30 +0000176libxml_xmlPythonCleanupParser(PyObject *self ATTRIBUTE_UNUSED,
177 PyObject *args ATTRIBUTE_UNUSED) {
178
William M. Brack8e2cc6f2004-07-03 23:28:52 +0000179 int ix;
180 long freed = -1;
Daniel Veillardf93a8662004-07-01 12:56:30 +0000181
182 if (libxmlMemoryDebug) {
183 freed = xmlMemUsed();
184 }
185
186 xmlCleanupParser();
William M. Brack8e2cc6f2004-07-03 23:28:52 +0000187 /*
188 * Need to confirm whether we really want to do this (required for
189 * memcheck) in all cases...
190 */
191
192 if (libxml_xpathCallbacks != NULL) { /* if ext funcs declared */
193 for (ix=0; ix<libxml_xpathCallbacksNb; ix++) {
194 if ((*libxml_xpathCallbacks)[ix].name != NULL)
195 xmlFree((*libxml_xpathCallbacks)[ix].name);
196 if ((*libxml_xpathCallbacks)[ix].ns_uri != NULL)
197 xmlFree((*libxml_xpathCallbacks)[ix].ns_uri);
198 }
199 libxml_xpathCallbacksNb = 0;
200 xmlFree(libxml_xpathCallbacks);
201 libxml_xpathCallbacks = NULL;
202 }
Daniel Veillardf93a8662004-07-01 12:56:30 +0000203
204 if (libxmlMemoryDebug) {
205 freed -= xmlMemUsed();
206 libxmlMemoryAllocatedBase -= freed;
207 if (libxmlMemoryAllocatedBase < 0)
208 libxmlMemoryAllocatedBase = 0;
209 }
210
211 Py_INCREF(Py_None);
212 return(Py_None);
213}
214
215PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +0000216libxml_xmlDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
217 ATTRIBUTE_UNUSED PyObject * args)
218{
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000219
220 if (libxmlMemoryDebug != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +0000221 xmlMemoryDump();
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000222 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +0000223 return (Py_None);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000224}
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000225
226/************************************************************************
227 * *
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000228 * Handling Python FILE I/O at the C level *
229 * The raw I/O attack diectly the File objects, while the *
230 * other routines address the ioWrapper instance instead *
231 * *
232 ************************************************************************/
233
234/**
235 * xmlPythonFileCloseUnref:
236 * @context: the I/O context
237 *
238 * Close an I/O channel
239 */
240static int
241xmlPythonFileCloseRaw (void * context) {
242 PyObject *file, *ret;
243
244#ifdef DEBUG_FILES
245 printf("xmlPythonFileCloseUnref\n");
246#endif
247 file = (PyObject *) context;
248 if (file == NULL) return(-1);
Daniel Veillard118aed72002-09-24 14:13:13 +0000249 ret = PyEval_CallMethod(file, (char *) "close", (char *) "()");
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000250 if (ret != NULL) {
251 Py_DECREF(ret);
252 }
253 Py_DECREF(file);
254 return(0);
255}
256
257/**
258 * xmlPythonFileReadRaw:
259 * @context: the I/O context
260 * @buffer: where to drop data
261 * @len: number of bytes to write
262 *
263 * Read @len bytes to @buffer from the Python file in the I/O channel
264 *
265 * Returns the number of bytes read
266 */
267static int
268xmlPythonFileReadRaw (void * context, char * buffer, int len) {
269 PyObject *file;
270 PyObject *ret;
271 int lenread = -1;
272 char *data;
273
274#ifdef DEBUG_FILES
275 printf("xmlPythonFileReadRaw: %d\n", len);
276#endif
277 file = (PyObject *) context;
278 if (file == NULL) return(-1);
Daniel Veillard118aed72002-09-24 14:13:13 +0000279 ret = PyEval_CallMethod(file, (char *) "read", (char *) "(i)", len);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000280 if (ret == NULL) {
281 printf("xmlPythonFileReadRaw: result is NULL\n");
282 return(-1);
283 } else if (PyString_Check(ret)) {
284 lenread = PyString_Size(ret);
285 data = PyString_AsString(ret);
286 if (lenread > len)
287 memcpy(buffer, data, len);
288 else
289 memcpy(buffer, data, lenread);
290 Py_DECREF(ret);
291 } else {
292 printf("xmlPythonFileReadRaw: result is not a String\n");
293 Py_DECREF(ret);
294 }
295 return(lenread);
296}
297
298/**
299 * xmlPythonFileRead:
300 * @context: the I/O context
301 * @buffer: where to drop data
302 * @len: number of bytes to write
303 *
304 * Read @len bytes to @buffer from the I/O channel.
305 *
306 * Returns the number of bytes read
307 */
308static int
309xmlPythonFileRead (void * context, char * buffer, int len) {
310 PyObject *file;
311 PyObject *ret;
312 int lenread = -1;
313 char *data;
314
315#ifdef DEBUG_FILES
316 printf("xmlPythonFileRead: %d\n", len);
317#endif
318 file = (PyObject *) context;
319 if (file == NULL) return(-1);
Daniel Veillard118aed72002-09-24 14:13:13 +0000320 ret = PyEval_CallMethod(file, (char *) "io_read", (char *) "(i)", len);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000321 if (ret == NULL) {
322 printf("xmlPythonFileRead: result is NULL\n");
323 return(-1);
324 } else if (PyString_Check(ret)) {
325 lenread = PyString_Size(ret);
326 data = PyString_AsString(ret);
327 if (lenread > len)
328 memcpy(buffer, data, len);
329 else
330 memcpy(buffer, data, lenread);
331 Py_DECREF(ret);
332 } else {
333 printf("xmlPythonFileRead: result is not a String\n");
334 Py_DECREF(ret);
335 }
336 return(lenread);
337}
338
339/**
340 * xmlFileWrite:
341 * @context: the I/O context
342 * @buffer: where to drop data
343 * @len: number of bytes to write
344 *
345 * Write @len bytes from @buffer to the I/O channel.
346 *
347 * Returns the number of bytes written
348 */
349static int
350xmlPythonFileWrite (void * context, const char * buffer, int len) {
351 PyObject *file;
352 PyObject *string;
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000353 PyObject *ret = NULL;
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000354 int written = -1;
355
356#ifdef DEBUG_FILES
357 printf("xmlPythonFileWrite: %d\n", len);
358#endif
359 file = (PyObject *) context;
360 if (file == NULL) return(-1);
361 string = PyString_FromStringAndSize(buffer, len);
362 if (string == NULL) return(-1);
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000363 if (PyObject_HasAttrString(file, (char *) "io_write")) {
364 ret = PyEval_CallMethod(file, (char *) "io_write", (char *) "(O)",
365 string);
366 } else if (PyObject_HasAttrString(file, (char *) "write")) {
367 ret = PyEval_CallMethod(file, (char *) "write", (char *) "(O)",
368 string);
369 }
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000370 Py_DECREF(string);
371 if (ret == NULL) {
372 printf("xmlPythonFileWrite: result is NULL\n");
373 return(-1);
374 } else if (PyInt_Check(ret)) {
375 written = (int) PyInt_AsLong(ret);
376 Py_DECREF(ret);
377 } else if (ret == Py_None) {
378 written = len;
379 Py_DECREF(ret);
380 } else {
381 printf("xmlPythonFileWrite: result is not an Int nor None\n");
382 Py_DECREF(ret);
383 }
384 return(written);
385}
386
387/**
388 * xmlPythonFileClose:
389 * @context: the I/O context
390 *
391 * Close an I/O channel
392 */
393static int
394xmlPythonFileClose (void * context) {
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000395 PyObject *file, *ret = NULL;
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000396
397#ifdef DEBUG_FILES
398 printf("xmlPythonFileClose\n");
399#endif
400 file = (PyObject *) context;
401 if (file == NULL) return(-1);
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000402 if (PyObject_HasAttrString(file, (char *) "io_close")) {
403 ret = PyEval_CallMethod(file, (char *) "io_close", (char *) "()");
404 } else if (PyObject_HasAttrString(file, (char *) "flush")) {
405 ret = PyEval_CallMethod(file, (char *) "flush", (char *) "()");
406 }
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000407 if (ret != NULL) {
408 Py_DECREF(ret);
409 }
410 return(0);
411}
412
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000413#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000414/**
415 * xmlOutputBufferCreatePythonFile:
416 * @file: a PyFile_Type
417 * @encoder: the encoding converter or NULL
418 *
419 * Create a buffered output for the progressive saving to a PyFile_Type
420 * buffered C I/O
421 *
422 * Returns the new parser output or NULL
423 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000424static xmlOutputBufferPtr
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000425xmlOutputBufferCreatePythonFile(PyObject *file,
426 xmlCharEncodingHandlerPtr encoder) {
427 xmlOutputBufferPtr ret;
428
429 if (file == NULL) return(NULL);
430
431 ret = xmlAllocOutputBuffer(encoder);
432 if (ret != NULL) {
433 ret->context = file;
434 /* Py_INCREF(file); */
435 ret->writecallback = xmlPythonFileWrite;
436 ret->closecallback = xmlPythonFileClose;
437 }
438
439 return(ret);
440}
441
442PyObject *
443libxml_xmlCreateOutputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
444 PyObject *py_retval;
445 PyObject *file;
446 xmlChar *encoding;
447 xmlCharEncodingHandlerPtr handler = NULL;
448 xmlOutputBufferPtr buffer;
449
450
451 if (!PyArg_ParseTuple(args, (char *)"Oz:xmlOutputBufferCreate",
452 &file, &encoding))
453 return(NULL);
454 if ((encoding != NULL) && (encoding[0] != 0)) {
Daniel Veillard118aed72002-09-24 14:13:13 +0000455 handler = xmlFindCharEncodingHandler((const char *) encoding);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000456 }
457 buffer = xmlOutputBufferCreatePythonFile(file, handler);
458 if (buffer == NULL)
459 printf("libxml_xmlCreateOutputBuffer: buffer == NULL\n");
460 py_retval = libxml_xmlOutputBufferPtrWrap(buffer);
461 return(py_retval);
462}
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000463
464/**
465 * libxml_outputBufferGetPythonFile:
466 * @buffer: the I/O buffer
467 *
468 * read the Python I/O from the CObject
469 *
470 * Returns the new parser output or NULL
471 */
472static PyObject *
473libxml_outputBufferGetPythonFile(ATTRIBUTE_UNUSED PyObject *self,
474 PyObject *args) {
475 PyObject *buffer;
476 PyObject *file;
477 xmlOutputBufferPtr obj;
478
479 if (!PyArg_ParseTuple(args, (char *)"O:outputBufferGetPythonFile",
480 &buffer))
481 return(NULL);
482
483 obj = PyoutputBuffer_Get(buffer);
484 if (obj == NULL) {
485 fprintf(stderr,
486 "outputBufferGetPythonFile: obj == NULL\n");
487 Py_INCREF(Py_None);
488 return(Py_None);
489 }
490 if (obj->closecallback != xmlPythonFileClose) {
491 fprintf(stderr,
492 "outputBufferGetPythonFile: not a python file wrapper\n");
493 Py_INCREF(Py_None);
494 return(Py_None);
495 }
496 file = (PyObject *) obj->context;
497 if (file == NULL) {
498 Py_INCREF(Py_None);
499 return(Py_None);
500 }
501 Py_INCREF(file);
502 return(file);
503}
504
Daniel Veillardd5e198a2004-03-09 09:03:28 +0000505static PyObject *
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000506libxml_xmlOutputBufferClose(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
507 PyObject *py_retval;
508 int c_retval;
509 xmlOutputBufferPtr out;
510 PyObject *pyobj_out;
511
512 if (!PyArg_ParseTuple(args, (char *)"O:xmlOutputBufferClose", &pyobj_out))
513 return(NULL);
514 out = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_out);
Daniel Veillard263ec862004-10-04 10:26:54 +0000515 /* Buffer may already have been destroyed elsewhere. This is harmless. */
516 if (out == NULL) {
517 Py_INCREF(Py_None);
518 return(Py_None);
519 }
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000520
521 c_retval = xmlOutputBufferClose(out);
522 py_retval = libxml_intWrap((int) c_retval);
523 return(py_retval);
524}
525
Daniel Veillardd5e198a2004-03-09 09:03:28 +0000526static PyObject *
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000527libxml_xmlOutputBufferFlush(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
528 PyObject *py_retval;
529 int c_retval;
530 xmlOutputBufferPtr out;
531 PyObject *pyobj_out;
532
533 if (!PyArg_ParseTuple(args, (char *)"O:xmlOutputBufferFlush", &pyobj_out))
534 return(NULL);
535 out = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_out);
536
537 c_retval = xmlOutputBufferFlush(out);
538 py_retval = libxml_intWrap((int) c_retval);
539 return(py_retval);
540}
Daniel Veillard263ec862004-10-04 10:26:54 +0000541
542static PyObject *
543libxml_xmlSaveFileTo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
544 PyObject *py_retval;
545 int c_retval;
546 xmlOutputBufferPtr buf;
547 PyObject *pyobj_buf;
548 xmlDocPtr cur;
549 PyObject *pyobj_cur;
550 char * encoding;
551
552 if (!PyArg_ParseTuple(args, (char *)"OOz:xmlSaveFileTo", &pyobj_buf, &pyobj_cur, &encoding))
553 return(NULL);
554 buf = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_buf);
555 cur = (xmlDocPtr) PyxmlNode_Get(pyobj_cur);
556
557 c_retval = xmlSaveFileTo(buf, cur, encoding);
558 /* xmlSaveTo() freed the memory pointed to by buf, so record that in the
559 * Python object. */
560 ((PyoutputBuffer_Object *)(pyobj_buf))->obj = NULL;
561 py_retval = libxml_intWrap((int) c_retval);
562 return(py_retval);
563}
564
565static PyObject *
566libxml_xmlSaveFormatFileTo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
567 PyObject *py_retval;
568 int c_retval;
569 xmlOutputBufferPtr buf;
570 PyObject *pyobj_buf;
571 xmlDocPtr cur;
572 PyObject *pyobj_cur;
573 char * encoding;
574 int format;
575
576 if (!PyArg_ParseTuple(args, (char *)"OOzi:xmlSaveFormatFileTo", &pyobj_buf, &pyobj_cur, &encoding, &format))
577 return(NULL);
578 buf = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_buf);
579 cur = (xmlDocPtr) PyxmlNode_Get(pyobj_cur);
580
581 c_retval = xmlSaveFormatFileTo(buf, cur, encoding, format);
582 /* xmlSaveFormatFileTo() freed the memory pointed to by buf, so record that
583 * in the Python object */
584 ((PyoutputBuffer_Object *)(pyobj_buf))->obj = NULL;
585 py_retval = libxml_intWrap((int) c_retval);
586 return(py_retval);
587}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000588#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000589
590
591/**
592 * xmlParserInputBufferCreatePythonFile:
593 * @file: a PyFile_Type
594 * @encoder: the encoding converter or NULL
595 *
596 * Create a buffered output for the progressive saving to a PyFile_Type
597 * buffered C I/O
598 *
599 * Returns the new parser output or NULL
600 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000601static xmlParserInputBufferPtr
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000602xmlParserInputBufferCreatePythonFile(PyObject *file,
603 xmlCharEncoding encoding) {
604 xmlParserInputBufferPtr ret;
605
606 if (file == NULL) return(NULL);
607
608 ret = xmlAllocParserInputBuffer(encoding);
609 if (ret != NULL) {
610 ret->context = file;
611 /* Py_INCREF(file); */
612 ret->readcallback = xmlPythonFileRead;
613 ret->closecallback = xmlPythonFileClose;
614 }
615
616 return(ret);
617}
618
619PyObject *
620libxml_xmlCreateInputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
621 PyObject *py_retval;
622 PyObject *file;
623 xmlChar *encoding;
624 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
625 xmlParserInputBufferPtr buffer;
626
627
628 if (!PyArg_ParseTuple(args, (char *)"Oz:xmlParserInputBufferCreate",
629 &file, &encoding))
630 return(NULL);
631 if ((encoding != NULL) && (encoding[0] != 0)) {
Daniel Veillard118aed72002-09-24 14:13:13 +0000632 enc = xmlParseCharEncoding((const char *) encoding);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000633 }
634 buffer = xmlParserInputBufferCreatePythonFile(file, enc);
635 if (buffer == NULL)
636 printf("libxml_xmlParserInputBufferCreate: buffer == NULL\n");
637 py_retval = libxml_xmlParserInputBufferPtrWrap(buffer);
638 return(py_retval);
639}
640
641/************************************************************************
642 * *
643 * Providing the resolver at the Python level *
644 * *
645 ************************************************************************/
646
647static xmlExternalEntityLoader defaultExternalEntityLoader = NULL;
648static PyObject *pythonExternalEntityLoaderObjext;
649
650static xmlParserInputPtr
651pythonExternalEntityLoader(const char *URL, const char *ID,
652 xmlParserCtxtPtr ctxt) {
653 xmlParserInputPtr result = NULL;
654 if (pythonExternalEntityLoaderObjext != NULL) {
655 PyObject *ret;
656 PyObject *ctxtobj;
657
658 ctxtobj = libxml_xmlParserCtxtPtrWrap(ctxt);
659#ifdef DEBUG_LOADER
660 printf("pythonExternalEntityLoader: ready to call\n");
661#endif
662
663 ret = PyObject_CallFunction(pythonExternalEntityLoaderObjext,
Daniel Veillard118aed72002-09-24 14:13:13 +0000664 (char *) "(ssO)", URL, ID, ctxtobj);
Daniel Veillarde4a07e72003-01-14 14:40:25 +0000665 Py_XDECREF(ctxtobj);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000666#ifdef DEBUG_LOADER
667 printf("pythonExternalEntityLoader: result ");
Daniel Veillard007d51e2003-09-17 20:07:28 +0000668 PyObject_Print(ret, stderr, 0);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000669 printf("\n");
670#endif
671
672 if (ret != NULL) {
Daniel Veillard118aed72002-09-24 14:13:13 +0000673 if (PyObject_HasAttrString(ret, (char *) "read")) {
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000674 xmlParserInputBufferPtr buf;
675
676 buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
677 if (buf != NULL) {
678 buf->context = ret;
679 buf->readcallback = xmlPythonFileReadRaw;
680 buf->closecallback = xmlPythonFileCloseRaw;
681 result = xmlNewIOInputStream(ctxt, buf,
682 XML_CHAR_ENCODING_NONE);
683 }
Daniel Veillard4ea89f02005-07-29 10:12:45 +0000684#if 0
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000685 } else {
Daniel Veillard4ea89f02005-07-29 10:12:45 +0000686 if (URL != NULL)
687 printf("pythonExternalEntityLoader: can't read %s\n",
688 URL);
689#endif
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000690 }
691 if (result == NULL) {
692 Py_DECREF(ret);
Daniel Veillardc64b8e92003-02-24 11:47:13 +0000693 } else if (URL != NULL) {
William M. Brackc1939562003-08-05 15:52:22 +0000694 result->filename = (char *) xmlStrdup((const xmlChar *)URL);
Daniel Veillardc64b8e92003-02-24 11:47:13 +0000695 result->directory = xmlParserGetDirectory((const char *) URL);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000696 }
697 }
698 }
699 if ((result == NULL) && (defaultExternalEntityLoader != NULL)) {
700 result = defaultExternalEntityLoader(URL, ID, ctxt);
701 }
702 return(result);
703}
704
705PyObject *
706libxml_xmlSetEntityLoader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
707 PyObject *py_retval;
708 PyObject *loader;
709
710 if (!PyArg_ParseTuple(args, (char *)"O:libxml_xmlSetEntityLoader",
711 &loader))
712 return(NULL);
713
714#ifdef DEBUG_LOADER
715 printf("libxml_xmlSetEntityLoader\n");
716#endif
717 if (defaultExternalEntityLoader == NULL)
718 defaultExternalEntityLoader = xmlGetExternalEntityLoader();
719
720 pythonExternalEntityLoaderObjext = loader;
721 xmlSetExternalEntityLoader(pythonExternalEntityLoader);
722
723 py_retval = PyInt_FromLong(0);
724 return(py_retval);
725}
726
Alexey Neyman48da90b2013-02-25 15:54:25 +0800727/************************************************************************
728 * *
729 * Input callback registration *
730 * *
731 ************************************************************************/
732static PyObject *pythonInputOpenCallbackObject;
733static int pythonInputCallbackID = -1;
734
735static int
736pythonInputMatchCallback(ATTRIBUTE_UNUSED const char *URI)
737{
738 /* Always return success, real decision whether URI is supported will be
739 * made in open callback. */
740 return 1;
741}
742
743static void *
744pythonInputOpenCallback(const char *URI)
745{
746 PyObject *ret;
747
748 ret = PyObject_CallFunction(pythonInputOpenCallbackObject,
749 (char *)"s", URI);
750 if (ret == Py_None) {
751 Py_DECREF(Py_None);
752 return NULL;
753 }
754 return ret;
755}
756
757PyObject *
758libxml_xmlRegisterInputCallback(ATTRIBUTE_UNUSED PyObject *self,
759 PyObject *args) {
760 PyObject *cb;
761
762 if (!PyArg_ParseTuple(args,
763 (const char *)"O:libxml_xmlRegisterInputCallback", &cb))
764 return(NULL);
765
766 if (!PyCallable_Check(cb)) {
767 PyErr_SetString(PyExc_ValueError, "input callback is not callable");
768 return(NULL);
769 }
770
771 /* Python module registers a single callback and manages the list of
772 * all callbacks internally. This is necessitated by xmlInputMatchCallback
773 * API, which does not allow for passing of data objects to discriminate
774 * different Python methods. */
775 if (pythonInputCallbackID == -1) {
776 pythonInputCallbackID = xmlRegisterInputCallbacks(
777 pythonInputMatchCallback, pythonInputOpenCallback,
778 xmlPythonFileReadRaw, xmlPythonFileCloseRaw);
779 if (pythonInputCallbackID == -1)
780 return PyErr_NoMemory();
781 pythonInputOpenCallbackObject = cb;
782 Py_INCREF(pythonInputOpenCallbackObject);
783 }
784
785 Py_INCREF(Py_None);
786 return(Py_None);
787}
788
789PyObject *
790libxml_xmlUnregisterInputCallback(ATTRIBUTE_UNUSED PyObject *self,
791 ATTRIBUTE_UNUSED PyObject *args) {
792 int ret;
793
794 ret = xmlPopInputCallbacks();
795 if (pythonInputCallbackID != -1) {
796 /* Assert that the right input callback was popped. libxml's API does not
797 * allow removal by ID, so all that could be done is an assert. */
798 if (pythonInputCallbackID == ret) {
799 pythonInputCallbackID = -1;
800 Py_DECREF(pythonInputOpenCallbackObject);
801 pythonInputOpenCallbackObject = NULL;
802 } else {
803 PyErr_SetString(PyExc_AssertionError, "popped non-python input callback");
804 return(NULL);
805 }
806 } else if (ret == -1) {
807 /* No more callbacks to pop */
808 PyErr_SetString(PyExc_IndexError, "no input callbacks to pop");
809 return(NULL);
810 }
811
812 Py_INCREF(Py_None);
813 return(Py_None);
814}
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000815
816/************************************************************************
817 * *
Daniel Veillard3ce52572002-02-03 15:08:05 +0000818 * Handling SAX/xmllib/sgmlop callback interfaces *
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000819 * *
820 ************************************************************************/
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000821
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000822static void
823pythonStartElement(void *user_data, const xmlChar * name,
824 const xmlChar ** attrs)
825{
826 int i;
827 PyObject *handler;
828 PyObject *dict;
829 PyObject *attrname;
830 PyObject *attrvalue;
Daniel Veillardd2379012002-03-15 22:24:56 +0000831 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000832 int type = 0;
833
Daniel Veillard797a5652002-02-12 13:46:21 +0000834#ifdef DEBUG_SAX
835 printf("pythonStartElement(%s) called\n", name);
836#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000837 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000838 if (PyObject_HasAttrString(handler, (char *) "startElement"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000839 type = 1;
Daniel Veillardd2379012002-03-15 22:24:56 +0000840 else if (PyObject_HasAttrString(handler, (char *) "start"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000841 type = 2;
842 if (type != 0) {
843 /*
844 * the xmllib interface always generate a dictionnary,
845 * possibly empty
846 */
847 if ((attrs == NULL) && (type == 1)) {
848 Py_XINCREF(Py_None);
849 dict = Py_None;
Daniel Veillardd2379012002-03-15 22:24:56 +0000850 } else if (attrs == NULL) {
851 dict = PyDict_New();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000852 } else {
853 dict = PyDict_New();
854 for (i = 0; attrs[i] != NULL; i++) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000855 attrname = PyString_FromString((char *) attrs[i]);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000856 i++;
857 if (attrs[i] != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000858 attrvalue = PyString_FromString((char *) attrs[i]);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000859 } else {
860 Py_XINCREF(Py_None);
861 attrvalue = Py_None;
862 }
863 PyDict_SetItem(dict, attrname, attrvalue);
Daniel Veillarda3d23052007-01-09 21:24:34 +0000864 Py_DECREF(attrname);
865 Py_DECREF(attrvalue);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000866 }
867 }
868
869 if (type == 1)
Daniel Veillardd2379012002-03-15 22:24:56 +0000870 result = PyObject_CallMethod(handler, (char *) "startElement",
871 (char *) "sO", name, dict);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000872 else if (type == 2)
Daniel Veillardd2379012002-03-15 22:24:56 +0000873 result = PyObject_CallMethod(handler, (char *) "start",
874 (char *) "sO", name, dict);
875 if (PyErr_Occurred())
876 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000877 Py_XDECREF(dict);
878 Py_XDECREF(result);
879 }
880}
881
882static void
883pythonStartDocument(void *user_data)
884{
885 PyObject *handler;
886 PyObject *result;
887
Daniel Veillard797a5652002-02-12 13:46:21 +0000888#ifdef DEBUG_SAX
889 printf("pythonStartDocument() called\n");
890#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000891 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000892 if (PyObject_HasAttrString(handler, (char *) "startDocument")) {
893 result =
894 PyObject_CallMethod(handler, (char *) "startDocument", NULL);
895 if (PyErr_Occurred())
896 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000897 Py_XDECREF(result);
898 }
899}
900
901static void
902pythonEndDocument(void *user_data)
903{
904 PyObject *handler;
905 PyObject *result;
906
Daniel Veillard797a5652002-02-12 13:46:21 +0000907#ifdef DEBUG_SAX
908 printf("pythonEndDocument() called\n");
909#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000910 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000911 if (PyObject_HasAttrString(handler, (char *) "endDocument")) {
912 result =
913 PyObject_CallMethod(handler, (char *) "endDocument", NULL);
914 if (PyErr_Occurred())
915 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000916 Py_XDECREF(result);
917 }
918 /*
919 * The reference to the handler is released there
920 */
921 Py_XDECREF(handler);
922}
923
924static void
925pythonEndElement(void *user_data, const xmlChar * name)
926{
927 PyObject *handler;
928 PyObject *result;
929
Daniel Veillard797a5652002-02-12 13:46:21 +0000930#ifdef DEBUG_SAX
931 printf("pythonEndElement(%s) called\n", name);
932#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000933 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000934 if (PyObject_HasAttrString(handler, (char *) "endElement")) {
935 result = PyObject_CallMethod(handler, (char *) "endElement",
936 (char *) "s", name);
937 if (PyErr_Occurred())
938 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000939 Py_XDECREF(result);
Daniel Veillardd2379012002-03-15 22:24:56 +0000940 } else if (PyObject_HasAttrString(handler, (char *) "end")) {
941 result = PyObject_CallMethod(handler, (char *) "end",
942 (char *) "s", name);
943 if (PyErr_Occurred())
944 PyErr_Print();
Daniel Veillard797a5652002-02-12 13:46:21 +0000945 Py_XDECREF(result);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000946 }
947}
948
949static void
950pythonReference(void *user_data, const xmlChar * name)
951{
952 PyObject *handler;
953 PyObject *result;
954
Daniel Veillard797a5652002-02-12 13:46:21 +0000955#ifdef DEBUG_SAX
956 printf("pythonReference(%s) called\n", name);
957#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000958 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000959 if (PyObject_HasAttrString(handler, (char *) "reference")) {
960 result = PyObject_CallMethod(handler, (char *) "reference",
961 (char *) "s", name);
962 if (PyErr_Occurred())
963 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000964 Py_XDECREF(result);
965 }
966}
967
968static void
969pythonCharacters(void *user_data, const xmlChar * ch, int len)
970{
971 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +0000972 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000973 int type = 0;
974
Daniel Veillard797a5652002-02-12 13:46:21 +0000975#ifdef DEBUG_SAX
976 printf("pythonCharacters(%s, %d) called\n", ch, len);
977#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000978 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000979 if (PyObject_HasAttrString(handler, (char *) "characters"))
980 type = 1;
981 else if (PyObject_HasAttrString(handler, (char *) "data"))
982 type = 2;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000983 if (type != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000984 if (type == 1)
985 result = PyObject_CallMethod(handler, (char *) "characters",
986 (char *) "s#", ch, len);
987 else if (type == 2)
988 result = PyObject_CallMethod(handler, (char *) "data",
989 (char *) "s#", ch, len);
990 if (PyErr_Occurred())
991 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000992 Py_XDECREF(result);
993 }
994}
995
996static void
997pythonIgnorableWhitespace(void *user_data, const xmlChar * ch, int len)
998{
999 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +00001000 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001001 int type = 0;
1002
Daniel Veillard797a5652002-02-12 13:46:21 +00001003#ifdef DEBUG_SAX
1004 printf("pythonIgnorableWhitespace(%s, %d) called\n", ch, len);
1005#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001006 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001007 if (PyObject_HasAttrString(handler, (char *) "ignorableWhitespace"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001008 type = 1;
Daniel Veillardd2379012002-03-15 22:24:56 +00001009 else if (PyObject_HasAttrString(handler, (char *) "data"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001010 type = 2;
1011 if (type != 0) {
1012 if (type == 1)
1013 result =
Daniel Veillardd2379012002-03-15 22:24:56 +00001014 PyObject_CallMethod(handler,
1015 (char *) "ignorableWhitespace",
1016 (char *) "s#", ch, len);
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001017 else if (type == 2)
Daniel Veillardd2379012002-03-15 22:24:56 +00001018 result =
1019 PyObject_CallMethod(handler, (char *) "data",
1020 (char *) "s#", ch, len);
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001021 Py_XDECREF(result);
1022 }
1023}
1024
1025static void
1026pythonProcessingInstruction(void *user_data,
1027 const xmlChar * target, const xmlChar * data)
1028{
1029 PyObject *handler;
1030 PyObject *result;
1031
Daniel Veillard797a5652002-02-12 13:46:21 +00001032#ifdef DEBUG_SAX
1033 printf("pythonProcessingInstruction(%s, %s) called\n", target, data);
1034#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001035 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001036 if (PyObject_HasAttrString(handler, (char *) "processingInstruction")) {
1037 result = PyObject_CallMethod(handler, (char *)
1038 "processingInstruction",
1039 (char *) "ss", target, data);
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001040 Py_XDECREF(result);
1041 }
1042}
1043
1044static void
1045pythonComment(void *user_data, const xmlChar * value)
1046{
1047 PyObject *handler;
1048 PyObject *result;
1049
Daniel Veillard797a5652002-02-12 13:46:21 +00001050#ifdef DEBUG_SAX
1051 printf("pythonComment(%s) called\n", value);
1052#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001053 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001054 if (PyObject_HasAttrString(handler, (char *) "comment")) {
1055 result =
1056 PyObject_CallMethod(handler, (char *) "comment", (char *) "s",
1057 value);
1058 if (PyErr_Occurred())
1059 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001060 Py_XDECREF(result);
1061 }
1062}
1063
1064static void
1065pythonWarning(void *user_data, const char *msg, ...)
1066{
1067 PyObject *handler;
1068 PyObject *result;
1069 va_list args;
1070 char buf[1024];
1071
Daniel Veillard797a5652002-02-12 13:46:21 +00001072#ifdef DEBUG_SAX
1073 printf("pythonWarning(%s) called\n", msg);
1074#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001075 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001076 if (PyObject_HasAttrString(handler, (char *) "warning")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001077 va_start(args, msg);
1078 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +00001079 va_end(args);
1080 buf[1023] = 0;
1081 result =
1082 PyObject_CallMethod(handler, (char *) "warning", (char *) "s",
1083 buf);
1084 if (PyErr_Occurred())
1085 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001086 Py_XDECREF(result);
1087 }
1088}
1089
1090static void
1091pythonError(void *user_data, const char *msg, ...)
1092{
1093 PyObject *handler;
1094 PyObject *result;
1095 va_list args;
1096 char buf[1024];
1097
Daniel Veillard797a5652002-02-12 13:46:21 +00001098#ifdef DEBUG_SAX
1099 printf("pythonError(%s) called\n", msg);
1100#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001101 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001102 if (PyObject_HasAttrString(handler, (char *) "error")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001103 va_start(args, msg);
1104 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +00001105 va_end(args);
1106 buf[1023] = 0;
1107 result =
1108 PyObject_CallMethod(handler, (char *) "error", (char *) "s",
1109 buf);
1110 if (PyErr_Occurred())
1111 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001112 Py_XDECREF(result);
1113 }
1114}
1115
1116static void
1117pythonFatalError(void *user_data, const char *msg, ...)
1118{
1119 PyObject *handler;
1120 PyObject *result;
1121 va_list args;
1122 char buf[1024];
1123
Daniel Veillard797a5652002-02-12 13:46:21 +00001124#ifdef DEBUG_SAX
1125 printf("pythonFatalError(%s) called\n", msg);
1126#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001127 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001128 if (PyObject_HasAttrString(handler, (char *) "fatalError")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001129 va_start(args, msg);
1130 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +00001131 va_end(args);
1132 buf[1023] = 0;
1133 result =
1134 PyObject_CallMethod(handler, (char *) "fatalError",
1135 (char *) "s", buf);
1136 if (PyErr_Occurred())
1137 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001138 Py_XDECREF(result);
1139 }
1140}
1141
1142static void
1143pythonCdataBlock(void *user_data, const xmlChar * ch, int len)
1144{
1145 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +00001146 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001147 int type = 0;
1148
Daniel Veillard797a5652002-02-12 13:46:21 +00001149#ifdef DEBUG_SAX
1150 printf("pythonCdataBlock(%s, %d) called\n", ch, len);
1151#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001152 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001153 if (PyObject_HasAttrString(handler, (char *) "cdataBlock"))
1154 type = 1;
1155 else if (PyObject_HasAttrString(handler, (char *) "cdata"))
1156 type = 2;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001157 if (type != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001158 if (type == 1)
1159 result =
1160 PyObject_CallMethod(handler, (char *) "cdataBlock",
1161 (char *) "s#", ch, len);
1162 else if (type == 2)
1163 result =
1164 PyObject_CallMethod(handler, (char *) "cdata",
1165 (char *) "s#", ch, len);
1166 if (PyErr_Occurred())
1167 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001168 Py_XDECREF(result);
1169 }
1170}
1171
1172static void
1173pythonExternalSubset(void *user_data,
1174 const xmlChar * name,
1175 const xmlChar * externalID, const xmlChar * systemID)
1176{
1177 PyObject *handler;
1178 PyObject *result;
1179
Daniel Veillard797a5652002-02-12 13:46:21 +00001180#ifdef DEBUG_SAX
1181 printf("pythonExternalSubset(%s, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001182 name, externalID, systemID);
Daniel Veillard797a5652002-02-12 13:46:21 +00001183#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001184 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001185 if (PyObject_HasAttrString(handler, (char *) "externalSubset")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001186 result =
Daniel Veillardd2379012002-03-15 22:24:56 +00001187 PyObject_CallMethod(handler, (char *) "externalSubset",
1188 (char *) "sss", name, externalID,
1189 systemID);
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001190 Py_XDECREF(result);
1191 }
1192}
1193
1194static void
1195pythonEntityDecl(void *user_data,
1196 const xmlChar * name,
1197 int type,
1198 const xmlChar * publicId,
1199 const xmlChar * systemId, xmlChar * content)
1200{
1201 PyObject *handler;
1202 PyObject *result;
1203
1204 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001205 if (PyObject_HasAttrString(handler, (char *) "entityDecl")) {
1206 result = PyObject_CallMethod(handler, (char *) "entityDecl",
1207 (char *) "sisss", name, type,
1208 publicId, systemId, content);
1209 if (PyErr_Occurred())
1210 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001211 Py_XDECREF(result);
1212 }
1213}
1214
1215
1216
1217static void
1218
1219pythonNotationDecl(void *user_data,
1220 const xmlChar * name,
1221 const xmlChar * publicId, const xmlChar * systemId)
1222{
1223 PyObject *handler;
1224 PyObject *result;
1225
1226 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001227 if (PyObject_HasAttrString(handler, (char *) "notationDecl")) {
1228 result = PyObject_CallMethod(handler, (char *) "notationDecl",
1229 (char *) "sss", name, publicId,
1230 systemId);
1231 if (PyErr_Occurred())
1232 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001233 Py_XDECREF(result);
1234 }
1235}
1236
1237static void
1238pythonAttributeDecl(void *user_data,
1239 const xmlChar * elem,
1240 const xmlChar * name,
1241 int type,
1242 int def,
Daniel Veillardd2379012002-03-15 22:24:56 +00001243 const xmlChar * defaultValue, xmlEnumerationPtr tree)
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001244{
1245 PyObject *handler;
1246 PyObject *nameList;
1247 PyObject *newName;
1248 xmlEnumerationPtr node;
1249 PyObject *result;
1250 int count;
1251
1252 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001253 if (PyObject_HasAttrString(handler, (char *) "attributeDecl")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001254 count = 0;
1255 for (node = tree; node != NULL; node = node->next) {
1256 count++;
1257 }
1258 nameList = PyList_New(count);
1259 count = 0;
1260 for (node = tree; node != NULL; node = node->next) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001261 newName = PyString_FromString((char *) node->name);
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001262 PyList_SetItem(nameList, count, newName);
Daniel Veillarda3d23052007-01-09 21:24:34 +00001263 Py_DECREF(newName);
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001264 count++;
1265 }
Daniel Veillardd2379012002-03-15 22:24:56 +00001266 result = PyObject_CallMethod(handler, (char *) "attributeDecl",
1267 (char *) "ssiisO", elem, name, type,
1268 def, defaultValue, nameList);
1269 if (PyErr_Occurred())
1270 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001271 Py_XDECREF(nameList);
1272 Py_XDECREF(result);
1273 }
1274}
1275
1276static void
1277pythonElementDecl(void *user_data,
1278 const xmlChar * name,
Daniel Veillardd2379012002-03-15 22:24:56 +00001279 int type, ATTRIBUTE_UNUSED xmlElementContentPtr content)
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001280{
1281 PyObject *handler;
1282 PyObject *obj;
1283 PyObject *result;
1284
1285 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001286 if (PyObject_HasAttrString(handler, (char *) "elementDecl")) {
1287 /* TODO: wrap in an elementContent object */
1288 printf
1289 ("pythonElementDecl: xmlElementContentPtr wrapper missing !\n");
1290 obj = Py_None;
1291 /* Py_XINCREF(Py_None); isn't the reference just borrowed ??? */
1292 result = PyObject_CallMethod(handler, (char *) "elementDecl",
1293 (char *) "siO", name, type, obj);
1294 if (PyErr_Occurred())
1295 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001296 Py_XDECREF(result);
1297 }
1298}
1299
1300static void
1301pythonUnparsedEntityDecl(void *user_data,
1302 const xmlChar * name,
1303 const xmlChar * publicId,
1304 const xmlChar * systemId,
1305 const xmlChar * notationName)
1306{
1307 PyObject *handler;
1308 PyObject *result;
1309
1310 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001311 if (PyObject_HasAttrString(handler, (char *) "unparsedEntityDecl")) {
1312 result =
1313 PyObject_CallMethod(handler, (char *) "unparsedEntityDecl",
1314 (char *) "ssss", name, publicId, systemId,
1315 notationName);
1316 if (PyErr_Occurred())
1317 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001318 Py_XDECREF(result);
1319 }
1320}
1321
1322static void
1323pythonInternalSubset(void *user_data, const xmlChar * name,
1324 const xmlChar * ExternalID, const xmlChar * SystemID)
1325{
1326 PyObject *handler;
1327 PyObject *result;
1328
Daniel Veillard797a5652002-02-12 13:46:21 +00001329#ifdef DEBUG_SAX
1330 printf("pythonInternalSubset(%s, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001331 name, ExternalID, SystemID);
Daniel Veillard797a5652002-02-12 13:46:21 +00001332#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001333 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001334 if (PyObject_HasAttrString(handler, (char *) "internalSubset")) {
1335 result = PyObject_CallMethod(handler, (char *) "internalSubset",
1336 (char *) "sss", name, ExternalID,
1337 SystemID);
1338 if (PyErr_Occurred())
1339 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001340 Py_XDECREF(result);
1341 }
1342}
1343
1344static xmlSAXHandler pythonSaxHandler = {
1345 pythonInternalSubset,
Daniel Veillardd2379012002-03-15 22:24:56 +00001346 NULL, /* TODO pythonIsStandalone, */
1347 NULL, /* TODO pythonHasInternalSubset, */
1348 NULL, /* TODO pythonHasExternalSubset, */
1349 NULL, /* TODO pythonResolveEntity, */
1350 NULL, /* TODO pythonGetEntity, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001351 pythonEntityDecl,
1352 pythonNotationDecl,
1353 pythonAttributeDecl,
1354 pythonElementDecl,
1355 pythonUnparsedEntityDecl,
Daniel Veillardd2379012002-03-15 22:24:56 +00001356 NULL, /* OBSOLETED pythonSetDocumentLocator, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001357 pythonStartDocument,
1358 pythonEndDocument,
1359 pythonStartElement,
1360 pythonEndElement,
1361 pythonReference,
1362 pythonCharacters,
1363 pythonIgnorableWhitespace,
1364 pythonProcessingInstruction,
1365 pythonComment,
1366 pythonWarning,
1367 pythonError,
1368 pythonFatalError,
Daniel Veillardd2379012002-03-15 22:24:56 +00001369 NULL, /* TODO pythonGetParameterEntity, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001370 pythonCdataBlock,
1371 pythonExternalSubset,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001372 1,
1373 NULL, /* TODO mograte to SAX2 */
1374 NULL,
William M. Brack871611b2003-10-18 04:53:14 +00001375 NULL,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001376 NULL
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001377};
Daniel Veillard3ce52572002-02-03 15:08:05 +00001378
1379/************************************************************************
1380 * *
1381 * Handling of specific parser context *
1382 * *
1383 ************************************************************************/
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001384
1385PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001386libxml_xmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
1387 PyObject * args)
1388{
1389 const char *chunk;
Daniel Veillard3ce52572002-02-03 15:08:05 +00001390 int size;
Daniel Veillardd2379012002-03-15 22:24:56 +00001391 const char *URI;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001392 PyObject *pyobj_SAX = NULL;
Daniel Veillard3ce52572002-02-03 15:08:05 +00001393 xmlSAXHandlerPtr SAX = NULL;
Daniel Veillard3ce52572002-02-03 15:08:05 +00001394 xmlParserCtxtPtr ret;
1395 PyObject *pyret;
Daniel Veillard96fe0952002-01-30 20:52:23 +00001396
Daniel Veillardd2379012002-03-15 22:24:56 +00001397 if (!PyArg_ParseTuple
1398 (args, (char *) "Oziz:xmlCreatePushParser", &pyobj_SAX, &chunk,
1399 &size, &URI))
1400 return (NULL);
Daniel Veillard3ce52572002-02-03 15:08:05 +00001401
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001402#ifdef DEBUG
Daniel Veillard3ce52572002-02-03 15:08:05 +00001403 printf("libxml_xmlCreatePushParser(%p, %s, %d, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001404 pyobj_SAX, chunk, size, URI);
Daniel Veillard96fe0952002-01-30 20:52:23 +00001405#endif
Daniel Veillard3ce52572002-02-03 15:08:05 +00001406 if (pyobj_SAX != Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001407 SAX = &pythonSaxHandler;
1408 Py_INCREF(pyobj_SAX);
1409 /* The reference is released in pythonEndDocument() */
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001410 }
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001411 ret = xmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI);
Daniel Veillard3ce52572002-02-03 15:08:05 +00001412 pyret = libxml_xmlParserCtxtPtrWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +00001413 return (pyret);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001414}
Daniel Veillard5d819032002-02-02 21:49:17 +00001415
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001416PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001417libxml_htmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
1418 PyObject * args)
1419{
Daniel Veillard656ce942004-04-30 23:11:45 +00001420#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001421 const char *chunk;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001422 int size;
Daniel Veillardd2379012002-03-15 22:24:56 +00001423 const char *URI;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001424 PyObject *pyobj_SAX = NULL;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001425 xmlSAXHandlerPtr SAX = NULL;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001426 xmlParserCtxtPtr ret;
1427 PyObject *pyret;
1428
Daniel Veillardd2379012002-03-15 22:24:56 +00001429 if (!PyArg_ParseTuple
1430 (args, (char *) "Oziz:htmlCreatePushParser", &pyobj_SAX, &chunk,
1431 &size, &URI))
1432 return (NULL);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001433
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001434#ifdef DEBUG
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001435 printf("libxml_htmlCreatePushParser(%p, %s, %d, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001436 pyobj_SAX, chunk, size, URI);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001437#endif
1438 if (pyobj_SAX != Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001439 SAX = &pythonSaxHandler;
1440 Py_INCREF(pyobj_SAX);
1441 /* The reference is released in pythonEndDocument() */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001442 }
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001443 ret = htmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI,
Daniel Veillardd2379012002-03-15 22:24:56 +00001444 XML_CHAR_ENCODING_NONE);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001445 pyret = libxml_xmlParserCtxtPtrWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +00001446 return (pyret);
Daniel Veillard656ce942004-04-30 23:11:45 +00001447#else
1448 Py_INCREF(Py_None);
1449 return (Py_None);
1450#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001451}
1452
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001453PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001454libxml_xmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1455{
Akira TAGOH961b5352012-07-03 14:13:59 +09001456#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001457 int recover;
Daniel Veillardd2379012002-03-15 22:24:56 +00001458 const char *URI;
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001459 PyObject *pyobj_SAX = NULL;
1460 xmlSAXHandlerPtr SAX = NULL;
1461
Daniel Veillardd2379012002-03-15 22:24:56 +00001462 if (!PyArg_ParseTuple(args, (char *) "Osi:xmlSAXParseFile", &pyobj_SAX,
1463 &URI, &recover))
1464 return (NULL);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001465
1466#ifdef DEBUG
1467 printf("libxml_xmlSAXParseFile(%p, %s, %d) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001468 pyobj_SAX, URI, recover);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001469#endif
1470 if (pyobj_SAX == Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001471 Py_INCREF(Py_None);
1472 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001473 }
1474 SAX = &pythonSaxHandler;
1475 Py_INCREF(pyobj_SAX);
1476 /* The reference is released in pythonEndDocument() */
Daniel Veillardf2531af2005-03-31 11:06:29 +00001477 xmlSAXUserParseFile(SAX, pyobj_SAX, URI);
Akira TAGOH961b5352012-07-03 14:13:59 +09001478#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001479 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +00001480 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001481}
1482
1483PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001484libxml_htmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1485{
Daniel Veillard656ce942004-04-30 23:11:45 +00001486#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001487 const char *URI;
1488 const char *encoding;
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001489 PyObject *pyobj_SAX = NULL;
1490 xmlSAXHandlerPtr SAX = NULL;
1491
Daniel Veillardd2379012002-03-15 22:24:56 +00001492 if (!PyArg_ParseTuple
1493 (args, (char *) "Osz:htmlSAXParseFile", &pyobj_SAX, &URI,
1494 &encoding))
1495 return (NULL);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001496
1497#ifdef DEBUG
1498 printf("libxml_htmlSAXParseFile(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001499 pyobj_SAX, URI, encoding);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001500#endif
1501 if (pyobj_SAX == Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001502 Py_INCREF(Py_None);
1503 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001504 }
1505 SAX = &pythonSaxHandler;
1506 Py_INCREF(pyobj_SAX);
1507 /* The reference is released in pythonEndDocument() */
1508 htmlSAXParseFile(URI, encoding, SAX, pyobj_SAX);
1509 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +00001510 return (Py_None);
Daniel Veillard656ce942004-04-30 23:11:45 +00001511#else
1512 Py_INCREF(Py_None);
1513 return (Py_None);
1514#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001515}
1516
Daniel Veillard5d819032002-02-02 21:49:17 +00001517/************************************************************************
1518 * *
1519 * Error message callback *
1520 * *
1521 ************************************************************************/
1522
1523static PyObject *libxml_xmlPythonErrorFuncHandler = NULL;
1524static PyObject *libxml_xmlPythonErrorFuncCtxt = NULL;
1525
Daniel Veillarde6227e02003-01-14 11:42:39 +00001526/* helper to build a xmlMalloc'ed string from a format and va_list */
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001527/*
1528 * disabled the loop, the repeated call to vsnprintf without reset of ap
1529 * in case the initial buffer was too small segfaulted on x86_64
1530 * we now directly vsnprintf on a large buffer.
1531 */
Daniel Veillarde6227e02003-01-14 11:42:39 +00001532static char *
1533libxml_buildMessage(const char *msg, va_list ap)
Daniel Veillardd2379012002-03-15 22:24:56 +00001534{
Daniel Veillardd2379012002-03-15 22:24:56 +00001535 int chars;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001536 char *str;
1537
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001538 str = (char *) xmlMalloc(1000);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001539 if (str == NULL)
1540 return NULL;
1541
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001542 chars = vsnprintf(str, 999, msg, ap);
1543 if (chars >= 998)
1544 str[999] = 0;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001545
1546 return str;
1547}
1548
1549static void
1550libxml_xmlErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, const char *msg,
1551 ...)
1552{
Daniel Veillardd2379012002-03-15 22:24:56 +00001553 va_list ap;
Daniel Veillard5d819032002-02-02 21:49:17 +00001554 PyObject *list;
1555 PyObject *message;
1556 PyObject *result;
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001557 char str[1000];
Daniel Veillard5d819032002-02-02 21:49:17 +00001558
1559#ifdef DEBUG_ERROR
1560 printf("libxml_xmlErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
1561#endif
1562
1563
1564 if (libxml_xmlPythonErrorFuncHandler == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001565 va_start(ap, msg);
Daniel Veillard007d51e2003-09-17 20:07:28 +00001566 vfprintf(stderr, msg, ap);
Daniel Veillardd2379012002-03-15 22:24:56 +00001567 va_end(ap);
Daniel Veillard5d819032002-02-02 21:49:17 +00001568 } else {
Daniel Veillarde6227e02003-01-14 11:42:39 +00001569 va_start(ap, msg);
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001570 if (vsnprintf(str, 999, msg, ap) >= 998)
1571 str[999] = 0;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001572 va_end(ap);
Daniel Veillard5d819032002-02-02 21:49:17 +00001573
Daniel Veillardd2379012002-03-15 22:24:56 +00001574 list = PyTuple_New(2);
1575 PyTuple_SetItem(list, 0, libxml_xmlPythonErrorFuncCtxt);
1576 Py_XINCREF(libxml_xmlPythonErrorFuncCtxt);
Daniel Veillardad9fb7c2004-10-22 11:05:37 +00001577 message = libxml_charPtrConstWrap(str);
Daniel Veillardd2379012002-03-15 22:24:56 +00001578 PyTuple_SetItem(list, 1, message);
1579 result = PyEval_CallObject(libxml_xmlPythonErrorFuncHandler, list);
1580 Py_XDECREF(list);
1581 Py_XDECREF(result);
Daniel Veillard5d819032002-02-02 21:49:17 +00001582 }
1583}
1584
1585static void
Daniel Veillardd2379012002-03-15 22:24:56 +00001586libxml_xmlErrorInitialize(void)
1587{
Daniel Veillard5d819032002-02-02 21:49:17 +00001588#ifdef DEBUG_ERROR
1589 printf("libxml_xmlErrorInitialize() called\n");
1590#endif
1591 xmlSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
Daniel Veillard781ac8b2003-05-15 22:11:36 +00001592 xmlThrDefSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
Daniel Veillard5d819032002-02-02 21:49:17 +00001593}
1594
Daniel Veillardc2664642003-07-29 20:44:53 +00001595static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001596libxml_xmlRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self,
1597 PyObject * args)
1598{
Daniel Veillard5d819032002-02-02 21:49:17 +00001599 PyObject *py_retval;
1600 PyObject *pyobj_f;
1601 PyObject *pyobj_ctx;
1602
Daniel Veillardd2379012002-03-15 22:24:56 +00001603 if (!PyArg_ParseTuple
1604 (args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f,
1605 &pyobj_ctx))
1606 return (NULL);
Daniel Veillard5d819032002-02-02 21:49:17 +00001607
1608#ifdef DEBUG_ERROR
William M. Brack8e2cc6f2004-07-03 23:28:52 +00001609 printf("libxml_xmlRegisterErrorHandler(%p, %p) called\n", pyobj_ctx,
Daniel Veillardd2379012002-03-15 22:24:56 +00001610 pyobj_f);
Daniel Veillard5d819032002-02-02 21:49:17 +00001611#endif
1612
1613 if (libxml_xmlPythonErrorFuncHandler != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001614 Py_XDECREF(libxml_xmlPythonErrorFuncHandler);
Daniel Veillard5d819032002-02-02 21:49:17 +00001615 }
1616 if (libxml_xmlPythonErrorFuncCtxt != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001617 Py_XDECREF(libxml_xmlPythonErrorFuncCtxt);
Daniel Veillard5d819032002-02-02 21:49:17 +00001618 }
1619
1620 Py_XINCREF(pyobj_ctx);
1621 Py_XINCREF(pyobj_f);
1622
1623 /* TODO: check f is a function ! */
1624 libxml_xmlPythonErrorFuncHandler = pyobj_f;
1625 libxml_xmlPythonErrorFuncCtxt = pyobj_ctx;
1626
1627 py_retval = libxml_intWrap(1);
Daniel Veillardd2379012002-03-15 22:24:56 +00001628 return (py_retval);
Daniel Veillard5d819032002-02-02 21:49:17 +00001629}
Daniel Veillardd2379012002-03-15 22:24:56 +00001630
Daniel Veillarde6227e02003-01-14 11:42:39 +00001631
1632/************************************************************************
1633 * *
1634 * Per parserCtxt error handler *
1635 * *
1636 ************************************************************************/
1637
Daniel Veillard417be3a2003-01-20 21:26:34 +00001638typedef struct
Daniel Veillarde6227e02003-01-14 11:42:39 +00001639{
Daniel Veillard417be3a2003-01-20 21:26:34 +00001640 PyObject *f;
1641 PyObject *arg;
1642} xmlParserCtxtPyCtxt;
1643typedef xmlParserCtxtPyCtxt *xmlParserCtxtPyCtxtPtr;
1644
1645static void
1646libxml_xmlParserCtxtGenericErrorFuncHandler(void *ctx, int severity, char *str)
1647{
Daniel Veillarde6227e02003-01-14 11:42:39 +00001648 PyObject *list;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001649 PyObject *result;
Daniel Veillard417be3a2003-01-20 21:26:34 +00001650 xmlParserCtxtPtr ctxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001651 xmlParserCtxtPyCtxtPtr pyCtxt;
1652
1653#ifdef DEBUG_ERROR
Daniel Veillard850ce9b2004-11-10 11:55:47 +00001654 printf("libxml_xmlParserCtxtGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001655#endif
1656
Daniel Veillard417be3a2003-01-20 21:26:34 +00001657 ctxt = (xmlParserCtxtPtr)ctx;
1658 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001659
Daniel Veillard417be3a2003-01-20 21:26:34 +00001660 list = PyTuple_New(4);
1661 PyTuple_SetItem(list, 0, pyCtxt->arg);
1662 Py_XINCREF(pyCtxt->arg);
1663 PyTuple_SetItem(list, 1, libxml_charPtrWrap(str));
1664 PyTuple_SetItem(list, 2, libxml_intWrap(severity));
1665 PyTuple_SetItem(list, 3, Py_None);
1666 Py_INCREF(Py_None);
1667 result = PyEval_CallObject(pyCtxt->f, list);
1668 if (result == NULL)
1669 {
1670 /* TODO: manage for the exception to be propagated... */
1671 PyErr_Print();
Daniel Veillarde6227e02003-01-14 11:42:39 +00001672 }
Daniel Veillard417be3a2003-01-20 21:26:34 +00001673 Py_XDECREF(list);
1674 Py_XDECREF(result);
1675}
1676
1677static void
1678libxml_xmlParserCtxtErrorFuncHandler(void *ctx, const char *msg, ...)
1679{
1680 va_list ap;
1681
1682 va_start(ap, msg);
1683 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_ERROR,libxml_buildMessage(msg,ap));
1684 va_end(ap);
1685}
1686
1687static void
1688libxml_xmlParserCtxtWarningFuncHandler(void *ctx, const char *msg, ...)
1689{
1690 va_list ap;
1691
1692 va_start(ap, msg);
1693 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_WARNING,libxml_buildMessage(msg,ap));
1694 va_end(ap);
1695}
1696
1697static void
1698libxml_xmlParserCtxtValidityErrorFuncHandler(void *ctx, const char *msg, ...)
1699{
1700 va_list ap;
1701
1702 va_start(ap, msg);
1703 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap));
1704 va_end(ap);
1705}
1706
1707static void
1708libxml_xmlParserCtxtValidityWarningFuncHandler(void *ctx, const char *msg, ...)
1709{
1710 va_list ap;
1711
1712 va_start(ap, msg);
1713 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap));
1714 va_end(ap);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001715}
1716
Daniel Veillardc2664642003-07-29 20:44:53 +00001717static PyObject *
Daniel Veillard417be3a2003-01-20 21:26:34 +00001718libxml_xmlParserCtxtSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
Daniel Veillarde6227e02003-01-14 11:42:39 +00001719{
1720 PyObject *py_retval;
1721 xmlParserCtxtPtr ctxt;
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001722 xmlParserCtxtPyCtxtPtr pyCtxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001723 PyObject *pyobj_ctxt;
1724 PyObject *pyobj_f;
1725 PyObject *pyobj_arg;
1726
Daniel Veillard417be3a2003-01-20 21:26:34 +00001727 if (!PyArg_ParseTuple(args, (char *)"OOO:xmlParserCtxtSetErrorHandler",
Daniel Veillarde6227e02003-01-14 11:42:39 +00001728 &pyobj_ctxt, &pyobj_f, &pyobj_arg))
1729 return(NULL);
1730 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1731 if (ctxt->_private == NULL) {
Daniel Veillarde6227e02003-01-14 11:42:39 +00001732 pyCtxt = xmlMalloc(sizeof(xmlParserCtxtPyCtxt));
1733 if (pyCtxt == NULL) {
1734 py_retval = libxml_intWrap(-1);
1735 return(py_retval);
1736 }
1737 memset(pyCtxt,0,sizeof(xmlParserCtxtPyCtxt));
1738 ctxt->_private = pyCtxt;
1739 }
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001740 else {
Daniel Veillard417be3a2003-01-20 21:26:34 +00001741 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001742 }
Daniel Veillarde6227e02003-01-14 11:42:39 +00001743 /* TODO: check f is a function ! */
Daniel Veillard417be3a2003-01-20 21:26:34 +00001744 Py_XDECREF(pyCtxt->f);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001745 Py_XINCREF(pyobj_f);
Daniel Veillard417be3a2003-01-20 21:26:34 +00001746 pyCtxt->f = pyobj_f;
1747 Py_XDECREF(pyCtxt->arg);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001748 Py_XINCREF(pyobj_arg);
Daniel Veillard417be3a2003-01-20 21:26:34 +00001749 pyCtxt->arg = pyobj_arg;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001750
Daniel Veillard417be3a2003-01-20 21:26:34 +00001751 if (pyobj_f != Py_None) {
1752 ctxt->sax->error = libxml_xmlParserCtxtErrorFuncHandler;
1753 ctxt->sax->warning = libxml_xmlParserCtxtWarningFuncHandler;
1754 ctxt->vctxt.error = libxml_xmlParserCtxtValidityErrorFuncHandler;
1755 ctxt->vctxt.warning = libxml_xmlParserCtxtValidityWarningFuncHandler;
1756 }
1757 else {
1758 ctxt->sax->error = xmlParserError;
1759 ctxt->vctxt.error = xmlParserValidityError;
1760 ctxt->sax->warning = xmlParserWarning;
1761 ctxt->vctxt.warning = xmlParserValidityWarning;
1762 }
Daniel Veillarde6227e02003-01-14 11:42:39 +00001763
1764 py_retval = libxml_intWrap(1);
1765 return(py_retval);
1766}
1767
Daniel Veillardc2664642003-07-29 20:44:53 +00001768static PyObject *
Daniel Veillard417be3a2003-01-20 21:26:34 +00001769libxml_xmlParserCtxtGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
Daniel Veillarde6227e02003-01-14 11:42:39 +00001770{
1771 PyObject *py_retval;
1772 xmlParserCtxtPtr ctxt;
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001773 xmlParserCtxtPyCtxtPtr pyCtxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001774 PyObject *pyobj_ctxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001775
Daniel Veillard417be3a2003-01-20 21:26:34 +00001776 if (!PyArg_ParseTuple(args, (char *)"O:xmlParserCtxtGetErrorHandler",
1777 &pyobj_ctxt))
Daniel Veillarde6227e02003-01-14 11:42:39 +00001778 return(NULL);
1779 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
Daniel Veillard417be3a2003-01-20 21:26:34 +00001780 py_retval = PyTuple_New(2);
1781 if (ctxt->_private != NULL) {
1782 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
1783
1784 PyTuple_SetItem(py_retval, 0, pyCtxt->f);
1785 Py_XINCREF(pyCtxt->f);
1786 PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
1787 Py_XINCREF(pyCtxt->arg);
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001788 }
1789 else {
Daniel Veillard417be3a2003-01-20 21:26:34 +00001790 /* no python error handler registered */
1791 PyTuple_SetItem(py_retval, 0, Py_None);
1792 Py_XINCREF(Py_None);
1793 PyTuple_SetItem(py_retval, 1, Py_None);
1794 Py_XINCREF(Py_None);
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001795 }
Daniel Veillarde6227e02003-01-14 11:42:39 +00001796 return(py_retval);
1797}
1798
Daniel Veillardc2664642003-07-29 20:44:53 +00001799static PyObject *
Daniel Veillard417be3a2003-01-20 21:26:34 +00001800libxml_xmlFreeParserCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
1801 xmlParserCtxtPtr ctxt;
1802 PyObject *pyobj_ctxt;
1803 xmlParserCtxtPyCtxtPtr pyCtxt;
1804
1805 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeParserCtxt", &pyobj_ctxt))
1806 return(NULL);
1807 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1808
1809 if (ctxt != NULL) {
1810 pyCtxt = (xmlParserCtxtPyCtxtPtr)((xmlParserCtxtPtr)ctxt)->_private;
1811 if (pyCtxt) {
1812 Py_XDECREF(pyCtxt->f);
1813 Py_XDECREF(pyCtxt->arg);
1814 xmlFree(pyCtxt);
1815 }
1816 xmlFreeParserCtxt(ctxt);
1817 }
1818
1819 Py_INCREF(Py_None);
1820 return(Py_None);
1821}
1822
Daniel Veillard850ce9b2004-11-10 11:55:47 +00001823/***
1824 * xmlValidCtxt stuff
1825 */
1826
1827typedef struct
1828{
1829 PyObject *warn;
1830 PyObject *error;
1831 PyObject *arg;
1832} xmlValidCtxtPyCtxt;
1833typedef xmlValidCtxtPyCtxt *xmlValidCtxtPyCtxtPtr;
1834
1835static void
Stefan Kostc31e06c2011-05-09 12:52:28 +03001836libxml_xmlValidCtxtGenericErrorFuncHandler(void *ctx, ATTRIBUTE_UNUSED int severity, char *str)
Daniel Veillard850ce9b2004-11-10 11:55:47 +00001837{
1838 PyObject *list;
1839 PyObject *result;
1840 xmlValidCtxtPyCtxtPtr pyCtxt;
1841
1842#ifdef DEBUG_ERROR
1843 printf("libxml_xmlValidCtxtGenericErrorFuncHandler(%p, %d, %s, ...) called\n", ctx, severity, str);
1844#endif
1845
1846 pyCtxt = (xmlValidCtxtPyCtxtPtr)ctx;
1847
1848 list = PyTuple_New(2);
1849 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
1850 PyTuple_SetItem(list, 1, pyCtxt->arg);
1851 Py_XINCREF(pyCtxt->arg);
1852 result = PyEval_CallObject(pyCtxt->error, list);
1853 if (result == NULL)
1854 {
1855 /* TODO: manage for the exception to be propagated... */
1856 PyErr_Print();
1857 }
1858 Py_XDECREF(list);
1859 Py_XDECREF(result);
1860}
1861
1862static void
Stefan Kostc31e06c2011-05-09 12:52:28 +03001863libxml_xmlValidCtxtGenericWarningFuncHandler(void *ctx, ATTRIBUTE_UNUSED int severity, char *str)
Daniel Veillard850ce9b2004-11-10 11:55:47 +00001864{
1865 PyObject *list;
1866 PyObject *result;
1867 xmlValidCtxtPyCtxtPtr pyCtxt;
1868
1869#ifdef DEBUG_ERROR
1870 printf("libxml_xmlValidCtxtGenericWarningFuncHandler(%p, %d, %s, ...) called\n", ctx, severity, str);
1871#endif
1872
1873 pyCtxt = (xmlValidCtxtPyCtxtPtr)ctx;
1874
1875 list = PyTuple_New(2);
1876 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
1877 PyTuple_SetItem(list, 1, pyCtxt->arg);
1878 Py_XINCREF(pyCtxt->arg);
1879 result = PyEval_CallObject(pyCtxt->warn, list);
1880 if (result == NULL)
1881 {
1882 /* TODO: manage for the exception to be propagated... */
1883 PyErr_Print();
1884 }
1885 Py_XDECREF(list);
1886 Py_XDECREF(result);
1887}
1888
1889static void
1890libxml_xmlValidCtxtErrorFuncHandler(void *ctx, const char *msg, ...)
1891{
1892 va_list ap;
1893
1894 va_start(ap, msg);
1895 libxml_xmlValidCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap));
1896 va_end(ap);
1897}
1898
1899static void
1900libxml_xmlValidCtxtWarningFuncHandler(void *ctx, const char *msg, ...)
1901{
1902 va_list ap;
1903
1904 va_start(ap, msg);
1905 libxml_xmlValidCtxtGenericWarningFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap));
1906 va_end(ap);
1907}
1908
1909static PyObject *
1910libxml_xmlSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1911{
1912 PyObject *py_retval;
1913 PyObject *pyobj_error;
1914 PyObject *pyobj_warn;
1915 PyObject *pyobj_ctx;
1916 PyObject *pyobj_arg = Py_None;
1917 xmlValidCtxtPtr ctxt;
1918 xmlValidCtxtPyCtxtPtr pyCtxt;
1919
1920 if (!PyArg_ParseTuple
1921 (args, (char *) "OOO|O:xmlSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
1922 return (NULL);
1923
1924#ifdef DEBUG_ERROR
1925 printf("libxml_xmlSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
1926#endif
1927
1928 ctxt = PyValidCtxt_Get(pyobj_ctx);
1929 pyCtxt = xmlMalloc(sizeof(xmlValidCtxtPyCtxt));
1930 if (pyCtxt == NULL) {
1931 py_retval = libxml_intWrap(-1);
1932 return(py_retval);
1933 }
1934 memset(pyCtxt, 0, sizeof(xmlValidCtxtPyCtxt));
1935
1936
1937 /* TODO: check warn and error is a function ! */
1938 Py_XDECREF(pyCtxt->error);
1939 Py_XINCREF(pyobj_error);
1940 pyCtxt->error = pyobj_error;
1941
1942 Py_XDECREF(pyCtxt->warn);
1943 Py_XINCREF(pyobj_warn);
1944 pyCtxt->warn = pyobj_warn;
1945
1946 Py_XDECREF(pyCtxt->arg);
1947 Py_XINCREF(pyobj_arg);
1948 pyCtxt->arg = pyobj_arg;
1949
1950 ctxt->error = libxml_xmlValidCtxtErrorFuncHandler;
1951 ctxt->warning = libxml_xmlValidCtxtWarningFuncHandler;
1952 ctxt->userData = pyCtxt;
1953
1954 py_retval = libxml_intWrap(1);
1955 return (py_retval);
1956}
1957
Daniel Veillard25c90c52005-03-02 10:47:41 +00001958
Daniel Veillardbb8502c2005-03-30 07:40:35 +00001959static PyObject *
Daniel Veillard25c90c52005-03-02 10:47:41 +00001960libxml_xmlFreeValidCtxt(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1961 xmlValidCtxtPtr cur;
1962 xmlValidCtxtPyCtxtPtr pyCtxt;
1963 PyObject *pyobj_cur;
1964
1965 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeValidCtxt", &pyobj_cur))
1966 return(NULL);
1967 cur = (xmlValidCtxtPtr) PyValidCtxt_Get(pyobj_cur);
1968
1969 pyCtxt = (xmlValidCtxtPyCtxtPtr)(cur->userData);
1970 if (pyCtxt != NULL)
1971 {
1972 Py_XDECREF(pyCtxt->error);
1973 Py_XDECREF(pyCtxt->warn);
1974 Py_XDECREF(pyCtxt->arg);
1975 xmlFree(pyCtxt);
1976 }
1977
1978 xmlFreeValidCtxt(cur);
1979 Py_INCREF(Py_None);
1980 return(Py_None);
1981}
1982
Daniel Veillard438ebbd2008-05-12 12:58:46 +00001983#ifdef LIBXML_READER_ENABLED
Daniel Veillarda7340c82002-02-01 17:56:45 +00001984/************************************************************************
1985 * *
Daniel Veillard26f70262003-01-16 22:45:08 +00001986 * Per xmlTextReader error handler *
1987 * *
1988 ************************************************************************/
1989
1990typedef struct
1991{
1992 PyObject *f;
1993 PyObject *arg;
1994} xmlTextReaderPyCtxt;
1995typedef xmlTextReaderPyCtxt *xmlTextReaderPyCtxtPtr;
1996
1997static void
1998libxml_xmlTextReaderErrorCallback(void *arg,
1999 const char *msg,
Daniel Veillard417be3a2003-01-20 21:26:34 +00002000 int severity,
2001 xmlTextReaderLocatorPtr locator)
Daniel Veillard26f70262003-01-16 22:45:08 +00002002{
2003 xmlTextReaderPyCtxt *pyCtxt = (xmlTextReaderPyCtxt *)arg;
2004 PyObject *list;
2005 PyObject *result;
2006
Daniel Veillard417be3a2003-01-20 21:26:34 +00002007 list = PyTuple_New(4);
Daniel Veillard26f70262003-01-16 22:45:08 +00002008 PyTuple_SetItem(list, 0, pyCtxt->arg);
2009 Py_XINCREF(pyCtxt->arg);
2010 PyTuple_SetItem(list, 1, libxml_charPtrConstWrap(msg));
Daniel Veillard417be3a2003-01-20 21:26:34 +00002011 PyTuple_SetItem(list, 2, libxml_intWrap(severity));
2012 PyTuple_SetItem(list, 3, libxml_xmlTextReaderLocatorPtrWrap(locator));
Daniel Veillard26f70262003-01-16 22:45:08 +00002013 result = PyEval_CallObject(pyCtxt->f, list);
2014 if (result == NULL)
2015 {
Daniel Veillard417be3a2003-01-20 21:26:34 +00002016 /* TODO: manage for the exception to be propagated... */
Daniel Veillard26f70262003-01-16 22:45:08 +00002017 PyErr_Print();
2018 }
2019 Py_XDECREF(list);
2020 Py_XDECREF(result);
2021}
2022
Daniel Veillardc2664642003-07-29 20:44:53 +00002023static PyObject *
Daniel Veillard26f70262003-01-16 22:45:08 +00002024libxml_xmlTextReaderSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
2025{
2026 xmlTextReaderPtr reader;
2027 xmlTextReaderPyCtxtPtr pyCtxt;
2028 xmlTextReaderErrorFunc f;
2029 void *arg;
2030 PyObject *pyobj_reader;
2031 PyObject *pyobj_f;
2032 PyObject *pyobj_arg;
2033 PyObject *py_retval;
2034
2035 if (!PyArg_ParseTuple(args, (char *)"OOO:xmlTextReaderSetErrorHandler", &pyobj_reader, &pyobj_f, &pyobj_arg))
2036 return(NULL);
2037 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
2038 /* clear previous error handler */
2039 xmlTextReaderGetErrorHandler(reader,&f,&arg);
2040 if (arg != NULL) {
Daniel Veillardc2664642003-07-29 20:44:53 +00002041 if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) {
Daniel Veillard26f70262003-01-16 22:45:08 +00002042 /* ok, it's our error handler! */
2043 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
2044 Py_XDECREF(pyCtxt->f);
2045 Py_XDECREF(pyCtxt->arg);
2046 xmlFree(pyCtxt);
2047 }
2048 else {
2049 /*
2050 * there already an arg, and it's not ours,
2051 * there is definitely something wrong going on here...
2052 * we don't know how to free it, so we bail out...
2053 */
2054 py_retval = libxml_intWrap(-1);
2055 return(py_retval);
2056 }
2057 }
2058 xmlTextReaderSetErrorHandler(reader,NULL,NULL);
2059 /* set new error handler */
2060 if (pyobj_f != Py_None)
2061 {
2062 pyCtxt = (xmlTextReaderPyCtxtPtr)xmlMalloc(sizeof(xmlTextReaderPyCtxt));
2063 if (pyCtxt == NULL) {
2064 py_retval = libxml_intWrap(-1);
2065 return(py_retval);
2066 }
2067 Py_XINCREF(pyobj_f);
2068 pyCtxt->f = pyobj_f;
2069 Py_XINCREF(pyobj_arg);
2070 pyCtxt->arg = pyobj_arg;
Daniel Veillardc2664642003-07-29 20:44:53 +00002071 xmlTextReaderSetErrorHandler(reader,
2072 (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback,
2073 pyCtxt);
Daniel Veillard26f70262003-01-16 22:45:08 +00002074 }
2075
2076 py_retval = libxml_intWrap(1);
2077 return(py_retval);
2078}
2079
Daniel Veillardc2664642003-07-29 20:44:53 +00002080static PyObject *
Daniel Veillard26f70262003-01-16 22:45:08 +00002081libxml_xmlTextReaderGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
2082{
2083 xmlTextReaderPtr reader;
2084 xmlTextReaderPyCtxtPtr pyCtxt;
2085 xmlTextReaderErrorFunc f;
2086 void *arg;
2087 PyObject *pyobj_reader;
2088 PyObject *py_retval;
2089
2090 if (!PyArg_ParseTuple(args, (char *)"O:xmlTextReaderSetErrorHandler", &pyobj_reader))
2091 return(NULL);
2092 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
2093 xmlTextReaderGetErrorHandler(reader,&f,&arg);
2094 py_retval = PyTuple_New(2);
Daniel Veillardc2664642003-07-29 20:44:53 +00002095 if (f == (xmlTextReaderErrorFunc)libxml_xmlTextReaderErrorCallback) {
Daniel Veillard26f70262003-01-16 22:45:08 +00002096 /* ok, it's our error handler! */
2097 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
2098 PyTuple_SetItem(py_retval, 0, pyCtxt->f);
2099 Py_XINCREF(pyCtxt->f);
2100 PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
2101 Py_XINCREF(pyCtxt->arg);
2102 }
2103 else
2104 {
2105 /* f is null or it's not our error handler */
2106 PyTuple_SetItem(py_retval, 0, Py_None);
2107 Py_XINCREF(Py_None);
2108 PyTuple_SetItem(py_retval, 1, Py_None);
2109 Py_XINCREF(Py_None);
2110 }
2111 return(py_retval);
2112}
2113
Daniel Veillardc2664642003-07-29 20:44:53 +00002114static PyObject *
Daniel Veillard26f70262003-01-16 22:45:08 +00002115libxml_xmlFreeTextReader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
2116 xmlTextReaderPtr reader;
2117 PyObject *pyobj_reader;
2118 xmlTextReaderPyCtxtPtr pyCtxt;
2119 xmlTextReaderErrorFunc f;
2120 void *arg;
2121
Daniel Veillard157fee02003-10-31 10:36:03 +00002122 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeTextReader", &pyobj_reader))
2123 return(NULL);
2124 if (!PyCObject_Check(pyobj_reader)) {
Daniel Veillardbb3ba322003-10-30 13:12:43 +00002125 Py_INCREF(Py_None);
2126 return(Py_None);
2127 }
Daniel Veillard26f70262003-01-16 22:45:08 +00002128 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
Daniel Veillardbb3ba322003-10-30 13:12:43 +00002129 if (reader == NULL) {
2130 Py_INCREF(Py_None);
2131 return(Py_None);
2132 }
Daniel Veillard26f70262003-01-16 22:45:08 +00002133
2134 xmlTextReaderGetErrorHandler(reader,&f,&arg);
2135 if (arg != NULL) {
Daniel Veillardc2664642003-07-29 20:44:53 +00002136 if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) {
Daniel Veillard26f70262003-01-16 22:45:08 +00002137 /* ok, it's our error handler! */
2138 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
2139 Py_XDECREF(pyCtxt->f);
2140 Py_XDECREF(pyCtxt->arg);
2141 xmlFree(pyCtxt);
2142 }
2143 /*
2144 * else, something wrong happened, because the error handler is
2145 * not owned by the python bindings...
2146 */
2147 }
2148
2149 xmlFreeTextReader(reader);
2150 Py_INCREF(Py_None);
2151 return(Py_None);
2152}
Daniel Veillard438ebbd2008-05-12 12:58:46 +00002153#endif
Daniel Veillard26f70262003-01-16 22:45:08 +00002154
2155/************************************************************************
2156 * *
Daniel Veillarda7340c82002-02-01 17:56:45 +00002157 * XPath extensions *
2158 * *
2159 ************************************************************************/
2160
Daniel Veillarda7340c82002-02-01 17:56:45 +00002161static void
Daniel Veillardd2379012002-03-15 22:24:56 +00002162libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs)
2163{
Daniel Veillarda7340c82002-02-01 17:56:45 +00002164 PyObject *list, *cur, *result;
2165 xmlXPathObjectPtr obj;
Daniel Veillard70cab352002-02-06 16:06:58 +00002166 xmlXPathContextPtr rctxt;
2167 PyObject *current_function = NULL;
2168 const xmlChar *name;
2169 const xmlChar *ns_uri;
Daniel Veillarda7340c82002-02-01 17:56:45 +00002170 int i;
2171
Daniel Veillard70cab352002-02-06 16:06:58 +00002172 if (ctxt == NULL)
Daniel Veillardd2379012002-03-15 22:24:56 +00002173 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00002174 rctxt = ctxt->context;
2175 if (rctxt == NULL)
Daniel Veillardd2379012002-03-15 22:24:56 +00002176 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00002177 name = rctxt->function;
2178 ns_uri = rctxt->functionURI;
Daniel Veillarda7340c82002-02-01 17:56:45 +00002179#ifdef DEBUG_XPATH
Daniel Veillardd2379012002-03-15 22:24:56 +00002180 printf("libxml_xmlXPathFuncCallback called name %s URI %s\n", name,
2181 ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002182#endif
2183
Daniel Veillard70cab352002-02-06 16:06:58 +00002184 /*
2185 * Find the function, it should be there it was there at lookup
2186 */
Daniel Veillardd2379012002-03-15 22:24:56 +00002187 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
2188 if ( /* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002189 (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
2190 (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
2191 current_function = (*libxml_xpathCallbacks)[i].function;
Daniel Veillardd2379012002-03-15 22:24:56 +00002192 }
Daniel Veillard70cab352002-02-06 16:06:58 +00002193 }
2194 if (current_function == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002195 printf
2196 ("libxml_xmlXPathFuncCallback: internal error %s not found !\n",
2197 name);
2198 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00002199 }
2200
Daniel Veillardc575b992002-02-08 13:28:40 +00002201 list = PyTuple_New(nargs + 1);
2202 PyTuple_SetItem(list, 0, libxml_xmlXPathParserContextPtrWrap(ctxt));
Daniel Veillard05349ab2004-01-25 20:01:35 +00002203 for (i = nargs - 1; i >= 0; i--) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002204 obj = valuePop(ctxt);
2205 cur = libxml_xmlXPathObjectPtrWrap(obj);
2206 PyTuple_SetItem(list, i + 1, cur);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002207 }
2208 result = PyEval_CallObject(current_function, list);
2209 Py_DECREF(list);
2210
2211 obj = libxml_xmlXPathObjectPtrConvert(result);
2212 valuePush(ctxt, obj);
2213}
2214
2215static xmlXPathFunction
Daniel Veillardd2379012002-03-15 22:24:56 +00002216libxml_xmlXPathFuncLookupFunc(void *ctxt, const xmlChar * name,
2217 const xmlChar * ns_uri)
2218{
Daniel Veillarda7340c82002-02-01 17:56:45 +00002219 int i;
Daniel Veillardd2379012002-03-15 22:24:56 +00002220
Daniel Veillarda7340c82002-02-01 17:56:45 +00002221#ifdef DEBUG_XPATH
2222 printf("libxml_xmlXPathFuncLookupFunc(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00002223 ctxt, name, ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002224#endif
Daniel Veillard70cab352002-02-06 16:06:58 +00002225 /*
2226 * This is called once only. The address is then stored in the
2227 * XPath expression evaluation, the proper object to call can
2228 * then still be found using the execution context function
2229 * and functionURI fields.
2230 */
Daniel Veillardd2379012002-03-15 22:24:56 +00002231 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002232 if ((ctxt == (*libxml_xpathCallbacks)[i].ctx) &&
2233 (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
2234 (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002235 return (libxml_xmlXPathFuncCallback);
2236 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00002237 }
Daniel Veillardd2379012002-03-15 22:24:56 +00002238 return (NULL);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002239}
2240
2241static void
Daniel Veillardd2379012002-03-15 22:24:56 +00002242libxml_xpathCallbacksInitialize(void)
2243{
Daniel Veillarda7340c82002-02-01 17:56:45 +00002244 int i;
2245
2246 if (libxml_xpathCallbacksInitialized != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00002247 return;
Daniel Veillarda7340c82002-02-01 17:56:45 +00002248
2249#ifdef DEBUG_XPATH
2250 printf("libxml_xpathCallbacksInitialized called\n");
2251#endif
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002252 libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlMalloc(
2253 libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
Daniel Veillarda7340c82002-02-01 17:56:45 +00002254
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002255 for (i = 0; i < libxml_xpathCallbacksAllocd; i++) {
2256 (*libxml_xpathCallbacks)[i].ctx = NULL;
2257 (*libxml_xpathCallbacks)[i].name = NULL;
2258 (*libxml_xpathCallbacks)[i].ns_uri = NULL;
2259 (*libxml_xpathCallbacks)[i].function = NULL;
Daniel Veillarda7340c82002-02-01 17:56:45 +00002260 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00002261 libxml_xpathCallbacksInitialized = 1;
2262}
2263
2264PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002265libxml_xmlRegisterXPathFunction(ATTRIBUTE_UNUSED PyObject * self,
2266 PyObject * args)
2267{
Daniel Veillarda7340c82002-02-01 17:56:45 +00002268 PyObject *py_retval;
2269 int c_retval = 0;
2270 xmlChar *name;
2271 xmlChar *ns_uri;
2272 xmlXPathContextPtr ctx;
2273 PyObject *pyobj_ctx;
2274 PyObject *pyobj_f;
2275 int i;
2276
Daniel Veillardd2379012002-03-15 22:24:56 +00002277 if (!PyArg_ParseTuple
2278 (args, (char *) "OszO:registerXPathFunction", &pyobj_ctx, &name,
2279 &ns_uri, &pyobj_f))
2280 return (NULL);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002281
2282 ctx = (xmlXPathContextPtr) PyxmlXPathContext_Get(pyobj_ctx);
2283 if (libxml_xpathCallbacksInitialized == 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00002284 libxml_xpathCallbacksInitialize();
Daniel Veillarda7340c82002-02-01 17:56:45 +00002285 xmlXPathRegisterFuncLookup(ctx, libxml_xmlXPathFuncLookupFunc, ctx);
2286
2287 if ((pyobj_ctx == NULL) || (name == NULL) || (pyobj_f == NULL)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002288 py_retval = libxml_intWrap(-1);
2289 return (py_retval);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002290 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00002291#ifdef DEBUG_XPATH
2292 printf("libxml_registerXPathFunction(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00002293 ctx, name, ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002294#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002295 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002296 if ((ctx == (*libxml_xpathCallbacks)[i].ctx) &&
2297 (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
2298 (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002299 Py_XINCREF(pyobj_f);
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002300 Py_XDECREF((*libxml_xpathCallbacks)[i].function);
2301 (*libxml_xpathCallbacks)[i].function = pyobj_f;
Daniel Veillardd2379012002-03-15 22:24:56 +00002302 c_retval = 1;
2303 goto done;
2304 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00002305 }
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002306 if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksAllocd) {
2307 libxml_xpathCallbacksAllocd+=10;
2308 libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlRealloc(
2309 libxml_xpathCallbacks,
2310 libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
2311 }
2312 i = libxml_xpathCallbacksNb++;
2313 Py_XINCREF(pyobj_f);
2314 (*libxml_xpathCallbacks)[i].ctx = ctx;
2315 (*libxml_xpathCallbacks)[i].name = xmlStrdup(name);
2316 (*libxml_xpathCallbacks)[i].ns_uri = xmlStrdup(ns_uri);
2317 (*libxml_xpathCallbacks)[i].function = pyobj_f;
Daniel Veillardd2379012002-03-15 22:24:56 +00002318 c_retval = 1;
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002319
Daniel Veillardd2379012002-03-15 22:24:56 +00002320 done:
Daniel Veillarda7340c82002-02-01 17:56:45 +00002321 py_retval = libxml_intWrap((int) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00002322 return (py_retval);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002323}
2324
Daniel Veillard1971ee22002-01-31 20:29:19 +00002325/************************************************************************
2326 * *
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002327 * Global properties access *
2328 * *
2329 ************************************************************************/
2330static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002331libxml_name(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002332{
2333 PyObject *resultobj, *obj;
2334 xmlNodePtr cur;
2335 const xmlChar *res;
2336
Daniel Veillardd2379012002-03-15 22:24:56 +00002337 if (!PyArg_ParseTuple(args, (char *) "O:name", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002338 return NULL;
2339 cur = PyxmlNode_Get(obj);
2340
2341#ifdef DEBUG
2342 printf("libxml_name: cur = %p type %d\n", cur, cur->type);
2343#endif
2344
Daniel Veillardd2379012002-03-15 22:24:56 +00002345 switch (cur->type) {
2346 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002347#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002348 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002349#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002350 case XML_HTML_DOCUMENT_NODE:{
2351 xmlDocPtr doc = (xmlDocPtr) cur;
2352
2353 res = doc->URL;
2354 break;
2355 }
2356 case XML_ATTRIBUTE_NODE:{
2357 xmlAttrPtr attr = (xmlAttrPtr) cur;
2358
2359 res = attr->name;
2360 break;
2361 }
2362 case XML_NAMESPACE_DECL:{
2363 xmlNsPtr ns = (xmlNsPtr) cur;
2364
2365 res = ns->prefix;
2366 break;
2367 }
2368 default:
2369 res = cur->name;
2370 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002371 }
Daniel Veillard1971ee22002-01-31 20:29:19 +00002372 resultobj = libxml_constxmlCharPtrWrap(res);
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002373
2374 return resultobj;
2375}
2376
2377static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002378libxml_doc(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002379{
2380 PyObject *resultobj, *obj;
2381 xmlNodePtr cur;
2382 xmlDocPtr res;
2383
Daniel Veillardd2379012002-03-15 22:24:56 +00002384 if (!PyArg_ParseTuple(args, (char *) "O:doc", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002385 return NULL;
2386 cur = PyxmlNode_Get(obj);
2387
2388#ifdef DEBUG
2389 printf("libxml_doc: cur = %p\n", cur);
2390#endif
2391
Daniel Veillardd2379012002-03-15 22:24:56 +00002392 switch (cur->type) {
2393 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002394#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002395 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002396#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002397 case XML_HTML_DOCUMENT_NODE:
2398 res = NULL;
2399 break;
2400 case XML_ATTRIBUTE_NODE:{
2401 xmlAttrPtr attr = (xmlAttrPtr) cur;
2402
2403 res = attr->doc;
2404 break;
2405 }
2406 case XML_NAMESPACE_DECL:
2407 res = NULL;
2408 break;
2409 default:
2410 res = cur->doc;
2411 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002412 }
2413 resultobj = libxml_xmlDocPtrWrap(res);
2414 return resultobj;
2415}
2416
2417static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002418libxml_properties(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002419{
2420 PyObject *resultobj, *obj;
Daniel Veillard2728f842006-03-09 16:49:24 +00002421 xmlNodePtr cur;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002422 xmlAttrPtr res;
2423
Daniel Veillardd2379012002-03-15 22:24:56 +00002424 if (!PyArg_ParseTuple(args, (char *) "O:properties", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002425 return NULL;
2426 cur = PyxmlNode_Get(obj);
Daniel Veillard2728f842006-03-09 16:49:24 +00002427 if ((cur != NULL) && (cur->type == XML_ELEMENT_NODE))
Daniel Veillardd2379012002-03-15 22:24:56 +00002428 res = cur->properties;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002429 else
Daniel Veillardd2379012002-03-15 22:24:56 +00002430 res = NULL;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002431 resultobj = libxml_xmlAttrPtrWrap(res);
2432 return resultobj;
2433}
2434
2435static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002436libxml_next(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002437{
2438 PyObject *resultobj, *obj;
2439 xmlNodePtr cur;
2440 xmlNodePtr res;
2441
Daniel Veillardd2379012002-03-15 22:24:56 +00002442 if (!PyArg_ParseTuple(args, (char *) "O:next", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002443 return NULL;
2444 cur = PyxmlNode_Get(obj);
2445
2446#ifdef DEBUG
2447 printf("libxml_next: cur = %p\n", cur);
2448#endif
2449
Daniel Veillardd2379012002-03-15 22:24:56 +00002450 switch (cur->type) {
2451 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002452#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002453 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002454#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002455 case XML_HTML_DOCUMENT_NODE:
2456 res = NULL;
2457 break;
2458 case XML_ATTRIBUTE_NODE:{
2459 xmlAttrPtr attr = (xmlAttrPtr) cur;
2460
2461 res = (xmlNodePtr) attr->next;
2462 break;
2463 }
2464 case XML_NAMESPACE_DECL:{
2465 xmlNsPtr ns = (xmlNsPtr) cur;
2466
2467 res = (xmlNodePtr) ns->next;
2468 break;
2469 }
2470 default:
2471 res = cur->next;
2472 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002473
2474 }
2475 resultobj = libxml_xmlNodePtrWrap(res);
2476 return resultobj;
2477}
2478
2479static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002480libxml_prev(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002481{
2482 PyObject *resultobj, *obj;
2483 xmlNodePtr cur;
2484 xmlNodePtr res;
2485
Daniel Veillardd2379012002-03-15 22:24:56 +00002486 if (!PyArg_ParseTuple(args, (char *) "O:prev", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002487 return NULL;
2488 cur = PyxmlNode_Get(obj);
2489
2490#ifdef DEBUG
2491 printf("libxml_prev: cur = %p\n", cur);
2492#endif
2493
Daniel Veillardd2379012002-03-15 22:24:56 +00002494 switch (cur->type) {
2495 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002496#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002497 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002498#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002499 case XML_HTML_DOCUMENT_NODE:
2500 res = NULL;
2501 break;
2502 case XML_ATTRIBUTE_NODE:{
2503 xmlAttrPtr attr = (xmlAttrPtr) cur;
2504
Daniel Veillardfaa35ff2002-11-24 13:53:43 +00002505 res = (xmlNodePtr) attr->prev;
Daniel Veillardd2379012002-03-15 22:24:56 +00002506 }
Daniel Veillard594e5df2009-09-07 14:58:47 +02002507 break;
Daniel Veillardd2379012002-03-15 22:24:56 +00002508 case XML_NAMESPACE_DECL:
2509 res = NULL;
2510 break;
2511 default:
Daniel Veillardfaa35ff2002-11-24 13:53:43 +00002512 res = cur->prev;
Daniel Veillardd2379012002-03-15 22:24:56 +00002513 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002514 }
2515 resultobj = libxml_xmlNodePtrWrap(res);
2516 return resultobj;
2517}
2518
2519static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002520libxml_children(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002521{
2522 PyObject *resultobj, *obj;
2523 xmlNodePtr cur;
2524 xmlNodePtr res;
2525
Daniel Veillardd2379012002-03-15 22:24:56 +00002526 if (!PyArg_ParseTuple(args, (char *) "O:children", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002527 return NULL;
2528 cur = PyxmlNode_Get(obj);
2529
2530#ifdef DEBUG
2531 printf("libxml_children: cur = %p\n", cur);
2532#endif
2533
Daniel Veillardd2379012002-03-15 22:24:56 +00002534 switch (cur->type) {
2535 case XML_ELEMENT_NODE:
2536 case XML_ENTITY_REF_NODE:
2537 case XML_ENTITY_NODE:
2538 case XML_PI_NODE:
2539 case XML_COMMENT_NODE:
2540 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002541#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002542 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002543#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002544 case XML_HTML_DOCUMENT_NODE:
2545 case XML_DTD_NODE:
2546 res = cur->children;
2547 break;
2548 case XML_ATTRIBUTE_NODE:{
2549 xmlAttrPtr attr = (xmlAttrPtr) cur;
2550
2551 res = attr->children;
2552 break;
2553 }
2554 default:
2555 res = NULL;
2556 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002557 }
2558 resultobj = libxml_xmlNodePtrWrap(res);
2559 return resultobj;
2560}
2561
2562static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002563libxml_last(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002564{
2565 PyObject *resultobj, *obj;
2566 xmlNodePtr cur;
2567 xmlNodePtr res;
2568
Daniel Veillardd2379012002-03-15 22:24:56 +00002569 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002570 return NULL;
2571 cur = PyxmlNode_Get(obj);
2572
2573#ifdef DEBUG
2574 printf("libxml_last: cur = %p\n", cur);
2575#endif
2576
Daniel Veillardd2379012002-03-15 22:24:56 +00002577 switch (cur->type) {
2578 case XML_ELEMENT_NODE:
2579 case XML_ENTITY_REF_NODE:
2580 case XML_ENTITY_NODE:
2581 case XML_PI_NODE:
2582 case XML_COMMENT_NODE:
2583 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002584#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002585 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002586#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002587 case XML_HTML_DOCUMENT_NODE:
2588 case XML_DTD_NODE:
2589 res = cur->last;
2590 break;
2591 case XML_ATTRIBUTE_NODE:{
2592 xmlAttrPtr attr = (xmlAttrPtr) cur;
2593
2594 res = attr->last;
2595 }
2596 default:
2597 res = NULL;
2598 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002599 }
2600 resultobj = libxml_xmlNodePtrWrap(res);
2601 return resultobj;
2602}
2603
2604static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002605libxml_parent(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002606{
2607 PyObject *resultobj, *obj;
2608 xmlNodePtr cur;
2609 xmlNodePtr res;
2610
Daniel Veillardd2379012002-03-15 22:24:56 +00002611 if (!PyArg_ParseTuple(args, (char *) "O:parent", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002612 return NULL;
2613 cur = PyxmlNode_Get(obj);
2614
2615#ifdef DEBUG
2616 printf("libxml_parent: cur = %p\n", cur);
2617#endif
2618
Daniel Veillardd2379012002-03-15 22:24:56 +00002619 switch (cur->type) {
2620 case XML_DOCUMENT_NODE:
2621 case XML_HTML_DOCUMENT_NODE:
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:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002624#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002625 res = NULL;
2626 break;
2627 case XML_ATTRIBUTE_NODE:{
2628 xmlAttrPtr attr = (xmlAttrPtr) cur;
2629
2630 res = attr->parent;
2631 }
Daniel Veillard22eda2b2005-08-01 05:20:16 +00002632 break;
Daniel Veillardd2379012002-03-15 22:24:56 +00002633 case XML_ENTITY_DECL:
2634 case XML_NAMESPACE_DECL:
2635 case XML_XINCLUDE_START:
2636 case XML_XINCLUDE_END:
2637 res = NULL;
2638 break;
2639 default:
2640 res = cur->parent;
2641 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002642 }
2643 resultobj = libxml_xmlNodePtrWrap(res);
2644 return resultobj;
2645}
2646
2647static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002648libxml_type(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002649{
2650 PyObject *resultobj, *obj;
2651 xmlNodePtr cur;
Daniel Veillardd2379012002-03-15 22:24:56 +00002652 const xmlChar *res = NULL;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002653
Daniel Veillardd2379012002-03-15 22:24:56 +00002654 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002655 return NULL;
2656 cur = PyxmlNode_Get(obj);
2657
2658#ifdef DEBUG
2659 printf("libxml_type: cur = %p\n", cur);
2660#endif
2661
Daniel Veillardd2379012002-03-15 22:24:56 +00002662 switch (cur->type) {
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002663 case XML_ELEMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002664 res = (const xmlChar *) "element";
2665 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002666 case XML_ATTRIBUTE_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002667 res = (const xmlChar *) "attribute";
2668 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002669 case XML_TEXT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002670 res = (const xmlChar *) "text";
2671 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002672 case XML_CDATA_SECTION_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002673 res = (const xmlChar *) "cdata";
2674 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002675 case XML_ENTITY_REF_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002676 res = (const xmlChar *) "entity_ref";
2677 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002678 case XML_ENTITY_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002679 res = (const xmlChar *) "entity";
2680 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002681 case XML_PI_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002682 res = (const xmlChar *) "pi";
2683 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002684 case XML_COMMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002685 res = (const xmlChar *) "comment";
2686 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002687 case XML_DOCUMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002688 res = (const xmlChar *) "document_xml";
2689 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002690 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002691 res = (const xmlChar *) "doctype";
2692 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002693 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002694 res = (const xmlChar *) "fragment";
2695 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002696 case XML_NOTATION_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002697 res = (const xmlChar *) "notation";
2698 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002699 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002700 res = (const xmlChar *) "document_html";
2701 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002702 case XML_DTD_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002703 res = (const xmlChar *) "dtd";
2704 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002705 case XML_ELEMENT_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002706 res = (const xmlChar *) "elem_decl";
2707 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002708 case XML_ATTRIBUTE_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002709 res = (const xmlChar *) "attribute_decl";
2710 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002711 case XML_ENTITY_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002712 res = (const xmlChar *) "entity_decl";
2713 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002714 case XML_NAMESPACE_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002715 res = (const xmlChar *) "namespace";
2716 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002717 case XML_XINCLUDE_START:
Daniel Veillardd2379012002-03-15 22:24:56 +00002718 res = (const xmlChar *) "xinclude_start";
2719 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002720 case XML_XINCLUDE_END:
Daniel Veillardd2379012002-03-15 22:24:56 +00002721 res = (const xmlChar *) "xinclude_end";
2722 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002723#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002724 case XML_DOCB_DOCUMENT_NODE:
2725 res = (const xmlChar *) "document_docbook";
2726 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002727#endif
2728 }
2729#ifdef DEBUG
2730 printf("libxml_type: cur = %p: %s\n", cur, res);
2731#endif
2732
Daniel Veillard1971ee22002-01-31 20:29:19 +00002733 resultobj = libxml_constxmlCharPtrWrap(res);
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002734 return resultobj;
2735}
2736
2737/************************************************************************
2738 * *
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002739 * Specific accessor functions *
2740 * *
2741 ************************************************************************/
2742PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002743libxml_xmlNodeGetNsDefs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2744{
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002745 PyObject *py_retval;
2746 xmlNsPtr c_retval;
2747 xmlNodePtr node;
2748 PyObject *pyobj_node;
2749
Daniel Veillardd2379012002-03-15 22:24:56 +00002750 if (!PyArg_ParseTuple
2751 (args, (char *) "O:xmlNodeGetNsDefs", &pyobj_node))
2752 return (NULL);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002753 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2754
2755 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002756 Py_INCREF(Py_None);
2757 return (Py_None);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002758 }
2759 c_retval = node->nsDef;
2760 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00002761 return (py_retval);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002762}
2763
2764PyObject *
Daniel Veillardf9cf6f52005-04-12 01:02:29 +00002765libxml_xmlNodeRemoveNsDef(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2766{
2767 PyObject *py_retval;
2768 xmlNsPtr ns, prev;
2769 xmlNodePtr node;
2770 PyObject *pyobj_node;
2771 xmlChar *href;
2772 xmlNsPtr c_retval;
2773
2774 if (!PyArg_ParseTuple
2775 (args, (char *) "Oz:xmlNodeRemoveNsDef", &pyobj_node, &href))
2776 return (NULL);
2777 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2778 ns = NULL;
2779
2780 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
2781 Py_INCREF(Py_None);
2782 return (Py_None);
2783 }
2784
2785 if (href == NULL) {
2786 ns = node->nsDef;
2787 node->nsDef = NULL;
2788 c_retval = 0;
2789 }
2790 else {
2791 prev = NULL;
2792 ns = node->nsDef;
2793 while (ns != NULL) {
2794 if (xmlStrEqual(ns->href, href)) {
2795 if (prev != NULL)
2796 prev->next = ns->next;
2797 else
2798 node->nsDef = ns->next;
2799 ns->next = NULL;
2800 c_retval = 0;
2801 break;
2802 }
2803 prev = ns;
2804 ns = ns->next;
2805 }
2806 }
2807
2808 c_retval = ns;
2809 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
2810 return (py_retval);
2811}
2812
2813PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002814libxml_xmlNodeGetNs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2815{
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002816 PyObject *py_retval;
2817 xmlNsPtr c_retval;
2818 xmlNodePtr node;
2819 PyObject *pyobj_node;
2820
Daniel Veillardd2379012002-03-15 22:24:56 +00002821 if (!PyArg_ParseTuple(args, (char *) "O:xmlNodeGetNs", &pyobj_node))
2822 return (NULL);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002823 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2824
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002825 if ((node == NULL) ||
2826 ((node->type != XML_ELEMENT_NODE) &&
2827 (node->type != XML_ATTRIBUTE_NODE))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002828 Py_INCREF(Py_None);
2829 return (Py_None);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002830 }
2831 c_retval = node->ns;
2832 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00002833 return (py_retval);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002834}
2835
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002836#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002837/************************************************************************
2838 * *
Daniel Veillard1e774382002-03-06 17:35:40 +00002839 * Serialization front-end *
2840 * *
2841 ************************************************************************/
2842
Daniel Veillardd2379012002-03-15 22:24:56 +00002843static PyObject *
2844libxml_serializeNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2845{
Daniel Veillard1e774382002-03-06 17:35:40 +00002846 PyObject *py_retval = NULL;
2847 xmlChar *c_retval;
2848 PyObject *pyobj_node;
2849 xmlNodePtr node;
2850 xmlDocPtr doc;
Daniel Veillardd2379012002-03-15 22:24:56 +00002851 const char *encoding;
Daniel Veillard1e774382002-03-06 17:35:40 +00002852 int format;
Daniel Veillardda3fee42008-09-01 13:08:57 +00002853 xmlSaveCtxtPtr ctxt;
2854 xmlBufferPtr buf;
2855 int options = 0;
Daniel Veillard1e774382002-03-06 17:35:40 +00002856
Daniel Veillardd2379012002-03-15 22:24:56 +00002857 if (!PyArg_ParseTuple(args, (char *) "Ozi:serializeNode", &pyobj_node,
2858 &encoding, &format))
2859 return (NULL);
Daniel Veillard1e774382002-03-06 17:35:40 +00002860 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2861
2862 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002863 Py_INCREF(Py_None);
2864 return (Py_None);
Daniel Veillard1e774382002-03-06 17:35:40 +00002865 }
2866 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002867 doc = (xmlDocPtr) node;
Daniel Veillardda3fee42008-09-01 13:08:57 +00002868 node = NULL;
Daniel Veillard656ce942004-04-30 23:11:45 +00002869#ifdef LIBXML_HTML_ENABLED
Daniel Veillard1e774382002-03-06 17:35:40 +00002870 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002871 doc = (xmlDocPtr) node;
Daniel Veillardda3fee42008-09-01 13:08:57 +00002872 node = NULL;
2873#endif
Daniel Veillard1e774382002-03-06 17:35:40 +00002874 } else {
William M. Brack95af5942004-02-08 04:12:49 +00002875 if (node->type == XML_NAMESPACE_DECL)
2876 doc = NULL;
2877 else
2878 doc = node->doc;
Daniel Veillarda8c0adb2002-11-17 22:37:35 +00002879 if ((doc == NULL) || (doc->type == XML_DOCUMENT_NODE)) {
Daniel Veillard656ce942004-04-30 23:11:45 +00002880#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002881 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillard656ce942004-04-30 23:11:45 +00002882#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2379012002-03-15 22:24:56 +00002883 } else {
2884 Py_INCREF(Py_None);
2885 return (Py_None);
2886 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002887 }
Daniel Veillardda3fee42008-09-01 13:08:57 +00002888
2889
2890 buf = xmlBufferCreate();
2891 if (buf == NULL) {
2892 Py_INCREF(Py_None);
2893 return (Py_None);
2894 }
2895 if (format) options |= XML_SAVE_FORMAT;
2896 ctxt = xmlSaveToBuffer(buf, encoding, options);
2897 if (ctxt == NULL) {
2898 xmlBufferFree(buf);
2899 Py_INCREF(Py_None);
2900 return (Py_None);
2901 }
2902 if (node == NULL)
2903 xmlSaveDoc(ctxt, doc);
2904 else
2905 xmlSaveTree(ctxt, node);
2906 xmlSaveClose(ctxt);
2907
2908 c_retval = buf->content;
2909 buf->content = NULL;
2910
2911 xmlBufferFree(buf);
2912 py_retval = libxml_charPtrWrap((char *) c_retval);
2913
Daniel Veillardd2379012002-03-15 22:24:56 +00002914 return (py_retval);
Daniel Veillard1e774382002-03-06 17:35:40 +00002915}
2916
Daniel Veillardd2379012002-03-15 22:24:56 +00002917static PyObject *
2918libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2919{
Daniel Veillard1e774382002-03-06 17:35:40 +00002920 PyObject *py_file = NULL;
2921 FILE *output;
2922 PyObject *pyobj_node;
2923 xmlNodePtr node;
2924 xmlDocPtr doc;
Daniel Veillardd2379012002-03-15 22:24:56 +00002925 const char *encoding;
Daniel Veillard1e774382002-03-06 17:35:40 +00002926 int format;
2927 int len;
2928 xmlOutputBufferPtr buf;
2929 xmlCharEncodingHandlerPtr handler = NULL;
2930
Daniel Veillardd2379012002-03-15 22:24:56 +00002931 if (!PyArg_ParseTuple(args, (char *) "OOzi:serializeNode", &pyobj_node,
2932 &py_file, &encoding, &format))
2933 return (NULL);
Daniel Veillard1e774382002-03-06 17:35:40 +00002934 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2935
2936 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002937 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002938 }
2939 if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002940 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002941 }
2942 output = PyFile_AsFile(py_file);
2943 if (output == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002944 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002945 }
2946
2947 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002948 doc = (xmlDocPtr) node;
Daniel Veillard1e774382002-03-06 17:35:40 +00002949 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002950 doc = (xmlDocPtr) node;
Daniel Veillard1e774382002-03-06 17:35:40 +00002951 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00002952 doc = node->doc;
Daniel Veillard1e774382002-03-06 17:35:40 +00002953 }
Daniel Veillard656ce942004-04-30 23:11:45 +00002954#ifdef LIBXML_HTML_ENABLED
Daniel Veillard1e774382002-03-06 17:35:40 +00002955 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002956 if (encoding == NULL)
2957 encoding = (const char *) htmlGetMetaEncoding(doc);
Daniel Veillard1e774382002-03-06 17:35:40 +00002958 }
Daniel Veillard656ce942004-04-30 23:11:45 +00002959#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard1e774382002-03-06 17:35:40 +00002960 if (encoding != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002961 handler = xmlFindCharEncodingHandler(encoding);
2962 if (handler == NULL) {
2963 return (PyInt_FromLong((long) -1));
2964 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002965 }
2966 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002967 if (handler == NULL)
2968 handler = xmlFindCharEncodingHandler("HTML");
2969 if (handler == NULL)
2970 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00002971 }
2972
2973 buf = xmlOutputBufferCreateFile(output, handler);
2974 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002975 len = xmlSaveFormatFileTo(buf, doc, encoding, format);
Daniel Veillard656ce942004-04-30 23:11:45 +00002976#ifdef LIBXML_HTML_ENABLED
Daniel Veillard1e774382002-03-06 17:35:40 +00002977 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002978 htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
2979 len = xmlOutputBufferClose(buf);
Daniel Veillard1e774382002-03-06 17:35:40 +00002980 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002981 htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
2982 len = xmlOutputBufferClose(buf);
Daniel Veillard656ce942004-04-30 23:11:45 +00002983#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard1e774382002-03-06 17:35:40 +00002984 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00002985 xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
2986 len = xmlOutputBufferClose(buf);
Daniel Veillard1e774382002-03-06 17:35:40 +00002987 }
Daniel Veillardd2379012002-03-15 22:24:56 +00002988 return (PyInt_FromLong((long) len));
Daniel Veillard1e774382002-03-06 17:35:40 +00002989}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002990#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard1e774382002-03-06 17:35:40 +00002991
2992/************************************************************************
2993 * *
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002994 * Extra stuff *
2995 * *
2996 ************************************************************************/
2997PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002998libxml_xmlNewNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2999{
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00003000 PyObject *py_retval;
Daniel Veillardd2379012002-03-15 22:24:56 +00003001 xmlChar *name;
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00003002 xmlNodePtr node;
3003
Daniel Veillardd2379012002-03-15 22:24:56 +00003004 if (!PyArg_ParseTuple(args, (char *) "s:xmlNewNode", &name))
3005 return (NULL);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00003006 node = (xmlNodePtr) xmlNewNode(NULL, name);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00003007#ifdef DEBUG
Daniel Veillardd2379012002-03-15 22:24:56 +00003008 printf("NewNode: %s : %p\n", name, (void *) node);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00003009#endif
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00003010
3011 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00003012 Py_INCREF(Py_None);
3013 return (Py_None);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00003014 }
3015 py_retval = libxml_xmlNodePtrWrap(node);
Daniel Veillardd2379012002-03-15 22:24:56 +00003016 return (py_retval);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00003017}
3018
Daniel Veillard54396242003-04-23 07:36:50 +00003019
3020/************************************************************************
3021 * *
3022 * Local Catalog stuff *
3023 * *
3024 ************************************************************************/
3025static PyObject *
3026libxml_addLocalCatalog(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3027{
3028 xmlChar *URL;
3029 xmlParserCtxtPtr ctxt;
3030 PyObject *pyobj_ctxt;
3031
3032 if (!PyArg_ParseTuple(args, (char *)"Os:addLocalCatalog", &pyobj_ctxt, &URL))
3033 return(NULL);
3034
3035 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
3036
3037 if (URL != NULL) {
3038 ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL);
3039 }
3040
3041#ifdef DEBUG
3042 printf("LocalCatalog: %s\n", URL);
3043#endif
3044
3045 Py_INCREF(Py_None);
3046 return (Py_None);
3047}
3048
Daniel Veillardc2664642003-07-29 20:44:53 +00003049#ifdef LIBXML_SCHEMAS_ENABLED
3050
3051/************************************************************************
3052 * *
3053 * RelaxNG error handler registration *
3054 * *
3055 ************************************************************************/
3056
3057typedef struct
3058{
3059 PyObject *warn;
3060 PyObject *error;
3061 PyObject *arg;
3062} xmlRelaxNGValidCtxtPyCtxt;
3063typedef xmlRelaxNGValidCtxtPyCtxt *xmlRelaxNGValidCtxtPyCtxtPtr;
3064
3065static void
3066libxml_xmlRelaxNGValidityGenericErrorFuncHandler(void *ctx, char *str)
3067{
3068 PyObject *list;
3069 PyObject *result;
3070 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3071
3072#ifdef DEBUG_ERROR
3073 printf("libxml_xmlRelaxNGValidityGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
3074#endif
3075
3076 pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx;
3077
3078 list = PyTuple_New(2);
3079 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3080 PyTuple_SetItem(list, 1, pyCtxt->arg);
3081 Py_XINCREF(pyCtxt->arg);
3082 result = PyEval_CallObject(pyCtxt->error, list);
3083 if (result == NULL)
3084 {
3085 /* TODO: manage for the exception to be propagated... */
3086 PyErr_Print();
3087 }
3088 Py_XDECREF(list);
3089 Py_XDECREF(result);
3090}
3091
3092static void
3093libxml_xmlRelaxNGValidityGenericWarningFuncHandler(void *ctx, char *str)
3094{
3095 PyObject *list;
3096 PyObject *result;
3097 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3098
3099#ifdef DEBUG_ERROR
3100 printf("libxml_xmlRelaxNGValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str);
3101#endif
3102
3103 pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx;
3104
3105 list = PyTuple_New(2);
3106 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3107 PyTuple_SetItem(list, 1, pyCtxt->arg);
3108 Py_XINCREF(pyCtxt->arg);
3109 result = PyEval_CallObject(pyCtxt->warn, list);
3110 if (result == NULL)
3111 {
3112 /* TODO: manage for the exception to be propagated... */
3113 PyErr_Print();
3114 }
3115 Py_XDECREF(list);
3116 Py_XDECREF(result);
3117}
3118
3119static void
3120libxml_xmlRelaxNGValidityErrorFunc(void *ctx, const char *msg, ...)
3121{
3122 va_list ap;
3123
3124 va_start(ap, msg);
3125 libxml_xmlRelaxNGValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap));
3126 va_end(ap);
3127}
3128
3129static void
3130libxml_xmlRelaxNGValidityWarningFunc(void *ctx, const char *msg, ...)
3131{
3132 va_list ap;
3133
3134 va_start(ap, msg);
3135 libxml_xmlRelaxNGValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap));
3136 va_end(ap);
3137}
3138
3139static PyObject *
3140libxml_xmlRelaxNGSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3141{
3142 PyObject *py_retval;
3143 PyObject *pyobj_error;
3144 PyObject *pyobj_warn;
3145 PyObject *pyobj_ctx;
3146 PyObject *pyobj_arg = Py_None;
3147 xmlRelaxNGValidCtxtPtr ctxt;
3148 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3149
3150 if (!PyArg_ParseTuple
3151 (args, (char *) "OOO|O:xmlRelaxNGSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
3152 return (NULL);
3153
3154#ifdef DEBUG_ERROR
3155 printf("libxml_xmlRelaxNGSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
3156#endif
3157
3158 ctxt = PyrelaxNgValidCtxt_Get(pyobj_ctx);
William M. Brackc1939562003-08-05 15:52:22 +00003159 if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1)
Daniel Veillardc2664642003-07-29 20:44:53 +00003160 {
3161 py_retval = libxml_intWrap(-1);
3162 return(py_retval);
3163 }
3164
3165 if (pyCtxt == NULL)
3166 {
3167 /* first time to set the error handlers */
3168 pyCtxt = xmlMalloc(sizeof(xmlRelaxNGValidCtxtPyCtxt));
3169 if (pyCtxt == NULL) {
3170 py_retval = libxml_intWrap(-1);
3171 return(py_retval);
3172 }
3173 memset(pyCtxt, 0, sizeof(xmlRelaxNGValidCtxtPyCtxt));
3174 }
3175
3176 /* TODO: check warn and error is a function ! */
3177 Py_XDECREF(pyCtxt->error);
3178 Py_XINCREF(pyobj_error);
3179 pyCtxt->error = pyobj_error;
3180
3181 Py_XDECREF(pyCtxt->warn);
3182 Py_XINCREF(pyobj_warn);
3183 pyCtxt->warn = pyobj_warn;
3184
3185 Py_XDECREF(pyCtxt->arg);
3186 Py_XINCREF(pyobj_arg);
3187 pyCtxt->arg = pyobj_arg;
3188
3189 xmlRelaxNGSetValidErrors(ctxt, &libxml_xmlRelaxNGValidityErrorFunc, &libxml_xmlRelaxNGValidityWarningFunc, pyCtxt);
3190
3191 py_retval = libxml_intWrap(1);
3192 return (py_retval);
3193}
3194
3195static PyObject *
3196libxml_xmlRelaxNGFreeValidCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
3197 xmlRelaxNGValidCtxtPtr ctxt;
3198 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
3199 PyObject *pyobj_ctxt;
3200
3201 if (!PyArg_ParseTuple(args, (char *)"O:xmlRelaxNGFreeValidCtxt", &pyobj_ctxt))
3202 return(NULL);
3203 ctxt = (xmlRelaxNGValidCtxtPtr) PyrelaxNgValidCtxt_Get(pyobj_ctxt);
3204
William M. Brackc1939562003-08-05 15:52:22 +00003205 if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0)
Daniel Veillardc2664642003-07-29 20:44:53 +00003206 {
3207 if (pyCtxt != NULL)
3208 {
3209 Py_XDECREF(pyCtxt->error);
3210 Py_XDECREF(pyCtxt->warn);
3211 Py_XDECREF(pyCtxt->arg);
3212 xmlFree(pyCtxt);
3213 }
3214 }
3215
3216 xmlRelaxNGFreeValidCtxt(ctxt);
3217 Py_INCREF(Py_None);
3218 return(Py_None);
3219}
3220
Daniel Veillard259f0df2004-08-18 09:13:18 +00003221typedef struct
3222{
3223 PyObject *warn;
3224 PyObject *error;
3225 PyObject *arg;
3226} xmlSchemaValidCtxtPyCtxt;
3227typedef xmlSchemaValidCtxtPyCtxt *xmlSchemaValidCtxtPyCtxtPtr;
3228
3229static void
3230libxml_xmlSchemaValidityGenericErrorFuncHandler(void *ctx, char *str)
3231{
3232 PyObject *list;
3233 PyObject *result;
3234 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3235
3236#ifdef DEBUG_ERROR
3237 printf("libxml_xmlSchemaValiditiyGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
3238#endif
3239
3240 pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx;
3241
3242 list = PyTuple_New(2);
3243 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3244 PyTuple_SetItem(list, 1, pyCtxt->arg);
3245 Py_XINCREF(pyCtxt->arg);
3246 result = PyEval_CallObject(pyCtxt->error, list);
3247 if (result == NULL)
3248 {
3249 /* TODO: manage for the exception to be propagated... */
3250 PyErr_Print();
3251 }
3252 Py_XDECREF(list);
3253 Py_XDECREF(result);
3254}
3255
3256static void
3257libxml_xmlSchemaValidityGenericWarningFuncHandler(void *ctx, char *str)
3258{
3259 PyObject *list;
3260 PyObject *result;
3261 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3262
3263#ifdef DEBUG_ERROR
3264 printf("libxml_xmlSchemaValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str);
3265#endif
3266
3267 pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx;
3268
3269 list = PyTuple_New(2);
3270 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3271 PyTuple_SetItem(list, 1, pyCtxt->arg);
3272 Py_XINCREF(pyCtxt->arg);
3273 result = PyEval_CallObject(pyCtxt->warn, list);
3274 if (result == NULL)
3275 {
3276 /* TODO: manage for the exception to be propagated... */
3277 PyErr_Print();
3278 }
3279 Py_XDECREF(list);
3280 Py_XDECREF(result);
3281}
3282
3283static void
3284libxml_xmlSchemaValidityErrorFunc(void *ctx, const char *msg, ...)
3285{
3286 va_list ap;
3287
3288 va_start(ap, msg);
3289 libxml_xmlSchemaValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap));
3290 va_end(ap);
3291}
3292
3293static void
3294libxml_xmlSchemaValidityWarningFunc(void *ctx, const char *msg, ...)
3295{
3296 va_list ap;
3297
3298 va_start(ap, msg);
3299 libxml_xmlSchemaValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap));
3300 va_end(ap);
3301}
3302
Daniel Veillard6ebf3c42004-08-22 13:11:39 +00003303PyObject *
Daniel Veillard259f0df2004-08-18 09:13:18 +00003304libxml_xmlSchemaSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3305{
3306 PyObject *py_retval;
3307 PyObject *pyobj_error;
3308 PyObject *pyobj_warn;
3309 PyObject *pyobj_ctx;
3310 PyObject *pyobj_arg = Py_None;
3311 xmlSchemaValidCtxtPtr ctxt;
3312 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3313
3314 if (!PyArg_ParseTuple
3315 (args, (char *) "OOO|O:xmlSchemaSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
3316 return (NULL);
3317
3318#ifdef DEBUG_ERROR
3319 printf("libxml_xmlSchemaSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
3320#endif
3321
3322 ctxt = PySchemaValidCtxt_Get(pyobj_ctx);
3323 if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1)
3324 {
3325 py_retval = libxml_intWrap(-1);
3326 return(py_retval);
3327 }
3328
3329 if (pyCtxt == NULL)
3330 {
3331 /* first time to set the error handlers */
3332 pyCtxt = xmlMalloc(sizeof(xmlSchemaValidCtxtPyCtxt));
3333 if (pyCtxt == NULL) {
3334 py_retval = libxml_intWrap(-1);
3335 return(py_retval);
3336 }
3337 memset(pyCtxt, 0, sizeof(xmlSchemaValidCtxtPyCtxt));
3338 }
3339
3340 /* TODO: check warn and error is a function ! */
3341 Py_XDECREF(pyCtxt->error);
3342 Py_XINCREF(pyobj_error);
3343 pyCtxt->error = pyobj_error;
3344
3345 Py_XDECREF(pyCtxt->warn);
3346 Py_XINCREF(pyobj_warn);
3347 pyCtxt->warn = pyobj_warn;
3348
3349 Py_XDECREF(pyCtxt->arg);
3350 Py_XINCREF(pyobj_arg);
3351 pyCtxt->arg = pyobj_arg;
3352
3353 xmlSchemaSetValidErrors(ctxt, &libxml_xmlSchemaValidityErrorFunc, &libxml_xmlSchemaValidityWarningFunc, pyCtxt);
3354
3355 py_retval = libxml_intWrap(1);
3356 return(py_retval);
3357}
3358
Daniel Veillardbb8502c2005-03-30 07:40:35 +00003359static PyObject *
Daniel Veillard259f0df2004-08-18 09:13:18 +00003360libxml_xmlSchemaFreeValidCtxt(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3361{
3362 xmlSchemaValidCtxtPtr ctxt;
3363 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3364 PyObject *pyobj_ctxt;
3365
3366 if (!PyArg_ParseTuple(args, (char *)"O:xmlSchemaFreeValidCtxt", &pyobj_ctxt))
3367 return(NULL);
3368 ctxt = (xmlSchemaValidCtxtPtr) PySchemaValidCtxt_Get(pyobj_ctxt);
3369
3370 if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0)
3371 {
3372 if (pyCtxt != NULL)
3373 {
3374 Py_XDECREF(pyCtxt->error);
3375 Py_XDECREF(pyCtxt->warn);
3376 Py_XDECREF(pyCtxt->arg);
3377 xmlFree(pyCtxt);
3378 }
3379 }
3380
3381 xmlSchemaFreeValidCtxt(ctxt);
3382 Py_INCREF(Py_None);
3383 return(Py_None);
3384}
3385
Daniel Veillardc2664642003-07-29 20:44:53 +00003386#endif
3387
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003388#ifdef LIBXML_C14N_ENABLED
3389#ifdef LIBXML_OUTPUT_ENABLED
3390
3391/************************************************************************
3392 * *
3393 * XML Canonicalization c14n *
3394 * *
3395 ************************************************************************/
3396
3397static int
3398PyxmlNodeSet_Convert(PyObject *py_nodeset, xmlNodeSetPtr *result)
3399{
3400 xmlNodeSetPtr nodeSet;
3401 int is_tuple = 0;
3402
3403 if (PyTuple_Check(py_nodeset))
3404 is_tuple = 1;
3405 else if (PyList_Check(py_nodeset))
3406 is_tuple = 0;
3407 else if (py_nodeset == Py_None) {
3408 *result = NULL;
3409 return 0;
3410 }
3411 else {
3412 PyErr_SetString(PyExc_TypeError,
3413 "must be a tuple or list of nodes.");
3414 return -1;
3415 }
3416
3417 nodeSet = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet));
3418 if (nodeSet == NULL) {
3419 PyErr_SetString(PyExc_MemoryError, "");
3420 return -1;
3421 }
3422
3423 nodeSet->nodeNr = 0;
3424 nodeSet->nodeMax = (is_tuple
3425 ? PyTuple_GET_SIZE(py_nodeset)
3426 : PyList_GET_SIZE(py_nodeset));
3427 nodeSet->nodeTab
3428 = (xmlNodePtr *) xmlMalloc (nodeSet->nodeMax
3429 * sizeof(xmlNodePtr));
3430 if (nodeSet->nodeTab == NULL) {
3431 xmlFree(nodeSet);
3432 PyErr_SetString(PyExc_MemoryError, "");
3433 return -1;
3434 }
3435 memset(nodeSet->nodeTab, 0 ,
3436 nodeSet->nodeMax * sizeof(xmlNodePtr));
3437
3438 {
3439 int idx;
3440 for (idx=0; idx < nodeSet->nodeMax; ++idx) {
3441 xmlNodePtr pynode =
3442 PyxmlNode_Get (is_tuple
3443 ? PyTuple_GET_ITEM(py_nodeset, idx)
3444 : PyList_GET_ITEM(py_nodeset, idx));
3445 if (pynode)
3446 nodeSet->nodeTab[nodeSet->nodeNr++] = pynode;
3447 }
3448 }
3449 *result = nodeSet;
3450 return 0;
3451}
3452
3453static int
3454PystringSet_Convert(PyObject *py_strings, xmlChar *** result)
3455{
3456 /* NOTE: the array should be freed, but the strings are shared
3457 with the python strings and so must not be freed. */
3458
3459 xmlChar ** strings;
3460 int is_tuple = 0;
3461 int count;
3462 int init_index = 0;
3463
3464 if (PyTuple_Check(py_strings))
3465 is_tuple = 1;
3466 else if (PyList_Check(py_strings))
3467 is_tuple = 0;
3468 else if (py_strings == Py_None) {
3469 *result = NULL;
3470 return 0;
3471 }
3472 else {
3473 PyErr_SetString(PyExc_TypeError,
3474 "must be a tuple or list of strings.");
3475 return -1;
3476 }
3477
3478 count = (is_tuple
3479 ? PyTuple_GET_SIZE(py_strings)
3480 : PyList_GET_SIZE(py_strings));
3481
3482 strings = (xmlChar **) xmlMalloc(sizeof(xmlChar *) * count);
3483
3484 if (strings == NULL) {
3485 PyErr_SetString(PyExc_MemoryError, "");
3486 return -1;
3487 }
3488
3489 memset(strings, 0 , sizeof(xmlChar *) * count);
3490
3491 {
3492 int idx;
3493 for (idx=0; idx < count; ++idx) {
3494 char* s = PyString_AsString
3495 (is_tuple
3496 ? PyTuple_GET_ITEM(py_strings, idx)
3497 : PyList_GET_ITEM(py_strings, idx));
3498 if (s)
3499 strings[init_index++] = (xmlChar *)s;
3500 else {
3501 xmlFree(strings);
3502 PyErr_SetString(PyExc_TypeError,
3503 "must be a tuple or list of strings.");
3504 return -1;
3505 }
3506 }
3507 }
3508
3509 *result = strings;
3510 return 0;
3511}
3512
3513static PyObject *
3514libxml_C14NDocDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
3515 PyObject * args)
3516{
3517 PyObject *py_retval = NULL;
3518
3519 PyObject *pyobj_doc;
3520 PyObject *pyobj_nodes;
3521 int exclusive;
3522 PyObject *pyobj_prefixes;
3523 int with_comments;
3524
3525 xmlDocPtr doc;
3526 xmlNodeSetPtr nodes;
3527 xmlChar **prefixes = NULL;
3528 xmlChar *doc_txt;
3529
3530 int result;
3531
3532 if (!PyArg_ParseTuple(args, (char *) "OOiOi:C14NDocDumpMemory",
3533 &pyobj_doc,
3534 &pyobj_nodes,
3535 &exclusive,
3536 &pyobj_prefixes,
3537 &with_comments))
3538 return (NULL);
3539
3540 doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
3541 if (!doc) {
3542 PyErr_SetString(PyExc_TypeError, "bad document.");
3543 return NULL;
3544 }
3545
3546 result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes);
3547 if (result < 0) return NULL;
3548
3549 if (exclusive) {
3550 result = PystringSet_Convert(pyobj_prefixes, &prefixes);
3551 if (result < 0) {
3552 if (nodes) {
3553 xmlFree(nodes->nodeTab);
3554 xmlFree(nodes);
3555 }
3556 return NULL;
3557 }
3558 }
3559
3560 result = xmlC14NDocDumpMemory(doc,
3561 nodes,
3562 exclusive,
3563 prefixes,
3564 with_comments,
3565 &doc_txt);
3566
3567 if (nodes) {
3568 xmlFree(nodes->nodeTab);
3569 xmlFree(nodes);
3570 }
3571 if (prefixes) {
3572 xmlChar ** idx = prefixes;
3573 while (*idx) xmlFree(*(idx++));
3574 xmlFree(prefixes);
3575 }
3576
3577 if (result < 0) {
3578 PyErr_SetString(PyExc_Exception,
3579 "libxml2 xmlC14NDocDumpMemory failure.");
3580 return NULL;
3581 }
3582 else {
3583 py_retval = PyString_FromStringAndSize((const char *) doc_txt,
3584 result);
3585 xmlFree(doc_txt);
3586 return py_retval;
3587 }
3588}
3589
3590static PyObject *
3591libxml_C14NDocSaveTo(ATTRIBUTE_UNUSED PyObject * self,
3592 PyObject * args)
3593{
3594 PyObject *pyobj_doc;
3595 PyObject *py_file;
3596 PyObject *pyobj_nodes;
3597 int exclusive;
3598 PyObject *pyobj_prefixes;
3599 int with_comments;
3600
3601 xmlDocPtr doc;
3602 xmlNodeSetPtr nodes;
3603 xmlChar **prefixes = NULL;
3604 FILE * output;
3605 xmlOutputBufferPtr buf;
3606
3607 int result;
3608 int len;
3609
3610 if (!PyArg_ParseTuple(args, (char *) "OOiOiO:C14NDocSaveTo",
3611 &pyobj_doc,
3612 &pyobj_nodes,
3613 &exclusive,
3614 &pyobj_prefixes,
3615 &with_comments,
3616 &py_file))
3617 return (NULL);
3618
3619 doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
3620 if (!doc) {
3621 PyErr_SetString(PyExc_TypeError, "bad document.");
3622 return NULL;
3623 }
3624
3625 if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
3626 PyErr_SetString(PyExc_TypeError, "bad file.");
3627 return NULL;
3628 }
3629 output = PyFile_AsFile(py_file);
3630 if (output == NULL) {
3631 PyErr_SetString(PyExc_TypeError, "bad file.");
3632 return NULL;
3633 }
3634 buf = xmlOutputBufferCreateFile(output, NULL);
3635
3636 result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes);
3637 if (result < 0) return NULL;
3638
3639 if (exclusive) {
3640 result = PystringSet_Convert(pyobj_prefixes, &prefixes);
3641 if (result < 0) {
3642 if (nodes) {
3643 xmlFree(nodes->nodeTab);
3644 xmlFree(nodes);
3645 }
3646 return NULL;
3647 }
3648 }
3649
3650 result = xmlC14NDocSaveTo(doc,
3651 nodes,
3652 exclusive,
3653 prefixes,
3654 with_comments,
3655 buf);
3656
3657 if (nodes) {
3658 xmlFree(nodes->nodeTab);
3659 xmlFree(nodes);
3660 }
3661 if (prefixes) {
3662 xmlChar ** idx = prefixes;
3663 while (*idx) xmlFree(*(idx++));
3664 xmlFree(prefixes);
3665 }
3666
3667 len = xmlOutputBufferClose(buf);
3668
3669 if (result < 0) {
3670 PyErr_SetString(PyExc_Exception,
3671 "libxml2 xmlC14NDocSaveTo failure.");
3672 return NULL;
3673 }
3674 else
3675 return PyInt_FromLong((long) len);
3676}
3677
3678#endif
3679#endif
3680
William M. Brackc68d78d2004-07-16 10:39:30 +00003681static PyObject *
3682libxml_getObjDesc(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003683
William M. Brackc68d78d2004-07-16 10:39:30 +00003684 PyObject *obj;
3685 char *str;
3686
3687 if (!PyArg_ParseTuple(args, (char *)"O:getObjDesc", &obj))
3688 return NULL;
3689 str = PyCObject_GetDesc(obj);
3690 return Py_BuildValue((char *)"s", str);
3691}
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003692
William M. Brack40cca612006-06-26 18:25:40 +00003693static PyObject *
3694libxml_compareNodesEqual(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
3695
3696 PyObject *py_node1, *py_node2;
3697 xmlNodePtr node1, node2;
3698
3699 if (!PyArg_ParseTuple(args, (char *)"OO:compareNodesEqual",
3700 &py_node1, &py_node2))
3701 return NULL;
3702 /* To compare two node objects, we compare their pointer addresses */
3703 node1 = PyxmlNode_Get(py_node1);
3704 node2 = PyxmlNode_Get(py_node2);
3705 if ( node1 == node2 )
3706 return Py_BuildValue((char *)"i", 1);
3707 else
3708 return Py_BuildValue((char *)"i", 0);
3709
3710}
3711
3712static PyObject *
3713libxml_nodeHash(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
3714
3715 PyObject *py_node1;
3716 xmlNodePtr node1;
3717
3718 if (!PyArg_ParseTuple(args, (char *)"O:nodeHash", &py_node1))
3719 return NULL;
3720 /* For simplicity, we use the node pointer address as a hash value */
3721 node1 = PyxmlNode_Get(py_node1);
3722
3723 return PyLong_FromVoidPtr(node1);
3724
3725}
3726
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00003727/************************************************************************
3728 * *
Daniel Veillardd2897fd2002-01-30 16:37:32 +00003729 * The registration stuff *
3730 * *
3731 ************************************************************************/
3732static PyMethodDef libxmlMethods[] = {
Daniel Veillard96fe0952002-01-30 20:52:23 +00003733#include "libxml2-export.c"
Daniel Veillardd2379012002-03-15 22:24:56 +00003734 {(char *) "name", libxml_name, METH_VARARGS, NULL},
3735 {(char *) "children", libxml_children, METH_VARARGS, NULL},
3736 {(char *) "properties", libxml_properties, METH_VARARGS, NULL},
3737 {(char *) "last", libxml_last, METH_VARARGS, NULL},
3738 {(char *) "prev", libxml_prev, METH_VARARGS, NULL},
3739 {(char *) "next", libxml_next, METH_VARARGS, NULL},
3740 {(char *) "parent", libxml_parent, METH_VARARGS, NULL},
3741 {(char *) "type", libxml_type, METH_VARARGS, NULL},
3742 {(char *) "doc", libxml_doc, METH_VARARGS, NULL},
3743 {(char *) "xmlNewNode", libxml_xmlNewNode, METH_VARARGS, NULL},
Daniel Veillardf9cf6f52005-04-12 01:02:29 +00003744 {(char *) "xmlNodeRemoveNsDef", libxml_xmlNodeRemoveNsDef, METH_VARARGS, NULL},
Daniel Veillard850ce9b2004-11-10 11:55:47 +00003745 {(char *)"xmlSetValidErrors", libxml_xmlSetValidErrors, METH_VARARGS, NULL},
Daniel Veillard25c90c52005-03-02 10:47:41 +00003746 {(char *)"xmlFreeValidCtxt", libxml_xmlFreeValidCtxt, METH_VARARGS, NULL},
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003747#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00003748 {(char *) "serializeNode", libxml_serializeNode, METH_VARARGS, NULL},
3749 {(char *) "saveNodeTo", libxml_saveNodeTo, METH_VARARGS, NULL},
Daniel Veillardc6d4a932002-09-12 15:00:57 +00003750 {(char *) "outputBufferCreate", libxml_xmlCreateOutputBuffer, METH_VARARGS, NULL},
Daniel Veillard6cbd6c02003-12-04 12:31:49 +00003751 {(char *) "outputBufferGetPythonFile", libxml_outputBufferGetPythonFile, METH_VARARGS, NULL},
3752 {(char *) "xmlOutputBufferClose", libxml_xmlOutputBufferClose, METH_VARARGS, NULL},
3753 { (char *)"xmlOutputBufferFlush", libxml_xmlOutputBufferFlush, METH_VARARGS, NULL },
Daniel Veillard263ec862004-10-04 10:26:54 +00003754 { (char *)"xmlSaveFileTo", libxml_xmlSaveFileTo, METH_VARARGS, NULL },
3755 { (char *)"xmlSaveFormatFileTo", libxml_xmlSaveFormatFileTo, METH_VARARGS, NULL },
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003756#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardc6d4a932002-09-12 15:00:57 +00003757 {(char *) "inputBufferCreate", libxml_xmlCreateInputBuffer, METH_VARARGS, NULL},
3758 {(char *) "setEntityLoader", libxml_xmlSetEntityLoader, METH_VARARGS, NULL},
Daniel Veillard3e20a292003-01-10 13:14:40 +00003759 {(char *)"xmlRegisterErrorHandler", libxml_xmlRegisterErrorHandler, METH_VARARGS, NULL },
Daniel Veillard417be3a2003-01-20 21:26:34 +00003760 {(char *)"xmlParserCtxtSetErrorHandler", libxml_xmlParserCtxtSetErrorHandler, METH_VARARGS, NULL },
3761 {(char *)"xmlParserCtxtGetErrorHandler", libxml_xmlParserCtxtGetErrorHandler, METH_VARARGS, NULL },
Daniel Veillarde6227e02003-01-14 11:42:39 +00003762 {(char *)"xmlFreeParserCtxt", libxml_xmlFreeParserCtxt, METH_VARARGS, NULL },
Daniel Veillard438ebbd2008-05-12 12:58:46 +00003763#ifdef LIBXML_READER_ENABLED
Daniel Veillard26f70262003-01-16 22:45:08 +00003764 {(char *)"xmlTextReaderSetErrorHandler", libxml_xmlTextReaderSetErrorHandler, METH_VARARGS, NULL },
3765 {(char *)"xmlTextReaderGetErrorHandler", libxml_xmlTextReaderGetErrorHandler, METH_VARARGS, NULL },
3766 {(char *)"xmlFreeTextReader", libxml_xmlFreeTextReader, METH_VARARGS, NULL },
Daniel Veillard438ebbd2008-05-12 12:58:46 +00003767#endif
Daniel Veillard54396242003-04-23 07:36:50 +00003768 {(char *)"addLocalCatalog", libxml_addLocalCatalog, METH_VARARGS, NULL },
Daniel Veillardc2664642003-07-29 20:44:53 +00003769#ifdef LIBXML_SCHEMAS_ENABLED
3770 {(char *)"xmlRelaxNGSetValidErrors", libxml_xmlRelaxNGSetValidErrors, METH_VARARGS, NULL},
3771 {(char *)"xmlRelaxNGFreeValidCtxt", libxml_xmlRelaxNGFreeValidCtxt, METH_VARARGS, NULL},
Daniel Veillardeff45a92004-10-29 12:10:55 +00003772 {(char *)"xmlSchemaSetValidErrors", libxml_xmlSchemaSetValidErrors, METH_VARARGS, NULL},
Daniel Veillardbb8502c2005-03-30 07:40:35 +00003773 {(char *)"xmlSchemaFreeValidCtxt", libxml_xmlSchemaFreeValidCtxt, METH_VARARGS, NULL},
Daniel Veillardc2664642003-07-29 20:44:53 +00003774#endif
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003775#ifdef LIBXML_C14N_ENABLED
3776#ifdef LIBXML_OUTPUT_ENABLED
3777 {(char *)"xmlC14NDocDumpMemory", libxml_C14NDocDumpMemory, METH_VARARGS, NULL},
3778 {(char *)"xmlC14NDocSaveTo", libxml_C14NDocSaveTo, METH_VARARGS, NULL},
3779#endif
3780#endif
William M. Brackc68d78d2004-07-16 10:39:30 +00003781 {(char *) "getObjDesc", libxml_getObjDesc, METH_VARARGS, NULL},
William M. Brack40cca612006-06-26 18:25:40 +00003782 {(char *) "compareNodesEqual", libxml_compareNodesEqual, METH_VARARGS, NULL},
3783 {(char *) "nodeHash", libxml_nodeHash, METH_VARARGS, NULL},
Alexey Neyman48da90b2013-02-25 15:54:25 +08003784 {(char *) "xmlRegisterInputCallback", libxml_xmlRegisterInputCallback, METH_VARARGS, NULL},
3785 {(char *) "xmlUnregisterInputCallback", libxml_xmlUnregisterInputCallback, METH_VARARGS, NULL},
Daniel Veillardd2379012002-03-15 22:24:56 +00003786 {NULL, NULL, 0, NULL}
Daniel Veillardd2897fd2002-01-30 16:37:32 +00003787};
3788
Daniel Veillard0fea6f42002-02-22 22:51:13 +00003789#ifdef MERGED_MODULES
Daniel Veillard6361da02002-02-23 10:10:33 +00003790extern void initlibxsltmod(void);
Daniel Veillard0fea6f42002-02-22 22:51:13 +00003791#endif
3792
Daniel Veillardd2379012002-03-15 22:24:56 +00003793void
3794initlibxml2mod(void)
3795{
Daniel Veillardaf43f632002-03-08 15:05:20 +00003796 static int initialized = 0;
Daniel Veillardaf43f632002-03-08 15:05:20 +00003797
3798 if (initialized != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00003799 return;
William M. Brack8e2cc6f2004-07-03 23:28:52 +00003800
Daniel Veillardf93a8662004-07-01 12:56:30 +00003801 /* intialize the python extension module */
3802 Py_InitModule((char *) "libxml2mod", libxmlMethods);
3803
3804 /* initialize libxml2 */
3805 xmlInitParser();
Daniel Veillard5d819032002-02-02 21:49:17 +00003806 libxml_xmlErrorInitialize();
Daniel Veillard6361da02002-02-23 10:10:33 +00003807
Daniel Veillardf93a8662004-07-01 12:56:30 +00003808 initialized = 1;
3809
Daniel Veillard0fea6f42002-02-22 22:51:13 +00003810#ifdef MERGED_MODULES
3811 initlibxsltmod();
3812#endif
Daniel Veillardd2897fd2002-01-30 16:37:32 +00003813}