Vaucanson 1.4
handlers.hxx
00001 // handlers.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2007, 2008 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
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      * DocRegExpHandler
00031      */
00032     template <typename T>
00033     DocRegExpHandler<T>::DocRegExpHandler (xercesc::SAX2XMLReader* parser,
00034                                xercesc::DefaultHandler& root,
00035                                T& regexp,
00036                                XMLEq& eq)
00037       : Handler(parser, root, eq),
00038         regexp_(regexp),
00039         regexph_(parser, *this, regexp)
00040     {
00041     }
00042 
00043     template <typename T>
00044     void
00045     DocRegExpHandler<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         // T version
00054         // T xmlns
00055         parser_->setContentHandler(&regexph_);
00056       }
00057       else
00058         error::token(localname);
00059     }
00060 
00061     template <typename T>
00062     void
00063     DocRegExpHandler<T>::end (const XMLCh* const,
00064                                const XMLCh* const,
00065                                const XMLCh* const)
00066     {
00067     }
00068 
00069     /*
00070      * DocAutHandler
00071      */
00072     template <typename T>
00073     DocAutHandler<T>::DocAutHandler (xercesc::SAX2XMLReader* parser,
00074                                xercesc::DefaultHandler& root,
00075                                T& aut,
00076                                XMLEq& eq)
00077       : Handler(parser, root, eq),
00078         aut_(aut),
00079         auth_(parser, *this, aut)
00080     {
00081     }
00082 
00083     template <typename T>
00084     void
00085     DocAutHandler<T>::start (const XMLCh* const,
00086                                  const XMLCh* const localname,
00087                                  const XMLCh* const,
00088                                  const xercesc::Attributes&)
00089     {
00090       using namespace xercesc;
00091       if (XMLString::equals(eq_.fsmxml, localname))
00092       {
00093         // T version
00094         // T xmlns
00095         parser_->setContentHandler(&auth_);
00096       }
00097       else
00098         error::token(localname);
00099     }
00100 
00101     template <typename T>
00102     void
00103     DocAutHandler<T>::end (const XMLCh* const,
00104                                const XMLCh* const,
00105                                const XMLCh* const)
00106     {
00107     }
00108 
00112     template <typename T>
00113     RegExpHandler<T>::RegExpHandler (xercesc::SAX2XMLReader* parser,
00114                                Handler& root,
00115                                T& regexp)
00116       : Handler(parser, root),
00117         regexp_(regexp),
00118         typeh_(parser, *this, regexp),
00119         contenth_(parser, *this, regexp, eq_.typedRegExp),
00120         unsuph_(parser, *this)
00121     {
00122     }
00123 
00124     template <typename T>
00125     void
00126     RegExpHandler<T>::start (const XMLCh* const uri,
00127                                  const XMLCh* const localname,
00128                                  const XMLCh* const,
00129                                  const xercesc::Attributes& attrs)
00130     {
00131       using namespace xercesc;
00132       if (XMLString::equals(eq_.regExp, localname))
00133       {
00134         // nothing to do ?
00135       }
00136       else if (XMLString::equals(eq_.drawing, localname))
00137         parser_->setContentHandler(&unsuph_);
00138       else if (XMLString::equals(eq_.geometry, localname))
00139         parser_->setContentHandler(&unsuph_);
00140       else if (XMLString::equals(eq_.valueType, localname))
00141         parser_->setContentHandler(&typeh_);
00142       else if (XMLString::equals(eq_.typedRegExp, localname))
00143       {
00144         contenth_.series(regexp_);
00145         parser_->setContentHandler(&contenth_);
00146       }
00147       else
00148         error::token(localname);
00149     }
00150 
00151     template <typename T>
00152     void
00153     RegExpHandler<T>::end (const XMLCh* const,
00154                                const XMLCh* const localname,
00155                                const XMLCh* const)
00156     {
00157       using namespace xercesc;
00158       if (XMLString::equals(eq_.regExp, localname))
00159       {
00160         parser_->setContentHandler(&root_);
00161         regexp_ = contenth_.series();
00162       }
00163     }
00164 
00168     template <typename T>
00169     AutHandler<T>::AutHandler (xercesc::SAX2XMLReader* parser,
00170                                Handler& root,
00171                                T& aut)
00172       : Handler(parser, root),
00173         aut_(aut),
00174         typeh_(parser, *this, aut),
00175         contenth_(parser, *this, aut),
00176         unsuph_(parser, *this)
00177     {
00178     }
00179 
00180     template <typename T>
00181     void
00182     AutHandler<T>::start (const XMLCh* const uri,
00183                                  const XMLCh* const localname,
00184                                  const XMLCh* const,
00185                                  const xercesc::Attributes& attrs)
00186     {
00187       using namespace xercesc;
00188       if (XMLString::equals(eq_.automaton, localname))
00189       {
00190         const XMLCh* autname = tools::get_attribute(attrs, "name", uri);
00191         if (autname != 0)
00192           aut_.geometry().name() = xmlstr(autname);
00193         // T readingDirection
00194       }
00195       else if (XMLString::equals(eq_.drawing, localname))
00196         parser_->setContentHandler(&unsuph_);
00197       else if (XMLString::equals(eq_.geometry, localname))
00198         parser_->setContentHandler(&unsuph_);
00199       else if (XMLString::equals(eq_.valueType, localname))
00200         parser_->setContentHandler(&typeh_);
00201       else if (XMLString::equals(eq_.content, localname))
00202         parser_->setContentHandler(&contenth_);
00203       else
00204         error::token(localname);
00205     }
00206 
00207     template <typename T>
00208     void
00209     AutHandler<T>::end (const XMLCh* const,
00210                                const XMLCh* const localname,
00211                                const XMLCh* const)
00212     {
00213       using namespace xercesc;
00214       if (XMLString::equals(eq_.automaton, localname))
00215         parser_->setContentHandler(&root_);
00216     }
00217 
00221     template <typename T>
00222     TypeHandler<T>::TypeHandler (xercesc::SAX2XMLReader* parser,
00223                                  Handler& root,
00224                                  T& param)
00225       : Handler(parser, root),
00226         monoid_(0),
00227         semiring_(0),
00228         rep_(0),
00229         param_(param),
00230         monoidh_(0),
00231         reph_(0),
00232         semiringh_(0),
00233         unsuph_(parser, *this)
00234     {
00235     }
00236 
00237     template <typename T>
00238     void
00239     TypeHandler<T>::start(const XMLCh* const,
00240                           const XMLCh* const localname,
00241                           const XMLCh* const,
00242                           const xercesc::Attributes& attrs)
00243     {
00244       using namespace xercesc;
00245 
00246       if (XMLString::equals(eq_.semiring, localname))
00247       {
00248         semiring_ = builders::create_semiring(param_, localname, attrs);
00249         semiringh_ = builders::create_semiringh(*semiring_, attrs, parser_, *this);
00250         parser_->setContentHandler(semiringh_);
00251       }
00252       else if (XMLString::equals(eq_.monoid, localname))
00253       {
00254         monoid_ = builders::create_monoid(param_, localname, attrs, eq_);
00255         monoidh_ = builders::create_monoidh(*monoid_, attrs, parser_, *this);
00256         parser_->setContentHandler(monoidh_);
00257       }
00258       else if (XMLString::equals(eq_.writingData, localname))
00259       {
00260         rep_ = builders::create_series_representation(param_, localname, attrs, eq_);
00261         reph_ = builders::create_series_representationh(*rep_, attrs, parser_, *this, eq_);
00262         parser_->setContentHandler(reph_);
00263       }
00264       else
00265         error::token(localname);
00266     }
00267 
00268     template <typename T>
00269     void
00270     TypeHandler<T>::end(const XMLCh* const,
00271                         const XMLCh* const localname,
00272                         const XMLCh* const)
00273     {
00274       using namespace xercesc;
00275 
00276 
00277       // The series representation is optional.
00278       if (rep_)
00279       {
00280         typename T::series_set_t series(*semiring_, *monoid_, *rep_);
00281         param_.attach(series);
00282       }
00283       else
00284       {
00285         typename T::series_set_t series(*semiring_, *monoid_);
00286         param_.attach(series);
00287       }
00288 
00289       if (XMLString::equals(eq_.valueType, localname))
00290         parser_->setContentHandler(&root_);
00291 
00292       delete monoid_;
00293       delete monoidh_;
00294       delete rep_;
00295       delete reph_;
00296       delete semiring_;
00297       delete semiringh_;
00298     }
00299 
00303     template <typename T>
00304     ContHandler<T>::ContHandler (xercesc::SAX2XMLReader* parser,
00305                                  Handler& root,
00306                                  T& aut)
00307       : Handler(parser, root),
00308         aut_(aut),
00309         statesh_(parser, *this, aut, map_),
00310         transitionsh_(parser, *this, aut, map_)
00311     {
00312     }
00313 
00314     template <typename T>
00315     void
00316     ContHandler<T>::start (const XMLCh* const,
00317                                  const XMLCh* const localname,
00318                                  const XMLCh* const,
00319                                  const xercesc::Attributes&)
00320     {
00321       using namespace xercesc;
00322       if (XMLString::equals(eq_.states, localname))
00323         parser_->setContentHandler(&statesh_);
00324       else if (XMLString::equals(eq_.transitions, localname))
00325         parser_->setContentHandler(&transitionsh_);
00326       else
00327         error::token(localname);
00328     }
00329 
00330     template <typename T>
00331     void
00332     ContHandler<T>::end (const XMLCh* const,
00333                                const XMLCh* const localname,
00334                                const XMLCh* const)
00335     {
00336       using namespace xercesc;
00337       if (XMLString::equals(eq_.content, localname))
00338         parser_->setContentHandler(&root_);
00339       else
00340         error::token(localname);
00341     }
00342 
00346     template <typename T>
00347     StatesHandler<T>::StatesHandler (xercesc::SAX2XMLReader* parser,
00348                                  Handler& root,
00349                                  T& aut,
00350                                  map_t& map)
00351       : Handler(parser, root),
00352         aut_(aut),
00353         map_(map),
00354         stateh_(0)
00355     {
00356     }
00357 
00358     template <typename T>
00359     void
00360     StatesHandler<T>::start (const XMLCh* const,
00361                                  const XMLCh* const localname,
00362                                  const XMLCh* const,
00363                                  const xercesc::Attributes& attrs)
00364     {
00365       using namespace xercesc;
00366       if (XMLString::equals(eq_.state, localname))
00367       {
00368         hstate_t state = aut_.add_state();
00369         map_[xmlstr(tools::get_attribute(attrs, eq_.id))] = state;
00370         delete stateh_;
00371         stateh_ = new StateHandler<T>(parser_, *this, aut_, state);
00372         parser_->setContentHandler(stateh_);
00373       }
00374       else
00375         error::token(localname);
00376     }
00377 
00378     template <typename T>
00379     void
00380     StatesHandler<T>::end (const XMLCh* const,
00381                                const XMLCh* const localname,
00382                                const XMLCh* const)
00383     {
00384       using namespace xercesc;
00385       if (XMLString::equals(eq_.states, localname))
00386         parser_->setContentHandler(&root_);
00387       else
00388         error::token(localname);
00389       delete stateh_;
00390     }
00391 
00395     template <typename T>
00396     StateHandler<T>::StateHandler (xercesc::SAX2XMLReader* parser,
00397                                  Handler& root,
00398                                  T& aut,
00399                                  hstate_t state)
00400       : Handler(parser, root),
00401         aut_(aut),
00402         state_(state),
00403         geometryh_(parser, *this, aut, state),
00404         unsuph_(parser, *this)
00405     {
00406     }
00407 
00408     template <typename T>
00409     void
00410     StateHandler<T>::start (const XMLCh* const,
00411                                  const XMLCh* const localname,
00412                                  const XMLCh* const,
00413                                  const xercesc::Attributes& attrs)
00414     {
00415       using namespace xercesc;
00416       if (XMLString::equals(eq_.drawing, localname))
00417         parser_->setContentHandler(&unsuph_);
00418       else if (XMLString::equals(eq_.geometry, localname))
00419       {
00420         typedef typename T::geometry_t::states_geometry_map_t gmap_t;
00421 
00422         double y = 0;
00423         double x = 0;
00424         if (tools::has_attribute(attrs, eq_.x))
00425         {
00426           std::istringstream xstr(xmlstr(tools::get_attribute(attrs, eq_.x)));
00427           xstr >> x;
00428         }
00429         if (tools::has_attribute(attrs, eq_.y))
00430         {
00431           std::istringstream ystr(xmlstr(tools::get_attribute(attrs, eq_.y)));
00432           ystr >> y;
00433         }
00434         gmap_t& map = aut_.geometry().states();
00435         map[state_] = std::make_pair(x,y);
00436         parser_->setContentHandler(&geometryh_);
00437       }
00438       else
00439         error::token(localname);
00440     }
00441 
00442     template <typename T>
00443     void
00444     StateHandler<T>::end (const XMLCh* const,
00445                                const XMLCh* const localname,
00446                                const XMLCh* const)
00447     {
00448       using namespace xercesc;
00449       if (XMLString::equals(eq_.state, localname))
00450         parser_->setContentHandler(&root_);
00451       else
00452         error::token(localname);
00453     }
00454 
00458     template <typename T>
00459     TransitionsHandler<T>::TransitionsHandler (xercesc::SAX2XMLReader* parser,
00460                                  Handler& root,
00461                                  T& aut,
00462                                  map_t& map)
00463       : Handler(parser, root),
00464         aut_(aut),
00465         map_(map),
00466         transitionh_(0)
00467     {
00468     }
00469 
00470     template <typename T>
00471     void
00472     TransitionsHandler<T>::start (const XMLCh* const,
00473                                  const XMLCh* const localname,
00474                                  const XMLCh* const,
00475                                  const xercesc::Attributes& attrs)
00476     {
00477       using namespace xercesc;
00478       if (XMLString::equals(eq_.transition, localname))
00479       {
00480         hstate_t src = map_[xmlstr(tools::get_attribute(attrs, eq_.source))];
00481         hstate_t dst = map_[xmlstr(tools::get_attribute(attrs, eq_.target))];
00482         delete transitionh_;
00483         transitionh_ = new TransitionHandler<T>(parser_, *this, aut_, src, dst);
00484         parser_->setContentHandler(transitionh_);
00485       }
00486       else if (XMLString::equals(eq_.final, localname))
00487       {
00488         hstate_t state = map_[xmlstr(tools::get_attribute(attrs, eq_.state))];
00489         delete transitionh_;
00490         transitionh_ = new InitFinalHandler<T>(parser_, *this, aut_, state, false);
00491         parser_->setContentHandler(transitionh_);
00492       }
00493       else if (XMLString::equals(eq_.initial, localname))
00494       {
00495         hstate_t state = map_[xmlstr(tools::get_attribute(attrs, eq_.state))];
00496         delete transitionh_;
00497         transitionh_ = new InitFinalHandler<T>(parser_, *this, aut_, state, true);
00498         parser_->setContentHandler(transitionh_);
00499       }
00500       else
00501         error::token(localname);
00502     }
00503 
00504     template <typename T>
00505     void
00506     TransitionsHandler<T>::end (const XMLCh* const,
00507                                const XMLCh* const localname,
00508                                const XMLCh* const)
00509     {
00510       using namespace xercesc;
00511       if (XMLString::equals(eq_.transitions, localname))
00512         parser_->setContentHandler(&root_);
00513       else
00514         error::token(localname);
00515       delete transitionh_;
00516     }
00517 
00521     template <typename T>
00522     TransitionHandler<T>::TransitionHandler (xercesc::SAX2XMLReader* parser,
00523                                  Handler& root,
00524                                  T& aut,
00525                                  hstate_t src,
00526                                  hstate_t dst)
00527       : Handler(parser, root),
00528         aut_(aut),
00529         src_(src),
00530         dst_(dst),
00531         s_(aut.series()),
00532         labelh_(parser, *this, s_, eq_.label),
00533         unsuph_(parser, *this)
00534     {
00535     }
00536 
00537     template <typename T>
00538     void
00539     TransitionHandler<T>::start (const XMLCh* const,
00540                                  const XMLCh* const localname,
00541                                  const XMLCh* const,
00542                                  const xercesc::Attributes&)
00543     {
00544       using namespace xercesc;
00545       if (XMLString::equals(eq_.drawing, localname))
00546         parser_->setContentHandler(&unsuph_);
00547       else if (XMLString::equals(eq_.geometry, localname))
00548         parser_->setContentHandler(&unsuph_);
00549       else if (XMLString::equals(eq_.label, localname))
00550         parser_->setContentHandler(&labelh_);
00551       else
00552         error::token(localname);
00553     }
00554 
00555     template <typename T>
00556     void
00557     TransitionHandler<T>::end (const XMLCh* const,
00558                                const XMLCh* const localname,
00559                                const XMLCh* const)
00560     {
00561       using namespace xercesc;
00562       if (XMLString::equals(eq_.transition, localname))
00563       {
00564         parser_->setContentHandler(&root_);
00565         aut_.add_series_transition(src_, dst_, labelh_.series());
00566       }
00567       else
00568         error::token(localname);
00569     }
00570 
00574     template <typename T>
00575     InitFinalHandler<T>::InitFinalHandler (xercesc::SAX2XMLReader* parser,
00576                                  Handler& root,
00577                                  T& aut,
00578                                  hstate_t state,
00579                                  bool initial)
00580       : Handler(parser, root),
00581         aut_(aut),
00582         state_(state),
00583         initial_(initial),
00584         s_(aut.series().one_), // hack shouldn't exist.
00585         labelh_(parser, *this, s_, eq_.label),
00586         unsuph_(parser, *this)
00587     {
00588     }
00589 
00590     template <typename T>
00591     void
00592     InitFinalHandler<T>::start (const XMLCh* const,
00593                                  const XMLCh* const localname,
00594                                  const XMLCh* const,
00595                                  const xercesc::Attributes&)
00596     {
00597       using namespace xercesc;
00598       if (XMLString::equals(eq_.drawing, localname))
00599         parser_->setContentHandler(&unsuph_);
00600       else if (XMLString::equals(eq_.geometry, localname))
00601         parser_->setContentHandler(&unsuph_);
00602       else if (XMLString::equals(eq_.label, localname))
00603         parser_->setContentHandler(&labelh_);
00604       else
00605         error::token(localname);
00606     }
00607 
00608     template <typename T>
00609     void
00610     InitFinalHandler<T>::end (const XMLCh* const,
00611                                const XMLCh* const localname,
00612                                const XMLCh* const)
00613     {
00614       using namespace xercesc;
00615       if (XMLString::equals(eq_.initial, localname) && initial_)
00616       {
00617         parser_->setContentHandler(&root_);
00618         aut_.set_initial(state_, labelh_.series());
00619       }
00620       else if (XMLString::equals(eq_.final, localname) && !initial_)
00621       {
00622         parser_->setContentHandler(&root_);
00623         aut_.set_final(state_, labelh_.series());
00624       }
00625       else
00626         error::token(localname);
00627     }
00628 
00632     template <typename T>
00633     GeometryHandler<T>::GeometryHandler (xercesc::SAX2XMLReader* parser,
00634                                         Handler& root,
00635                                         T& aut,
00636                                         hstate_t state)
00637       : Handler(parser, root),
00638         aut_(aut),
00639         state_(state)
00640     {
00641     }
00642 
00643     template <typename T>
00644     void
00645     GeometryHandler<T>::start (const XMLCh* const,
00646                                  const XMLCh* const localname,
00647                                  const XMLCh* const,
00648                                  const xercesc::Attributes&)
00649     {
00650       error::token(localname);
00651     }
00652 
00653     template <typename T>
00654     void
00655     GeometryHandler<T>::end (const XMLCh* const,
00656                                const XMLCh* const localname,
00657                                const XMLCh* const)
00658     {
00659       using namespace xercesc;
00660       if (XMLString::equals(eq_.geometry, localname))
00661         parser_->setContentHandler(&root_);
00662       else
00663         error::token(localname);
00664     }
00665 
00669     template <typename T>
00670     RegexpHandler<T>*
00671     RegexpHandler<T>::create (const XMLCh* const localname)
00672     {
00673       using namespace xercesc;
00674       if (XMLString::equals(eq_.monElmt, localname))
00675         return builders::create_monElmth(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()));
00676       else if (XMLString::equals(eq_.star, localname))
00677         return new StarHandler<T>(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()));
00678       else if (XMLString::equals(eq_.leftExtMul, localname))
00679         return new ExtMulHandler<T>(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()), true);
00680       else if (XMLString::equals(eq_.rightExtMul, localname))
00681         return new ExtMulHandler<T>(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()), false);
00682       else if (XMLString::equals(eq_.sum, localname))
00683         return new SumHandler<T>(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()));
00684       else if (XMLString::equals(eq_.product, localname))
00685         return new ProductHandler<T>(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()));
00686       else if (XMLString::equals(eq_.one, localname))
00687         return new AtomHandler<T>(parser_, *this, algebra::identity_as<typename T::value_t>::of(param_.structure()), eq_.one);
00688       else if (XMLString::equals(eq_.zero, localname))
00689         return new AtomHandler<T>(parser_, *this, algebra::zero_as<typename T::value_t>::of(param_.structure()), eq_.zero);
00690       else
00691         return 0;
00692     }
00693 
00694     template <typename T>
00695     RegexpHandler<T>*
00696     RegexpHandler<T>::create_weight (const xercesc::Attributes& attrs)
00697     {
00698       using namespace xercesc;
00699       return builders::create_weighth(parser_, *this,
00700         algebra::identity_as<typename T::value_t>::of(param_.structure()), attrs);
00701     }
00702   } // !xml
00703 } // !vcsn
00704 
00705 #endif // !VCSN_XML_HANDLERS_HXX