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