Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / yacsloader / ProcCataLoader.cxx
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 #include "ProcCataLoader.hxx"
20 #include "parsers.hxx"
21 #include "Proc.hxx"
22 #include "Logger.hxx"
23 #include "TypeCode.hxx"
24
25 #include <set>
26 #include <iostream>
27 #include <sstream>
28 #include <stdexcept>
29
30
31 //#define _DEVDEBUG_
32 #include "YacsTrace.hxx"
33
34 using namespace YACS::ENGINE;
35
36 ProcCataLoader::ProcCataLoader(YACS::YACSLoader* xmlLoader,const std::string& path):CatalogLoader(path),_xmlLoader(xmlLoader)
37 {
38   //path should be a file path
39 }
40
41 ProcCataLoader::~ProcCataLoader()
42 {
43   DEBTRACE ("ProcCataLoader::~ProcCataLoader");
44 }
45
46 void ProcCataLoader::loadCata(Catalog* cata)
47 {
48   DEBTRACE("ProcCataLoader::load");
49   Proc* p;
50   try
51     {
52       p=_xmlLoader->load(_path.c_str());
53       if(p==0)
54         {
55           std::string msg="the file is probably not a YACS schema file";
56           cata->setErrors(msg);
57           std::cerr << msg << std::endl;
58           return; 
59         }
60     }
61   catch (YACS::Exception& e)
62     {
63       std::string msg="Caught a YACS exception";
64       msg=msg + e.what();
65       std::cerr << msg << std::endl;
66       cata->setErrors(msg);
67       return ;
68     }
69   catch (const std::invalid_argument& e)
70     {
71       cata->setErrors(e.what());
72       return ;
73     }
74   catch (const std::ios_base::failure&)
75     {
76       std::string msg="Caught an io failure exception";
77       std::cerr << msg << std::endl;
78       cata->setErrors(msg);
79       return ;
80     }
81
82   //Get the parser logger
83   Logger* logger=p->getLogger("parser");
84   //Print errors logged if any
85   if(!logger->isEmpty())
86     {
87       std::string msg=logger->getStr();
88       std::cerr << msg << std::endl;
89       cata->setErrors(msg);
90     }
91
92   std::map<std::string,TypeCode*>& typeMap=cata->_typeMap;
93   std::map<std::string,Node*>& nodeMap=cata->_nodeMap;
94   std::map<std::string,ComposedNode*>& composednodeMap=cata->_composednodeMap;
95   std::map<std::string,ComponentDefinition*>& componentMap=cata->_componentMap;
96
97   std::map<std::string,TypeCode*>::iterator it=p->typeMap.begin();
98   while(it != p->typeMap.end())
99     {
100       typeMap[it->first]=it->second;
101       it->second->incrRef();
102       it++;
103     }
104
105   std::list<Node *> s=p->getChildren();
106   for(std::list<Node *>::iterator iter=s.begin();iter!=s.end();iter++)
107     {
108       YACS::ENGINE::ComposedNode * cnode= dynamic_cast<YACS::ENGINE::ComposedNode *>(*iter);
109       if(cnode)
110         composednodeMap[cnode->getName()]=(YACS::ENGINE::ComposedNode *) cnode->clone(0);
111       else 
112         nodeMap[(*iter)->getName()]=(*iter)->clone(0);
113     }
114   delete p;
115 }