Salome HOME
merge from branch DEV tag mergeto_trunk_04apr08
[modules/yacs.git] / src / runtime / XMLCORBAConv.cxx
1
2 #include "TypeConversions.hxx"
3 #include "XMLCORBAConv.hxx"
4 #include "CORBAXMLConv.hxx"
5 #include "CORBAPorts.hxx"
6 #include "TypeCode.hxx"
7
8 #include <libxml/parser.h>
9 #include <iostream>
10 #include <sstream>
11
12 //#define _DEVDEBUG_
13 #include "YacsTrace.hxx"
14
15 using namespace YACS::ENGINE;
16 using namespace std;
17
18 XmlCorba::XmlCorba(InputCorbaPort* p)
19   : ProxyPort(p), DataPort(p->getName(), p->getNode(), p->edGetType()), Port(p->getNode())
20 {
21 }
22
23 void XmlCorba::put(const void *data) throw(ConversionException)
24 {
25   DEBTRACE((const char *)data);
26   put((const char *)data);
27 }
28
29 //!Convert a XML (char *) to CORBA::Any  and push it in the proxy port
30  /*!
31   *   \param data : Xml::char *
32   */
33 void XmlCorba::put(const char *data) throw(ConversionException)
34 {
35   DEBTRACE(data);
36   xmlDocPtr doc;
37   xmlNodePtr cur;
38   CORBA::Any *a=NULL;
39   
40   doc = xmlParseMemory(data, strlen(data));
41   if (doc == NULL ) 
42     {
43       stringstream msg;
44       msg << "Problem in conversion: XML Document not parsed successfully ";
45       msg << " (" << __FILE__ << ":" << __LINE__ << ")";
46       throw ConversionException(msg.str());
47     }
48   cur = xmlDocGetRootElement(doc);
49   if (cur == NULL) 
50     {
51       xmlFreeDoc(doc);
52       stringstream msg;
53       msg << "Problem in conversion: empty XML Document";
54       msg << " (" << __FILE__ << ":" << __LINE__ << ")";
55       throw ConversionException(msg.str());
56     }
57   while (cur != NULL)
58     {
59       if ((!xmlStrcmp(cur->name, (const xmlChar *)"value")))
60         {
61           try
62             {
63               a=convertXmlCorba(edGetType(),doc,cur);
64             }
65           catch(ConversionException)
66             {
67               throw;
68             }
69           catch(...)
70             {
71               stringstream msg;
72               msg << "Problem in conversion: kind= " << edGetType()->kind() ;
73               msg << " (" << __FILE__ << ":" << __LINE__ << ")";
74               throw ConversionException(msg.str());
75             }
76           break;
77         }
78       cur = cur->next;
79     }
80   xmlFreeDoc(doc);
81   if(a==NULL)
82     {
83       stringstream msg;
84       msg << "Problem in conversion: incorrect XML value";
85       msg << " (" << __FILE__ << ":" << __LINE__ << ")";
86       throw ConversionException(msg.str());
87     }
88
89   //xmlCleanupParser();
90   _port->put(a);
91   _port->setStringRef(data);
92   //delete Any that has been allocated by convertXmlCorba
93   delete a;
94 }