Version: 9.15.0
parsers.cxx
Go to the documentation of this file.
1 // Copyright (C) 2006-2025 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 "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>
41 
42 #define BUFFSIZE 8192
43 
44 char Buff[BUFFSIZE];
45 
47 extern _xmlParserCtxt* saxContext;
48 namespace YACS
49 {
50 
52 {
53  _defaultParsersMap.clear();
55 }
56 
58 {
60  theRuntime->setCatalogLoaderFactory("proc",factory);
61 }
62 
63 ENGINE::Proc* YACSLoader::load(const char * file)
64 {
65  DEBTRACE("YACSLoader::load: " << file);
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;
115  if ( !_defaultParsersMap.empty() )
117  else
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 
147 {
148 }
149 
150 }
#define DEBTRACE(msg)
Definition: YacsTrace.hxx:31
Class for logging error messages.
Definition: Logger.hxx:38
virtual void fatal(const std::string &message, const char *filename, int line)
Definition: Logger.cxx:55
Base class for all schema objects.
Definition: Proc.hxx:44
virtual Logger * getLogger(const std::string &name)
Definition: Proc.cxx:431
virtual void setCatalogLoaderFactory(const std::string &name, CatalogLoader *factory)
Add a catalog loader factory to the map _catalogLoaderFactoryMap under the name name.
Definition: Runtime.cxx:305
const char * what(void) const noexcept
Definition: Exception.cxx:50
virtual YACS::ENGINE::Proc * load(const char *filename)
Definition: parsers.cxx:63
void registerProcCataLoader()
Definition: parsers.cxx:57
std::map< std::string, parser * > _defaultParsersMap
Definition: parsers.hxx:44
virtual ~YACSLoader()
Definition: parsers.cxx:146
YACSLIBENGINE_EXPORT Runtime * getRuntime()
Definition: Runtime.cxx:61
#define BUFFSIZE
Definition: parsers.cxx:42
YACS::ENGINE::Runtime * theRuntime
Definition: parsers.cxx:40
char Buff[BUFFSIZE]
Definition: parsers.cxx:44
YACS::ENGINE::Proc * currentProc
Definition: parserBase.cxx:30
_xmlParserCtxt * saxContext
Definition: parserBase.cxx:31
static void XMLCALL start_element(void *data, const xmlChar *name, const xmlChar **p)
Definition: parserBase.cxx:237
static void XMLCALL warning(void *data, const char *fmt,...)
Definition: parserBase.cxx:284
virtual void init()
Definition: parserBase.cxx:86
virtual void SetUserDataAndPush(parser *pp)
Definition: parserBase.cxx:55
static std::stack< parser * > _stackParser
Definition: parserBase.hxx:42
static void XMLCALL fatal_error(void *data, const char *fmt,...)
Definition: parserBase.cxx:322
static void XMLCALL end_document(void *data)
Definition: parserBase.cxx:231
static void XMLCALL comment(void *data, const xmlChar *value)
Definition: parserBase.cxx:268
static parser main_parser
Definition: parserBase.hxx:41
static void XMLCALL error(void *data, const char *fmt,...)
Definition: parserBase.cxx:303
static void XMLCALL cdata_block(void *data, const xmlChar *value, int len)
Definition: parserBase.cxx:275
static void XMLCALL end_element(void *data, const xmlChar *name)
Definition: parserBase.cxx:247
std::string _file
Definition: parserBase.hxx:96
static void XMLCALL start_document(void *data)
Definition: parserBase.cxx:225
static void XMLCALL characters(void *data, const xmlChar *ch, int len)
Definition: parserBase.cxx:259
void setDefaultMap(std::map< std::string, parser * > *)
Definition: rootParser.cxx:61
static roottypeParser rootParser
Definition: rootParser.hxx:32