blob: 9e243146a9c71c23b3af00bb608561c777adf0a4 [file] [log] [blame]
Daniel Veillard3ce52572002-02-03 15:08:05 +00001/*
2 * libxml.c: this modules implements the main part of the glue of the
3 * libxml2 library and the Python interpreter. It provides the
4 * entry points where an automatically generated stub is either
5 * unpractical or would not match cleanly the Python model.
6 *
Daniel Veillard0fea6f42002-02-22 22:51:13 +00007 * If compiled with MERGED_MODULES, the entry point will be used to
8 * initialize both the libxml2 and the libxslt wrappers
9 *
Daniel Veillard3ce52572002-02-03 15:08:05 +000010 * See Copyright for the status of this software.
11 *
12 * daniel@veillard.com
13 */
Daniel Veillardd2897fd2002-01-30 16:37:32 +000014#include <Python.h>
Daniel Veillardc6d4a932002-09-12 15:00:57 +000015#include <fileobject.h>
Daniel Veillarda1196ed2002-11-23 11:22:49 +000016/* #include "config.h" */
Daniel Veillardf1d0e6b2002-01-31 23:42:44 +000017#include <libxml/xmlmemory.h>
Daniel Veillardd2897fd2002-01-30 16:37:32 +000018#include <libxml/parser.h>
19#include <libxml/tree.h>
Daniel Veillarda7340c82002-02-01 17:56:45 +000020#include <libxml/xpath.h>
Daniel Veillard5d819032002-02-02 21:49:17 +000021#include <libxml/xmlerror.h>
Daniel Veillarda7340c82002-02-01 17:56:45 +000022#include <libxml/xpathInternals.h>
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000023#include <libxml/xmlmemory.h>
Daniel Veillardc6d4a932002-09-12 15:00:57 +000024#include <libxml/xmlIO.h>
Daniel Veillardd5e198a2004-03-09 09:03:28 +000025#include <libxml/c14n.h>
Daniel Veillardd2897fd2002-01-30 16:37:32 +000026#include "libxml_wrap.h"
Daniel Veillard96fe0952002-01-30 20:52:23 +000027#include "libxml2-py.h"
Daniel Veillardd2897fd2002-01-30 16:37:32 +000028
Daniel Veillard0d132cf2002-12-23 14:43:32 +000029#if (defined(_MSC_VER) || defined(__MINGW32__)) && !defined(vsnprintf)
30#define vsnprintf(b,c,f,a) _vsnprintf(b,c,f,a)
Daniel Veillarde4fa2932003-03-26 00:38:10 +000031#elif defined(WITH_TRIO)
32#include "trio.h"
33#define vsnprintf trio_vsnprintf
Daniel Veillard0d132cf2002-12-23 14:43:32 +000034#endif
35
Daniel Veillardd2897fd2002-01-30 16:37:32 +000036/* #define DEBUG */
Daniel Veillard797a5652002-02-12 13:46:21 +000037/* #define DEBUG_SAX */
Daniel Veillarda7340c82002-02-01 17:56:45 +000038/* #define DEBUG_XPATH */
Daniel Veillard5d819032002-02-02 21:49:17 +000039/* #define DEBUG_ERROR */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000040/* #define DEBUG_MEMORY */
Daniel Veillardc6d4a932002-09-12 15:00:57 +000041/* #define DEBUG_FILES */
42/* #define DEBUG_LOADER */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000043
Daniel Veillardd2379012002-03-15 22:24:56 +000044void initlibxml2mod(void);
45
Daniel Veillardc6d4a932002-09-12 15:00:57 +000046/**
47 * TODO:
48 *
49 * macro to flag unimplemented blocks
50 */
51#define TODO \
52 xmlGenericError(xmlGenericErrorContext, \
53 "Unimplemented block at %s:%d\n", \
54 __FILE__, __LINE__);
William M. Brack8e2cc6f2004-07-03 23:28:52 +000055/*
56 * the following vars are used for XPath extensions, but
57 * are also referenced within the parser cleanup routine.
58 */
59static int libxml_xpathCallbacksInitialized = 0;
60
61typedef struct libxml_xpathCallback {
62 xmlXPathContextPtr ctx;
63 xmlChar *name;
64 xmlChar *ns_uri;
65 PyObject *function;
66} libxml_xpathCallback, *libxml_xpathCallbackPtr;
67typedef libxml_xpathCallback libxml_xpathCallbackArray[];
68static int libxml_xpathCallbacksAllocd = 10;
69static libxml_xpathCallbackArray *libxml_xpathCallbacks = NULL;
70static int libxml_xpathCallbacksNb = 0;
Daniel Veillardc6d4a932002-09-12 15:00:57 +000071
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000072/************************************************************************
73 * *
74 * Memory debug interface *
75 * *
76 ************************************************************************/
77
Daniel Veillardc2664642003-07-29 20:44:53 +000078#if 0
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000079extern void xmlMemFree(void *ptr);
80extern void *xmlMemMalloc(size_t size);
Daniel Veillardd2379012002-03-15 22:24:56 +000081extern void *xmlMemRealloc(void *ptr, size_t size);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000082extern char *xmlMemoryStrdup(const char *str);
Daniel Veillardc2664642003-07-29 20:44:53 +000083#endif
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000084
85static int libxmlMemoryDebugActivated = 0;
86static long libxmlMemoryAllocatedBase = 0;
87
88static int libxmlMemoryDebug = 0;
89static xmlFreeFunc freeFunc = NULL;
90static xmlMallocFunc mallocFunc = NULL;
91static xmlReallocFunc reallocFunc = NULL;
92static xmlStrdupFunc strdupFunc = NULL;
93
Daniel Veillardf93a8662004-07-01 12:56:30 +000094static void
95libxml_xmlErrorInitialize(void); /* forward declare */
96
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000097PyObject *
William M. Brackc68d78d2004-07-16 10:39:30 +000098libxml_xmlMemoryUsed(PyObject * self ATTRIBUTE_UNUSED,
99 PyObject * args ATTRIBUTE_UNUSED)
Daniel Veillard529233c2004-07-02 12:23:44 +0000100{
101 long ret;
102 PyObject *py_retval;
103
104 ret = xmlMemUsed();
105
106 py_retval = libxml_longWrap(ret);
107 return (py_retval);
108}
109
110PyObject *
William M. Brackc68d78d2004-07-16 10:39:30 +0000111libxml_xmlDebugMemory(PyObject * self ATTRIBUTE_UNUSED, PyObject * args)
Daniel Veillardd2379012002-03-15 22:24:56 +0000112{
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000113 int activate;
114 PyObject *py_retval;
115 long ret;
116
Daniel Veillardd2379012002-03-15 22:24:56 +0000117 if (!PyArg_ParseTuple(args, (char *) "i:xmlDebugMemory", &activate))
118 return (NULL);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000119
120#ifdef DEBUG_MEMORY
121 printf("libxml_xmlDebugMemory(%d) called\n", activate);
122#endif
123
124 if (activate != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000125 if (libxmlMemoryDebug == 0) {
126 /*
127 * First initialize the library and grab the old memory handlers
128 * and switch the library to memory debugging
129 */
130 xmlMemGet((xmlFreeFunc *) & freeFunc,
131 (xmlMallocFunc *) & mallocFunc,
132 (xmlReallocFunc *) & reallocFunc,
133 (xmlStrdupFunc *) & strdupFunc);
134 if ((freeFunc == xmlMemFree) && (mallocFunc == xmlMemMalloc) &&
135 (reallocFunc == xmlMemRealloc) &&
136 (strdupFunc == xmlMemoryStrdup)) {
137 libxmlMemoryAllocatedBase = xmlMemUsed();
138 } else {
Daniel Veillardf93a8662004-07-01 12:56:30 +0000139 /*
140 * cleanup first, because some memory has been
141 * allocated with the non-debug malloc in xmlInitParser
142 * when the python module was imported
143 */
144 xmlCleanupParser();
Daniel Veillardd2379012002-03-15 22:24:56 +0000145 ret = (long) xmlMemSetup(xmlMemFree, xmlMemMalloc,
146 xmlMemRealloc, xmlMemoryStrdup);
147 if (ret < 0)
148 goto error;
149 libxmlMemoryAllocatedBase = xmlMemUsed();
Daniel Veillardf93a8662004-07-01 12:56:30 +0000150 /* reinitialize */
151 xmlInitParser();
152 libxml_xmlErrorInitialize();
Daniel Veillardd2379012002-03-15 22:24:56 +0000153 }
Daniel Veillardd2379012002-03-15 22:24:56 +0000154 ret = 0;
155 } else if (libxmlMemoryDebugActivated == 0) {
156 libxmlMemoryAllocatedBase = xmlMemUsed();
157 ret = 0;
158 } else {
159 ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
160 }
161 libxmlMemoryDebug = 1;
162 libxmlMemoryDebugActivated = 1;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000163 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +0000164 if (libxmlMemoryDebugActivated == 1)
165 ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
166 else
167 ret = 0;
168 libxmlMemoryDebugActivated = 0;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000169 }
Daniel Veillardd2379012002-03-15 22:24:56 +0000170 error:
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000171 py_retval = libxml_longWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +0000172 return (py_retval);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000173}
174
175PyObject *
Daniel Veillardf93a8662004-07-01 12:56:30 +0000176libxml_xmlPythonCleanupParser(PyObject *self ATTRIBUTE_UNUSED,
177 PyObject *args ATTRIBUTE_UNUSED) {
178
William M. Brack8e2cc6f2004-07-03 23:28:52 +0000179 int ix;
180 long freed = -1;
Daniel Veillardf93a8662004-07-01 12:56:30 +0000181
182 if (libxmlMemoryDebug) {
183 freed = xmlMemUsed();
184 }
185
186 xmlCleanupParser();
William M. Brack8e2cc6f2004-07-03 23:28:52 +0000187 /*
188 * Need to confirm whether we really want to do this (required for
189 * memcheck) in all cases...
190 */
191
192 if (libxml_xpathCallbacks != NULL) { /* if ext funcs declared */
193 for (ix=0; ix<libxml_xpathCallbacksNb; ix++) {
194 if ((*libxml_xpathCallbacks)[ix].name != NULL)
195 xmlFree((*libxml_xpathCallbacks)[ix].name);
196 if ((*libxml_xpathCallbacks)[ix].ns_uri != NULL)
197 xmlFree((*libxml_xpathCallbacks)[ix].ns_uri);
198 }
199 libxml_xpathCallbacksNb = 0;
200 xmlFree(libxml_xpathCallbacks);
201 libxml_xpathCallbacks = NULL;
202 }
Daniel Veillardf93a8662004-07-01 12:56:30 +0000203
204 if (libxmlMemoryDebug) {
205 freed -= xmlMemUsed();
206 libxmlMemoryAllocatedBase -= freed;
207 if (libxmlMemoryAllocatedBase < 0)
208 libxmlMemoryAllocatedBase = 0;
209 }
210
211 Py_INCREF(Py_None);
212 return(Py_None);
213}
214
215PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +0000216libxml_xmlDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
217 ATTRIBUTE_UNUSED PyObject * args)
218{
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000219
220 if (libxmlMemoryDebug != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +0000221 xmlMemoryDump();
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000222 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +0000223 return (Py_None);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000224}
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000225
226/************************************************************************
227 * *
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000228 * Handling Python FILE I/O at the C level *
229 * The raw I/O attack diectly the File objects, while the *
230 * other routines address the ioWrapper instance instead *
231 * *
232 ************************************************************************/
233
234/**
235 * xmlPythonFileCloseUnref:
236 * @context: the I/O context
237 *
238 * Close an I/O channel
239 */
240static int
241xmlPythonFileCloseRaw (void * context) {
242 PyObject *file, *ret;
243
244#ifdef DEBUG_FILES
245 printf("xmlPythonFileCloseUnref\n");
246#endif
247 file = (PyObject *) context;
248 if (file == NULL) return(-1);
Daniel Veillard118aed72002-09-24 14:13:13 +0000249 ret = PyEval_CallMethod(file, (char *) "close", (char *) "()");
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000250 if (ret != NULL) {
251 Py_DECREF(ret);
252 }
253 Py_DECREF(file);
254 return(0);
255}
256
257/**
258 * xmlPythonFileReadRaw:
259 * @context: the I/O context
260 * @buffer: where to drop data
261 * @len: number of bytes to write
262 *
263 * Read @len bytes to @buffer from the Python file in the I/O channel
264 *
265 * Returns the number of bytes read
266 */
267static int
268xmlPythonFileReadRaw (void * context, char * buffer, int len) {
269 PyObject *file;
270 PyObject *ret;
271 int lenread = -1;
272 char *data;
273
274#ifdef DEBUG_FILES
275 printf("xmlPythonFileReadRaw: %d\n", len);
276#endif
277 file = (PyObject *) context;
278 if (file == NULL) return(-1);
Daniel Veillard118aed72002-09-24 14:13:13 +0000279 ret = PyEval_CallMethod(file, (char *) "read", (char *) "(i)", len);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000280 if (ret == NULL) {
281 printf("xmlPythonFileReadRaw: result is NULL\n");
282 return(-1);
283 } else if (PyString_Check(ret)) {
284 lenread = PyString_Size(ret);
285 data = PyString_AsString(ret);
286 if (lenread > len)
287 memcpy(buffer, data, len);
288 else
289 memcpy(buffer, data, lenread);
290 Py_DECREF(ret);
291 } else {
292 printf("xmlPythonFileReadRaw: result is not a String\n");
293 Py_DECREF(ret);
294 }
295 return(lenread);
296}
297
298/**
299 * xmlPythonFileRead:
300 * @context: the I/O context
301 * @buffer: where to drop data
302 * @len: number of bytes to write
303 *
304 * Read @len bytes to @buffer from the I/O channel.
305 *
306 * Returns the number of bytes read
307 */
308static int
309xmlPythonFileRead (void * context, char * buffer, int len) {
310 PyObject *file;
311 PyObject *ret;
312 int lenread = -1;
313 char *data;
314
315#ifdef DEBUG_FILES
316 printf("xmlPythonFileRead: %d\n", len);
317#endif
318 file = (PyObject *) context;
319 if (file == NULL) return(-1);
Daniel Veillard118aed72002-09-24 14:13:13 +0000320 ret = PyEval_CallMethod(file, (char *) "io_read", (char *) "(i)", len);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000321 if (ret == NULL) {
322 printf("xmlPythonFileRead: result is NULL\n");
323 return(-1);
324 } else if (PyString_Check(ret)) {
325 lenread = PyString_Size(ret);
326 data = PyString_AsString(ret);
327 if (lenread > len)
328 memcpy(buffer, data, len);
329 else
330 memcpy(buffer, data, lenread);
331 Py_DECREF(ret);
332 } else {
333 printf("xmlPythonFileRead: result is not a String\n");
334 Py_DECREF(ret);
335 }
336 return(lenread);
337}
338
339/**
340 * xmlFileWrite:
341 * @context: the I/O context
342 * @buffer: where to drop data
343 * @len: number of bytes to write
344 *
345 * Write @len bytes from @buffer to the I/O channel.
346 *
347 * Returns the number of bytes written
348 */
349static int
350xmlPythonFileWrite (void * context, const char * buffer, int len) {
351 PyObject *file;
352 PyObject *string;
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000353 PyObject *ret = NULL;
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000354 int written = -1;
355
356#ifdef DEBUG_FILES
357 printf("xmlPythonFileWrite: %d\n", len);
358#endif
359 file = (PyObject *) context;
360 if (file == NULL) return(-1);
361 string = PyString_FromStringAndSize(buffer, len);
362 if (string == NULL) return(-1);
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000363 if (PyObject_HasAttrString(file, (char *) "io_write")) {
364 ret = PyEval_CallMethod(file, (char *) "io_write", (char *) "(O)",
365 string);
366 } else if (PyObject_HasAttrString(file, (char *) "write")) {
367 ret = PyEval_CallMethod(file, (char *) "write", (char *) "(O)",
368 string);
369 }
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000370 Py_DECREF(string);
371 if (ret == NULL) {
372 printf("xmlPythonFileWrite: result is NULL\n");
373 return(-1);
374 } else if (PyInt_Check(ret)) {
375 written = (int) PyInt_AsLong(ret);
376 Py_DECREF(ret);
377 } else if (ret == Py_None) {
378 written = len;
379 Py_DECREF(ret);
380 } else {
381 printf("xmlPythonFileWrite: result is not an Int nor None\n");
382 Py_DECREF(ret);
383 }
384 return(written);
385}
386
387/**
388 * xmlPythonFileClose:
389 * @context: the I/O context
390 *
391 * Close an I/O channel
392 */
393static int
394xmlPythonFileClose (void * context) {
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000395 PyObject *file, *ret = NULL;
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000396
397#ifdef DEBUG_FILES
398 printf("xmlPythonFileClose\n");
399#endif
400 file = (PyObject *) context;
401 if (file == NULL) return(-1);
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000402 if (PyObject_HasAttrString(file, (char *) "io_close")) {
403 ret = PyEval_CallMethod(file, (char *) "io_close", (char *) "()");
404 } else if (PyObject_HasAttrString(file, (char *) "flush")) {
405 ret = PyEval_CallMethod(file, (char *) "flush", (char *) "()");
406 }
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000407 if (ret != NULL) {
408 Py_DECREF(ret);
409 }
410 return(0);
411}
412
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000413#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000414/**
415 * xmlOutputBufferCreatePythonFile:
416 * @file: a PyFile_Type
417 * @encoder: the encoding converter or NULL
418 *
419 * Create a buffered output for the progressive saving to a PyFile_Type
420 * buffered C I/O
421 *
422 * Returns the new parser output or NULL
423 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000424static xmlOutputBufferPtr
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000425xmlOutputBufferCreatePythonFile(PyObject *file,
426 xmlCharEncodingHandlerPtr encoder) {
427 xmlOutputBufferPtr ret;
428
429 if (file == NULL) return(NULL);
430
431 ret = xmlAllocOutputBuffer(encoder);
432 if (ret != NULL) {
433 ret->context = file;
434 /* Py_INCREF(file); */
435 ret->writecallback = xmlPythonFileWrite;
436 ret->closecallback = xmlPythonFileClose;
437 }
438
439 return(ret);
440}
441
442PyObject *
443libxml_xmlCreateOutputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
444 PyObject *py_retval;
445 PyObject *file;
446 xmlChar *encoding;
447 xmlCharEncodingHandlerPtr handler = NULL;
448 xmlOutputBufferPtr buffer;
449
450
451 if (!PyArg_ParseTuple(args, (char *)"Oz:xmlOutputBufferCreate",
452 &file, &encoding))
453 return(NULL);
454 if ((encoding != NULL) && (encoding[0] != 0)) {
Daniel Veillard118aed72002-09-24 14:13:13 +0000455 handler = xmlFindCharEncodingHandler((const char *) encoding);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000456 }
457 buffer = xmlOutputBufferCreatePythonFile(file, handler);
458 if (buffer == NULL)
459 printf("libxml_xmlCreateOutputBuffer: buffer == NULL\n");
460 py_retval = libxml_xmlOutputBufferPtrWrap(buffer);
461 return(py_retval);
462}
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000463
464/**
465 * libxml_outputBufferGetPythonFile:
466 * @buffer: the I/O buffer
467 *
468 * read the Python I/O from the CObject
469 *
470 * Returns the new parser output or NULL
471 */
472static PyObject *
473libxml_outputBufferGetPythonFile(ATTRIBUTE_UNUSED PyObject *self,
474 PyObject *args) {
475 PyObject *buffer;
476 PyObject *file;
477 xmlOutputBufferPtr obj;
478
479 if (!PyArg_ParseTuple(args, (char *)"O:outputBufferGetPythonFile",
480 &buffer))
481 return(NULL);
482
483 obj = PyoutputBuffer_Get(buffer);
484 if (obj == NULL) {
485 fprintf(stderr,
486 "outputBufferGetPythonFile: obj == NULL\n");
487 Py_INCREF(Py_None);
488 return(Py_None);
489 }
490 if (obj->closecallback != xmlPythonFileClose) {
491 fprintf(stderr,
492 "outputBufferGetPythonFile: not a python file wrapper\n");
493 Py_INCREF(Py_None);
494 return(Py_None);
495 }
496 file = (PyObject *) obj->context;
497 if (file == NULL) {
498 Py_INCREF(Py_None);
499 return(Py_None);
500 }
501 Py_INCREF(file);
502 return(file);
503}
504
Daniel Veillardd5e198a2004-03-09 09:03:28 +0000505static PyObject *
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000506libxml_xmlOutputBufferClose(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
507 PyObject *py_retval;
508 int c_retval;
509 xmlOutputBufferPtr out;
510 PyObject *pyobj_out;
511
512 if (!PyArg_ParseTuple(args, (char *)"O:xmlOutputBufferClose", &pyobj_out))
513 return(NULL);
514 out = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_out);
515
516 c_retval = xmlOutputBufferClose(out);
517 py_retval = libxml_intWrap((int) c_retval);
518 return(py_retval);
519}
520
Daniel Veillardd5e198a2004-03-09 09:03:28 +0000521static PyObject *
Daniel Veillard6cbd6c02003-12-04 12:31:49 +0000522libxml_xmlOutputBufferFlush(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
523 PyObject *py_retval;
524 int c_retval;
525 xmlOutputBufferPtr out;
526 PyObject *pyobj_out;
527
528 if (!PyArg_ParseTuple(args, (char *)"O:xmlOutputBufferFlush", &pyobj_out))
529 return(NULL);
530 out = (xmlOutputBufferPtr) PyoutputBuffer_Get(pyobj_out);
531
532 c_retval = xmlOutputBufferFlush(out);
533 py_retval = libxml_intWrap((int) c_retval);
534 return(py_retval);
535}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000536#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000537
538
539/**
540 * xmlParserInputBufferCreatePythonFile:
541 * @file: a PyFile_Type
542 * @encoder: the encoding converter or NULL
543 *
544 * Create a buffered output for the progressive saving to a PyFile_Type
545 * buffered C I/O
546 *
547 * Returns the new parser output or NULL
548 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000549static xmlParserInputBufferPtr
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000550xmlParserInputBufferCreatePythonFile(PyObject *file,
551 xmlCharEncoding encoding) {
552 xmlParserInputBufferPtr ret;
553
554 if (file == NULL) return(NULL);
555
556 ret = xmlAllocParserInputBuffer(encoding);
557 if (ret != NULL) {
558 ret->context = file;
559 /* Py_INCREF(file); */
560 ret->readcallback = xmlPythonFileRead;
561 ret->closecallback = xmlPythonFileClose;
562 }
563
564 return(ret);
565}
566
567PyObject *
568libxml_xmlCreateInputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
569 PyObject *py_retval;
570 PyObject *file;
571 xmlChar *encoding;
572 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
573 xmlParserInputBufferPtr buffer;
574
575
576 if (!PyArg_ParseTuple(args, (char *)"Oz:xmlParserInputBufferCreate",
577 &file, &encoding))
578 return(NULL);
579 if ((encoding != NULL) && (encoding[0] != 0)) {
Daniel Veillard118aed72002-09-24 14:13:13 +0000580 enc = xmlParseCharEncoding((const char *) encoding);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000581 }
582 buffer = xmlParserInputBufferCreatePythonFile(file, enc);
583 if (buffer == NULL)
584 printf("libxml_xmlParserInputBufferCreate: buffer == NULL\n");
585 py_retval = libxml_xmlParserInputBufferPtrWrap(buffer);
586 return(py_retval);
587}
588
589/************************************************************************
590 * *
591 * Providing the resolver at the Python level *
592 * *
593 ************************************************************************/
594
595static xmlExternalEntityLoader defaultExternalEntityLoader = NULL;
596static PyObject *pythonExternalEntityLoaderObjext;
597
598static xmlParserInputPtr
599pythonExternalEntityLoader(const char *URL, const char *ID,
600 xmlParserCtxtPtr ctxt) {
601 xmlParserInputPtr result = NULL;
602 if (pythonExternalEntityLoaderObjext != NULL) {
603 PyObject *ret;
604 PyObject *ctxtobj;
605
606 ctxtobj = libxml_xmlParserCtxtPtrWrap(ctxt);
607#ifdef DEBUG_LOADER
608 printf("pythonExternalEntityLoader: ready to call\n");
609#endif
610
611 ret = PyObject_CallFunction(pythonExternalEntityLoaderObjext,
Daniel Veillard118aed72002-09-24 14:13:13 +0000612 (char *) "(ssO)", URL, ID, ctxtobj);
Daniel Veillarde4a07e72003-01-14 14:40:25 +0000613 Py_XDECREF(ctxtobj);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000614#ifdef DEBUG_LOADER
615 printf("pythonExternalEntityLoader: result ");
Daniel Veillard007d51e2003-09-17 20:07:28 +0000616 PyObject_Print(ret, stderr, 0);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000617 printf("\n");
618#endif
619
620 if (ret != NULL) {
Daniel Veillard118aed72002-09-24 14:13:13 +0000621 if (PyObject_HasAttrString(ret, (char *) "read")) {
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000622 xmlParserInputBufferPtr buf;
623
624 buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
625 if (buf != NULL) {
626 buf->context = ret;
627 buf->readcallback = xmlPythonFileReadRaw;
628 buf->closecallback = xmlPythonFileCloseRaw;
629 result = xmlNewIOInputStream(ctxt, buf,
630 XML_CHAR_ENCODING_NONE);
631 }
632 } else {
633 printf("pythonExternalEntityLoader: can't read\n");
634 }
635 if (result == NULL) {
636 Py_DECREF(ret);
Daniel Veillardc64b8e92003-02-24 11:47:13 +0000637 } else if (URL != NULL) {
William M. Brackc1939562003-08-05 15:52:22 +0000638 result->filename = (char *) xmlStrdup((const xmlChar *)URL);
Daniel Veillardc64b8e92003-02-24 11:47:13 +0000639 result->directory = xmlParserGetDirectory((const char *) URL);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000640 }
641 }
642 }
643 if ((result == NULL) && (defaultExternalEntityLoader != NULL)) {
644 result = defaultExternalEntityLoader(URL, ID, ctxt);
645 }
646 return(result);
647}
648
649PyObject *
650libxml_xmlSetEntityLoader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
651 PyObject *py_retval;
652 PyObject *loader;
653
654 if (!PyArg_ParseTuple(args, (char *)"O:libxml_xmlSetEntityLoader",
655 &loader))
656 return(NULL);
657
658#ifdef DEBUG_LOADER
659 printf("libxml_xmlSetEntityLoader\n");
660#endif
661 if (defaultExternalEntityLoader == NULL)
662 defaultExternalEntityLoader = xmlGetExternalEntityLoader();
663
664 pythonExternalEntityLoaderObjext = loader;
665 xmlSetExternalEntityLoader(pythonExternalEntityLoader);
666
667 py_retval = PyInt_FromLong(0);
668 return(py_retval);
669}
670
671
672/************************************************************************
673 * *
Daniel Veillard3ce52572002-02-03 15:08:05 +0000674 * Handling SAX/xmllib/sgmlop callback interfaces *
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000675 * *
676 ************************************************************************/
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000677
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000678static void
679pythonStartElement(void *user_data, const xmlChar * name,
680 const xmlChar ** attrs)
681{
682 int i;
683 PyObject *handler;
684 PyObject *dict;
685 PyObject *attrname;
686 PyObject *attrvalue;
Daniel Veillardd2379012002-03-15 22:24:56 +0000687 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000688 int type = 0;
689
Daniel Veillard797a5652002-02-12 13:46:21 +0000690#ifdef DEBUG_SAX
691 printf("pythonStartElement(%s) called\n", name);
692#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000693 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000694 if (PyObject_HasAttrString(handler, (char *) "startElement"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000695 type = 1;
Daniel Veillardd2379012002-03-15 22:24:56 +0000696 else if (PyObject_HasAttrString(handler, (char *) "start"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000697 type = 2;
698 if (type != 0) {
699 /*
700 * the xmllib interface always generate a dictionnary,
701 * possibly empty
702 */
703 if ((attrs == NULL) && (type == 1)) {
704 Py_XINCREF(Py_None);
705 dict = Py_None;
Daniel Veillardd2379012002-03-15 22:24:56 +0000706 } else if (attrs == NULL) {
707 dict = PyDict_New();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000708 } else {
709 dict = PyDict_New();
710 for (i = 0; attrs[i] != NULL; i++) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000711 attrname = PyString_FromString((char *) attrs[i]);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000712 i++;
713 if (attrs[i] != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000714 attrvalue = PyString_FromString((char *) attrs[i]);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000715 } else {
716 Py_XINCREF(Py_None);
717 attrvalue = Py_None;
718 }
719 PyDict_SetItem(dict, attrname, attrvalue);
720 }
721 }
722
723 if (type == 1)
Daniel Veillardd2379012002-03-15 22:24:56 +0000724 result = PyObject_CallMethod(handler, (char *) "startElement",
725 (char *) "sO", name, dict);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000726 else if (type == 2)
Daniel Veillardd2379012002-03-15 22:24:56 +0000727 result = PyObject_CallMethod(handler, (char *) "start",
728 (char *) "sO", name, dict);
729 if (PyErr_Occurred())
730 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000731 Py_XDECREF(dict);
732 Py_XDECREF(result);
733 }
734}
735
736static void
737pythonStartDocument(void *user_data)
738{
739 PyObject *handler;
740 PyObject *result;
741
Daniel Veillard797a5652002-02-12 13:46:21 +0000742#ifdef DEBUG_SAX
743 printf("pythonStartDocument() called\n");
744#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000745 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000746 if (PyObject_HasAttrString(handler, (char *) "startDocument")) {
747 result =
748 PyObject_CallMethod(handler, (char *) "startDocument", NULL);
749 if (PyErr_Occurred())
750 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000751 Py_XDECREF(result);
752 }
753}
754
755static void
756pythonEndDocument(void *user_data)
757{
758 PyObject *handler;
759 PyObject *result;
760
Daniel Veillard797a5652002-02-12 13:46:21 +0000761#ifdef DEBUG_SAX
762 printf("pythonEndDocument() called\n");
763#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000764 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000765 if (PyObject_HasAttrString(handler, (char *) "endDocument")) {
766 result =
767 PyObject_CallMethod(handler, (char *) "endDocument", NULL);
768 if (PyErr_Occurred())
769 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000770 Py_XDECREF(result);
771 }
772 /*
773 * The reference to the handler is released there
774 */
775 Py_XDECREF(handler);
776}
777
778static void
779pythonEndElement(void *user_data, const xmlChar * name)
780{
781 PyObject *handler;
782 PyObject *result;
783
Daniel Veillard797a5652002-02-12 13:46:21 +0000784#ifdef DEBUG_SAX
785 printf("pythonEndElement(%s) called\n", name);
786#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000787 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000788 if (PyObject_HasAttrString(handler, (char *) "endElement")) {
789 result = PyObject_CallMethod(handler, (char *) "endElement",
790 (char *) "s", name);
791 if (PyErr_Occurred())
792 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000793 Py_XDECREF(result);
Daniel Veillardd2379012002-03-15 22:24:56 +0000794 } else if (PyObject_HasAttrString(handler, (char *) "end")) {
795 result = PyObject_CallMethod(handler, (char *) "end",
796 (char *) "s", name);
797 if (PyErr_Occurred())
798 PyErr_Print();
Daniel Veillard797a5652002-02-12 13:46:21 +0000799 Py_XDECREF(result);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000800 }
801}
802
803static void
804pythonReference(void *user_data, const xmlChar * name)
805{
806 PyObject *handler;
807 PyObject *result;
808
Daniel Veillard797a5652002-02-12 13:46:21 +0000809#ifdef DEBUG_SAX
810 printf("pythonReference(%s) called\n", name);
811#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000812 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000813 if (PyObject_HasAttrString(handler, (char *) "reference")) {
814 result = PyObject_CallMethod(handler, (char *) "reference",
815 (char *) "s", name);
816 if (PyErr_Occurred())
817 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000818 Py_XDECREF(result);
819 }
820}
821
822static void
823pythonCharacters(void *user_data, const xmlChar * ch, int len)
824{
825 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +0000826 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000827 int type = 0;
828
Daniel Veillard797a5652002-02-12 13:46:21 +0000829#ifdef DEBUG_SAX
830 printf("pythonCharacters(%s, %d) called\n", ch, len);
831#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000832 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000833 if (PyObject_HasAttrString(handler, (char *) "characters"))
834 type = 1;
835 else if (PyObject_HasAttrString(handler, (char *) "data"))
836 type = 2;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000837 if (type != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000838 if (type == 1)
839 result = PyObject_CallMethod(handler, (char *) "characters",
840 (char *) "s#", ch, len);
841 else if (type == 2)
842 result = PyObject_CallMethod(handler, (char *) "data",
843 (char *) "s#", ch, len);
844 if (PyErr_Occurred())
845 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000846 Py_XDECREF(result);
847 }
848}
849
850static void
851pythonIgnorableWhitespace(void *user_data, const xmlChar * ch, int len)
852{
853 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +0000854 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000855 int type = 0;
856
Daniel Veillard797a5652002-02-12 13:46:21 +0000857#ifdef DEBUG_SAX
858 printf("pythonIgnorableWhitespace(%s, %d) called\n", ch, len);
859#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000860 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000861 if (PyObject_HasAttrString(handler, (char *) "ignorableWhitespace"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000862 type = 1;
Daniel Veillardd2379012002-03-15 22:24:56 +0000863 else if (PyObject_HasAttrString(handler, (char *) "data"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000864 type = 2;
865 if (type != 0) {
866 if (type == 1)
867 result =
Daniel Veillardd2379012002-03-15 22:24:56 +0000868 PyObject_CallMethod(handler,
869 (char *) "ignorableWhitespace",
870 (char *) "s#", ch, len);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000871 else if (type == 2)
Daniel Veillardd2379012002-03-15 22:24:56 +0000872 result =
873 PyObject_CallMethod(handler, (char *) "data",
874 (char *) "s#", ch, len);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000875 Py_XDECREF(result);
876 }
877}
878
879static void
880pythonProcessingInstruction(void *user_data,
881 const xmlChar * target, const xmlChar * data)
882{
883 PyObject *handler;
884 PyObject *result;
885
Daniel Veillard797a5652002-02-12 13:46:21 +0000886#ifdef DEBUG_SAX
887 printf("pythonProcessingInstruction(%s, %s) called\n", target, data);
888#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000889 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000890 if (PyObject_HasAttrString(handler, (char *) "processingInstruction")) {
891 result = PyObject_CallMethod(handler, (char *)
892 "processingInstruction",
893 (char *) "ss", target, data);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000894 Py_XDECREF(result);
895 }
896}
897
898static void
899pythonComment(void *user_data, const xmlChar * value)
900{
901 PyObject *handler;
902 PyObject *result;
903
Daniel Veillard797a5652002-02-12 13:46:21 +0000904#ifdef DEBUG_SAX
905 printf("pythonComment(%s) called\n", value);
906#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000907 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000908 if (PyObject_HasAttrString(handler, (char *) "comment")) {
909 result =
910 PyObject_CallMethod(handler, (char *) "comment", (char *) "s",
911 value);
912 if (PyErr_Occurred())
913 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000914 Py_XDECREF(result);
915 }
916}
917
918static void
919pythonWarning(void *user_data, const char *msg, ...)
920{
921 PyObject *handler;
922 PyObject *result;
923 va_list args;
924 char buf[1024];
925
Daniel Veillard797a5652002-02-12 13:46:21 +0000926#ifdef DEBUG_SAX
927 printf("pythonWarning(%s) called\n", msg);
928#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000929 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000930 if (PyObject_HasAttrString(handler, (char *) "warning")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000931 va_start(args, msg);
932 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +0000933 va_end(args);
934 buf[1023] = 0;
935 result =
936 PyObject_CallMethod(handler, (char *) "warning", (char *) "s",
937 buf);
938 if (PyErr_Occurred())
939 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000940 Py_XDECREF(result);
941 }
942}
943
944static void
945pythonError(void *user_data, const char *msg, ...)
946{
947 PyObject *handler;
948 PyObject *result;
949 va_list args;
950 char buf[1024];
951
Daniel Veillard797a5652002-02-12 13:46:21 +0000952#ifdef DEBUG_SAX
953 printf("pythonError(%s) called\n", msg);
954#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000955 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000956 if (PyObject_HasAttrString(handler, (char *) "error")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000957 va_start(args, msg);
958 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +0000959 va_end(args);
960 buf[1023] = 0;
961 result =
962 PyObject_CallMethod(handler, (char *) "error", (char *) "s",
963 buf);
964 if (PyErr_Occurred())
965 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000966 Py_XDECREF(result);
967 }
968}
969
970static void
971pythonFatalError(void *user_data, const char *msg, ...)
972{
973 PyObject *handler;
974 PyObject *result;
975 va_list args;
976 char buf[1024];
977
Daniel Veillard797a5652002-02-12 13:46:21 +0000978#ifdef DEBUG_SAX
979 printf("pythonFatalError(%s) called\n", msg);
980#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000981 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000982 if (PyObject_HasAttrString(handler, (char *) "fatalError")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000983 va_start(args, msg);
984 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +0000985 va_end(args);
986 buf[1023] = 0;
987 result =
988 PyObject_CallMethod(handler, (char *) "fatalError",
989 (char *) "s", buf);
990 if (PyErr_Occurred())
991 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000992 Py_XDECREF(result);
993 }
994}
995
996static void
997pythonCdataBlock(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("pythonCdataBlock(%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 *) "cdataBlock"))
1008 type = 1;
1009 else if (PyObject_HasAttrString(handler, (char *) "cdata"))
1010 type = 2;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001011 if (type != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001012 if (type == 1)
1013 result =
1014 PyObject_CallMethod(handler, (char *) "cdataBlock",
1015 (char *) "s#", ch, len);
1016 else if (type == 2)
1017 result =
1018 PyObject_CallMethod(handler, (char *) "cdata",
1019 (char *) "s#", ch, len);
1020 if (PyErr_Occurred())
1021 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001022 Py_XDECREF(result);
1023 }
1024}
1025
1026static void
1027pythonExternalSubset(void *user_data,
1028 const xmlChar * name,
1029 const xmlChar * externalID, const xmlChar * systemID)
1030{
1031 PyObject *handler;
1032 PyObject *result;
1033
Daniel Veillard797a5652002-02-12 13:46:21 +00001034#ifdef DEBUG_SAX
1035 printf("pythonExternalSubset(%s, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001036 name, externalID, systemID);
Daniel Veillard797a5652002-02-12 13:46:21 +00001037#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001038 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001039 if (PyObject_HasAttrString(handler, (char *) "externalSubset")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001040 result =
Daniel Veillardd2379012002-03-15 22:24:56 +00001041 PyObject_CallMethod(handler, (char *) "externalSubset",
1042 (char *) "sss", name, externalID,
1043 systemID);
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001044 Py_XDECREF(result);
1045 }
1046}
1047
1048static void
1049pythonEntityDecl(void *user_data,
1050 const xmlChar * name,
1051 int type,
1052 const xmlChar * publicId,
1053 const xmlChar * systemId, xmlChar * content)
1054{
1055 PyObject *handler;
1056 PyObject *result;
1057
1058 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001059 if (PyObject_HasAttrString(handler, (char *) "entityDecl")) {
1060 result = PyObject_CallMethod(handler, (char *) "entityDecl",
1061 (char *) "sisss", name, type,
1062 publicId, systemId, content);
1063 if (PyErr_Occurred())
1064 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001065 Py_XDECREF(result);
1066 }
1067}
1068
1069
1070
1071static void
1072
1073pythonNotationDecl(void *user_data,
1074 const xmlChar * name,
1075 const xmlChar * publicId, const xmlChar * systemId)
1076{
1077 PyObject *handler;
1078 PyObject *result;
1079
1080 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001081 if (PyObject_HasAttrString(handler, (char *) "notationDecl")) {
1082 result = PyObject_CallMethod(handler, (char *) "notationDecl",
1083 (char *) "sss", name, publicId,
1084 systemId);
1085 if (PyErr_Occurred())
1086 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001087 Py_XDECREF(result);
1088 }
1089}
1090
1091static void
1092pythonAttributeDecl(void *user_data,
1093 const xmlChar * elem,
1094 const xmlChar * name,
1095 int type,
1096 int def,
Daniel Veillardd2379012002-03-15 22:24:56 +00001097 const xmlChar * defaultValue, xmlEnumerationPtr tree)
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001098{
1099 PyObject *handler;
1100 PyObject *nameList;
1101 PyObject *newName;
1102 xmlEnumerationPtr node;
1103 PyObject *result;
1104 int count;
1105
1106 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001107 if (PyObject_HasAttrString(handler, (char *) "attributeDecl")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001108 count = 0;
1109 for (node = tree; node != NULL; node = node->next) {
1110 count++;
1111 }
1112 nameList = PyList_New(count);
1113 count = 0;
1114 for (node = tree; node != NULL; node = node->next) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001115 newName = PyString_FromString((char *) node->name);
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001116 PyList_SetItem(nameList, count, newName);
1117 count++;
1118 }
Daniel Veillardd2379012002-03-15 22:24:56 +00001119 result = PyObject_CallMethod(handler, (char *) "attributeDecl",
1120 (char *) "ssiisO", elem, name, type,
1121 def, defaultValue, nameList);
1122 if (PyErr_Occurred())
1123 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001124 Py_XDECREF(nameList);
1125 Py_XDECREF(result);
1126 }
1127}
1128
1129static void
1130pythonElementDecl(void *user_data,
1131 const xmlChar * name,
Daniel Veillardd2379012002-03-15 22:24:56 +00001132 int type, ATTRIBUTE_UNUSED xmlElementContentPtr content)
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001133{
1134 PyObject *handler;
1135 PyObject *obj;
1136 PyObject *result;
1137
1138 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001139 if (PyObject_HasAttrString(handler, (char *) "elementDecl")) {
1140 /* TODO: wrap in an elementContent object */
1141 printf
1142 ("pythonElementDecl: xmlElementContentPtr wrapper missing !\n");
1143 obj = Py_None;
1144 /* Py_XINCREF(Py_None); isn't the reference just borrowed ??? */
1145 result = PyObject_CallMethod(handler, (char *) "elementDecl",
1146 (char *) "siO", name, type, obj);
1147 if (PyErr_Occurred())
1148 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001149 Py_XDECREF(result);
1150 }
1151}
1152
1153static void
1154pythonUnparsedEntityDecl(void *user_data,
1155 const xmlChar * name,
1156 const xmlChar * publicId,
1157 const xmlChar * systemId,
1158 const xmlChar * notationName)
1159{
1160 PyObject *handler;
1161 PyObject *result;
1162
1163 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001164 if (PyObject_HasAttrString(handler, (char *) "unparsedEntityDecl")) {
1165 result =
1166 PyObject_CallMethod(handler, (char *) "unparsedEntityDecl",
1167 (char *) "ssss", name, publicId, systemId,
1168 notationName);
1169 if (PyErr_Occurred())
1170 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001171 Py_XDECREF(result);
1172 }
1173}
1174
1175static void
1176pythonInternalSubset(void *user_data, const xmlChar * name,
1177 const xmlChar * ExternalID, const xmlChar * SystemID)
1178{
1179 PyObject *handler;
1180 PyObject *result;
1181
Daniel Veillard797a5652002-02-12 13:46:21 +00001182#ifdef DEBUG_SAX
1183 printf("pythonInternalSubset(%s, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001184 name, ExternalID, SystemID);
Daniel Veillard797a5652002-02-12 13:46:21 +00001185#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001186 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001187 if (PyObject_HasAttrString(handler, (char *) "internalSubset")) {
1188 result = PyObject_CallMethod(handler, (char *) "internalSubset",
1189 (char *) "sss", name, ExternalID,
1190 SystemID);
1191 if (PyErr_Occurred())
1192 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001193 Py_XDECREF(result);
1194 }
1195}
1196
1197static xmlSAXHandler pythonSaxHandler = {
1198 pythonInternalSubset,
Daniel Veillardd2379012002-03-15 22:24:56 +00001199 NULL, /* TODO pythonIsStandalone, */
1200 NULL, /* TODO pythonHasInternalSubset, */
1201 NULL, /* TODO pythonHasExternalSubset, */
1202 NULL, /* TODO pythonResolveEntity, */
1203 NULL, /* TODO pythonGetEntity, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001204 pythonEntityDecl,
1205 pythonNotationDecl,
1206 pythonAttributeDecl,
1207 pythonElementDecl,
1208 pythonUnparsedEntityDecl,
Daniel Veillardd2379012002-03-15 22:24:56 +00001209 NULL, /* OBSOLETED pythonSetDocumentLocator, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001210 pythonStartDocument,
1211 pythonEndDocument,
1212 pythonStartElement,
1213 pythonEndElement,
1214 pythonReference,
1215 pythonCharacters,
1216 pythonIgnorableWhitespace,
1217 pythonProcessingInstruction,
1218 pythonComment,
1219 pythonWarning,
1220 pythonError,
1221 pythonFatalError,
Daniel Veillardd2379012002-03-15 22:24:56 +00001222 NULL, /* TODO pythonGetParameterEntity, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001223 pythonCdataBlock,
1224 pythonExternalSubset,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001225 1,
1226 NULL, /* TODO mograte to SAX2 */
1227 NULL,
William M. Brack871611b2003-10-18 04:53:14 +00001228 NULL,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001229 NULL
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001230};
Daniel Veillard3ce52572002-02-03 15:08:05 +00001231
1232/************************************************************************
1233 * *
1234 * Handling of specific parser context *
1235 * *
1236 ************************************************************************/
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001237
1238PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001239libxml_xmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
1240 PyObject * args)
1241{
1242 const char *chunk;
Daniel Veillard3ce52572002-02-03 15:08:05 +00001243 int size;
Daniel Veillardd2379012002-03-15 22:24:56 +00001244 const char *URI;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001245 PyObject *pyobj_SAX = NULL;
Daniel Veillard3ce52572002-02-03 15:08:05 +00001246 xmlSAXHandlerPtr SAX = NULL;
Daniel Veillard3ce52572002-02-03 15:08:05 +00001247 xmlParserCtxtPtr ret;
1248 PyObject *pyret;
Daniel Veillard96fe0952002-01-30 20:52:23 +00001249
Daniel Veillardd2379012002-03-15 22:24:56 +00001250 if (!PyArg_ParseTuple
1251 (args, (char *) "Oziz:xmlCreatePushParser", &pyobj_SAX, &chunk,
1252 &size, &URI))
1253 return (NULL);
Daniel Veillard3ce52572002-02-03 15:08:05 +00001254
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001255#ifdef DEBUG
Daniel Veillard3ce52572002-02-03 15:08:05 +00001256 printf("libxml_xmlCreatePushParser(%p, %s, %d, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001257 pyobj_SAX, chunk, size, URI);
Daniel Veillard96fe0952002-01-30 20:52:23 +00001258#endif
Daniel Veillard3ce52572002-02-03 15:08:05 +00001259 if (pyobj_SAX != Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001260 SAX = &pythonSaxHandler;
1261 Py_INCREF(pyobj_SAX);
1262 /* The reference is released in pythonEndDocument() */
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001263 }
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001264 ret = xmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI);
Daniel Veillard3ce52572002-02-03 15:08:05 +00001265 pyret = libxml_xmlParserCtxtPtrWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +00001266 return (pyret);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001267}
Daniel Veillard5d819032002-02-02 21:49:17 +00001268
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001269PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001270libxml_htmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
1271 PyObject * args)
1272{
Daniel Veillard656ce942004-04-30 23:11:45 +00001273#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001274 const char *chunk;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001275 int size;
Daniel Veillardd2379012002-03-15 22:24:56 +00001276 const char *URI;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001277 PyObject *pyobj_SAX = NULL;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001278 xmlSAXHandlerPtr SAX = NULL;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001279 xmlParserCtxtPtr ret;
1280 PyObject *pyret;
1281
Daniel Veillardd2379012002-03-15 22:24:56 +00001282 if (!PyArg_ParseTuple
1283 (args, (char *) "Oziz:htmlCreatePushParser", &pyobj_SAX, &chunk,
1284 &size, &URI))
1285 return (NULL);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001286
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001287#ifdef DEBUG
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001288 printf("libxml_htmlCreatePushParser(%p, %s, %d, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001289 pyobj_SAX, chunk, size, URI);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001290#endif
1291 if (pyobj_SAX != Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001292 SAX = &pythonSaxHandler;
1293 Py_INCREF(pyobj_SAX);
1294 /* The reference is released in pythonEndDocument() */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001295 }
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001296 ret = htmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI,
Daniel Veillardd2379012002-03-15 22:24:56 +00001297 XML_CHAR_ENCODING_NONE);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001298 pyret = libxml_xmlParserCtxtPtrWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +00001299 return (pyret);
Daniel Veillard656ce942004-04-30 23:11:45 +00001300#else
1301 Py_INCREF(Py_None);
1302 return (Py_None);
1303#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001304}
1305
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001306PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001307libxml_xmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1308{
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001309 int recover;
Daniel Veillardd2379012002-03-15 22:24:56 +00001310 const char *URI;
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001311 PyObject *pyobj_SAX = NULL;
1312 xmlSAXHandlerPtr SAX = NULL;
1313
Daniel Veillardd2379012002-03-15 22:24:56 +00001314 if (!PyArg_ParseTuple(args, (char *) "Osi:xmlSAXParseFile", &pyobj_SAX,
1315 &URI, &recover))
1316 return (NULL);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001317
1318#ifdef DEBUG
1319 printf("libxml_xmlSAXParseFile(%p, %s, %d) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001320 pyobj_SAX, URI, recover);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001321#endif
1322 if (pyobj_SAX == Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001323 Py_INCREF(Py_None);
1324 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001325 }
1326 SAX = &pythonSaxHandler;
1327 Py_INCREF(pyobj_SAX);
1328 /* The reference is released in pythonEndDocument() */
1329 xmlSAXParseFileWithData(SAX, URI, recover, pyobj_SAX);
1330 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +00001331 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001332}
1333
1334PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001335libxml_htmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1336{
Daniel Veillard656ce942004-04-30 23:11:45 +00001337#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001338 const char *URI;
1339 const char *encoding;
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001340 PyObject *pyobj_SAX = NULL;
1341 xmlSAXHandlerPtr SAX = NULL;
1342
Daniel Veillardd2379012002-03-15 22:24:56 +00001343 if (!PyArg_ParseTuple
1344 (args, (char *) "Osz:htmlSAXParseFile", &pyobj_SAX, &URI,
1345 &encoding))
1346 return (NULL);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001347
1348#ifdef DEBUG
1349 printf("libxml_htmlSAXParseFile(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001350 pyobj_SAX, URI, encoding);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001351#endif
1352 if (pyobj_SAX == Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001353 Py_INCREF(Py_None);
1354 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001355 }
1356 SAX = &pythonSaxHandler;
1357 Py_INCREF(pyobj_SAX);
1358 /* The reference is released in pythonEndDocument() */
1359 htmlSAXParseFile(URI, encoding, SAX, pyobj_SAX);
1360 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +00001361 return (Py_None);
Daniel Veillard656ce942004-04-30 23:11:45 +00001362#else
1363 Py_INCREF(Py_None);
1364 return (Py_None);
1365#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001366}
1367
Daniel Veillard5d819032002-02-02 21:49:17 +00001368/************************************************************************
1369 * *
1370 * Error message callback *
1371 * *
1372 ************************************************************************/
1373
1374static PyObject *libxml_xmlPythonErrorFuncHandler = NULL;
1375static PyObject *libxml_xmlPythonErrorFuncCtxt = NULL;
1376
Daniel Veillarde6227e02003-01-14 11:42:39 +00001377/* helper to build a xmlMalloc'ed string from a format and va_list */
1378static char *
1379libxml_buildMessage(const char *msg, va_list ap)
Daniel Veillardd2379012002-03-15 22:24:56 +00001380{
1381 int size;
1382 int chars;
1383 char *larger;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001384 char *str;
1385
1386 str = (char *) xmlMalloc(150);
1387 if (str == NULL)
1388 return NULL;
1389
1390 size = 150;
1391
1392 while (1) {
1393 chars = vsnprintf(str, size, msg, ap);
1394 if ((chars > -1) && (chars < size))
1395 break;
1396 if (chars > -1)
1397 size += chars + 1;
1398 else
1399 size += 100;
1400 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
1401 xmlFree(str);
1402 return NULL;
1403 }
1404 str = larger;
1405 }
1406
1407 return str;
1408}
1409
1410static void
1411libxml_xmlErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, const char *msg,
1412 ...)
1413{
Daniel Veillardd2379012002-03-15 22:24:56 +00001414 va_list ap;
1415 char *str;
Daniel Veillard5d819032002-02-02 21:49:17 +00001416 PyObject *list;
1417 PyObject *message;
1418 PyObject *result;
1419
1420#ifdef DEBUG_ERROR
1421 printf("libxml_xmlErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
1422#endif
1423
1424
1425 if (libxml_xmlPythonErrorFuncHandler == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001426 va_start(ap, msg);
Daniel Veillard007d51e2003-09-17 20:07:28 +00001427 vfprintf(stderr, msg, ap);
Daniel Veillardd2379012002-03-15 22:24:56 +00001428 va_end(ap);
Daniel Veillard5d819032002-02-02 21:49:17 +00001429 } else {
Daniel Veillarde6227e02003-01-14 11:42:39 +00001430 va_start(ap, msg);
1431 str = libxml_buildMessage(msg,ap);
1432 va_end(ap);
Daniel Veillard5d819032002-02-02 21:49:17 +00001433
Daniel Veillardd2379012002-03-15 22:24:56 +00001434 list = PyTuple_New(2);
1435 PyTuple_SetItem(list, 0, libxml_xmlPythonErrorFuncCtxt);
1436 Py_XINCREF(libxml_xmlPythonErrorFuncCtxt);
1437 message = libxml_charPtrWrap(str);
1438 PyTuple_SetItem(list, 1, message);
1439 result = PyEval_CallObject(libxml_xmlPythonErrorFuncHandler, list);
1440 Py_XDECREF(list);
1441 Py_XDECREF(result);
Daniel Veillard5d819032002-02-02 21:49:17 +00001442 }
1443}
1444
1445static void
Daniel Veillardd2379012002-03-15 22:24:56 +00001446libxml_xmlErrorInitialize(void)
1447{
Daniel Veillard5d819032002-02-02 21:49:17 +00001448#ifdef DEBUG_ERROR
1449 printf("libxml_xmlErrorInitialize() called\n");
1450#endif
1451 xmlSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
Daniel Veillard781ac8b2003-05-15 22:11:36 +00001452 xmlThrDefSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
Daniel Veillard5d819032002-02-02 21:49:17 +00001453}
1454
Daniel Veillardc2664642003-07-29 20:44:53 +00001455static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001456libxml_xmlRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self,
1457 PyObject * args)
1458{
Daniel Veillard5d819032002-02-02 21:49:17 +00001459 PyObject *py_retval;
1460 PyObject *pyobj_f;
1461 PyObject *pyobj_ctx;
1462
Daniel Veillardd2379012002-03-15 22:24:56 +00001463 if (!PyArg_ParseTuple
1464 (args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f,
1465 &pyobj_ctx))
1466 return (NULL);
Daniel Veillard5d819032002-02-02 21:49:17 +00001467
1468#ifdef DEBUG_ERROR
William M. Brack8e2cc6f2004-07-03 23:28:52 +00001469 printf("libxml_xmlRegisterErrorHandler(%p, %p) called\n", pyobj_ctx,
Daniel Veillardd2379012002-03-15 22:24:56 +00001470 pyobj_f);
Daniel Veillard5d819032002-02-02 21:49:17 +00001471#endif
1472
1473 if (libxml_xmlPythonErrorFuncHandler != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001474 Py_XDECREF(libxml_xmlPythonErrorFuncHandler);
Daniel Veillard5d819032002-02-02 21:49:17 +00001475 }
1476 if (libxml_xmlPythonErrorFuncCtxt != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001477 Py_XDECREF(libxml_xmlPythonErrorFuncCtxt);
Daniel Veillard5d819032002-02-02 21:49:17 +00001478 }
1479
1480 Py_XINCREF(pyobj_ctx);
1481 Py_XINCREF(pyobj_f);
1482
1483 /* TODO: check f is a function ! */
1484 libxml_xmlPythonErrorFuncHandler = pyobj_f;
1485 libxml_xmlPythonErrorFuncCtxt = pyobj_ctx;
1486
1487 py_retval = libxml_intWrap(1);
Daniel Veillardd2379012002-03-15 22:24:56 +00001488 return (py_retval);
Daniel Veillard5d819032002-02-02 21:49:17 +00001489}
Daniel Veillardd2379012002-03-15 22:24:56 +00001490
Daniel Veillarde6227e02003-01-14 11:42:39 +00001491
1492/************************************************************************
1493 * *
1494 * Per parserCtxt error handler *
1495 * *
1496 ************************************************************************/
1497
Daniel Veillard417be3a2003-01-20 21:26:34 +00001498typedef struct
Daniel Veillarde6227e02003-01-14 11:42:39 +00001499{
Daniel Veillard417be3a2003-01-20 21:26:34 +00001500 PyObject *f;
1501 PyObject *arg;
1502} xmlParserCtxtPyCtxt;
1503typedef xmlParserCtxtPyCtxt *xmlParserCtxtPyCtxtPtr;
1504
1505static void
1506libxml_xmlParserCtxtGenericErrorFuncHandler(void *ctx, int severity, char *str)
1507{
Daniel Veillarde6227e02003-01-14 11:42:39 +00001508 PyObject *list;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001509 PyObject *result;
Daniel Veillard417be3a2003-01-20 21:26:34 +00001510 xmlParserCtxtPtr ctxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001511 xmlParserCtxtPyCtxtPtr pyCtxt;
1512
1513#ifdef DEBUG_ERROR
Daniel Veillard417be3a2003-01-20 21:26:34 +00001514 printf("libxml_xmlParserCtxtGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001515#endif
1516
Daniel Veillard417be3a2003-01-20 21:26:34 +00001517 ctxt = (xmlParserCtxtPtr)ctx;
1518 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001519
Daniel Veillard417be3a2003-01-20 21:26:34 +00001520 list = PyTuple_New(4);
1521 PyTuple_SetItem(list, 0, pyCtxt->arg);
1522 Py_XINCREF(pyCtxt->arg);
1523 PyTuple_SetItem(list, 1, libxml_charPtrWrap(str));
1524 PyTuple_SetItem(list, 2, libxml_intWrap(severity));
1525 PyTuple_SetItem(list, 3, Py_None);
1526 Py_INCREF(Py_None);
1527 result = PyEval_CallObject(pyCtxt->f, list);
1528 if (result == NULL)
1529 {
1530 /* TODO: manage for the exception to be propagated... */
1531 PyErr_Print();
Daniel Veillarde6227e02003-01-14 11:42:39 +00001532 }
Daniel Veillard417be3a2003-01-20 21:26:34 +00001533 Py_XDECREF(list);
1534 Py_XDECREF(result);
1535}
1536
1537static void
1538libxml_xmlParserCtxtErrorFuncHandler(void *ctx, const char *msg, ...)
1539{
1540 va_list ap;
1541
1542 va_start(ap, msg);
1543 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_ERROR,libxml_buildMessage(msg,ap));
1544 va_end(ap);
1545}
1546
1547static void
1548libxml_xmlParserCtxtWarningFuncHandler(void *ctx, const char *msg, ...)
1549{
1550 va_list ap;
1551
1552 va_start(ap, msg);
1553 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_WARNING,libxml_buildMessage(msg,ap));
1554 va_end(ap);
1555}
1556
1557static void
1558libxml_xmlParserCtxtValidityErrorFuncHandler(void *ctx, const char *msg, ...)
1559{
1560 va_list ap;
1561
1562 va_start(ap, msg);
1563 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_ERROR,libxml_buildMessage(msg,ap));
1564 va_end(ap);
1565}
1566
1567static void
1568libxml_xmlParserCtxtValidityWarningFuncHandler(void *ctx, const char *msg, ...)
1569{
1570 va_list ap;
1571
1572 va_start(ap, msg);
1573 libxml_xmlParserCtxtGenericErrorFuncHandler(ctx,XML_PARSER_SEVERITY_VALIDITY_WARNING,libxml_buildMessage(msg,ap));
1574 va_end(ap);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001575}
1576
Daniel Veillardc2664642003-07-29 20:44:53 +00001577static PyObject *
Daniel Veillard417be3a2003-01-20 21:26:34 +00001578libxml_xmlParserCtxtSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
Daniel Veillarde6227e02003-01-14 11:42:39 +00001579{
1580 PyObject *py_retval;
1581 xmlParserCtxtPtr ctxt;
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001582 xmlParserCtxtPyCtxtPtr pyCtxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001583 PyObject *pyobj_ctxt;
1584 PyObject *pyobj_f;
1585 PyObject *pyobj_arg;
1586
Daniel Veillard417be3a2003-01-20 21:26:34 +00001587 if (!PyArg_ParseTuple(args, (char *)"OOO:xmlParserCtxtSetErrorHandler",
Daniel Veillarde6227e02003-01-14 11:42:39 +00001588 &pyobj_ctxt, &pyobj_f, &pyobj_arg))
1589 return(NULL);
1590 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1591 if (ctxt->_private == NULL) {
Daniel Veillarde6227e02003-01-14 11:42:39 +00001592 pyCtxt = xmlMalloc(sizeof(xmlParserCtxtPyCtxt));
1593 if (pyCtxt == NULL) {
1594 py_retval = libxml_intWrap(-1);
1595 return(py_retval);
1596 }
1597 memset(pyCtxt,0,sizeof(xmlParserCtxtPyCtxt));
1598 ctxt->_private = pyCtxt;
1599 }
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001600 else {
Daniel Veillard417be3a2003-01-20 21:26:34 +00001601 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001602 }
Daniel Veillarde6227e02003-01-14 11:42:39 +00001603 /* TODO: check f is a function ! */
Daniel Veillard417be3a2003-01-20 21:26:34 +00001604 Py_XDECREF(pyCtxt->f);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001605 Py_XINCREF(pyobj_f);
Daniel Veillard417be3a2003-01-20 21:26:34 +00001606 pyCtxt->f = pyobj_f;
1607 Py_XDECREF(pyCtxt->arg);
Daniel Veillarde6227e02003-01-14 11:42:39 +00001608 Py_XINCREF(pyobj_arg);
Daniel Veillard417be3a2003-01-20 21:26:34 +00001609 pyCtxt->arg = pyobj_arg;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001610
Daniel Veillard417be3a2003-01-20 21:26:34 +00001611 if (pyobj_f != Py_None) {
1612 ctxt->sax->error = libxml_xmlParserCtxtErrorFuncHandler;
1613 ctxt->sax->warning = libxml_xmlParserCtxtWarningFuncHandler;
1614 ctxt->vctxt.error = libxml_xmlParserCtxtValidityErrorFuncHandler;
1615 ctxt->vctxt.warning = libxml_xmlParserCtxtValidityWarningFuncHandler;
1616 }
1617 else {
1618 ctxt->sax->error = xmlParserError;
1619 ctxt->vctxt.error = xmlParserValidityError;
1620 ctxt->sax->warning = xmlParserWarning;
1621 ctxt->vctxt.warning = xmlParserValidityWarning;
1622 }
Daniel Veillarde6227e02003-01-14 11:42:39 +00001623
1624 py_retval = libxml_intWrap(1);
1625 return(py_retval);
1626}
1627
Daniel Veillardc2664642003-07-29 20:44:53 +00001628static PyObject *
Daniel Veillard417be3a2003-01-20 21:26:34 +00001629libxml_xmlParserCtxtGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
Daniel Veillarde6227e02003-01-14 11:42:39 +00001630{
1631 PyObject *py_retval;
1632 xmlParserCtxtPtr ctxt;
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001633 xmlParserCtxtPyCtxtPtr pyCtxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001634 PyObject *pyobj_ctxt;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001635
Daniel Veillard417be3a2003-01-20 21:26:34 +00001636 if (!PyArg_ParseTuple(args, (char *)"O:xmlParserCtxtGetErrorHandler",
1637 &pyobj_ctxt))
Daniel Veillarde6227e02003-01-14 11:42:39 +00001638 return(NULL);
1639 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
Daniel Veillard417be3a2003-01-20 21:26:34 +00001640 py_retval = PyTuple_New(2);
1641 if (ctxt->_private != NULL) {
1642 pyCtxt = (xmlParserCtxtPyCtxtPtr)ctxt->_private;
1643
1644 PyTuple_SetItem(py_retval, 0, pyCtxt->f);
1645 Py_XINCREF(pyCtxt->f);
1646 PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
1647 Py_XINCREF(pyCtxt->arg);
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001648 }
1649 else {
Daniel Veillard417be3a2003-01-20 21:26:34 +00001650 /* no python error handler registered */
1651 PyTuple_SetItem(py_retval, 0, Py_None);
1652 Py_XINCREF(Py_None);
1653 PyTuple_SetItem(py_retval, 1, Py_None);
1654 Py_XINCREF(Py_None);
Daniel Veillarde4a07e72003-01-14 14:40:25 +00001655 }
Daniel Veillarde6227e02003-01-14 11:42:39 +00001656 return(py_retval);
1657}
1658
Daniel Veillardc2664642003-07-29 20:44:53 +00001659static PyObject *
Daniel Veillard417be3a2003-01-20 21:26:34 +00001660libxml_xmlFreeParserCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
1661 xmlParserCtxtPtr ctxt;
1662 PyObject *pyobj_ctxt;
1663 xmlParserCtxtPyCtxtPtr pyCtxt;
1664
1665 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeParserCtxt", &pyobj_ctxt))
1666 return(NULL);
1667 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1668
1669 if (ctxt != NULL) {
1670 pyCtxt = (xmlParserCtxtPyCtxtPtr)((xmlParserCtxtPtr)ctxt)->_private;
1671 if (pyCtxt) {
1672 Py_XDECREF(pyCtxt->f);
1673 Py_XDECREF(pyCtxt->arg);
1674 xmlFree(pyCtxt);
1675 }
1676 xmlFreeParserCtxt(ctxt);
1677 }
1678
1679 Py_INCREF(Py_None);
1680 return(Py_None);
1681}
1682
Daniel Veillarda7340c82002-02-01 17:56:45 +00001683/************************************************************************
1684 * *
Daniel Veillard26f70262003-01-16 22:45:08 +00001685 * Per xmlTextReader error handler *
1686 * *
1687 ************************************************************************/
1688
1689typedef struct
1690{
1691 PyObject *f;
1692 PyObject *arg;
1693} xmlTextReaderPyCtxt;
1694typedef xmlTextReaderPyCtxt *xmlTextReaderPyCtxtPtr;
1695
1696static void
1697libxml_xmlTextReaderErrorCallback(void *arg,
1698 const char *msg,
Daniel Veillard417be3a2003-01-20 21:26:34 +00001699 int severity,
1700 xmlTextReaderLocatorPtr locator)
Daniel Veillard26f70262003-01-16 22:45:08 +00001701{
1702 xmlTextReaderPyCtxt *pyCtxt = (xmlTextReaderPyCtxt *)arg;
1703 PyObject *list;
1704 PyObject *result;
1705
Daniel Veillard417be3a2003-01-20 21:26:34 +00001706 list = PyTuple_New(4);
Daniel Veillard26f70262003-01-16 22:45:08 +00001707 PyTuple_SetItem(list, 0, pyCtxt->arg);
1708 Py_XINCREF(pyCtxt->arg);
1709 PyTuple_SetItem(list, 1, libxml_charPtrConstWrap(msg));
Daniel Veillard417be3a2003-01-20 21:26:34 +00001710 PyTuple_SetItem(list, 2, libxml_intWrap(severity));
1711 PyTuple_SetItem(list, 3, libxml_xmlTextReaderLocatorPtrWrap(locator));
Daniel Veillard26f70262003-01-16 22:45:08 +00001712 result = PyEval_CallObject(pyCtxt->f, list);
1713 if (result == NULL)
1714 {
Daniel Veillard417be3a2003-01-20 21:26:34 +00001715 /* TODO: manage for the exception to be propagated... */
Daniel Veillard26f70262003-01-16 22:45:08 +00001716 PyErr_Print();
1717 }
1718 Py_XDECREF(list);
1719 Py_XDECREF(result);
1720}
1721
Daniel Veillardc2664642003-07-29 20:44:53 +00001722static PyObject *
Daniel Veillard26f70262003-01-16 22:45:08 +00001723libxml_xmlTextReaderSetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1724{
1725 xmlTextReaderPtr reader;
1726 xmlTextReaderPyCtxtPtr pyCtxt;
1727 xmlTextReaderErrorFunc f;
1728 void *arg;
1729 PyObject *pyobj_reader;
1730 PyObject *pyobj_f;
1731 PyObject *pyobj_arg;
1732 PyObject *py_retval;
1733
1734 if (!PyArg_ParseTuple(args, (char *)"OOO:xmlTextReaderSetErrorHandler", &pyobj_reader, &pyobj_f, &pyobj_arg))
1735 return(NULL);
1736 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
1737 /* clear previous error handler */
1738 xmlTextReaderGetErrorHandler(reader,&f,&arg);
1739 if (arg != NULL) {
Daniel Veillardc2664642003-07-29 20:44:53 +00001740 if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) {
Daniel Veillard26f70262003-01-16 22:45:08 +00001741 /* ok, it's our error handler! */
1742 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
1743 Py_XDECREF(pyCtxt->f);
1744 Py_XDECREF(pyCtxt->arg);
1745 xmlFree(pyCtxt);
1746 }
1747 else {
1748 /*
1749 * there already an arg, and it's not ours,
1750 * there is definitely something wrong going on here...
1751 * we don't know how to free it, so we bail out...
1752 */
1753 py_retval = libxml_intWrap(-1);
1754 return(py_retval);
1755 }
1756 }
1757 xmlTextReaderSetErrorHandler(reader,NULL,NULL);
1758 /* set new error handler */
1759 if (pyobj_f != Py_None)
1760 {
1761 pyCtxt = (xmlTextReaderPyCtxtPtr)xmlMalloc(sizeof(xmlTextReaderPyCtxt));
1762 if (pyCtxt == NULL) {
1763 py_retval = libxml_intWrap(-1);
1764 return(py_retval);
1765 }
1766 Py_XINCREF(pyobj_f);
1767 pyCtxt->f = pyobj_f;
1768 Py_XINCREF(pyobj_arg);
1769 pyCtxt->arg = pyobj_arg;
Daniel Veillardc2664642003-07-29 20:44:53 +00001770 xmlTextReaderSetErrorHandler(reader,
1771 (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback,
1772 pyCtxt);
Daniel Veillard26f70262003-01-16 22:45:08 +00001773 }
1774
1775 py_retval = libxml_intWrap(1);
1776 return(py_retval);
1777}
1778
Daniel Veillardc2664642003-07-29 20:44:53 +00001779static PyObject *
Daniel Veillard26f70262003-01-16 22:45:08 +00001780libxml_xmlTextReaderGetErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1781{
1782 xmlTextReaderPtr reader;
1783 xmlTextReaderPyCtxtPtr pyCtxt;
1784 xmlTextReaderErrorFunc f;
1785 void *arg;
1786 PyObject *pyobj_reader;
1787 PyObject *py_retval;
1788
1789 if (!PyArg_ParseTuple(args, (char *)"O:xmlTextReaderSetErrorHandler", &pyobj_reader))
1790 return(NULL);
1791 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
1792 xmlTextReaderGetErrorHandler(reader,&f,&arg);
1793 py_retval = PyTuple_New(2);
Daniel Veillardc2664642003-07-29 20:44:53 +00001794 if (f == (xmlTextReaderErrorFunc)libxml_xmlTextReaderErrorCallback) {
Daniel Veillard26f70262003-01-16 22:45:08 +00001795 /* ok, it's our error handler! */
1796 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
1797 PyTuple_SetItem(py_retval, 0, pyCtxt->f);
1798 Py_XINCREF(pyCtxt->f);
1799 PyTuple_SetItem(py_retval, 1, pyCtxt->arg);
1800 Py_XINCREF(pyCtxt->arg);
1801 }
1802 else
1803 {
1804 /* f is null or it's not our error handler */
1805 PyTuple_SetItem(py_retval, 0, Py_None);
1806 Py_XINCREF(Py_None);
1807 PyTuple_SetItem(py_retval, 1, Py_None);
1808 Py_XINCREF(Py_None);
1809 }
1810 return(py_retval);
1811}
1812
Daniel Veillardc2664642003-07-29 20:44:53 +00001813static PyObject *
Daniel Veillard26f70262003-01-16 22:45:08 +00001814libxml_xmlFreeTextReader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
1815 xmlTextReaderPtr reader;
1816 PyObject *pyobj_reader;
1817 xmlTextReaderPyCtxtPtr pyCtxt;
1818 xmlTextReaderErrorFunc f;
1819 void *arg;
1820
Daniel Veillard157fee02003-10-31 10:36:03 +00001821 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeTextReader", &pyobj_reader))
1822 return(NULL);
1823 if (!PyCObject_Check(pyobj_reader)) {
Daniel Veillardbb3ba322003-10-30 13:12:43 +00001824 Py_INCREF(Py_None);
1825 return(Py_None);
1826 }
Daniel Veillard26f70262003-01-16 22:45:08 +00001827 reader = (xmlTextReaderPtr) PyxmlTextReader_Get(pyobj_reader);
Daniel Veillardbb3ba322003-10-30 13:12:43 +00001828 if (reader == NULL) {
1829 Py_INCREF(Py_None);
1830 return(Py_None);
1831 }
Daniel Veillard26f70262003-01-16 22:45:08 +00001832
1833 xmlTextReaderGetErrorHandler(reader,&f,&arg);
1834 if (arg != NULL) {
Daniel Veillardc2664642003-07-29 20:44:53 +00001835 if (f == (xmlTextReaderErrorFunc) libxml_xmlTextReaderErrorCallback) {
Daniel Veillard26f70262003-01-16 22:45:08 +00001836 /* ok, it's our error handler! */
1837 pyCtxt = (xmlTextReaderPyCtxtPtr)arg;
1838 Py_XDECREF(pyCtxt->f);
1839 Py_XDECREF(pyCtxt->arg);
1840 xmlFree(pyCtxt);
1841 }
1842 /*
1843 * else, something wrong happened, because the error handler is
1844 * not owned by the python bindings...
1845 */
1846 }
1847
1848 xmlFreeTextReader(reader);
1849 Py_INCREF(Py_None);
1850 return(Py_None);
1851}
1852
1853/************************************************************************
1854 * *
Daniel Veillarda7340c82002-02-01 17:56:45 +00001855 * XPath extensions *
1856 * *
1857 ************************************************************************/
1858
Daniel Veillarda7340c82002-02-01 17:56:45 +00001859static void
Daniel Veillardd2379012002-03-15 22:24:56 +00001860libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs)
1861{
Daniel Veillarda7340c82002-02-01 17:56:45 +00001862 PyObject *list, *cur, *result;
1863 xmlXPathObjectPtr obj;
Daniel Veillard70cab352002-02-06 16:06:58 +00001864 xmlXPathContextPtr rctxt;
1865 PyObject *current_function = NULL;
1866 const xmlChar *name;
1867 const xmlChar *ns_uri;
Daniel Veillarda7340c82002-02-01 17:56:45 +00001868 int i;
1869
Daniel Veillard70cab352002-02-06 16:06:58 +00001870 if (ctxt == NULL)
Daniel Veillardd2379012002-03-15 22:24:56 +00001871 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00001872 rctxt = ctxt->context;
1873 if (rctxt == NULL)
Daniel Veillardd2379012002-03-15 22:24:56 +00001874 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00001875 name = rctxt->function;
1876 ns_uri = rctxt->functionURI;
Daniel Veillarda7340c82002-02-01 17:56:45 +00001877#ifdef DEBUG_XPATH
Daniel Veillardd2379012002-03-15 22:24:56 +00001878 printf("libxml_xmlXPathFuncCallback called name %s URI %s\n", name,
1879 ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001880#endif
1881
Daniel Veillard70cab352002-02-06 16:06:58 +00001882 /*
1883 * Find the function, it should be there it was there at lookup
1884 */
Daniel Veillardd2379012002-03-15 22:24:56 +00001885 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
1886 if ( /* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */
William M. Brack8e2cc6f2004-07-03 23:28:52 +00001887 (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
1888 (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
1889 current_function = (*libxml_xpathCallbacks)[i].function;
Daniel Veillardd2379012002-03-15 22:24:56 +00001890 }
Daniel Veillard70cab352002-02-06 16:06:58 +00001891 }
1892 if (current_function == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001893 printf
1894 ("libxml_xmlXPathFuncCallback: internal error %s not found !\n",
1895 name);
1896 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00001897 }
1898
Daniel Veillardc575b992002-02-08 13:28:40 +00001899 list = PyTuple_New(nargs + 1);
1900 PyTuple_SetItem(list, 0, libxml_xmlXPathParserContextPtrWrap(ctxt));
Daniel Veillard05349ab2004-01-25 20:01:35 +00001901 for (i = nargs - 1; i >= 0; i--) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001902 obj = valuePop(ctxt);
1903 cur = libxml_xmlXPathObjectPtrWrap(obj);
1904 PyTuple_SetItem(list, i + 1, cur);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001905 }
1906 result = PyEval_CallObject(current_function, list);
1907 Py_DECREF(list);
1908
1909 obj = libxml_xmlXPathObjectPtrConvert(result);
1910 valuePush(ctxt, obj);
1911}
1912
1913static xmlXPathFunction
Daniel Veillardd2379012002-03-15 22:24:56 +00001914libxml_xmlXPathFuncLookupFunc(void *ctxt, const xmlChar * name,
1915 const xmlChar * ns_uri)
1916{
Daniel Veillarda7340c82002-02-01 17:56:45 +00001917 int i;
Daniel Veillardd2379012002-03-15 22:24:56 +00001918
Daniel Veillarda7340c82002-02-01 17:56:45 +00001919#ifdef DEBUG_XPATH
1920 printf("libxml_xmlXPathFuncLookupFunc(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001921 ctxt, name, ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001922#endif
Daniel Veillard70cab352002-02-06 16:06:58 +00001923 /*
1924 * This is called once only. The address is then stored in the
1925 * XPath expression evaluation, the proper object to call can
1926 * then still be found using the execution context function
1927 * and functionURI fields.
1928 */
Daniel Veillardd2379012002-03-15 22:24:56 +00001929 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
William M. Brack8e2cc6f2004-07-03 23:28:52 +00001930 if ((ctxt == (*libxml_xpathCallbacks)[i].ctx) &&
1931 (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
1932 (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001933 return (libxml_xmlXPathFuncCallback);
1934 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00001935 }
Daniel Veillardd2379012002-03-15 22:24:56 +00001936 return (NULL);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001937}
1938
1939static void
Daniel Veillardd2379012002-03-15 22:24:56 +00001940libxml_xpathCallbacksInitialize(void)
1941{
Daniel Veillarda7340c82002-02-01 17:56:45 +00001942 int i;
1943
1944 if (libxml_xpathCallbacksInitialized != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00001945 return;
Daniel Veillarda7340c82002-02-01 17:56:45 +00001946
1947#ifdef DEBUG_XPATH
1948 printf("libxml_xpathCallbacksInitialized called\n");
1949#endif
William M. Brack8e2cc6f2004-07-03 23:28:52 +00001950 libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlMalloc(
1951 libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
Daniel Veillarda7340c82002-02-01 17:56:45 +00001952
William M. Brack8e2cc6f2004-07-03 23:28:52 +00001953 for (i = 0; i < libxml_xpathCallbacksAllocd; i++) {
1954 (*libxml_xpathCallbacks)[i].ctx = NULL;
1955 (*libxml_xpathCallbacks)[i].name = NULL;
1956 (*libxml_xpathCallbacks)[i].ns_uri = NULL;
1957 (*libxml_xpathCallbacks)[i].function = NULL;
Daniel Veillarda7340c82002-02-01 17:56:45 +00001958 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00001959 libxml_xpathCallbacksInitialized = 1;
1960}
1961
1962PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001963libxml_xmlRegisterXPathFunction(ATTRIBUTE_UNUSED PyObject * self,
1964 PyObject * args)
1965{
Daniel Veillarda7340c82002-02-01 17:56:45 +00001966 PyObject *py_retval;
1967 int c_retval = 0;
1968 xmlChar *name;
1969 xmlChar *ns_uri;
1970 xmlXPathContextPtr ctx;
1971 PyObject *pyobj_ctx;
1972 PyObject *pyobj_f;
1973 int i;
1974
Daniel Veillardd2379012002-03-15 22:24:56 +00001975 if (!PyArg_ParseTuple
1976 (args, (char *) "OszO:registerXPathFunction", &pyobj_ctx, &name,
1977 &ns_uri, &pyobj_f))
1978 return (NULL);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001979
1980 ctx = (xmlXPathContextPtr) PyxmlXPathContext_Get(pyobj_ctx);
1981 if (libxml_xpathCallbacksInitialized == 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00001982 libxml_xpathCallbacksInitialize();
Daniel Veillarda7340c82002-02-01 17:56:45 +00001983 xmlXPathRegisterFuncLookup(ctx, libxml_xmlXPathFuncLookupFunc, ctx);
1984
1985 if ((pyobj_ctx == NULL) || (name == NULL) || (pyobj_f == NULL)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001986 py_retval = libxml_intWrap(-1);
1987 return (py_retval);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001988 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00001989#ifdef DEBUG_XPATH
1990 printf("libxml_registerXPathFunction(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001991 ctx, name, ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001992#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001993 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
William M. Brack8e2cc6f2004-07-03 23:28:52 +00001994 if ((ctx == (*libxml_xpathCallbacks)[i].ctx) &&
1995 (xmlStrEqual(name, (*libxml_xpathCallbacks)[i].name)) &&
1996 (xmlStrEqual(ns_uri, (*libxml_xpathCallbacks)[i].ns_uri))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001997 Py_XINCREF(pyobj_f);
William M. Brack8e2cc6f2004-07-03 23:28:52 +00001998 Py_XDECREF((*libxml_xpathCallbacks)[i].function);
1999 (*libxml_xpathCallbacks)[i].function = pyobj_f;
Daniel Veillardd2379012002-03-15 22:24:56 +00002000 c_retval = 1;
2001 goto done;
2002 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00002003 }
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002004 if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksAllocd) {
2005 libxml_xpathCallbacksAllocd+=10;
2006 libxml_xpathCallbacks = (libxml_xpathCallbackArray*)xmlRealloc(
2007 libxml_xpathCallbacks,
2008 libxml_xpathCallbacksAllocd*sizeof(libxml_xpathCallback));
2009 }
2010 i = libxml_xpathCallbacksNb++;
2011 Py_XINCREF(pyobj_f);
2012 (*libxml_xpathCallbacks)[i].ctx = ctx;
2013 (*libxml_xpathCallbacks)[i].name = xmlStrdup(name);
2014 (*libxml_xpathCallbacks)[i].ns_uri = xmlStrdup(ns_uri);
2015 (*libxml_xpathCallbacks)[i].function = pyobj_f;
Daniel Veillardd2379012002-03-15 22:24:56 +00002016 c_retval = 1;
William M. Brack8e2cc6f2004-07-03 23:28:52 +00002017
Daniel Veillardd2379012002-03-15 22:24:56 +00002018 done:
Daniel Veillarda7340c82002-02-01 17:56:45 +00002019 py_retval = libxml_intWrap((int) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00002020 return (py_retval);
Daniel Veillarda7340c82002-02-01 17:56:45 +00002021}
2022
Daniel Veillard1971ee22002-01-31 20:29:19 +00002023/************************************************************************
2024 * *
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002025 * Global properties access *
2026 * *
2027 ************************************************************************/
2028static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002029libxml_name(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002030{
2031 PyObject *resultobj, *obj;
2032 xmlNodePtr cur;
2033 const xmlChar *res;
2034
Daniel Veillardd2379012002-03-15 22:24:56 +00002035 if (!PyArg_ParseTuple(args, (char *) "O:name", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002036 return NULL;
2037 cur = PyxmlNode_Get(obj);
2038
2039#ifdef DEBUG
2040 printf("libxml_name: cur = %p type %d\n", cur, cur->type);
2041#endif
2042
Daniel Veillardd2379012002-03-15 22:24:56 +00002043 switch (cur->type) {
2044 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002045#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002046 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002047#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002048 case XML_HTML_DOCUMENT_NODE:{
2049 xmlDocPtr doc = (xmlDocPtr) cur;
2050
2051 res = doc->URL;
2052 break;
2053 }
2054 case XML_ATTRIBUTE_NODE:{
2055 xmlAttrPtr attr = (xmlAttrPtr) cur;
2056
2057 res = attr->name;
2058 break;
2059 }
2060 case XML_NAMESPACE_DECL:{
2061 xmlNsPtr ns = (xmlNsPtr) cur;
2062
2063 res = ns->prefix;
2064 break;
2065 }
2066 default:
2067 res = cur->name;
2068 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002069 }
Daniel Veillard1971ee22002-01-31 20:29:19 +00002070 resultobj = libxml_constxmlCharPtrWrap(res);
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002071
2072 return resultobj;
2073}
2074
2075static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002076libxml_doc(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002077{
2078 PyObject *resultobj, *obj;
2079 xmlNodePtr cur;
2080 xmlDocPtr res;
2081
Daniel Veillardd2379012002-03-15 22:24:56 +00002082 if (!PyArg_ParseTuple(args, (char *) "O:doc", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002083 return NULL;
2084 cur = PyxmlNode_Get(obj);
2085
2086#ifdef DEBUG
2087 printf("libxml_doc: cur = %p\n", cur);
2088#endif
2089
Daniel Veillardd2379012002-03-15 22:24:56 +00002090 switch (cur->type) {
2091 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002092#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002093 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002094#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002095 case XML_HTML_DOCUMENT_NODE:
2096 res = NULL;
2097 break;
2098 case XML_ATTRIBUTE_NODE:{
2099 xmlAttrPtr attr = (xmlAttrPtr) cur;
2100
2101 res = attr->doc;
2102 break;
2103 }
2104 case XML_NAMESPACE_DECL:
2105 res = NULL;
2106 break;
2107 default:
2108 res = cur->doc;
2109 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002110 }
2111 resultobj = libxml_xmlDocPtrWrap(res);
2112 return resultobj;
2113}
2114
2115static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002116libxml_properties(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002117{
2118 PyObject *resultobj, *obj;
2119 xmlNodePtr cur = NULL;
2120 xmlAttrPtr res;
2121
Daniel Veillardd2379012002-03-15 22:24:56 +00002122 if (!PyArg_ParseTuple(args, (char *) "O:properties", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002123 return NULL;
2124 cur = PyxmlNode_Get(obj);
2125 if (cur->type == XML_ELEMENT_NODE)
Daniel Veillardd2379012002-03-15 22:24:56 +00002126 res = cur->properties;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002127 else
Daniel Veillardd2379012002-03-15 22:24:56 +00002128 res = NULL;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002129 resultobj = libxml_xmlAttrPtrWrap(res);
2130 return resultobj;
2131}
2132
2133static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002134libxml_next(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002135{
2136 PyObject *resultobj, *obj;
2137 xmlNodePtr cur;
2138 xmlNodePtr res;
2139
Daniel Veillardd2379012002-03-15 22:24:56 +00002140 if (!PyArg_ParseTuple(args, (char *) "O:next", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002141 return NULL;
2142 cur = PyxmlNode_Get(obj);
2143
2144#ifdef DEBUG
2145 printf("libxml_next: cur = %p\n", cur);
2146#endif
2147
Daniel Veillardd2379012002-03-15 22:24:56 +00002148 switch (cur->type) {
2149 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002150#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002151 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002152#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002153 case XML_HTML_DOCUMENT_NODE:
2154 res = NULL;
2155 break;
2156 case XML_ATTRIBUTE_NODE:{
2157 xmlAttrPtr attr = (xmlAttrPtr) cur;
2158
2159 res = (xmlNodePtr) attr->next;
2160 break;
2161 }
2162 case XML_NAMESPACE_DECL:{
2163 xmlNsPtr ns = (xmlNsPtr) cur;
2164
2165 res = (xmlNodePtr) ns->next;
2166 break;
2167 }
2168 default:
2169 res = cur->next;
2170 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002171
2172 }
2173 resultobj = libxml_xmlNodePtrWrap(res);
2174 return resultobj;
2175}
2176
2177static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002178libxml_prev(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002179{
2180 PyObject *resultobj, *obj;
2181 xmlNodePtr cur;
2182 xmlNodePtr res;
2183
Daniel Veillardd2379012002-03-15 22:24:56 +00002184 if (!PyArg_ParseTuple(args, (char *) "O:prev", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002185 return NULL;
2186 cur = PyxmlNode_Get(obj);
2187
2188#ifdef DEBUG
2189 printf("libxml_prev: cur = %p\n", cur);
2190#endif
2191
Daniel Veillardd2379012002-03-15 22:24:56 +00002192 switch (cur->type) {
2193 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002194#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002195 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002196#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002197 case XML_HTML_DOCUMENT_NODE:
2198 res = NULL;
2199 break;
2200 case XML_ATTRIBUTE_NODE:{
2201 xmlAttrPtr attr = (xmlAttrPtr) cur;
2202
Daniel Veillardfaa35ff2002-11-24 13:53:43 +00002203 res = (xmlNodePtr) attr->prev;
Daniel Veillardd2379012002-03-15 22:24:56 +00002204 }
2205 case XML_NAMESPACE_DECL:
2206 res = NULL;
2207 break;
2208 default:
Daniel Veillardfaa35ff2002-11-24 13:53:43 +00002209 res = cur->prev;
Daniel Veillardd2379012002-03-15 22:24:56 +00002210 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002211 }
2212 resultobj = libxml_xmlNodePtrWrap(res);
2213 return resultobj;
2214}
2215
2216static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002217libxml_children(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002218{
2219 PyObject *resultobj, *obj;
2220 xmlNodePtr cur;
2221 xmlNodePtr res;
2222
Daniel Veillardd2379012002-03-15 22:24:56 +00002223 if (!PyArg_ParseTuple(args, (char *) "O:children", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002224 return NULL;
2225 cur = PyxmlNode_Get(obj);
2226
2227#ifdef DEBUG
2228 printf("libxml_children: cur = %p\n", cur);
2229#endif
2230
Daniel Veillardd2379012002-03-15 22:24:56 +00002231 switch (cur->type) {
2232 case XML_ELEMENT_NODE:
2233 case XML_ENTITY_REF_NODE:
2234 case XML_ENTITY_NODE:
2235 case XML_PI_NODE:
2236 case XML_COMMENT_NODE:
2237 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002238#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002239 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002240#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002241 case XML_HTML_DOCUMENT_NODE:
2242 case XML_DTD_NODE:
2243 res = cur->children;
2244 break;
2245 case XML_ATTRIBUTE_NODE:{
2246 xmlAttrPtr attr = (xmlAttrPtr) cur;
2247
2248 res = attr->children;
2249 break;
2250 }
2251 default:
2252 res = NULL;
2253 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002254 }
2255 resultobj = libxml_xmlNodePtrWrap(res);
2256 return resultobj;
2257}
2258
2259static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002260libxml_last(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002261{
2262 PyObject *resultobj, *obj;
2263 xmlNodePtr cur;
2264 xmlNodePtr res;
2265
Daniel Veillardd2379012002-03-15 22:24:56 +00002266 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002267 return NULL;
2268 cur = PyxmlNode_Get(obj);
2269
2270#ifdef DEBUG
2271 printf("libxml_last: cur = %p\n", cur);
2272#endif
2273
Daniel Veillardd2379012002-03-15 22:24:56 +00002274 switch (cur->type) {
2275 case XML_ELEMENT_NODE:
2276 case XML_ENTITY_REF_NODE:
2277 case XML_ENTITY_NODE:
2278 case XML_PI_NODE:
2279 case XML_COMMENT_NODE:
2280 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002281#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002282 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002283#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002284 case XML_HTML_DOCUMENT_NODE:
2285 case XML_DTD_NODE:
2286 res = cur->last;
2287 break;
2288 case XML_ATTRIBUTE_NODE:{
2289 xmlAttrPtr attr = (xmlAttrPtr) cur;
2290
2291 res = attr->last;
2292 }
2293 default:
2294 res = NULL;
2295 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002296 }
2297 resultobj = libxml_xmlNodePtrWrap(res);
2298 return resultobj;
2299}
2300
2301static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002302libxml_parent(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002303{
2304 PyObject *resultobj, *obj;
2305 xmlNodePtr cur;
2306 xmlNodePtr res;
2307
Daniel Veillardd2379012002-03-15 22:24:56 +00002308 if (!PyArg_ParseTuple(args, (char *) "O:parent", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002309 return NULL;
2310 cur = PyxmlNode_Get(obj);
2311
2312#ifdef DEBUG
2313 printf("libxml_parent: cur = %p\n", cur);
2314#endif
2315
Daniel Veillardd2379012002-03-15 22:24:56 +00002316 switch (cur->type) {
2317 case XML_DOCUMENT_NODE:
2318 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002319#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002320 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002321#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00002322 res = NULL;
2323 break;
2324 case XML_ATTRIBUTE_NODE:{
2325 xmlAttrPtr attr = (xmlAttrPtr) cur;
2326
2327 res = attr->parent;
2328 }
2329 case XML_ENTITY_DECL:
2330 case XML_NAMESPACE_DECL:
2331 case XML_XINCLUDE_START:
2332 case XML_XINCLUDE_END:
2333 res = NULL;
2334 break;
2335 default:
2336 res = cur->parent;
2337 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002338 }
2339 resultobj = libxml_xmlNodePtrWrap(res);
2340 return resultobj;
2341}
2342
2343static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002344libxml_type(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002345{
2346 PyObject *resultobj, *obj;
2347 xmlNodePtr cur;
Daniel Veillardd2379012002-03-15 22:24:56 +00002348 const xmlChar *res = NULL;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002349
Daniel Veillardd2379012002-03-15 22:24:56 +00002350 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002351 return NULL;
2352 cur = PyxmlNode_Get(obj);
2353
2354#ifdef DEBUG
2355 printf("libxml_type: cur = %p\n", cur);
2356#endif
2357
Daniel Veillardd2379012002-03-15 22:24:56 +00002358 switch (cur->type) {
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002359 case XML_ELEMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002360 res = (const xmlChar *) "element";
2361 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002362 case XML_ATTRIBUTE_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002363 res = (const xmlChar *) "attribute";
2364 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002365 case XML_TEXT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002366 res = (const xmlChar *) "text";
2367 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002368 case XML_CDATA_SECTION_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002369 res = (const xmlChar *) "cdata";
2370 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002371 case XML_ENTITY_REF_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002372 res = (const xmlChar *) "entity_ref";
2373 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002374 case XML_ENTITY_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002375 res = (const xmlChar *) "entity";
2376 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002377 case XML_PI_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002378 res = (const xmlChar *) "pi";
2379 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002380 case XML_COMMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002381 res = (const xmlChar *) "comment";
2382 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002383 case XML_DOCUMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002384 res = (const xmlChar *) "document_xml";
2385 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002386 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002387 res = (const xmlChar *) "doctype";
2388 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002389 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002390 res = (const xmlChar *) "fragment";
2391 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002392 case XML_NOTATION_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002393 res = (const xmlChar *) "notation";
2394 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002395 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002396 res = (const xmlChar *) "document_html";
2397 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002398 case XML_DTD_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002399 res = (const xmlChar *) "dtd";
2400 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002401 case XML_ELEMENT_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002402 res = (const xmlChar *) "elem_decl";
2403 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002404 case XML_ATTRIBUTE_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002405 res = (const xmlChar *) "attribute_decl";
2406 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002407 case XML_ENTITY_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002408 res = (const xmlChar *) "entity_decl";
2409 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002410 case XML_NAMESPACE_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002411 res = (const xmlChar *) "namespace";
2412 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002413 case XML_XINCLUDE_START:
Daniel Veillardd2379012002-03-15 22:24:56 +00002414 res = (const xmlChar *) "xinclude_start";
2415 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002416 case XML_XINCLUDE_END:
Daniel Veillardd2379012002-03-15 22:24:56 +00002417 res = (const xmlChar *) "xinclude_end";
2418 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002419#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002420 case XML_DOCB_DOCUMENT_NODE:
2421 res = (const xmlChar *) "document_docbook";
2422 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002423#endif
2424 }
2425#ifdef DEBUG
2426 printf("libxml_type: cur = %p: %s\n", cur, res);
2427#endif
2428
Daniel Veillard1971ee22002-01-31 20:29:19 +00002429 resultobj = libxml_constxmlCharPtrWrap(res);
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002430 return resultobj;
2431}
2432
2433/************************************************************************
2434 * *
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002435 * Specific accessor functions *
2436 * *
2437 ************************************************************************/
2438PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002439libxml_xmlNodeGetNsDefs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2440{
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002441 PyObject *py_retval;
2442 xmlNsPtr c_retval;
2443 xmlNodePtr node;
2444 PyObject *pyobj_node;
2445
Daniel Veillardd2379012002-03-15 22:24:56 +00002446 if (!PyArg_ParseTuple
2447 (args, (char *) "O:xmlNodeGetNsDefs", &pyobj_node))
2448 return (NULL);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002449 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2450
2451 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002452 Py_INCREF(Py_None);
2453 return (Py_None);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002454 }
2455 c_retval = node->nsDef;
2456 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00002457 return (py_retval);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002458}
2459
2460PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002461libxml_xmlNodeGetNs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2462{
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002463 PyObject *py_retval;
2464 xmlNsPtr c_retval;
2465 xmlNodePtr node;
2466 PyObject *pyobj_node;
2467
Daniel Veillardd2379012002-03-15 22:24:56 +00002468 if (!PyArg_ParseTuple(args, (char *) "O:xmlNodeGetNs", &pyobj_node))
2469 return (NULL);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002470 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2471
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002472 if ((node == NULL) ||
2473 ((node->type != XML_ELEMENT_NODE) &&
2474 (node->type != XML_ATTRIBUTE_NODE))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002475 Py_INCREF(Py_None);
2476 return (Py_None);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002477 }
2478 c_retval = node->ns;
2479 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00002480 return (py_retval);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002481}
2482
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002483#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002484/************************************************************************
2485 * *
Daniel Veillard1e774382002-03-06 17:35:40 +00002486 * Serialization front-end *
2487 * *
2488 ************************************************************************/
2489
Daniel Veillardd2379012002-03-15 22:24:56 +00002490static PyObject *
2491libxml_serializeNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2492{
Daniel Veillard1e774382002-03-06 17:35:40 +00002493 PyObject *py_retval = NULL;
2494 xmlChar *c_retval;
2495 PyObject *pyobj_node;
2496 xmlNodePtr node;
2497 xmlDocPtr doc;
Daniel Veillardd2379012002-03-15 22:24:56 +00002498 const char *encoding;
Daniel Veillard1e774382002-03-06 17:35:40 +00002499 int format;
2500 int len;
2501
Daniel Veillardd2379012002-03-15 22:24:56 +00002502 if (!PyArg_ParseTuple(args, (char *) "Ozi:serializeNode", &pyobj_node,
2503 &encoding, &format))
2504 return (NULL);
Daniel Veillard1e774382002-03-06 17:35:40 +00002505 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2506
2507 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002508 Py_INCREF(Py_None);
2509 return (Py_None);
Daniel Veillard1e774382002-03-06 17:35:40 +00002510 }
2511 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002512 doc = (xmlDocPtr) node;
2513 xmlDocDumpFormatMemoryEnc(doc, &c_retval, &len,
2514 (const char *) encoding, format);
2515 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard656ce942004-04-30 23:11:45 +00002516#ifdef LIBXML_HTML_ENABLED
Daniel Veillard1e774382002-03-06 17:35:40 +00002517 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002518 xmlOutputBufferPtr buf;
2519 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00002520
Daniel Veillardd2379012002-03-15 22:24:56 +00002521 doc = (xmlDocPtr) node;
2522 if (encoding != NULL)
2523 htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
2524 encoding = (const char *) htmlGetMetaEncoding(doc);
Daniel Veillard1e774382002-03-06 17:35:40 +00002525
Daniel Veillardd2379012002-03-15 22:24:56 +00002526 if (encoding != NULL) {
2527 handler = xmlFindCharEncodingHandler(encoding);
2528 if (handler == NULL) {
2529 Py_INCREF(Py_None);
2530 return (Py_None);
2531 }
2532 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002533
Daniel Veillardd2379012002-03-15 22:24:56 +00002534 /*
2535 * Fallback to HTML or ASCII when the encoding is unspecified
2536 */
2537 if (handler == NULL)
2538 handler = xmlFindCharEncodingHandler("HTML");
2539 if (handler == NULL)
2540 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00002541
Daniel Veillardd2379012002-03-15 22:24:56 +00002542 buf = xmlAllocOutputBuffer(handler);
2543 if (buf == NULL) {
2544 Py_INCREF(Py_None);
2545 return (Py_None);
2546 }
2547 htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
2548 xmlOutputBufferFlush(buf);
2549 if (buf->conv != NULL) {
2550 len = buf->conv->use;
2551 c_retval = buf->conv->content;
2552 buf->conv->content = NULL;
2553 } else {
2554 len = buf->buffer->use;
2555 c_retval = buf->buffer->content;
2556 buf->buffer->content = NULL;
2557 }
2558 (void) xmlOutputBufferClose(buf);
2559 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard656ce942004-04-30 23:11:45 +00002560#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard1e774382002-03-06 17:35:40 +00002561 } else {
William M. Brack95af5942004-02-08 04:12:49 +00002562 if (node->type == XML_NAMESPACE_DECL)
2563 doc = NULL;
2564 else
2565 doc = node->doc;
Daniel Veillarda8c0adb2002-11-17 22:37:35 +00002566 if ((doc == NULL) || (doc->type == XML_DOCUMENT_NODE)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002567 xmlOutputBufferPtr buf;
2568 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00002569
Daniel Veillardd2379012002-03-15 22:24:56 +00002570 if (encoding != NULL) {
2571 handler = xmlFindCharEncodingHandler(encoding);
2572 if (handler == NULL) {
2573 Py_INCREF(Py_None);
2574 return (Py_None);
2575 }
2576 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002577
Daniel Veillardd2379012002-03-15 22:24:56 +00002578 buf = xmlAllocOutputBuffer(handler);
2579 if (buf == NULL) {
2580 Py_INCREF(Py_None);
2581 return (Py_None);
2582 }
2583 xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
2584 xmlOutputBufferFlush(buf);
2585 if (buf->conv != NULL) {
2586 len = buf->conv->use;
2587 c_retval = buf->conv->content;
2588 buf->conv->content = NULL;
2589 } else {
2590 len = buf->buffer->use;
2591 c_retval = buf->buffer->content;
2592 buf->buffer->content = NULL;
2593 }
2594 (void) xmlOutputBufferClose(buf);
2595 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard656ce942004-04-30 23:11:45 +00002596#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002597 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2598 xmlOutputBufferPtr buf;
2599 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00002600
Daniel Veillardd2379012002-03-15 22:24:56 +00002601 if (encoding != NULL)
2602 htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
2603 encoding = (const char *) htmlGetMetaEncoding(doc);
2604 if (encoding != NULL) {
2605 handler = xmlFindCharEncodingHandler(encoding);
2606 if (handler == NULL) {
2607 Py_INCREF(Py_None);
2608 return (Py_None);
2609 }
2610 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002611
Daniel Veillardd2379012002-03-15 22:24:56 +00002612 /*
2613 * Fallback to HTML or ASCII when the encoding is unspecified
2614 */
2615 if (handler == NULL)
2616 handler = xmlFindCharEncodingHandler("HTML");
2617 if (handler == NULL)
2618 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00002619
Daniel Veillardd2379012002-03-15 22:24:56 +00002620 buf = xmlAllocOutputBuffer(handler);
2621 if (buf == NULL) {
2622 Py_INCREF(Py_None);
2623 return (Py_None);
2624 }
2625 htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
2626 xmlOutputBufferFlush(buf);
2627 if (buf->conv != NULL) {
2628 len = buf->conv->use;
2629 c_retval = buf->conv->content;
2630 buf->conv->content = NULL;
2631 } else {
2632 len = buf->buffer->use;
2633 c_retval = buf->buffer->content;
2634 buf->buffer->content = NULL;
2635 }
2636 (void) xmlOutputBufferClose(buf);
2637 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard656ce942004-04-30 23:11:45 +00002638#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2379012002-03-15 22:24:56 +00002639 } else {
2640 Py_INCREF(Py_None);
2641 return (Py_None);
2642 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002643 }
Daniel Veillardd2379012002-03-15 22:24:56 +00002644 return (py_retval);
Daniel Veillard1e774382002-03-06 17:35:40 +00002645}
2646
Daniel Veillardd2379012002-03-15 22:24:56 +00002647static PyObject *
2648libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2649{
Daniel Veillard1e774382002-03-06 17:35:40 +00002650 PyObject *py_file = NULL;
2651 FILE *output;
2652 PyObject *pyobj_node;
2653 xmlNodePtr node;
2654 xmlDocPtr doc;
Daniel Veillardd2379012002-03-15 22:24:56 +00002655 const char *encoding;
Daniel Veillard1e774382002-03-06 17:35:40 +00002656 int format;
2657 int len;
2658 xmlOutputBufferPtr buf;
2659 xmlCharEncodingHandlerPtr handler = NULL;
2660
Daniel Veillardd2379012002-03-15 22:24:56 +00002661 if (!PyArg_ParseTuple(args, (char *) "OOzi:serializeNode", &pyobj_node,
2662 &py_file, &encoding, &format))
2663 return (NULL);
Daniel Veillard1e774382002-03-06 17:35:40 +00002664 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2665
2666 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002667 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002668 }
2669 if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002670 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002671 }
2672 output = PyFile_AsFile(py_file);
2673 if (output == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002674 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002675 }
2676
2677 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002678 doc = (xmlDocPtr) node;
Daniel Veillard1e774382002-03-06 17:35:40 +00002679 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002680 doc = (xmlDocPtr) node;
Daniel Veillard1e774382002-03-06 17:35:40 +00002681 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00002682 doc = node->doc;
Daniel Veillard1e774382002-03-06 17:35:40 +00002683 }
Daniel Veillard656ce942004-04-30 23:11:45 +00002684#ifdef LIBXML_HTML_ENABLED
Daniel Veillard1e774382002-03-06 17:35:40 +00002685 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002686 if (encoding == NULL)
2687 encoding = (const char *) htmlGetMetaEncoding(doc);
Daniel Veillard1e774382002-03-06 17:35:40 +00002688 }
Daniel Veillard656ce942004-04-30 23:11:45 +00002689#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard1e774382002-03-06 17:35:40 +00002690 if (encoding != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002691 handler = xmlFindCharEncodingHandler(encoding);
2692 if (handler == NULL) {
2693 return (PyInt_FromLong((long) -1));
2694 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002695 }
2696 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002697 if (handler == NULL)
2698 handler = xmlFindCharEncodingHandler("HTML");
2699 if (handler == NULL)
2700 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00002701 }
2702
2703 buf = xmlOutputBufferCreateFile(output, handler);
2704 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002705 len = xmlSaveFormatFileTo(buf, doc, encoding, format);
Daniel Veillard656ce942004-04-30 23:11:45 +00002706#ifdef LIBXML_HTML_ENABLED
Daniel Veillard1e774382002-03-06 17:35:40 +00002707 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002708 htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
2709 len = xmlOutputBufferClose(buf);
Daniel Veillard1e774382002-03-06 17:35:40 +00002710 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002711 htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
2712 len = xmlOutputBufferClose(buf);
Daniel Veillard656ce942004-04-30 23:11:45 +00002713#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard1e774382002-03-06 17:35:40 +00002714 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00002715 xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
2716 len = xmlOutputBufferClose(buf);
Daniel Veillard1e774382002-03-06 17:35:40 +00002717 }
Daniel Veillardd2379012002-03-15 22:24:56 +00002718 return (PyInt_FromLong((long) len));
Daniel Veillard1e774382002-03-06 17:35:40 +00002719}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002720#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard1e774382002-03-06 17:35:40 +00002721
2722/************************************************************************
2723 * *
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002724 * Extra stuff *
2725 * *
2726 ************************************************************************/
2727PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002728libxml_xmlNewNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2729{
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002730 PyObject *py_retval;
Daniel Veillardd2379012002-03-15 22:24:56 +00002731 xmlChar *name;
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002732 xmlNodePtr node;
2733
Daniel Veillardd2379012002-03-15 22:24:56 +00002734 if (!PyArg_ParseTuple(args, (char *) "s:xmlNewNode", &name))
2735 return (NULL);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002736 node = (xmlNodePtr) xmlNewNode(NULL, name);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00002737#ifdef DEBUG
Daniel Veillardd2379012002-03-15 22:24:56 +00002738 printf("NewNode: %s : %p\n", name, (void *) node);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00002739#endif
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002740
2741 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002742 Py_INCREF(Py_None);
2743 return (Py_None);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002744 }
2745 py_retval = libxml_xmlNodePtrWrap(node);
Daniel Veillardd2379012002-03-15 22:24:56 +00002746 return (py_retval);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002747}
2748
Daniel Veillard54396242003-04-23 07:36:50 +00002749
2750/************************************************************************
2751 * *
2752 * Local Catalog stuff *
2753 * *
2754 ************************************************************************/
2755static PyObject *
2756libxml_addLocalCatalog(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2757{
2758 xmlChar *URL;
2759 xmlParserCtxtPtr ctxt;
2760 PyObject *pyobj_ctxt;
2761
2762 if (!PyArg_ParseTuple(args, (char *)"Os:addLocalCatalog", &pyobj_ctxt, &URL))
2763 return(NULL);
2764
2765 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
2766
2767 if (URL != NULL) {
2768 ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL);
2769 }
2770
2771#ifdef DEBUG
2772 printf("LocalCatalog: %s\n", URL);
2773#endif
2774
2775 Py_INCREF(Py_None);
2776 return (Py_None);
2777}
2778
Daniel Veillardc2664642003-07-29 20:44:53 +00002779#ifdef LIBXML_SCHEMAS_ENABLED
2780
2781/************************************************************************
2782 * *
2783 * RelaxNG error handler registration *
2784 * *
2785 ************************************************************************/
2786
2787typedef struct
2788{
2789 PyObject *warn;
2790 PyObject *error;
2791 PyObject *arg;
2792} xmlRelaxNGValidCtxtPyCtxt;
2793typedef xmlRelaxNGValidCtxtPyCtxt *xmlRelaxNGValidCtxtPyCtxtPtr;
2794
2795static void
2796libxml_xmlRelaxNGValidityGenericErrorFuncHandler(void *ctx, char *str)
2797{
2798 PyObject *list;
2799 PyObject *result;
2800 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
2801
2802#ifdef DEBUG_ERROR
2803 printf("libxml_xmlRelaxNGValidityGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
2804#endif
2805
2806 pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx;
2807
2808 list = PyTuple_New(2);
2809 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
2810 PyTuple_SetItem(list, 1, pyCtxt->arg);
2811 Py_XINCREF(pyCtxt->arg);
2812 result = PyEval_CallObject(pyCtxt->error, list);
2813 if (result == NULL)
2814 {
2815 /* TODO: manage for the exception to be propagated... */
2816 PyErr_Print();
2817 }
2818 Py_XDECREF(list);
2819 Py_XDECREF(result);
2820}
2821
2822static void
2823libxml_xmlRelaxNGValidityGenericWarningFuncHandler(void *ctx, char *str)
2824{
2825 PyObject *list;
2826 PyObject *result;
2827 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
2828
2829#ifdef DEBUG_ERROR
2830 printf("libxml_xmlRelaxNGValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str);
2831#endif
2832
2833 pyCtxt = (xmlRelaxNGValidCtxtPyCtxtPtr)ctx;
2834
2835 list = PyTuple_New(2);
2836 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
2837 PyTuple_SetItem(list, 1, pyCtxt->arg);
2838 Py_XINCREF(pyCtxt->arg);
2839 result = PyEval_CallObject(pyCtxt->warn, list);
2840 if (result == NULL)
2841 {
2842 /* TODO: manage for the exception to be propagated... */
2843 PyErr_Print();
2844 }
2845 Py_XDECREF(list);
2846 Py_XDECREF(result);
2847}
2848
2849static void
2850libxml_xmlRelaxNGValidityErrorFunc(void *ctx, const char *msg, ...)
2851{
2852 va_list ap;
2853
2854 va_start(ap, msg);
2855 libxml_xmlRelaxNGValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap));
2856 va_end(ap);
2857}
2858
2859static void
2860libxml_xmlRelaxNGValidityWarningFunc(void *ctx, const char *msg, ...)
2861{
2862 va_list ap;
2863
2864 va_start(ap, msg);
2865 libxml_xmlRelaxNGValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap));
2866 va_end(ap);
2867}
2868
2869static PyObject *
2870libxml_xmlRelaxNGSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2871{
2872 PyObject *py_retval;
2873 PyObject *pyobj_error;
2874 PyObject *pyobj_warn;
2875 PyObject *pyobj_ctx;
2876 PyObject *pyobj_arg = Py_None;
2877 xmlRelaxNGValidCtxtPtr ctxt;
2878 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
2879
2880 if (!PyArg_ParseTuple
2881 (args, (char *) "OOO|O:xmlRelaxNGSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
2882 return (NULL);
2883
2884#ifdef DEBUG_ERROR
2885 printf("libxml_xmlRelaxNGSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
2886#endif
2887
2888 ctxt = PyrelaxNgValidCtxt_Get(pyobj_ctx);
William M. Brackc1939562003-08-05 15:52:22 +00002889 if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1)
Daniel Veillardc2664642003-07-29 20:44:53 +00002890 {
2891 py_retval = libxml_intWrap(-1);
2892 return(py_retval);
2893 }
2894
2895 if (pyCtxt == NULL)
2896 {
2897 /* first time to set the error handlers */
2898 pyCtxt = xmlMalloc(sizeof(xmlRelaxNGValidCtxtPyCtxt));
2899 if (pyCtxt == NULL) {
2900 py_retval = libxml_intWrap(-1);
2901 return(py_retval);
2902 }
2903 memset(pyCtxt, 0, sizeof(xmlRelaxNGValidCtxtPyCtxt));
2904 }
2905
2906 /* TODO: check warn and error is a function ! */
2907 Py_XDECREF(pyCtxt->error);
2908 Py_XINCREF(pyobj_error);
2909 pyCtxt->error = pyobj_error;
2910
2911 Py_XDECREF(pyCtxt->warn);
2912 Py_XINCREF(pyobj_warn);
2913 pyCtxt->warn = pyobj_warn;
2914
2915 Py_XDECREF(pyCtxt->arg);
2916 Py_XINCREF(pyobj_arg);
2917 pyCtxt->arg = pyobj_arg;
2918
2919 xmlRelaxNGSetValidErrors(ctxt, &libxml_xmlRelaxNGValidityErrorFunc, &libxml_xmlRelaxNGValidityWarningFunc, pyCtxt);
2920
2921 py_retval = libxml_intWrap(1);
2922 return (py_retval);
2923}
2924
2925static PyObject *
2926libxml_xmlRelaxNGFreeValidCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
2927 xmlRelaxNGValidCtxtPtr ctxt;
2928 xmlRelaxNGValidCtxtPyCtxtPtr pyCtxt;
2929 PyObject *pyobj_ctxt;
2930
2931 if (!PyArg_ParseTuple(args, (char *)"O:xmlRelaxNGFreeValidCtxt", &pyobj_ctxt))
2932 return(NULL);
2933 ctxt = (xmlRelaxNGValidCtxtPtr) PyrelaxNgValidCtxt_Get(pyobj_ctxt);
2934
William M. Brackc1939562003-08-05 15:52:22 +00002935 if (xmlRelaxNGGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0)
Daniel Veillardc2664642003-07-29 20:44:53 +00002936 {
2937 if (pyCtxt != NULL)
2938 {
2939 Py_XDECREF(pyCtxt->error);
2940 Py_XDECREF(pyCtxt->warn);
2941 Py_XDECREF(pyCtxt->arg);
2942 xmlFree(pyCtxt);
2943 }
2944 }
2945
2946 xmlRelaxNGFreeValidCtxt(ctxt);
2947 Py_INCREF(Py_None);
2948 return(Py_None);
2949}
2950
Daniel Veillard259f0df2004-08-18 09:13:18 +00002951typedef struct
2952{
2953 PyObject *warn;
2954 PyObject *error;
2955 PyObject *arg;
2956} xmlSchemaValidCtxtPyCtxt;
2957typedef xmlSchemaValidCtxtPyCtxt *xmlSchemaValidCtxtPyCtxtPtr;
2958
2959static void
2960libxml_xmlSchemaValidityGenericErrorFuncHandler(void *ctx, char *str)
2961{
2962 PyObject *list;
2963 PyObject *result;
2964 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
2965
2966#ifdef DEBUG_ERROR
2967 printf("libxml_xmlSchemaValiditiyGenericErrorFuncHandler(%p, %s, ...) called\n", ctx, str);
2968#endif
2969
2970 pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx;
2971
2972 list = PyTuple_New(2);
2973 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
2974 PyTuple_SetItem(list, 1, pyCtxt->arg);
2975 Py_XINCREF(pyCtxt->arg);
2976 result = PyEval_CallObject(pyCtxt->error, list);
2977 if (result == NULL)
2978 {
2979 /* TODO: manage for the exception to be propagated... */
2980 PyErr_Print();
2981 }
2982 Py_XDECREF(list);
2983 Py_XDECREF(result);
2984}
2985
2986static void
2987libxml_xmlSchemaValidityGenericWarningFuncHandler(void *ctx, char *str)
2988{
2989 PyObject *list;
2990 PyObject *result;
2991 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
2992
2993#ifdef DEBUG_ERROR
2994 printf("libxml_xmlSchemaValidityGenericWarningFuncHandler(%p, %s, ...) called\n", ctx, str);
2995#endif
2996
2997 pyCtxt = (xmlSchemaValidCtxtPyCtxtPtr) ctx;
2998
2999 list = PyTuple_New(2);
3000 PyTuple_SetItem(list, 0, libxml_charPtrWrap(str));
3001 PyTuple_SetItem(list, 1, pyCtxt->arg);
3002 Py_XINCREF(pyCtxt->arg);
3003 result = PyEval_CallObject(pyCtxt->warn, list);
3004 if (result == NULL)
3005 {
3006 /* TODO: manage for the exception to be propagated... */
3007 PyErr_Print();
3008 }
3009 Py_XDECREF(list);
3010 Py_XDECREF(result);
3011}
3012
3013static void
3014libxml_xmlSchemaValidityErrorFunc(void *ctx, const char *msg, ...)
3015{
3016 va_list ap;
3017
3018 va_start(ap, msg);
3019 libxml_xmlSchemaValidityGenericErrorFuncHandler(ctx, libxml_buildMessage(msg, ap));
3020 va_end(ap);
3021}
3022
3023static void
3024libxml_xmlSchemaValidityWarningFunc(void *ctx, const char *msg, ...)
3025{
3026 va_list ap;
3027
3028 va_start(ap, msg);
3029 libxml_xmlSchemaValidityGenericWarningFuncHandler(ctx, libxml_buildMessage(msg, ap));
3030 va_end(ap);
3031}
3032
Daniel Veillard6ebf3c42004-08-22 13:11:39 +00003033PyObject *
Daniel Veillard259f0df2004-08-18 09:13:18 +00003034libxml_xmlSchemaSetValidErrors(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3035{
3036 PyObject *py_retval;
3037 PyObject *pyobj_error;
3038 PyObject *pyobj_warn;
3039 PyObject *pyobj_ctx;
3040 PyObject *pyobj_arg = Py_None;
3041 xmlSchemaValidCtxtPtr ctxt;
3042 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3043
3044 if (!PyArg_ParseTuple
3045 (args, (char *) "OOO|O:xmlSchemaSetValidErrors", &pyobj_ctx, &pyobj_error, &pyobj_warn, &pyobj_arg))
3046 return (NULL);
3047
3048#ifdef DEBUG_ERROR
3049 printf("libxml_xmlSchemaSetValidErrors(%p, %p, %p) called\n", pyobj_ctx, pyobj_error, pyobj_warn);
3050#endif
3051
3052 ctxt = PySchemaValidCtxt_Get(pyobj_ctx);
3053 if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == -1)
3054 {
3055 py_retval = libxml_intWrap(-1);
3056 return(py_retval);
3057 }
3058
3059 if (pyCtxt == NULL)
3060 {
3061 /* first time to set the error handlers */
3062 pyCtxt = xmlMalloc(sizeof(xmlSchemaValidCtxtPyCtxt));
3063 if (pyCtxt == NULL) {
3064 py_retval = libxml_intWrap(-1);
3065 return(py_retval);
3066 }
3067 memset(pyCtxt, 0, sizeof(xmlSchemaValidCtxtPyCtxt));
3068 }
3069
3070 /* TODO: check warn and error is a function ! */
3071 Py_XDECREF(pyCtxt->error);
3072 Py_XINCREF(pyobj_error);
3073 pyCtxt->error = pyobj_error;
3074
3075 Py_XDECREF(pyCtxt->warn);
3076 Py_XINCREF(pyobj_warn);
3077 pyCtxt->warn = pyobj_warn;
3078
3079 Py_XDECREF(pyCtxt->arg);
3080 Py_XINCREF(pyobj_arg);
3081 pyCtxt->arg = pyobj_arg;
3082
3083 xmlSchemaSetValidErrors(ctxt, &libxml_xmlSchemaValidityErrorFunc, &libxml_xmlSchemaValidityWarningFunc, pyCtxt);
3084
3085 py_retval = libxml_intWrap(1);
3086 return(py_retval);
3087}
3088
Daniel Veillard36505562004-08-22 14:02:09 +00003089#if 0
Daniel Veillard6ebf3c42004-08-22 13:11:39 +00003090PyObject *
Daniel Veillard259f0df2004-08-18 09:13:18 +00003091libxml_xmlSchemaFreeValidCtxt(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
3092{
3093 xmlSchemaValidCtxtPtr ctxt;
3094 xmlSchemaValidCtxtPyCtxtPtr pyCtxt;
3095 PyObject *pyobj_ctxt;
3096
3097 if (!PyArg_ParseTuple(args, (char *)"O:xmlSchemaFreeValidCtxt", &pyobj_ctxt))
3098 return(NULL);
3099 ctxt = (xmlSchemaValidCtxtPtr) PySchemaValidCtxt_Get(pyobj_ctxt);
3100
3101 if (xmlSchemaGetValidErrors(ctxt, NULL, NULL, (void **) &pyCtxt) == 0)
3102 {
3103 if (pyCtxt != NULL)
3104 {
3105 Py_XDECREF(pyCtxt->error);
3106 Py_XDECREF(pyCtxt->warn);
3107 Py_XDECREF(pyCtxt->arg);
3108 xmlFree(pyCtxt);
3109 }
3110 }
3111
3112 xmlSchemaFreeValidCtxt(ctxt);
3113 Py_INCREF(Py_None);
3114 return(Py_None);
3115}
Daniel Veillard36505562004-08-22 14:02:09 +00003116#endif
Daniel Veillard259f0df2004-08-18 09:13:18 +00003117
Daniel Veillardc2664642003-07-29 20:44:53 +00003118#endif
3119
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003120#ifdef LIBXML_C14N_ENABLED
3121#ifdef LIBXML_OUTPUT_ENABLED
3122
3123/************************************************************************
3124 * *
3125 * XML Canonicalization c14n *
3126 * *
3127 ************************************************************************/
3128
3129static int
3130PyxmlNodeSet_Convert(PyObject *py_nodeset, xmlNodeSetPtr *result)
3131{
3132 xmlNodeSetPtr nodeSet;
3133 int is_tuple = 0;
3134
3135 if (PyTuple_Check(py_nodeset))
3136 is_tuple = 1;
3137 else if (PyList_Check(py_nodeset))
3138 is_tuple = 0;
3139 else if (py_nodeset == Py_None) {
3140 *result = NULL;
3141 return 0;
3142 }
3143 else {
3144 PyErr_SetString(PyExc_TypeError,
3145 "must be a tuple or list of nodes.");
3146 return -1;
3147 }
3148
3149 nodeSet = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet));
3150 if (nodeSet == NULL) {
3151 PyErr_SetString(PyExc_MemoryError, "");
3152 return -1;
3153 }
3154
3155 nodeSet->nodeNr = 0;
3156 nodeSet->nodeMax = (is_tuple
3157 ? PyTuple_GET_SIZE(py_nodeset)
3158 : PyList_GET_SIZE(py_nodeset));
3159 nodeSet->nodeTab
3160 = (xmlNodePtr *) xmlMalloc (nodeSet->nodeMax
3161 * sizeof(xmlNodePtr));
3162 if (nodeSet->nodeTab == NULL) {
3163 xmlFree(nodeSet);
3164 PyErr_SetString(PyExc_MemoryError, "");
3165 return -1;
3166 }
3167 memset(nodeSet->nodeTab, 0 ,
3168 nodeSet->nodeMax * sizeof(xmlNodePtr));
3169
3170 {
3171 int idx;
3172 for (idx=0; idx < nodeSet->nodeMax; ++idx) {
3173 xmlNodePtr pynode =
3174 PyxmlNode_Get (is_tuple
3175 ? PyTuple_GET_ITEM(py_nodeset, idx)
3176 : PyList_GET_ITEM(py_nodeset, idx));
3177 if (pynode)
3178 nodeSet->nodeTab[nodeSet->nodeNr++] = pynode;
3179 }
3180 }
3181 *result = nodeSet;
3182 return 0;
3183}
3184
3185static int
3186PystringSet_Convert(PyObject *py_strings, xmlChar *** result)
3187{
3188 /* NOTE: the array should be freed, but the strings are shared
3189 with the python strings and so must not be freed. */
3190
3191 xmlChar ** strings;
3192 int is_tuple = 0;
3193 int count;
3194 int init_index = 0;
3195
3196 if (PyTuple_Check(py_strings))
3197 is_tuple = 1;
3198 else if (PyList_Check(py_strings))
3199 is_tuple = 0;
3200 else if (py_strings == Py_None) {
3201 *result = NULL;
3202 return 0;
3203 }
3204 else {
3205 PyErr_SetString(PyExc_TypeError,
3206 "must be a tuple or list of strings.");
3207 return -1;
3208 }
3209
3210 count = (is_tuple
3211 ? PyTuple_GET_SIZE(py_strings)
3212 : PyList_GET_SIZE(py_strings));
3213
3214 strings = (xmlChar **) xmlMalloc(sizeof(xmlChar *) * count);
3215
3216 if (strings == NULL) {
3217 PyErr_SetString(PyExc_MemoryError, "");
3218 return -1;
3219 }
3220
3221 memset(strings, 0 , sizeof(xmlChar *) * count);
3222
3223 {
3224 int idx;
3225 for (idx=0; idx < count; ++idx) {
3226 char* s = PyString_AsString
3227 (is_tuple
3228 ? PyTuple_GET_ITEM(py_strings, idx)
3229 : PyList_GET_ITEM(py_strings, idx));
3230 if (s)
3231 strings[init_index++] = (xmlChar *)s;
3232 else {
3233 xmlFree(strings);
3234 PyErr_SetString(PyExc_TypeError,
3235 "must be a tuple or list of strings.");
3236 return -1;
3237 }
3238 }
3239 }
3240
3241 *result = strings;
3242 return 0;
3243}
3244
3245static PyObject *
3246libxml_C14NDocDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
3247 PyObject * args)
3248{
3249 PyObject *py_retval = NULL;
3250
3251 PyObject *pyobj_doc;
3252 PyObject *pyobj_nodes;
3253 int exclusive;
3254 PyObject *pyobj_prefixes;
3255 int with_comments;
3256
3257 xmlDocPtr doc;
3258 xmlNodeSetPtr nodes;
3259 xmlChar **prefixes = NULL;
3260 xmlChar *doc_txt;
3261
3262 int result;
3263
3264 if (!PyArg_ParseTuple(args, (char *) "OOiOi:C14NDocDumpMemory",
3265 &pyobj_doc,
3266 &pyobj_nodes,
3267 &exclusive,
3268 &pyobj_prefixes,
3269 &with_comments))
3270 return (NULL);
3271
3272 doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
3273 if (!doc) {
3274 PyErr_SetString(PyExc_TypeError, "bad document.");
3275 return NULL;
3276 }
3277
3278 result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes);
3279 if (result < 0) return NULL;
3280
3281 if (exclusive) {
3282 result = PystringSet_Convert(pyobj_prefixes, &prefixes);
3283 if (result < 0) {
3284 if (nodes) {
3285 xmlFree(nodes->nodeTab);
3286 xmlFree(nodes);
3287 }
3288 return NULL;
3289 }
3290 }
3291
3292 result = xmlC14NDocDumpMemory(doc,
3293 nodes,
3294 exclusive,
3295 prefixes,
3296 with_comments,
3297 &doc_txt);
3298
3299 if (nodes) {
3300 xmlFree(nodes->nodeTab);
3301 xmlFree(nodes);
3302 }
3303 if (prefixes) {
3304 xmlChar ** idx = prefixes;
3305 while (*idx) xmlFree(*(idx++));
3306 xmlFree(prefixes);
3307 }
3308
3309 if (result < 0) {
3310 PyErr_SetString(PyExc_Exception,
3311 "libxml2 xmlC14NDocDumpMemory failure.");
3312 return NULL;
3313 }
3314 else {
3315 py_retval = PyString_FromStringAndSize((const char *) doc_txt,
3316 result);
3317 xmlFree(doc_txt);
3318 return py_retval;
3319 }
3320}
3321
3322static PyObject *
3323libxml_C14NDocSaveTo(ATTRIBUTE_UNUSED PyObject * self,
3324 PyObject * args)
3325{
3326 PyObject *pyobj_doc;
3327 PyObject *py_file;
3328 PyObject *pyobj_nodes;
3329 int exclusive;
3330 PyObject *pyobj_prefixes;
3331 int with_comments;
3332
3333 xmlDocPtr doc;
3334 xmlNodeSetPtr nodes;
3335 xmlChar **prefixes = NULL;
3336 FILE * output;
3337 xmlOutputBufferPtr buf;
3338
3339 int result;
3340 int len;
3341
3342 if (!PyArg_ParseTuple(args, (char *) "OOiOiO:C14NDocSaveTo",
3343 &pyobj_doc,
3344 &pyobj_nodes,
3345 &exclusive,
3346 &pyobj_prefixes,
3347 &with_comments,
3348 &py_file))
3349 return (NULL);
3350
3351 doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
3352 if (!doc) {
3353 PyErr_SetString(PyExc_TypeError, "bad document.");
3354 return NULL;
3355 }
3356
3357 if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
3358 PyErr_SetString(PyExc_TypeError, "bad file.");
3359 return NULL;
3360 }
3361 output = PyFile_AsFile(py_file);
3362 if (output == NULL) {
3363 PyErr_SetString(PyExc_TypeError, "bad file.");
3364 return NULL;
3365 }
3366 buf = xmlOutputBufferCreateFile(output, NULL);
3367
3368 result = PyxmlNodeSet_Convert(pyobj_nodes, &nodes);
3369 if (result < 0) return NULL;
3370
3371 if (exclusive) {
3372 result = PystringSet_Convert(pyobj_prefixes, &prefixes);
3373 if (result < 0) {
3374 if (nodes) {
3375 xmlFree(nodes->nodeTab);
3376 xmlFree(nodes);
3377 }
3378 return NULL;
3379 }
3380 }
3381
3382 result = xmlC14NDocSaveTo(doc,
3383 nodes,
3384 exclusive,
3385 prefixes,
3386 with_comments,
3387 buf);
3388
3389 if (nodes) {
3390 xmlFree(nodes->nodeTab);
3391 xmlFree(nodes);
3392 }
3393 if (prefixes) {
3394 xmlChar ** idx = prefixes;
3395 while (*idx) xmlFree(*(idx++));
3396 xmlFree(prefixes);
3397 }
3398
3399 len = xmlOutputBufferClose(buf);
3400
3401 if (result < 0) {
3402 PyErr_SetString(PyExc_Exception,
3403 "libxml2 xmlC14NDocSaveTo failure.");
3404 return NULL;
3405 }
3406 else
3407 return PyInt_FromLong((long) len);
3408}
3409
3410#endif
3411#endif
3412
William M. Brackc68d78d2004-07-16 10:39:30 +00003413static PyObject *
3414libxml_getObjDesc(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003415
William M. Brackc68d78d2004-07-16 10:39:30 +00003416 PyObject *obj;
3417 char *str;
3418
3419 if (!PyArg_ParseTuple(args, (char *)"O:getObjDesc", &obj))
3420 return NULL;
3421 str = PyCObject_GetDesc(obj);
3422 return Py_BuildValue((char *)"s", str);
3423}
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003424
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00003425/************************************************************************
3426 * *
Daniel Veillardd2897fd2002-01-30 16:37:32 +00003427 * The registration stuff *
3428 * *
3429 ************************************************************************/
3430static PyMethodDef libxmlMethods[] = {
Daniel Veillard96fe0952002-01-30 20:52:23 +00003431#include "libxml2-export.c"
Daniel Veillardd2379012002-03-15 22:24:56 +00003432 {(char *) "name", libxml_name, METH_VARARGS, NULL},
3433 {(char *) "children", libxml_children, METH_VARARGS, NULL},
3434 {(char *) "properties", libxml_properties, METH_VARARGS, NULL},
3435 {(char *) "last", libxml_last, METH_VARARGS, NULL},
3436 {(char *) "prev", libxml_prev, METH_VARARGS, NULL},
3437 {(char *) "next", libxml_next, METH_VARARGS, NULL},
3438 {(char *) "parent", libxml_parent, METH_VARARGS, NULL},
3439 {(char *) "type", libxml_type, METH_VARARGS, NULL},
3440 {(char *) "doc", libxml_doc, METH_VARARGS, NULL},
3441 {(char *) "xmlNewNode", libxml_xmlNewNode, METH_VARARGS, NULL},
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003442#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00003443 {(char *) "serializeNode", libxml_serializeNode, METH_VARARGS, NULL},
3444 {(char *) "saveNodeTo", libxml_saveNodeTo, METH_VARARGS, NULL},
Daniel Veillardc6d4a932002-09-12 15:00:57 +00003445 {(char *) "outputBufferCreate", libxml_xmlCreateOutputBuffer, METH_VARARGS, NULL},
Daniel Veillard6cbd6c02003-12-04 12:31:49 +00003446 {(char *) "outputBufferGetPythonFile", libxml_outputBufferGetPythonFile, METH_VARARGS, NULL},
3447 {(char *) "xmlOutputBufferClose", libxml_xmlOutputBufferClose, METH_VARARGS, NULL},
3448 { (char *)"xmlOutputBufferFlush", libxml_xmlOutputBufferFlush, METH_VARARGS, NULL },
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003449#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardc6d4a932002-09-12 15:00:57 +00003450 {(char *) "inputBufferCreate", libxml_xmlCreateInputBuffer, METH_VARARGS, NULL},
3451 {(char *) "setEntityLoader", libxml_xmlSetEntityLoader, METH_VARARGS, NULL},
Daniel Veillard3e20a292003-01-10 13:14:40 +00003452 {(char *)"xmlRegisterErrorHandler", libxml_xmlRegisterErrorHandler, METH_VARARGS, NULL },
Daniel Veillard417be3a2003-01-20 21:26:34 +00003453 {(char *)"xmlParserCtxtSetErrorHandler", libxml_xmlParserCtxtSetErrorHandler, METH_VARARGS, NULL },
3454 {(char *)"xmlParserCtxtGetErrorHandler", libxml_xmlParserCtxtGetErrorHandler, METH_VARARGS, NULL },
Daniel Veillarde6227e02003-01-14 11:42:39 +00003455 {(char *)"xmlFreeParserCtxt", libxml_xmlFreeParserCtxt, METH_VARARGS, NULL },
Daniel Veillard26f70262003-01-16 22:45:08 +00003456 {(char *)"xmlTextReaderSetErrorHandler", libxml_xmlTextReaderSetErrorHandler, METH_VARARGS, NULL },
3457 {(char *)"xmlTextReaderGetErrorHandler", libxml_xmlTextReaderGetErrorHandler, METH_VARARGS, NULL },
3458 {(char *)"xmlFreeTextReader", libxml_xmlFreeTextReader, METH_VARARGS, NULL },
Daniel Veillard54396242003-04-23 07:36:50 +00003459 {(char *)"addLocalCatalog", libxml_addLocalCatalog, METH_VARARGS, NULL },
Daniel Veillardc2664642003-07-29 20:44:53 +00003460#ifdef LIBXML_SCHEMAS_ENABLED
3461 {(char *)"xmlRelaxNGSetValidErrors", libxml_xmlRelaxNGSetValidErrors, METH_VARARGS, NULL},
3462 {(char *)"xmlRelaxNGFreeValidCtxt", libxml_xmlRelaxNGFreeValidCtxt, METH_VARARGS, NULL},
3463#endif
Daniel Veillardd5e198a2004-03-09 09:03:28 +00003464#ifdef LIBXML_C14N_ENABLED
3465#ifdef LIBXML_OUTPUT_ENABLED
3466 {(char *)"xmlC14NDocDumpMemory", libxml_C14NDocDumpMemory, METH_VARARGS, NULL},
3467 {(char *)"xmlC14NDocSaveTo", libxml_C14NDocSaveTo, METH_VARARGS, NULL},
3468#endif
3469#endif
William M. Brackc68d78d2004-07-16 10:39:30 +00003470 {(char *) "getObjDesc", libxml_getObjDesc, METH_VARARGS, NULL},
Daniel Veillardd2379012002-03-15 22:24:56 +00003471 {NULL, NULL, 0, NULL}
Daniel Veillardd2897fd2002-01-30 16:37:32 +00003472};
3473
Daniel Veillard0fea6f42002-02-22 22:51:13 +00003474#ifdef MERGED_MODULES
Daniel Veillard6361da02002-02-23 10:10:33 +00003475extern void initlibxsltmod(void);
Daniel Veillard0fea6f42002-02-22 22:51:13 +00003476#endif
3477
Daniel Veillardd2379012002-03-15 22:24:56 +00003478void
3479initlibxml2mod(void)
3480{
Daniel Veillardaf43f632002-03-08 15:05:20 +00003481 static int initialized = 0;
Daniel Veillardaf43f632002-03-08 15:05:20 +00003482
3483 if (initialized != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00003484 return;
William M. Brack8e2cc6f2004-07-03 23:28:52 +00003485
Daniel Veillardf93a8662004-07-01 12:56:30 +00003486 /* intialize the python extension module */
3487 Py_InitModule((char *) "libxml2mod", libxmlMethods);
3488
3489 /* initialize libxml2 */
3490 xmlInitParser();
Daniel Veillard5d819032002-02-02 21:49:17 +00003491 libxml_xmlErrorInitialize();
Daniel Veillard6361da02002-02-23 10:10:33 +00003492
Daniel Veillardf93a8662004-07-01 12:56:30 +00003493 initialized = 1;
3494
Daniel Veillard0fea6f42002-02-22 22:51:13 +00003495#ifdef MERGED_MODULES
3496 initlibxsltmod();
3497#endif
Daniel Veillardd2897fd2002-01-30 16:37:32 +00003498}