1 #ifndef __XMLPARSERBASE_HXX_
2 #define __XMLPARSERBASE_HXX_
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.
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 ...
14 // Developpement and tests are done with libexpat.
19 // --- specific part for libxml2 ----------------------------------------------
24 #include <libxml/parser.h>
28 inline XML_Char* tochar(const xmlChar *c) { return (XML_Char*)c; };
31 // --- specific part for expat ------------------------------------------------
35 #define xmlChar XML_Char
36 inline const XML_Char* tochar(const xmlChar *c) { return c; };
40 // --- generic part -----------------------------------------------------------
49 //! \brief base class for parse an xml file, use a dedicated parser, runtime independant.
54 xmlReader(xmlParserBase* parser);
55 virtual void parse(std::string xmlFile);
57 xmlParserBase* _rootParser;
60 //! \brief base class for xml parsers, runtime independant
65 static void XMLCALL start_document(void* userData);
66 static void XMLCALL end_document (void* userData);
67 static void XMLCALL start_element (void* userData,
70 static void XMLCALL end_element (void* userData,
72 static void XMLCALL characters (void* userData,
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,
86 static void cleanGarbage();
87 static int getGarbageSize() {return _garbage.size(); };
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);
94 std::map< std::string, int > counts;
97 static _xmlParserCtxt* _xmlParser;
98 static void XML_SetUserData(_xmlParserCtxt* ctxt,
99 xmlParserBase* parser);
103 static XML_Parser _xmlParser;
106 static std::stack<xmlParserBase*> _stackParser;
109 void getAttributes(const xmlChar** p);
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);
116 virtual void stopParse(std::string what);
119 std::map<std::string, std::string> _mapAttrib;
120 static std::list<xmlParserBase*> _garbage;
122 xmlParserBase *_father;