Salome HOME
merge from branch DEV tag mergeto_trunk_04apr08
[modules/yacs.git] / src / runtime / XMLPythonConv.cxx
1
2 #include "XMLPythonConv.hxx"
3 #include "TypeConversions.hxx"
4
5 #include <libxml/parser.h>
6 #include <iostream>
7 #include <sstream>
8
9 //#define _DEVDEBUG_
10 #include "YacsTrace.hxx"
11
12 using namespace YACS::ENGINE;
13 using namespace std;
14
15 XmlPython::XmlPython(InputPyPort* p)
16   : ProxyPort(p), DataPort(p->getName(), p->getNode(), p->edGetType()), Port(p->getNode())
17 {
18 }
19
20 void XmlPython::put(const void *data) throw(ConversionException)
21 {
22   DEBTRACE((const char *)data);
23   put((const char *)data);
24 }
25
26 //!Convert a XML (char *) to PyObject and push it into proxy port
27  /*!
28   *   \param data : Xml::char *
29   */
30
31 void XmlPython::put(const char *data) throw(ConversionException)
32 {
33   DEBTRACE(data);
34   xmlDocPtr doc;
35   xmlNodePtr cur;
36   PyObject *ob=NULL;
37 {
38   InterpreterUnlocker l;
39   doc = xmlParseMemory(data, strlen(data));
40   if (doc == NULL )
41     {
42       stringstream msg;
43       msg << "Problem in conversion: XML Document not parsed successfully ";
44       msg << " (" << __FILE__ << ":" << __LINE__ << ")";
45       throw ConversionException(msg.str());
46     }
47   cur = xmlDocGetRootElement(doc);
48   if (cur == NULL)
49     {
50       xmlFreeDoc(doc);
51       stringstream msg;
52       msg << "Problem in conversion: empty XML Document";
53       msg << " (" << __FILE__ << ":" << __LINE__ << ")";
54       throw ConversionException(msg.str());
55     }
56   while (cur != NULL)
57     {
58       if ((!xmlStrcmp(cur->name, (const xmlChar *)"value")))
59         {
60           ob=convertXmlPyObject(edGetType(),doc,cur);
61           break;
62         }
63       cur = cur->next;
64     }
65   xmlFreeDoc(doc);
66   if(ob==NULL)
67     {
68       stringstream msg;
69       msg << "Problem in conversion: incorrect XML value";
70       msg << " (" << __FILE__ << ":" << __LINE__ << ")";
71       throw ConversionException(msg.str());
72     }
73 //  xmlCleanupParser();
74 #ifdef _DEVDEBUG_
75   PyObject_Print(ob,stderr,Py_PRINT_RAW);
76   cerr << endl;
77 #endif
78   _port->put(ob);
79   Py_XDECREF(ob);
80   DEBTRACE("ob refcnt: " << ob->ob_refcnt);
81   _port->setStringRef(data);
82 }
83 }