blob: 15a30b5a74f60ec42aaad4b0288a9159f3c96fe4 [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 Veillardd2897fd2002-01-30 16:37:32 +000025#include "libxml_wrap.h"
Daniel Veillard96fe0952002-01-30 20:52:23 +000026#include "libxml2-py.h"
Daniel Veillardd2897fd2002-01-30 16:37:32 +000027
Daniel Veillard0d132cf2002-12-23 14:43:32 +000028#if (defined(_MSC_VER) || defined(__MINGW32__)) && !defined(vsnprintf)
29#define vsnprintf(b,c,f,a) _vsnprintf(b,c,f,a)
30#endif
31
Daniel Veillardd2897fd2002-01-30 16:37:32 +000032/* #define DEBUG */
Daniel Veillard797a5652002-02-12 13:46:21 +000033/* #define DEBUG_SAX */
Daniel Veillarda7340c82002-02-01 17:56:45 +000034/* #define DEBUG_XPATH */
Daniel Veillard5d819032002-02-02 21:49:17 +000035/* #define DEBUG_ERROR */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000036/* #define DEBUG_MEMORY */
Daniel Veillardc6d4a932002-09-12 15:00:57 +000037/* #define DEBUG_FILES */
38/* #define DEBUG_LOADER */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000039
Daniel Veillardd2379012002-03-15 22:24:56 +000040void initlibxml2mod(void);
41
Daniel Veillardc6d4a932002-09-12 15:00:57 +000042/**
43 * TODO:
44 *
45 * macro to flag unimplemented blocks
46 */
47#define TODO \
48 xmlGenericError(xmlGenericErrorContext, \
49 "Unimplemented block at %s:%d\n", \
50 __FILE__, __LINE__);
51
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000052/************************************************************************
53 * *
54 * Memory debug interface *
55 * *
56 ************************************************************************/
57
58extern void xmlMemFree(void *ptr);
59extern void *xmlMemMalloc(size_t size);
Daniel Veillardd2379012002-03-15 22:24:56 +000060extern void *xmlMemRealloc(void *ptr, size_t size);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000061extern char *xmlMemoryStrdup(const char *str);
62
63static int libxmlMemoryDebugActivated = 0;
64static long libxmlMemoryAllocatedBase = 0;
65
66static int libxmlMemoryDebug = 0;
67static xmlFreeFunc freeFunc = NULL;
68static xmlMallocFunc mallocFunc = NULL;
69static xmlReallocFunc reallocFunc = NULL;
70static xmlStrdupFunc strdupFunc = NULL;
71
72PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +000073libxml_xmlDebugMemory(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
74{
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000075 int activate;
76 PyObject *py_retval;
77 long ret;
78
Daniel Veillardd2379012002-03-15 22:24:56 +000079 if (!PyArg_ParseTuple(args, (char *) "i:xmlDebugMemory", &activate))
80 return (NULL);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000081
82#ifdef DEBUG_MEMORY
83 printf("libxml_xmlDebugMemory(%d) called\n", activate);
84#endif
85
86 if (activate != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +000087 if (libxmlMemoryDebug == 0) {
88 /*
89 * First initialize the library and grab the old memory handlers
90 * and switch the library to memory debugging
91 */
92 xmlMemGet((xmlFreeFunc *) & freeFunc,
93 (xmlMallocFunc *) & mallocFunc,
94 (xmlReallocFunc *) & reallocFunc,
95 (xmlStrdupFunc *) & strdupFunc);
96 if ((freeFunc == xmlMemFree) && (mallocFunc == xmlMemMalloc) &&
97 (reallocFunc == xmlMemRealloc) &&
98 (strdupFunc == xmlMemoryStrdup)) {
99 libxmlMemoryAllocatedBase = xmlMemUsed();
100 } else {
101 ret = (long) xmlMemSetup(xmlMemFree, xmlMemMalloc,
102 xmlMemRealloc, xmlMemoryStrdup);
103 if (ret < 0)
104 goto error;
105 libxmlMemoryAllocatedBase = xmlMemUsed();
106 }
107 xmlInitParser();
108 ret = 0;
109 } else if (libxmlMemoryDebugActivated == 0) {
110 libxmlMemoryAllocatedBase = xmlMemUsed();
111 ret = 0;
112 } else {
113 ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
114 }
115 libxmlMemoryDebug = 1;
116 libxmlMemoryDebugActivated = 1;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000117 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +0000118 if (libxmlMemoryDebugActivated == 1)
119 ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
120 else
121 ret = 0;
122 libxmlMemoryDebugActivated = 0;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000123 }
Daniel Veillardd2379012002-03-15 22:24:56 +0000124 error:
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000125 py_retval = libxml_longWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +0000126 return (py_retval);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000127}
128
129PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +0000130libxml_xmlDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
131 ATTRIBUTE_UNUSED PyObject * args)
132{
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000133
134 if (libxmlMemoryDebug != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +0000135 xmlMemoryDump();
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000136 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +0000137 return (Py_None);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000138}
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000139
140/************************************************************************
141 * *
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000142 * Handling Python FILE I/O at the C level *
143 * The raw I/O attack diectly the File objects, while the *
144 * other routines address the ioWrapper instance instead *
145 * *
146 ************************************************************************/
147
148/**
149 * xmlPythonFileCloseUnref:
150 * @context: the I/O context
151 *
152 * Close an I/O channel
153 */
154static int
155xmlPythonFileCloseRaw (void * context) {
156 PyObject *file, *ret;
157
158#ifdef DEBUG_FILES
159 printf("xmlPythonFileCloseUnref\n");
160#endif
161 file = (PyObject *) context;
162 if (file == NULL) return(-1);
Daniel Veillard118aed72002-09-24 14:13:13 +0000163 ret = PyEval_CallMethod(file, (char *) "close", (char *) "()");
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000164 if (ret != NULL) {
165 Py_DECREF(ret);
166 }
167 Py_DECREF(file);
168 return(0);
169}
170
171/**
172 * xmlPythonFileReadRaw:
173 * @context: the I/O context
174 * @buffer: where to drop data
175 * @len: number of bytes to write
176 *
177 * Read @len bytes to @buffer from the Python file in the I/O channel
178 *
179 * Returns the number of bytes read
180 */
181static int
182xmlPythonFileReadRaw (void * context, char * buffer, int len) {
183 PyObject *file;
184 PyObject *ret;
185 int lenread = -1;
186 char *data;
187
188#ifdef DEBUG_FILES
189 printf("xmlPythonFileReadRaw: %d\n", len);
190#endif
191 file = (PyObject *) context;
192 if (file == NULL) return(-1);
Daniel Veillard118aed72002-09-24 14:13:13 +0000193 ret = PyEval_CallMethod(file, (char *) "read", (char *) "(i)", len);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000194 if (ret == NULL) {
195 printf("xmlPythonFileReadRaw: result is NULL\n");
196 return(-1);
197 } else if (PyString_Check(ret)) {
198 lenread = PyString_Size(ret);
199 data = PyString_AsString(ret);
200 if (lenread > len)
201 memcpy(buffer, data, len);
202 else
203 memcpy(buffer, data, lenread);
204 Py_DECREF(ret);
205 } else {
206 printf("xmlPythonFileReadRaw: result is not a String\n");
207 Py_DECREF(ret);
208 }
209 return(lenread);
210}
211
212/**
213 * xmlPythonFileRead:
214 * @context: the I/O context
215 * @buffer: where to drop data
216 * @len: number of bytes to write
217 *
218 * Read @len bytes to @buffer from the I/O channel.
219 *
220 * Returns the number of bytes read
221 */
222static int
223xmlPythonFileRead (void * context, char * buffer, int len) {
224 PyObject *file;
225 PyObject *ret;
226 int lenread = -1;
227 char *data;
228
229#ifdef DEBUG_FILES
230 printf("xmlPythonFileRead: %d\n", len);
231#endif
232 file = (PyObject *) context;
233 if (file == NULL) return(-1);
Daniel Veillard118aed72002-09-24 14:13:13 +0000234 ret = PyEval_CallMethod(file, (char *) "io_read", (char *) "(i)", len);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000235 if (ret == NULL) {
236 printf("xmlPythonFileRead: result is NULL\n");
237 return(-1);
238 } else if (PyString_Check(ret)) {
239 lenread = PyString_Size(ret);
240 data = PyString_AsString(ret);
241 if (lenread > len)
242 memcpy(buffer, data, len);
243 else
244 memcpy(buffer, data, lenread);
245 Py_DECREF(ret);
246 } else {
247 printf("xmlPythonFileRead: result is not a String\n");
248 Py_DECREF(ret);
249 }
250 return(lenread);
251}
252
253/**
254 * xmlFileWrite:
255 * @context: the I/O context
256 * @buffer: where to drop data
257 * @len: number of bytes to write
258 *
259 * Write @len bytes from @buffer to the I/O channel.
260 *
261 * Returns the number of bytes written
262 */
263static int
264xmlPythonFileWrite (void * context, const char * buffer, int len) {
265 PyObject *file;
266 PyObject *string;
267 PyObject *ret;
268 int written = -1;
269
270#ifdef DEBUG_FILES
271 printf("xmlPythonFileWrite: %d\n", len);
272#endif
273 file = (PyObject *) context;
274 if (file == NULL) return(-1);
275 string = PyString_FromStringAndSize(buffer, len);
276 if (string == NULL) return(-1);
Daniel Veillard118aed72002-09-24 14:13:13 +0000277 ret = PyEval_CallMethod(file, (char *) "io_write", (char *) "(O)", string);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000278 Py_DECREF(string);
279 if (ret == NULL) {
280 printf("xmlPythonFileWrite: result is NULL\n");
281 return(-1);
282 } else if (PyInt_Check(ret)) {
283 written = (int) PyInt_AsLong(ret);
284 Py_DECREF(ret);
285 } else if (ret == Py_None) {
286 written = len;
287 Py_DECREF(ret);
288 } else {
289 printf("xmlPythonFileWrite: result is not an Int nor None\n");
290 Py_DECREF(ret);
291 }
292 return(written);
293}
294
295/**
296 * xmlPythonFileClose:
297 * @context: the I/O context
298 *
299 * Close an I/O channel
300 */
301static int
302xmlPythonFileClose (void * context) {
303 PyObject *file, *ret;
304
305#ifdef DEBUG_FILES
306 printf("xmlPythonFileClose\n");
307#endif
308 file = (PyObject *) context;
309 if (file == NULL) return(-1);
Daniel Veillard118aed72002-09-24 14:13:13 +0000310 ret = PyEval_CallMethod(file, (char *) "io_close", (char *) "()");
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000311 if (ret != NULL) {
312 Py_DECREF(ret);
313 }
314 return(0);
315}
316
317/**
318 * xmlOutputBufferCreatePythonFile:
319 * @file: a PyFile_Type
320 * @encoder: the encoding converter or NULL
321 *
322 * Create a buffered output for the progressive saving to a PyFile_Type
323 * buffered C I/O
324 *
325 * Returns the new parser output or NULL
326 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000327static xmlOutputBufferPtr
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000328xmlOutputBufferCreatePythonFile(PyObject *file,
329 xmlCharEncodingHandlerPtr encoder) {
330 xmlOutputBufferPtr ret;
331
332 if (file == NULL) return(NULL);
333
334 ret = xmlAllocOutputBuffer(encoder);
335 if (ret != NULL) {
336 ret->context = file;
337 /* Py_INCREF(file); */
338 ret->writecallback = xmlPythonFileWrite;
339 ret->closecallback = xmlPythonFileClose;
340 }
341
342 return(ret);
343}
344
345PyObject *
346libxml_xmlCreateOutputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
347 PyObject *py_retval;
348 PyObject *file;
349 xmlChar *encoding;
350 xmlCharEncodingHandlerPtr handler = NULL;
351 xmlOutputBufferPtr buffer;
352
353
354 if (!PyArg_ParseTuple(args, (char *)"Oz:xmlOutputBufferCreate",
355 &file, &encoding))
356 return(NULL);
357 if ((encoding != NULL) && (encoding[0] != 0)) {
Daniel Veillard118aed72002-09-24 14:13:13 +0000358 handler = xmlFindCharEncodingHandler((const char *) encoding);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000359 }
360 buffer = xmlOutputBufferCreatePythonFile(file, handler);
361 if (buffer == NULL)
362 printf("libxml_xmlCreateOutputBuffer: buffer == NULL\n");
363 py_retval = libxml_xmlOutputBufferPtrWrap(buffer);
364 return(py_retval);
365}
366
367
368/**
369 * xmlParserInputBufferCreatePythonFile:
370 * @file: a PyFile_Type
371 * @encoder: the encoding converter or NULL
372 *
373 * Create a buffered output for the progressive saving to a PyFile_Type
374 * buffered C I/O
375 *
376 * Returns the new parser output or NULL
377 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000378static xmlParserInputBufferPtr
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000379xmlParserInputBufferCreatePythonFile(PyObject *file,
380 xmlCharEncoding encoding) {
381 xmlParserInputBufferPtr ret;
382
383 if (file == NULL) return(NULL);
384
385 ret = xmlAllocParserInputBuffer(encoding);
386 if (ret != NULL) {
387 ret->context = file;
388 /* Py_INCREF(file); */
389 ret->readcallback = xmlPythonFileRead;
390 ret->closecallback = xmlPythonFileClose;
391 }
392
393 return(ret);
394}
395
396PyObject *
397libxml_xmlCreateInputBuffer(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
398 PyObject *py_retval;
399 PyObject *file;
400 xmlChar *encoding;
401 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
402 xmlParserInputBufferPtr buffer;
403
404
405 if (!PyArg_ParseTuple(args, (char *)"Oz:xmlParserInputBufferCreate",
406 &file, &encoding))
407 return(NULL);
408 if ((encoding != NULL) && (encoding[0] != 0)) {
Daniel Veillard118aed72002-09-24 14:13:13 +0000409 enc = xmlParseCharEncoding((const char *) encoding);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000410 }
411 buffer = xmlParserInputBufferCreatePythonFile(file, enc);
412 if (buffer == NULL)
413 printf("libxml_xmlParserInputBufferCreate: buffer == NULL\n");
414 py_retval = libxml_xmlParserInputBufferPtrWrap(buffer);
415 return(py_retval);
416}
417
418/************************************************************************
419 * *
420 * Providing the resolver at the Python level *
421 * *
422 ************************************************************************/
423
424static xmlExternalEntityLoader defaultExternalEntityLoader = NULL;
425static PyObject *pythonExternalEntityLoaderObjext;
426
427static xmlParserInputPtr
428pythonExternalEntityLoader(const char *URL, const char *ID,
429 xmlParserCtxtPtr ctxt) {
430 xmlParserInputPtr result = NULL;
431 if (pythonExternalEntityLoaderObjext != NULL) {
432 PyObject *ret;
433 PyObject *ctxtobj;
434
435 ctxtobj = libxml_xmlParserCtxtPtrWrap(ctxt);
436#ifdef DEBUG_LOADER
437 printf("pythonExternalEntityLoader: ready to call\n");
438#endif
439
440 ret = PyObject_CallFunction(pythonExternalEntityLoaderObjext,
Daniel Veillard118aed72002-09-24 14:13:13 +0000441 (char *) "(ssO)", URL, ID, ctxtobj);
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000442#ifdef DEBUG_LOADER
443 printf("pythonExternalEntityLoader: result ");
444 PyObject_Print(ret, stdout, 0);
445 printf("\n");
446#endif
447
448 if (ret != NULL) {
Daniel Veillard118aed72002-09-24 14:13:13 +0000449 if (PyObject_HasAttrString(ret, (char *) "read")) {
Daniel Veillardc6d4a932002-09-12 15:00:57 +0000450 xmlParserInputBufferPtr buf;
451
452 buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
453 if (buf != NULL) {
454 buf->context = ret;
455 buf->readcallback = xmlPythonFileReadRaw;
456 buf->closecallback = xmlPythonFileCloseRaw;
457 result = xmlNewIOInputStream(ctxt, buf,
458 XML_CHAR_ENCODING_NONE);
459 }
460 } else {
461 printf("pythonExternalEntityLoader: can't read\n");
462 }
463 if (result == NULL) {
464 Py_DECREF(ret);
465 }
466 }
467 }
468 if ((result == NULL) && (defaultExternalEntityLoader != NULL)) {
469 result = defaultExternalEntityLoader(URL, ID, ctxt);
470 }
471 return(result);
472}
473
474PyObject *
475libxml_xmlSetEntityLoader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
476 PyObject *py_retval;
477 PyObject *loader;
478
479 if (!PyArg_ParseTuple(args, (char *)"O:libxml_xmlSetEntityLoader",
480 &loader))
481 return(NULL);
482
483#ifdef DEBUG_LOADER
484 printf("libxml_xmlSetEntityLoader\n");
485#endif
486 if (defaultExternalEntityLoader == NULL)
487 defaultExternalEntityLoader = xmlGetExternalEntityLoader();
488
489 pythonExternalEntityLoaderObjext = loader;
490 xmlSetExternalEntityLoader(pythonExternalEntityLoader);
491
492 py_retval = PyInt_FromLong(0);
493 return(py_retval);
494}
495
496
497/************************************************************************
498 * *
Daniel Veillard3ce52572002-02-03 15:08:05 +0000499 * Handling SAX/xmllib/sgmlop callback interfaces *
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000500 * *
501 ************************************************************************/
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000502
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000503static void
504pythonStartElement(void *user_data, const xmlChar * name,
505 const xmlChar ** attrs)
506{
507 int i;
508 PyObject *handler;
509 PyObject *dict;
510 PyObject *attrname;
511 PyObject *attrvalue;
Daniel Veillardd2379012002-03-15 22:24:56 +0000512 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000513 int type = 0;
514
Daniel Veillard797a5652002-02-12 13:46:21 +0000515#ifdef DEBUG_SAX
516 printf("pythonStartElement(%s) called\n", name);
517#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000518 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000519 if (PyObject_HasAttrString(handler, (char *) "startElement"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000520 type = 1;
Daniel Veillardd2379012002-03-15 22:24:56 +0000521 else if (PyObject_HasAttrString(handler, (char *) "start"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000522 type = 2;
523 if (type != 0) {
524 /*
525 * the xmllib interface always generate a dictionnary,
526 * possibly empty
527 */
528 if ((attrs == NULL) && (type == 1)) {
529 Py_XINCREF(Py_None);
530 dict = Py_None;
Daniel Veillardd2379012002-03-15 22:24:56 +0000531 } else if (attrs == NULL) {
532 dict = PyDict_New();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000533 } else {
534 dict = PyDict_New();
535 for (i = 0; attrs[i] != NULL; i++) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000536 attrname = PyString_FromString((char *) attrs[i]);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000537 i++;
538 if (attrs[i] != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000539 attrvalue = PyString_FromString((char *) attrs[i]);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000540 } else {
541 Py_XINCREF(Py_None);
542 attrvalue = Py_None;
543 }
544 PyDict_SetItem(dict, attrname, attrvalue);
545 }
546 }
547
548 if (type == 1)
Daniel Veillardd2379012002-03-15 22:24:56 +0000549 result = PyObject_CallMethod(handler, (char *) "startElement",
550 (char *) "sO", name, dict);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000551 else if (type == 2)
Daniel Veillardd2379012002-03-15 22:24:56 +0000552 result = PyObject_CallMethod(handler, (char *) "start",
553 (char *) "sO", name, dict);
554 if (PyErr_Occurred())
555 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000556 Py_XDECREF(dict);
557 Py_XDECREF(result);
558 }
559}
560
561static void
562pythonStartDocument(void *user_data)
563{
564 PyObject *handler;
565 PyObject *result;
566
Daniel Veillard797a5652002-02-12 13:46:21 +0000567#ifdef DEBUG_SAX
568 printf("pythonStartDocument() called\n");
569#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000570 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000571 if (PyObject_HasAttrString(handler, (char *) "startDocument")) {
572 result =
573 PyObject_CallMethod(handler, (char *) "startDocument", NULL);
574 if (PyErr_Occurred())
575 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000576 Py_XDECREF(result);
577 }
578}
579
580static void
581pythonEndDocument(void *user_data)
582{
583 PyObject *handler;
584 PyObject *result;
585
Daniel Veillard797a5652002-02-12 13:46:21 +0000586#ifdef DEBUG_SAX
587 printf("pythonEndDocument() called\n");
588#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000589 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000590 if (PyObject_HasAttrString(handler, (char *) "endDocument")) {
591 result =
592 PyObject_CallMethod(handler, (char *) "endDocument", NULL);
593 if (PyErr_Occurred())
594 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000595 Py_XDECREF(result);
596 }
597 /*
598 * The reference to the handler is released there
599 */
600 Py_XDECREF(handler);
601}
602
603static void
604pythonEndElement(void *user_data, const xmlChar * name)
605{
606 PyObject *handler;
607 PyObject *result;
608
Daniel Veillard797a5652002-02-12 13:46:21 +0000609#ifdef DEBUG_SAX
610 printf("pythonEndElement(%s) called\n", name);
611#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000612 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000613 if (PyObject_HasAttrString(handler, (char *) "endElement")) {
614 result = PyObject_CallMethod(handler, (char *) "endElement",
615 (char *) "s", name);
616 if (PyErr_Occurred())
617 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000618 Py_XDECREF(result);
Daniel Veillardd2379012002-03-15 22:24:56 +0000619 } else if (PyObject_HasAttrString(handler, (char *) "end")) {
620 result = PyObject_CallMethod(handler, (char *) "end",
621 (char *) "s", name);
622 if (PyErr_Occurred())
623 PyErr_Print();
Daniel Veillard797a5652002-02-12 13:46:21 +0000624 Py_XDECREF(result);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000625 }
626}
627
628static void
629pythonReference(void *user_data, const xmlChar * name)
630{
631 PyObject *handler;
632 PyObject *result;
633
Daniel Veillard797a5652002-02-12 13:46:21 +0000634#ifdef DEBUG_SAX
635 printf("pythonReference(%s) called\n", name);
636#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000637 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000638 if (PyObject_HasAttrString(handler, (char *) "reference")) {
639 result = PyObject_CallMethod(handler, (char *) "reference",
640 (char *) "s", name);
641 if (PyErr_Occurred())
642 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000643 Py_XDECREF(result);
644 }
645}
646
647static void
648pythonCharacters(void *user_data, const xmlChar * ch, int len)
649{
650 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +0000651 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000652 int type = 0;
653
Daniel Veillard797a5652002-02-12 13:46:21 +0000654#ifdef DEBUG_SAX
655 printf("pythonCharacters(%s, %d) called\n", ch, len);
656#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000657 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000658 if (PyObject_HasAttrString(handler, (char *) "characters"))
659 type = 1;
660 else if (PyObject_HasAttrString(handler, (char *) "data"))
661 type = 2;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000662 if (type != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000663 if (type == 1)
664 result = PyObject_CallMethod(handler, (char *) "characters",
665 (char *) "s#", ch, len);
666 else if (type == 2)
667 result = PyObject_CallMethod(handler, (char *) "data",
668 (char *) "s#", ch, len);
669 if (PyErr_Occurred())
670 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000671 Py_XDECREF(result);
672 }
673}
674
675static void
676pythonIgnorableWhitespace(void *user_data, const xmlChar * ch, int len)
677{
678 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +0000679 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000680 int type = 0;
681
Daniel Veillard797a5652002-02-12 13:46:21 +0000682#ifdef DEBUG_SAX
683 printf("pythonIgnorableWhitespace(%s, %d) called\n", ch, len);
684#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000685 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000686 if (PyObject_HasAttrString(handler, (char *) "ignorableWhitespace"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000687 type = 1;
Daniel Veillardd2379012002-03-15 22:24:56 +0000688 else if (PyObject_HasAttrString(handler, (char *) "data"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000689 type = 2;
690 if (type != 0) {
691 if (type == 1)
692 result =
Daniel Veillardd2379012002-03-15 22:24:56 +0000693 PyObject_CallMethod(handler,
694 (char *) "ignorableWhitespace",
695 (char *) "s#", ch, len);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000696 else if (type == 2)
Daniel Veillardd2379012002-03-15 22:24:56 +0000697 result =
698 PyObject_CallMethod(handler, (char *) "data",
699 (char *) "s#", ch, len);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000700 Py_XDECREF(result);
701 }
702}
703
704static void
705pythonProcessingInstruction(void *user_data,
706 const xmlChar * target, const xmlChar * data)
707{
708 PyObject *handler;
709 PyObject *result;
710
Daniel Veillard797a5652002-02-12 13:46:21 +0000711#ifdef DEBUG_SAX
712 printf("pythonProcessingInstruction(%s, %s) called\n", target, data);
713#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000714 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000715 if (PyObject_HasAttrString(handler, (char *) "processingInstruction")) {
716 result = PyObject_CallMethod(handler, (char *)
717 "processingInstruction",
718 (char *) "ss", target, data);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000719 Py_XDECREF(result);
720 }
721}
722
723static void
724pythonComment(void *user_data, const xmlChar * value)
725{
726 PyObject *handler;
727 PyObject *result;
728
Daniel Veillard797a5652002-02-12 13:46:21 +0000729#ifdef DEBUG_SAX
730 printf("pythonComment(%s) called\n", value);
731#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000732 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000733 if (PyObject_HasAttrString(handler, (char *) "comment")) {
734 result =
735 PyObject_CallMethod(handler, (char *) "comment", (char *) "s",
736 value);
737 if (PyErr_Occurred())
738 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000739 Py_XDECREF(result);
740 }
741}
742
743static void
744pythonWarning(void *user_data, const char *msg, ...)
745{
746 PyObject *handler;
747 PyObject *result;
748 va_list args;
749 char buf[1024];
750
Daniel Veillard797a5652002-02-12 13:46:21 +0000751#ifdef DEBUG_SAX
752 printf("pythonWarning(%s) called\n", msg);
753#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000754 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000755 if (PyObject_HasAttrString(handler, (char *) "warning")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000756 va_start(args, msg);
757 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +0000758 va_end(args);
759 buf[1023] = 0;
760 result =
761 PyObject_CallMethod(handler, (char *) "warning", (char *) "s",
762 buf);
763 if (PyErr_Occurred())
764 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000765 Py_XDECREF(result);
766 }
767}
768
769static void
770pythonError(void *user_data, const char *msg, ...)
771{
772 PyObject *handler;
773 PyObject *result;
774 va_list args;
775 char buf[1024];
776
Daniel Veillard797a5652002-02-12 13:46:21 +0000777#ifdef DEBUG_SAX
778 printf("pythonError(%s) called\n", msg);
779#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000780 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000781 if (PyObject_HasAttrString(handler, (char *) "error")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000782 va_start(args, msg);
783 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +0000784 va_end(args);
785 buf[1023] = 0;
786 result =
787 PyObject_CallMethod(handler, (char *) "error", (char *) "s",
788 buf);
789 if (PyErr_Occurred())
790 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000791 Py_XDECREF(result);
792 }
793}
794
795static void
796pythonFatalError(void *user_data, const char *msg, ...)
797{
798 PyObject *handler;
799 PyObject *result;
800 va_list args;
801 char buf[1024];
802
Daniel Veillard797a5652002-02-12 13:46:21 +0000803#ifdef DEBUG_SAX
804 printf("pythonFatalError(%s) called\n", msg);
805#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000806 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000807 if (PyObject_HasAttrString(handler, (char *) "fatalError")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000808 va_start(args, msg);
809 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +0000810 va_end(args);
811 buf[1023] = 0;
812 result =
813 PyObject_CallMethod(handler, (char *) "fatalError",
814 (char *) "s", buf);
815 if (PyErr_Occurred())
816 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000817 Py_XDECREF(result);
818 }
819}
820
821static void
822pythonCdataBlock(void *user_data, const xmlChar * ch, int len)
823{
824 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +0000825 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000826 int type = 0;
827
Daniel Veillard797a5652002-02-12 13:46:21 +0000828#ifdef DEBUG_SAX
829 printf("pythonCdataBlock(%s, %d) called\n", ch, len);
830#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000831 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000832 if (PyObject_HasAttrString(handler, (char *) "cdataBlock"))
833 type = 1;
834 else if (PyObject_HasAttrString(handler, (char *) "cdata"))
835 type = 2;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000836 if (type != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000837 if (type == 1)
838 result =
839 PyObject_CallMethod(handler, (char *) "cdataBlock",
840 (char *) "s#", ch, len);
841 else if (type == 2)
842 result =
843 PyObject_CallMethod(handler, (char *) "cdata",
844 (char *) "s#", ch, len);
845 if (PyErr_Occurred())
846 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000847 Py_XDECREF(result);
848 }
849}
850
851static void
852pythonExternalSubset(void *user_data,
853 const xmlChar * name,
854 const xmlChar * externalID, const xmlChar * systemID)
855{
856 PyObject *handler;
857 PyObject *result;
858
Daniel Veillard797a5652002-02-12 13:46:21 +0000859#ifdef DEBUG_SAX
860 printf("pythonExternalSubset(%s, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +0000861 name, externalID, systemID);
Daniel Veillard797a5652002-02-12 13:46:21 +0000862#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000863 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000864 if (PyObject_HasAttrString(handler, (char *) "externalSubset")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000865 result =
Daniel Veillardd2379012002-03-15 22:24:56 +0000866 PyObject_CallMethod(handler, (char *) "externalSubset",
867 (char *) "sss", name, externalID,
868 systemID);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000869 Py_XDECREF(result);
870 }
871}
872
873static void
874pythonEntityDecl(void *user_data,
875 const xmlChar * name,
876 int type,
877 const xmlChar * publicId,
878 const xmlChar * systemId, xmlChar * content)
879{
880 PyObject *handler;
881 PyObject *result;
882
883 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000884 if (PyObject_HasAttrString(handler, (char *) "entityDecl")) {
885 result = PyObject_CallMethod(handler, (char *) "entityDecl",
886 (char *) "sisss", name, type,
887 publicId, systemId, content);
888 if (PyErr_Occurred())
889 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000890 Py_XDECREF(result);
891 }
892}
893
894
895
896static void
897
898pythonNotationDecl(void *user_data,
899 const xmlChar * name,
900 const xmlChar * publicId, const xmlChar * systemId)
901{
902 PyObject *handler;
903 PyObject *result;
904
905 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000906 if (PyObject_HasAttrString(handler, (char *) "notationDecl")) {
907 result = PyObject_CallMethod(handler, (char *) "notationDecl",
908 (char *) "sss", name, publicId,
909 systemId);
910 if (PyErr_Occurred())
911 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000912 Py_XDECREF(result);
913 }
914}
915
916static void
917pythonAttributeDecl(void *user_data,
918 const xmlChar * elem,
919 const xmlChar * name,
920 int type,
921 int def,
Daniel Veillardd2379012002-03-15 22:24:56 +0000922 const xmlChar * defaultValue, xmlEnumerationPtr tree)
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000923{
924 PyObject *handler;
925 PyObject *nameList;
926 PyObject *newName;
927 xmlEnumerationPtr node;
928 PyObject *result;
929 int count;
930
931 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000932 if (PyObject_HasAttrString(handler, (char *) "attributeDecl")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000933 count = 0;
934 for (node = tree; node != NULL; node = node->next) {
935 count++;
936 }
937 nameList = PyList_New(count);
938 count = 0;
939 for (node = tree; node != NULL; node = node->next) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000940 newName = PyString_FromString((char *) node->name);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000941 PyList_SetItem(nameList, count, newName);
942 count++;
943 }
Daniel Veillardd2379012002-03-15 22:24:56 +0000944 result = PyObject_CallMethod(handler, (char *) "attributeDecl",
945 (char *) "ssiisO", elem, name, type,
946 def, defaultValue, nameList);
947 if (PyErr_Occurred())
948 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000949 Py_XDECREF(nameList);
950 Py_XDECREF(result);
951 }
952}
953
954static void
955pythonElementDecl(void *user_data,
956 const xmlChar * name,
Daniel Veillardd2379012002-03-15 22:24:56 +0000957 int type, ATTRIBUTE_UNUSED xmlElementContentPtr content)
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000958{
959 PyObject *handler;
960 PyObject *obj;
961 PyObject *result;
962
963 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000964 if (PyObject_HasAttrString(handler, (char *) "elementDecl")) {
965 /* TODO: wrap in an elementContent object */
966 printf
967 ("pythonElementDecl: xmlElementContentPtr wrapper missing !\n");
968 obj = Py_None;
969 /* Py_XINCREF(Py_None); isn't the reference just borrowed ??? */
970 result = PyObject_CallMethod(handler, (char *) "elementDecl",
971 (char *) "siO", name, type, obj);
972 if (PyErr_Occurred())
973 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000974 Py_XDECREF(result);
975 }
976}
977
978static void
979pythonUnparsedEntityDecl(void *user_data,
980 const xmlChar * name,
981 const xmlChar * publicId,
982 const xmlChar * systemId,
983 const xmlChar * notationName)
984{
985 PyObject *handler;
986 PyObject *result;
987
988 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000989 if (PyObject_HasAttrString(handler, (char *) "unparsedEntityDecl")) {
990 result =
991 PyObject_CallMethod(handler, (char *) "unparsedEntityDecl",
992 (char *) "ssss", name, publicId, systemId,
993 notationName);
994 if (PyErr_Occurred())
995 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000996 Py_XDECREF(result);
997 }
998}
999
1000static void
1001pythonInternalSubset(void *user_data, const xmlChar * name,
1002 const xmlChar * ExternalID, const xmlChar * SystemID)
1003{
1004 PyObject *handler;
1005 PyObject *result;
1006
Daniel Veillard797a5652002-02-12 13:46:21 +00001007#ifdef DEBUG_SAX
1008 printf("pythonInternalSubset(%s, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001009 name, ExternalID, SystemID);
Daniel Veillard797a5652002-02-12 13:46:21 +00001010#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001011 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +00001012 if (PyObject_HasAttrString(handler, (char *) "internalSubset")) {
1013 result = PyObject_CallMethod(handler, (char *) "internalSubset",
1014 (char *) "sss", name, ExternalID,
1015 SystemID);
1016 if (PyErr_Occurred())
1017 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001018 Py_XDECREF(result);
1019 }
1020}
1021
1022static xmlSAXHandler pythonSaxHandler = {
1023 pythonInternalSubset,
Daniel Veillardd2379012002-03-15 22:24:56 +00001024 NULL, /* TODO pythonIsStandalone, */
1025 NULL, /* TODO pythonHasInternalSubset, */
1026 NULL, /* TODO pythonHasExternalSubset, */
1027 NULL, /* TODO pythonResolveEntity, */
1028 NULL, /* TODO pythonGetEntity, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001029 pythonEntityDecl,
1030 pythonNotationDecl,
1031 pythonAttributeDecl,
1032 pythonElementDecl,
1033 pythonUnparsedEntityDecl,
Daniel Veillardd2379012002-03-15 22:24:56 +00001034 NULL, /* OBSOLETED pythonSetDocumentLocator, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001035 pythonStartDocument,
1036 pythonEndDocument,
1037 pythonStartElement,
1038 pythonEndElement,
1039 pythonReference,
1040 pythonCharacters,
1041 pythonIgnorableWhitespace,
1042 pythonProcessingInstruction,
1043 pythonComment,
1044 pythonWarning,
1045 pythonError,
1046 pythonFatalError,
Daniel Veillardd2379012002-03-15 22:24:56 +00001047 NULL, /* TODO pythonGetParameterEntity, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001048 pythonCdataBlock,
1049 pythonExternalSubset,
1050 1
1051};
Daniel Veillard3ce52572002-02-03 15:08:05 +00001052
1053/************************************************************************
1054 * *
1055 * Handling of specific parser context *
1056 * *
1057 ************************************************************************/
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001058
1059PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001060libxml_xmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
1061 PyObject * args)
1062{
1063 const char *chunk;
Daniel Veillard3ce52572002-02-03 15:08:05 +00001064 int size;
Daniel Veillardd2379012002-03-15 22:24:56 +00001065 const char *URI;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001066 PyObject *pyobj_SAX = NULL;
Daniel Veillard3ce52572002-02-03 15:08:05 +00001067 xmlSAXHandlerPtr SAX = NULL;
Daniel Veillard3ce52572002-02-03 15:08:05 +00001068 xmlParserCtxtPtr ret;
1069 PyObject *pyret;
Daniel Veillard96fe0952002-01-30 20:52:23 +00001070
Daniel Veillardd2379012002-03-15 22:24:56 +00001071 if (!PyArg_ParseTuple
1072 (args, (char *) "Oziz:xmlCreatePushParser", &pyobj_SAX, &chunk,
1073 &size, &URI))
1074 return (NULL);
Daniel Veillard3ce52572002-02-03 15:08:05 +00001075
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001076#ifdef DEBUG
Daniel Veillard3ce52572002-02-03 15:08:05 +00001077 printf("libxml_xmlCreatePushParser(%p, %s, %d, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001078 pyobj_SAX, chunk, size, URI);
Daniel Veillard96fe0952002-01-30 20:52:23 +00001079#endif
Daniel Veillard3ce52572002-02-03 15:08:05 +00001080 if (pyobj_SAX != Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001081 SAX = &pythonSaxHandler;
1082 Py_INCREF(pyobj_SAX);
1083 /* The reference is released in pythonEndDocument() */
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001084 }
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001085 ret = xmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI);
Daniel Veillard3ce52572002-02-03 15:08:05 +00001086 pyret = libxml_xmlParserCtxtPtrWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +00001087 return (pyret);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001088}
Daniel Veillard5d819032002-02-02 21:49:17 +00001089
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001090PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001091libxml_htmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
1092 PyObject * args)
1093{
1094 const char *chunk;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001095 int size;
Daniel Veillardd2379012002-03-15 22:24:56 +00001096 const char *URI;
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001097 PyObject *pyobj_SAX = NULL;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001098 xmlSAXHandlerPtr SAX = NULL;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001099 xmlParserCtxtPtr ret;
1100 PyObject *pyret;
1101
Daniel Veillardd2379012002-03-15 22:24:56 +00001102 if (!PyArg_ParseTuple
1103 (args, (char *) "Oziz:htmlCreatePushParser", &pyobj_SAX, &chunk,
1104 &size, &URI))
1105 return (NULL);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001106
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001107#ifdef DEBUG
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001108 printf("libxml_htmlCreatePushParser(%p, %s, %d, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001109 pyobj_SAX, chunk, size, URI);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001110#endif
1111 if (pyobj_SAX != Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001112 SAX = &pythonSaxHandler;
1113 Py_INCREF(pyobj_SAX);
1114 /* The reference is released in pythonEndDocument() */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001115 }
Daniel Veillard33caa0b2002-02-04 14:07:26 +00001116 ret = htmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI,
Daniel Veillardd2379012002-03-15 22:24:56 +00001117 XML_CHAR_ENCODING_NONE);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001118 pyret = libxml_xmlParserCtxtPtrWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +00001119 return (pyret);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001120}
1121
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001122PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001123libxml_xmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1124{
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001125 int recover;
Daniel Veillardd2379012002-03-15 22:24:56 +00001126 const char *URI;
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001127 PyObject *pyobj_SAX = NULL;
1128 xmlSAXHandlerPtr SAX = NULL;
1129
Daniel Veillardd2379012002-03-15 22:24:56 +00001130 if (!PyArg_ParseTuple(args, (char *) "Osi:xmlSAXParseFile", &pyobj_SAX,
1131 &URI, &recover))
1132 return (NULL);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001133
1134#ifdef DEBUG
1135 printf("libxml_xmlSAXParseFile(%p, %s, %d) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001136 pyobj_SAX, URI, recover);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001137#endif
1138 if (pyobj_SAX == Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001139 Py_INCREF(Py_None);
1140 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001141 }
1142 SAX = &pythonSaxHandler;
1143 Py_INCREF(pyobj_SAX);
1144 /* The reference is released in pythonEndDocument() */
1145 xmlSAXParseFileWithData(SAX, URI, recover, pyobj_SAX);
1146 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +00001147 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001148}
1149
1150PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001151libxml_htmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1152{
1153 const char *URI;
1154 const char *encoding;
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001155 PyObject *pyobj_SAX = NULL;
1156 xmlSAXHandlerPtr SAX = NULL;
1157
Daniel Veillardd2379012002-03-15 22:24:56 +00001158 if (!PyArg_ParseTuple
1159 (args, (char *) "Osz:htmlSAXParseFile", &pyobj_SAX, &URI,
1160 &encoding))
1161 return (NULL);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001162
1163#ifdef DEBUG
1164 printf("libxml_htmlSAXParseFile(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001165 pyobj_SAX, URI, encoding);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001166#endif
1167 if (pyobj_SAX == Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001168 Py_INCREF(Py_None);
1169 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001170 }
1171 SAX = &pythonSaxHandler;
1172 Py_INCREF(pyobj_SAX);
1173 /* The reference is released in pythonEndDocument() */
1174 htmlSAXParseFile(URI, encoding, SAX, pyobj_SAX);
1175 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +00001176 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +00001177}
1178
Daniel Veillarde6227e02003-01-14 11:42:39 +00001179PyObject *
1180libxml_xmlFreeParserCtxt(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
1181 xmlParserCtxtPtr ctxt;
1182 PyObject *pyobj_ctxt;
1183 xmlParserCtxtPyCtxtPtr pyCtxt;
1184
1185 if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeParserCtxt", &pyobj_ctxt))
1186 return(NULL);
1187 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1188
1189 if (ctxt != NULL) {
1190 pyCtxt = (xmlParserCtxtPyCtxtPtr)((xmlParserCtxtPtr)ctxt)->_private;
1191 if (pyCtxt) {
1192 Py_XDECREF(pyCtxt->errorFunc);
1193 Py_XDECREF(pyCtxt->errorFuncArg);
1194 Py_XDECREF(pyCtxt->warningFunc);
1195 Py_XDECREF(pyCtxt->warningFuncArg);
1196 xmlFree(pyCtxt);
1197 }
1198 xmlFreeParserCtxt(ctxt);
1199 }
1200
1201 Py_INCREF(Py_None);
1202 return(Py_None);
1203}
1204
Daniel Veillard5d819032002-02-02 21:49:17 +00001205/************************************************************************
1206 * *
1207 * Error message callback *
1208 * *
1209 ************************************************************************/
1210
1211static PyObject *libxml_xmlPythonErrorFuncHandler = NULL;
1212static PyObject *libxml_xmlPythonErrorFuncCtxt = NULL;
1213
Daniel Veillarde6227e02003-01-14 11:42:39 +00001214/* helper to build a xmlMalloc'ed string from a format and va_list */
1215static char *
1216libxml_buildMessage(const char *msg, va_list ap)
Daniel Veillardd2379012002-03-15 22:24:56 +00001217{
1218 int size;
1219 int chars;
1220 char *larger;
Daniel Veillarde6227e02003-01-14 11:42:39 +00001221 char *str;
1222
1223 str = (char *) xmlMalloc(150);
1224 if (str == NULL)
1225 return NULL;
1226
1227 size = 150;
1228
1229 while (1) {
1230 chars = vsnprintf(str, size, msg, ap);
1231 if ((chars > -1) && (chars < size))
1232 break;
1233 if (chars > -1)
1234 size += chars + 1;
1235 else
1236 size += 100;
1237 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
1238 xmlFree(str);
1239 return NULL;
1240 }
1241 str = larger;
1242 }
1243
1244 return str;
1245}
1246
1247static void
1248libxml_xmlErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, const char *msg,
1249 ...)
1250{
Daniel Veillardd2379012002-03-15 22:24:56 +00001251 va_list ap;
1252 char *str;
Daniel Veillard5d819032002-02-02 21:49:17 +00001253 PyObject *list;
1254 PyObject *message;
1255 PyObject *result;
1256
1257#ifdef DEBUG_ERROR
1258 printf("libxml_xmlErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
1259#endif
1260
1261
1262 if (libxml_xmlPythonErrorFuncHandler == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001263 va_start(ap, msg);
1264 vfprintf(stdout, msg, ap);
1265 va_end(ap);
Daniel Veillard5d819032002-02-02 21:49:17 +00001266 } else {
Daniel Veillarde6227e02003-01-14 11:42:39 +00001267 va_start(ap, msg);
1268 str = libxml_buildMessage(msg,ap);
1269 va_end(ap);
Daniel Veillard5d819032002-02-02 21:49:17 +00001270
Daniel Veillardd2379012002-03-15 22:24:56 +00001271 list = PyTuple_New(2);
1272 PyTuple_SetItem(list, 0, libxml_xmlPythonErrorFuncCtxt);
1273 Py_XINCREF(libxml_xmlPythonErrorFuncCtxt);
1274 message = libxml_charPtrWrap(str);
1275 PyTuple_SetItem(list, 1, message);
1276 result = PyEval_CallObject(libxml_xmlPythonErrorFuncHandler, list);
1277 Py_XDECREF(list);
1278 Py_XDECREF(result);
Daniel Veillard5d819032002-02-02 21:49:17 +00001279 }
1280}
1281
1282static void
Daniel Veillardd2379012002-03-15 22:24:56 +00001283libxml_xmlErrorInitialize(void)
1284{
Daniel Veillard5d819032002-02-02 21:49:17 +00001285#ifdef DEBUG_ERROR
1286 printf("libxml_xmlErrorInitialize() called\n");
1287#endif
1288 xmlSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
1289}
1290
1291PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001292libxml_xmlRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self,
1293 PyObject * args)
1294{
Daniel Veillard5d819032002-02-02 21:49:17 +00001295 PyObject *py_retval;
1296 PyObject *pyobj_f;
1297 PyObject *pyobj_ctx;
1298
Daniel Veillardd2379012002-03-15 22:24:56 +00001299 if (!PyArg_ParseTuple
1300 (args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f,
1301 &pyobj_ctx))
1302 return (NULL);
Daniel Veillard5d819032002-02-02 21:49:17 +00001303
1304#ifdef DEBUG_ERROR
Daniel Veillardd2379012002-03-15 22:24:56 +00001305 printf("libxml_registerXPathFunction(%p, %p) called\n", pyobj_ctx,
1306 pyobj_f);
Daniel Veillard5d819032002-02-02 21:49:17 +00001307#endif
1308
1309 if (libxml_xmlPythonErrorFuncHandler != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001310 Py_XDECREF(libxml_xmlPythonErrorFuncHandler);
Daniel Veillard5d819032002-02-02 21:49:17 +00001311 }
1312 if (libxml_xmlPythonErrorFuncCtxt != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001313 Py_XDECREF(libxml_xmlPythonErrorFuncCtxt);
Daniel Veillard5d819032002-02-02 21:49:17 +00001314 }
1315
1316 Py_XINCREF(pyobj_ctx);
1317 Py_XINCREF(pyobj_f);
1318
1319 /* TODO: check f is a function ! */
1320 libxml_xmlPythonErrorFuncHandler = pyobj_f;
1321 libxml_xmlPythonErrorFuncCtxt = pyobj_ctx;
1322
1323 py_retval = libxml_intWrap(1);
Daniel Veillardd2379012002-03-15 22:24:56 +00001324 return (py_retval);
Daniel Veillard5d819032002-02-02 21:49:17 +00001325}
Daniel Veillardd2379012002-03-15 22:24:56 +00001326
Daniel Veillarde6227e02003-01-14 11:42:39 +00001327
1328/************************************************************************
1329 * *
1330 * Per parserCtxt error handler *
1331 * *
1332 ************************************************************************/
1333
1334static void
1335libxml_xmlParserCtxtErrorFuncHandler(void *ctxt, const char *msg, ...)
1336{
1337 char *str;
1338 va_list ap;
1339 PyObject *list;
1340 PyObject *message;
1341 PyObject *result;
1342 xmlParserCtxtPyCtxtPtr pyCtxt;
1343
1344#ifdef DEBUG_ERROR
1345 printf("libxml_xmlParserCtxtErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
1346#endif
1347
1348 pyCtxt = (xmlParserCtxtPyCtxtPtr)((xmlParserCtxtPtr)ctxt)->_private;
1349
1350 if (pyCtxt->errorFunc == NULL) {
1351 va_start(ap, msg);
1352 vfprintf(stdout, msg, ap);
1353 va_end(ap);
1354 } else {
1355 va_start(ap, msg);
1356 str = libxml_buildMessage(msg,ap);
1357 va_end(ap);
1358
1359 list = PyTuple_New(2);
1360 PyTuple_SetItem(list, 0, pyCtxt->errorFuncArg);
1361 Py_XINCREF(pyCtxt->errorFuncArg);
1362 message = libxml_charPtrWrap(str);
1363 PyTuple_SetItem(list, 1, message);
1364 result = PyEval_CallObject(pyCtxt->errorFunc, list);
1365 Py_XDECREF(list);
1366 Py_XDECREF(result);
1367 }
1368}
1369
1370PyObject *
1371libxml_xmlSetParserCtxtErrorHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1372{
1373 PyObject *py_retval;
1374 xmlParserCtxtPtr ctxt;
1375 PyObject *pyobj_ctxt;
1376 PyObject *pyobj_f;
1377 PyObject *pyobj_arg;
1378
1379 if (!PyArg_ParseTuple(args, (char *)"OOO:xmlSetParserCtxtErrorHandler",
1380 &pyobj_ctxt, &pyobj_f, &pyobj_arg))
1381 return(NULL);
1382 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1383 if (ctxt->_private == NULL) {
1384 xmlParserCtxtPyCtxt *pyCtxt;
1385
1386 pyCtxt = xmlMalloc(sizeof(xmlParserCtxtPyCtxt));
1387 if (pyCtxt == NULL) {
1388 py_retval = libxml_intWrap(-1);
1389 return(py_retval);
1390 }
1391 memset(pyCtxt,0,sizeof(xmlParserCtxtPyCtxt));
1392 ctxt->_private = pyCtxt;
1393 }
1394 /* TODO: check f is a function ! */
1395 Py_XINCREF(pyobj_f);
1396 ((xmlParserCtxtPyCtxt *)ctxt->_private)->errorFunc = pyobj_f;
1397 Py_XINCREF(pyobj_arg);
1398 ((xmlParserCtxtPyCtxt *)ctxt->_private)->errorFuncArg = pyobj_arg;
1399
1400 ctxt->sax->error = libxml_xmlParserCtxtErrorFuncHandler;
1401 ctxt->vctxt.error = libxml_xmlParserCtxtErrorFuncHandler;
1402
1403 py_retval = libxml_intWrap(1);
1404 return(py_retval);
1405}
1406
1407static void
1408libxml_xmlParserCtxtWarningFuncHandler(void *ctxt, const char *msg, ...)
1409{
1410 char *str;
1411 va_list ap;
1412 PyObject *list;
1413 PyObject *message;
1414 PyObject *result;
1415 xmlParserCtxtPyCtxtPtr pyCtxt;
1416
1417#ifdef DEBUG_ERROR
1418 printf("libxml_xmlParserCtxtWarningFuncHandler(%p, %s, ...) called\n", ctx, msg);
1419#endif
1420
1421 pyCtxt = (xmlParserCtxtPyCtxtPtr)((xmlParserCtxtPtr)ctxt)->_private;
1422
1423 if (pyCtxt->warningFunc == NULL) {
1424 va_start(ap, msg);
1425 vfprintf(stdout, msg, ap);
1426 va_end(ap);
1427 } else {
1428 va_start(ap, msg);
1429 str = libxml_buildMessage(msg,ap);
1430 va_end(ap);
1431
1432 list = PyTuple_New(2);
1433 PyTuple_SetItem(list, 0, pyCtxt->warningFuncArg);
1434 Py_XINCREF(pyCtxt->warningFuncArg);
1435 message = libxml_charPtrWrap(str);
1436 PyTuple_SetItem(list, 1, message);
1437 result = PyEval_CallObject(pyCtxt->warningFunc, list);
1438 Py_XDECREF(list);
1439 Py_XDECREF(result);
1440 }
1441}
1442
1443PyObject *
1444libxml_xmlSetParserCtxtWarningHandler(ATTRIBUTE_UNUSED PyObject *self, PyObject *args)
1445{
1446 PyObject *py_retval;
1447 xmlParserCtxtPtr ctxt;
1448 PyObject *pyobj_ctxt;
1449 PyObject *pyobj_f;
1450 PyObject *pyobj_arg;
1451
1452 if (!PyArg_ParseTuple(args, (char *)"OOO:xmlSetParserCtxtWarningHandler", &pyobj_ctxt, &pyobj_f, &pyobj_arg))
1453 return(NULL);
1454 ctxt = (xmlParserCtxtPtr) PyparserCtxt_Get(pyobj_ctxt);
1455 /* TODO: check f is a function ! */
1456 Py_XINCREF(pyobj_f);
1457 ((xmlParserCtxtPyCtxt *)ctxt->_private)->warningFunc = pyobj_f;
1458 Py_XINCREF(pyobj_arg);
1459 ((xmlParserCtxtPyCtxt *)ctxt->_private)->warningFuncArg = pyobj_arg;
1460
1461 ctxt->sax->warning = libxml_xmlParserCtxtWarningFuncHandler;
1462 ctxt->vctxt.warning = libxml_xmlParserCtxtWarningFuncHandler;
1463
1464 py_retval = libxml_intWrap(1);
1465 return(py_retval);
1466}
1467
Daniel Veillarda7340c82002-02-01 17:56:45 +00001468/************************************************************************
1469 * *
1470 * XPath extensions *
1471 * *
1472 ************************************************************************/
1473
1474static int libxml_xpathCallbacksInitialized = 0;
1475
1476typedef struct libxml_xpathCallback {
1477 xmlXPathContextPtr ctx;
1478 xmlChar *name;
1479 xmlChar *ns_uri;
1480 PyObject *function;
1481} libxml_xpathCallback, *libxml_xpathCallbackPtr;
1482static libxml_xpathCallback libxml_xpathCallbacks[10];
1483static int libxml_xpathCallbacksNb = 0;
1484static int libxml_xpathCallbacksMax = 10;
1485
Daniel Veillarda7340c82002-02-01 17:56:45 +00001486static void
Daniel Veillardd2379012002-03-15 22:24:56 +00001487libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs)
1488{
Daniel Veillarda7340c82002-02-01 17:56:45 +00001489 PyObject *list, *cur, *result;
1490 xmlXPathObjectPtr obj;
Daniel Veillard70cab352002-02-06 16:06:58 +00001491 xmlXPathContextPtr rctxt;
1492 PyObject *current_function = NULL;
1493 const xmlChar *name;
1494 const xmlChar *ns_uri;
Daniel Veillarda7340c82002-02-01 17:56:45 +00001495 int i;
1496
Daniel Veillard70cab352002-02-06 16:06:58 +00001497 if (ctxt == NULL)
Daniel Veillardd2379012002-03-15 22:24:56 +00001498 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00001499 rctxt = ctxt->context;
1500 if (rctxt == NULL)
Daniel Veillardd2379012002-03-15 22:24:56 +00001501 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00001502 name = rctxt->function;
1503 ns_uri = rctxt->functionURI;
Daniel Veillarda7340c82002-02-01 17:56:45 +00001504#ifdef DEBUG_XPATH
Daniel Veillardd2379012002-03-15 22:24:56 +00001505 printf("libxml_xmlXPathFuncCallback called name %s URI %s\n", name,
1506 ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001507#endif
1508
Daniel Veillard70cab352002-02-06 16:06:58 +00001509 /*
1510 * Find the function, it should be there it was there at lookup
1511 */
Daniel Veillardd2379012002-03-15 22:24:56 +00001512 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
1513 if ( /* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */
1514 (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) &&
1515 (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) {
1516 current_function = libxml_xpathCallbacks[i].function;
1517 }
Daniel Veillard70cab352002-02-06 16:06:58 +00001518 }
1519 if (current_function == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001520 printf
1521 ("libxml_xmlXPathFuncCallback: internal error %s not found !\n",
1522 name);
1523 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00001524 }
1525
Daniel Veillardc575b992002-02-08 13:28:40 +00001526 list = PyTuple_New(nargs + 1);
1527 PyTuple_SetItem(list, 0, libxml_xmlXPathParserContextPtrWrap(ctxt));
Daniel Veillardd2379012002-03-15 22:24:56 +00001528 for (i = 0; i < nargs; i++) {
1529 obj = valuePop(ctxt);
1530 cur = libxml_xmlXPathObjectPtrWrap(obj);
1531 PyTuple_SetItem(list, i + 1, cur);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001532 }
1533 result = PyEval_CallObject(current_function, list);
1534 Py_DECREF(list);
1535
1536 obj = libxml_xmlXPathObjectPtrConvert(result);
1537 valuePush(ctxt, obj);
1538}
1539
1540static xmlXPathFunction
Daniel Veillardd2379012002-03-15 22:24:56 +00001541libxml_xmlXPathFuncLookupFunc(void *ctxt, const xmlChar * name,
1542 const xmlChar * ns_uri)
1543{
Daniel Veillarda7340c82002-02-01 17:56:45 +00001544 int i;
Daniel Veillardd2379012002-03-15 22:24:56 +00001545
Daniel Veillarda7340c82002-02-01 17:56:45 +00001546#ifdef DEBUG_XPATH
1547 printf("libxml_xmlXPathFuncLookupFunc(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001548 ctxt, name, ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001549#endif
Daniel Veillard70cab352002-02-06 16:06:58 +00001550 /*
1551 * This is called once only. The address is then stored in the
1552 * XPath expression evaluation, the proper object to call can
1553 * then still be found using the execution context function
1554 * and functionURI fields.
1555 */
Daniel Veillardd2379012002-03-15 22:24:56 +00001556 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
1557 if ((ctxt == libxml_xpathCallbacks[i].ctx) &&
1558 (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) &&
1559 (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) {
1560 return (libxml_xmlXPathFuncCallback);
1561 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00001562 }
Daniel Veillardd2379012002-03-15 22:24:56 +00001563 return (NULL);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001564}
1565
1566static void
Daniel Veillardd2379012002-03-15 22:24:56 +00001567libxml_xpathCallbacksInitialize(void)
1568{
Daniel Veillarda7340c82002-02-01 17:56:45 +00001569 int i;
1570
1571 if (libxml_xpathCallbacksInitialized != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00001572 return;
Daniel Veillarda7340c82002-02-01 17:56:45 +00001573
1574#ifdef DEBUG_XPATH
1575 printf("libxml_xpathCallbacksInitialized called\n");
1576#endif
1577
Daniel Veillardd2379012002-03-15 22:24:56 +00001578 for (i = 0; i < 10; i++) {
1579 libxml_xpathCallbacks[i].ctx = NULL;
1580 libxml_xpathCallbacks[i].name = NULL;
1581 libxml_xpathCallbacks[i].ns_uri = NULL;
1582 libxml_xpathCallbacks[i].function = NULL;
Daniel Veillarda7340c82002-02-01 17:56:45 +00001583 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00001584 libxml_xpathCallbacksInitialized = 1;
1585}
1586
1587PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001588libxml_xmlRegisterXPathFunction(ATTRIBUTE_UNUSED PyObject * self,
1589 PyObject * args)
1590{
Daniel Veillarda7340c82002-02-01 17:56:45 +00001591 PyObject *py_retval;
1592 int c_retval = 0;
1593 xmlChar *name;
1594 xmlChar *ns_uri;
1595 xmlXPathContextPtr ctx;
1596 PyObject *pyobj_ctx;
1597 PyObject *pyobj_f;
1598 int i;
1599
Daniel Veillardd2379012002-03-15 22:24:56 +00001600 if (!PyArg_ParseTuple
1601 (args, (char *) "OszO:registerXPathFunction", &pyobj_ctx, &name,
1602 &ns_uri, &pyobj_f))
1603 return (NULL);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001604
1605 ctx = (xmlXPathContextPtr) PyxmlXPathContext_Get(pyobj_ctx);
1606 if (libxml_xpathCallbacksInitialized == 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00001607 libxml_xpathCallbacksInitialize();
Daniel Veillarda7340c82002-02-01 17:56:45 +00001608 xmlXPathRegisterFuncLookup(ctx, libxml_xmlXPathFuncLookupFunc, ctx);
1609
1610 if ((pyobj_ctx == NULL) || (name == NULL) || (pyobj_f == NULL)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001611 py_retval = libxml_intWrap(-1);
1612 return (py_retval);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001613 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00001614#ifdef DEBUG_XPATH
1615 printf("libxml_registerXPathFunction(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001616 ctx, name, ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001617#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001618 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
1619 if ((ctx == libxml_xpathCallbacks[i].ctx) &&
1620 (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) &&
1621 (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) {
1622 Py_XINCREF(pyobj_f);
1623 Py_XDECREF(libxml_xpathCallbacks[i].function);
1624 libxml_xpathCallbacks[i].function = pyobj_f;
1625 c_retval = 1;
1626 goto done;
1627 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00001628 }
1629 if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksMax) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001630 printf("libxml_registerXPathFunction() table full\n");
Daniel Veillarda7340c82002-02-01 17:56:45 +00001631 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00001632 i = libxml_xpathCallbacksNb++;
1633 Py_XINCREF(pyobj_f);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001634 libxml_xpathCallbacks[i].ctx = ctx;
1635 libxml_xpathCallbacks[i].name = xmlStrdup(name);
1636 libxml_xpathCallbacks[i].ns_uri = xmlStrdup(ns_uri);
Daniel Veillardd2379012002-03-15 22:24:56 +00001637 libxml_xpathCallbacks[i].function = pyobj_f;
1638 c_retval = 1;
Daniel Veillarda7340c82002-02-01 17:56:45 +00001639 }
Daniel Veillardd2379012002-03-15 22:24:56 +00001640 done:
Daniel Veillarda7340c82002-02-01 17:56:45 +00001641 py_retval = libxml_intWrap((int) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00001642 return (py_retval);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001643}
1644
Daniel Veillard1971ee22002-01-31 20:29:19 +00001645/************************************************************************
1646 * *
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001647 * Global properties access *
1648 * *
1649 ************************************************************************/
1650static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001651libxml_name(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001652{
1653 PyObject *resultobj, *obj;
1654 xmlNodePtr cur;
1655 const xmlChar *res;
1656
Daniel Veillardd2379012002-03-15 22:24:56 +00001657 if (!PyArg_ParseTuple(args, (char *) "O:name", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001658 return NULL;
1659 cur = PyxmlNode_Get(obj);
1660
1661#ifdef DEBUG
1662 printf("libxml_name: cur = %p type %d\n", cur, cur->type);
1663#endif
1664
Daniel Veillardd2379012002-03-15 22:24:56 +00001665 switch (cur->type) {
1666 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001667#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001668 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001669#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001670 case XML_HTML_DOCUMENT_NODE:{
1671 xmlDocPtr doc = (xmlDocPtr) cur;
1672
1673 res = doc->URL;
1674 break;
1675 }
1676 case XML_ATTRIBUTE_NODE:{
1677 xmlAttrPtr attr = (xmlAttrPtr) cur;
1678
1679 res = attr->name;
1680 break;
1681 }
1682 case XML_NAMESPACE_DECL:{
1683 xmlNsPtr ns = (xmlNsPtr) cur;
1684
1685 res = ns->prefix;
1686 break;
1687 }
1688 default:
1689 res = cur->name;
1690 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001691 }
Daniel Veillard1971ee22002-01-31 20:29:19 +00001692 resultobj = libxml_constxmlCharPtrWrap(res);
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001693
1694 return resultobj;
1695}
1696
1697static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001698libxml_doc(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001699{
1700 PyObject *resultobj, *obj;
1701 xmlNodePtr cur;
1702 xmlDocPtr res;
1703
Daniel Veillardd2379012002-03-15 22:24:56 +00001704 if (!PyArg_ParseTuple(args, (char *) "O:doc", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001705 return NULL;
1706 cur = PyxmlNode_Get(obj);
1707
1708#ifdef DEBUG
1709 printf("libxml_doc: cur = %p\n", cur);
1710#endif
1711
Daniel Veillardd2379012002-03-15 22:24:56 +00001712 switch (cur->type) {
1713 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001714#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001715 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001716#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001717 case XML_HTML_DOCUMENT_NODE:
1718 res = NULL;
1719 break;
1720 case XML_ATTRIBUTE_NODE:{
1721 xmlAttrPtr attr = (xmlAttrPtr) cur;
1722
1723 res = attr->doc;
1724 break;
1725 }
1726 case XML_NAMESPACE_DECL:
1727 res = NULL;
1728 break;
1729 default:
1730 res = cur->doc;
1731 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001732 }
1733 resultobj = libxml_xmlDocPtrWrap(res);
1734 return resultobj;
1735}
1736
1737static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001738libxml_properties(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001739{
1740 PyObject *resultobj, *obj;
1741 xmlNodePtr cur = NULL;
1742 xmlAttrPtr res;
1743
Daniel Veillardd2379012002-03-15 22:24:56 +00001744 if (!PyArg_ParseTuple(args, (char *) "O:properties", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001745 return NULL;
1746 cur = PyxmlNode_Get(obj);
1747 if (cur->type == XML_ELEMENT_NODE)
Daniel Veillardd2379012002-03-15 22:24:56 +00001748 res = cur->properties;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001749 else
Daniel Veillardd2379012002-03-15 22:24:56 +00001750 res = NULL;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001751 resultobj = libxml_xmlAttrPtrWrap(res);
1752 return resultobj;
1753}
1754
1755static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001756libxml_next(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001757{
1758 PyObject *resultobj, *obj;
1759 xmlNodePtr cur;
1760 xmlNodePtr res;
1761
Daniel Veillardd2379012002-03-15 22:24:56 +00001762 if (!PyArg_ParseTuple(args, (char *) "O:next", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001763 return NULL;
1764 cur = PyxmlNode_Get(obj);
1765
1766#ifdef DEBUG
1767 printf("libxml_next: cur = %p\n", cur);
1768#endif
1769
Daniel Veillardd2379012002-03-15 22:24:56 +00001770 switch (cur->type) {
1771 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001772#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001773 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001774#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001775 case XML_HTML_DOCUMENT_NODE:
1776 res = NULL;
1777 break;
1778 case XML_ATTRIBUTE_NODE:{
1779 xmlAttrPtr attr = (xmlAttrPtr) cur;
1780
1781 res = (xmlNodePtr) attr->next;
1782 break;
1783 }
1784 case XML_NAMESPACE_DECL:{
1785 xmlNsPtr ns = (xmlNsPtr) cur;
1786
1787 res = (xmlNodePtr) ns->next;
1788 break;
1789 }
1790 default:
1791 res = cur->next;
1792 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001793
1794 }
1795 resultobj = libxml_xmlNodePtrWrap(res);
1796 return resultobj;
1797}
1798
1799static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001800libxml_prev(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001801{
1802 PyObject *resultobj, *obj;
1803 xmlNodePtr cur;
1804 xmlNodePtr res;
1805
Daniel Veillardd2379012002-03-15 22:24:56 +00001806 if (!PyArg_ParseTuple(args, (char *) "O:prev", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001807 return NULL;
1808 cur = PyxmlNode_Get(obj);
1809
1810#ifdef DEBUG
1811 printf("libxml_prev: cur = %p\n", cur);
1812#endif
1813
Daniel Veillardd2379012002-03-15 22:24:56 +00001814 switch (cur->type) {
1815 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001816#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001817 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001818#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001819 case XML_HTML_DOCUMENT_NODE:
1820 res = NULL;
1821 break;
1822 case XML_ATTRIBUTE_NODE:{
1823 xmlAttrPtr attr = (xmlAttrPtr) cur;
1824
Daniel Veillardfaa35ff2002-11-24 13:53:43 +00001825 res = (xmlNodePtr) attr->prev;
Daniel Veillardd2379012002-03-15 22:24:56 +00001826 }
1827 case XML_NAMESPACE_DECL:
1828 res = NULL;
1829 break;
1830 default:
Daniel Veillardfaa35ff2002-11-24 13:53:43 +00001831 res = cur->prev;
Daniel Veillardd2379012002-03-15 22:24:56 +00001832 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001833 }
1834 resultobj = libxml_xmlNodePtrWrap(res);
1835 return resultobj;
1836}
1837
1838static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001839libxml_children(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001840{
1841 PyObject *resultobj, *obj;
1842 xmlNodePtr cur;
1843 xmlNodePtr res;
1844
Daniel Veillardd2379012002-03-15 22:24:56 +00001845 if (!PyArg_ParseTuple(args, (char *) "O:children", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001846 return NULL;
1847 cur = PyxmlNode_Get(obj);
1848
1849#ifdef DEBUG
1850 printf("libxml_children: cur = %p\n", cur);
1851#endif
1852
Daniel Veillardd2379012002-03-15 22:24:56 +00001853 switch (cur->type) {
1854 case XML_ELEMENT_NODE:
1855 case XML_ENTITY_REF_NODE:
1856 case XML_ENTITY_NODE:
1857 case XML_PI_NODE:
1858 case XML_COMMENT_NODE:
1859 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001860#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001861 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001862#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001863 case XML_HTML_DOCUMENT_NODE:
1864 case XML_DTD_NODE:
1865 res = cur->children;
1866 break;
1867 case XML_ATTRIBUTE_NODE:{
1868 xmlAttrPtr attr = (xmlAttrPtr) cur;
1869
1870 res = attr->children;
1871 break;
1872 }
1873 default:
1874 res = NULL;
1875 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001876 }
1877 resultobj = libxml_xmlNodePtrWrap(res);
1878 return resultobj;
1879}
1880
1881static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001882libxml_last(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001883{
1884 PyObject *resultobj, *obj;
1885 xmlNodePtr cur;
1886 xmlNodePtr res;
1887
Daniel Veillardd2379012002-03-15 22:24:56 +00001888 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001889 return NULL;
1890 cur = PyxmlNode_Get(obj);
1891
1892#ifdef DEBUG
1893 printf("libxml_last: cur = %p\n", cur);
1894#endif
1895
Daniel Veillardd2379012002-03-15 22:24:56 +00001896 switch (cur->type) {
1897 case XML_ELEMENT_NODE:
1898 case XML_ENTITY_REF_NODE:
1899 case XML_ENTITY_NODE:
1900 case XML_PI_NODE:
1901 case XML_COMMENT_NODE:
1902 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001903#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001904 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001905#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001906 case XML_HTML_DOCUMENT_NODE:
1907 case XML_DTD_NODE:
1908 res = cur->last;
1909 break;
1910 case XML_ATTRIBUTE_NODE:{
1911 xmlAttrPtr attr = (xmlAttrPtr) cur;
1912
1913 res = attr->last;
1914 }
1915 default:
1916 res = NULL;
1917 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001918 }
1919 resultobj = libxml_xmlNodePtrWrap(res);
1920 return resultobj;
1921}
1922
1923static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001924libxml_parent(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001925{
1926 PyObject *resultobj, *obj;
1927 xmlNodePtr cur;
1928 xmlNodePtr res;
1929
Daniel Veillardd2379012002-03-15 22:24:56 +00001930 if (!PyArg_ParseTuple(args, (char *) "O:parent", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001931 return NULL;
1932 cur = PyxmlNode_Get(obj);
1933
1934#ifdef DEBUG
1935 printf("libxml_parent: cur = %p\n", cur);
1936#endif
1937
Daniel Veillardd2379012002-03-15 22:24:56 +00001938 switch (cur->type) {
1939 case XML_DOCUMENT_NODE:
1940 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001941#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001942 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001943#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001944 res = NULL;
1945 break;
1946 case XML_ATTRIBUTE_NODE:{
1947 xmlAttrPtr attr = (xmlAttrPtr) cur;
1948
1949 res = attr->parent;
1950 }
1951 case XML_ENTITY_DECL:
1952 case XML_NAMESPACE_DECL:
1953 case XML_XINCLUDE_START:
1954 case XML_XINCLUDE_END:
1955 res = NULL;
1956 break;
1957 default:
1958 res = cur->parent;
1959 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001960 }
1961 resultobj = libxml_xmlNodePtrWrap(res);
1962 return resultobj;
1963}
1964
1965static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001966libxml_type(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001967{
1968 PyObject *resultobj, *obj;
1969 xmlNodePtr cur;
Daniel Veillardd2379012002-03-15 22:24:56 +00001970 const xmlChar *res = NULL;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001971
Daniel Veillardd2379012002-03-15 22:24:56 +00001972 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001973 return NULL;
1974 cur = PyxmlNode_Get(obj);
1975
1976#ifdef DEBUG
1977 printf("libxml_type: cur = %p\n", cur);
1978#endif
1979
Daniel Veillardd2379012002-03-15 22:24:56 +00001980 switch (cur->type) {
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001981 case XML_ELEMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001982 res = (const xmlChar *) "element";
1983 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001984 case XML_ATTRIBUTE_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001985 res = (const xmlChar *) "attribute";
1986 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001987 case XML_TEXT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001988 res = (const xmlChar *) "text";
1989 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001990 case XML_CDATA_SECTION_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001991 res = (const xmlChar *) "cdata";
1992 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001993 case XML_ENTITY_REF_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001994 res = (const xmlChar *) "entity_ref";
1995 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001996 case XML_ENTITY_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001997 res = (const xmlChar *) "entity";
1998 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001999 case XML_PI_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002000 res = (const xmlChar *) "pi";
2001 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002002 case XML_COMMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002003 res = (const xmlChar *) "comment";
2004 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002005 case XML_DOCUMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002006 res = (const xmlChar *) "document_xml";
2007 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002008 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002009 res = (const xmlChar *) "doctype";
2010 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002011 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002012 res = (const xmlChar *) "fragment";
2013 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002014 case XML_NOTATION_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002015 res = (const xmlChar *) "notation";
2016 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002017 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002018 res = (const xmlChar *) "document_html";
2019 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002020 case XML_DTD_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00002021 res = (const xmlChar *) "dtd";
2022 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002023 case XML_ELEMENT_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002024 res = (const xmlChar *) "elem_decl";
2025 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002026 case XML_ATTRIBUTE_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002027 res = (const xmlChar *) "attribute_decl";
2028 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002029 case XML_ENTITY_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002030 res = (const xmlChar *) "entity_decl";
2031 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002032 case XML_NAMESPACE_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00002033 res = (const xmlChar *) "namespace";
2034 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002035 case XML_XINCLUDE_START:
Daniel Veillardd2379012002-03-15 22:24:56 +00002036 res = (const xmlChar *) "xinclude_start";
2037 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002038 case XML_XINCLUDE_END:
Daniel Veillardd2379012002-03-15 22:24:56 +00002039 res = (const xmlChar *) "xinclude_end";
2040 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002041#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00002042 case XML_DOCB_DOCUMENT_NODE:
2043 res = (const xmlChar *) "document_docbook";
2044 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002045#endif
2046 }
2047#ifdef DEBUG
2048 printf("libxml_type: cur = %p: %s\n", cur, res);
2049#endif
2050
Daniel Veillard1971ee22002-01-31 20:29:19 +00002051 resultobj = libxml_constxmlCharPtrWrap(res);
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002052 return resultobj;
2053}
2054
2055/************************************************************************
2056 * *
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002057 * Specific accessor functions *
2058 * *
2059 ************************************************************************/
2060PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002061libxml_xmlNodeGetNsDefs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2062{
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002063 PyObject *py_retval;
2064 xmlNsPtr c_retval;
2065 xmlNodePtr node;
2066 PyObject *pyobj_node;
2067
Daniel Veillardd2379012002-03-15 22:24:56 +00002068 if (!PyArg_ParseTuple
2069 (args, (char *) "O:xmlNodeGetNsDefs", &pyobj_node))
2070 return (NULL);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002071 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2072
2073 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002074 Py_INCREF(Py_None);
2075 return (Py_None);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002076 }
2077 c_retval = node->nsDef;
2078 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00002079 return (py_retval);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002080}
2081
2082PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002083libxml_xmlNodeGetNs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2084{
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002085 PyObject *py_retval;
2086 xmlNsPtr c_retval;
2087 xmlNodePtr node;
2088 PyObject *pyobj_node;
2089
Daniel Veillardd2379012002-03-15 22:24:56 +00002090 if (!PyArg_ParseTuple(args, (char *) "O:xmlNodeGetNs", &pyobj_node))
2091 return (NULL);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002092 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2093
2094 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002095 Py_INCREF(Py_None);
2096 return (Py_None);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002097 }
2098 c_retval = node->ns;
2099 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00002100 return (py_retval);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002101}
2102
2103/************************************************************************
2104 * *
Daniel Veillard1e774382002-03-06 17:35:40 +00002105 * Serialization front-end *
2106 * *
2107 ************************************************************************/
2108
Daniel Veillardd2379012002-03-15 22:24:56 +00002109static PyObject *
2110libxml_serializeNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2111{
Daniel Veillard1e774382002-03-06 17:35:40 +00002112 PyObject *py_retval = NULL;
2113 xmlChar *c_retval;
2114 PyObject *pyobj_node;
2115 xmlNodePtr node;
2116 xmlDocPtr doc;
Daniel Veillardd2379012002-03-15 22:24:56 +00002117 const char *encoding;
Daniel Veillard1e774382002-03-06 17:35:40 +00002118 int format;
2119 int len;
2120
Daniel Veillardd2379012002-03-15 22:24:56 +00002121 if (!PyArg_ParseTuple(args, (char *) "Ozi:serializeNode", &pyobj_node,
2122 &encoding, &format))
2123 return (NULL);
Daniel Veillard1e774382002-03-06 17:35:40 +00002124 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2125
2126 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002127 Py_INCREF(Py_None);
2128 return (Py_None);
Daniel Veillard1e774382002-03-06 17:35:40 +00002129 }
2130 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002131 doc = (xmlDocPtr) node;
2132 xmlDocDumpFormatMemoryEnc(doc, &c_retval, &len,
2133 (const char *) encoding, format);
2134 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard1e774382002-03-06 17:35:40 +00002135 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002136 xmlOutputBufferPtr buf;
2137 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00002138
Daniel Veillardd2379012002-03-15 22:24:56 +00002139 doc = (xmlDocPtr) node;
2140 if (encoding != NULL)
2141 htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
2142 encoding = (const char *) htmlGetMetaEncoding(doc);
Daniel Veillard1e774382002-03-06 17:35:40 +00002143
Daniel Veillardd2379012002-03-15 22:24:56 +00002144 if (encoding != NULL) {
2145 handler = xmlFindCharEncodingHandler(encoding);
2146 if (handler == NULL) {
2147 Py_INCREF(Py_None);
2148 return (Py_None);
2149 }
2150 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002151
Daniel Veillardd2379012002-03-15 22:24:56 +00002152 /*
2153 * Fallback to HTML or ASCII when the encoding is unspecified
2154 */
2155 if (handler == NULL)
2156 handler = xmlFindCharEncodingHandler("HTML");
2157 if (handler == NULL)
2158 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00002159
Daniel Veillardd2379012002-03-15 22:24:56 +00002160 buf = xmlAllocOutputBuffer(handler);
2161 if (buf == NULL) {
2162 Py_INCREF(Py_None);
2163 return (Py_None);
2164 }
2165 htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
2166 xmlOutputBufferFlush(buf);
2167 if (buf->conv != NULL) {
2168 len = buf->conv->use;
2169 c_retval = buf->conv->content;
2170 buf->conv->content = NULL;
2171 } else {
2172 len = buf->buffer->use;
2173 c_retval = buf->buffer->content;
2174 buf->buffer->content = NULL;
2175 }
2176 (void) xmlOutputBufferClose(buf);
2177 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard1e774382002-03-06 17:35:40 +00002178 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00002179 doc = node->doc;
Daniel Veillarda8c0adb2002-11-17 22:37:35 +00002180 if ((doc == NULL) || (doc->type == XML_DOCUMENT_NODE)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002181 xmlOutputBufferPtr buf;
2182 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00002183
Daniel Veillardd2379012002-03-15 22:24:56 +00002184 if (encoding != NULL) {
2185 handler = xmlFindCharEncodingHandler(encoding);
2186 if (handler == NULL) {
2187 Py_INCREF(Py_None);
2188 return (Py_None);
2189 }
2190 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002191
Daniel Veillardd2379012002-03-15 22:24:56 +00002192 buf = xmlAllocOutputBuffer(handler);
2193 if (buf == NULL) {
2194 Py_INCREF(Py_None);
2195 return (Py_None);
2196 }
2197 xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
2198 xmlOutputBufferFlush(buf);
2199 if (buf->conv != NULL) {
2200 len = buf->conv->use;
2201 c_retval = buf->conv->content;
2202 buf->conv->content = NULL;
2203 } else {
2204 len = buf->buffer->use;
2205 c_retval = buf->buffer->content;
2206 buf->buffer->content = NULL;
2207 }
2208 (void) xmlOutputBufferClose(buf);
2209 py_retval = libxml_charPtrWrap((char *) c_retval);
2210 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2211 xmlOutputBufferPtr buf;
2212 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00002213
Daniel Veillardd2379012002-03-15 22:24:56 +00002214 if (encoding != NULL)
2215 htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
2216 encoding = (const char *) htmlGetMetaEncoding(doc);
2217 if (encoding != NULL) {
2218 handler = xmlFindCharEncodingHandler(encoding);
2219 if (handler == NULL) {
2220 Py_INCREF(Py_None);
2221 return (Py_None);
2222 }
2223 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002224
Daniel Veillardd2379012002-03-15 22:24:56 +00002225 /*
2226 * Fallback to HTML or ASCII when the encoding is unspecified
2227 */
2228 if (handler == NULL)
2229 handler = xmlFindCharEncodingHandler("HTML");
2230 if (handler == NULL)
2231 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00002232
Daniel Veillardd2379012002-03-15 22:24:56 +00002233 buf = xmlAllocOutputBuffer(handler);
2234 if (buf == NULL) {
2235 Py_INCREF(Py_None);
2236 return (Py_None);
2237 }
2238 htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
2239 xmlOutputBufferFlush(buf);
2240 if (buf->conv != NULL) {
2241 len = buf->conv->use;
2242 c_retval = buf->conv->content;
2243 buf->conv->content = NULL;
2244 } else {
2245 len = buf->buffer->use;
2246 c_retval = buf->buffer->content;
2247 buf->buffer->content = NULL;
2248 }
2249 (void) xmlOutputBufferClose(buf);
2250 py_retval = libxml_charPtrWrap((char *) c_retval);
2251 } else {
2252 Py_INCREF(Py_None);
2253 return (Py_None);
2254 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002255 }
Daniel Veillardd2379012002-03-15 22:24:56 +00002256 return (py_retval);
Daniel Veillard1e774382002-03-06 17:35:40 +00002257}
2258
Daniel Veillardd2379012002-03-15 22:24:56 +00002259static PyObject *
2260libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2261{
Daniel Veillard1e774382002-03-06 17:35:40 +00002262 PyObject *py_file = NULL;
2263 FILE *output;
2264 PyObject *pyobj_node;
2265 xmlNodePtr node;
2266 xmlDocPtr doc;
Daniel Veillardd2379012002-03-15 22:24:56 +00002267 const char *encoding;
Daniel Veillard1e774382002-03-06 17:35:40 +00002268 int format;
2269 int len;
2270 xmlOutputBufferPtr buf;
2271 xmlCharEncodingHandlerPtr handler = NULL;
2272
Daniel Veillardd2379012002-03-15 22:24:56 +00002273 if (!PyArg_ParseTuple(args, (char *) "OOzi:serializeNode", &pyobj_node,
2274 &py_file, &encoding, &format))
2275 return (NULL);
Daniel Veillard1e774382002-03-06 17:35:40 +00002276 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2277
2278 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002279 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002280 }
2281 if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002282 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002283 }
2284 output = PyFile_AsFile(py_file);
2285 if (output == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002286 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002287 }
2288
2289 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002290 doc = (xmlDocPtr) node;
Daniel Veillard1e774382002-03-06 17:35:40 +00002291 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002292 doc = (xmlDocPtr) node;
Daniel Veillard1e774382002-03-06 17:35:40 +00002293 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00002294 doc = node->doc;
Daniel Veillard1e774382002-03-06 17:35:40 +00002295 }
2296 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002297 if (encoding == NULL)
2298 encoding = (const char *) htmlGetMetaEncoding(doc);
Daniel Veillard1e774382002-03-06 17:35:40 +00002299 }
2300 if (encoding != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002301 handler = xmlFindCharEncodingHandler(encoding);
2302 if (handler == NULL) {
2303 return (PyInt_FromLong((long) -1));
2304 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002305 }
2306 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002307 if (handler == NULL)
2308 handler = xmlFindCharEncodingHandler("HTML");
2309 if (handler == NULL)
2310 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00002311 }
2312
2313 buf = xmlOutputBufferCreateFile(output, handler);
2314 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002315 len = xmlSaveFormatFileTo(buf, doc, encoding, format);
Daniel Veillard1e774382002-03-06 17:35:40 +00002316 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002317 htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
2318 len = xmlOutputBufferClose(buf);
Daniel Veillard1e774382002-03-06 17:35:40 +00002319 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002320 htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
2321 len = xmlOutputBufferClose(buf);
Daniel Veillard1e774382002-03-06 17:35:40 +00002322 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00002323 xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
2324 len = xmlOutputBufferClose(buf);
Daniel Veillard1e774382002-03-06 17:35:40 +00002325 }
Daniel Veillardd2379012002-03-15 22:24:56 +00002326 return (PyInt_FromLong((long) len));
Daniel Veillard1e774382002-03-06 17:35:40 +00002327}
2328
2329/************************************************************************
2330 * *
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002331 * Extra stuff *
2332 * *
2333 ************************************************************************/
2334PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002335libxml_xmlNewNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2336{
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002337 PyObject *py_retval;
Daniel Veillardd2379012002-03-15 22:24:56 +00002338 xmlChar *name;
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002339 xmlNodePtr node;
2340
Daniel Veillardd2379012002-03-15 22:24:56 +00002341 if (!PyArg_ParseTuple(args, (char *) "s:xmlNewNode", &name))
2342 return (NULL);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002343 node = (xmlNodePtr) xmlNewNode(NULL, name);
Daniel Veillardd2379012002-03-15 22:24:56 +00002344 printf("NewNode: %s : %p\n", name, (void *) node);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002345
2346 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002347 Py_INCREF(Py_None);
2348 return (Py_None);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002349 }
2350 py_retval = libxml_xmlNodePtrWrap(node);
Daniel Veillardd2379012002-03-15 22:24:56 +00002351 return (py_retval);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002352}
2353
2354/************************************************************************
2355 * *
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002356 * The registration stuff *
2357 * *
2358 ************************************************************************/
2359static PyMethodDef libxmlMethods[] = {
Daniel Veillard96fe0952002-01-30 20:52:23 +00002360#include "libxml2-export.c"
Daniel Veillardd2379012002-03-15 22:24:56 +00002361 {(char *) "name", libxml_name, METH_VARARGS, NULL},
2362 {(char *) "children", libxml_children, METH_VARARGS, NULL},
2363 {(char *) "properties", libxml_properties, METH_VARARGS, NULL},
2364 {(char *) "last", libxml_last, METH_VARARGS, NULL},
2365 {(char *) "prev", libxml_prev, METH_VARARGS, NULL},
2366 {(char *) "next", libxml_next, METH_VARARGS, NULL},
2367 {(char *) "parent", libxml_parent, METH_VARARGS, NULL},
2368 {(char *) "type", libxml_type, METH_VARARGS, NULL},
2369 {(char *) "doc", libxml_doc, METH_VARARGS, NULL},
2370 {(char *) "xmlNewNode", libxml_xmlNewNode, METH_VARARGS, NULL},
2371 {(char *) "serializeNode", libxml_serializeNode, METH_VARARGS, NULL},
2372 {(char *) "saveNodeTo", libxml_saveNodeTo, METH_VARARGS, NULL},
Daniel Veillardc6d4a932002-09-12 15:00:57 +00002373 {(char *) "outputBufferCreate", libxml_xmlCreateOutputBuffer, METH_VARARGS, NULL},
2374 {(char *) "inputBufferCreate", libxml_xmlCreateInputBuffer, METH_VARARGS, NULL},
2375 {(char *) "setEntityLoader", libxml_xmlSetEntityLoader, METH_VARARGS, NULL},
Daniel Veillard3e20a292003-01-10 13:14:40 +00002376 {(char *)"xmlRegisterErrorHandler", libxml_xmlRegisterErrorHandler, METH_VARARGS, NULL },
Daniel Veillarde6227e02003-01-14 11:42:39 +00002377 {(char *)"xmlSetParserCtxtErrorHandler", libxml_xmlSetParserCtxtErrorHandler, METH_VARARGS, NULL },
2378 {(char *)"xmlSetParserCtxtWarningHandler", libxml_xmlSetParserCtxtWarningHandler, METH_VARARGS, NULL },
2379 {(char *)"xmlFreeParserCtxt", libxml_xmlFreeParserCtxt, METH_VARARGS, NULL },
Daniel Veillardd2379012002-03-15 22:24:56 +00002380 {NULL, NULL, 0, NULL}
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002381};
2382
Daniel Veillard0fea6f42002-02-22 22:51:13 +00002383#ifdef MERGED_MODULES
Daniel Veillard6361da02002-02-23 10:10:33 +00002384extern void initlibxsltmod(void);
Daniel Veillard0fea6f42002-02-22 22:51:13 +00002385#endif
2386
Daniel Veillardd2379012002-03-15 22:24:56 +00002387void
2388initlibxml2mod(void)
2389{
Daniel Veillardaf43f632002-03-08 15:05:20 +00002390 static int initialized = 0;
Daniel Veillard3ce52572002-02-03 15:08:05 +00002391 PyObject *m;
Daniel Veillardaf43f632002-03-08 15:05:20 +00002392
2393 if (initialized != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00002394 return;
Daniel Veillardc6d4a932002-09-12 15:00:57 +00002395 xmlRegisterDefaultOutputCallbacks();
2396 xmlRegisterDefaultInputCallbacks();
Daniel Veillardd2379012002-03-15 22:24:56 +00002397 m = Py_InitModule((char *) "libxml2mod", libxmlMethods);
Daniel Veillardaf43f632002-03-08 15:05:20 +00002398 initialized = 1;
Daniel Veillard5d819032002-02-02 21:49:17 +00002399 libxml_xmlErrorInitialize();
Daniel Veillard6361da02002-02-23 10:10:33 +00002400
Daniel Veillard0fea6f42002-02-22 22:51:13 +00002401#ifdef MERGED_MODULES
2402 initlibxsltmod();
2403#endif
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002404}