Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / runtime / XMLCppConv.cxx
1 //  Copyright (C) 2006-2008  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.
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 #include "TypeConversions.hxx"
20 #include "XMLCppConv.hxx"
21
22
23 #include <libxml/parser.h>
24 #include <iostream>
25 #include <sstream>
26 #include "Any.hxx"
27
28 //#define _DEVDEBUG_
29 #include "YacsTrace.hxx"
30
31 using namespace std;
32
33 namespace YACS
34 {
35   namespace ENGINE
36   {
37     Any * convertXmlCpp(const TypeCode *t, xmlDocPtr doc, xmlNodePtr cur)
38     {
39       return convertXmlNeutral(t, doc, cur);
40     }
41
42     XmlCpp::XmlCpp(InputPort* p)
43       : ProxyPort(p), DataPort(p->getName(), p->getNode(), p->edGetType()), Port(p->getNode())
44     {
45     }
46
47     void XmlCpp::put(const void *data) throw(ConversionException)
48     {
49       DEBTRACE(" XmlCpp::put(const void *data)");
50       put((const char *)data);
51     }
52
53     //!Convert received XML (char *) value to C++ (Any *) value and send it to proxy port
54     /*!
55      *   \param data : Xml::char *
56      */
57
58     void XmlCpp::put(const char *data) throw(ConversionException)
59     {
60       DEBTRACE("XmlCpp::put " << data);
61       xmlDocPtr doc;
62       xmlNodePtr cur;
63       Any *ob=NULL;
64       {
65         doc = xmlParseMemory(data, strlen(data));
66         if (doc == NULL )
67           {
68             stringstream msg;
69             msg << "Problem in conversion: XML Document not parsed successfully ";
70             msg << " (" << __FILE__ << ":" << __LINE__ << ")";
71             throw ConversionException(msg.str());
72           }
73         cur = xmlDocGetRootElement(doc);
74         if (cur == NULL)
75           {
76             xmlFreeDoc(doc);
77             stringstream msg;
78             msg << "Problem in conversion: empty XML Document";
79             msg << " (" << __FILE__ << ":" << __LINE__ << ")";
80             throw ConversionException(msg.str());
81           }
82         while (cur != NULL)
83           {
84             if ((!xmlStrcmp(cur->name, (const xmlChar *)"value")))
85               {
86                 ob=convertXmlCpp(edGetType(),doc,cur);
87                 break;
88               }
89             cur = cur->next;
90           }
91         xmlFreeDoc(doc);
92         if(ob==NULL)
93           {
94             stringstream msg;
95             msg << "Problem in conversion: incorrect XML value";
96             msg << " (" << __FILE__ << ":" << __LINE__ << ")";
97             throw ConversionException(msg.str());
98           }
99       }
100       _port->put(ob);
101       ob->decrRef();
102     }
103
104     int isAdaptableCppXml(const TypeCode *t1, const TypeCode *t2)
105     {
106       return isAdaptableNeutralXml(t1, t2);
107     }
108   }
109 }