2 #include "parserBase.hxx"
3 #include "Exception.hxx"
9 #include "YacsTrace.hxx"
11 YACS::ENGINE::Proc* currentProc;
13 std::stack<YACS::parser*> sp;
17 parser parser::main_parser;
27 DEBTRACE("Problem with parser: final stack level should be 0 and not " << _level);
31 std::stack<parser*>& parser::getStack()
36 void parser::SetUserDataAndPush(parser* pp)
38 XML_SetUserData(p,pp);
40 DEBTRACE("parser::SetUserDataAndPush, stack size: " << sp.size());
43 void XMLCALL parser::start(void *data, const XML_Char* el, const XML_Char** attr)
45 DEBTRACE("parser::start, stack size: " << sp.size());
46 parser* pp=static_cast <parser *> (data);
51 void parser::onEnd(const XML_Char *el,parser* child)
53 DEBTRACE("parser::onEnd: " << el)
56 void XMLCALL parser::end(void *data, const char *el)
58 DEBTRACE("parser::end: " << el);
59 parser* child=static_cast <parser *> (data);
61 DEBTRACE("parser::end, stack size: " << sp.size());
63 XML_SetUserData(p,pp);
68 void parser::charData(const XML_Char *s, int len)
70 _content=_content+std::string(s,len);
73 void XMLCALL parser::charac(void *data, const XML_Char *s, int len)
75 parser* pp=static_cast <parser *> (data);
79 void parser::endParser()
81 DEBTRACE("parser::endParser, level: " <<_level);
86 _counts=_stackCount.top();
87 _orderState=_stackOrder.top();
95 DEBTRACE("parser::init, level: " <<_level);
98 _stackCount.push(_counts);
99 _stackOrder.push(_orderState);
100 _counts=new std::map<std::string,int>;
107 void parser::incrCount(const XML_Char *el)
109 if(_counts->count(el)==0)
112 (*_counts)[el]=(*_counts)[el]+1;
115 void parser::checkOrder(std::string& el)
117 if(_orders.count(el)==0)return;
118 if(_orders[el] < _orderState)
120 std::string msg="unexpected "+el+" element (wrong order)";
121 throw YACS::Exception::Exception(msg);
123 else if(_orders[el] > _orderState)
125 _orderState=_orders[el];
129 void parser::maxcount(std::string name, int max, std::string& el)
132 if((*_counts)[name]>max)
134 std::stringstream msg;
135 msg <<"unexpected "+name+" element (count="<<(*_counts)[name];
136 msg <<" > maxOccurs=" << max << ")";
137 throw YACS::Exception::Exception(msg.str());
141 void parser::mincount(std::string name,int min )
143 if((*_counts)[name]<min)
145 std::stringstream msg;
146 msg<<"expected "+name+" element (count="<<(*_counts)[name];
147 msg << " < minOccurs=" << min << ")";
148 throw YACS::Exception::Exception(msg.str());
152 void parser::maxchoice(std::string *names, int max, std::string& el)
156 while (names[i]!= "")
158 ncount=ncount+(*_counts)[names[i]];
163 std::stringstream msg;
164 msg<<"unexpected "+el+" element (choice count="<<ncount<<" > maxOccurs=" << max << ")";
165 throw YACS::Exception::Exception(msg.str());
169 void parser::minchoice(std::string *names, int min)
173 while (names[i]!= "")
175 ncount=ncount+(*_counts)[names[i]];
180 std::stringstream msg;
181 msg << "expected element ";
183 while (names[i]!= "")
185 msg << names[i] << ",";
188 msg << "(choice count="<<ncount<<" < minOccurs=" << min << ")";
189 throw YACS::Exception::Exception(msg.str());
193 void parser::required(const std::string& name, const XML_Char** attr)
195 for (int i = 0; attr[i]; i += 2)
197 if(name == std::string(attr[i]))return;
199 throw YACS::Exception::Exception("Attribute: "+name+" is required");
202 void parser::buildAttr(const XML_Char** attr)
204 for (int i = 0; attr[i]; i += 2)
206 DEBTRACE(attr[i] << "=" << attr[i + 1]);
210 void parser::onStart(const XML_Char* el, const XML_Char** attr)
212 DEBTRACE( "parser::onStart: " << el );
213 SetUserDataAndPush(&main_parser);
216 main_parser.buildAttr(attr);
219 void parser::logError(const std::string& reason)
221 DEBTRACE( "parser::logError: " << _file );
222 currentProc->getLogger("parser")->error(reason,main_parser._file.c_str(),XML_GetCurrentLineNumber(p));