Salome HOME
Merge from V6_main 01/04/2013
[modules/yacs.git] / src / yacsloader / portParsers.hxx
1 // Copyright (C) 2006-2013  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
20 #ifndef _PORTPARSERS_HXX_
21 #define _PORTPARSERS_HXX_
22
23 #include "parserBase.hxx"
24 #include "propertyParsers.hxx"
25
26 #include "factory.hxx"
27
28 namespace YACS
29 {
30
31 /*! \brief Class for Inport parser.
32  *
33  *  This class is a base class for other inport parsers
34  *
35  *  XML schema:
36    \verbatim
37      <xsd:complexType name="InPortType">
38        <xsd:sequence>
39          <xsd:element name="property" type="PropertyType" minOccurs="0"/>
40        </xsd:sequence>
41        <xsd:attribute name="name" type="xsd:string" use="required"/>
42        <xsd:attribute name="type" type="xsd:string" use="required"/>
43      </xsd:complexType>
44    \endverbatim
45  *
46  */
47 template <class T=myinport>
48 struct inporttypeParser: parser
49 {
50   static inporttypeParser<T> inportParser;
51   virtual void onStart(const XML_Char* el, const XML_Char** attr);
52   virtual void onEnd(const char *el,parser* child);
53   virtual void buildAttr(const XML_Char** attr);
54   virtual void pre ();
55   virtual void name(const std::string& name);
56   virtual void type(const std::string& type);
57   virtual void property (const myprop& prop);
58   virtual T& post();
59 protected:
60     T _port;
61 };
62
63 /*! \brief Class for Outport parser.
64  *
65  *  This class is also used for OutputDataStream Port
66  *  same XML schema as inporttypeParser
67  */
68 template <class T=myoutport>
69 struct outporttypeParser:public inporttypeParser<T>
70 {
71   static outporttypeParser<T> outportParser;
72 };
73
74 template <class T> inporttypeParser<T> inporttypeParser<T>::inportParser;
75 template <class T> outporttypeParser<T> outporttypeParser<T>::outportParser;
76
77 template <class T>
78   void inporttypeParser<T>::onStart(const XML_Char* el, const XML_Char** attr)
79     {
80       std::string element(el);
81       parser* pp=&parser::main_parser;
82       if(element == "property")pp=&propertytypeParser::propertyParser;
83       this->SetUserDataAndPush(pp);
84       pp->init();
85       pp->pre();
86       pp->buildAttr(attr);
87     }
88 template <class T>
89   void inporttypeParser<T>::onEnd(const char *el,parser* child)
90     {
91       std::string element(el);
92       if(element == "property")property(((propertytypeParser*)child)->post());
93     }
94 template <class T>
95   void inporttypeParser<T>::buildAttr(const XML_Char** attr)
96     {
97       required("name",attr);
98       required("type",attr);
99       for (int i = 0; attr[i]; i += 2) 
100         {
101           if(std::string(attr[i]) == "name")name(attr[i+1]);
102           if(std::string(attr[i]) == "type")type(attr[i+1]);
103         }
104     }
105 template <class T>
106   void inporttypeParser<T>::pre ()
107     {
108       _port._name="";
109       _port._type="";
110       _port.clear();
111     }
112 template <class T>
113   void inporttypeParser<T>::name(const std::string& name)
114     {
115       _port._name=name;
116     }
117 template <class T>
118   void inporttypeParser<T>::type(const std::string& type)
119     {
120       _port._type=type;
121     }
122 template <class T>
123   void inporttypeParser<T>::property (const myprop& prop)
124     {
125       DEBTRACE( "property_set: " << prop._name << prop._value )             
126       _port.setProperty(prop._name,prop._value);
127     }
128 template <class T>
129   T& inporttypeParser<T>::post()
130     {
131       return _port;
132     }
133
134 }
135
136 #endif