Salome HOME
c21ac84f57de7a7d9530158186d547b05375e52a
[modules/yacs.git] / src / yacsloader / parserBase.hxx
1 // Copyright (C) 2006-2024  CEA, EDF
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 _PARSERBASE_HXX_
21 #define _PARSERBASE_HXX_
22
23 #include "YACSloaderExport.hxx"
24
25 #include <stack>
26 #include <map>
27 #include <string>
28 #include <iostream>
29
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
32 #include <libxml/xmlreader.h>
33
34 namespace YACS
35 {
36
37 typedef char XML_Char;
38
39 struct YACSLOADER_EXPORT parser
40 {
41   static parser main_parser;
42   static std::stack<parser*> _stackParser;
43
44   parser():_level(0),_defaultParsersMap(0)
45   {
46     _counts=new std::map<std::string,int>;
47   }
48   virtual ~parser();
49   virtual void SetUserDataAndPush(parser* pp);
50   virtual void onStart(const XML_Char *el, const XML_Char** attr);
51   virtual void onEnd(const XML_Char *el, parser* child);
52   virtual void charData(const XML_Char *s, int len);
53   virtual void endParser();
54   virtual void init();
55   virtual void incrCount(const XML_Char *elem);
56   virtual void checkOrder(std::string& el);
57   virtual void maxcount(std::string name, int max, std::string& el);
58   virtual void mincount(std::string name,int min );
59   virtual void maxchoice(std::string *names, int max, std::string& el);
60   virtual void minchoice(std::string *names, int min);
61   virtual void pre(){_content="";};
62   virtual void required(const std::string& name, const XML_Char** attr);
63   virtual void buildAttr(const XML_Char** attr);
64
65   static void XMLCALL start_document(void* data);
66   static void XMLCALL end_document  (void* data);
67   static void XMLCALL start_element (void* data,
68                                      const xmlChar* name,
69                                      const xmlChar** p);
70   static void XMLCALL end_element   (void* data,
71                                      const xmlChar* name);
72   static void XMLCALL characters    (void* data,
73                                      const xmlChar* ch,
74                                      int len);
75   static void XMLCALL comment       (void* data,
76                                      const xmlChar* value);
77   static void XMLCALL cdata_block   (void* data,
78                                      const xmlChar* value,
79                                      int len);
80   static void XMLCALL warning       (void* data,
81                                      const char* fmt, ...);
82   static void XMLCALL error         (void* data,
83                                      const char* fmt, ...);
84   static void XMLCALL fatal_error   (void* data,
85                                      const char* fmt, ...);
86
87   static void XML_SetUserData(_xmlParserCtxt* ctxt, parser* par);
88
89   std::stack<parser*>& getStack();
90
91   template<class T>
92   T post() { }
93
94   void logError(const std::string& reason);
95
96   std::string _file;
97   std::string _content;
98   std::map<std::string,int> *_counts;
99   std::map<std::string,int> _orders;
100   int _orderState;
101   int _level;
102   std::stack<std::map<std::string,int>*> _stackCount;
103   std::stack<int> _stackOrder;
104   // OCC: san -- Allow external parsers for handling of unknown elements
105   // and attributes. This capability is used by YACS GUI to read
106   // graph presentation data
107   std::map<std::string,parser*> *_defaultParsersMap;
108 };
109
110 }
111 #endif