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