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