1 // Copyright (C) 2006-2012 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
20 #include "parserBase.hxx"
21 #include "Exception.hxx"
27 #include "YacsTrace.hxx"
29 YACS::ENGINE::Proc* currentProc;
31 std::stack<YACS::parser*> sp;
35 parser parser::main_parser;
45 DEBTRACE("Problem with parser: final stack level should be 0 and not " << _level);
49 std::stack<parser*>& parser::getStack()
54 void parser::SetUserDataAndPush(parser* pp)
56 XML_SetUserData(p,pp);
58 DEBTRACE("parser::SetUserDataAndPush, stack size: " << sp.size());
61 void XMLCALL parser::start(void *data, const XML_Char* el, const XML_Char** attr)
63 DEBTRACE("parser::start, stack size: " << sp.size());
64 parser* pp=static_cast <parser *> (data);
69 void parser::onEnd(const XML_Char *el,parser* child)
71 DEBTRACE("parser::onEnd: " << el)
74 void XMLCALL parser::end(void *data, const char *el)
76 DEBTRACE("parser::end: " << el);
77 parser* child=static_cast <parser *> (data);
79 DEBTRACE("parser::end, stack size: " << sp.size());
81 XML_SetUserData(p,pp);
86 void parser::charData(const XML_Char *s, int len)
88 _content=_content+std::string(s,len);
91 void XMLCALL parser::charac(void *data, const XML_Char *s, int len)
93 parser* pp=static_cast <parser *> (data);
97 void parser::endParser()
99 DEBTRACE("parser::endParser, level: " <<_level);
104 _counts=_stackCount.top();
105 _orderState=_stackOrder.top();
113 DEBTRACE("parser::init, level: " <<_level);
116 _stackCount.push(_counts);
117 _stackOrder.push(_orderState);
118 _counts=new std::map<std::string,int>;
125 void parser::incrCount(const XML_Char *el)
127 if(_counts->count(el)==0)
130 (*_counts)[el]=(*_counts)[el]+1;
133 void parser::checkOrder(std::string& el)
135 if(_orders.count(el)==0)return;
136 if(_orders[el] < _orderState)
138 std::string msg="unexpected "+el+" element (wrong order)";
139 throw YACS::Exception(msg);
141 else if(_orders[el] > _orderState)
143 _orderState=_orders[el];
147 void parser::maxcount(std::string name, int max, std::string& el)
150 if((*_counts)[name]>max)
152 std::stringstream msg;
153 msg <<"unexpected "+name+" element (count="<<(*_counts)[name];
154 msg <<" > maxOccurs=" << max << ")";
155 throw YACS::Exception(msg.str());
159 void parser::mincount(std::string name,int min )
161 if((*_counts)[name]<min)
163 std::stringstream msg;
164 msg<<"expected "+name+" element (count="<<(*_counts)[name];
165 msg << " < minOccurs=" << min << ")";
166 throw YACS::Exception(msg.str());
170 void parser::maxchoice(std::string *names, int max, std::string& el)
174 while (names[i]!= "")
176 ncount=ncount+(*_counts)[names[i]];
181 std::stringstream msg;
182 msg<<"unexpected "+el+" element (choice count="<<ncount<<" > maxOccurs=" << max << ")";
183 throw YACS::Exception(msg.str());
187 void parser::minchoice(std::string *names, int min)
191 while (names[i]!= "")
193 ncount=ncount+(*_counts)[names[i]];
198 std::stringstream msg;
199 msg << "expected element ";
201 while (names[i]!= "")
203 msg << names[i] << ",";
206 msg << "(choice count="<<ncount<<" < minOccurs=" << min << ")";
207 throw YACS::Exception(msg.str());
211 void parser::required(const std::string& name, const XML_Char** attr)
213 for (int i = 0; attr[i]; i += 2)
215 if(name == std::string(attr[i]))return;
217 throw YACS::Exception("Attribute: "+name+" is required");
220 void parser::buildAttr(const XML_Char** attr)
222 for (int i = 0; attr[i]; i += 2)
224 DEBTRACE(attr[i] << "=" << attr[i + 1]);
228 void parser::onStart(const XML_Char* el, const XML_Char** attr)
230 DEBTRACE( "parser::onStart: " << el );
231 SetUserDataAndPush(&main_parser);
234 main_parser.buildAttr(attr);
237 void parser::logError(const std::string& reason)
239 DEBTRACE( "parser::logError: " << _file );
240 currentProc->getLogger("parser")->error(reason,main_parser._file.c_str(),XML_GetCurrentLineNumber(p));