handlers.hh

00001 // handlers.hh: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2005, 2006, 2007 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_HH
00019 # define VCSN_XML_HANDLERS_HH
00020 
00031 # include <map>
00032 # include <string>
00033 
00034 # include <xercesc/sax2/SAX2XMLReader.hpp>
00035 # include <xercesc/sax2/XMLReaderFactory.hpp>
00036 # include <xercesc/sax2/DefaultHandler.hpp>
00037 # include <xercesc/sax2/Attributes.hpp>
00038 
00039 # include <xercesc/sax/SAXException.hpp>
00040 # include <xercesc/util/XMLString.hpp>
00041 
00042 # include <vaucanson/xml/strings.hh>
00043 # include <vaucanson/xml/xmleq.hh>
00044 
00045 # include <vaucanson/design_pattern/element.hh>
00046 # include <vaucanson/automata/concept/automata.hh>
00047 # include <vaucanson/automata/concept/transducer.hh>
00048 # include <vaucanson/algebra/implementation/series/series.hh>
00049 # include <vaucanson/algebra/concept/freemonoid_product.hh>
00050 
00051 
00052 namespace vcsn
00053 {
00054   namespace xml
00055   {
00056     /*
00057      * Handler class
00058      */
00059     class Handler : public xercesc::DefaultHandler
00060     {
00061       public:
00062         Handler (xercesc::SAX2XMLReader* parser,
00063                  XMLEq& eq,
00064                  xercesc::DefaultHandler& root);
00065 
00066         void
00067         startElement (const XMLCh* const uri,
00068                       const XMLCh* const localname,
00069                       const XMLCh* const qname,
00070                       const xercesc::Attributes& attrs) = 0;
00071         void
00072         endElement (const XMLCh* const uri,
00073                     const XMLCh* const localname,
00074                     const XMLCh* const qname) = 0;
00075       protected:
00076         // Required to enable a change of the Handler while parsing.
00077         xercesc::SAX2XMLReader* parser_;
00078         xercesc::DefaultHandler&        root_;
00079 
00080         XMLEq& eq_;
00081     };
00082 
00083     /*
00084      * ErrHandler class, error handler.
00085      */
00086     class ErrHandler : public xercesc::DefaultHandler
00087     {
00088       public:
00089         ErrHandler () : DefaultHandler() {}
00090 
00091         void
00092         warning (const xercesc::SAXParseException& exc);
00093         // Receive notification of a warning.
00094         void
00095         error (const xercesc::SAXParseException& exc);
00096         // Receive notification of a recoverable error.
00097         void
00098         fatalError (const xercesc::SAXParseException& exc);
00099         // Receive notification of a non-recoverable error.
00100         void
00101         resetErrors () {}
00102         // Reset the Error handler object on its reuse.
00103     };
00104 
00105 # define HANDLER_DEF_FUN                                \
00106   void                                                  \
00107   startElement (const XMLCh* const uri,                 \
00108                 const XMLCh* const localname,           \
00109                 const XMLCh* const qname,               \
00110                 const xercesc::Attributes& attrs);      \
00111   void                                                  \
00112   endElement (const XMLCh* const uri,                   \
00113               const XMLCh* const localname,             \
00114               const XMLCh* const qname);
00115 
00116     /*
00117      * Predecls
00118      */
00119     template <typename T>
00120     class ContHandler;
00121     template <typename T>
00122     class TypeHandler;
00123     template <typename T>
00124     class TransitionsHandler;
00125     template <typename T>
00126     class StatesHandler;
00127 
00128     /*
00129      * UnsupHandler class, Handler dedicated to "eat" all part
00130      * of the format Vaucanson still not supports.
00131      */
00132     class UnsupHandler : public xercesc::DefaultHandler
00133     {
00134       public:
00135         UnsupHandler (xercesc::DefaultHandler& root,
00136                       xercesc::SAX2XMLReader* parser);
00137 
00138         void
00139         startElement (const XMLCh* const uri,
00140                       const XMLCh* const localname,
00141                       const XMLCh* const qname,
00142                       const xercesc::Attributes& attrs);
00143         void
00144         endElement (const XMLCh* const uri,
00145                     const XMLCh* const localname,
00146                     const XMLCh* const qname);
00147       private:
00148         xercesc::SAX2XMLReader* parser_;
00149         xercesc::DefaultHandler&        root_;
00150         int             depth_;
00151     };
00152 
00153     /*
00154      * AutHandler class, Handler for <automaton> tag.
00155      */
00156     template <typename T>
00157     class AutHandler : public xercesc::DefaultHandler
00158     {
00159       public:
00160         AutHandler (T& aut,
00161                     xercesc::SAX2XMLReader* parser,
00162                     XMLEq& eq);
00163 
00164         void
00165         startElement (const XMLCh* const uri,
00166                       const XMLCh* const localname,
00167                       const XMLCh* const qname,
00168                       const xercesc::Attributes& attrs);
00169         void
00170         endElement (const XMLCh* const uri,
00171                     const XMLCh* const localname,
00172                     const XMLCh* const qname);
00173         // Used by SessHandler
00174         bool
00175         end ();
00176 
00177       private:
00178         xercesc::SAX2XMLReader* parser_;
00179         XMLEq& eq_;
00180         // the automaton to be filled.
00181         T&                      aut_;
00182         // used only for session management.
00183         bool                    end_;
00184 
00185         // Handlers that could be called while parsing.
00186         UnsupHandler    unsuph_;
00187         ContHandler<T>  contenth_;
00188         TypeHandler<T>  typeh_;
00189     };
00190 
00191     /*
00192      * MonoidHandler class, dedicated to parse <monoid> tag.
00193      */
00194     class MonoidHandler : public Handler
00195     {
00196       public:
00197         MonoidHandler (xercesc::DefaultHandler& father,
00198                        xercesc::SAX2XMLReader* parser,
00199                        XMLEq& eq);
00200 
00201         void startElement (const XMLCh* const uri,
00202                            const XMLCh* const localname,
00203                            const XMLCh* const qname,
00204                            const xercesc::Attributes& attrs) = 0;
00205         void endElement (const XMLCh* const uri,
00206                          const XMLCh* const localname,
00207                          const XMLCh* const qname) = 0;
00208     };
00209     template <typename T>
00210     class FreeMonoidHandler : public MonoidHandler
00211     {
00212       public:
00213         FreeMonoidHandler (xercesc::DefaultHandler& father,
00214                            T& monoid,
00215                            xercesc::SAX2XMLReader* parser,
00216                            XMLEq& eq);
00217 
00218         void startElement (const XMLCh* const uri,
00219                            const XMLCh* const localname,
00220                            const XMLCh* const qname,
00221                            const xercesc::Attributes& attrs);
00222         void endElement (const XMLCh* const uri,
00223                          const XMLCh* const localname,
00224                          const XMLCh* const qname);
00225 
00226       private:
00227         T&                              monoid_;
00228     };
00229     template <typename T>
00230     class ProdMonoidHandler : public MonoidHandler
00231     {
00232       public:
00233         ProdMonoidHandler (xercesc::DefaultHandler& father,
00234                            T& monoid,
00235                            xercesc::SAX2XMLReader* parser,
00236                            XMLEq& eq);
00237 
00238         void startElement (const XMLCh* const uri,
00239                            const XMLCh* const localname,
00240                            const XMLCh* const qname,
00241                            const xercesc::Attributes& attrs);
00242         void endElement (const XMLCh* const uri,
00243                          const XMLCh* const localname,
00244                          const XMLCh* const qname);
00245 
00246       private:
00247         T&      monoid_;
00248         bool    first_;
00249 
00250         MonoidHandler*                  monoidh_;
00251     };
00252 
00253     /*
00254      * SemiringHandler class, dedicated to parse <semiring> tag.
00255      */
00256     class SemiringHandler : public Handler
00257     {
00258       public:
00259         SemiringHandler (xercesc::DefaultHandler& father,
00260                          xercesc::SAX2XMLReader* parser,
00261                          XMLEq& eq);
00262     };
00263     template <typename T>
00264     class SeriesSemiringHandler : public SemiringHandler
00265     {
00266       public:
00267         SeriesSemiringHandler (xercesc::DefaultHandler& father,
00268                                T& semiring,
00269                                xercesc::SAX2XMLReader* parser,
00270                                XMLEq& eq);
00271 
00272         void startElement (const XMLCh* const uri,
00273                            const XMLCh* const localname,
00274                            const XMLCh* const qname,
00275                            const xercesc::Attributes& attrs);
00276         void endElement (const XMLCh* const uri,
00277                          const XMLCh* const localname,
00278                          const XMLCh* const qname);
00279 
00280       private:
00281         T&                      semiring_;
00282         SemiringHandler*        semiringh_;
00283         MonoidHandler*          monoidh_;
00284     };
00285     template <typename T>
00286     class NumSemiringHandler : public SemiringHandler
00287     {
00288       public:
00289         NumSemiringHandler (xercesc::DefaultHandler& father,
00290                             T& semiring,
00291                             xercesc::SAX2XMLReader* parser,
00292                             XMLEq& eq);
00293 
00294         void startElement (const XMLCh* const uri,
00295                            const XMLCh* const localname,
00296                            const XMLCh* const qname,
00297                            const xercesc::Attributes& attrs);
00298         void endElement (const XMLCh* const uri,
00299                          const XMLCh* const localname,
00300                          const XMLCh* const qname);
00301 
00302       private:
00303         T&      semiring_;
00304     };
00305 
00306     /*
00307      * TypeHandler class, dedicated to parse <labelType> tag.
00308      */
00309     template <typename T>
00310     class TypeHandler : public Handler
00311     {
00312       public:
00313         TypeHandler (xercesc::DefaultHandler& father,
00314                      T& aut,
00315                      xercesc::SAX2XMLReader* parser,
00316                      XMLEq& eq);
00317 
00318         void startElement (const XMLCh* const uri,
00319                            const XMLCh* const localname,
00320                            const XMLCh* const qname,
00321                            const xercesc::Attributes& attrs);
00322         void endElement (const XMLCh* const uri,
00323                          const XMLCh* const localname,
00324                          const XMLCh* const qname);
00325 
00326       private:
00327         typedef typename T::monoid_t            monoid_t;
00328         typedef typename T::semiring_t          semiring_t;
00329         typedef typename T::series_set_t        series_set_t;
00330 
00331         monoid_t*                       monoid_;
00332         semiring_t*                     semiring_;
00333 
00334         T&                              aut_;
00335 
00336         SemiringHandler*                semiringh_;
00337         MonoidHandler*                  monoidh_;
00338     };
00339 
00340     /*
00341      * ContHandler class, dedicated to parse <content> tag.
00342      */
00343     template <typename T>
00344     class ContHandler : public Handler
00345     {
00346       public:
00347         ContHandler (xercesc::DefaultHandler& father,
00348                      T& aut,
00349                      xercesc::SAX2XMLReader* parser,
00350                      XMLEq& eq);
00351 
00352         void startElement (const XMLCh* const uri,
00353                            const XMLCh* const localname,
00354                            const XMLCh* const qname,
00355                            const xercesc::Attributes& attrs);
00356         void endElement (const XMLCh* const uri,
00357                          const XMLCh* const localname,
00358                          const XMLCh* const qname);
00359 
00360       private:
00361         typedef std::map<std::string, hstate_t> map_t;
00362 
00363         T&                              aut_;
00364         TransitionsHandler<T>           transh_;
00365         StatesHandler<T>                statesh_;
00366 
00367         map_t                           map_;
00368     };
00369 
00370     /*
00371      * StateHandler class, dedicated to parse <state> tag.
00372      */
00373     class StateHandler : public Handler
00374     {
00375       public:
00376         typedef std::map<std::string, hstate_t> map_t;
00377 
00378         StateHandler (xercesc::DefaultHandler& father,
00379                       map_t& map,
00380                       xercesc::SAX2XMLReader* parser,
00381                       XMLEq& eq);
00382 
00383         void startElement (const XMLCh* const uri,
00384                            const XMLCh* const localname,
00385                            const XMLCh* const qname,
00386                            const xercesc::Attributes& attrs);
00387         void endElement (const XMLCh* const uri,
00388                          const XMLCh* const localname,
00389                          const XMLCh* const qname);
00390         void reset (hstate_t* state);
00391       private:
00392         map_t&                          map_;
00393 
00394         hstate_t*                       state_;
00395         UnsupHandler    unsuph_;
00396     };
00397 
00398     /*
00399      * StatesHandler class, dedicated to parse <states> tag.
00400      */
00401     template <typename T>
00402     class StatesHandler : public Handler
00403     {
00404       public:
00405         typedef std::map<std::string, hstate_t> map_t;
00406 
00407         StatesHandler (xercesc::DefaultHandler& father,
00408                        T& aut,
00409                        map_t& map,
00410                        xercesc::SAX2XMLReader* parser,
00411                        XMLEq& eq);
00412 
00413         void startElement (const XMLCh* const uri,
00414                            const XMLCh* const localname,
00415                            const XMLCh* const qname,
00416                            const xercesc::Attributes& attrs);
00417         void endElement (const XMLCh* const uri,
00418                          const XMLCh* const localname,
00419                          const XMLCh* const qname);
00420 
00421       private:
00422         map_t&                          map_;
00423 
00424         T&                              aut_;
00425         StateHandler                    stateh_;
00426     };
00427 
00428     /*
00429      * RegExpHandler class, dedicated to parse regular expression tag.
00430      */
00431     template <typename T>
00432     class RegExpHandler : public Handler
00433     {
00434       public:
00435         typedef typename T::series_set_elt_t series_set_elt_t;
00436         //typedef Element<typename T::series_set_t,
00437         //              rat::exp<typename T::monoid_elt_value_t,
00438         //                       typename T::semiring_elt_value_t> >
00439         //      series_set_elt_t;
00440 
00441         RegExpHandler (xercesc::DefaultHandler& father,
00442                        T& aut,
00443                        xercesc::SAX2XMLReader* parser,
00444                        XMLEq& eq);
00445 
00446         void startElement (const XMLCh* const uri,
00447                            const XMLCh* const localname,
00448                            const XMLCh* const qname,
00449                            const xercesc::Attributes& attrs) = 0;
00450         void endElement (const XMLCh* const uri,
00451                          const XMLCh* const localname,
00452                          const XMLCh* const qname) = 0;
00453 
00454         series_set_elt_t
00455         get_series ();
00456       protected:
00457         T&                              aut_;
00458 
00459         series_set_elt_t                krat_exp_;
00460     };
00461 
00462     template <typename T>
00463     class SumHandler : public RegExpHandler<T>
00464     {
00465       public:
00466         SumHandler (xercesc::DefaultHandler& father,
00467                     T& aut,
00468                     xercesc::SAX2XMLReader* parser,
00469                     XMLEq& eq);
00470 
00471         void startElement (const XMLCh* const uri,
00472                            const XMLCh* const localname,
00473                            const XMLCh* const qname,
00474                            const xercesc::Attributes& attrs);
00475         void endElement (const XMLCh* const uri,
00476                          const XMLCh* const localname,
00477                          const XMLCh* const qname);
00478 
00479       protected:
00480         bool                    first_;
00481         RegExpHandler<T>*       lefth_;
00482         RegExpHandler<T>*       righth_;
00483     };
00484     template <typename T>
00485     class ProdHandler : public RegExpHandler<T>
00486     {
00487       public:
00488         ProdHandler (xercesc::DefaultHandler& father,
00489                      T& aut,
00490                      xercesc::SAX2XMLReader* parser,
00491                      XMLEq& eq);
00492 
00493         void startElement (const XMLCh* const uri,
00494                            const XMLCh* const localname,
00495                            const XMLCh* const qname,
00496                            const xercesc::Attributes& attrs);
00497         void endElement (const XMLCh* const uri,
00498                          const XMLCh* const localname,
00499                          const XMLCh* const qname);
00500 
00501       protected:
00502         bool                    first_;
00503         RegExpHandler<T>*       lefth_;
00504         RegExpHandler<T>*       righth_;
00505     };
00506     template <typename T>
00507     class StarHandler : public RegExpHandler<T>
00508     {
00509       public:
00510         StarHandler (xercesc::DefaultHandler& father,
00511                      T& aut,
00512                      xercesc::SAX2XMLReader* parser,
00513                      XMLEq& eq);
00514 
00515         void startElement (const XMLCh* const uri,
00516                            const XMLCh* const localname,
00517                            const XMLCh* const qname,
00518                            const xercesc::Attributes& attrs);
00519         void endElement (const XMLCh* const uri,
00520                          const XMLCh* const localname,
00521                          const XMLCh* const qname);
00522 
00523       protected:
00524         RegExpHandler<T>*       sonh_;
00525     };
00526     template <typename T>
00527     class WordHandler : public RegExpHandler<T>
00528     {
00529       public:
00530         WordHandler (xercesc::DefaultHandler& father,
00531                      T& aut,
00532                      xercesc::SAX2XMLReader* parser,
00533                      XMLEq& eq,
00534                      typename RegExpHandler<T>::series_set_elt_t val);
00535 
00536         void startElement (const XMLCh* const uri,
00537                            const XMLCh* const localname,
00538                            const XMLCh* const qname,
00539                            const xercesc::Attributes& attrs);
00540         void endElement (const XMLCh* const uri,
00541                          const XMLCh* const localname,
00542                          const XMLCh* const qname);
00543     };
00544 
00545     /*
00546      * LabelHandler class, dedicated to parse <label> tag.
00547      */
00548     template <typename T>
00549     class LabelHandler : public RegExpHandler<T>
00550     {
00551       public:
00552         typedef typename T::series_set_elt_t series_set_elt_t;
00553         LabelHandler (xercesc::DefaultHandler& father,
00554                       T& aut,
00555                       xercesc::SAX2XMLReader* parser,
00556                       XMLEq& eq);
00557 
00558         void startElement (const XMLCh* const uri,
00559                            const XMLCh* const localname,
00560                            const XMLCh* const qname,
00561                            const xercesc::Attributes& attrs);
00562         void endElement (const XMLCh* const uri,
00563                          const XMLCh* const localname,
00564                          const XMLCh* const qname);
00565         bool used ();
00566         void reset ();
00567         series_set_elt_t
00568         value();
00569       protected:
00570         RegExpHandler<T>*       sonh_;
00571         bool                    used_;
00572     };
00573 
00574     /*
00575      * TransitionHandler class, dedicated to parse <transition> tag.
00576      */
00577     template <typename T>
00578     class TransitionHandler : public Handler
00579     {
00580       public:
00581         typedef std::map<std::string, hstate_t> map_t;
00582         typedef std::map<std::string, std::string> curattrs_t;
00583 
00584         TransitionHandler (xercesc::DefaultHandler& father,
00585                            T& aut,
00586                            map_t& map,
00587                            curattrs_t& attrs,
00588                            xercesc::SAX2XMLReader* parser,
00589                            XMLEq& eq);
00590         ~TransitionHandler();
00591 
00592         void startElement (const XMLCh* const uri,
00593                            const XMLCh* const localname,
00594                            const XMLCh* const qname,
00595                            const xercesc::Attributes& attrs);
00596         void endElement (const XMLCh* const uri,
00597                          const XMLCh* const localname,
00598                          const XMLCh* const qname);
00599       private:
00600         typedef typename T::series_set_elt_t    series_set_elt_t;
00601 
00602         curattrs_t&                     attrs_;
00603         map_t&                          map_;
00604 
00605         T&                              aut_;
00606         UnsupHandler                    unsuph_;
00607         LabelHandler<T>                 labelh_;
00608     };
00609 
00610     /*
00611      * TransitionsHandler class, dedicated to parse <transitions> tag.
00612      */
00613     template <typename T>
00614     class TransitionsHandler : public Handler
00615     {
00616       public:
00617         typedef std::map<std::string, hstate_t> map_t;
00618 
00619         TransitionsHandler (xercesc::DefaultHandler& father,
00620                             T& aut,
00621                             map_t& map,
00622                             xercesc::SAX2XMLReader* parser,
00623                             XMLEq& eq);
00624         ~TransitionsHandler();
00625 
00626         void startElement (const XMLCh* const uri,
00627                            const XMLCh* const localname,
00628                            const XMLCh* const qname,
00629                            const xercesc::Attributes& attrs);
00630         void endElement (const XMLCh* const uri,
00631                          const XMLCh* const localname,
00632                          const XMLCh* const qname);
00633       private:
00634         typedef typename T::series_set_elt_t    series_set_elt_t;
00635         typedef std::map<std::string, std::string> curattrs_t;
00636 
00637         curattrs_t                      curattrs_;
00638 
00639         map_t&                          map_;
00640 
00641         T&                              aut_;
00642         //TransitionHandler<typename T::set_t, typename T::value_t>     transh_;
00643         TransitionHandler<T>    transh_;
00644     };
00645 
00646 # undef HANDLER_DEF_FUN
00647   } // !xml
00648 } // !vcsn
00649 
00650 # if ! defined VCSN_USE_INTERFACE_ONLY || defined VCSN_USE_LIB
00651 #  include <vaucanson/xml/handlers.hxx>
00652 # endif // !VCSN_USE_INTERFACE_ONLY || VCSN_USE_LIB
00653 
00654 #endif // !VCSN_XML_HANDLERS_HH

Generated on Sun Jul 29 19:35:19 2007 for Vaucanson by  doxygen 1.5.2