00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef VCSN_XML_HANDLERS_HXX
00019 # define VCSN_XML_HANDLERS_HXX
00020
00021 # include <xercesc/util/XMLString.hpp>
00022
00023 # include <vaucanson/xml/tools.hh>
00024
00025 namespace vcsn
00026 {
00027 namespace xml
00028 {
00029
00030
00031
00032 template <typename T>
00033 DocHandler<T>::DocHandler (xercesc::SAX2XMLReader* parser,
00034 xercesc::DefaultHandler& root,
00035 T& aut,
00036 XMLEq& eq)
00037 : Handler(parser, root, eq),
00038 aut_(aut),
00039 auth_(parser, *this, aut)
00040 {
00041 }
00042
00043 template <typename T>
00044 void
00045 DocHandler<T>::start (const XMLCh* const,
00046 const XMLCh* const localname,
00047 const XMLCh* const,
00048 const xercesc::Attributes&)
00049 {
00050 using namespace xercesc;
00051 if (XMLString::equals(eq_.fsmxml, localname))
00052 {
00053
00054
00055 parser_->setContentHandler(&auth_);
00056 }
00057 else
00058 error::token(localname);
00059 }
00060
00061 template <typename T>
00062 void
00063 DocHandler<T>::end (const XMLCh* const,
00064 const XMLCh* const,
00065 const XMLCh* const)
00066 {
00067 }
00068
00072 template <typename T>
00073 AutHandler<T>::AutHandler (xercesc::SAX2XMLReader* parser,
00074 Handler& root,
00075 T& aut)
00076 : Handler(parser, root),
00077 aut_(aut),
00078 typeh_(parser, *this, aut),
00079 contenth_(parser, *this, aut),
00080 unsuph_(parser, *this)
00081 {
00082 }
00083
00084 template <typename T>
00085 void
00086 AutHandler<T>::start (const XMLCh* const uri,
00087 const XMLCh* const localname,
00088 const XMLCh* const,
00089 const xercesc::Attributes& attrs)
00090 {
00091 using namespace xercesc;
00092 if (XMLString::equals(eq_.automaton, localname))
00093 {
00094 const XMLCh* autname = tools::get_attribute(attrs, "name", uri);
00095 if (autname != 0)
00096 aut_.geometry().name() = xmlstr(autname);
00097
00098 }
00099 else if (XMLString::equals(eq_.drawing, localname))
00100 parser_->setContentHandler(&unsuph_);
00101 else if (XMLString::equals(eq_.geometry, localname))
00102 parser_->setContentHandler(&unsuph_);
00103 else if (XMLString::equals(eq_.valueType, localname))
00104 parser_->setContentHandler(&typeh_);
00105 else if (XMLString::equals(eq_.content, localname))
00106 parser_->setContentHandler(&contenth_);
00107 else
00108 error::token(localname);
00109 }
00110
00111 template <typename T>
00112 void
00113 AutHandler<T>::end (const XMLCh* const,
00114 const XMLCh* const localname,
00115 const XMLCh* const)
00116 {
00117 using namespace xercesc;
00118 if (XMLString::equals(eq_.automaton, localname))
00119 parser_->setContentHandler(&root_);
00120 }
00121
00125 template <typename T>
00126 TypeHandler<T>::TypeHandler (xercesc::SAX2XMLReader* parser,
00127 Handler& root,
00128 T& param)
00129 : Handler(parser, root),
00130 param_(param)
00131 {
00132 }
00133
00134 template <typename T>
00135 void
00136 TypeHandler<T>::start (const XMLCh* const,
00137 const XMLCh* const localname,
00138 const XMLCh* const,
00139 const xercesc::Attributes& attrs)
00140 {
00141 using namespace xercesc;
00142 if (XMLString::equals(eq_.semiring, localname))
00143 {
00144 semiring_ = builders::create_semiring(param_, localname, attrs);
00145 semiringh_ = builders::create_semiringh(*semiring_, attrs, parser_, *this);
00146 parser_->setContentHandler(semiringh_);
00147 }
00148 else if (XMLString::equals(eq_.monoid, localname))
00149 {
00150 monoid_ = builders::create_monoid(param_, localname, attrs);
00151 monoidh_ = builders::create_monoidh(*monoid_, attrs, parser_, *this);
00152 parser_->setContentHandler(monoidh_);
00153 }
00154 else
00155 error::token(localname);
00156 }
00157
00158 template <typename T>
00159 void
00160 TypeHandler<T>::end (const XMLCh* const,
00161 const XMLCh* const localname,
00162 const XMLCh* const)
00163 {
00164 using namespace xercesc;
00165 typename T::series_set_t series(*semiring_, *monoid_);
00166 param_.attach(series);
00167
00168 if (XMLString::equals(eq_.valueType, localname))
00169 parser_->setContentHandler(&root_);
00170 delete monoid_;
00171 delete monoidh_;
00172 delete semiring_;
00173 delete semiringh_;
00174 }
00175
00179 template <typename T>
00180 ContHandler<T>::ContHandler (xercesc::SAX2XMLReader* parser,
00181 Handler& root,
00182 T& aut)
00183 : Handler(parser, root),
00184 aut_(aut),
00185 statesh_(parser, *this, aut, map_),
00186 transitionsh_(parser, *this, aut, map_)
00187 {
00188 }
00189
00190 template <typename T>
00191 void
00192 ContHandler<T>::start (const XMLCh* const,
00193 const XMLCh* const localname,
00194 const XMLCh* const,
00195 const xercesc::Attributes&)
00196 {
00197 using namespace xercesc;
00198 if (XMLString::equals(eq_.states, localname))
00199 parser_->setContentHandler(&statesh_);
00200 else if (XMLString::equals(eq_.transitions, localname))
00201 parser_->setContentHandler(&transitionsh_);
00202 else
00203 error::token(localname);
00204 }
00205
00206 template <typename T>
00207 void
00208 ContHandler<T>::end (const XMLCh* const,
00209 const XMLCh* const localname,
00210 const XMLCh* const)
00211 {
00212 using namespace xercesc;
00213 if (XMLString::equals(eq_.content, localname))
00214 parser_->setContentHandler(&root_);
00215 else
00216 error::token(localname);
00217 }
00218
00222 template <typename T>
00223 StatesHandler<T>::StatesHandler (xercesc::SAX2XMLReader* parser,
00224 Handler& root,
00225 T& aut,
00226 map_t& map)
00227 : Handler(parser, root),
00228 aut_(aut),
00229 map_(map),
00230 stateh_(0)
00231 {
00232 }
00233
00234 template <typename T>
00235 void
00236 StatesHandler<T>::start (const XMLCh* const,
00237 const XMLCh* const localname,
00238 const XMLCh* const,
00239 const xercesc::Attributes& attrs)
00240 {
00241 using namespace xercesc;
00242 if (XMLString::equals(eq_.state, localname))
00243 {
00244 hstate_t state = aut_.add_state();
00245 map_[xmlstr(tools::get_attribute(attrs, "id"))] = state;
00246 if (stateh_)
00247 delete stateh_;
00248 stateh_ = new StateHandler<T>(parser_, *this, aut_, state);
00249 parser_->setContentHandler(stateh_);
00250 }
00251 else
00252 error::token(localname);
00253 }
00254
00255 template <typename T>
00256 void
00257 StatesHandler<T>::end (const XMLCh* const,
00258 const XMLCh* const localname,
00259 const XMLCh* const)
00260 {
00261 using namespace xercesc;
00262 if (XMLString::equals(eq_.states, localname))
00263 parser_->setContentHandler(&root_);
00264 else
00265 error::token(localname);
00266 if (stateh_)
00267 delete stateh_;
00268 }
00269
00273 template <typename T>
00274 StateHandler<T>::StateHandler (xercesc::SAX2XMLReader* parser,
00275 Handler& root,
00276 T& aut,
00277 hstate_t state)
00278 : Handler(parser, root),
00279 aut_(aut),
00280 state_(state),
00281 geometryh_(parser, *this, aut, state),
00282 unsuph_(parser, *this)
00283 {
00284 }
00285
00286 template <typename T>
00287 void
00288 StateHandler<T>::start (const XMLCh* const,
00289 const XMLCh* const localname,
00290 const XMLCh* const,
00291 const xercesc::Attributes& attrs)
00292 {
00293 using namespace xercesc;
00294 if (XMLString::equals(eq_.drawing, localname))
00295 parser_->setContentHandler(&unsuph_);
00296 else if (XMLString::equals(eq_.geometry, localname))
00297 {
00298 typedef typename T::geometry_t::states_geometry_map_t gmap_t;
00299
00300 double y = 0;
00301 double x = 0;
00302 if (tools::has_attribute(attrs, "x"))
00303 {
00304 std::istringstream xstr(xmlstr(tools::get_attribute(attrs, "x")));
00305 xstr >> x;
00306 }
00307 if (tools::has_attribute(attrs, "y"))
00308 {
00309 std::istringstream ystr(xmlstr(tools::get_attribute(attrs, "y")));
00310 ystr >> y;
00311 }
00312 gmap_t& map = aut_.geometry().states();
00313 map[state_] = std::make_pair(x,y);
00314 parser_->setContentHandler(&geometryh_);
00315 }
00316 else
00317 error::token(localname);
00318 }
00319
00320 template <typename T>
00321 void
00322 StateHandler<T>::end (const XMLCh* const,
00323 const XMLCh* const localname,
00324 const XMLCh* const)
00325 {
00326 using namespace xercesc;
00327 if (XMLString::equals(eq_.state, localname))
00328 parser_->setContentHandler(&root_);
00329 else
00330 error::token(localname);
00331 }
00332
00336 template <typename T>
00337 TransitionsHandler<T>::TransitionsHandler (xercesc::SAX2XMLReader* parser,
00338 Handler& root,
00339 T& aut,
00340 map_t& map)
00341 : Handler(parser, root),
00342 aut_(aut),
00343 map_(map),
00344 transitionh_(0)
00345 {
00346 }
00347
00348 template <typename T>
00349 void
00350 TransitionsHandler<T>::start (const XMLCh* const,
00351 const XMLCh* const localname,
00352 const XMLCh* const,
00353 const xercesc::Attributes& attrs)
00354 {
00355 using namespace xercesc;
00356 if (XMLString::equals(eq_.transition, localname))
00357 {
00358 hstate_t src = map_[xmlstr(tools::get_attribute(attrs, "src"))];
00359 hstate_t dst = map_[xmlstr(tools::get_attribute(attrs, "target"))];
00360 if (transitionh_)
00361 delete transitionh_;
00362 transitionh_ = new TransitionHandler<T>(parser_, *this, aut_, src, dst);
00363 parser_->setContentHandler(transitionh_);
00364 }
00365 else if (XMLString::equals(eq_.final, localname))
00366 {
00367 hstate_t state = map_[xmlstr(tools::get_attribute(attrs, "state"))];
00368 if (transitionh_)
00369 delete transitionh_;
00370 transitionh_ = new InitFinalHandler<T>(parser_, *this, aut_, state, false);
00371 parser_->setContentHandler(transitionh_);
00372 }
00373 else if (XMLString::equals(eq_.initial, localname))
00374 {
00375 hstate_t state = map_[xmlstr(tools::get_attribute(attrs, "state"))];
00376 if (transitionh_)
00377 delete transitionh_;
00378 transitionh_ = new InitFinalHandler<T>(parser_, *this, aut_, state, true);
00379 parser_->setContentHandler(transitionh_);
00380 }
00381 else
00382 error::token(localname);
00383 }
00384
00385 template <typename T>
00386 void
00387 TransitionsHandler<T>::end (const XMLCh* const,
00388 const XMLCh* const localname,
00389 const XMLCh* const)
00390 {
00391 using namespace xercesc;
00392 if (XMLString::equals(eq_.transitions, localname))
00393 parser_->setContentHandler(&root_);
00394 else
00395 error::token(localname);
00396 if (transitionh_)
00397 delete transitionh_;
00398 }
00399
00403 template <typename T>
00404 TransitionHandler<T>::TransitionHandler (xercesc::SAX2XMLReader* parser,
00405 Handler& root,
00406 T& aut,
00407 hstate_t src,
00408 hstate_t dst)
00409 : Handler(parser, root),
00410 aut_(aut),
00411 src_(src),
00412 dst_(dst),
00413 s_(aut.structure().series()),
00414 labelh_(parser, *this, s_, eq_.label),
00415 unsuph_(parser, *this)
00416 {
00417 }
00418
00419 template <typename T>
00420 void
00421 TransitionHandler<T>::start (const XMLCh* const,
00422 const XMLCh* const localname,
00423 const XMLCh* const,
00424 const xercesc::Attributes&)
00425 {
00426 using namespace xercesc;
00427 if (XMLString::equals(eq_.drawing, localname))
00428 parser_->setContentHandler(&unsuph_);
00429 else if (XMLString::equals(eq_.geometry, localname))
00430 parser_->setContentHandler(&unsuph_);
00431 else if (XMLString::equals(eq_.label, localname))
00432 parser_->setContentHandler(&labelh_);
00433 else
00434 error::token(localname);
00435 }
00436
00437 template <typename T>
00438 void
00439 TransitionHandler<T>::end (const XMLCh* const,
00440 const XMLCh* const localname,
00441 const XMLCh* const)
00442 {
00443 using namespace xercesc;
00444 if (XMLString::equals(eq_.transition, localname))
00445 {
00446 parser_->setContentHandler(&root_);
00447 aut_.add_series_transition(src_, dst_, labelh_.series());
00448 }
00449 else
00450 error::token(localname);
00451 }
00452
00456 template <typename T>
00457 InitFinalHandler<T>::InitFinalHandler (xercesc::SAX2XMLReader* parser,
00458 Handler& root,
00459 T& aut,
00460 hstate_t state,
00461 bool initial)
00462 : Handler(parser, root),
00463 aut_(aut),
00464 state_(state),
00465 initial_(initial),
00466 s_(aut.series().one_),
00467 labelh_(parser, *this, s_, eq_.label),
00468 unsuph_(parser, *this)
00469 {
00470 }
00471
00472 template <typename T>
00473 void
00474 InitFinalHandler<T>::start (const XMLCh* const,
00475 const XMLCh* const localname,
00476 const XMLCh* const,
00477 const xercesc::Attributes&)
00478 {
00479 using namespace xercesc;
00480 if (XMLString::equals(eq_.drawing, localname))
00481 parser_->setContentHandler(&unsuph_);
00482 else if (XMLString::equals(eq_.geometry, localname))
00483 parser_->setContentHandler(&unsuph_);
00484 else if (XMLString::equals(eq_.label, localname))
00485 parser_->setContentHandler(&labelh_);
00486 else
00487 error::token(localname);
00488 }
00489
00490 template <typename T>
00491 void
00492 InitFinalHandler<T>::end (const XMLCh* const,
00493 const XMLCh* const localname,
00494 const XMLCh* const)
00495 {
00496 using namespace xercesc;
00497 if (XMLString::equals(eq_.initial, localname) && initial_)
00498 {
00499 parser_->setContentHandler(&root_);
00500 aut_.set_initial(state_, labelh_.series());
00501 }
00502 else if (XMLString::equals(eq_.final, localname) && !initial_)
00503 {
00504 parser_->setContentHandler(&root_);
00505 aut_.set_final(state_, labelh_.series());
00506 }
00507 else
00508 error::token(localname);
00509 }
00510
00514 template <typename T>
00515 GeometryHandler<T>::GeometryHandler (xercesc::SAX2XMLReader* parser,
00516 Handler& root,
00517 T& aut,
00518 hstate_t state)
00519 : Handler(parser, root),
00520 aut_(aut),
00521 state_(state)
00522 {
00523 }
00524
00525 template <typename T>
00526 void
00527 GeometryHandler<T>::start (const XMLCh* const,
00528 const XMLCh* const localname,
00529 const XMLCh* const,
00530 const xercesc::Attributes&)
00531 {
00532 error::token(localname);
00533 }
00534
00535 template <typename T>
00536 void
00537 GeometryHandler<T>::end (const XMLCh* const,
00538 const XMLCh* const localname,
00539 const XMLCh* const)
00540 {
00541 using namespace xercesc;
00542 if (XMLString::equals(eq_.geometry, localname))
00543 parser_->setContentHandler(&root_);
00544 else
00545 error::token(localname);
00546 }
00547
00551 template <typename T>
00552 RegexpHandler<T>*
00553 RegexpHandler<T>::create (const XMLCh* const localname)
00554 {
00555 using namespace xercesc;
00556 if (XMLString::equals(eq_.monElmt, localname))
00557 return builders::create_monElmth(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()));
00558 else if (XMLString::equals(eq_.star, localname))
00559 return new StarHandler<T>(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()));
00560 else if (XMLString::equals(eq_.leftExtMul, localname))
00561 return new ExtMulHandler<T>(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()), true);
00562 else if (XMLString::equals(eq_.rightExtMul, localname))
00563 return new ExtMulHandler<T>(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()), false);
00564 else if (XMLString::equals(eq_.sum, localname))
00565 return new SumHandler<T>(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()));
00566 else if (XMLString::equals(eq_.product, localname))
00567 return new ProductHandler<T>(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()));
00568 else if (XMLString::equals(eq_.one, localname))
00569 return new AtomHandler<T>(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()), eq_.one);
00570 else if (XMLString::equals(eq_.zero, localname))
00571 return new AtomHandler<T>(parser_, *this, algebra::zero_as<typename T::value_t>::of(param_.structure()), eq_.zero);
00572 else
00573 return 0;
00574 }
00575
00576 template <typename T>
00577 RegexpHandler<T>*
00578 RegexpHandler<T>::create_weight (const xercesc::Attributes& attrs)
00579 {
00580 using namespace xercesc;
00581 return builders::create_weighth(parser_, *this,
00582 algebra::identity_as<typename T::value_t>::of(param_.structure()), attrs);
00583 }
00584 }
00585 }
00586
00587 #endif // !VCSN_XML_HANDLERS_HXX