Salome HOME
Synchronize adm files
[modules/yacs.git] / src / yacsloader / parsers.cxx
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 #include "parsers.hxx"
21
22 #include <expat.h>
23 #include <iostream>
24 #include <stdexcept>
25 #include <cstdio>
26
27 #include "Runtime.hxx"
28 #include "Proc.hxx"
29 #include "ProcCataLoader.hxx"
30 #include "Logger.hxx"
31
32 #include "rootParser.hxx"
33
34 //#define _DEVDEBUG_
35 #include "YacsTrace.hxx"
36
37 YACS::ENGINE::Runtime* theRuntime=0;
38
39 #define BUFFSIZE        8192
40
41 char Buff[BUFFSIZE];
42
43 extern YACS::ENGINE::Proc* currentProc;
44 extern XML_Parser p ;
45
46 namespace YACS
47 {
48
49 YACSLoader::YACSLoader()
50 {
51   _defaultParsersMap.clear();
52
53   theRuntime = ENGINE::getRuntime();
54 }
55
56 void YACSLoader::registerProcCataLoader()
57 {
58   YACS::ENGINE::ProcCataLoader* factory= new YACS::ENGINE::ProcCataLoader(this);
59   theRuntime->setCatalogLoaderFactory("proc",factory);
60 }
61
62 ENGINE::Proc* YACSLoader::load(const char * file)
63 {
64   DEBTRACE("YACSLoader::load: " << file);
65   FILE* fin=fopen(file,"r");
66   if (! fin) 
67     {
68       std::cerr << "Couldn't open schema file" << std::endl;
69       throw std::invalid_argument("Couldn't open schema file");
70     }
71
72   p = XML_ParserCreate(NULL);
73   if (! p) 
74     {
75       std::cerr << "Couldn't allocate memory for parser" << std::endl;
76       throw Exception("Couldn't allocate memory for parser");
77     }
78   XML_SetElementHandler(p, parser::start,parser::end);
79   XML_SetCharacterDataHandler(p,parser::charac );
80
81   parser::main_parser.SetUserDataAndPush(&YACS::roottypeParser::rootParser);
82
83   // OCC: san -- Allow external parsers for handling of unknown elements
84   // and attributes. This capability is used by YACS GUI to read
85   // graph presentation data
86   if ( !_defaultParsersMap.empty() )
87     roottypeParser::rootParser.setDefaultMap(&_defaultParsersMap);
88   else
89     roottypeParser::rootParser.setDefaultMap(0);
90   
91   parser::main_parser._file=file;
92
93   currentProc=0;
94
95   try
96     {
97       for (;;) 
98         {
99           int done;
100           int len;
101
102           len = fread(Buff, 1, BUFFSIZE, fin);
103           if (ferror(fin)) 
104             {
105               std::cerr << "Read error" << std::endl;
106               throw Exception("Read error");
107             }
108           done = feof(fin);
109
110           if (XML_Parse(p, Buff, len, done) == XML_STATUS_ERROR) 
111             {
112               if(currentProc==0)
113                 {
114                   std::cerr <<XML_ErrorString(XML_GetErrorCode(p))<<" "<<file<<" "<<XML_GetCurrentLineNumber(p)<<std::endl;
115                   break;
116                 }
117               YACS::ENGINE::Logger* logger=currentProc->getLogger("parser");
118               logger->fatal(XML_ErrorString(XML_GetErrorCode(p)),file,XML_GetCurrentLineNumber(p));
119               break;
120             }
121
122           if (done)
123             break;
124         }
125       XML_ParserFree (p);
126       p=0;
127       return currentProc;
128     }
129   catch(Exception& e)
130     {
131       //get line number from XML parser
132       YACS::ENGINE::Logger* logger=currentProc->getLogger("parser");
133       logger->fatal(e.what(),file,XML_GetCurrentLineNumber(p));
134       XML_ParserFree (p);
135       p=0;
136       return currentProc;
137     }
138 }
139
140 YACSLoader::~YACSLoader()
141 {
142 }
143
144 }