Initial revision
[TestXSLT.git] / libxml2 / python / types.c
1 /*
2  * types.c: converter functions between the internal representation
3  *          and the Python objects
4  *
5  * See Copyright for the status of this software.
6  *
7  * daniel@veillard.com
8  */
9 #include "libxml_wrap.h"
10
11 PyObject *
12 libxml_intWrap(int val)
13 {
14     PyObject *ret;
15
16 #ifdef DEBUG
17     printf("libxml_intWrap: val = %d\n", val);
18 #endif
19     ret = PyInt_FromLong((long) val);
20     return (ret);
21 }
22
23 PyObject *
24 libxml_longWrap(long val)
25 {
26     PyObject *ret;
27
28 #ifdef DEBUG
29     printf("libxml_longWrap: val = %ld\n", val);
30 #endif
31     ret = PyInt_FromLong(val);
32     return (ret);
33 }
34
35 PyObject *
36 libxml_doubleWrap(double val)
37 {
38     PyObject *ret;
39
40 #ifdef DEBUG
41     printf("libxml_doubleWrap: val = %f\n", val);
42 #endif
43     ret = PyFloat_FromDouble((double) val);
44     return (ret);
45 }
46
47 PyObject *
48 libxml_charPtrWrap(char *str)
49 {
50     PyObject *ret;
51
52 #ifdef DEBUG
53     printf("libxml_xmlcharPtrWrap: str = %s\n", str);
54 #endif
55     if (str == NULL) {
56         Py_INCREF(Py_None);
57         return (Py_None);
58     }
59     /* TODO: look at deallocation */
60     ret = PyString_FromString(str);
61     xmlFree(str);
62     return (ret);
63 }
64
65 PyObject *
66 libxml_charPtrConstWrap(const char *str)
67 {
68     PyObject *ret;
69
70 #ifdef DEBUG
71     printf("libxml_xmlcharPtrWrap: str = %s\n", str);
72 #endif
73     if (str == NULL) {
74         Py_INCREF(Py_None);
75         return (Py_None);
76     }
77     /* TODO: look at deallocation */
78     ret = PyString_FromString(str);
79     return (ret);
80 }
81
82 PyObject *
83 libxml_xmlCharPtrWrap(xmlChar * str)
84 {
85     PyObject *ret;
86
87 #ifdef DEBUG
88     printf("libxml_xmlCharPtrWrap: str = %s\n", str);
89 #endif
90     if (str == NULL) {
91         Py_INCREF(Py_None);
92         return (Py_None);
93     }
94     /* TODO: look at deallocation */
95     ret = PyString_FromString((char *) str);
96     xmlFree(str);
97     return (ret);
98 }
99
100 PyObject *
101 libxml_xmlCharPtrConstWrap(const xmlChar * str)
102 {
103     PyObject *ret;
104
105 #ifdef DEBUG
106     printf("libxml_xmlCharPtrWrap: str = %s\n", str);
107 #endif
108     if (str == NULL) {
109         Py_INCREF(Py_None);
110         return (Py_None);
111     }
112     /* TODO: look at deallocation */
113     ret = PyString_FromString((char *) str);
114     return (ret);
115 }
116
117 PyObject *
118 libxml_constcharPtrWrap(const char *str)
119 {
120     PyObject *ret;
121
122 #ifdef DEBUG
123     printf("libxml_xmlcharPtrWrap: str = %s\n", str);
124 #endif
125     if (str == NULL) {
126         Py_INCREF(Py_None);
127         return (Py_None);
128     }
129     /* TODO: look at deallocation */
130     ret = PyString_FromString(str);
131     return (ret);
132 }
133
134 PyObject *
135 libxml_constxmlCharPtrWrap(const xmlChar * str)
136 {
137     PyObject *ret;
138
139 #ifdef DEBUG
140     printf("libxml_xmlCharPtrWrap: str = %s\n", str);
141 #endif
142     if (str == NULL) {
143         Py_INCREF(Py_None);
144         return (Py_None);
145     }
146     /* TODO: look at deallocation */
147     ret = PyString_FromString((char *) str);
148     return (ret);
149 }
150
151 PyObject *
152 libxml_xmlDocPtrWrap(xmlDocPtr doc)
153 {
154     PyObject *ret;
155
156 #ifdef DEBUG
157     printf("libxml_xmlDocPtrWrap: doc = %p\n", doc);
158 #endif
159     if (doc == NULL) {
160         Py_INCREF(Py_None);
161         return (Py_None);
162     }
163     /* TODO: look at deallocation */
164     ret =
165         PyCObject_FromVoidPtrAndDesc((void *) doc, (char *) "xmlDocPtr",
166                                      NULL);
167     return (ret);
168 }
169
170 PyObject *
171 libxml_xmlNodePtrWrap(xmlNodePtr node)
172 {
173     PyObject *ret;
174
175 #ifdef DEBUG
176     printf("libxml_xmlNodePtrWrap: node = %p\n", node);
177 #endif
178     if (node == NULL) {
179         Py_INCREF(Py_None);
180         return (Py_None);
181     }
182     ret =
183         PyCObject_FromVoidPtrAndDesc((void *) node, (char *) "xmlNodePtr",
184                                      NULL);
185     return (ret);
186 }
187
188 PyObject *
189 libxml_xmlURIPtrWrap(xmlURIPtr uri)
190 {
191     PyObject *ret;
192
193 #ifdef DEBUG
194     printf("libxml_xmlURIPtrWrap: uri = %p\n", uri);
195 #endif
196     if (uri == NULL) {
197         Py_INCREF(Py_None);
198         return (Py_None);
199     }
200     ret =
201         PyCObject_FromVoidPtrAndDesc((void *) uri, (char *) "xmlURIPtr",
202                                      NULL);
203     return (ret);
204 }
205
206 PyObject *
207 libxml_xmlNsPtrWrap(xmlNsPtr ns)
208 {
209     PyObject *ret;
210
211 #ifdef DEBUG
212     printf("libxml_xmlNsPtrWrap: node = %p\n", ns);
213 #endif
214     if (ns == NULL) {
215         Py_INCREF(Py_None);
216         return (Py_None);
217     }
218     ret =
219         PyCObject_FromVoidPtrAndDesc((void *) ns, (char *) "xmlNsPtr",
220                                      NULL);
221     return (ret);
222 }
223
224 PyObject *
225 libxml_xmlAttrPtrWrap(xmlAttrPtr attr)
226 {
227     PyObject *ret;
228
229 #ifdef DEBUG
230     printf("libxml_xmlAttrNodePtrWrap: attr = %p\n", attr);
231 #endif
232     if (attr == NULL) {
233         Py_INCREF(Py_None);
234         return (Py_None);
235     }
236     ret =
237         PyCObject_FromVoidPtrAndDesc((void *) attr, (char *) "xmlAttrPtr",
238                                      NULL);
239     return (ret);
240 }
241
242 PyObject *
243 libxml_xmlAttributePtrWrap(xmlAttributePtr attr)
244 {
245     PyObject *ret;
246
247 #ifdef DEBUG
248     printf("libxml_xmlAttributePtrWrap: attr = %p\n", attr);
249 #endif
250     if (attr == NULL) {
251         Py_INCREF(Py_None);
252         return (Py_None);
253     }
254     ret =
255         PyCObject_FromVoidPtrAndDesc((void *) attr,
256                                      (char *) "xmlAttributePtr", NULL);
257     return (ret);
258 }
259
260 PyObject *
261 libxml_xmlElementPtrWrap(xmlElementPtr elem)
262 {
263     PyObject *ret;
264
265 #ifdef DEBUG
266     printf("libxml_xmlElementNodePtrWrap: elem = %p\n", elem);
267 #endif
268     if (elem == NULL) {
269         Py_INCREF(Py_None);
270         return (Py_None);
271     }
272     ret =
273         PyCObject_FromVoidPtrAndDesc((void *) elem,
274                                      (char *) "xmlElementPtr", NULL);
275     return (ret);
276 }
277
278 PyObject *
279 libxml_xmlXPathContextPtrWrap(xmlXPathContextPtr ctxt)
280 {
281     PyObject *ret;
282
283 #ifdef DEBUG
284     printf("libxml_xmlXPathContextPtrWrap: ctxt = %p\n", ctxt);
285 #endif
286     if (ctxt == NULL) {
287         Py_INCREF(Py_None);
288         return (Py_None);
289     }
290     ret =
291         PyCObject_FromVoidPtrAndDesc((void *) ctxt,
292                                      (char *) "xmlXPathContextPtr", NULL);
293     return (ret);
294 }
295
296 PyObject *
297 libxml_xmlXPathParserContextPtrWrap(xmlXPathParserContextPtr ctxt)
298 {
299     PyObject *ret;
300
301 #ifdef DEBUG
302     printf("libxml_xmlXPathParserContextPtrWrap: ctxt = %p\n", ctxt);
303 #endif
304     if (ctxt == NULL) {
305         Py_INCREF(Py_None);
306         return (Py_None);
307     }
308     ret = PyCObject_FromVoidPtrAndDesc((void *) ctxt,
309                                        (char *) "xmlXPathParserContextPtr",
310                                        NULL);
311     return (ret);
312 }
313
314 PyObject *
315 libxml_xmlParserCtxtPtrWrap(xmlParserCtxtPtr ctxt)
316 {
317     PyObject *ret;
318
319 #ifdef DEBUG
320     printf("libxml_xmlParserCtxtPtrWrap: ctxt = %p\n", ctxt);
321 #endif
322     if (ctxt == NULL) {
323         Py_INCREF(Py_None);
324         return (Py_None);
325     }
326
327     ret =
328         PyCObject_FromVoidPtrAndDesc((void *) ctxt,
329                                      (char *) "xmlParserCtxtPtr", NULL);
330     return (ret);
331 }
332
333 PyObject *
334 libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj)
335 {
336     PyObject *ret;
337
338 #ifdef DEBUG
339     printf("libxml_xmlXPathObjectPtrWrap: ctxt = %p\n", obj);
340 #endif
341     if (obj == NULL) {
342         Py_INCREF(Py_None);
343         return (Py_None);
344     }
345     switch (obj->type) {
346         case XPATH_XSLT_TREE: {
347             if ((obj->nodesetval == NULL) ||
348                 (obj->nodesetval->nodeNr == 0) ||
349                 (obj->nodesetval->nodeTab == NULL)) {
350                 ret = PyList_New(0);
351             } else {
352                 int i, len = 0;
353                 xmlNodePtr node;
354
355                 node = obj->nodesetval->nodeTab[0]->children;
356                 while (node != NULL) {
357                     len++;
358                     node = node->next;
359                 }
360                 ret = PyList_New(len);
361                 node = obj->nodesetval->nodeTab[0]->children;
362                 for (i = 0;i < len;i++) {
363                     PyList_SetItem(ret, i, libxml_xmlNodePtrWrap(node));
364                     node = node->next;
365                 }
366             }
367             /*
368              * Return now, do not free the object passed down
369              */
370             return (ret);
371         }
372         case XPATH_NODESET:
373             if ((obj->nodesetval == NULL)
374                 || (obj->nodesetval->nodeNr == 0)) {
375                 ret = PyList_New(0);
376             } else {
377                 int i;
378                 xmlNodePtr node;
379
380                 ret = PyList_New(obj->nodesetval->nodeNr);
381                 for (i = 0; i < obj->nodesetval->nodeNr; i++) {
382                     node = obj->nodesetval->nodeTab[i];
383                     /* TODO: try to cast directly to the proper node type */
384                     PyList_SetItem(ret, i, libxml_xmlNodePtrWrap(node));
385                 }
386             }
387             break;
388         case XPATH_BOOLEAN:
389             ret = PyInt_FromLong((long) obj->boolval);
390             break;
391         case XPATH_NUMBER:
392             ret = PyFloat_FromDouble(obj->floatval);
393             break;
394         case XPATH_STRING:
395             ret = PyString_FromString((char *) obj->stringval);
396             break;
397         case XPATH_POINT:
398         case XPATH_RANGE:
399         case XPATH_LOCATIONSET:
400         default:
401             printf("Unable to convert XPath object type %d\n", obj->type);
402             Py_INCREF(Py_None);
403             ret = Py_None;
404     }
405     xmlXPathFreeObject(obj);
406     return (ret);
407 }
408
409 xmlXPathObjectPtr
410 libxml_xmlXPathObjectPtrConvert(PyObject * obj)
411 {
412     xmlXPathObjectPtr ret = NULL;
413
414 #ifdef DEBUG
415     printf("libxml_xmlXPathObjectPtrConvert: obj = %p\n", obj);
416 #endif
417     if (obj == NULL) {
418         return (NULL);
419     }
420     if PyFloat_Check
421         (obj) {
422         ret = xmlXPathNewFloat((double) PyFloat_AS_DOUBLE(obj));
423     } else if PyString_Check
424         (obj) {
425         xmlChar *str;
426
427         str = xmlStrndup((const xmlChar *) PyString_AS_STRING(obj),
428                          PyString_GET_SIZE(obj));
429         ret = xmlXPathWrapString(str);
430     } else if PyList_Check
431         (obj) {
432         int i;
433         PyObject *node;
434         xmlNodePtr cur;
435         xmlNodeSetPtr set;
436
437         set = xmlXPathNodeSetCreate(NULL);
438
439         for (i = 0; i < PyList_Size(obj); i++) {
440             node = PyList_GetItem(obj, i);
441             if ((node == NULL) || (node->ob_type == NULL))
442                 continue;
443
444             cur = NULL;
445             if (PyCObject_Check(node)) {
446                 printf("Got a CObject\n");
447                 cur = PyxmlNode_Get(node);
448             } else if (PyInstance_Check(node)) {
449                 PyInstanceObject *inst = (PyInstanceObject *) node;
450                 PyObject *name = inst->in_class->cl_name;
451
452                 if PyString_Check
453                     (name) {
454                     char *type = PyString_AS_STRING(name);
455                     PyObject *wrapper;
456
457                     if (!strcmp(type, "xmlNode")) {
458                         wrapper =
459                             PyObject_GetAttrString(node, (char *) "_o");
460                         if (wrapper != NULL) {
461                             cur = PyxmlNode_Get(wrapper);
462                         }
463                     }
464                     }
465             } else {
466                 printf("Unknown object in Python return list\n");
467             }
468             if (cur != NULL) {
469                 xmlXPathNodeSetAdd(set, cur);
470             }
471         }
472         ret = xmlXPathWrapNodeSet(set);
473     } else {
474         printf("Unable to convert Python Object to XPath");
475     }
476     Py_DECREF(obj);
477     return (ret);
478 }
479
480 PyObject *
481 libxml_xmlCatalogPtrWrap(xmlCatalogPtr catal)
482 {
483     PyObject *ret;
484
485 #ifdef DEBUG
486     printf("libxml_xmlNodePtrWrap: catal = %p\n", catal);
487 #endif
488     if (catal == NULL) {
489         Py_INCREF(Py_None);
490         return (Py_None);
491     }
492     ret =
493         PyCObject_FromVoidPtrAndDesc((void *) catal,
494                                      (char *) "xmlCatalogPtr", NULL);
495     return (ret);
496 }
497
498 PyObject *
499 libxml_xmlOutputBufferPtrWrap(xmlOutputBufferPtr buffer)
500 {
501     PyObject *ret;
502
503 #ifdef DEBUG
504     printf("libxml_xmlOutputBufferPtrWrap: buffer = %p\n", buffer);
505 #endif
506     if (buffer == NULL) {
507         Py_INCREF(Py_None);
508         return (Py_None);
509     }
510     ret =
511         PyCObject_FromVoidPtrAndDesc((void *) buffer,
512                                      (char *) "xmlOutputBufferPtr", NULL);
513     return (ret);
514 }
515
516 PyObject *
517 libxml_xmlParserInputBufferPtrWrap(xmlParserInputBufferPtr buffer)
518 {
519     PyObject *ret;
520
521 #ifdef DEBUG
522     printf("libxml_xmlParserInputBufferPtrWrap: buffer = %p\n", buffer);
523 #endif
524     if (buffer == NULL) {
525         Py_INCREF(Py_None);
526         return (Py_None);
527     }
528     ret =
529         PyCObject_FromVoidPtrAndDesc((void *) buffer,
530                                      (char *) "xmlParserInputBufferPtr", NULL);
531     return (ret);
532 }
533
534 #ifdef LIBXML_REGEXP_ENABLED
535 PyObject *
536 libxml_xmlRegexpPtrWrap(xmlRegexpPtr regexp)
537 {
538     PyObject *ret;
539
540 #ifdef DEBUG
541     printf("libxml_xmlRegexpPtrWrap: regexp = %p\n", regexp);
542 #endif
543     if (regexp == NULL) {
544         Py_INCREF(Py_None);
545         return (Py_None);
546     }
547     ret =
548         PyCObject_FromVoidPtrAndDesc((void *) regexp,
549                                      (char *) "xmlRegexpPtr", NULL);
550     return (ret);
551 }
552 #endif /* LIBXML_REGEXP_ENABLED */
553
554 PyObject *
555 libxml_xmlTextReaderPtrWrap(xmlTextReaderPtr reader)
556 {
557     PyObject *ret;
558
559 #ifdef DEBUG
560     printf("libxml_xmlTextReaderPtrWrap: reader = %p\n", reader);
561 #endif
562     if (reader == NULL) {
563         Py_INCREF(Py_None);
564         return (Py_None);
565     }
566     ret =
567         PyCObject_FromVoidPtrAndDesc((void *) reader,
568                                      (char *) "xmlTextReaderPtr", NULL);
569     return (ret);
570 }
571
572 PyObject *
573 libxml_xmlTextReaderLocatorPtrWrap(xmlTextReaderLocatorPtr locator)
574 {
575     PyObject *ret;
576
577 #ifdef DEBUG
578     printf("libxml_xmlTextReaderLocatorPtrWrap: locator = %p\n", locator);
579 #endif
580     if (locator == NULL) {
581         Py_INCREF(Py_None);
582         return (Py_None);
583     }
584     ret =
585         PyCObject_FromVoidPtrAndDesc((void *) locator,
586                                      (char *) "xmlTextReaderLocatorPtr", NULL);
587     return (ret);
588 }
589
590 #ifdef LIBXML_SCHEMAS_ENABLED
591 PyObject *
592 libxml_xmlRelaxNGPtrWrap(xmlRelaxNGPtr ctxt)
593 {
594     PyObject *ret;
595
596 #ifdef DEBUG
597     printf("libxml_xmlRelaxNGPtrWrap: ctxt = %p\n", ctxt);
598 #endif
599     if (ctxt == NULL) {
600         Py_INCREF(Py_None);
601         return (Py_None);
602     }
603     ret =
604         PyCObject_FromVoidPtrAndDesc((void *) ctxt,
605                                      (char *) "xmlRelaxNGPtr", NULL);
606     return (ret);
607 }
608
609 PyObject *
610 libxml_xmlRelaxNGParserCtxtPtrWrap(xmlRelaxNGParserCtxtPtr ctxt)
611 {
612     PyObject *ret;
613
614 #ifdef DEBUG
615     printf("libxml_xmlRelaxNGParserCtxtPtrWrap: ctxt = %p\n", ctxt);
616 #endif
617     if (ctxt == NULL) {
618         Py_INCREF(Py_None);
619         return (Py_None);
620     }
621     ret =
622         PyCObject_FromVoidPtrAndDesc((void *) ctxt,
623                                      (char *) "xmlRelaxNGParserCtxtPtr", NULL);
624     return (ret);
625 }
626 PyObject *
627 libxml_xmlRelaxNGValidCtxtPtrWrap(xmlRelaxNGValidCtxtPtr valid)
628 {
629     PyObject *ret;
630
631 #ifdef DEBUG
632     printf("libxml_xmlRelaxNGValidCtxtPtrWrap: valid = %p\n", valid);
633 #endif
634     if (valid == NULL) {
635         Py_INCREF(Py_None);
636         return (Py_None);
637     }
638     ret =
639         PyCObject_FromVoidPtrAndDesc((void *) valid,
640                                      (char *) "xmlRelaxNGValidCtxtPtr", NULL);
641     return (ret);
642 }
643 #endif /* LIBXML_SCHEMAS_ENABLED */