regexp.hxx

Go to the documentation of this file.
00001 // regexp.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_REGEXP_HXX
00019 # define VCSN_XML_REGEXP_HXX
00020 
00021 # include <set>
00022 
00023 # include <xercesc/util/XMLString.hpp>
00024 
00025 # include <vaucanson/algebra/concept/monoid_base.hh>
00026 # include <vaucanson/algebra/concept/freemonoid_base.hh>
00027 
00028 # include <vaucanson/xml/tools.hh>
00029 
00030 namespace vcsn
00031 {
00032   namespace xml
00033   {
00034     /*
00035      * RegexpHandler
00036      */
00037     template <typename T>
00038     RegexpHandler<T>::RegexpHandler (xercesc::SAX2XMLReader* parser,
00039                                Handler& root,
00040                                T param)
00041       : Handler(parser, root),
00042         end_(eq_.typedRegExp),
00043         param_(param),
00044         lefth_(0)
00045     {
00046     }
00047 
00048     template <typename T>
00049     RegexpHandler<T>::RegexpHandler (xercesc::SAX2XMLReader* parser,
00050                                Handler& root,
00051                                T param,
00052                                XMLCh* end)
00053       : Handler(parser, root),
00054         end_(end),
00055         param_(param),
00056         lefth_(0)
00057     {
00058     }
00059 
00060     template <typename T>
00061     RegexpHandler<T>::~RegexpHandler()
00062     {
00063       if (lefth_)
00064         delete lefth_;
00065     }
00066 
00067     template <typename T>
00068     void
00069     RegexpHandler<T>::start (const XMLCh* const,
00070                                  const XMLCh* const localname,
00071                                  const XMLCh* const,
00072                                  const xercesc::Attributes&)
00073     {
00074       if (!(lefth_ = create(localname)))
00075         error::token(localname);
00076       else
00077         parser_->setContentHandler(lefth_);
00078     }
00079 
00080     template <typename T>
00081     void
00082     RegexpHandler<T>::end (const XMLCh* const,
00083                                const XMLCh* const localname,
00084                                const XMLCh* const)
00085     {
00086       using namespace xercesc;
00087       if (XMLString::equals(end_, localname))
00088       {
00089         param_ = lefth_->series();
00090         parser_->setContentHandler(&root_);
00091       }
00092       else
00093         error::token(localname);
00094     }
00095 
00096     template <typename T>
00097     T&
00098     RegexpHandler<T>::series()
00099     {
00100       return param_;
00101     }
00102 
00103     /*
00104      * StarHandler
00105      */
00106     template <typename T>
00107     StarHandler<T>::StarHandler (xercesc::SAX2XMLReader* parser,
00108                                Handler& root,
00109                                T param)
00110       : RegexpHandler<T>(parser, root, param)
00111     {
00112       end_ = eq_.star;
00113     }
00114 
00115     template <typename T>
00116     void
00117     StarHandler<T>::start (const XMLCh* const,
00118                                  const XMLCh* const localname,
00119                                  const XMLCh* const,
00120                                  const xercesc::Attributes&)
00121     {
00122       using namespace xercesc;
00123       in_++;
00124       if (in_ == 1 && (lefth_ = create(localname)))
00125         parser_->setContentHandler(lefth_);
00126       else
00127         error::token(localname);
00128     }
00129 
00130     template <typename T>
00131     void
00132     StarHandler<T>::end (const XMLCh* const,
00133                                const XMLCh* const localname,
00134                                const XMLCh* const)
00135     {
00136       using namespace xercesc;
00137       if (XMLString::equals(end_, localname))
00138       {
00139         param_ = lefth_->series().star();
00140         parser_->setContentHandler(&root_);
00141       }
00142       else
00143         error::token(localname);
00144     }
00145 
00146     /*
00147      * AtomHandler
00148      */
00149     template <typename T>
00150     AtomHandler<T>::AtomHandler (xercesc::SAX2XMLReader* parser,
00151                                Handler& root,
00152                                T param,
00153                                XMLCh* end)
00154       : RegexpHandler<T>(parser, root, param, end)
00155     {
00156     }
00157 
00158     template <typename T>
00159     void
00160     AtomHandler<T>::start (const XMLCh* const,
00161                                  const XMLCh* const localname,
00162                                  const XMLCh* const,
00163                                  const xercesc::Attributes&)
00164     {
00165       error::token(localname);
00166     }
00167 
00168     template <typename T>
00169     void
00170     AtomHandler<T>::end (const XMLCh* const,
00171                                const XMLCh* const localname,
00172                                const XMLCh* const)
00173     {
00174       using namespace xercesc;
00175       if (XMLString::equals(end_, localname))
00176         parser_->setContentHandler(&root_);
00177       else
00178         error::token(localname);
00179     }
00180 
00181     /*
00182      * WeightHandler
00183      */
00184     template <typename T>
00185     WeightHandler<T>::WeightHandler (xercesc::SAX2XMLReader* parser,
00186                                Handler& root,
00187                                T param)
00188       : RegexpHandler<T>(parser, root, param)
00189     {
00190       end_ = eq_.weight;
00191     }
00192 
00193     template <typename T>
00194     void
00195     WeightHandler<T>::start (const XMLCh* const,
00196                                  const XMLCh* const localname,
00197                                  const XMLCh* const,
00198                                  const xercesc::Attributes&)
00199     {
00200       error::token(localname);
00201     }
00202 
00203     template <typename T>
00204     void
00205     WeightHandler<T>::end (const XMLCh* const,
00206                                const XMLCh* const localname,
00207                                const XMLCh* const)
00208     {
00209       using namespace xercesc;
00210       if (XMLString::equals(end_, localname))
00211         parser_->setContentHandler(&root_);
00212       else
00213         error::token(localname);
00214     }
00215 
00216     /*
00217      * ExtMulHandler
00218      */
00219     template <typename T>
00220     ExtMulHandler<T>::ExtMulHandler (xercesc::SAX2XMLReader* parser,
00221                                Handler& root,
00222                                T param,
00223                                bool left)
00224       : RegexpHandler<T>(parser, root, param),
00225         left_(left),
00226         righth_(0)
00227     {
00228       if (left)
00229         end_ = eq_.leftExtMul;
00230       else
00231         end_ = eq_.rightExtMul;
00232     }
00233 
00234     template <typename T>
00235     ExtMulHandler<T>::~ExtMulHandler ()
00236     {
00237       delete righth_;
00238     }
00239 
00240     template <typename T>
00241     void
00242     ExtMulHandler<T>::start (const XMLCh* const,
00243                                  const XMLCh* const localname,
00244                                  const XMLCh* const,
00245                                  const xercesc::Attributes& attrs)
00246     {
00247       using namespace xercesc;
00248       if (XMLString::equals(eq_.weight, localname) &&
00249           (lefth_ = create_weight(attrs)))
00250         parser_->setContentHandler(lefth_);
00251       else if ((righth_ = create(localname)))
00252         parser_->setContentHandler(righth_);
00253       else
00254         error::token(localname);
00255     }
00256 
00257     template <typename T>
00258     void
00259     ExtMulHandler<T>::end (const XMLCh* const,
00260                                const XMLCh* const localname,
00261                                const XMLCh* const)
00262     {
00263       using namespace xercesc;
00264       if (XMLString::equals(end_, localname))
00265       {
00266         if (left_)
00267           param_ = lefth_->series() * righth_->series();
00268         else
00269           param_ = righth_->series() * lefth_->series();
00270         parser_->setContentHandler(&root_);
00271       }
00272       else
00273         error::token(localname);
00274     }
00275 
00276     /*
00277      * ProductHandler
00278      */
00279     // FIXME: should factorize
00280     template <typename T>
00281     ProductHandler<T>::ProductHandler (xercesc::SAX2XMLReader* parser,
00282                                Handler& root,
00283                                T param)
00284       : RegexpHandler<T>(parser, root, param),
00285         in_(1),
00286         righth_(0)
00287     {
00288       end_ = eq_.product;
00289     }
00290 
00291     template <typename T>
00292     ProductHandler<T>::~ProductHandler()
00293     {
00294       delete righth_;
00295     }
00296 
00297     template <typename T>
00298     void
00299     ProductHandler<T>::start (const XMLCh* const,
00300                                  const XMLCh* const localname,
00301                                  const XMLCh* const,
00302                                  const xercesc::Attributes&)
00303     {
00304       using namespace xercesc;
00305       if (in_ == 1 && (lefth_ = create(localname)))
00306         parser_->setContentHandler(lefth_);
00307       else if (in_ == 2 && (righth_ = create(localname)))
00308         parser_->setContentHandler(righth_);
00309       else
00310         error::token(localname);
00311       in_++;
00312     }
00313 
00314     template <typename T>
00315     void
00316     ProductHandler<T>::end (const XMLCh* const,
00317                                const XMLCh* const localname,
00318                                const XMLCh* const)
00319     {
00320       using namespace xercesc;
00321       if (XMLString::equals(end_, localname))
00322       {
00323         param_ = lefth_->series() * righth_->series();
00324         parser_->setContentHandler(&root_);
00325       }
00326       else if (!XMLString::equals(eq_.monGen, localname))
00327         error::token(localname);
00328     }
00329     /*
00330      * SumHandler
00331      */
00332     template <typename T>
00333     SumHandler<T>::SumHandler (xercesc::SAX2XMLReader* parser,
00334                                Handler& root,
00335                                T param)
00336       : RegexpHandler<T>(parser, root, param),
00337         in_(1),
00338         righth_(0)
00339     {
00340       end_ = eq_.sum;
00341     }
00342 
00343     template <typename T>
00344     SumHandler<T>::~SumHandler ()
00345     {
00346       delete righth_;
00347     }
00348 
00349     template <typename T>
00350     void
00351     SumHandler<T>::start (const XMLCh* const,
00352                                  const XMLCh* const localname,
00353                                  const XMLCh* const,
00354                                  const xercesc::Attributes&)
00355     {
00356       using namespace xercesc;
00357 
00358       if (in_ == 1 && (lefth_ = create(localname)))
00359         parser_->setContentHandler(lefth_);
00360       else if (in_ == 2 && (righth_ = create(localname)))
00361         parser_->setContentHandler(righth_);
00362       else
00363         error::token(localname);
00364 
00365       in_++;
00366     }
00367 
00368     template <typename T>
00369     void
00370     SumHandler<T>::end (const XMLCh* const,
00371                                const XMLCh* const localname,
00372                                const XMLCh* const)
00373     {
00374       using namespace xercesc;
00375 
00376       if (XMLString::equals(end_, localname))
00377       {
00378         param_ = lefth_->series() + righth_->series();
00379         parser_->setContentHandler(&root_);
00380       }
00381       else if (!XMLString::equals(eq_.monGen, localname))
00382         error::token(localname);
00383     }
00384 
00385     /*
00386      * MonElmtHandler
00387      */
00388     template <typename T>
00389     MonElmtHandler<T>::MonElmtHandler (xercesc::SAX2XMLReader* parser,
00390                                Handler& root,
00391                                T param)
00392       : RegexpHandler<T>(parser, root, param),
00393         mongenh_(0)
00394     {
00395       end_ = eq_.monElmt;
00396     }
00397 
00398     template <typename T>
00399     void
00400     MonElmtHandler<T>::start (const XMLCh* const,
00401                                  const XMLCh* const localname,
00402                                  const XMLCh* const,
00403                                  const xercesc::Attributes& attrs)
00404     {
00405       // FIXME: This code is very similar to the one used by
00406       // FreeMonoidHandler::start. Ultimately we should provide a way to
00407       // parse "sequences of" (to promote reusability, and ease transcription
00408       // from the XSD to parser code).
00409       using namespace xercesc;
00410 
00411       if (XMLString::equals(eq_.monGen, localname))
00412       {
00413         typedef typename T::set_t::monoid_t monoid_t;
00414         typedef T actee_t;
00415 
00416         // When we have a monGen, we will concatenate param_ with it.
00417         monGenAction<actee_t> action(param_);
00418 
00419         // Delete the old handler.
00420         if (mongenh_)
00421           delete mongenh_;
00422 
00423         // Choose statically the kind of generator.
00424         if (algebra::letter_traits<typename monoid_t::alphabet_t::letter_t>::kind() == "simple")
00425         {
00426           const XMLCh* value = tools::get_attribute(attrs, "value");
00427           mongenh_ = new monGenHandler<monoid_t, actee_t>(parser_, *this, action, value);
00428         }
00429         else
00430           mongenh_ = new monGenTupleHandler<monoid_t, actee_t>(parser_, *this, action);
00431 
00432         // Setup the new handler.
00433         parser_->setContentHandler(mongenh_);
00434       }
00435       else
00436         error::token(localname);
00437     }
00438 
00439     template <typename T>
00440     void
00441     MonElmtHandler<T>::end (const XMLCh* const,
00442                                const XMLCh* const localname,
00443                                const XMLCh* const)
00444     {
00445       using namespace xercesc;
00446 
00447       if (XMLString::equals(end_, localname))
00448       {
00449         // We are done with the "parent", so delete remaining data.
00450         if (mongenh_)
00451           delete mongenh_;
00452 
00453         // Go up one level.
00454         parser_->setContentHandler(&root_);
00455       }
00456       else if (!XMLString::equals(eq_.monGen, localname))
00457         error::token(localname);
00458     }
00459 
00460   } // !xml
00461 } // !vcsn
00462 
00463 #endif // !VCSN_XML_REGEXP_HXX

Generated on Thu Oct 9 20:22:41 2008 for Vaucanson by  doxygen 1.5.1