Salome HOME
Merge branch 'master' into omu/workloadmanager
[modules/yacs.git] / src / yacsloader / xmlParserBase.hxx
1 // Copyright (C) 2006-2020  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 #ifndef __XMLPARSERBASE_HXX_
21 #define __XMLPARSERBASE_HXX_
22
23 #include <libxml/parser.h>
24
25 #define XMLCALL
26 #define XML_Char char
27 inline XML_Char* tochar(const xmlChar *c) { return (XML_Char*)c; };
28
29 // --- generic part -----------------------------------------------------------
30
31 #include <string>
32 #include <map>
33 #include <stack>
34 #include <list>
35
36 class xmlParserBase;
37
38 //! \brief base class for parse an xml file, use a dedicated parser, runtime independant.
39
40 class xmlReader
41 {
42 public:
43   xmlReader(xmlParserBase* parser);
44   virtual void parse(std::string xmlFile);
45 protected:
46   xmlParserBase* _rootParser;
47 };
48
49 //! \brief base class for xml parsers, runtime independant
50
51 class xmlParserBase
52 {
53 public:
54 #ifndef SWIG
55   static void XMLCALL start_document(void* userData);
56   static void XMLCALL end_document  (void* userData);
57   static void XMLCALL start_element (void* userData,
58                                      const xmlChar* name,
59                                      const xmlChar** p);
60   static void XMLCALL end_element   (void* userData,
61                                      const xmlChar* name);
62   static void XMLCALL characters    (void* userData,
63                                      const xmlChar* ch,
64                                      int len);
65   static void XMLCALL comment       (void* userData,
66                                      const xmlChar* value);
67   static void XMLCALL warning       (void* userData,
68                                      const char* fmt, ...);
69   static void XMLCALL error         (void* userData,
70                                      const char* fmt, ...);
71   static void XMLCALL fatal_error   (void* userData,
72                                      const char* fmt, ...);
73   static void XMLCALL cdata_block   (void* userData,
74                                      const xmlChar* value,
75                                      int len);
76 #endif
77   static void cleanGarbage();
78   static int getGarbageSize() {return _garbage.size(); };
79 public:
80   void setAttribute(std::string key, std::string value);
81   std::string getAttribute(std::string key);
82   virtual void addData(std::string value);
83   virtual void init (const xmlChar** p, xmlParserBase* father=0);
84
85   std::map< std::string, int > counts;
86
87   static _xmlParserCtxt* _xmlParser;
88   static void XML_SetUserData(_xmlParserCtxt* ctxt,
89                               xmlParserBase* parser);
90
91   static std::stack<xmlParserBase*> _stackParser;
92
93 protected:
94   void getAttributes(const xmlChar** p);
95
96   virtual void onStart  (const XML_Char* elem, const xmlChar** p);
97   virtual void onEnd    (const XML_Char* name);
98   virtual void charData (std::string data);
99   virtual void incrCount(const XML_Char *elem);
100   virtual void end();
101   virtual void stopParse(std::string what);
102
103 protected:
104   std::map<std::string, std::string> _mapAttrib;
105   static std::list<xmlParserBase*> _garbage;
106   std::string _data;
107   xmlParserBase *_father;
108 };
109
110 #endif