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