Salome HOME
Removed expat support (replaced by libxml2)
[modules/yacs.git] / src / yacsloader / nodeParsers.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 _NODEPARSERS_HXX_
21 #define _NODEPARSERS_HXX_
22
23 #include "parserBase.hxx"
24 #include "containerParsers.hxx"
25 #include "dataParsers.hxx"
26 #include "portParsers.hxx"
27 #include "codeParsers.hxx"
28 #include "propertyParsers.hxx"
29
30 #include "Proc.hxx"
31 #include "TypeCode.hxx"
32 #include "InlineNode.hxx"
33 #include "Exception.hxx"
34 #include "Runtime.hxx"
35 #include "Container.hxx"
36 #include "ComponentInstance.hxx"
37 #include "OutputDataStreamPort.hxx"
38 #include "InputDataStreamPort.hxx"
39 #include "ComponentInstance.hxx"
40 #include "factory.hxx"
41
42 extern YACS::ENGINE::Proc* currentProc;
43 extern YACS::ENGINE::Runtime* theRuntime;
44
45 namespace YACS
46 {
47
48 /*! \brief Class for node parser.
49  *
50  *  This class is a base class for other parsers
51  */
52 template <class T=YACS::ENGINE::InlineNode*>
53 struct nodetypeParser: parser
54 {
55   static nodetypeParser<T> nodeParser;
56
57   virtual void onStart(const XML_Char* el, const XML_Char** attr)
58     {
59       DEBTRACE( "nodetypeParser::onStart: " << el )             
60       std::string element(el);
61       parser* pp=&parser::main_parser;
62       this->SetUserDataAndPush(pp);
63       pp->init();
64       pp->pre();
65       pp->buildAttr(attr);
66     }
67   virtual void onEnd(const char *el,parser* child)
68     {
69       DEBTRACE( "nodetypeParser::onEnd: " << el )             
70       std::string element(el);
71     }
72   virtual void buildAttr(const XML_Char** attr)
73     {
74       if (!attr)
75         return;
76       required("name",attr);
77       required("type",attr);
78       for (int i = 0; attr[i]; i += 2) 
79       {
80         if(std::string(attr[i]) == "name")name(attr[i+1]);
81         if(std::string(attr[i]) == "state")state(attr[i+1]);
82         if(std::string(attr[i]) == "type")type(attr[i+1]);
83       }
84     }
85   virtual void pre()
86     {
87       _node=0;
88       _container="";
89     }
90   virtual void name (const std::string& name)
91     {
92       DEBTRACE( "inline_name: " << name )             
93       _name=name;
94     }
95   virtual void state (const std::string& name)
96     {
97       _state=name;
98     }
99   virtual void type (const std::string& name)
100     {
101       DEBTRACE( "node_type " << name )             
102       _type=name;
103     }
104   virtual void property (const myprop& prop);
105   virtual T post();
106   std::string _type;
107   std::string _name;
108   std::string _state;
109   std::string _container;
110   T _node;
111 };
112
113 template <class T> nodetypeParser<T> nodetypeParser<T>::nodeParser;
114
115 template <class T>
116 void nodetypeParser<T>::property (const myprop& prop)
117 {
118    if(this->_node==0)
119      throw YACS::Exception("Node must be completely defined before setting its properties");
120   _node->setProperty(prop._name,prop._value);
121 }
122
123 template <class T>
124 T nodetypeParser<T>::post()
125 {
126   return _node;
127 }
128
129 template <>
130 YACS::ENGINE::InlineNode* nodetypeParser<YACS::ENGINE::InlineNode*>::post ()
131 {
132   std::string fullname = currentProc->names.back()+_type;
133   if(currentProc->inlineMap.count(_type) != 0)
134     {
135       //InlineNode type with absolute name found
136       YACS::ENGINE::InlineNode* n=currentProc->inlineMap[_type];
137       _node=n->cloneNode(_name);
138     }
139   else if(currentProc->inlineMap.count(fullname) != 0)
140     {
141       //InlineNode type with relative name found
142       YACS::ENGINE::InlineNode* n=currentProc->inlineMap[fullname];
143       _node=n->cloneNode(_name);
144     }
145   else
146     {
147       throw YACS::Exception("Unknown InlineNode type");
148     }
149   if(_state == "disabled")_node->exDisabledState();
150   DEBTRACE( "node_post " << _node->getName() )             
151   return _node;
152 }
153
154 } // end of namespace YACS
155
156 #endif