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