Salome HOME
Copyright update: 2016
[modules/yacs.git] / src / runtime / XMLCppConv.cxx
1 // Copyright (C) 2006-2016  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "TypeConversions.hxx"
21 #include "XMLCppConv.hxx"
22
23
24 #include <libxml/parser.h>
25 #include <iostream>
26 #include <sstream>
27 #include "Any.hxx"
28
29 //#define _DEVDEBUG_
30 #include "YacsTrace.hxx"
31
32 using namespace std;
33
34 namespace YACS
35 {
36   namespace ENGINE
37   {
38     Any * convertXmlCpp(const TypeCode *t, xmlDocPtr doc, xmlNodePtr cur)
39     {
40       return convertXmlNeutral(t, doc, cur);
41     }
42
43     XmlCpp::XmlCpp(InputPort* p)
44       : ProxyPort(p), DataPort(p->getName(), p->getNode(), p->edGetType()), Port(p->getNode())
45     {
46     }
47
48     void XmlCpp::put(const void *data) throw(ConversionException)
49     {
50       DEBTRACE(" XmlCpp::put(const void *data)");
51       put((const char *)data);
52     }
53
54     //!Convert received XML (char *) value to C++ (Any *) value and send it to proxy port
55     /*!
56      *   \param data : Xml::char *
57      */
58
59     void XmlCpp::put(const char *data) throw(ConversionException)
60     {
61       DEBTRACE("XmlCpp::put " << data);
62       xmlDocPtr doc;
63       xmlNodePtr cur;
64       Any *ob=NULL;
65       {
66         doc = xmlParseMemory(data, strlen(data));
67         if (doc == NULL )
68           {
69             stringstream msg;
70             msg << "Problem in conversion: XML Document not parsed successfully ";
71             msg << " (" << __FILE__ << ":" << __LINE__ << ")";
72             throw ConversionException(msg.str());
73           }
74         cur = xmlDocGetRootElement(doc);
75         if (cur == NULL)
76           {
77             xmlFreeDoc(doc);
78             stringstream msg;
79             msg << "Problem in conversion: empty XML Document";
80             msg << " (" << __FILE__ << ":" << __LINE__ << ")";
81             throw ConversionException(msg.str());
82           }
83         while (cur != NULL)
84           {
85             if ((!xmlStrcmp(cur->name, (const xmlChar *)"value")))
86               {
87                 ob=convertXmlCpp(edGetType(),doc,cur);
88                 break;
89               }
90             cur = cur->next;
91           }
92         xmlFreeDoc(doc);
93         if(ob==NULL)
94           {
95             stringstream msg;
96             msg << "Problem in conversion: incorrect XML value";
97             msg << " (" << __FILE__ << ":" << __LINE__ << ")";
98             throw ConversionException(msg.str());
99           }
100       }
101       _port->put(ob);
102       ob->decrRef();
103     }
104
105     int isAdaptableCppXml(const TypeCode *t1, const TypeCode *t2)
106     {
107       return isAdaptableNeutralXml(t1, t2);
108     }
109   }
110 }