blob: f79af7d68741d16177b2e53bdb13599932e294de [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 Veillardf1d0e6b2002-01-31 23:42:44 +000015#include <libxml/xmlmemory.h>
Daniel Veillardd2897fd2002-01-30 16:37:32 +000016#include <libxml/parser.h>
17#include <libxml/tree.h>
Daniel Veillarda7340c82002-02-01 17:56:45 +000018#include <libxml/xpath.h>
Daniel Veillard5d819032002-02-02 21:49:17 +000019#include <libxml/xmlerror.h>
Daniel Veillarda7340c82002-02-01 17:56:45 +000020#include <libxml/xpathInternals.h>
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000021#include <libxml/xmlmemory.h>
Daniel Veillardd2897fd2002-01-30 16:37:32 +000022#include "libxml_wrap.h"
Daniel Veillard96fe0952002-01-30 20:52:23 +000023#include "libxml2-py.h"
Daniel Veillardd2897fd2002-01-30 16:37:32 +000024
25/* #define DEBUG */
Daniel Veillard797a5652002-02-12 13:46:21 +000026/* #define DEBUG_SAX */
Daniel Veillarda7340c82002-02-01 17:56:45 +000027/* #define DEBUG_XPATH */
Daniel Veillard5d819032002-02-02 21:49:17 +000028/* #define DEBUG_ERROR */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000029/* #define DEBUG_MEMORY */
30
31/************************************************************************
32 * *
33 * Memory debug interface *
34 * *
35 ************************************************************************/
36
37extern void xmlMemFree(void *ptr);
38extern void *xmlMemMalloc(size_t size);
39extern void *xmlMemRealloc(void *ptr,size_t size);
40extern char *xmlMemoryStrdup(const char *str);
41
42static int libxmlMemoryDebugActivated = 0;
43static long libxmlMemoryAllocatedBase = 0;
44
45static int libxmlMemoryDebug = 0;
46static xmlFreeFunc freeFunc = NULL;
47static xmlMallocFunc mallocFunc = NULL;
48static xmlReallocFunc reallocFunc = NULL;
49static xmlStrdupFunc strdupFunc = NULL;
50
51PyObject *
52libxml_xmlDebugMemory(PyObject *self, PyObject *args) {
53 int activate;
54 PyObject *py_retval;
55 long ret;
56
57 if (!PyArg_ParseTuple(args, "i:xmlDebugMemory", &activate))
58 return(NULL);
59
60#ifdef DEBUG_MEMORY
61 printf("libxml_xmlDebugMemory(%d) called\n", activate);
62#endif
63
64 if (activate != 0) {
65 if (libxmlMemoryDebug == 0) {
66 /*
67 * First initialize the library and grab the old memory handlers
68 * and switch the library to memory debugging
69 */
70 xmlMemGet((xmlFreeFunc *) &freeFunc,
71 (xmlMallocFunc *)&mallocFunc,
72 (xmlReallocFunc *)&reallocFunc,
73 (xmlStrdupFunc *) &strdupFunc);
74 if ((freeFunc == xmlMemFree) && (mallocFunc == xmlMemMalloc) &&
75 (reallocFunc == xmlMemRealloc) &&
76 (strdupFunc == xmlMemoryStrdup)) {
77 libxmlMemoryAllocatedBase = xmlMemUsed();
78 } else {
79 ret = (long) xmlMemSetup(xmlMemFree, xmlMemMalloc,
80 xmlMemRealloc, xmlMemoryStrdup);
81 if (ret < 0)
82 goto error;
83 libxmlMemoryAllocatedBase = xmlMemUsed();
84 }
85 xmlInitParser();
86 ret = 0;
87 } else if (libxmlMemoryDebugActivated == 0) {
88 libxmlMemoryAllocatedBase = xmlMemUsed();
89 ret = 0;
90 } else {
91 ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
92 }
93 libxmlMemoryDebug = 1;
94 libxmlMemoryDebugActivated = 1;
95 } else {
96 if (libxmlMemoryDebugActivated == 1)
97 ret = xmlMemUsed() - libxmlMemoryAllocatedBase;
98 else
99 ret = 0;
100 libxmlMemoryDebugActivated = 0;
101 }
102error:
103 py_retval = libxml_longWrap(ret);
104 return(py_retval);
105}
106
107PyObject *
108libxml_xmlDumpMemory(PyObject *self, PyObject *args) {
109
110 if (libxmlMemoryDebug != 0)
111 xmlMemoryDump();
112 Py_INCREF(Py_None);
113 return(Py_None);
114}
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000115
116/************************************************************************
117 * *
Daniel Veillard3ce52572002-02-03 15:08:05 +0000118 * Handling SAX/xmllib/sgmlop callback interfaces *
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000119 * *
120 ************************************************************************/
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000121
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000122static void
123pythonStartElement(void *user_data, const xmlChar * name,
124 const xmlChar ** attrs)
125{
126 int i;
127 PyObject *handler;
128 PyObject *dict;
129 PyObject *attrname;
130 PyObject *attrvalue;
131 PyObject *result;
132 int type = 0;
133
Daniel Veillard797a5652002-02-12 13:46:21 +0000134#ifdef DEBUG_SAX
135 printf("pythonStartElement(%s) called\n", name);
136#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000137 handler = (PyObject *) user_data;
138 if (PyObject_HasAttrString(handler, "startElement"))
139 type = 1;
140 else if (PyObject_HasAttrString(handler, "start"))
141 type = 2;
142 if (type != 0) {
143 /*
144 * the xmllib interface always generate a dictionnary,
145 * possibly empty
146 */
147 if ((attrs == NULL) && (type == 1)) {
148 Py_XINCREF(Py_None);
149 dict = Py_None;
Daniel Veillard797a5652002-02-12 13:46:21 +0000150 } else if (attrs == NULL) {
151 dict = PyDict_New();
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000152 } else {
153 dict = PyDict_New();
154 for (i = 0; attrs[i] != NULL; i++) {
155 attrname = PyString_FromString(attrs[i]);
156 i++;
157 if (attrs[i] != NULL) {
158 attrvalue = PyString_FromString(attrs[i]);
159 } else {
160 Py_XINCREF(Py_None);
161 attrvalue = Py_None;
162 }
163 PyDict_SetItem(dict, attrname, attrvalue);
164 }
165 }
166
167 if (type == 1)
168 result = PyObject_CallMethod(handler, "startElement",
169 "sO", name, dict);
170 else if (type == 2)
171 result = PyObject_CallMethod(handler, "start",
172 "sO", name, dict);
173 if (PyErr_Occurred())
174 PyErr_Print();
175 Py_XDECREF(dict);
176 Py_XDECREF(result);
177 }
178}
179
180static void
181pythonStartDocument(void *user_data)
182{
183 PyObject *handler;
184 PyObject *result;
185
Daniel Veillard797a5652002-02-12 13:46:21 +0000186#ifdef DEBUG_SAX
187 printf("pythonStartDocument() called\n");
188#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000189 handler = (PyObject *) user_data;
190 if (PyObject_HasAttrString(handler, "startDocument")) {
191 result = PyObject_CallMethod(handler, "startDocument", NULL);
192 if (PyErr_Occurred())
193 PyErr_Print();
194 Py_XDECREF(result);
195 }
196}
197
198static void
199pythonEndDocument(void *user_data)
200{
201 PyObject *handler;
202 PyObject *result;
203
Daniel Veillard797a5652002-02-12 13:46:21 +0000204#ifdef DEBUG_SAX
205 printf("pythonEndDocument() called\n");
206#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000207 handler = (PyObject *) user_data;
208 if (PyObject_HasAttrString(handler, "endDocument")) {
209 result = PyObject_CallMethod(handler, "endDocument", NULL);
210 if (PyErr_Occurred())
211 PyErr_Print();
212 Py_XDECREF(result);
213 }
214 /*
215 * The reference to the handler is released there
216 */
217 Py_XDECREF(handler);
218}
219
220static void
221pythonEndElement(void *user_data, const xmlChar * name)
222{
223 PyObject *handler;
224 PyObject *result;
225
Daniel Veillard797a5652002-02-12 13:46:21 +0000226#ifdef DEBUG_SAX
227 printf("pythonEndElement(%s) called\n", name);
228#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000229 handler = (PyObject *) user_data;
230 if (PyObject_HasAttrString(handler, "endElement")) {
231 result = PyObject_CallMethod(handler, "endElement", "s", name);
232 if (PyErr_Occurred())
233 PyErr_Print();
234 Py_XDECREF(result);
Daniel Veillard797a5652002-02-12 13:46:21 +0000235 } else if (PyObject_HasAttrString(handler, "end")) {
236 result = PyObject_CallMethod(handler, "end", "s", name);
237 if (PyErr_Occurred())
238 PyErr_Print();
239 Py_XDECREF(result);
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000240 }
241}
242
243static void
244pythonReference(void *user_data, const xmlChar * name)
245{
246 PyObject *handler;
247 PyObject *result;
248
Daniel Veillard797a5652002-02-12 13:46:21 +0000249#ifdef DEBUG_SAX
250 printf("pythonReference(%s) called\n", name);
251#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000252 handler = (PyObject *) user_data;
253 if (PyObject_HasAttrString(handler, "reference")) {
254 result = PyObject_CallMethod(handler, "reference", "s", name);
255 if (PyErr_Occurred())
256 PyErr_Print();
257 Py_XDECREF(result);
258 }
259}
260
261static void
262pythonCharacters(void *user_data, const xmlChar * ch, int len)
263{
264 PyObject *handler;
265 PyObject *result;
266 int type = 0;
267
Daniel Veillard797a5652002-02-12 13:46:21 +0000268#ifdef DEBUG_SAX
269 printf("pythonCharacters(%s, %d) called\n", ch, len);
270#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000271 handler = (PyObject *) user_data;
272 if (PyObject_HasAttrString(handler, "characters"))
273 type = 1;
274 else if (PyObject_HasAttrString(handler, "data"))
275 type = 2;
276 if (type != 0) {
277 if (type == 1)
278 result = PyObject_CallMethod(handler, "characters", "s#", ch, len);
279 else if (type == 2)
280 result = PyObject_CallMethod(handler, "data", "s#", ch, len);
281 if (PyErr_Occurred())
282 PyErr_Print();
283 Py_XDECREF(result);
284 }
285}
286
287static void
288pythonIgnorableWhitespace(void *user_data, const xmlChar * ch, int len)
289{
290 PyObject *handler;
291 PyObject *result;
292 int type = 0;
293
Daniel Veillard797a5652002-02-12 13:46:21 +0000294#ifdef DEBUG_SAX
295 printf("pythonIgnorableWhitespace(%s, %d) called\n", ch, len);
296#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000297 handler = (PyObject *) user_data;
298 if (PyObject_HasAttrString(handler, "ignorableWhitespace"))
299 type = 1;
300 else if (PyObject_HasAttrString(handler, "data"))
301 type = 2;
302 if (type != 0) {
303 if (type == 1)
304 result =
305 PyObject_CallMethod(handler, "ignorableWhitespace", "s#",
306 ch, len);
307 else if (type == 2)
308 result = PyObject_CallMethod(handler, "data", "s#", ch, len);
309 Py_XDECREF(result);
310 }
311}
312
313static void
314pythonProcessingInstruction(void *user_data,
315 const xmlChar * target, const xmlChar * data)
316{
317 PyObject *handler;
318 PyObject *result;
319
Daniel Veillard797a5652002-02-12 13:46:21 +0000320#ifdef DEBUG_SAX
321 printf("pythonProcessingInstruction(%s, %s) called\n", target, data);
322#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000323 handler = (PyObject *) user_data;
324 if (PyObject_HasAttrString(handler, "processingInstruction")) {
325 result =
326 PyObject_CallMethod(handler,
327 "ignorableWhitespace", "ss", target, data);
328 Py_XDECREF(result);
329 }
330}
331
332static void
333pythonComment(void *user_data, const xmlChar * value)
334{
335 PyObject *handler;
336 PyObject *result;
337
Daniel Veillard797a5652002-02-12 13:46:21 +0000338#ifdef DEBUG_SAX
339 printf("pythonComment(%s) called\n", value);
340#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000341 handler = (PyObject *) user_data;
342 if (PyObject_HasAttrString(handler, "comment")) {
343 result = PyObject_CallMethod(handler, "comment", "s", value);
344 if (PyErr_Occurred())
345 PyErr_Print();
346 Py_XDECREF(result);
347 }
348}
349
350static void
351pythonWarning(void *user_data, const char *msg, ...)
352{
353 PyObject *handler;
354 PyObject *result;
355 va_list args;
356 char buf[1024];
357
Daniel Veillard797a5652002-02-12 13:46:21 +0000358#ifdef DEBUG_SAX
359 printf("pythonWarning(%s) called\n", msg);
360#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000361 handler = (PyObject *) user_data;
362 if (PyObject_HasAttrString(handler, "warning")) {
363 va_start(args, msg);
364 vsnprintf(buf, 1023, msg, args);
365 va_end(args);
366 buf[1023] = 0;
367 result = PyObject_CallMethod(handler, "warning", "s", buf);
368 if (PyErr_Occurred())
369 PyErr_Print();
370 Py_XDECREF(result);
371 }
372}
373
374static void
375pythonError(void *user_data, const char *msg, ...)
376{
377 PyObject *handler;
378 PyObject *result;
379 va_list args;
380 char buf[1024];
381
Daniel Veillard797a5652002-02-12 13:46:21 +0000382#ifdef DEBUG_SAX
383 printf("pythonError(%s) called\n", msg);
384#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000385 handler = (PyObject *) user_data;
386 if (PyObject_HasAttrString(handler, "error")) {
387 va_start(args, msg);
388 vsnprintf(buf, 1023, msg, args);
389 va_end(args);
390 buf[1023] = 0;
391 result = PyObject_CallMethod(handler, "error", "s", buf);
392 if (PyErr_Occurred())
393 PyErr_Print();
394 Py_XDECREF(result);
395 }
396}
397
398static void
399pythonFatalError(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("pythonFatalError(%s) called\n", msg);
408#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000409 handler = (PyObject *) user_data;
410 if (PyObject_HasAttrString(handler, "fatalError")) {
411 va_start(args, msg);
412 vsnprintf(buf, 1023, msg, args);
413 va_end(args);
414 buf[1023] = 0;
415 result = PyObject_CallMethod(handler, "fatalError", "s", buf);
416 if (PyErr_Occurred())
417 PyErr_Print();
418 Py_XDECREF(result);
419 }
420}
421
422static void
423pythonCdataBlock(void *user_data, const xmlChar * ch, int len)
424{
425 PyObject *handler;
426 PyObject *result;
427 int type = 0;
428
Daniel Veillard797a5652002-02-12 13:46:21 +0000429#ifdef DEBUG_SAX
430 printf("pythonCdataBlock(%s, %d) called\n", ch, len);
431#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000432 handler = (PyObject *) user_data;
433 if (PyObject_HasAttrString(handler, "cdataBlock"))
434 type = 1;
435 else if (PyObject_HasAttrString(handler, "cdata"))
436 type = 2;
437 if (type != 0) {
438 if (type == 1)
439 result = PyObject_CallMethod(handler, "cdataBlock", "s#", ch, len);
440 else if (type == 2)
441 result = PyObject_CallMethod(handler, "cdata", "s#", ch, len);
442 if (PyErr_Occurred())
443 PyErr_Print();
444 Py_XDECREF(result);
445 }
446}
447
448static void
449pythonExternalSubset(void *user_data,
450 const xmlChar * name,
451 const xmlChar * externalID, const xmlChar * systemID)
452{
453 PyObject *handler;
454 PyObject *result;
455
Daniel Veillard797a5652002-02-12 13:46:21 +0000456#ifdef DEBUG_SAX
457 printf("pythonExternalSubset(%s, %s, %s) called\n",
458 name, externalID, systemID);
459#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000460 handler = (PyObject *) user_data;
461 if (PyObject_HasAttrString(handler, "externalSubset")) {
462 result =
463 PyObject_CallMethod(handler, "externalSubset",
464 "sss", name, externalID, systemID);
465 Py_XDECREF(result);
466 }
467}
468
469static void
470pythonEntityDecl(void *user_data,
471 const xmlChar * name,
472 int type,
473 const xmlChar * publicId,
474 const xmlChar * systemId, xmlChar * content)
475{
476 PyObject *handler;
477 PyObject *result;
478
479 handler = (PyObject *) user_data;
480 if (PyObject_HasAttrString(handler, "entityDecl")) {
481 result = PyObject_CallMethod(handler, "entityDecl",
482 "sisss", name, type, publicId,
483 systemId, content);
484 if (PyErr_Occurred())
485 PyErr_Print();
486 Py_XDECREF(result);
487 }
488}
489
490
491
492static void
493
494pythonNotationDecl(void *user_data,
495 const xmlChar * name,
496 const xmlChar * publicId, const xmlChar * systemId)
497{
498 PyObject *handler;
499 PyObject *result;
500
501 handler = (PyObject *) user_data;
502 if (PyObject_HasAttrString(handler, "notationDecl")) {
503 result = PyObject_CallMethod(handler, "notationDecl",
504 "sss", name, publicId, systemId);
505 if (PyErr_Occurred())
506 PyErr_Print();
507 Py_XDECREF(result);
508 }
509}
510
511static void
512pythonAttributeDecl(void *user_data,
513 const xmlChar * elem,
514 const xmlChar * name,
515 int type,
516 int def,
517 const xmlChar * defaultValue,
518 xmlEnumerationPtr tree)
519{
520 PyObject *handler;
521 PyObject *nameList;
522 PyObject *newName;
523 xmlEnumerationPtr node;
524 PyObject *result;
525 int count;
526
527 handler = (PyObject *) user_data;
528 if (PyObject_HasAttrString(handler, "attributeDecl")) {
529 count = 0;
530 for (node = tree; node != NULL; node = node->next) {
531 count++;
532 }
533 nameList = PyList_New(count);
534 count = 0;
535 for (node = tree; node != NULL; node = node->next) {
536 newName = PyString_FromString(node->name);
537 PyList_SetItem(nameList, count, newName);
538 count++;
539 }
540 result = PyObject_CallMethod(handler, "attributeDecl",
541 "ssiisO", elem, name, type, def,
542 defaultValue, nameList);
543 if (PyErr_Occurred())
544 PyErr_Print();
545 Py_XDECREF(nameList);
546 Py_XDECREF(result);
547 }
548}
549
550static void
551pythonElementDecl(void *user_data,
552 const xmlChar * name,
553 int type, xmlElementContentPtr content)
554{
555 PyObject *handler;
556 PyObject *obj;
557 PyObject *result;
558
559 handler = (PyObject *) user_data;
560 if (PyObject_HasAttrString(handler, "elementDecl")) {
561 /* TODO: wrap in an elementContent object */
562 printf("pythonElementDecl: xmlElementContentPtr wrapper missing !\n");
563 obj = Py_None;
564 /* Py_XINCREF(Py_None); isn't the reference just borrowed ??? */
565 result = PyObject_CallMethod(handler, "elementDecl",
566 "siO", name, type, obj);
567 if (PyErr_Occurred())
568 PyErr_Print();
569 Py_XDECREF(result);
570 }
571}
572
573static void
574pythonUnparsedEntityDecl(void *user_data,
575 const xmlChar * name,
576 const xmlChar * publicId,
577 const xmlChar * systemId,
578 const xmlChar * notationName)
579{
580 PyObject *handler;
581 PyObject *result;
582
583 handler = (PyObject *) user_data;
584 if (PyObject_HasAttrString(handler, "unparsedEntityDecl")) {
585 result = PyObject_CallMethod(handler, "unparsedEntityDecl",
586 "ssss", name, publicId, systemId,
587 notationName);
588 if (PyErr_Occurred())
589 PyErr_Print();
590 Py_XDECREF(result);
591 }
592}
593
594static void
595pythonInternalSubset(void *user_data, const xmlChar * name,
596 const xmlChar * ExternalID, const xmlChar * SystemID)
597{
598 PyObject *handler;
599 PyObject *result;
600
Daniel Veillard797a5652002-02-12 13:46:21 +0000601#ifdef DEBUG_SAX
602 printf("pythonInternalSubset(%s, %s, %s) called\n",
603 name, ExternalID, SystemID);
604#endif
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000605 handler = (PyObject *) user_data;
606 if (PyObject_HasAttrString(handler, "internalSubset")) {
607 result = PyObject_CallMethod(handler, "internalSubset",
608 "sss", name, ExternalID, SystemID);
609 if (PyErr_Occurred())
610 PyErr_Print();
611 Py_XDECREF(result);
612 }
613}
614
615static xmlSAXHandler pythonSaxHandler = {
616 pythonInternalSubset,
617 NULL, /* TODO pythonIsStandalone, */
618 NULL, /* TODO pythonHasInternalSubset, */
619 NULL, /* TODO pythonHasExternalSubset, */
620 NULL, /* TODO pythonResolveEntity, */
621 NULL, /* TODO pythonGetEntity, */
622 pythonEntityDecl,
623 pythonNotationDecl,
624 pythonAttributeDecl,
625 pythonElementDecl,
626 pythonUnparsedEntityDecl,
627 NULL, /* OBSOLETED pythonSetDocumentLocator, */
628 pythonStartDocument,
629 pythonEndDocument,
630 pythonStartElement,
631 pythonEndElement,
632 pythonReference,
633 pythonCharacters,
634 pythonIgnorableWhitespace,
635 pythonProcessingInstruction,
636 pythonComment,
637 pythonWarning,
638 pythonError,
639 pythonFatalError,
640 NULL, /* TODO pythonGetParameterEntity, */
641 pythonCdataBlock,
642 pythonExternalSubset,
643 1
644};
Daniel Veillard3ce52572002-02-03 15:08:05 +0000645
646/************************************************************************
647 * *
648 * Handling of specific parser context *
649 * *
650 ************************************************************************/
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000651
652PyObject *
Daniel Veillard3ce52572002-02-03 15:08:05 +0000653libxml_xmlCreatePushParser(PyObject *self, PyObject *args) {
654 xmlChar *chunk;
655 int size;
656 xmlChar *URI;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000657 PyObject *pyobj_SAX = NULL;
Daniel Veillard3ce52572002-02-03 15:08:05 +0000658 xmlSAXHandlerPtr SAX = NULL;
Daniel Veillard3ce52572002-02-03 15:08:05 +0000659 xmlParserCtxtPtr ret;
660 PyObject *pyret;
Daniel Veillard96fe0952002-01-30 20:52:23 +0000661
Daniel Veillard3ce52572002-02-03 15:08:05 +0000662 if (!PyArg_ParseTuple(args, "Oziz:xmlCreatePushParser", &pyobj_SAX,
663 &chunk, &size, &URI))
664 return(NULL);
665
666#ifdef DEBUG_ERROR
667 printf("libxml_xmlCreatePushParser(%p, %s, %d, %s) called\n",
668 pyobj_SAX, chunk, size, URI);
Daniel Veillard96fe0952002-01-30 20:52:23 +0000669#endif
Daniel Veillard3ce52572002-02-03 15:08:05 +0000670 if (pyobj_SAX != Py_None) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000671 SAX = &pythonSaxHandler;
672 Py_INCREF(pyobj_SAX);
673 /* The reference is released in pythonEndDocument() */
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000674 }
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000675 ret = xmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI);
Daniel Veillard3ce52572002-02-03 15:08:05 +0000676 pyret = libxml_xmlParserCtxtPtrWrap(ret);
677 return(pyret);
Daniel Veillarda7340c82002-02-01 17:56:45 +0000678}
Daniel Veillard5d819032002-02-02 21:49:17 +0000679
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000680PyObject *
681libxml_htmlCreatePushParser(PyObject *self, PyObject *args) {
682 xmlChar *chunk;
683 int size;
684 xmlChar *URI;
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000685 PyObject *pyobj_SAX = NULL;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000686 xmlSAXHandlerPtr SAX = NULL;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000687 xmlParserCtxtPtr ret;
688 PyObject *pyret;
689
690 if (!PyArg_ParseTuple(args, "Oziz:htmlCreatePushParser", &pyobj_SAX,
691 &chunk, &size, &URI))
692 return(NULL);
693
694#ifdef DEBUG_ERROR
695 printf("libxml_htmlCreatePushParser(%p, %s, %d, %s) called\n",
696 pyobj_SAX, chunk, size, URI);
697#endif
698 if (pyobj_SAX != Py_None) {
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000699 SAX = &pythonSaxHandler;
700 Py_INCREF(pyobj_SAX);
701 /* The reference is released in pythonEndDocument() */
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000702 }
Daniel Veillard33caa0b2002-02-04 14:07:26 +0000703 ret = htmlCreatePushParserCtxt(SAX, pyobj_SAX, chunk, size, URI,
Daniel Veillard4e1b26c2002-02-03 20:13:06 +0000704 XML_CHAR_ENCODING_NONE);
705 pyret = libxml_xmlParserCtxtPtrWrap(ret);
706 return(pyret);
707}
708
Daniel Veillard5d819032002-02-02 21:49:17 +0000709/************************************************************************
710 * *
711 * Error message callback *
712 * *
713 ************************************************************************/
714
715static PyObject *libxml_xmlPythonErrorFuncHandler = NULL;
716static PyObject *libxml_xmlPythonErrorFuncCtxt = NULL;
717
718static void
719libxml_xmlErrorFuncHandler(void *ctx, const char *msg, ...) {
720 int size;
721 int chars;
722 char *larger;
723 va_list ap;
724 char *str;
725 PyObject *list;
726 PyObject *message;
727 PyObject *result;
728
729#ifdef DEBUG_ERROR
730 printf("libxml_xmlErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
731#endif
732
733
734 if (libxml_xmlPythonErrorFuncHandler == NULL) {
735 va_start(ap, msg);
736 vfprintf(stdout, msg, ap);
737 va_end(ap);
738 } else {
739 str = (char *) xmlMalloc(150);
740 if (str == NULL)
741 return;
742
743 size = 150;
744
745 while (1) {
746 va_start(ap, msg);
747 chars = vsnprintf(str, size, msg, ap);
748 va_end(ap);
749 if ((chars > -1) && (chars < size))
750 break;
751 if (chars > -1)
752 size += chars + 1;
753 else
754 size += 100;
755 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
756 xmlFree(str);
757 return;
758 }
759 str = larger;
760 }
761
762 list = PyTuple_New(2);
763 PyTuple_SetItem(list, 0, libxml_xmlPythonErrorFuncCtxt);
764 Py_XINCREF(libxml_xmlPythonErrorFuncCtxt);
765 message = libxml_charPtrWrap(str);
766 PyTuple_SetItem(list, 1, message);
767 result = PyEval_CallObject(libxml_xmlPythonErrorFuncHandler, list);
768 Py_XDECREF(list);
769 Py_XDECREF(result);
770 }
771}
772
773static void
774libxml_xmlErrorInitialize(void) {
775#ifdef DEBUG_ERROR
776 printf("libxml_xmlErrorInitialize() called\n");
777#endif
778 xmlSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
779}
780
781PyObject *
782libxml_xmlRegisterErrorHandler(PyObject *self, PyObject *args) {
783 PyObject *py_retval;
784 PyObject *pyobj_f;
785 PyObject *pyobj_ctx;
786
787 if (!PyArg_ParseTuple(args, "OO:xmlRegisterErrorHandler", &pyobj_f,
788 &pyobj_ctx))
789 return(NULL);
790
791#ifdef DEBUG_ERROR
792 printf("libxml_registerXPathFunction(%p, %p) called\n", pyobj_ctx, pyobj_f);
793#endif
794
795 if (libxml_xmlPythonErrorFuncHandler != NULL) {
796 Py_XDECREF(libxml_xmlPythonErrorFuncHandler);
797 }
798 if (libxml_xmlPythonErrorFuncCtxt != NULL) {
799 Py_XDECREF(libxml_xmlPythonErrorFuncCtxt);
800 }
801
802 Py_XINCREF(pyobj_ctx);
803 Py_XINCREF(pyobj_f);
804
805 /* TODO: check f is a function ! */
806 libxml_xmlPythonErrorFuncHandler = pyobj_f;
807 libxml_xmlPythonErrorFuncCtxt = pyobj_ctx;
808
809 py_retval = libxml_intWrap(1);
810 return(py_retval);
811}
Daniel Veillarda7340c82002-02-01 17:56:45 +0000812/************************************************************************
813 * *
814 * XPath extensions *
815 * *
816 ************************************************************************/
817
818static int libxml_xpathCallbacksInitialized = 0;
819
820typedef struct libxml_xpathCallback {
821 xmlXPathContextPtr ctx;
822 xmlChar *name;
823 xmlChar *ns_uri;
824 PyObject *function;
825} libxml_xpathCallback, *libxml_xpathCallbackPtr;
826static libxml_xpathCallback libxml_xpathCallbacks[10];
827static int libxml_xpathCallbacksNb = 0;
828static int libxml_xpathCallbacksMax = 10;
829
Daniel Veillarda7340c82002-02-01 17:56:45 +0000830static void
831libxml_xmlXPathFuncCallback(xmlXPathParserContextPtr ctxt, int nargs) {
832 PyObject *list, *cur, *result;
833 xmlXPathObjectPtr obj;
Daniel Veillard70cab352002-02-06 16:06:58 +0000834 xmlXPathContextPtr rctxt;
835 PyObject *current_function = NULL;
836 const xmlChar *name;
837 const xmlChar *ns_uri;
Daniel Veillarda7340c82002-02-01 17:56:45 +0000838 int i;
839
Daniel Veillard70cab352002-02-06 16:06:58 +0000840 if (ctxt == NULL)
841 return;
842 rctxt = ctxt->context;
843 if (rctxt == NULL)
844 return;
845 name = rctxt->function;
846 ns_uri = rctxt->functionURI;
Daniel Veillarda7340c82002-02-01 17:56:45 +0000847#ifdef DEBUG_XPATH
Daniel Veillard70cab352002-02-06 16:06:58 +0000848 printf("libxml_xmlXPathFuncCallback called name %s URI %s\n", name, ns_uri);
Daniel Veillarda7340c82002-02-01 17:56:45 +0000849#endif
850
Daniel Veillard70cab352002-02-06 16:06:58 +0000851 /*
852 * Find the function, it should be there it was there at lookup
853 */
854 for (i = 0;i < libxml_xpathCallbacksNb;i++) {
855 if (/* TODO (ctxt == libxml_xpathCallbacks[i].ctx) && */
856 (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) &&
857 (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) {
858 current_function = libxml_xpathCallbacks[i].function;
859 }
860 }
861 if (current_function == NULL) {
862 printf("libxml_xmlXPathFuncCallback: internal error %s not found !\n",
863 name);
864 return;
865 }
866
Daniel Veillardc575b992002-02-08 13:28:40 +0000867 list = PyTuple_New(nargs + 1);
868 PyTuple_SetItem(list, 0, libxml_xmlXPathParserContextPtrWrap(ctxt));
Daniel Veillarda7340c82002-02-01 17:56:45 +0000869 for (i = 0;i < nargs;i++) {
870 obj = valuePop(ctxt);
871 cur = libxml_xmlXPathObjectPtrWrap(obj);
Daniel Veillardc575b992002-02-08 13:28:40 +0000872 PyTuple_SetItem(list, i + 1, cur);
Daniel Veillarda7340c82002-02-01 17:56:45 +0000873 }
874 result = PyEval_CallObject(current_function, list);
875 Py_DECREF(list);
876
877 obj = libxml_xmlXPathObjectPtrConvert(result);
878 valuePush(ctxt, obj);
879}
880
881static xmlXPathFunction
882libxml_xmlXPathFuncLookupFunc(void *ctxt, const xmlChar *name,
883 const xmlChar *ns_uri) {
884 int i;
885#ifdef DEBUG_XPATH
886 printf("libxml_xmlXPathFuncLookupFunc(%p, %s, %s) called\n",
887 ctxt, name, ns_uri);
888#endif
Daniel Veillard70cab352002-02-06 16:06:58 +0000889 /*
890 * This is called once only. The address is then stored in the
891 * XPath expression evaluation, the proper object to call can
892 * then still be found using the execution context function
893 * and functionURI fields.
894 */
Daniel Veillarda7340c82002-02-01 17:56:45 +0000895 for (i = 0;i < libxml_xpathCallbacksNb;i++) {
896 if ((ctxt == libxml_xpathCallbacks[i].ctx) &&
897 (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) &&
898 (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) {
Daniel Veillarda7340c82002-02-01 17:56:45 +0000899 return(libxml_xmlXPathFuncCallback);
900 }
901 }
Daniel Veillarda7340c82002-02-01 17:56:45 +0000902 return(NULL);
903}
904
905static void
906libxml_xpathCallbacksInitialize(void) {
907 int i;
908
909 if (libxml_xpathCallbacksInitialized != 0)
910 return;
911
912#ifdef DEBUG_XPATH
913 printf("libxml_xpathCallbacksInitialized called\n");
914#endif
915
916 for (i = 0;i < 10;i++) {
917 libxml_xpathCallbacks[i].ctx = NULL;
918 libxml_xpathCallbacks[i].name = NULL;
919 libxml_xpathCallbacks[i].ns_uri = NULL;
920 libxml_xpathCallbacks[i].function = NULL;
921 }
Daniel Veillarda7340c82002-02-01 17:56:45 +0000922 libxml_xpathCallbacksInitialized = 1;
923}
924
925PyObject *
Daniel Veillard7fd7a942002-02-02 12:19:46 +0000926libxml_xmlRegisterXPathFunction(PyObject *self, PyObject *args) {
Daniel Veillarda7340c82002-02-01 17:56:45 +0000927 PyObject *py_retval;
928 int c_retval = 0;
929 xmlChar *name;
930 xmlChar *ns_uri;
931 xmlXPathContextPtr ctx;
932 PyObject *pyobj_ctx;
933 PyObject *pyobj_f;
934 int i;
935
936 if (!PyArg_ParseTuple(args, "OszO:registerXPathFunction", &pyobj_ctx,
937 &name, &ns_uri, &pyobj_f))
938 return(NULL);
939
940 ctx = (xmlXPathContextPtr) PyxmlXPathContext_Get(pyobj_ctx);
941 if (libxml_xpathCallbacksInitialized == 0)
942 libxml_xpathCallbacksInitialize();
943 xmlXPathRegisterFuncLookup(ctx, libxml_xmlXPathFuncLookupFunc, ctx);
944
945 if ((pyobj_ctx == NULL) || (name == NULL) || (pyobj_f == NULL)) {
946 py_retval = libxml_intWrap(-1);
947 return(py_retval);
948 }
949
950#ifdef DEBUG_XPATH
951 printf("libxml_registerXPathFunction(%p, %s, %s) called\n",
952 ctx, name, ns_uri);
953#endif
954 for (i = 0;i < libxml_xpathCallbacksNb;i++) {
955 if ((ctx == libxml_xpathCallbacks[i].ctx) &&
956 (xmlStrEqual(name, libxml_xpathCallbacks[i].name)) &&
957 (xmlStrEqual(ns_uri, libxml_xpathCallbacks[i].ns_uri))) {
958 Py_XINCREF(pyobj_f);
959 Py_XDECREF(libxml_xpathCallbacks[i].function);
960 libxml_xpathCallbacks[i].function = pyobj_f;
961 c_retval = 1;
962 goto done;
963 }
964 }
965 if (libxml_xpathCallbacksNb >= libxml_xpathCallbacksMax) {
966 printf("libxml_registerXPathFunction() table full\n");
967 } else {
968 i = libxml_xpathCallbacksNb++;
969 Py_XINCREF(pyobj_f);
970 libxml_xpathCallbacks[i].ctx = ctx;
971 libxml_xpathCallbacks[i].name = xmlStrdup(name);
972 libxml_xpathCallbacks[i].ns_uri = xmlStrdup(ns_uri);
973 libxml_xpathCallbacks[i].function = pyobj_f;
Daniel Veillard9589d452002-02-02 10:28:17 +0000974 c_retval = 1;
Daniel Veillarda7340c82002-02-01 17:56:45 +0000975 }
976done:
977 py_retval = libxml_intWrap((int) c_retval);
978 return(py_retval);
979}
980
Daniel Veillard1971ee22002-01-31 20:29:19 +0000981/************************************************************************
982 * *
Daniel Veillardd2897fd2002-01-30 16:37:32 +0000983 * Global properties access *
984 * *
985 ************************************************************************/
986static PyObject *
987libxml_name(PyObject *self, PyObject *args)
988{
989 PyObject *resultobj, *obj;
990 xmlNodePtr cur;
991 const xmlChar *res;
992
993 if (!PyArg_ParseTuple(args, "O:name", &obj))
994 return NULL;
995 cur = PyxmlNode_Get(obj);
996
997#ifdef DEBUG
998 printf("libxml_name: cur = %p type %d\n", cur, cur->type);
999#endif
1000
1001 switch(cur->type) {
1002 case XML_DOCUMENT_NODE:
1003#ifdef LIBXML_DOCB_ENABLED
1004 case XML_DOCB_DOCUMENT_NODE:
1005#endif
1006 case XML_HTML_DOCUMENT_NODE: {
1007 xmlDocPtr doc = (xmlDocPtr) cur;
1008 res = doc->URL;
1009 break;
1010 }
1011 case XML_ATTRIBUTE_NODE: {
1012 xmlAttrPtr attr = (xmlAttrPtr) cur;
1013 res = attr->name;
1014 break;
1015 }
1016 case XML_NAMESPACE_DECL: {
1017 xmlNsPtr ns = (xmlNsPtr) cur;
1018 res = ns->prefix;
1019 break;
1020 }
1021 default:
1022 res = cur->name;
1023 break;
1024 }
Daniel Veillard1971ee22002-01-31 20:29:19 +00001025 resultobj = libxml_constxmlCharPtrWrap(res);
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001026
1027 return resultobj;
1028}
1029
1030static PyObject *
1031libxml_doc(PyObject *self, PyObject *args)
1032{
1033 PyObject *resultobj, *obj;
1034 xmlNodePtr cur;
1035 xmlDocPtr res;
1036
1037 if (!PyArg_ParseTuple(args, "O:doc", &obj))
1038 return NULL;
1039 cur = PyxmlNode_Get(obj);
1040
1041#ifdef DEBUG
1042 printf("libxml_doc: cur = %p\n", cur);
1043#endif
1044
1045 switch(cur->type) {
1046 case XML_DOCUMENT_NODE:
1047#ifdef LIBXML_DOCB_ENABLED
1048 case XML_DOCB_DOCUMENT_NODE:
1049#endif
1050 case XML_HTML_DOCUMENT_NODE:
1051 res = NULL;
1052 break;
1053 case XML_ATTRIBUTE_NODE: {
1054 xmlAttrPtr attr = (xmlAttrPtr) cur;
1055 res = attr->doc;
1056 break;
1057 }
1058 case XML_NAMESPACE_DECL:
1059 res = NULL;
1060 break;
1061 default:
1062 res = cur->doc;
1063 break;
1064 }
1065 resultobj = libxml_xmlDocPtrWrap(res);
1066 return resultobj;
1067}
1068
1069static PyObject *
1070libxml_properties(PyObject *self, PyObject *args)
1071{
1072 PyObject *resultobj, *obj;
1073 xmlNodePtr cur = NULL;
1074 xmlAttrPtr res;
1075
1076 if (!PyArg_ParseTuple(args, "O:properties", &obj))
1077 return NULL;
1078 cur = PyxmlNode_Get(obj);
1079 if (cur->type == XML_ELEMENT_NODE)
1080 res = cur->properties;
1081 else
1082 res = NULL;
1083 resultobj = libxml_xmlAttrPtrWrap(res);
1084 return resultobj;
1085}
1086
1087static PyObject *
1088libxml_next(PyObject *self, PyObject *args)
1089{
1090 PyObject *resultobj, *obj;
1091 xmlNodePtr cur;
1092 xmlNodePtr res;
1093
1094 if (!PyArg_ParseTuple(args, "O:next", &obj))
1095 return NULL;
1096 cur = PyxmlNode_Get(obj);
1097
1098#ifdef DEBUG
1099 printf("libxml_next: cur = %p\n", cur);
1100#endif
1101
1102 switch(cur->type) {
1103 case XML_DOCUMENT_NODE:
1104#ifdef LIBXML_DOCB_ENABLED
1105 case XML_DOCB_DOCUMENT_NODE:
1106#endif
1107 case XML_HTML_DOCUMENT_NODE:
1108 res = NULL;
1109 break;
1110 case XML_ATTRIBUTE_NODE: {
1111 xmlAttrPtr attr = (xmlAttrPtr) cur;
1112 res = (xmlNodePtr) attr->next;
1113 break;
1114 }
1115 case XML_NAMESPACE_DECL: {
1116 xmlNsPtr ns = (xmlNsPtr) cur;
1117 res = (xmlNodePtr) ns->next;
1118 break;
1119 }
1120 default:
1121 res = cur->next;
1122 break;
1123
1124 }
1125 resultobj = libxml_xmlNodePtrWrap(res);
1126 return resultobj;
1127}
1128
1129static PyObject *
1130libxml_prev(PyObject *self, PyObject *args)
1131{
1132 PyObject *resultobj, *obj;
1133 xmlNodePtr cur;
1134 xmlNodePtr res;
1135
1136 if (!PyArg_ParseTuple(args, "O:prev", &obj))
1137 return NULL;
1138 cur = PyxmlNode_Get(obj);
1139
1140#ifdef DEBUG
1141 printf("libxml_prev: cur = %p\n", cur);
1142#endif
1143
1144 switch(cur->type) {
1145 case XML_DOCUMENT_NODE:
1146#ifdef LIBXML_DOCB_ENABLED
1147 case XML_DOCB_DOCUMENT_NODE:
1148#endif
1149 case XML_HTML_DOCUMENT_NODE:
1150 res = NULL;
1151 break;
1152 case XML_ATTRIBUTE_NODE: {
1153 xmlAttrPtr attr = (xmlAttrPtr) cur;
1154 res = (xmlNodePtr) attr->next;
1155 }
1156 case XML_NAMESPACE_DECL:
1157 res = NULL;
1158 break;
1159 default:
1160 res = cur->next;
1161 break;
1162 }
1163 resultobj = libxml_xmlNodePtrWrap(res);
1164 return resultobj;
1165}
1166
1167static PyObject *
1168libxml_children(PyObject *self, PyObject *args)
1169{
1170 PyObject *resultobj, *obj;
1171 xmlNodePtr cur;
1172 xmlNodePtr res;
1173
1174 if (!PyArg_ParseTuple(args, "O:children", &obj))
1175 return NULL;
1176 cur = PyxmlNode_Get(obj);
1177
1178#ifdef DEBUG
1179 printf("libxml_children: cur = %p\n", cur);
1180#endif
1181
1182 switch(cur->type) {
1183 case XML_ELEMENT_NODE:
1184 case XML_ENTITY_REF_NODE:
1185 case XML_ENTITY_NODE:
1186 case XML_PI_NODE:
1187 case XML_COMMENT_NODE:
1188 case XML_DOCUMENT_NODE:
1189#ifdef LIBXML_DOCB_ENABLED
1190 case XML_DOCB_DOCUMENT_NODE:
1191#endif
1192 case XML_HTML_DOCUMENT_NODE:
1193 case XML_DTD_NODE:
1194 res = cur->children;
1195 break;
1196 case XML_ATTRIBUTE_NODE: {
1197 xmlAttrPtr attr = (xmlAttrPtr) cur;
1198 res = attr->children;
1199 break;
1200 }
1201 default:
1202 res = NULL;
1203 break;
1204 }
1205 resultobj = libxml_xmlNodePtrWrap(res);
1206 return resultobj;
1207}
1208
1209static PyObject *
1210libxml_last(PyObject *self, PyObject *args)
1211{
1212 PyObject *resultobj, *obj;
1213 xmlNodePtr cur;
1214 xmlNodePtr res;
1215
1216 if (!PyArg_ParseTuple(args, "O:last", &obj))
1217 return NULL;
1218 cur = PyxmlNode_Get(obj);
1219
1220#ifdef DEBUG
1221 printf("libxml_last: cur = %p\n", cur);
1222#endif
1223
1224 switch(cur->type) {
1225 case XML_ELEMENT_NODE:
1226 case XML_ENTITY_REF_NODE:
1227 case XML_ENTITY_NODE:
1228 case XML_PI_NODE:
1229 case XML_COMMENT_NODE:
1230 case XML_DOCUMENT_NODE:
1231#ifdef LIBXML_DOCB_ENABLED
1232 case XML_DOCB_DOCUMENT_NODE:
1233#endif
1234 case XML_HTML_DOCUMENT_NODE:
1235 case XML_DTD_NODE:
1236 res = cur->last;
1237 break;
1238 case XML_ATTRIBUTE_NODE: {
1239 xmlAttrPtr attr = (xmlAttrPtr) cur;
1240 res = attr->last;
1241 }
1242 default:
1243 res = NULL;
1244 break;
1245 }
1246 resultobj = libxml_xmlNodePtrWrap(res);
1247 return resultobj;
1248}
1249
1250static PyObject *
1251libxml_parent(PyObject *self, PyObject *args)
1252{
1253 PyObject *resultobj, *obj;
1254 xmlNodePtr cur;
1255 xmlNodePtr res;
1256
1257 if (!PyArg_ParseTuple(args, "O:parent", &obj))
1258 return NULL;
1259 cur = PyxmlNode_Get(obj);
1260
1261#ifdef DEBUG
1262 printf("libxml_parent: cur = %p\n", cur);
1263#endif
1264
1265 switch(cur->type) {
1266 case XML_DOCUMENT_NODE:
1267 case XML_HTML_DOCUMENT_NODE:
1268#ifdef LIBXML_DOCB_ENABLED
1269 case XML_DOCB_DOCUMENT_NODE:
1270#endif
1271 res = NULL;
1272 break;
1273 case XML_ATTRIBUTE_NODE: {
1274 xmlAttrPtr attr = (xmlAttrPtr) cur;
1275 res = attr->parent;
1276 }
1277 case XML_ENTITY_DECL:
1278 case XML_NAMESPACE_DECL:
1279 case XML_XINCLUDE_START:
1280 case XML_XINCLUDE_END:
1281 res = NULL;
1282 break;
1283 default:
1284 res = cur->parent;
1285 break;
1286 }
1287 resultobj = libxml_xmlNodePtrWrap(res);
1288 return resultobj;
1289}
1290
1291static PyObject *
1292libxml_type(PyObject *self, PyObject *args)
1293{
1294 PyObject *resultobj, *obj;
1295 xmlNodePtr cur;
1296 const xmlChar *res;
1297
1298 if (!PyArg_ParseTuple(args, "O:last", &obj))
1299 return NULL;
1300 cur = PyxmlNode_Get(obj);
1301
1302#ifdef DEBUG
1303 printf("libxml_type: cur = %p\n", cur);
1304#endif
1305
1306 switch(cur->type) {
1307 case XML_ELEMENT_NODE:
1308 res = (const xmlChar *) "element"; break;
1309 case XML_ATTRIBUTE_NODE:
1310 res = (const xmlChar *) "attribute"; break;
1311 case XML_TEXT_NODE:
1312 res = (const xmlChar *) "text"; break;
1313 case XML_CDATA_SECTION_NODE:
1314 res = (const xmlChar *) "cdata"; break;
1315 case XML_ENTITY_REF_NODE:
1316 res = (const xmlChar *) "entity_ref"; break;
1317 case XML_ENTITY_NODE:
1318 res = (const xmlChar *) "entity"; break;
1319 case XML_PI_NODE:
1320 res = (const xmlChar *) "pi"; break;
1321 case XML_COMMENT_NODE:
1322 res = (const xmlChar *) "comment"; break;
1323 case XML_DOCUMENT_NODE:
1324 res = (const xmlChar *) "document_xml"; break;
1325 case XML_DOCUMENT_TYPE_NODE:
1326 res = (const xmlChar *) "doctype"; break;
1327 case XML_DOCUMENT_FRAG_NODE:
1328 res = (const xmlChar *) "fragment"; break;
1329 case XML_NOTATION_NODE:
1330 res = (const xmlChar *) "notation"; break;
1331 case XML_HTML_DOCUMENT_NODE:
1332 res = (const xmlChar *) "document_html"; break;
1333 case XML_DTD_NODE:
1334 res = (const xmlChar *) "dtd"; break;
1335 case XML_ELEMENT_DECL:
1336 res = (const xmlChar *) "elem_decl"; break;
1337 case XML_ATTRIBUTE_DECL:
1338 res = (const xmlChar *) "attribute_decl"; break;
1339 case XML_ENTITY_DECL:
1340 res = (const xmlChar *) "entity_decl"; break;
1341 case XML_NAMESPACE_DECL:
1342 res = (const xmlChar *) "namespace"; break;
1343 case XML_XINCLUDE_START:
1344 res = (const xmlChar *) "xinclude_start"; break;
1345 case XML_XINCLUDE_END:
1346 res = (const xmlChar *) "xinclude_end"; break;
1347#ifdef LIBXML_DOCB_ENABLED
1348 case XML_DOCB_DOCUMENT_NODE:
1349 res = (const xmlChar *) "document_docbook"; break;
1350#endif
1351 }
1352#ifdef DEBUG
1353 printf("libxml_type: cur = %p: %s\n", cur, res);
1354#endif
1355
Daniel Veillard1971ee22002-01-31 20:29:19 +00001356 resultobj = libxml_constxmlCharPtrWrap(res);
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001357 return resultobj;
1358}
1359
1360/************************************************************************
1361 * *
Daniel Veillard36eea2d2002-02-04 00:17:01 +00001362 * Specific accessor functions *
1363 * *
1364 ************************************************************************/
1365PyObject *
1366libxml_xmlNodeGetNsDefs(PyObject *self, PyObject *args) {
1367 PyObject *py_retval;
1368 xmlNsPtr c_retval;
1369 xmlNodePtr node;
1370 PyObject *pyobj_node;
1371
1372 if (!PyArg_ParseTuple(args, "O:xmlNodeGetNsDefs", &pyobj_node))
1373 return(NULL);
1374 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
1375
1376 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
1377 Py_INCREF(Py_None);
1378 return(Py_None);
1379 }
1380 c_retval = node->nsDef;
1381 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
1382 return(py_retval);
1383}
1384
1385PyObject *
1386libxml_xmlNodeGetNs(PyObject *self, PyObject *args) {
1387 PyObject *py_retval;
1388 xmlNsPtr c_retval;
1389 xmlNodePtr node;
1390 PyObject *pyobj_node;
1391
1392 if (!PyArg_ParseTuple(args, "O:xmlNodeGetNs", &pyobj_node))
1393 return(NULL);
1394 node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
1395
1396 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
1397 Py_INCREF(Py_None);
1398 return(Py_None);
1399 }
1400 c_retval = node->ns;
1401 py_retval = libxml_xmlNsPtrWrap((xmlNsPtr) c_retval);
1402 return(py_retval);
1403}
1404
1405/************************************************************************
1406 * *
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00001407 * Extra stuff *
1408 * *
1409 ************************************************************************/
1410PyObject *
1411libxml_xmlNewNode(PyObject *self, PyObject *args) {
1412 PyObject *py_retval;
1413 xmlChar * name;
1414 xmlNodePtr node;
1415
1416 if (!PyArg_ParseTuple(args, "s:xmlNewNode", &name))
1417 return(NULL);
1418 node = (xmlNodePtr) xmlNewNode(NULL, name);
1419 printf("NewNode: %s : %p\n", name, node);
1420
1421 if (node == NULL) {
1422 Py_INCREF(Py_None);
1423 return(Py_None);
1424 }
1425 py_retval = libxml_xmlNodePtrWrap(node);
1426 return(py_retval);
1427}
1428
1429/************************************************************************
1430 * *
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001431 * The registration stuff *
1432 * *
1433 ************************************************************************/
1434static PyMethodDef libxmlMethods[] = {
Daniel Veillard96fe0952002-01-30 20:52:23 +00001435#include "libxml2-export.c"
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00001436 { "name", libxml_name, METH_VARARGS, NULL },
1437 { "children", libxml_children, METH_VARARGS, NULL },
1438 { "properties", libxml_properties, METH_VARARGS, NULL },
1439 { "last", libxml_last, METH_VARARGS, NULL },
1440 { "prev", libxml_prev, METH_VARARGS, NULL },
1441 { "next", libxml_next, METH_VARARGS, NULL },
1442 { "parent", libxml_parent, METH_VARARGS, NULL },
1443 { "type", libxml_type, METH_VARARGS, NULL },
1444 { "doc", libxml_doc, METH_VARARGS, NULL },
Daniel Veillarda94ec6f2002-03-01 13:00:53 +00001445 { "xmlNewNode", libxml_xmlNewNode, METH_VARARGS, NULL },
Daniel Veillard0ba59232002-02-10 13:20:39 +00001446 { NULL }
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001447};
1448
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001449#ifdef MERGED_MODULES
Daniel Veillard6361da02002-02-23 10:10:33 +00001450extern void initlibxsltmod(void);
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001451#endif
1452
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00001453void initlibxml2mod(void) {
Daniel Veillard3ce52572002-02-03 15:08:05 +00001454 PyObject *m;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00001455 m = Py_InitModule("libxml2mod", libxmlMethods);
Daniel Veillard5d819032002-02-02 21:49:17 +00001456 libxml_xmlErrorInitialize();
Daniel Veillard6361da02002-02-23 10:10:33 +00001457
Daniel Veillard0fea6f42002-02-22 22:51:13 +00001458#ifdef MERGED_MODULES
1459 initlibxsltmod();
1460#endif
Daniel Veillardd2897fd2002-01-30 16:37:32 +00001461}
1462