1 // Copyright (C) 2006-2008 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #ifndef __XMLPARSERBASE_HXX_
20 #define __XMLPARSERBASE_HXX_
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.
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 ...
32 // Developpement and tests are done with libexpat.
37 // --- specific part for libxml2 ----------------------------------------------
42 #include <libxml/parser.h>
46 inline XML_Char* tochar(const xmlChar *c) { return (XML_Char*)c; };
49 // --- specific part for expat ------------------------------------------------
53 #define xmlChar XML_Char
54 inline const XML_Char* tochar(const xmlChar *c) { return c; };
58 // --- generic part -----------------------------------------------------------
67 //! \brief base class for parse an xml file, use a dedicated parser, runtime independant.
72 xmlReader(xmlParserBase* parser);
73 virtual void parse(std::string xmlFile);
75 xmlParserBase* _rootParser;
78 //! \brief base class for xml parsers, runtime independant
84 static void XMLCALL start_document(void* userData);
85 static void XMLCALL end_document (void* userData);
86 static void XMLCALL start_element (void* userData,
89 static void XMLCALL end_element (void* userData,
91 static void XMLCALL characters (void* userData,
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,
106 static void cleanGarbage();
107 static int getGarbageSize() {return _garbage.size(); };
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);
114 std::map< std::string, int > counts;
117 static _xmlParserCtxt* _xmlParser;
118 static void XML_SetUserData(_xmlParserCtxt* ctxt,
119 xmlParserBase* parser);
123 static XML_Parser _xmlParser;
126 static std::stack<xmlParserBase*> _stackParser;
129 void getAttributes(const xmlChar** p);
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);
136 virtual void stopParse(std::string what);
139 std::map<std::string, std::string> _mapAttrib;
140 static std::list<xmlParserBase*> _garbage;
142 xmlParserBase *_father;