blob: b67c4ebe6211f3c0a9acf19d3dc3916079c6473d [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 Veillard5d819032002-02-02 21:49:17 +00001179/************************************************************************
1180 * *
1181 * Error message callback *
1182 * *
1183 ************************************************************************/
1184
1185static PyObject *libxml_xmlPythonErrorFuncHandler = NULL;
1186static PyObject *libxml_xmlPythonErrorFuncCtxt = NULL;
1187
1188static void
Daniel Veillardd2379012002-03-15 22:24:56 +00001189libxml_xmlErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, const char *msg,
1190 ...)
1191{
1192 int size;
1193 int chars;
1194 char *larger;
1195 va_list ap;
1196 char *str;
Daniel Veillard5d819032002-02-02 21:49:17 +00001197 PyObject *list;
1198 PyObject *message;
1199 PyObject *result;
1200
1201#ifdef DEBUG_ERROR
1202 printf("libxml_xmlErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
1203#endif
1204
1205
1206 if (libxml_xmlPythonErrorFuncHandler == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001207 va_start(ap, msg);
1208 vfprintf(stdout, msg, ap);
1209 va_end(ap);
Daniel Veillard5d819032002-02-02 21:49:17 +00001210 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00001211 str = (char *) xmlMalloc(150);
1212 if (str == NULL)
1213 return;
Daniel Veillard5d819032002-02-02 21:49:17 +00001214
Daniel Veillardd2379012002-03-15 22:24:56 +00001215 size = 150;
Daniel Veillard5d819032002-02-02 21:49:17 +00001216
Daniel Veillardd2379012002-03-15 22:24:56 +00001217 while (1) {
1218 va_start(ap, msg);
1219 chars = vsnprintf(str, size, msg, ap);
1220 va_end(ap);
1221 if ((chars > -1) && (chars < size))
1222 break;
1223 if (chars > -1)
1224 size += chars + 1;
1225 else
1226 size += 100;
1227 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
1228 xmlFree(str);
1229 return;
1230 }
1231 str = larger;
1232 }
Daniel Veillard5d819032002-02-02 21:49:17 +00001233
Daniel Veillardd2379012002-03-15 22:24:56 +00001234 list = PyTuple_New(2);
1235 PyTuple_SetItem(list, 0, libxml_xmlPythonErrorFuncCtxt);
1236 Py_XINCREF(libxml_xmlPythonErrorFuncCtxt);
1237 message = libxml_charPtrWrap(str);
1238 PyTuple_SetItem(list, 1, message);
1239 result = PyEval_CallObject(libxml_xmlPythonErrorFuncHandler, list);
1240 Py_XDECREF(list);
1241 Py_XDECREF(result);
Daniel Veillard5d819032002-02-02 21:49:17 +00001242 }
1243}
1244
1245static void
Daniel Veillardd2379012002-03-15 22:24:56 +00001246libxml_xmlErrorInitialize(void)
1247{
Daniel Veillard5d819032002-02-02 21:49:17 +00001248#ifdef DEBUG_ERROR
1249 printf("libxml_xmlErrorInitialize() called\n");
1250#endif
1251 xmlSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
1252}
1253
1254PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001255libxml_xmlRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self,
1256 PyObject * args)
1257{
Daniel Veillard5d819032002-02-02 21:49:17 +00001258 PyObject *py_retval;
1259 PyObject *pyobj_f;
1260 PyObject *pyobj_ctx;
1261
Daniel Veillardd2379012002-03-15 22:24:56 +00001262 if (!PyArg_ParseTuple
1263 (args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f,
1264 &pyobj_ctx))
1265 return (NULL);
Daniel Veillard5d819032002-02-02 21:49:17 +00001266
1267#ifdef DEBUG_ERROR
Daniel Veillardd2379012002-03-15 22:24:56 +00001268 printf("libxml_registerXPathFunction(%p, %p) called\n", pyobj_ctx,
1269 pyobj_f);
Daniel Veillard5d819032002-02-02 21:49:17 +00001270#endif
1271
1272 if (libxml_xmlPythonErrorFuncHandler != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001273 Py_XDECREF(libxml_xmlPythonErrorFuncHandler);
Daniel Veillard5d819032002-02-02 21:49:17 +00001274 }
1275 if (libxml_xmlPythonErrorFuncCtxt != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001276 Py_XDECREF(libxml_xmlPythonErrorFuncCtxt);
Daniel Veillard5d819032002-02-02 21:49:17 +00001277 }
1278
1279 Py_XINCREF(pyobj_ctx);
1280 Py_XINCREF(pyobj_f);
1281
1282 /* TODO: check f is a function ! */
1283 libxml_xmlPythonErrorFuncHandler = pyobj_f;
1284 libxml_xmlPythonErrorFuncCtxt = pyobj_ctx;
1285
1286 py_retval = libxml_intWrap(1);
Daniel Veillardd2379012002-03-15 22:24:56 +00001287 return (py_retval);
Daniel Veillard5d819032002-02-02 21:49:17 +00001288}
Daniel Veillardd2379012002-03-15 22:24:56 +00001289
Daniel Veillarda7340c82002-02-01 17:56:45 +00001290/************************************************************************
1291 * *
1292 * XPath extensions *
1293 * *
1294 ************************************************************************/
1295
1296static int libxml_xpathCallbacksInitialized = 0;
1297
1298typedef struct libxml_xpathCallback {
1299 xmlXPathContextPtr ctx;
1300 xmlChar *name;
1301 xmlChar *ns_uri;
1302 PyObject *function;
1303} libxml_xpathCallback, *libxml_xpathCallbackPtr;
1304static libxml_xpathCallback libxml_xpathCallbacks[10];
1305static int libxml_xpathCallbacksNb = 0;
1306static int libxml_xpathCallbacksMax = 10;
1307
Daniel Veillarda7340c82002-02-01 17:56:45 +00001308static void
Daniel Veillardd2379012002-03-15 22:24:56 +00001309libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs)
1310{
Daniel Veillarda7340c82002-02-01 17:56:45 +00001311 PyObject *list, *cur, *result;
1312 xmlXPathObjectPtr obj;
Daniel Veillard70cab352002-02-06 16:06:58 +00001313 xmlXPathContextPtr rctxt;
1314 PyObject *current_function = NULL;
1315 const xmlChar *name;
1316 const xmlChar *ns_uri;
Daniel Veillarda7340c82002-02-01 17:56:45 +00001317 int i;
1318
Daniel Veillard70cab352002-02-06 16:06:58 +00001319 if (ctxt == NULL)
Daniel Veillardd2379012002-03-15 22:24:56 +00001320 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00001321 rctxt = ctxt->context;
1322 if (rctxt == NULL)
Daniel Veillardd2379012002-03-15 22:24:56 +00001323 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00001324 name = rctxt->function;
1325 ns_uri = rctxt->functionURI;
Daniel Veillarda7340c82002-02-01 17:56:45 +00001326#ifdef DEBUG_XPATH
Daniel Veillardd2379012002-03-15 22:24:56 +00001327 printf("libxml_xmlXPathFuncCallback called name %s URI %s\n", name,
1328 ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001329#endif
1330
Daniel Veillard70cab352002-02-06 16:06:58 +00001331 /*
1332 * Find the function, it should be there it was there at lookup
1333 */
Daniel Veillardd2379012002-03-15 22:24:56 +00001334 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
1335 if ( /* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */
1336 (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) &&
1337 (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) {
1338 current_function = libxml_xpathCallbacks[i].function;
1339 }
Daniel Veillard70cab352002-02-06 16:06:58 +00001340 }
1341 if (current_function == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001342 printf
1343 ("libxml_xmlXPathFuncCallback: internal error %s not found !\n",
1344 name);
1345 return;
Daniel Veillard70cab352002-02-06 16:06:58 +00001346 }
1347
Daniel Veillardc575b992002-02-08 13:28:40 +00001348 list = PyTuple_New(nargs + 1);
1349 PyTuple_SetItem(list, 0, libxml_xmlXPathParserContextPtrWrap(ctxt));
Daniel Veillardd2379012002-03-15 22:24:56 +00001350 for (i = 0; i < nargs; i++) {
1351 obj = valuePop(ctxt);
1352 cur = libxml_xmlXPathObjectPtrWrap(obj);
1353 PyTuple_SetItem(list, i + 1, cur);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001354 }
1355 result = PyEval_CallObject(current_function, list);
1356 Py_DECREF(list);
1357
1358 obj = libxml_xmlXPathObjectPtrConvert(result);
1359 valuePush(ctxt, obj);
1360}
1361
1362static xmlXPathFunction
Daniel Veillardd2379012002-03-15 22:24:56 +00001363libxml_xmlXPathFuncLookupFunc(void *ctxt, const xmlChar * name,
1364 const xmlChar * ns_uri)
1365{
Daniel Veillarda7340c82002-02-01 17:56:45 +00001366 int i;
Daniel Veillardd2379012002-03-15 22:24:56 +00001367
Daniel Veillarda7340c82002-02-01 17:56:45 +00001368#ifdef DEBUG_XPATH
1369 printf("libxml_xmlXPathFuncLookupFunc(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001370 ctxt, name, ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001371#endif
Daniel Veillard70cab352002-02-06 16:06:58 +00001372 /*
1373 * This is called once only. The address is then stored in the
1374 * XPath expression evaluation, the proper object to call can
1375 * then still be found using the execution context function
1376 * and functionURI fields.
1377 */
Daniel Veillardd2379012002-03-15 22:24:56 +00001378 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
1379 if ((ctxt == libxml_xpathCallbacks[i].ctx) &&
1380 (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) &&
1381 (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) {
1382 return (libxml_xmlXPathFuncCallback);
1383 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00001384 }
Daniel Veillardd2379012002-03-15 22:24:56 +00001385 return (NULL);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001386}
1387
1388static void
Daniel Veillardd2379012002-03-15 22:24:56 +00001389libxml_xpathCallbacksInitialize(void)
1390{
Daniel Veillarda7340c82002-02-01 17:56:45 +00001391 int i;
1392
1393 if (libxml_xpathCallbacksInitialized != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00001394 return;
Daniel Veillarda7340c82002-02-01 17:56:45 +00001395
1396#ifdef DEBUG_XPATH
1397 printf("libxml_xpathCallbacksInitialized called\n");
1398#endif
1399
Daniel Veillardd2379012002-03-15 22:24:56 +00001400 for (i = 0; i < 10; i++) {
1401 libxml_xpathCallbacks[i].ctx = NULL;
1402 libxml_xpathCallbacks[i].name = NULL;
1403 libxml_xpathCallbacks[i].ns_uri = NULL;
1404 libxml_xpathCallbacks[i].function = NULL;
Daniel Veillarda7340c82002-02-01 17:56:45 +00001405 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00001406 libxml_xpathCallbacksInitialized = 1;
1407}
1408
1409PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001410libxml_xmlRegisterXPathFunction(ATTRIBUTE_UNUSED PyObject * self,
1411 PyObject * args)
1412{
Daniel Veillarda7340c82002-02-01 17:56:45 +00001413 PyObject *py_retval;
1414 int c_retval = 0;
1415 xmlChar *name;
1416 xmlChar *ns_uri;
1417 xmlXPathContextPtr ctx;
1418 PyObject *pyobj_ctx;
1419 PyObject *pyobj_f;
1420 int i;
1421
Daniel Veillardd2379012002-03-15 22:24:56 +00001422 if (!PyArg_ParseTuple
1423 (args, (char *) "OszO:registerXPathFunction", &pyobj_ctx, &name,
1424 &ns_uri, &pyobj_f))
1425 return (NULL);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001426
1427 ctx = (xmlXPathContextPtr) PyxmlXPathContext_Get(pyobj_ctx);
1428 if (libxml_xpathCallbacksInitialized == 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00001429 libxml_xpathCallbacksInitialize();
Daniel Veillarda7340c82002-02-01 17:56:45 +00001430 xmlXPathRegisterFuncLookup(ctx, libxml_xmlXPathFuncLookupFunc, ctx);
1431
1432 if ((pyobj_ctx == NULL) || (name == NULL) || (pyobj_f == NULL)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001433 py_retval = libxml_intWrap(-1);
1434 return (py_retval);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001435 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00001436#ifdef DEBUG_XPATH
1437 printf("libxml_registerXPathFunction(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001438 ctx, name, ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001439#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001440 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
1441 if ((ctx == libxml_xpathCallbacks[i].ctx) &&
1442 (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) &&
1443 (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) {
1444 Py_XINCREF(pyobj_f);
1445 Py_XDECREF(libxml_xpathCallbacks[i].function);
1446 libxml_xpathCallbacks[i].function = pyobj_f;
1447 c_retval = 1;
1448 goto done;
1449 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00001450 }
1451 if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksMax) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001452 printf("libxml_registerXPathFunction() table full\n");
Daniel Veillarda7340c82002-02-01 17:56:45 +00001453 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00001454 i = libxml_xpathCallbacksNb++;
1455 Py_XINCREF(pyobj_f);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001456 libxml_xpathCallbacks[i].ctx = ctx;
1457 libxml_xpathCallbacks[i].name = xmlStrdup(name);
1458 libxml_xpathCallbacks[i].ns_uri = xmlStrdup(ns_uri);
Daniel Veillardd2379012002-03-15 22:24:56 +00001459 libxml_xpathCallbacks[i].function = pyobj_f;
1460 c_retval = 1;
Daniel Veillarda7340c82002-02-01 17:56:45 +00001461 }
Daniel Veillardd2379012002-03-15 22:24:56 +00001462 done:
Daniel Veillarda7340c82002-02-01 17:56:45 +00001463 py_retval = libxml_intWrap((int) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00001464 return (py_retval);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001465}
1466
Daniel Veillard1971ee22002-01-31 20:29:19 +00001467/************************************************************************
1468 * *
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001469 * Global properties access *
1470 * *
1471 ************************************************************************/
1472static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001473libxml_name(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001474{
1475 PyObject *resultobj, *obj;
1476 xmlNodePtr cur;
1477 const xmlChar *res;
1478
Daniel Veillardd2379012002-03-15 22:24:56 +00001479 if (!PyArg_ParseTuple(args, (char *) "O:name", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001480 return NULL;
1481 cur = PyxmlNode_Get(obj);
1482
1483#ifdef DEBUG
1484 printf("libxml_name: cur = %p type %d\n", cur, cur->type);
1485#endif
1486
Daniel Veillardd2379012002-03-15 22:24:56 +00001487 switch (cur->type) {
1488 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001489#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001490 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001491#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001492 case XML_HTML_DOCUMENT_NODE:{
1493 xmlDocPtr doc = (xmlDocPtr) cur;
1494
1495 res = doc->URL;
1496 break;
1497 }
1498 case XML_ATTRIBUTE_NODE:{
1499 xmlAttrPtr attr = (xmlAttrPtr) cur;
1500
1501 res = attr->name;
1502 break;
1503 }
1504 case XML_NAMESPACE_DECL:{
1505 xmlNsPtr ns = (xmlNsPtr) cur;
1506
1507 res = ns->prefix;
1508 break;
1509 }
1510 default:
1511 res = cur->name;
1512 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001513 }
Daniel Veillard1971ee22002-01-31 20:29:19 +00001514 resultobj = libxml_constxmlCharPtrWrap(res);
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001515
1516 return resultobj;
1517}
1518
1519static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001520libxml_doc(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001521{
1522 PyObject *resultobj, *obj;
1523 xmlNodePtr cur;
1524 xmlDocPtr res;
1525
Daniel Veillardd2379012002-03-15 22:24:56 +00001526 if (!PyArg_ParseTuple(args, (char *) "O:doc", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001527 return NULL;
1528 cur = PyxmlNode_Get(obj);
1529
1530#ifdef DEBUG
1531 printf("libxml_doc: cur = %p\n", cur);
1532#endif
1533
Daniel Veillardd2379012002-03-15 22:24:56 +00001534 switch (cur->type) {
1535 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001536#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001537 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001538#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001539 case XML_HTML_DOCUMENT_NODE:
1540 res = NULL;
1541 break;
1542 case XML_ATTRIBUTE_NODE:{
1543 xmlAttrPtr attr = (xmlAttrPtr) cur;
1544
1545 res = attr->doc;
1546 break;
1547 }
1548 case XML_NAMESPACE_DECL:
1549 res = NULL;
1550 break;
1551 default:
1552 res = cur->doc;
1553 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001554 }
1555 resultobj = libxml_xmlDocPtrWrap(res);
1556 return resultobj;
1557}
1558
1559static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001560libxml_properties(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001561{
1562 PyObject *resultobj, *obj;
1563 xmlNodePtr cur = NULL;
1564 xmlAttrPtr res;
1565
Daniel Veillardd2379012002-03-15 22:24:56 +00001566 if (!PyArg_ParseTuple(args, (char *) "O:properties", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001567 return NULL;
1568 cur = PyxmlNode_Get(obj);
1569 if (cur->type == XML_ELEMENT_NODE)
Daniel Veillardd2379012002-03-15 22:24:56 +00001570 res = cur->properties;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001571 else
Daniel Veillardd2379012002-03-15 22:24:56 +00001572 res = NULL;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001573 resultobj = libxml_xmlAttrPtrWrap(res);
1574 return resultobj;
1575}
1576
1577static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001578libxml_next(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001579{
1580 PyObject *resultobj, *obj;
1581 xmlNodePtr cur;
1582 xmlNodePtr res;
1583
Daniel Veillardd2379012002-03-15 22:24:56 +00001584 if (!PyArg_ParseTuple(args, (char *) "O:next", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001585 return NULL;
1586 cur = PyxmlNode_Get(obj);
1587
1588#ifdef DEBUG
1589 printf("libxml_next: cur = %p\n", cur);
1590#endif
1591
Daniel Veillardd2379012002-03-15 22:24:56 +00001592 switch (cur->type) {
1593 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001594#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001595 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001596#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001597 case XML_HTML_DOCUMENT_NODE:
1598 res = NULL;
1599 break;
1600 case XML_ATTRIBUTE_NODE:{
1601 xmlAttrPtr attr = (xmlAttrPtr) cur;
1602
1603 res = (xmlNodePtr) attr->next;
1604 break;
1605 }
1606 case XML_NAMESPACE_DECL:{
1607 xmlNsPtr ns = (xmlNsPtr) cur;
1608
1609 res = (xmlNodePtr) ns->next;
1610 break;
1611 }
1612 default:
1613 res = cur->next;
1614 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001615
1616 }
1617 resultobj = libxml_xmlNodePtrWrap(res);
1618 return resultobj;
1619}
1620
1621static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001622libxml_prev(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001623{
1624 PyObject *resultobj, *obj;
1625 xmlNodePtr cur;
1626 xmlNodePtr res;
1627
Daniel Veillardd2379012002-03-15 22:24:56 +00001628 if (!PyArg_ParseTuple(args, (char *) "O:prev", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001629 return NULL;
1630 cur = PyxmlNode_Get(obj);
1631
1632#ifdef DEBUG
1633 printf("libxml_prev: cur = %p\n", cur);
1634#endif
1635
Daniel Veillardd2379012002-03-15 22:24:56 +00001636 switch (cur->type) {
1637 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001638#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001639 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001640#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001641 case XML_HTML_DOCUMENT_NODE:
1642 res = NULL;
1643 break;
1644 case XML_ATTRIBUTE_NODE:{
1645 xmlAttrPtr attr = (xmlAttrPtr) cur;
1646
Daniel Veillardfaa35ff2002-11-24 13:53:43 +00001647 res = (xmlNodePtr) attr->prev;
Daniel Veillardd2379012002-03-15 22:24:56 +00001648 }
1649 case XML_NAMESPACE_DECL:
1650 res = NULL;
1651 break;
1652 default:
Daniel Veillardfaa35ff2002-11-24 13:53:43 +00001653 res = cur->prev;
Daniel Veillardd2379012002-03-15 22:24:56 +00001654 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001655 }
1656 resultobj = libxml_xmlNodePtrWrap(res);
1657 return resultobj;
1658}
1659
1660static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001661libxml_children(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001662{
1663 PyObject *resultobj, *obj;
1664 xmlNodePtr cur;
1665 xmlNodePtr res;
1666
Daniel Veillardd2379012002-03-15 22:24:56 +00001667 if (!PyArg_ParseTuple(args, (char *) "O:children", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001668 return NULL;
1669 cur = PyxmlNode_Get(obj);
1670
1671#ifdef DEBUG
1672 printf("libxml_children: cur = %p\n", cur);
1673#endif
1674
Daniel Veillardd2379012002-03-15 22:24:56 +00001675 switch (cur->type) {
1676 case XML_ELEMENT_NODE:
1677 case XML_ENTITY_REF_NODE:
1678 case XML_ENTITY_NODE:
1679 case XML_PI_NODE:
1680 case XML_COMMENT_NODE:
1681 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001682#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001683 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001684#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001685 case XML_HTML_DOCUMENT_NODE:
1686 case XML_DTD_NODE:
1687 res = cur->children;
1688 break;
1689 case XML_ATTRIBUTE_NODE:{
1690 xmlAttrPtr attr = (xmlAttrPtr) cur;
1691
1692 res = attr->children;
1693 break;
1694 }
1695 default:
1696 res = NULL;
1697 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001698 }
1699 resultobj = libxml_xmlNodePtrWrap(res);
1700 return resultobj;
1701}
1702
1703static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001704libxml_last(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001705{
1706 PyObject *resultobj, *obj;
1707 xmlNodePtr cur;
1708 xmlNodePtr res;
1709
Daniel Veillardd2379012002-03-15 22:24:56 +00001710 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001711 return NULL;
1712 cur = PyxmlNode_Get(obj);
1713
1714#ifdef DEBUG
1715 printf("libxml_last: cur = %p\n", cur);
1716#endif
1717
Daniel Veillardd2379012002-03-15 22:24:56 +00001718 switch (cur->type) {
1719 case XML_ELEMENT_NODE:
1720 case XML_ENTITY_REF_NODE:
1721 case XML_ENTITY_NODE:
1722 case XML_PI_NODE:
1723 case XML_COMMENT_NODE:
1724 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001725#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001726 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001727#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001728 case XML_HTML_DOCUMENT_NODE:
1729 case XML_DTD_NODE:
1730 res = cur->last;
1731 break;
1732 case XML_ATTRIBUTE_NODE:{
1733 xmlAttrPtr attr = (xmlAttrPtr) cur;
1734
1735 res = attr->last;
1736 }
1737 default:
1738 res = NULL;
1739 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001740 }
1741 resultobj = libxml_xmlNodePtrWrap(res);
1742 return resultobj;
1743}
1744
1745static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001746libxml_parent(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001747{
1748 PyObject *resultobj, *obj;
1749 xmlNodePtr cur;
1750 xmlNodePtr res;
1751
Daniel Veillardd2379012002-03-15 22:24:56 +00001752 if (!PyArg_ParseTuple(args, (char *) "O:parent", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001753 return NULL;
1754 cur = PyxmlNode_Get(obj);
1755
1756#ifdef DEBUG
1757 printf("libxml_parent: cur = %p\n", cur);
1758#endif
1759
Daniel Veillardd2379012002-03-15 22:24:56 +00001760 switch (cur->type) {
1761 case XML_DOCUMENT_NODE:
1762 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001763#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001764 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001765#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001766 res = NULL;
1767 break;
1768 case XML_ATTRIBUTE_NODE:{
1769 xmlAttrPtr attr = (xmlAttrPtr) cur;
1770
1771 res = attr->parent;
1772 }
1773 case XML_ENTITY_DECL:
1774 case XML_NAMESPACE_DECL:
1775 case XML_XINCLUDE_START:
1776 case XML_XINCLUDE_END:
1777 res = NULL;
1778 break;
1779 default:
1780 res = cur->parent;
1781 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001782 }
1783 resultobj = libxml_xmlNodePtrWrap(res);
1784 return resultobj;
1785}
1786
1787static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001788libxml_type(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001789{
1790 PyObject *resultobj, *obj;
1791 xmlNodePtr cur;
Daniel Veillardd2379012002-03-15 22:24:56 +00001792 const xmlChar *res = NULL;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001793
Daniel Veillardd2379012002-03-15 22:24:56 +00001794 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001795 return NULL;
1796 cur = PyxmlNode_Get(obj);
1797
1798#ifdef DEBUG
1799 printf("libxml_type: cur = %p\n", cur);
1800#endif
1801
Daniel Veillardd2379012002-03-15 22:24:56 +00001802 switch (cur->type) {
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001803 case XML_ELEMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001804 res = (const xmlChar *) "element";
1805 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001806 case XML_ATTRIBUTE_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001807 res = (const xmlChar *) "attribute";
1808 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001809 case XML_TEXT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001810 res = (const xmlChar *) "text";
1811 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001812 case XML_CDATA_SECTION_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001813 res = (const xmlChar *) "cdata";
1814 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001815 case XML_ENTITY_REF_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001816 res = (const xmlChar *) "entity_ref";
1817 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001818 case XML_ENTITY_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001819 res = (const xmlChar *) "entity";
1820 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001821 case XML_PI_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001822 res = (const xmlChar *) "pi";
1823 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001824 case XML_COMMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001825 res = (const xmlChar *) "comment";
1826 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001827 case XML_DOCUMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001828 res = (const xmlChar *) "document_xml";
1829 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001830 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001831 res = (const xmlChar *) "doctype";
1832 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001833 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001834 res = (const xmlChar *) "fragment";
1835 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001836 case XML_NOTATION_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001837 res = (const xmlChar *) "notation";
1838 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001839 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001840 res = (const xmlChar *) "document_html";
1841 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001842 case XML_DTD_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001843 res = (const xmlChar *) "dtd";
1844 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001845 case XML_ELEMENT_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00001846 res = (const xmlChar *) "elem_decl";
1847 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001848 case XML_ATTRIBUTE_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00001849 res = (const xmlChar *) "attribute_decl";
1850 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001851 case XML_ENTITY_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00001852 res = (const xmlChar *) "entity_decl";
1853 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001854 case XML_NAMESPACE_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00001855 res = (const xmlChar *) "namespace";
1856 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001857 case XML_XINCLUDE_START:
Daniel Veillardd2379012002-03-15 22:24:56 +00001858 res = (const xmlChar *) "xinclude_start";
1859 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001860 case XML_XINCLUDE_END:
Daniel Veillardd2379012002-03-15 22:24:56 +00001861 res = (const xmlChar *) "xinclude_end";
1862 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001863#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001864 case XML_DOCB_DOCUMENT_NODE:
1865 res = (const xmlChar *) "document_docbook";
1866 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001867#endif
1868 }
1869#ifdef DEBUG
1870 printf("libxml_type: cur = %p: %s\n", cur, res);
1871#endif
1872
Daniel Veillard1971ee22002-01-31 20:29:19 +00001873 resultobj = libxml_constxmlCharPtrWrap(res);
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001874 return resultobj;
1875}
1876
1877/************************************************************************
1878 * *
Daniel Veillard36eea2d2002-02-04 00:17:01 +00001879 * Specific accessor functions *
1880 * *
1881 ************************************************************************/
1882PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001883libxml_xmlNodeGetNsDefs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1884{
Daniel Veillard36eea2d2002-02-04 00:17:01 +00001885 PyObject *py_retval;
1886 xmlNsPtr c_retval;
1887 xmlNodePtr node;
1888 PyObject *pyobj_node;
1889
Daniel Veillardd2379012002-03-15 22:24:56 +00001890 if (!PyArg_ParseTuple
1891 (args, (char *) "O:xmlNodeGetNsDefs", &pyobj_node))
1892 return (NULL);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00001893 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
1894
1895 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001896 Py_INCREF(Py_None);
1897 return (Py_None);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00001898 }
1899 c_retval = node->nsDef;
1900 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00001901 return (py_retval);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00001902}
1903
1904PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001905libxml_xmlNodeGetNs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1906{
Daniel Veillard36eea2d2002-02-04 00:17:01 +00001907 PyObject *py_retval;
1908 xmlNsPtr c_retval;
1909 xmlNodePtr node;
1910 PyObject *pyobj_node;
1911
Daniel Veillardd2379012002-03-15 22:24:56 +00001912 if (!PyArg_ParseTuple(args, (char *) "O:xmlNodeGetNs", &pyobj_node))
1913 return (NULL);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00001914 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
1915
1916 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001917 Py_INCREF(Py_None);
1918 return (Py_None);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00001919 }
1920 c_retval = node->ns;
1921 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00001922 return (py_retval);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00001923}
1924
1925/************************************************************************
1926 * *
Daniel Veillard1e774382002-03-06 17:35:40 +00001927 * Serialization front-end *
1928 * *
1929 ************************************************************************/
1930
Daniel Veillardd2379012002-03-15 22:24:56 +00001931static PyObject *
1932libxml_serializeNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1933{
Daniel Veillard1e774382002-03-06 17:35:40 +00001934 PyObject *py_retval = NULL;
1935 xmlChar *c_retval;
1936 PyObject *pyobj_node;
1937 xmlNodePtr node;
1938 xmlDocPtr doc;
Daniel Veillardd2379012002-03-15 22:24:56 +00001939 const char *encoding;
Daniel Veillard1e774382002-03-06 17:35:40 +00001940 int format;
1941 int len;
1942
Daniel Veillardd2379012002-03-15 22:24:56 +00001943 if (!PyArg_ParseTuple(args, (char *) "Ozi:serializeNode", &pyobj_node,
1944 &encoding, &format))
1945 return (NULL);
Daniel Veillard1e774382002-03-06 17:35:40 +00001946 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
1947
1948 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001949 Py_INCREF(Py_None);
1950 return (Py_None);
Daniel Veillard1e774382002-03-06 17:35:40 +00001951 }
1952 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001953 doc = (xmlDocPtr) node;
1954 xmlDocDumpFormatMemoryEnc(doc, &c_retval, &len,
1955 (const char *) encoding, format);
1956 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard1e774382002-03-06 17:35:40 +00001957 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001958 xmlOutputBufferPtr buf;
1959 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00001960
Daniel Veillardd2379012002-03-15 22:24:56 +00001961 doc = (xmlDocPtr) node;
1962 if (encoding != NULL)
1963 htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
1964 encoding = (const char *) htmlGetMetaEncoding(doc);
Daniel Veillard1e774382002-03-06 17:35:40 +00001965
Daniel Veillardd2379012002-03-15 22:24:56 +00001966 if (encoding != NULL) {
1967 handler = xmlFindCharEncodingHandler(encoding);
1968 if (handler == NULL) {
1969 Py_INCREF(Py_None);
1970 return (Py_None);
1971 }
1972 }
Daniel Veillard1e774382002-03-06 17:35:40 +00001973
Daniel Veillardd2379012002-03-15 22:24:56 +00001974 /*
1975 * Fallback to HTML or ASCII when the encoding is unspecified
1976 */
1977 if (handler == NULL)
1978 handler = xmlFindCharEncodingHandler("HTML");
1979 if (handler == NULL)
1980 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00001981
Daniel Veillardd2379012002-03-15 22:24:56 +00001982 buf = xmlAllocOutputBuffer(handler);
1983 if (buf == NULL) {
1984 Py_INCREF(Py_None);
1985 return (Py_None);
1986 }
1987 htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
1988 xmlOutputBufferFlush(buf);
1989 if (buf->conv != NULL) {
1990 len = buf->conv->use;
1991 c_retval = buf->conv->content;
1992 buf->conv->content = NULL;
1993 } else {
1994 len = buf->buffer->use;
1995 c_retval = buf->buffer->content;
1996 buf->buffer->content = NULL;
1997 }
1998 (void) xmlOutputBufferClose(buf);
1999 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard1e774382002-03-06 17:35:40 +00002000 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00002001 doc = node->doc;
Daniel Veillarda8c0adb2002-11-17 22:37:35 +00002002 if ((doc == NULL) || (doc->type == XML_DOCUMENT_NODE)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002003 xmlOutputBufferPtr buf;
2004 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00002005
Daniel Veillardd2379012002-03-15 22:24:56 +00002006 if (encoding != NULL) {
2007 handler = xmlFindCharEncodingHandler(encoding);
2008 if (handler == NULL) {
2009 Py_INCREF(Py_None);
2010 return (Py_None);
2011 }
2012 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002013
Daniel Veillardd2379012002-03-15 22:24:56 +00002014 buf = xmlAllocOutputBuffer(handler);
2015 if (buf == NULL) {
2016 Py_INCREF(Py_None);
2017 return (Py_None);
2018 }
2019 xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
2020 xmlOutputBufferFlush(buf);
2021 if (buf->conv != NULL) {
2022 len = buf->conv->use;
2023 c_retval = buf->conv->content;
2024 buf->conv->content = NULL;
2025 } else {
2026 len = buf->buffer->use;
2027 c_retval = buf->buffer->content;
2028 buf->buffer->content = NULL;
2029 }
2030 (void) xmlOutputBufferClose(buf);
2031 py_retval = libxml_charPtrWrap((char *) c_retval);
2032 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2033 xmlOutputBufferPtr buf;
2034 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00002035
Daniel Veillardd2379012002-03-15 22:24:56 +00002036 if (encoding != NULL)
2037 htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
2038 encoding = (const char *) htmlGetMetaEncoding(doc);
2039 if (encoding != NULL) {
2040 handler = xmlFindCharEncodingHandler(encoding);
2041 if (handler == NULL) {
2042 Py_INCREF(Py_None);
2043 return (Py_None);
2044 }
2045 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002046
Daniel Veillardd2379012002-03-15 22:24:56 +00002047 /*
2048 * Fallback to HTML or ASCII when the encoding is unspecified
2049 */
2050 if (handler == NULL)
2051 handler = xmlFindCharEncodingHandler("HTML");
2052 if (handler == NULL)
2053 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00002054
Daniel Veillardd2379012002-03-15 22:24:56 +00002055 buf = xmlAllocOutputBuffer(handler);
2056 if (buf == NULL) {
2057 Py_INCREF(Py_None);
2058 return (Py_None);
2059 }
2060 htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
2061 xmlOutputBufferFlush(buf);
2062 if (buf->conv != NULL) {
2063 len = buf->conv->use;
2064 c_retval = buf->conv->content;
2065 buf->conv->content = NULL;
2066 } else {
2067 len = buf->buffer->use;
2068 c_retval = buf->buffer->content;
2069 buf->buffer->content = NULL;
2070 }
2071 (void) xmlOutputBufferClose(buf);
2072 py_retval = libxml_charPtrWrap((char *) c_retval);
2073 } else {
2074 Py_INCREF(Py_None);
2075 return (Py_None);
2076 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002077 }
Daniel Veillardd2379012002-03-15 22:24:56 +00002078 return (py_retval);
Daniel Veillard1e774382002-03-06 17:35:40 +00002079}
2080
Daniel Veillardd2379012002-03-15 22:24:56 +00002081static PyObject *
2082libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2083{
Daniel Veillard1e774382002-03-06 17:35:40 +00002084 PyObject *py_file = NULL;
2085 FILE *output;
2086 PyObject *pyobj_node;
2087 xmlNodePtr node;
2088 xmlDocPtr doc;
Daniel Veillardd2379012002-03-15 22:24:56 +00002089 const char *encoding;
Daniel Veillard1e774382002-03-06 17:35:40 +00002090 int format;
2091 int len;
2092 xmlOutputBufferPtr buf;
2093 xmlCharEncodingHandlerPtr handler = NULL;
2094
Daniel Veillardd2379012002-03-15 22:24:56 +00002095 if (!PyArg_ParseTuple(args, (char *) "OOzi:serializeNode", &pyobj_node,
2096 &py_file, &encoding, &format))
2097 return (NULL);
Daniel Veillard1e774382002-03-06 17:35:40 +00002098 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2099
2100 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002101 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002102 }
2103 if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002104 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002105 }
2106 output = PyFile_AsFile(py_file);
2107 if (output == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002108 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00002109 }
2110
2111 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002112 doc = (xmlDocPtr) node;
Daniel Veillard1e774382002-03-06 17:35:40 +00002113 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002114 doc = (xmlDocPtr) node;
Daniel Veillard1e774382002-03-06 17:35:40 +00002115 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00002116 doc = node->doc;
Daniel Veillard1e774382002-03-06 17:35:40 +00002117 }
2118 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002119 if (encoding == NULL)
2120 encoding = (const char *) htmlGetMetaEncoding(doc);
Daniel Veillard1e774382002-03-06 17:35:40 +00002121 }
2122 if (encoding != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002123 handler = xmlFindCharEncodingHandler(encoding);
2124 if (handler == NULL) {
2125 return (PyInt_FromLong((long) -1));
2126 }
Daniel Veillard1e774382002-03-06 17:35:40 +00002127 }
2128 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002129 if (handler == NULL)
2130 handler = xmlFindCharEncodingHandler("HTML");
2131 if (handler == NULL)
2132 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00002133 }
2134
2135 buf = xmlOutputBufferCreateFile(output, handler);
2136 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002137 len = xmlSaveFormatFileTo(buf, doc, encoding, format);
Daniel Veillard1e774382002-03-06 17:35:40 +00002138 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002139 htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
2140 len = xmlOutputBufferClose(buf);
Daniel Veillard1e774382002-03-06 17:35:40 +00002141 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002142 htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
2143 len = xmlOutputBufferClose(buf);
Daniel Veillard1e774382002-03-06 17:35:40 +00002144 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00002145 xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
2146 len = xmlOutputBufferClose(buf);
Daniel Veillard1e774382002-03-06 17:35:40 +00002147 }
Daniel Veillardd2379012002-03-15 22:24:56 +00002148 return (PyInt_FromLong((long) len));
Daniel Veillard1e774382002-03-06 17:35:40 +00002149}
2150
2151/************************************************************************
2152 * *
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002153 * Extra stuff *
2154 * *
2155 ************************************************************************/
2156PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00002157libxml_xmlNewNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
2158{
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002159 PyObject *py_retval;
Daniel Veillardd2379012002-03-15 22:24:56 +00002160 xmlChar *name;
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002161 xmlNodePtr node;
2162
Daniel Veillardd2379012002-03-15 22:24:56 +00002163 if (!PyArg_ParseTuple(args, (char *) "s:xmlNewNode", &name))
2164 return (NULL);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002165 node = (xmlNodePtr) xmlNewNode(NULL, name);
Daniel Veillardd2379012002-03-15 22:24:56 +00002166 printf("NewNode: %s : %p\n", name, (void *) node);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002167
2168 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00002169 Py_INCREF(Py_None);
2170 return (Py_None);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002171 }
2172 py_retval = libxml_xmlNodePtrWrap(node);
Daniel Veillardd2379012002-03-15 22:24:56 +00002173 return (py_retval);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00002174}
2175
2176/************************************************************************
2177 * *
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002178 * The registration stuff *
2179 * *
2180 ************************************************************************/
2181static PyMethodDef libxmlMethods[] = {
Daniel Veillard96fe0952002-01-30 20:52:23 +00002182#include "libxml2-export.c"
Daniel Veillardd2379012002-03-15 22:24:56 +00002183 {(char *) "name", libxml_name, METH_VARARGS, NULL},
2184 {(char *) "children", libxml_children, METH_VARARGS, NULL},
2185 {(char *) "properties", libxml_properties, METH_VARARGS, NULL},
2186 {(char *) "last", libxml_last, METH_VARARGS, NULL},
2187 {(char *) "prev", libxml_prev, METH_VARARGS, NULL},
2188 {(char *) "next", libxml_next, METH_VARARGS, NULL},
2189 {(char *) "parent", libxml_parent, METH_VARARGS, NULL},
2190 {(char *) "type", libxml_type, METH_VARARGS, NULL},
2191 {(char *) "doc", libxml_doc, METH_VARARGS, NULL},
2192 {(char *) "xmlNewNode", libxml_xmlNewNode, METH_VARARGS, NULL},
2193 {(char *) "serializeNode", libxml_serializeNode, METH_VARARGS, NULL},
2194 {(char *) "saveNodeTo", libxml_saveNodeTo, METH_VARARGS, NULL},
Daniel Veillardc6d4a932002-09-12 15:00:57 +00002195 {(char *) "outputBufferCreate", libxml_xmlCreateOutputBuffer, METH_VARARGS, NULL},
2196 {(char *) "inputBufferCreate", libxml_xmlCreateInputBuffer, METH_VARARGS, NULL},
2197 {(char *) "setEntityLoader", libxml_xmlSetEntityLoader, METH_VARARGS, NULL},
Daniel Veillard3e20a292003-01-10 13:14:40 +00002198 {(char *)"xmlRegisterErrorHandler", libxml_xmlRegisterErrorHandler, METH_VARARGS, NULL },
Daniel Veillardd2379012002-03-15 22:24:56 +00002199 {NULL, NULL, 0, NULL}
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002200};
2201
Daniel Veillard0fea6f42002-02-22 22:51:13 +00002202#ifdef MERGED_MODULES
Daniel Veillard6361da02002-02-23 10:10:33 +00002203extern void initlibxsltmod(void);
Daniel Veillard0fea6f42002-02-22 22:51:13 +00002204#endif
2205
Daniel Veillardd2379012002-03-15 22:24:56 +00002206void
2207initlibxml2mod(void)
2208{
Daniel Veillardaf43f632002-03-08 15:05:20 +00002209 static int initialized = 0;
Daniel Veillard3ce52572002-02-03 15:08:05 +00002210 PyObject *m;
Daniel Veillardaf43f632002-03-08 15:05:20 +00002211
2212 if (initialized != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00002213 return;
Daniel Veillardc6d4a932002-09-12 15:00:57 +00002214 xmlRegisterDefaultOutputCallbacks();
2215 xmlRegisterDefaultInputCallbacks();
Daniel Veillardd2379012002-03-15 22:24:56 +00002216 m = Py_InitModule((char *) "libxml2mod", libxmlMethods);
Daniel Veillardaf43f632002-03-08 15:05:20 +00002217 initialized = 1;
Daniel Veillard5d819032002-02-02 21:49:17 +00002218 libxml_xmlErrorInitialize();
Daniel Veillard6361da02002-02-23 10:10:33 +00002219
Daniel Veillard0fea6f42002-02-22 22:51:13 +00002220#ifdef MERGED_MODULES
2221 initlibxsltmod();
2222#endif
Daniel Veillardd2897fd2002-01-30 16:37:32 +00002223}