Version: 8.3.0
parserBase.cxx
Go to the documentation of this file.
1 // Copyright (C) 2006-2016 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 #include "parserBase.hxx"
21 #include "Exception.hxx"
22 #include "Proc.hxx"
23 #include "Logger.hxx"
24 #include <sstream>
25 
26 //#define _DEVDEBUG_
27 #include "YacsTrace.hxx"
28 #include <libxml/parserInternals.h>
29 
31 _xmlParserCtxt* saxContext;
32 
33 namespace YACS
34 {
35  parser parser::main_parser;
36  std::stack<parser*> parser::_stackParser;
37 
39 {
40  if(_level==0)
41  {
42  delete _counts;
43  }
44  else
45  {
46  DEBTRACE("Problem with parser: final stack level should be 0 and not " << _level);
47  }
48 }
49 
50 std::stack<parser*>& parser::getStack()
51 {
52  return _stackParser;
53 }
54 
56 {
58  _stackParser.push(pp);
59  DEBTRACE("parser::SetUserDataAndPush, stack size: " << sp.size());
60 }
61 
62 void parser::onEnd(const XML_Char *el, parser* child)
63 {
64  DEBTRACE("parser::onEnd: " << el)
65 }
66 
67 void parser::charData(const XML_Char *s, int len)
68 {
69  _content=_content+std::string(s,len);
70 }
71 
73 {
74  DEBTRACE("parser::endParser, level: " <<_level);
75  _level=_level-1;
76  if(_level>0)
77  {
78  delete _counts;
79  _counts=_stackCount.top();
81  _stackCount.pop();
82  _stackOrder.pop();
83  }
84 }
85 
86 void parser::init ()
87 {
88  DEBTRACE("parser::init, level: " <<_level);
89  if(_level>0)
90  {
91  _stackCount.push(_counts);
93  _counts=new std::map<std::string,int>;
94  }
95  _level=_level+1;
96  _counts->clear();
97  _orderState=0;
98 }
99 
101 {
102  if((*_counts).find(el)==(*_counts).end())
103  (*_counts)[el]=1;
104  else
105  (*_counts)[el]=(*_counts)[el]+1;
106 }
107 
108 void parser::checkOrder(std::string& el)
109 {
110  if(_orders.count(el)==0)return;
111  if(_orders[el] < _orderState)
112  {
113  std::string msg="unexpected "+el+" element (wrong order)";
114  throw YACS::Exception(msg);
115  }
116  else if(_orders[el] > _orderState)
117  {
118  _orderState=_orders[el];
119  }
120 }
121 
122 void parser::maxcount(std::string name, int max, std::string& el)
123 {
124  if(el!=name)return;
125  if((*_counts)[name]>max)
126  {
127  std::stringstream msg;
128  msg <<"unexpected "+name+" element (count="<<(*_counts)[name];
129  msg <<" > maxOccurs=" << max << ")";
130  throw YACS::Exception(msg.str());
131  }
132 }
133 
134 void parser::mincount(std::string name,int min )
135 {
136  if((*_counts)[name]<min)
137  {
138  std::stringstream msg;
139  msg<<"expected "+name+" element (count="<<(*_counts)[name];
140  msg << " < minOccurs=" << min << ")";
141  throw YACS::Exception(msg.str());
142  }
143 }
144 
145 void parser::maxchoice(std::string *names, int max, std::string& el)
146 {
147  int i=0;
148  int ncount=0;
149  while (names[i]!= "")
150  {
151  ncount=ncount+(*_counts)[names[i]];
152  ++i;
153  }
154  if(ncount>max)
155  {
156  std::stringstream msg;
157  msg<<"unexpected "+el+" element (choice count="<<ncount<<" > maxOccurs=" << max << ")";
158  throw YACS::Exception(msg.str());
159  }
160 }
161 
162 void parser::minchoice(std::string *names, int min)
163 {
164  int i=0;
165  int ncount=0;
166  while (names[i]!= "")
167  {
168  ncount=ncount+(*_counts)[names[i]];
169  ++i;
170  }
171  if(ncount<min)
172  {
173  std::stringstream msg;
174  msg << "expected element ";
175  i=0;
176  while (names[i]!= "")
177  {
178  msg << names[i] << ",";
179  ++i;
180  }
181  msg << "(choice count="<<ncount<<" < minOccurs=" << min << ")";
182  throw YACS::Exception(msg.str());
183  }
184 }
185 
186 void parser::required(const std::string& name, const XML_Char** attr)
187 {
188  for (int i = 0; attr[i]; i += 2)
189  {
190  if(name == std::string(attr[i]))return;
191  }
192  throw YACS::Exception("Attribute: "+name+" is required");
193 }
194 
195 void parser::buildAttr(const XML_Char** attr)
196 {
197  if (!attr)
198  return;
199  for (int i = 0; attr[i]; i += 2)
200  {
201  DEBTRACE(attr[i] << "=" << attr[i + 1]);
202  }
203 }
204 
205 void parser::onStart(const XML_Char* el, const XML_Char** attr)
206 {
207  DEBTRACE( "parser::onStart: " << el );
209  main_parser.init();
210  main_parser.pre();
211  main_parser.buildAttr(attr);
212 }
213 
214 void parser::logError(const std::string& reason)
215 {
216  DEBTRACE( "parser::logError: " << _file );
217  currentProc->getLogger("parser")->error(reason,main_parser._file.c_str(),saxContext->input->line);
218 }
219 void parser::XML_SetUserData(_xmlParserCtxt* ctxt,
220  parser* par)
221 {
222  ctxt->userData = par;
223 }
224 
226 {
227  DEBTRACE("parser::start_document");
228  parser *currentParser = static_cast<parser *> (data);
229 }
230 
232 {
233  DEBTRACE("parser::end_document");
234  parser *currentParser = static_cast<parser *> (data);
235 }
236 
238  const xmlChar* name,
239  const xmlChar** p)
240 {
241  DEBTRACE("parser::start_element " << name);
242  parser *currentParser = static_cast<parser *> (data);
243  currentParser->incrCount((const XML_Char *)name);
244  currentParser->onStart((const XML_Char *)name, (const XML_Char **)p);
245 }
246 
247 void XMLCALL parser::end_element(void* data,
248  const xmlChar* name)
249 {
250  DEBTRACE("parser::end_element");
251  parser *childParser = static_cast<parser *> (data);
252  _stackParser.pop();
253  parser* pp=_stackParser.top();
255  pp->onEnd((const XML_Char *)name, childParser);
256  childParser->endParser();
257  }
258 
259 void XMLCALL parser::characters(void* data,
260  const xmlChar* ch,
261  int len)
262 {
263  DEBTRACE("parser::characters " << len);
264  parser *currentParser = (parser *) (data);
265  currentParser->charData((const XML_Char*) ch, len);
266 }
267 
268 void XMLCALL parser::comment(void* data,
269  const xmlChar* value)
270 {
271  DEBTRACE("parser::comment");
272  parser *currentParser = static_cast<parser *> (data);
273 }
274 
275 void XMLCALL parser::cdata_block(void* data,
276  const xmlChar* value,
277  int len)
278 {
279  DEBTRACE("parser::cdata_block");
280  parser *currentParser = static_cast<parser *> (data);
281  currentParser->charData((const XML_Char*) value, len);
282 }
283 
284 void XMLCALL parser::warning(void* data,
285  const char* fmt, ...)
286 {
287  DEBTRACE("parser::warning");
288  parser *currentParser = static_cast<parser *> (data);
289  va_list args;
290  va_start(args, fmt);
291  std::string format = "%s";
292  if (format == fmt)
293  {
294  char* parv;
295  parv = va_arg(args, char*);
296  std::cerr << parv ;
297  }
298  else std::cerr << __FILE__ << " [" << __LINE__ << "] : "
299  << "error format not taken into account: " << fmt << std::endl;
300  va_end(args);
301 }
302 
303 void XMLCALL parser::error(void* data,
304  const char* fmt, ...)
305 {
306  DEBTRACE("parser::error");
307  parser *currentParser = static_cast<parser *> (data);
308  va_list args;
309  va_start(args, fmt);
310  std::string format = "%s";
311  if (format == fmt)
312  {
313  char* parv;
314  parv = va_arg(args, char*);
315  std::cerr << parv ;
316  }
317  else std::cerr << __FILE__ << " [" << __LINE__ << "] : "
318  << "error format not taken into account: " << fmt << std::endl;
319  va_end(args);
320 }
321 
322 void XMLCALL parser::fatal_error(void* data,
323  const char* fmt, ...)
324 {
325  DEBTRACE("parser::fatal_error");
326  parser *currentParser = static_cast<parser *> (data);
327  va_list args;
328  va_start(args, fmt);
329  std::string format = "%s";
330  if (format == fmt)
331  {
332  char* parv;
333  parv = va_arg(args, char*);
334  std::cerr << parv ;
335  }
336  else std::cerr << __FILE__ << " [" << __LINE__ << "] : "
337  << "error format not taken into account: " << fmt << std::endl;
338  va_end(args);
339 }
340 
341 }