Salome HOME
2937dc633dd0fe5eea4b7eac14281dd9a2c5439f
[modules/yacs.git] / src / yacsloader / outputParsers.hxx
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 #ifndef _OUTPUTPARSERS_HXX_
20 #define _OUTPUTPARSERS_HXX_
21
22 #include "nodeParsers.hxx"
23
24 #include "factory.hxx"
25
26 #include "DataNode.hxx"
27
28 namespace YACS
29 {
30
31 /*! \brief Class for outputdata parser.
32  *
33  *  Its XML schema is:
34  \verbatim
35      <xsd:complexType name="OutputDataType">
36        <xsd:attribute name="name" type="xsd:string" use="required"/>
37        <xsd:attribute name="type" type="xsd:string" use="required"/>
38        <xsd:attribute name="ref" type="xsd::string" />
39      </xsd:complexType>
40  \endverbatim
41  */
42 struct outputdatatypeParser: parser
43 {
44   static outputdatatypeParser outputdataParser;
45
46   virtual void onStart(const XML_Char* el, const XML_Char** attr)
47     {
48       DEBTRACE("outputdatatypeParser::onStart");
49       std::string element(el);
50       parser* pp=&parser::main_parser;
51       SetUserDataAndPush(pp);
52       pp->init();
53       pp->pre();
54       pp->buildAttr(attr);
55     }
56   virtual void onEnd(const char *el,parser* child)
57     {
58     }
59   virtual void buildAttr(const XML_Char** attr)
60     {
61       DEBTRACE("outputdatatypeParser::buildAttr");
62       required("name",attr);
63       required("type",attr);
64       for (int i = 0; attr[i]; i += 2)
65       {
66         if(std::string(attr[i]) == "name")name(attr[i+1]);
67         if(std::string(attr[i]) == "type")type(attr[i+1]);
68         if(std::string(attr[i]) == "ref")ref(attr[i+1]);
69       }
70     }
71   virtual void name (const std::string& name)
72     {
73       DEBTRACE("outputdatatypeParser::name");
74       _param._name=name;
75     }
76   virtual void type (const std::string& type)
77     {
78       DEBTRACE("outputdatatypeParser::type");
79       _param._type=type;
80     }
81   virtual void ref (const std::string& ref)
82     {
83       _param.setProperty("ref",ref);
84     }
85
86   virtual void pre ()
87     {
88       DEBTRACE("outputdatatypeParser::pre");
89       _param.clear();
90     }
91   virtual myoutport& post()
92     {
93       return _param;
94     }
95   myoutport _param;
96 };
97
98 /*! \brief Class for OutNode parser.
99  *
100  *  Its XML schema is:
101  \verbatim
102      <xsd:complexType name="OutNodeType">
103        <xsd:attribute name="name" type="xsd:string" use="required"/>
104        <xsd:attribute name="kind" type="xsd:string" />
105        <xsd:attribute name="ref" type="xsd:string" />
106        <xsd:element name="parameter" type="OutputDataType"/>
107      </xsd:complexType>
108  \endverbatim
109  */
110 template <class T=ENGINE::DataNode*>
111 struct outnodetypeParser:public nodetypeParser<T>
112 {
113   static outnodetypeParser<T> outnodeParser;
114   virtual void onStart(const XML_Char* el, const XML_Char** attr);
115   virtual void onEnd(const char *el,parser* child);
116   virtual void buildAttr(const XML_Char** attr);
117   virtual void pre ();
118   virtual void name (const std::string& name);
119   virtual void kind (const std::string& kind);
120   virtual void ref (const std::string& ref);
121   virtual void create ();
122   virtual void parameter (myoutport& p);
123   virtual T post();
124   std::string _name;
125   std::string _kind;
126   std::string _ref;
127 };
128
129 template <class T> outnodetypeParser<T> outnodetypeParser<T>::outnodeParser;
130
131
132 template <class T>
133 void outnodetypeParser<T>::onStart(const XML_Char* el, const XML_Char** attr)
134     {
135       DEBTRACE("outnodetypeParser::onStart");
136       std::string element(el);
137       parser* pp=&parser::main_parser;
138       if(element == "parameter")pp=&outputdatatypeParser::outputdataParser;
139       if(element == "property")pp=&propertytypeParser::propertyParser;
140       this->SetUserDataAndPush(pp);
141       pp->init();
142       pp->pre();
143       pp->buildAttr(attr);
144     }
145
146 template <class T>
147 void outnodetypeParser<T>::onEnd(const char *el,parser* child)
148     {
149       DEBTRACE("outnodetypeParser::onEnd");
150       std::string element(el);
151       if(element == "parameter")parameter(((outputdatatypeParser*)child)->post());
152       if(element == "property")this->property(((propertytypeParser*)child)->post());
153     }
154
155 template <class T>
156 void outnodetypeParser<T>::buildAttr(const XML_Char** attr)
157     {
158       DEBTRACE("outnodetypeParser::buildAttr");
159       this->required("name",attr);
160       for (int i = 0; attr[i]; i += 2)
161         {
162           if(std::string(attr[i]) == "name")name(attr[i+1]);
163           if(std::string(attr[i]) == "kind")kind(attr[i+1]);
164           if(std::string(attr[i]) == "ref")ref(attr[i+1]);
165         }
166       create();
167     }
168
169 template <class T>
170 void outnodetypeParser<T>::pre ()
171 {
172   this->_node=0;
173   _kind="";
174   _ref="";
175 }
176
177 template <class T>
178 void outnodetypeParser<T>::name (const std::string& name)
179 {
180   _name=name;
181 }
182 template <class T>
183 void outnodetypeParser<T>::kind (const std::string& kind)
184 {
185   _kind=kind;
186 }
187 template <class T>
188 void outnodetypeParser<T>::ref (const std::string& ref)
189 {
190   _ref=ref;
191 }
192
193 template <class T>
194 void outnodetypeParser<T>::create ()
195 {
196   this->_node = theRuntime->createOutDataNode(_kind,_name);
197 }
198
199 template <class T>
200 void outnodetypeParser<T>::parameter (myoutport& p)
201 {
202   DEBTRACE("outnodetypeParser::parameter");
203   if(currentProc->typeMap.count(p._type)==0)
204     {
205       //Check if the typecode is defined in the runtime
206       YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(p._type);
207       if(t==0)
208       {
209         std::string msg="Unknown Type: ";
210         msg=msg+p._type+" for node: "+this->_node->getName()+" port name: "+p._name;
211         this->logError(msg);
212         return;
213       }
214       else
215       {
216         currentProc->typeMap[p._type]=t;
217         t->incrRef();
218       }
219     }
220   ENGINE::InputPort *port = this->_node->edAddInputPort(p._name,currentProc->typeMap[p._type]);
221   this->_node->setData(port,p._props["ref"]);
222 }
223
224 template <class T>
225 T outnodetypeParser<T>::post()
226 {
227   this->_node->setRef(_ref);
228   return this->_node;
229 }
230
231 }
232 #endif