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