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