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