blob: 0072c31f397462226542cd2c5c7e9fba6d53266e [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 Veillardd2379012002-03-15 22:24:56 +000015#include "config.h"
Daniel Veillardf1d0e6b2002-01-31 23:42:44 +000016#include <libxml/xmlmemory.h>
Daniel Veillardd2897fd2002-01-30 16:37:32 +000017#include <libxml/parser.h>
18#include <libxml/tree.h>
Daniel Veillarda7340c82002-02-01 17:56:45 +000019#include <libxml/xpath.h>
Daniel Veillard5d819032002-02-02 21:49:17 +000020#include <libxml/xmlerror.h>
Daniel Veillarda7340c82002-02-01 17:56:45 +000021#include <libxml/xpathInternals.h>
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000022#include <libxml/xmlmemory.h>
Daniel Veillardd2897fd2002-01-30 16:37:32 +000023#include "libxml_wrap.h"
Daniel Veillard96fe0952002-01-30 20:52:23 +000024#include "libxml2-py.h"
Daniel Veillardd2897fd2002-01-30 16:37:32 +000025
26/* #define DEBUG */
Daniel Veillardd2379012002-03-15 22:24:56 +000027
Daniel Veillard797a5652002-02-12 13:46:21 +000028/* #define DEBUG_SAX */
Daniel Veillardd2379012002-03-15 22:24:56 +000029
Daniel Veillarda7340c82002-02-01 17:56:45 +000030/* #define DEBUG_XPATH */
Daniel Veillardd2379012002-03-15 22:24:56 +000031
Daniel Veillard5d819032002-02-02 21:49:17 +000032/* #define DEBUG_ERROR */
Daniel Veillardd2379012002-03-15 22:24:56 +000033
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000034/* #define DEBUG_MEMORY */
35
Daniel Veillardd2379012002-03-15 22:24:56 +000036void initlibxml2mod(void);
37
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000038/************************************************************************
39 * *
40 * Memory debug interface *
41 * *
42 ************************************************************************/
43
44extern void xmlMemFree(void *ptr);
45extern void *xmlMemMalloc(size_t size);
Daniel Veillardd2379012002-03-15 22:24:56 +000046extern void *xmlMemRealloc(void *ptr, size_t size);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000047extern char *xmlMemoryStrdup(const char *str);
48
49static int libxmlMemoryDebugActivated = 0;
50static long libxmlMemoryAllocatedBase = 0;
51
52static int libxmlMemoryDebug = 0;
53static xmlFreeFunc freeFunc = NULL;
54static xmlMallocFunc mallocFunc = NULL;
55static xmlReallocFunc reallocFunc = NULL;
56static xmlStrdupFunc strdupFunc = NULL;
57
58PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +000059libxml_xmlDebugMemory(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
60{
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000061 int activate;
62 PyObject *py_retval;
63 long ret;
64
Daniel Veillardd2379012002-03-15 22:24:56 +000065 if (!PyArg_ParseTuple(args, (char *) "i:xmlDebugMemory", &activate))
66 return (NULL);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000067
68#ifdef DEBUG_MEMORY
69 printf("libxml_xmlDebugMemory(%d) called\n", activate);
70#endif
71
72 if (activate != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +000073 if (libxmlMemoryDebug == 0) {
74 /*
75 * First initialize the library and grab the old memory handlers
76 * and switch the library to memory debugging
77 */
78 xmlMemGet((xmlFreeFunc *) & freeFunc,
79 (xmlMallocFunc *) & mallocFunc,
80 (xmlReallocFunc *) & reallocFunc,
81 (xmlStrdupFunc *) & strdupFunc);
82 if ((freeFunc == xmlMemFree) && (mallocFunc == xmlMemMalloc) &&
83 (reallocFunc == xmlMemRealloc) &&
84 (strdupFunc == xmlMemoryStrdup)) {
85 libxmlMemoryAllocatedBase = xmlMemUsed();
86 } else {
87 ret = (long) xmlMemSetup(xmlMemFree, xmlMemMalloc,
88 xmlMemRealloc, xmlMemoryStrdup);
89 if (ret < 0)
90 goto error;
91 libxmlMemoryAllocatedBase = xmlMemUsed();
92 }
93 xmlInitParser();
94 ret = 0;
95 } else if (libxmlMemoryDebugActivated == 0) {
96 libxmlMemoryAllocatedBase = xmlMemUsed();
97 ret = 0;
98 } else {
99 ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
100 }
101 libxmlMemoryDebug = 1;
102 libxmlMemoryDebugActivated = 1;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000103 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +0000104 if (libxmlMemoryDebugActivated == 1)
105 ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
106 else
107 ret = 0;
108 libxmlMemoryDebugActivated = 0;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000109 }
Daniel Veillardd2379012002-03-15 22:24:56 +0000110 error:
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000111 py_retval = libxml_longWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +0000112 return (py_retval);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000113}
114
115PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +0000116libxml_xmlDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
117 ATTRIBUTE_UNUSED PyObject * args)
118{
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000119
120 if (libxmlMemoryDebug != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +0000121 xmlMemoryDump();
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000122 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +0000123 return (Py_None);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000124}
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000125
126/************************************************************************
127 * *
Daniel Veillard3ce52572002-02-03 15:08:05 +0000128 * Handling SAX/xmllib/sgmlop callback interfaces *
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000129 * *
130 ************************************************************************/
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000131
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000132static void
133pythonStartElement(void *user_data, const xmlChar * name,
134 const xmlChar ** attrs)
135{
136 int i;
137 PyObject *handler;
138 PyObject *dict;
139 PyObject *attrname;
140 PyObject *attrvalue;
Daniel Veillardd2379012002-03-15 22:24:56 +0000141 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000142 int type = 0;
143
Daniel Veillard797a5652002-02-12 13:46:21 +0000144#ifdef DEBUG_SAX
145 printf("pythonStartElement(%s) called\n", name);
146#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000147 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000148 if (PyObject_HasAttrString(handler, (char *) "startElement"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000149 type = 1;
Daniel Veillardd2379012002-03-15 22:24:56 +0000150 else if (PyObject_HasAttrString(handler, (char *) "start"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000151 type = 2;
152 if (type != 0) {
153 /*
154 * the xmllib interface always generate a dictionnary,
155 * possibly empty
156 */
157 if ((attrs == NULL) && (type == 1)) {
158 Py_XINCREF(Py_None);
159 dict = Py_None;
Daniel Veillardd2379012002-03-15 22:24:56 +0000160 } else if (attrs == NULL) {
161 dict = PyDict_New();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000162 } else {
163 dict = PyDict_New();
164 for (i = 0; attrs[i] != NULL; i++) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000165 attrname = PyString_FromString((char *) attrs[i]);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000166 i++;
167 if (attrs[i] != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000168 attrvalue = PyString_FromString((char *) attrs[i]);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000169 } else {
170 Py_XINCREF(Py_None);
171 attrvalue = Py_None;
172 }
173 PyDict_SetItem(dict, attrname, attrvalue);
174 }
175 }
176
177 if (type == 1)
Daniel Veillardd2379012002-03-15 22:24:56 +0000178 result = PyObject_CallMethod(handler, (char *) "startElement",
179 (char *) "sO", name, dict);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000180 else if (type == 2)
Daniel Veillardd2379012002-03-15 22:24:56 +0000181 result = PyObject_CallMethod(handler, (char *) "start",
182 (char *) "sO", name, dict);
183 if (PyErr_Occurred())
184 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000185 Py_XDECREF(dict);
186 Py_XDECREF(result);
187 }
188}
189
190static void
191pythonStartDocument(void *user_data)
192{
193 PyObject *handler;
194 PyObject *result;
195
Daniel Veillard797a5652002-02-12 13:46:21 +0000196#ifdef DEBUG_SAX
197 printf("pythonStartDocument() called\n");
198#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000199 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000200 if (PyObject_HasAttrString(handler, (char *) "startDocument")) {
201 result =
202 PyObject_CallMethod(handler, (char *) "startDocument", NULL);
203 if (PyErr_Occurred())
204 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000205 Py_XDECREF(result);
206 }
207}
208
209static void
210pythonEndDocument(void *user_data)
211{
212 PyObject *handler;
213 PyObject *result;
214
Daniel Veillard797a5652002-02-12 13:46:21 +0000215#ifdef DEBUG_SAX
216 printf("pythonEndDocument() called\n");
217#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000218 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000219 if (PyObject_HasAttrString(handler, (char *) "endDocument")) {
220 result =
221 PyObject_CallMethod(handler, (char *) "endDocument", NULL);
222 if (PyErr_Occurred())
223 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000224 Py_XDECREF(result);
225 }
226 /*
227 * The reference to the handler is released there
228 */
229 Py_XDECREF(handler);
230}
231
232static void
233pythonEndElement(void *user_data, const xmlChar * name)
234{
235 PyObject *handler;
236 PyObject *result;
237
Daniel Veillard797a5652002-02-12 13:46:21 +0000238#ifdef DEBUG_SAX
239 printf("pythonEndElement(%s) called\n", name);
240#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000241 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000242 if (PyObject_HasAttrString(handler, (char *) "endElement")) {
243 result = PyObject_CallMethod(handler, (char *) "endElement",
244 (char *) "s", name);
245 if (PyErr_Occurred())
246 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000247 Py_XDECREF(result);
Daniel Veillardd2379012002-03-15 22:24:56 +0000248 } else if (PyObject_HasAttrString(handler, (char *) "end")) {
249 result = PyObject_CallMethod(handler, (char *) "end",
250 (char *) "s", name);
251 if (PyErr_Occurred())
252 PyErr_Print();
Daniel Veillard797a5652002-02-12 13:46:21 +0000253 Py_XDECREF(result);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000254 }
255}
256
257static void
258pythonReference(void *user_data, const xmlChar * name)
259{
260 PyObject *handler;
261 PyObject *result;
262
Daniel Veillard797a5652002-02-12 13:46:21 +0000263#ifdef DEBUG_SAX
264 printf("pythonReference(%s) called\n", name);
265#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000266 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000267 if (PyObject_HasAttrString(handler, (char *) "reference")) {
268 result = PyObject_CallMethod(handler, (char *) "reference",
269 (char *) "s", name);
270 if (PyErr_Occurred())
271 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000272 Py_XDECREF(result);
273 }
274}
275
276static void
277pythonCharacters(void *user_data, const xmlChar * ch, int len)
278{
279 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +0000280 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000281 int type = 0;
282
Daniel Veillard797a5652002-02-12 13:46:21 +0000283#ifdef DEBUG_SAX
284 printf("pythonCharacters(%s, %d) called\n", ch, len);
285#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000286 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000287 if (PyObject_HasAttrString(handler, (char *) "characters"))
288 type = 1;
289 else if (PyObject_HasAttrString(handler, (char *) "data"))
290 type = 2;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000291 if (type != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000292 if (type == 1)
293 result = PyObject_CallMethod(handler, (char *) "characters",
294 (char *) "s#", ch, len);
295 else if (type == 2)
296 result = PyObject_CallMethod(handler, (char *) "data",
297 (char *) "s#", ch, len);
298 if (PyErr_Occurred())
299 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000300 Py_XDECREF(result);
301 }
302}
303
304static void
305pythonIgnorableWhitespace(void *user_data, const xmlChar * ch, int len)
306{
307 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +0000308 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000309 int type = 0;
310
Daniel Veillard797a5652002-02-12 13:46:21 +0000311#ifdef DEBUG_SAX
312 printf("pythonIgnorableWhitespace(%s, %d) called\n", ch, len);
313#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000314 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000315 if (PyObject_HasAttrString(handler, (char *) "ignorableWhitespace"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000316 type = 1;
Daniel Veillardd2379012002-03-15 22:24:56 +0000317 else if (PyObject_HasAttrString(handler, (char *) "data"))
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000318 type = 2;
319 if (type != 0) {
320 if (type == 1)
321 result =
Daniel Veillardd2379012002-03-15 22:24:56 +0000322 PyObject_CallMethod(handler,
323 (char *) "ignorableWhitespace",
324 (char *) "s#", ch, len);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000325 else if (type == 2)
Daniel Veillardd2379012002-03-15 22:24:56 +0000326 result =
327 PyObject_CallMethod(handler, (char *) "data",
328 (char *) "s#", ch, len);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000329 Py_XDECREF(result);
330 }
331}
332
333static void
334pythonProcessingInstruction(void *user_data,
335 const xmlChar * target, const xmlChar * data)
336{
337 PyObject *handler;
338 PyObject *result;
339
Daniel Veillard797a5652002-02-12 13:46:21 +0000340#ifdef DEBUG_SAX
341 printf("pythonProcessingInstruction(%s, %s) called\n", target, data);
342#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000343 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000344 if (PyObject_HasAttrString(handler, (char *) "processingInstruction")) {
345 result = PyObject_CallMethod(handler, (char *)
346 "processingInstruction",
347 (char *) "ss", target, data);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000348 Py_XDECREF(result);
349 }
350}
351
352static void
353pythonComment(void *user_data, const xmlChar * value)
354{
355 PyObject *handler;
356 PyObject *result;
357
Daniel Veillard797a5652002-02-12 13:46:21 +0000358#ifdef DEBUG_SAX
359 printf("pythonComment(%s) called\n", value);
360#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000361 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000362 if (PyObject_HasAttrString(handler, (char *) "comment")) {
363 result =
364 PyObject_CallMethod(handler, (char *) "comment", (char *) "s",
365 value);
366 if (PyErr_Occurred())
367 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000368 Py_XDECREF(result);
369 }
370}
371
372static void
373pythonWarning(void *user_data, const char *msg, ...)
374{
375 PyObject *handler;
376 PyObject *result;
377 va_list args;
378 char buf[1024];
379
Daniel Veillard797a5652002-02-12 13:46:21 +0000380#ifdef DEBUG_SAX
381 printf("pythonWarning(%s) called\n", msg);
382#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000383 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000384 if (PyObject_HasAttrString(handler, (char *) "warning")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000385 va_start(args, msg);
386 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +0000387 va_end(args);
388 buf[1023] = 0;
389 result =
390 PyObject_CallMethod(handler, (char *) "warning", (char *) "s",
391 buf);
392 if (PyErr_Occurred())
393 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000394 Py_XDECREF(result);
395 }
396}
397
398static void
399pythonError(void *user_data, const char *msg, ...)
400{
401 PyObject *handler;
402 PyObject *result;
403 va_list args;
404 char buf[1024];
405
Daniel Veillard797a5652002-02-12 13:46:21 +0000406#ifdef DEBUG_SAX
407 printf("pythonError(%s) called\n", msg);
408#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000409 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000410 if (PyObject_HasAttrString(handler, (char *) "error")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000411 va_start(args, msg);
412 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +0000413 va_end(args);
414 buf[1023] = 0;
415 result =
416 PyObject_CallMethod(handler, (char *) "error", (char *) "s",
417 buf);
418 if (PyErr_Occurred())
419 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000420 Py_XDECREF(result);
421 }
422}
423
424static void
425pythonFatalError(void *user_data, const char *msg, ...)
426{
427 PyObject *handler;
428 PyObject *result;
429 va_list args;
430 char buf[1024];
431
Daniel Veillard797a5652002-02-12 13:46:21 +0000432#ifdef DEBUG_SAX
433 printf("pythonFatalError(%s) called\n", msg);
434#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000435 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000436 if (PyObject_HasAttrString(handler, (char *) "fatalError")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000437 va_start(args, msg);
438 vsnprintf(buf, 1023, msg, args);
Daniel Veillardd2379012002-03-15 22:24:56 +0000439 va_end(args);
440 buf[1023] = 0;
441 result =
442 PyObject_CallMethod(handler, (char *) "fatalError",
443 (char *) "s", buf);
444 if (PyErr_Occurred())
445 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000446 Py_XDECREF(result);
447 }
448}
449
450static void
451pythonCdataBlock(void *user_data, const xmlChar * ch, int len)
452{
453 PyObject *handler;
Daniel Veillardd2379012002-03-15 22:24:56 +0000454 PyObject *result = NULL;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000455 int type = 0;
456
Daniel Veillard797a5652002-02-12 13:46:21 +0000457#ifdef DEBUG_SAX
458 printf("pythonCdataBlock(%s, %d) called\n", ch, len);
459#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000460 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000461 if (PyObject_HasAttrString(handler, (char *) "cdataBlock"))
462 type = 1;
463 else if (PyObject_HasAttrString(handler, (char *) "cdata"))
464 type = 2;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000465 if (type != 0) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000466 if (type == 1)
467 result =
468 PyObject_CallMethod(handler, (char *) "cdataBlock",
469 (char *) "s#", ch, len);
470 else if (type == 2)
471 result =
472 PyObject_CallMethod(handler, (char *) "cdata",
473 (char *) "s#", ch, len);
474 if (PyErr_Occurred())
475 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000476 Py_XDECREF(result);
477 }
478}
479
480static void
481pythonExternalSubset(void *user_data,
482 const xmlChar * name,
483 const xmlChar * externalID, const xmlChar * systemID)
484{
485 PyObject *handler;
486 PyObject *result;
487
Daniel Veillard797a5652002-02-12 13:46:21 +0000488#ifdef DEBUG_SAX
489 printf("pythonExternalSubset(%s, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +0000490 name, externalID, systemID);
Daniel Veillard797a5652002-02-12 13:46:21 +0000491#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000492 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000493 if (PyObject_HasAttrString(handler, (char *) "externalSubset")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000494 result =
Daniel Veillardd2379012002-03-15 22:24:56 +0000495 PyObject_CallMethod(handler, (char *) "externalSubset",
496 (char *) "sss", name, externalID,
497 systemID);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000498 Py_XDECREF(result);
499 }
500}
501
502static void
503pythonEntityDecl(void *user_data,
504 const xmlChar * name,
505 int type,
506 const xmlChar * publicId,
507 const xmlChar * systemId, xmlChar * content)
508{
509 PyObject *handler;
510 PyObject *result;
511
512 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000513 if (PyObject_HasAttrString(handler, (char *) "entityDecl")) {
514 result = PyObject_CallMethod(handler, (char *) "entityDecl",
515 (char *) "sisss", name, type,
516 publicId, systemId, content);
517 if (PyErr_Occurred())
518 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000519 Py_XDECREF(result);
520 }
521}
522
523
524
525static void
526
527pythonNotationDecl(void *user_data,
528 const xmlChar * name,
529 const xmlChar * publicId, const xmlChar * systemId)
530{
531 PyObject *handler;
532 PyObject *result;
533
534 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000535 if (PyObject_HasAttrString(handler, (char *) "notationDecl")) {
536 result = PyObject_CallMethod(handler, (char *) "notationDecl",
537 (char *) "sss", name, publicId,
538 systemId);
539 if (PyErr_Occurred())
540 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000541 Py_XDECREF(result);
542 }
543}
544
545static void
546pythonAttributeDecl(void *user_data,
547 const xmlChar * elem,
548 const xmlChar * name,
549 int type,
550 int def,
Daniel Veillardd2379012002-03-15 22:24:56 +0000551 const xmlChar * defaultValue, xmlEnumerationPtr tree)
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000552{
553 PyObject *handler;
554 PyObject *nameList;
555 PyObject *newName;
556 xmlEnumerationPtr node;
557 PyObject *result;
558 int count;
559
560 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000561 if (PyObject_HasAttrString(handler, (char *) "attributeDecl")) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000562 count = 0;
563 for (node = tree; node != NULL; node = node->next) {
564 count++;
565 }
566 nameList = PyList_New(count);
567 count = 0;
568 for (node = tree; node != NULL; node = node->next) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000569 newName = PyString_FromString((char *) node->name);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000570 PyList_SetItem(nameList, count, newName);
571 count++;
572 }
Daniel Veillardd2379012002-03-15 22:24:56 +0000573 result = PyObject_CallMethod(handler, (char *) "attributeDecl",
574 (char *) "ssiisO", elem, name, type,
575 def, defaultValue, nameList);
576 if (PyErr_Occurred())
577 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000578 Py_XDECREF(nameList);
579 Py_XDECREF(result);
580 }
581}
582
583static void
584pythonElementDecl(void *user_data,
585 const xmlChar * name,
Daniel Veillardd2379012002-03-15 22:24:56 +0000586 int type, ATTRIBUTE_UNUSED xmlElementContentPtr content)
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000587{
588 PyObject *handler;
589 PyObject *obj;
590 PyObject *result;
591
592 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000593 if (PyObject_HasAttrString(handler, (char *) "elementDecl")) {
594 /* TODO: wrap in an elementContent object */
595 printf
596 ("pythonElementDecl: xmlElementContentPtr wrapper missing !\n");
597 obj = Py_None;
598 /* Py_XINCREF(Py_None); isn't the reference just borrowed ??? */
599 result = PyObject_CallMethod(handler, (char *) "elementDecl",
600 (char *) "siO", name, type, obj);
601 if (PyErr_Occurred())
602 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000603 Py_XDECREF(result);
604 }
605}
606
607static void
608pythonUnparsedEntityDecl(void *user_data,
609 const xmlChar * name,
610 const xmlChar * publicId,
611 const xmlChar * systemId,
612 const xmlChar * notationName)
613{
614 PyObject *handler;
615 PyObject *result;
616
617 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000618 if (PyObject_HasAttrString(handler, (char *) "unparsedEntityDecl")) {
619 result =
620 PyObject_CallMethod(handler, (char *) "unparsedEntityDecl",
621 (char *) "ssss", name, publicId, systemId,
622 notationName);
623 if (PyErr_Occurred())
624 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000625 Py_XDECREF(result);
626 }
627}
628
629static void
630pythonInternalSubset(void *user_data, const xmlChar * name,
631 const xmlChar * ExternalID, const xmlChar * SystemID)
632{
633 PyObject *handler;
634 PyObject *result;
635
Daniel Veillard797a5652002-02-12 13:46:21 +0000636#ifdef DEBUG_SAX
637 printf("pythonInternalSubset(%s, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +0000638 name, ExternalID, SystemID);
Daniel Veillard797a5652002-02-12 13:46:21 +0000639#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000640 handler = (PyObject *) user_data;
Daniel Veillardd2379012002-03-15 22:24:56 +0000641 if (PyObject_HasAttrString(handler, (char *) "internalSubset")) {
642 result = PyObject_CallMethod(handler, (char *) "internalSubset",
643 (char *) "sss", name, ExternalID,
644 SystemID);
645 if (PyErr_Occurred())
646 PyErr_Print();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000647 Py_XDECREF(result);
648 }
649}
650
651static xmlSAXHandler pythonSaxHandler = {
652 pythonInternalSubset,
Daniel Veillardd2379012002-03-15 22:24:56 +0000653 NULL, /* TODO pythonIsStandalone, */
654 NULL, /* TODO pythonHasInternalSubset, */
655 NULL, /* TODO pythonHasExternalSubset, */
656 NULL, /* TODO pythonResolveEntity, */
657 NULL, /* TODO pythonGetEntity, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000658 pythonEntityDecl,
659 pythonNotationDecl,
660 pythonAttributeDecl,
661 pythonElementDecl,
662 pythonUnparsedEntityDecl,
Daniel Veillardd2379012002-03-15 22:24:56 +0000663 NULL, /* OBSOLETED pythonSetDocumentLocator, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000664 pythonStartDocument,
665 pythonEndDocument,
666 pythonStartElement,
667 pythonEndElement,
668 pythonReference,
669 pythonCharacters,
670 pythonIgnorableWhitespace,
671 pythonProcessingInstruction,
672 pythonComment,
673 pythonWarning,
674 pythonError,
675 pythonFatalError,
Daniel Veillardd2379012002-03-15 22:24:56 +0000676 NULL, /* TODO pythonGetParameterEntity, */
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000677 pythonCdataBlock,
678 pythonExternalSubset,
679 1
680};
Daniel Veillard3ce52572002-02-03 15:08:05 +0000681
682/************************************************************************
683 * *
684 * Handling of specific parser context *
685 * *
686 ************************************************************************/
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000687
688PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +0000689libxml_xmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
690 PyObject * args)
691{
692 const char *chunk;
Daniel Veillard3ce52572002-02-03 15:08:05 +0000693 int size;
Daniel Veillardd2379012002-03-15 22:24:56 +0000694 const char *URI;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000695 PyObject *pyobj_SAX = NULL;
Daniel Veillard3ce52572002-02-03 15:08:05 +0000696 xmlSAXHandlerPtr SAX = NULL;
Daniel Veillard3ce52572002-02-03 15:08:05 +0000697 xmlParserCtxtPtr ret;
698 PyObject *pyret;
Daniel Veillard96fe0952002-01-30 20:52:23 +0000699
Daniel Veillardd2379012002-03-15 22:24:56 +0000700 if (!PyArg_ParseTuple
701 (args, (char *) "Oziz:xmlCreatePushParser", &pyobj_SAX, &chunk,
702 &size, &URI))
703 return (NULL);
Daniel Veillard3ce52572002-02-03 15:08:05 +0000704
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000705#ifdef DEBUG
Daniel Veillard3ce52572002-02-03 15:08:05 +0000706 printf("libxml_xmlCreatePushParser(%p, %s, %d, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +0000707 pyobj_SAX, chunk, size, URI);
Daniel Veillard96fe0952002-01-30 20:52:23 +0000708#endif
Daniel Veillard3ce52572002-02-03 15:08:05 +0000709 if (pyobj_SAX != Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000710 SAX = &pythonSaxHandler;
711 Py_INCREF(pyobj_SAX);
712 /* The reference is released in pythonEndDocument() */
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000713 }
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000714 ret = xmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI);
Daniel Veillard3ce52572002-02-03 15:08:05 +0000715 pyret = libxml_xmlParserCtxtPtrWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +0000716 return (pyret);
Daniel Veillarda7340c82002-02-01 17:56:45 +0000717}
Daniel Veillard5d819032002-02-02 21:49:17 +0000718
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000719PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +0000720libxml_htmlCreatePushParser(ATTRIBUTE_UNUSED PyObject * self,
721 PyObject * args)
722{
723 const char *chunk;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000724 int size;
Daniel Veillardd2379012002-03-15 22:24:56 +0000725 const char *URI;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000726 PyObject *pyobj_SAX = NULL;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000727 xmlSAXHandlerPtr SAX = NULL;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000728 xmlParserCtxtPtr ret;
729 PyObject *pyret;
730
Daniel Veillardd2379012002-03-15 22:24:56 +0000731 if (!PyArg_ParseTuple
732 (args, (char *) "Oziz:htmlCreatePushParser", &pyobj_SAX, &chunk,
733 &size, &URI))
734 return (NULL);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000735
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000736#ifdef DEBUG
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000737 printf("libxml_htmlCreatePushParser(%p, %s, %d, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +0000738 pyobj_SAX, chunk, size, URI);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000739#endif
740 if (pyobj_SAX != Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000741 SAX = &pythonSaxHandler;
742 Py_INCREF(pyobj_SAX);
743 /* The reference is released in pythonEndDocument() */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000744 }
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000745 ret = htmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI,
Daniel Veillardd2379012002-03-15 22:24:56 +0000746 XML_CHAR_ENCODING_NONE);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000747 pyret = libxml_xmlParserCtxtPtrWrap(ret);
Daniel Veillardd2379012002-03-15 22:24:56 +0000748 return (pyret);
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000749}
750
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000751PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +0000752libxml_xmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
753{
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000754 int recover;
Daniel Veillardd2379012002-03-15 22:24:56 +0000755 const char *URI;
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000756 PyObject *pyobj_SAX = NULL;
757 xmlSAXHandlerPtr SAX = NULL;
758
Daniel Veillardd2379012002-03-15 22:24:56 +0000759 if (!PyArg_ParseTuple(args, (char *) "Osi:xmlSAXParseFile", &pyobj_SAX,
760 &URI, &recover))
761 return (NULL);
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000762
763#ifdef DEBUG
764 printf("libxml_xmlSAXParseFile(%p, %s, %d) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +0000765 pyobj_SAX, URI, recover);
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000766#endif
767 if (pyobj_SAX == Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000768 Py_INCREF(Py_None);
769 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000770 }
771 SAX = &pythonSaxHandler;
772 Py_INCREF(pyobj_SAX);
773 /* The reference is released in pythonEndDocument() */
774 xmlSAXParseFileWithData(SAX, URI, recover, pyobj_SAX);
775 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +0000776 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000777}
778
779PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +0000780libxml_htmlSAXParseFile(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
781{
782 const char *URI;
783 const char *encoding;
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000784 PyObject *pyobj_SAX = NULL;
785 xmlSAXHandlerPtr SAX = NULL;
786
Daniel Veillardd2379012002-03-15 22:24:56 +0000787 if (!PyArg_ParseTuple
788 (args, (char *) "Osz:htmlSAXParseFile", &pyobj_SAX, &URI,
789 &encoding))
790 return (NULL);
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000791
792#ifdef DEBUG
793 printf("libxml_htmlSAXParseFile(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +0000794 pyobj_SAX, URI, encoding);
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000795#endif
796 if (pyobj_SAX == Py_None) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000797 Py_INCREF(Py_None);
798 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000799 }
800 SAX = &pythonSaxHandler;
801 Py_INCREF(pyobj_SAX);
802 /* The reference is released in pythonEndDocument() */
803 htmlSAXParseFile(URI, encoding, SAX, pyobj_SAX);
804 Py_INCREF(Py_None);
Daniel Veillardd2379012002-03-15 22:24:56 +0000805 return (Py_None);
Daniel Veillard8d24cc12002-03-05 15:41:29 +0000806}
807
Daniel Veillard5d819032002-02-02 21:49:17 +0000808/************************************************************************
809 * *
810 * Error message callback *
811 * *
812 ************************************************************************/
813
814static PyObject *libxml_xmlPythonErrorFuncHandler = NULL;
815static PyObject *libxml_xmlPythonErrorFuncCtxt = NULL;
816
817static void
Daniel Veillardd2379012002-03-15 22:24:56 +0000818libxml_xmlErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, const char *msg,
819 ...)
820{
821 int size;
822 int chars;
823 char *larger;
824 va_list ap;
825 char *str;
Daniel Veillard5d819032002-02-02 21:49:17 +0000826 PyObject *list;
827 PyObject *message;
828 PyObject *result;
829
830#ifdef DEBUG_ERROR
831 printf("libxml_xmlErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
832#endif
833
834
835 if (libxml_xmlPythonErrorFuncHandler == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000836 va_start(ap, msg);
837 vfprintf(stdout, msg, ap);
838 va_end(ap);
Daniel Veillard5d819032002-02-02 21:49:17 +0000839 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +0000840 str = (char *) xmlMalloc(150);
841 if (str == NULL)
842 return;
Daniel Veillard5d819032002-02-02 21:49:17 +0000843
Daniel Veillardd2379012002-03-15 22:24:56 +0000844 size = 150;
Daniel Veillard5d819032002-02-02 21:49:17 +0000845
Daniel Veillardd2379012002-03-15 22:24:56 +0000846 while (1) {
847 va_start(ap, msg);
848 chars = vsnprintf(str, size, msg, ap);
849 va_end(ap);
850 if ((chars > -1) && (chars < size))
851 break;
852 if (chars > -1)
853 size += chars + 1;
854 else
855 size += 100;
856 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
857 xmlFree(str);
858 return;
859 }
860 str = larger;
861 }
Daniel Veillard5d819032002-02-02 21:49:17 +0000862
Daniel Veillardd2379012002-03-15 22:24:56 +0000863 list = PyTuple_New(2);
864 PyTuple_SetItem(list, 0, libxml_xmlPythonErrorFuncCtxt);
865 Py_XINCREF(libxml_xmlPythonErrorFuncCtxt);
866 message = libxml_charPtrWrap(str);
867 PyTuple_SetItem(list, 1, message);
868 result = PyEval_CallObject(libxml_xmlPythonErrorFuncHandler, list);
869 Py_XDECREF(list);
870 Py_XDECREF(result);
Daniel Veillard5d819032002-02-02 21:49:17 +0000871 }
872}
873
874static void
Daniel Veillardd2379012002-03-15 22:24:56 +0000875libxml_xmlErrorInitialize(void)
876{
Daniel Veillard5d819032002-02-02 21:49:17 +0000877#ifdef DEBUG_ERROR
878 printf("libxml_xmlErrorInitialize() called\n");
879#endif
880 xmlSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
881}
882
883PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +0000884libxml_xmlRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self,
885 PyObject * args)
886{
Daniel Veillard5d819032002-02-02 21:49:17 +0000887 PyObject *py_retval;
888 PyObject *pyobj_f;
889 PyObject *pyobj_ctx;
890
Daniel Veillardd2379012002-03-15 22:24:56 +0000891 if (!PyArg_ParseTuple
892 (args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f,
893 &pyobj_ctx))
894 return (NULL);
Daniel Veillard5d819032002-02-02 21:49:17 +0000895
896#ifdef DEBUG_ERROR
Daniel Veillardd2379012002-03-15 22:24:56 +0000897 printf("libxml_registerXPathFunction(%p, %p) called\n", pyobj_ctx,
898 pyobj_f);
Daniel Veillard5d819032002-02-02 21:49:17 +0000899#endif
900
901 if (libxml_xmlPythonErrorFuncHandler != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000902 Py_XDECREF(libxml_xmlPythonErrorFuncHandler);
Daniel Veillard5d819032002-02-02 21:49:17 +0000903 }
904 if (libxml_xmlPythonErrorFuncCtxt != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000905 Py_XDECREF(libxml_xmlPythonErrorFuncCtxt);
Daniel Veillard5d819032002-02-02 21:49:17 +0000906 }
907
908 Py_XINCREF(pyobj_ctx);
909 Py_XINCREF(pyobj_f);
910
911 /* TODO: check f is a function ! */
912 libxml_xmlPythonErrorFuncHandler = pyobj_f;
913 libxml_xmlPythonErrorFuncCtxt = pyobj_ctx;
914
915 py_retval = libxml_intWrap(1);
Daniel Veillardd2379012002-03-15 22:24:56 +0000916 return (py_retval);
Daniel Veillard5d819032002-02-02 21:49:17 +0000917}
Daniel Veillardd2379012002-03-15 22:24:56 +0000918
Daniel Veillarda7340c82002-02-01 17:56:45 +0000919/************************************************************************
920 * *
921 * XPath extensions *
922 * *
923 ************************************************************************/
924
925static int libxml_xpathCallbacksInitialized = 0;
926
927typedef struct libxml_xpathCallback {
928 xmlXPathContextPtr ctx;
929 xmlChar *name;
930 xmlChar *ns_uri;
931 PyObject *function;
932} libxml_xpathCallback, *libxml_xpathCallbackPtr;
933static libxml_xpathCallback libxml_xpathCallbacks[10];
934static int libxml_xpathCallbacksNb = 0;
935static int libxml_xpathCallbacksMax = 10;
936
Daniel Veillarda7340c82002-02-01 17:56:45 +0000937static void
Daniel Veillardd2379012002-03-15 22:24:56 +0000938libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs)
939{
Daniel Veillarda7340c82002-02-01 17:56:45 +0000940 PyObject *list, *cur, *result;
941 xmlXPathObjectPtr obj;
Daniel Veillard70cab352002-02-06 16:06:58 +0000942 xmlXPathContextPtr rctxt;
943 PyObject *current_function = NULL;
944 const xmlChar *name;
945 const xmlChar *ns_uri;
Daniel Veillarda7340c82002-02-01 17:56:45 +0000946 int i;
947
Daniel Veillard70cab352002-02-06 16:06:58 +0000948 if (ctxt == NULL)
Daniel Veillardd2379012002-03-15 22:24:56 +0000949 return;
Daniel Veillard70cab352002-02-06 16:06:58 +0000950 rctxt = ctxt->context;
951 if (rctxt == NULL)
Daniel Veillardd2379012002-03-15 22:24:56 +0000952 return;
Daniel Veillard70cab352002-02-06 16:06:58 +0000953 name = rctxt->function;
954 ns_uri = rctxt->functionURI;
Daniel Veillarda7340c82002-02-01 17:56:45 +0000955#ifdef DEBUG_XPATH
Daniel Veillardd2379012002-03-15 22:24:56 +0000956 printf("libxml_xmlXPathFuncCallback called name %s URI %s\n", name,
957 ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +0000958#endif
959
Daniel Veillard70cab352002-02-06 16:06:58 +0000960 /*
961 * Find the function, it should be there it was there at lookup
962 */
Daniel Veillardd2379012002-03-15 22:24:56 +0000963 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
964 if ( /* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */
965 (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) &&
966 (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) {
967 current_function = libxml_xpathCallbacks[i].function;
968 }
Daniel Veillard70cab352002-02-06 16:06:58 +0000969 }
970 if (current_function == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +0000971 printf
972 ("libxml_xmlXPathFuncCallback: internal error %s not found !\n",
973 name);
974 return;
Daniel Veillard70cab352002-02-06 16:06:58 +0000975 }
976
Daniel Veillardc575b992002-02-08 13:28:40 +0000977 list = PyTuple_New(nargs + 1);
978 PyTuple_SetItem(list, 0, libxml_xmlXPathParserContextPtrWrap(ctxt));
Daniel Veillardd2379012002-03-15 22:24:56 +0000979 for (i = 0; i < nargs; i++) {
980 obj = valuePop(ctxt);
981 cur = libxml_xmlXPathObjectPtrWrap(obj);
982 PyTuple_SetItem(list, i + 1, cur);
Daniel Veillarda7340c82002-02-01 17:56:45 +0000983 }
984 result = PyEval_CallObject(current_function, list);
985 Py_DECREF(list);
986
987 obj = libxml_xmlXPathObjectPtrConvert(result);
988 valuePush(ctxt, obj);
989}
990
991static xmlXPathFunction
Daniel Veillardd2379012002-03-15 22:24:56 +0000992libxml_xmlXPathFuncLookupFunc(void *ctxt, const xmlChar * name,
993 const xmlChar * ns_uri)
994{
Daniel Veillarda7340c82002-02-01 17:56:45 +0000995 int i;
Daniel Veillardd2379012002-03-15 22:24:56 +0000996
Daniel Veillarda7340c82002-02-01 17:56:45 +0000997#ifdef DEBUG_XPATH
998 printf("libxml_xmlXPathFuncLookupFunc(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +0000999 ctxt, name, ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001000#endif
Daniel Veillard70cab352002-02-06 16:06:58 +00001001 /*
1002 * This is called once only. The address is then stored in the
1003 * XPath expression evaluation, the proper object to call can
1004 * then still be found using the execution context function
1005 * and functionURI fields.
1006 */
Daniel Veillardd2379012002-03-15 22:24:56 +00001007 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
1008 if ((ctxt == libxml_xpathCallbacks[i].ctx) &&
1009 (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) &&
1010 (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) {
1011 return (libxml_xmlXPathFuncCallback);
1012 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00001013 }
Daniel Veillardd2379012002-03-15 22:24:56 +00001014 return (NULL);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001015}
1016
1017static void
Daniel Veillardd2379012002-03-15 22:24:56 +00001018libxml_xpathCallbacksInitialize(void)
1019{
Daniel Veillarda7340c82002-02-01 17:56:45 +00001020 int i;
1021
1022 if (libxml_xpathCallbacksInitialized != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00001023 return;
Daniel Veillarda7340c82002-02-01 17:56:45 +00001024
1025#ifdef DEBUG_XPATH
1026 printf("libxml_xpathCallbacksInitialized called\n");
1027#endif
1028
Daniel Veillardd2379012002-03-15 22:24:56 +00001029 for (i = 0; i < 10; i++) {
1030 libxml_xpathCallbacks[i].ctx = NULL;
1031 libxml_xpathCallbacks[i].name = NULL;
1032 libxml_xpathCallbacks[i].ns_uri = NULL;
1033 libxml_xpathCallbacks[i].function = NULL;
Daniel Veillarda7340c82002-02-01 17:56:45 +00001034 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00001035 libxml_xpathCallbacksInitialized = 1;
1036}
1037
1038PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001039libxml_xmlRegisterXPathFunction(ATTRIBUTE_UNUSED PyObject * self,
1040 PyObject * args)
1041{
Daniel Veillarda7340c82002-02-01 17:56:45 +00001042 PyObject *py_retval;
1043 int c_retval = 0;
1044 xmlChar *name;
1045 xmlChar *ns_uri;
1046 xmlXPathContextPtr ctx;
1047 PyObject *pyobj_ctx;
1048 PyObject *pyobj_f;
1049 int i;
1050
Daniel Veillardd2379012002-03-15 22:24:56 +00001051 if (!PyArg_ParseTuple
1052 (args, (char *) "OszO:registerXPathFunction", &pyobj_ctx, &name,
1053 &ns_uri, &pyobj_f))
1054 return (NULL);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001055
1056 ctx = (xmlXPathContextPtr) PyxmlXPathContext_Get(pyobj_ctx);
1057 if (libxml_xpathCallbacksInitialized == 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00001058 libxml_xpathCallbacksInitialize();
Daniel Veillarda7340c82002-02-01 17:56:45 +00001059 xmlXPathRegisterFuncLookup(ctx, libxml_xmlXPathFuncLookupFunc, ctx);
1060
1061 if ((pyobj_ctx == NULL) || (name == NULL) || (pyobj_f == NULL)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001062 py_retval = libxml_intWrap(-1);
1063 return (py_retval);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001064 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00001065#ifdef DEBUG_XPATH
1066 printf("libxml_registerXPathFunction(%p, %s, %s) called\n",
Daniel Veillardd2379012002-03-15 22:24:56 +00001067 ctx, name, ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001068#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001069 for (i = 0; i < libxml_xpathCallbacksNb; i++) {
1070 if ((ctx == libxml_xpathCallbacks[i].ctx) &&
1071 (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) &&
1072 (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) {
1073 Py_XINCREF(pyobj_f);
1074 Py_XDECREF(libxml_xpathCallbacks[i].function);
1075 libxml_xpathCallbacks[i].function = pyobj_f;
1076 c_retval = 1;
1077 goto done;
1078 }
Daniel Veillarda7340c82002-02-01 17:56:45 +00001079 }
1080 if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksMax) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001081 printf("libxml_registerXPathFunction() table full\n");
Daniel Veillarda7340c82002-02-01 17:56:45 +00001082 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00001083 i = libxml_xpathCallbacksNb++;
1084 Py_XINCREF(pyobj_f);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001085 libxml_xpathCallbacks[i].ctx = ctx;
1086 libxml_xpathCallbacks[i].name = xmlStrdup(name);
1087 libxml_xpathCallbacks[i].ns_uri = xmlStrdup(ns_uri);
Daniel Veillardd2379012002-03-15 22:24:56 +00001088 libxml_xpathCallbacks[i].function = pyobj_f;
1089 c_retval = 1;
Daniel Veillarda7340c82002-02-01 17:56:45 +00001090 }
Daniel Veillardd2379012002-03-15 22:24:56 +00001091 done:
Daniel Veillarda7340c82002-02-01 17:56:45 +00001092 py_retval = libxml_intWrap((int) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00001093 return (py_retval);
Daniel Veillarda7340c82002-02-01 17:56:45 +00001094}
1095
Daniel Veillard1971ee22002-01-31 20:29:19 +00001096/************************************************************************
1097 * *
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001098 * Global properties access *
1099 * *
1100 ************************************************************************/
1101static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001102libxml_name(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001103{
1104 PyObject *resultobj, *obj;
1105 xmlNodePtr cur;
1106 const xmlChar *res;
1107
Daniel Veillardd2379012002-03-15 22:24:56 +00001108 if (!PyArg_ParseTuple(args, (char *) "O:name", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001109 return NULL;
1110 cur = PyxmlNode_Get(obj);
1111
1112#ifdef DEBUG
1113 printf("libxml_name: cur = %p type %d\n", cur, cur->type);
1114#endif
1115
Daniel Veillardd2379012002-03-15 22:24:56 +00001116 switch (cur->type) {
1117 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001118#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001119 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001120#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001121 case XML_HTML_DOCUMENT_NODE:{
1122 xmlDocPtr doc = (xmlDocPtr) cur;
1123
1124 res = doc->URL;
1125 break;
1126 }
1127 case XML_ATTRIBUTE_NODE:{
1128 xmlAttrPtr attr = (xmlAttrPtr) cur;
1129
1130 res = attr->name;
1131 break;
1132 }
1133 case XML_NAMESPACE_DECL:{
1134 xmlNsPtr ns = (xmlNsPtr) cur;
1135
1136 res = ns->prefix;
1137 break;
1138 }
1139 default:
1140 res = cur->name;
1141 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001142 }
Daniel Veillard1971ee22002-01-31 20:29:19 +00001143 resultobj = libxml_constxmlCharPtrWrap(res);
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001144
1145 return resultobj;
1146}
1147
1148static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001149libxml_doc(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001150{
1151 PyObject *resultobj, *obj;
1152 xmlNodePtr cur;
1153 xmlDocPtr res;
1154
Daniel Veillardd2379012002-03-15 22:24:56 +00001155 if (!PyArg_ParseTuple(args, (char *) "O:doc", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001156 return NULL;
1157 cur = PyxmlNode_Get(obj);
1158
1159#ifdef DEBUG
1160 printf("libxml_doc: cur = %p\n", cur);
1161#endif
1162
Daniel Veillardd2379012002-03-15 22:24:56 +00001163 switch (cur->type) {
1164 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001165#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001166 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001167#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001168 case XML_HTML_DOCUMENT_NODE:
1169 res = NULL;
1170 break;
1171 case XML_ATTRIBUTE_NODE:{
1172 xmlAttrPtr attr = (xmlAttrPtr) cur;
1173
1174 res = attr->doc;
1175 break;
1176 }
1177 case XML_NAMESPACE_DECL:
1178 res = NULL;
1179 break;
1180 default:
1181 res = cur->doc;
1182 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001183 }
1184 resultobj = libxml_xmlDocPtrWrap(res);
1185 return resultobj;
1186}
1187
1188static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001189libxml_properties(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001190{
1191 PyObject *resultobj, *obj;
1192 xmlNodePtr cur = NULL;
1193 xmlAttrPtr res;
1194
Daniel Veillardd2379012002-03-15 22:24:56 +00001195 if (!PyArg_ParseTuple(args, (char *) "O:properties", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001196 return NULL;
1197 cur = PyxmlNode_Get(obj);
1198 if (cur->type == XML_ELEMENT_NODE)
Daniel Veillardd2379012002-03-15 22:24:56 +00001199 res = cur->properties;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001200 else
Daniel Veillardd2379012002-03-15 22:24:56 +00001201 res = NULL;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001202 resultobj = libxml_xmlAttrPtrWrap(res);
1203 return resultobj;
1204}
1205
1206static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001207libxml_next(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001208{
1209 PyObject *resultobj, *obj;
1210 xmlNodePtr cur;
1211 xmlNodePtr res;
1212
Daniel Veillardd2379012002-03-15 22:24:56 +00001213 if (!PyArg_ParseTuple(args, (char *) "O:next", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001214 return NULL;
1215 cur = PyxmlNode_Get(obj);
1216
1217#ifdef DEBUG
1218 printf("libxml_next: cur = %p\n", cur);
1219#endif
1220
Daniel Veillardd2379012002-03-15 22:24:56 +00001221 switch (cur->type) {
1222 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001223#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001224 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001225#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001226 case XML_HTML_DOCUMENT_NODE:
1227 res = NULL;
1228 break;
1229 case XML_ATTRIBUTE_NODE:{
1230 xmlAttrPtr attr = (xmlAttrPtr) cur;
1231
1232 res = (xmlNodePtr) attr->next;
1233 break;
1234 }
1235 case XML_NAMESPACE_DECL:{
1236 xmlNsPtr ns = (xmlNsPtr) cur;
1237
1238 res = (xmlNodePtr) ns->next;
1239 break;
1240 }
1241 default:
1242 res = cur->next;
1243 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001244
1245 }
1246 resultobj = libxml_xmlNodePtrWrap(res);
1247 return resultobj;
1248}
1249
1250static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001251libxml_prev(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001252{
1253 PyObject *resultobj, *obj;
1254 xmlNodePtr cur;
1255 xmlNodePtr res;
1256
Daniel Veillardd2379012002-03-15 22:24:56 +00001257 if (!PyArg_ParseTuple(args, (char *) "O:prev", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001258 return NULL;
1259 cur = PyxmlNode_Get(obj);
1260
1261#ifdef DEBUG
1262 printf("libxml_prev: cur = %p\n", cur);
1263#endif
1264
Daniel Veillardd2379012002-03-15 22:24:56 +00001265 switch (cur->type) {
1266 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001267#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001268 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001269#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001270 case XML_HTML_DOCUMENT_NODE:
1271 res = NULL;
1272 break;
1273 case XML_ATTRIBUTE_NODE:{
1274 xmlAttrPtr attr = (xmlAttrPtr) cur;
1275
1276 res = (xmlNodePtr) attr->next;
1277 }
1278 case XML_NAMESPACE_DECL:
1279 res = NULL;
1280 break;
1281 default:
1282 res = cur->next;
1283 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001284 }
1285 resultobj = libxml_xmlNodePtrWrap(res);
1286 return resultobj;
1287}
1288
1289static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001290libxml_children(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001291{
1292 PyObject *resultobj, *obj;
1293 xmlNodePtr cur;
1294 xmlNodePtr res;
1295
Daniel Veillardd2379012002-03-15 22:24:56 +00001296 if (!PyArg_ParseTuple(args, (char *) "O:children", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001297 return NULL;
1298 cur = PyxmlNode_Get(obj);
1299
1300#ifdef DEBUG
1301 printf("libxml_children: cur = %p\n", cur);
1302#endif
1303
Daniel Veillardd2379012002-03-15 22:24:56 +00001304 switch (cur->type) {
1305 case XML_ELEMENT_NODE:
1306 case XML_ENTITY_REF_NODE:
1307 case XML_ENTITY_NODE:
1308 case XML_PI_NODE:
1309 case XML_COMMENT_NODE:
1310 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001311#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001312 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001313#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001314 case XML_HTML_DOCUMENT_NODE:
1315 case XML_DTD_NODE:
1316 res = cur->children;
1317 break;
1318 case XML_ATTRIBUTE_NODE:{
1319 xmlAttrPtr attr = (xmlAttrPtr) cur;
1320
1321 res = attr->children;
1322 break;
1323 }
1324 default:
1325 res = NULL;
1326 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001327 }
1328 resultobj = libxml_xmlNodePtrWrap(res);
1329 return resultobj;
1330}
1331
1332static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001333libxml_last(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001334{
1335 PyObject *resultobj, *obj;
1336 xmlNodePtr cur;
1337 xmlNodePtr res;
1338
Daniel Veillardd2379012002-03-15 22:24:56 +00001339 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001340 return NULL;
1341 cur = PyxmlNode_Get(obj);
1342
1343#ifdef DEBUG
1344 printf("libxml_last: cur = %p\n", cur);
1345#endif
1346
Daniel Veillardd2379012002-03-15 22:24:56 +00001347 switch (cur->type) {
1348 case XML_ELEMENT_NODE:
1349 case XML_ENTITY_REF_NODE:
1350 case XML_ENTITY_NODE:
1351 case XML_PI_NODE:
1352 case XML_COMMENT_NODE:
1353 case XML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001354#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001355 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001356#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001357 case XML_HTML_DOCUMENT_NODE:
1358 case XML_DTD_NODE:
1359 res = cur->last;
1360 break;
1361 case XML_ATTRIBUTE_NODE:{
1362 xmlAttrPtr attr = (xmlAttrPtr) cur;
1363
1364 res = attr->last;
1365 }
1366 default:
1367 res = NULL;
1368 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001369 }
1370 resultobj = libxml_xmlNodePtrWrap(res);
1371 return resultobj;
1372}
1373
1374static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001375libxml_parent(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001376{
1377 PyObject *resultobj, *obj;
1378 xmlNodePtr cur;
1379 xmlNodePtr res;
1380
Daniel Veillardd2379012002-03-15 22:24:56 +00001381 if (!PyArg_ParseTuple(args, (char *) "O:parent", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001382 return NULL;
1383 cur = PyxmlNode_Get(obj);
1384
1385#ifdef DEBUG
1386 printf("libxml_parent: cur = %p\n", cur);
1387#endif
1388
Daniel Veillardd2379012002-03-15 22:24:56 +00001389 switch (cur->type) {
1390 case XML_DOCUMENT_NODE:
1391 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001392#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001393 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001394#endif
Daniel Veillardd2379012002-03-15 22:24:56 +00001395 res = NULL;
1396 break;
1397 case XML_ATTRIBUTE_NODE:{
1398 xmlAttrPtr attr = (xmlAttrPtr) cur;
1399
1400 res = attr->parent;
1401 }
1402 case XML_ENTITY_DECL:
1403 case XML_NAMESPACE_DECL:
1404 case XML_XINCLUDE_START:
1405 case XML_XINCLUDE_END:
1406 res = NULL;
1407 break;
1408 default:
1409 res = cur->parent;
1410 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001411 }
1412 resultobj = libxml_xmlNodePtrWrap(res);
1413 return resultobj;
1414}
1415
1416static PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001417libxml_type(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001418{
1419 PyObject *resultobj, *obj;
1420 xmlNodePtr cur;
Daniel Veillardd2379012002-03-15 22:24:56 +00001421 const xmlChar *res = NULL;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001422
Daniel Veillardd2379012002-03-15 22:24:56 +00001423 if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001424 return NULL;
1425 cur = PyxmlNode_Get(obj);
1426
1427#ifdef DEBUG
1428 printf("libxml_type: cur = %p\n", cur);
1429#endif
1430
Daniel Veillardd2379012002-03-15 22:24:56 +00001431 switch (cur->type) {
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001432 case XML_ELEMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001433 res = (const xmlChar *) "element";
1434 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001435 case XML_ATTRIBUTE_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001436 res = (const xmlChar *) "attribute";
1437 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001438 case XML_TEXT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001439 res = (const xmlChar *) "text";
1440 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001441 case XML_CDATA_SECTION_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001442 res = (const xmlChar *) "cdata";
1443 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001444 case XML_ENTITY_REF_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001445 res = (const xmlChar *) "entity_ref";
1446 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001447 case XML_ENTITY_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001448 res = (const xmlChar *) "entity";
1449 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001450 case XML_PI_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001451 res = (const xmlChar *) "pi";
1452 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001453 case XML_COMMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001454 res = (const xmlChar *) "comment";
1455 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001456 case XML_DOCUMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001457 res = (const xmlChar *) "document_xml";
1458 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001459 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001460 res = (const xmlChar *) "doctype";
1461 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001462 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001463 res = (const xmlChar *) "fragment";
1464 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001465 case XML_NOTATION_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001466 res = (const xmlChar *) "notation";
1467 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001468 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001469 res = (const xmlChar *) "document_html";
1470 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001471 case XML_DTD_NODE:
Daniel Veillardd2379012002-03-15 22:24:56 +00001472 res = (const xmlChar *) "dtd";
1473 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001474 case XML_ELEMENT_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00001475 res = (const xmlChar *) "elem_decl";
1476 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001477 case XML_ATTRIBUTE_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00001478 res = (const xmlChar *) "attribute_decl";
1479 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001480 case XML_ENTITY_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00001481 res = (const xmlChar *) "entity_decl";
1482 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001483 case XML_NAMESPACE_DECL:
Daniel Veillardd2379012002-03-15 22:24:56 +00001484 res = (const xmlChar *) "namespace";
1485 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001486 case XML_XINCLUDE_START:
Daniel Veillardd2379012002-03-15 22:24:56 +00001487 res = (const xmlChar *) "xinclude_start";
1488 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001489 case XML_XINCLUDE_END:
Daniel Veillardd2379012002-03-15 22:24:56 +00001490 res = (const xmlChar *) "xinclude_end";
1491 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001492#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd2379012002-03-15 22:24:56 +00001493 case XML_DOCB_DOCUMENT_NODE:
1494 res = (const xmlChar *) "document_docbook";
1495 break;
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001496#endif
1497 }
1498#ifdef DEBUG
1499 printf("libxml_type: cur = %p: %s\n", cur, res);
1500#endif
1501
Daniel Veillard1971ee22002-01-31 20:29:19 +00001502 resultobj = libxml_constxmlCharPtrWrap(res);
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001503 return resultobj;
1504}
1505
1506/************************************************************************
1507 * *
Daniel Veillard36eea2d2002-02-04 00:17:01 +00001508 * Specific accessor functions *
1509 * *
1510 ************************************************************************/
1511PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001512libxml_xmlNodeGetNsDefs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1513{
Daniel Veillard36eea2d2002-02-04 00:17:01 +00001514 PyObject *py_retval;
1515 xmlNsPtr c_retval;
1516 xmlNodePtr node;
1517 PyObject *pyobj_node;
1518
Daniel Veillardd2379012002-03-15 22:24:56 +00001519 if (!PyArg_ParseTuple
1520 (args, (char *) "O:xmlNodeGetNsDefs", &pyobj_node))
1521 return (NULL);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00001522 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
1523
1524 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001525 Py_INCREF(Py_None);
1526 return (Py_None);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00001527 }
1528 c_retval = node->nsDef;
1529 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00001530 return (py_retval);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00001531}
1532
1533PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001534libxml_xmlNodeGetNs(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1535{
Daniel Veillard36eea2d2002-02-04 00:17:01 +00001536 PyObject *py_retval;
1537 xmlNsPtr c_retval;
1538 xmlNodePtr node;
1539 PyObject *pyobj_node;
1540
Daniel Veillardd2379012002-03-15 22:24:56 +00001541 if (!PyArg_ParseTuple(args, (char *) "O:xmlNodeGetNs", &pyobj_node))
1542 return (NULL);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00001543 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
1544
1545 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001546 Py_INCREF(Py_None);
1547 return (Py_None);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00001548 }
1549 c_retval = node->ns;
1550 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
Daniel Veillardd2379012002-03-15 22:24:56 +00001551 return (py_retval);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00001552}
1553
1554/************************************************************************
1555 * *
Daniel Veillard1e774382002-03-06 17:35:40 +00001556 * Serialization front-end *
1557 * *
1558 ************************************************************************/
1559
Daniel Veillardd2379012002-03-15 22:24:56 +00001560static PyObject *
1561libxml_serializeNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1562{
Daniel Veillard1e774382002-03-06 17:35:40 +00001563 PyObject *py_retval = NULL;
1564 xmlChar *c_retval;
1565 PyObject *pyobj_node;
1566 xmlNodePtr node;
1567 xmlDocPtr doc;
Daniel Veillardd2379012002-03-15 22:24:56 +00001568 const char *encoding;
Daniel Veillard1e774382002-03-06 17:35:40 +00001569 int format;
1570 int len;
1571
Daniel Veillardd2379012002-03-15 22:24:56 +00001572 if (!PyArg_ParseTuple(args, (char *) "Ozi:serializeNode", &pyobj_node,
1573 &encoding, &format))
1574 return (NULL);
Daniel Veillard1e774382002-03-06 17:35:40 +00001575 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
1576
1577 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001578 Py_INCREF(Py_None);
1579 return (Py_None);
Daniel Veillard1e774382002-03-06 17:35:40 +00001580 }
1581 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001582 doc = (xmlDocPtr) node;
1583 xmlDocDumpFormatMemoryEnc(doc, &c_retval, &len,
1584 (const char *) encoding, format);
1585 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard1e774382002-03-06 17:35:40 +00001586 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001587 xmlOutputBufferPtr buf;
1588 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00001589
Daniel Veillardd2379012002-03-15 22:24:56 +00001590 doc = (xmlDocPtr) node;
1591 if (encoding != NULL)
1592 htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
1593 encoding = (const char *) htmlGetMetaEncoding(doc);
Daniel Veillard1e774382002-03-06 17:35:40 +00001594
Daniel Veillardd2379012002-03-15 22:24:56 +00001595 if (encoding != NULL) {
1596 handler = xmlFindCharEncodingHandler(encoding);
1597 if (handler == NULL) {
1598 Py_INCREF(Py_None);
1599 return (Py_None);
1600 }
1601 }
Daniel Veillard1e774382002-03-06 17:35:40 +00001602
Daniel Veillardd2379012002-03-15 22:24:56 +00001603 /*
1604 * Fallback to HTML or ASCII when the encoding is unspecified
1605 */
1606 if (handler == NULL)
1607 handler = xmlFindCharEncodingHandler("HTML");
1608 if (handler == NULL)
1609 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00001610
Daniel Veillardd2379012002-03-15 22:24:56 +00001611 buf = xmlAllocOutputBuffer(handler);
1612 if (buf == NULL) {
1613 Py_INCREF(Py_None);
1614 return (Py_None);
1615 }
1616 htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
1617 xmlOutputBufferFlush(buf);
1618 if (buf->conv != NULL) {
1619 len = buf->conv->use;
1620 c_retval = buf->conv->content;
1621 buf->conv->content = NULL;
1622 } else {
1623 len = buf->buffer->use;
1624 c_retval = buf->buffer->content;
1625 buf->buffer->content = NULL;
1626 }
1627 (void) xmlOutputBufferClose(buf);
1628 py_retval = libxml_charPtrWrap((char *) c_retval);
Daniel Veillard1e774382002-03-06 17:35:40 +00001629 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00001630 doc = node->doc;
1631 if (doc->type == XML_DOCUMENT_NODE) {
1632 xmlOutputBufferPtr buf;
1633 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00001634
Daniel Veillardd2379012002-03-15 22:24:56 +00001635 if (encoding != NULL) {
1636 handler = xmlFindCharEncodingHandler(encoding);
1637 if (handler == NULL) {
1638 Py_INCREF(Py_None);
1639 return (Py_None);
1640 }
1641 }
Daniel Veillard1e774382002-03-06 17:35:40 +00001642
Daniel Veillardd2379012002-03-15 22:24:56 +00001643 buf = xmlAllocOutputBuffer(handler);
1644 if (buf == NULL) {
1645 Py_INCREF(Py_None);
1646 return (Py_None);
1647 }
1648 xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
1649 xmlOutputBufferFlush(buf);
1650 if (buf->conv != NULL) {
1651 len = buf->conv->use;
1652 c_retval = buf->conv->content;
1653 buf->conv->content = NULL;
1654 } else {
1655 len = buf->buffer->use;
1656 c_retval = buf->buffer->content;
1657 buf->buffer->content = NULL;
1658 }
1659 (void) xmlOutputBufferClose(buf);
1660 py_retval = libxml_charPtrWrap((char *) c_retval);
1661 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
1662 xmlOutputBufferPtr buf;
1663 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00001664
Daniel Veillardd2379012002-03-15 22:24:56 +00001665 if (encoding != NULL)
1666 htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
1667 encoding = (const char *) htmlGetMetaEncoding(doc);
1668 if (encoding != NULL) {
1669 handler = xmlFindCharEncodingHandler(encoding);
1670 if (handler == NULL) {
1671 Py_INCREF(Py_None);
1672 return (Py_None);
1673 }
1674 }
Daniel Veillard1e774382002-03-06 17:35:40 +00001675
Daniel Veillardd2379012002-03-15 22:24:56 +00001676 /*
1677 * Fallback to HTML or ASCII when the encoding is unspecified
1678 */
1679 if (handler == NULL)
1680 handler = xmlFindCharEncodingHandler("HTML");
1681 if (handler == NULL)
1682 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00001683
Daniel Veillardd2379012002-03-15 22:24:56 +00001684 buf = xmlAllocOutputBuffer(handler);
1685 if (buf == NULL) {
1686 Py_INCREF(Py_None);
1687 return (Py_None);
1688 }
1689 htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
1690 xmlOutputBufferFlush(buf);
1691 if (buf->conv != NULL) {
1692 len = buf->conv->use;
1693 c_retval = buf->conv->content;
1694 buf->conv->content = NULL;
1695 } else {
1696 len = buf->buffer->use;
1697 c_retval = buf->buffer->content;
1698 buf->buffer->content = NULL;
1699 }
1700 (void) xmlOutputBufferClose(buf);
1701 py_retval = libxml_charPtrWrap((char *) c_retval);
1702 } else {
1703 Py_INCREF(Py_None);
1704 return (Py_None);
1705 }
Daniel Veillard1e774382002-03-06 17:35:40 +00001706 }
Daniel Veillardd2379012002-03-15 22:24:56 +00001707 return (py_retval);
Daniel Veillard1e774382002-03-06 17:35:40 +00001708}
1709
Daniel Veillardd2379012002-03-15 22:24:56 +00001710static PyObject *
1711libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1712{
Daniel Veillard1e774382002-03-06 17:35:40 +00001713 PyObject *py_file = NULL;
1714 FILE *output;
1715 PyObject *pyobj_node;
1716 xmlNodePtr node;
1717 xmlDocPtr doc;
Daniel Veillardd2379012002-03-15 22:24:56 +00001718 const char *encoding;
Daniel Veillard1e774382002-03-06 17:35:40 +00001719 int format;
1720 int len;
1721 xmlOutputBufferPtr buf;
1722 xmlCharEncodingHandlerPtr handler = NULL;
1723
Daniel Veillardd2379012002-03-15 22:24:56 +00001724 if (!PyArg_ParseTuple(args, (char *) "OOzi:serializeNode", &pyobj_node,
1725 &py_file, &encoding, &format))
1726 return (NULL);
Daniel Veillard1e774382002-03-06 17:35:40 +00001727 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
1728
1729 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001730 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00001731 }
1732 if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001733 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00001734 }
1735 output = PyFile_AsFile(py_file);
1736 if (output == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001737 return (PyInt_FromLong((long) -1));
Daniel Veillard1e774382002-03-06 17:35:40 +00001738 }
1739
1740 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001741 doc = (xmlDocPtr) node;
Daniel Veillard1e774382002-03-06 17:35:40 +00001742 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001743 doc = (xmlDocPtr) node;
Daniel Veillard1e774382002-03-06 17:35:40 +00001744 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00001745 doc = node->doc;
Daniel Veillard1e774382002-03-06 17:35:40 +00001746 }
1747 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001748 if (encoding == NULL)
1749 encoding = (const char *) htmlGetMetaEncoding(doc);
Daniel Veillard1e774382002-03-06 17:35:40 +00001750 }
1751 if (encoding != NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001752 handler = xmlFindCharEncodingHandler(encoding);
1753 if (handler == NULL) {
1754 return (PyInt_FromLong((long) -1));
1755 }
Daniel Veillard1e774382002-03-06 17:35:40 +00001756 }
1757 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001758 if (handler == NULL)
1759 handler = xmlFindCharEncodingHandler("HTML");
1760 if (handler == NULL)
1761 handler = xmlFindCharEncodingHandler("ascii");
Daniel Veillard1e774382002-03-06 17:35:40 +00001762 }
1763
1764 buf = xmlOutputBufferCreateFile(output, handler);
1765 if (node->type == XML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001766 len = xmlSaveFormatFileTo(buf, doc, encoding, format);
Daniel Veillard1e774382002-03-06 17:35:40 +00001767 } else if (node->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001768 htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
1769 len = xmlOutputBufferClose(buf);
Daniel Veillard1e774382002-03-06 17:35:40 +00001770 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001771 htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
1772 len = xmlOutputBufferClose(buf);
Daniel Veillard1e774382002-03-06 17:35:40 +00001773 } else {
Daniel Veillardd2379012002-03-15 22:24:56 +00001774 xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
1775 len = xmlOutputBufferClose(buf);
Daniel Veillard1e774382002-03-06 17:35:40 +00001776 }
Daniel Veillardd2379012002-03-15 22:24:56 +00001777 return (PyInt_FromLong((long) len));
Daniel Veillard1e774382002-03-06 17:35:40 +00001778}
1779
1780/************************************************************************
1781 * *
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00001782 * Extra stuff *
1783 * *
1784 ************************************************************************/
1785PyObject *
Daniel Veillardd2379012002-03-15 22:24:56 +00001786libxml_xmlNewNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
1787{
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00001788 PyObject *py_retval;
Daniel Veillardd2379012002-03-15 22:24:56 +00001789 xmlChar *name;
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00001790 xmlNodePtr node;
1791
Daniel Veillardd2379012002-03-15 22:24:56 +00001792 if (!PyArg_ParseTuple(args, (char *) "s:xmlNewNode", &name))
1793 return (NULL);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00001794 node = (xmlNodePtr) xmlNewNode(NULL, name);
Daniel Veillardd2379012002-03-15 22:24:56 +00001795 printf("NewNode: %s : %p\n", name, (void *) node);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00001796
1797 if (node == NULL) {
Daniel Veillardd2379012002-03-15 22:24:56 +00001798 Py_INCREF(Py_None);
1799 return (Py_None);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00001800 }
1801 py_retval = libxml_xmlNodePtrWrap(node);
Daniel Veillardd2379012002-03-15 22:24:56 +00001802 return (py_retval);
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00001803}
1804
1805/************************************************************************
1806 * *
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001807 * The registration stuff *
1808 * *
1809 ************************************************************************/
1810static PyMethodDef libxmlMethods[] = {
Daniel Veillard96fe0952002-01-30 20:52:23 +00001811#include "libxml2-export.c"
Daniel Veillardd2379012002-03-15 22:24:56 +00001812 {(char *) "name", libxml_name, METH_VARARGS, NULL},
1813 {(char *) "children", libxml_children, METH_VARARGS, NULL},
1814 {(char *) "properties", libxml_properties, METH_VARARGS, NULL},
1815 {(char *) "last", libxml_last, METH_VARARGS, NULL},
1816 {(char *) "prev", libxml_prev, METH_VARARGS, NULL},
1817 {(char *) "next", libxml_next, METH_VARARGS, NULL},
1818 {(char *) "parent", libxml_parent, METH_VARARGS, NULL},
1819 {(char *) "type", libxml_type, METH_VARARGS, NULL},
1820 {(char *) "doc", libxml_doc, METH_VARARGS, NULL},
1821 {(char *) "xmlNewNode", libxml_xmlNewNode, METH_VARARGS, NULL},
1822 {(char *) "serializeNode", libxml_serializeNode, METH_VARARGS, NULL},
1823 {(char *) "saveNodeTo", libxml_saveNodeTo, METH_VARARGS, NULL},
1824 {NULL, NULL, 0, NULL}
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001825};
1826
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001827#ifdef MERGED_MODULES
Daniel Veillard6361da02002-02-23 10:10:33 +00001828extern void initlibxsltmod(void);
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001829#endif
1830
Daniel Veillardd2379012002-03-15 22:24:56 +00001831void
1832initlibxml2mod(void)
1833{
Daniel Veillardaf43f632002-03-08 15:05:20 +00001834 static int initialized = 0;
Daniel Veillard3ce52572002-02-03 15:08:05 +00001835 PyObject *m;
Daniel Veillardaf43f632002-03-08 15:05:20 +00001836
1837 if (initialized != 0)
Daniel Veillardd2379012002-03-15 22:24:56 +00001838 return;
1839 m = Py_InitModule((char *) "libxml2mod", libxmlMethods);
Daniel Veillardaf43f632002-03-08 15:05:20 +00001840 initialized = 1;
Daniel Veillard5d819032002-02-02 21:49:17 +00001841 libxml_xmlErrorInitialize();
Daniel Veillard6361da02002-02-23 10:10:33 +00001842
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001843#ifdef MERGED_MODULES
1844 initlibxsltmod();
1845#endif
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001846}