Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / yacsloader / linkParsers.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 _LINKPARSERS_HXX_
20 #define _LINKPARSERS_HXX_
21
22 #include "parserBase.hxx"
23 #include "dataParsers.hxx"
24 #include "propertyParsers.hxx"
25
26 #include "factory.hxx"
27
28 namespace YACS
29 {
30 template <class T=mycontrol>
31 struct controltypeParser: parser
32 {
33   static controltypeParser<T> controlParser;
34
35   virtual void onStart(const XML_Char* el, const XML_Char** attr)
36     {
37       std::string element(el);
38       parser* pp=&parser::main_parser;
39       this->maxcount("fromnode",1,element);
40       this->maxcount("tonode",1,element);
41       if(element == "fromnode")pp=&stringtypeParser::stringParser;
42       else if(element == "tonode")pp=&stringtypeParser::stringParser;
43       this->SetUserDataAndPush(pp);
44       pp->init();
45       pp->pre();
46       pp->buildAttr(attr);
47     }
48   virtual void onEnd(const char *el,parser* child)
49     {
50       std::string element(el);
51       if(element == "fromnode")fromnode(((stringtypeParser*)child)->post());
52       else if(element == "tonode")tonode(((stringtypeParser*)child)->post());
53     }
54   virtual void pre ()
55     {
56       _link.clear();
57     }
58   virtual void fromnode (const std::string& name)
59     {
60       _link.fromnode(name);
61     }
62   virtual void tonode (const std::string& name)
63     {
64       _link.tonode(name);
65     }
66   virtual void property (const myprop& prop)
67     {
68       DEBTRACE( "property_set: " << prop._name << prop._value )             
69       _link.setProperty(prop._name,prop._value);
70     }
71   virtual T& post()
72     {
73       mincount("fromnode",1);
74       mincount("tonode",1);
75       return _link;
76     }
77     T _link;
78 };
79
80 template <class T=mylink>
81 struct linktypeParser: controltypeParser<T>
82 {
83   static linktypeParser<T> linkParser;
84
85   virtual void onStart(const XML_Char* el, const XML_Char** attr)
86     {
87       std::string element(el);
88       this->maxcount("fromnode",1,element);
89       this->maxcount("tonode",1,element);
90       this->maxcount("fromport",1,element);
91       this->maxcount("toport",1,element);
92       parser* pp=&parser::main_parser;
93       if(element == "fromnode")pp=&stringtypeParser::stringParser;
94       else if(element == "tonode")pp=&stringtypeParser::stringParser;
95       else if(element == "toport")pp=&stringtypeParser::stringParser;
96       else if(element == "fromport")pp=&stringtypeParser::stringParser;
97       else if(element == "property")pp=&propertytypeParser::propertyParser;
98       this->SetUserDataAndPush(pp);
99       pp->init();
100       pp->pre();
101       pp->buildAttr(attr);
102     }
103   virtual void onEnd(const char *el,parser* child)
104     {
105       std::string element(el);
106       if(element == "fromnode")this->fromnode(((stringtypeParser*)child)->post());
107       else if(element == "tonode")this->tonode(((stringtypeParser*)child)->post());
108       else if(element == "toport")toport(((stringtypeParser*)child)->post());
109       else if(element == "fromport")fromport(((stringtypeParser*)child)->post());
110       else if(element == "property")this->property(((propertytypeParser*)child)->post());
111     }
112   virtual void buildAttr(const XML_Char** attr)
113     {
114       for (int i = 0; attr[i]; i += 2) 
115         {
116           if((std::string(attr[i]) == "control")
117              && (std::string(attr[i+1]) == "false"))
118             this->_link._withControl=false;
119         }
120     }
121   virtual void fromport (const std::string& name)
122     {
123       this->_link.fromport(name);
124     }
125   virtual void toport (const std::string& name)
126     {
127       this->_link.toport(name);
128     }
129   virtual T& post()
130     {
131       this->mincount("fromnode",1);
132       this->mincount("tonode",1);
133       this->mincount("fromport",1);
134       this->mincount("toport",1);
135       return this->_link;
136     }
137 };
138
139 template <class T=mystream>
140 struct streamtypeParser: linktypeParser<T>
141 {
142   static streamtypeParser<T> streamParser;
143 };
144
145 template <class T> streamtypeParser<T> streamtypeParser<T>::streamParser;
146 template <class T> controltypeParser<T> controltypeParser<T>::controlParser;
147 template <class T> linktypeParser<T> linktypeParser<T>::linkParser;
148
149 }
150
151 #endif