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 #include "parserBase.hxx"
20 #include "Exception.hxx"
26 #include "YacsTrace.hxx"
28 YACS::ENGINE::Proc* currentProc;
30 std::stack<YACS::parser*> sp;
34 parser parser::main_parser;
44 DEBTRACE("Problem with parser: final stack level should be 0 and not " << _level);
48 std::stack<parser*>& parser::getStack()
53 void parser::SetUserDataAndPush(parser* pp)
55 XML_SetUserData(p,pp);
57 DEBTRACE("parser::SetUserDataAndPush, stack size: " << sp.size());
60 void XMLCALL parser::start(void *data, const XML_Char* el, const XML_Char** attr)
62 DEBTRACE("parser::start, stack size: " << sp.size());
63 parser* pp=static_cast <parser *> (data);
68 void parser::onEnd(const XML_Char *el,parser* child)
70 DEBTRACE("parser::onEnd: " << el)
73 void XMLCALL parser::end(void *data, const char *el)
75 DEBTRACE("parser::end: " << el);
76 parser* child=static_cast <parser *> (data);
78 DEBTRACE("parser::end, stack size: " << sp.size());
80 XML_SetUserData(p,pp);
85 void parser::charData(const XML_Char *s, int len)
87 _content=_content+std::string(s,len);
90 void XMLCALL parser::charac(void *data, const XML_Char *s, int len)
92 parser* pp=static_cast <parser *> (data);
96 void parser::endParser()
98 DEBTRACE("parser::endParser, level: " <<_level);
103 _counts=_stackCount.top();
104 _orderState=_stackOrder.top();
112 DEBTRACE("parser::init, level: " <<_level);
115 _stackCount.push(_counts);
116 _stackOrder.push(_orderState);
117 _counts=new std::map<std::string,int>;
124 void parser::incrCount(const XML_Char *el)
126 if(_counts->count(el)==0)
129 (*_counts)[el]=(*_counts)[el]+1;
132 void parser::checkOrder(std::string& el)
134 if(_orders.count(el)==0)return;
135 if(_orders[el] < _orderState)
137 std::string msg="unexpected "+el+" element (wrong order)";
138 throw YACS::Exception::Exception(msg);
140 else if(_orders[el] > _orderState)
142 _orderState=_orders[el];
146 void parser::maxcount(std::string name, int max, std::string& el)
149 if((*_counts)[name]>max)
151 std::stringstream msg;
152 msg <<"unexpected "+name+" element (count="<<(*_counts)[name];
153 msg <<" > maxOccurs=" << max << ")";
154 throw YACS::Exception::Exception(msg.str());
158 void parser::mincount(std::string name,int min )
160 if((*_counts)[name]<min)
162 std::stringstream msg;
163 msg<<"expected "+name+" element (count="<<(*_counts)[name];
164 msg << " < minOccurs=" << min << ")";
165 throw YACS::Exception::Exception(msg.str());
169 void parser::maxchoice(std::string *names, int max, std::string& el)
173 while (names[i]!= "")
175 ncount=ncount+(*_counts)[names[i]];
180 std::stringstream msg;
181 msg<<"unexpected "+el+" element (choice count="<<ncount<<" > maxOccurs=" << max << ")";
182 throw YACS::Exception::Exception(msg.str());
186 void parser::minchoice(std::string *names, int min)
190 while (names[i]!= "")
192 ncount=ncount+(*_counts)[names[i]];
197 std::stringstream msg;
198 msg << "expected element ";
200 while (names[i]!= "")
202 msg << names[i] << ",";
205 msg << "(choice count="<<ncount<<" < minOccurs=" << min << ")";
206 throw YACS::Exception::Exception(msg.str());
210 void parser::required(const std::string& name, const XML_Char** attr)
212 for (int i = 0; attr[i]; i += 2)
214 if(name == std::string(attr[i]))return;
216 throw YACS::Exception::Exception("Attribute: "+name+" is required");
219 void parser::buildAttr(const XML_Char** attr)
221 for (int i = 0; attr[i]; i += 2)
223 DEBTRACE(attr[i] << "=" << attr[i + 1]);
227 void parser::onStart(const XML_Char* el, const XML_Char** attr)
229 DEBTRACE( "parser::onStart: " << el );
230 SetUserDataAndPush(&main_parser);
233 main_parser.buildAttr(attr);
236 void parser::logError(const std::string& reason)
238 DEBTRACE( "parser::logError: " << _file );
239 currentProc->getLogger("parser")->error(reason,main_parser._file.c_str(),XML_GetCurrentLineNumber(p));