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