Salome HOME
066b11cc7d90f2421731550259dd7dbe29763b9c
[modules/yacs.git] / src / yacsloader / xmlParserBase.hxx
1 #ifndef __XMLPARSERBASE_HXX_
2 #define __XMLPARSERBASE_HXX_
3
4
5 // --- select only one of the following packages ------------------------------
6 // - libxml2 comes with gnome, so it's almost always already installed,
7 //   but may not not work (see below).
8 // - libexpat is a less common package, but light, and works fine.
9
10 // With standard installation of libxml2, C++ exception cannot be catched 
11 // during the parse process. This is required for normal use of yacs.
12 // libxml2 must be generated with configure --with-fexceptions ...
13 // (to be tested)
14 // Developpement and tests are done with libexpat.
15
16 //#define USE_LIBXML2
17 #define USE_EXPAT
18
19 // --- specific part for libxml2 ----------------------------------------------
20
21 #ifdef USE_LIBXML2
22 extern "C"
23 {
24 #include <libxml/parser.h>
25 }
26 #define XMLCALL
27 #define XML_Char char
28 inline XML_Char* tochar(const xmlChar *c) { return (XML_Char*)c; };
29 #endif
30
31 // --- specific part for expat ------------------------------------------------
32
33 #ifdef USE_EXPAT
34   #include <expat.h>
35 #define xmlChar XML_Char
36 inline const XML_Char* tochar(const xmlChar *c) { return c; };
37 #endif
38
39
40 // --- generic part -----------------------------------------------------------
41
42 #include <string>
43 #include <map>
44 #include <stack>
45 #include <list>
46
47 class xmlParserBase;
48
49 //! \brief base class for parse an xml file, use a dedicated parser, runtime independant.
50
51 class xmlReader
52 {
53 public:
54   xmlReader(xmlParserBase* parser);
55   virtual void parse(std::string xmlFile);
56 protected:
57   xmlParserBase* _rootParser;
58 };
59
60 //! \brief base class for xml parsers, runtime independant
61
62 class xmlParserBase
63 {
64 public:
65   static void XMLCALL start_document(void* userData);
66   static void XMLCALL end_document  (void* userData);
67   static void XMLCALL start_element (void* userData,
68                                      const xmlChar* name,
69                                      const xmlChar** p);
70   static void XMLCALL end_element   (void* userData,
71                                      const xmlChar* name);
72   static void XMLCALL characters    (void* userData,
73                                      const xmlChar* ch,
74                                      int len);
75   static void XMLCALL comment       (void* userData,
76                                      const xmlChar* value);
77   static void XMLCALL warning       (void* userData,
78                                      const char* fmt, ...);
79   static void XMLCALL error         (void* userData,
80                                      const char* fmt, ...);
81   static void XMLCALL fatal_error   (void* userData,
82                                      const char* fmt, ...);
83   static void XMLCALL cdata_block   (void* userData,
84                                      const xmlChar* value,
85                                      int len);
86   static void cleanGarbage();
87   static int getGarbageSize() {return _garbage.size(); };
88 public:
89   void setAttribute(std::string key, std::string value);
90   std::string getAttribute(std::string key);
91   virtual void addData(std::string value);
92   virtual void init (const xmlChar** p, xmlParserBase* father=0);
93
94   std::map< std::string, int > counts;
95
96 #ifdef USE_LIBXML2
97   static _xmlParserCtxt* _xmlParser;
98   static void XML_SetUserData(_xmlParserCtxt* ctxt,
99                               xmlParserBase* parser);
100 #endif
101
102 #ifdef USE_EXPAT
103   static XML_Parser _xmlParser;
104 #endif
105
106   static std::stack<xmlParserBase*> _stackParser;
107
108 protected:
109   void getAttributes(const xmlChar** p);
110
111   virtual void onStart  (const XML_Char* elem, const xmlChar** p);
112   virtual void onEnd    (const XML_Char* name);
113   virtual void charData (std::string data);
114   virtual void incrCount(const XML_Char *elem);
115   virtual void end();
116   virtual void stopParse(std::string what);
117
118 protected:
119   std::map<std::string, std::string> _mapAttrib;
120   static std::list<xmlParserBase*> _garbage;
121   std::string _data;
122   xmlParserBase *_father;
123 };
124
125 #endif