Salome HOME
68208ebc82c08d577d811e52b66d472843b119d5
[modules/yacs.git] / src / yacsloader / parsers.cxx
1 // Copyright (C) 2006-2023  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 <iostream>
23 #include <stdexcept>
24 #include <cstdio>
25
26 #include "Runtime.hxx"
27 #include "Proc.hxx"
28 #include "ProcCataLoader.hxx"
29 #include "Logger.hxx"
30
31 #include "rootParser.hxx"
32
33 //#define _DEVDEBUG_
34 #include "YacsTrace.hxx"
35
36 #include <libxml/parser.h>
37 #include <libxml/tree.h>
38 #include <libxml/xmlreader.h>
39 #include <libxml/parserInternals.h>
40 YACS::ENGINE::Runtime* theRuntime=0;
41
42 #define BUFFSIZE        8192
43
44 char Buff[BUFFSIZE];
45
46 extern YACS::ENGINE::Proc* currentProc;
47 extern _xmlParserCtxt* saxContext;
48 namespace YACS
49 {
50
51 YACSLoader::YACSLoader()
52 {
53   _defaultParsersMap.clear();
54   theRuntime = ENGINE::getRuntime();
55 }
56
57 void YACSLoader::registerProcCataLoader()
58 {
59   YACS::ENGINE::ProcCataLoader* factory= new YACS::ENGINE::ProcCataLoader(this);
60   theRuntime->setCatalogLoaderFactory("proc",factory);
61 }
62
63 ENGINE::Proc* YACSLoader::load(const char * file)
64 {
65   DEBTRACE("YACSLoader::load: " << file);
66   parser::main_parser.init();
67   parser::main_parser._stackParser.push(&parser::main_parser);
68   xmlSAXHandler baseHandler =
69   {
70     0,  // internal_subset,
71     0,  // isStandalone
72     0,  // hasInternalSubset
73     0,  // hasExternalSubset
74     0,  // resolveEntity
75     0,  // getEntity
76     0,  // entityDecl
77     0,  // notationDecl
78     0,  // attributeDecl
79     0,  // elementDecl
80     0,  // unparsedEntityDecl
81     0,  // setDocumentLocator
82     parser::start_document, // startDocument
83     parser::end_document,   // endDocument
84     parser::start_element,  // startElement
85     parser::end_element,    // endElement
86     0,  // reference
87     parser::characters,     // characters
88     0,  // ignorableWhitespace
89     0,  // processingInstruction
90     parser::comment,        // comment
91     parser::warning,  // warning
92     parser::error,  // error
93     parser::fatal_error,  // fatalError
94     0,  // getParameterEntity
95     parser::cdata_block,    // cdataBlock
96     0  // externalSubset
97   };
98
99   FILE* fin=fopen(file,"r");
100   if (! fin)
101   {
102     std::cerr << "Couldn't open schema file" << std::endl;
103     throw std::invalid_argument("Couldn't open schema file");
104   }
105
106   saxContext = xmlCreateFileParserCtxt(file);
107
108   if (!saxContext)
109   {
110     std::cerr << "Couldn't allocate memory for parser" << std::endl;
111     throw Exception("Couldn't allocate memory for parser");
112   }
113   saxContext->sax = &baseHandler;
114   parser::main_parser.SetUserDataAndPush(&YACS::roottypeParser::rootParser);
115   if ( !_defaultParsersMap.empty() )
116     roottypeParser::rootParser.setDefaultMap(&_defaultParsersMap);
117   else
118     roottypeParser::rootParser.setDefaultMap(0);
119
120   parser::main_parser._file = file;
121   currentProc = 0;
122
123   try
124   {
125     if ( xmlParseDocument(saxContext) == -1 )
126     {
127       if(!currentProc)
128         throw Exception("Basic error during parsing.");
129       YACS::ENGINE::Logger* logger = currentProc->getLogger("parser");
130       logger->fatal( saxContext->lastError.message, file, saxContext->input->line );
131     }
132     saxContext = 0;
133     return currentProc;
134   }
135   catch(Exception& e)
136   {
137     if(!currentProc)
138       throw e;
139     YACS::ENGINE::Logger* logger = currentProc->getLogger("parser");
140     logger->fatal(e.what(), file, saxContext->input->line);
141     saxContext = 0;
142     return currentProc;
143   }
144 }
145
146 YACSLoader::~YACSLoader()
147 {
148 }
149
150 }