fmp.hxx

00001 // fmp.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_CONTEXTS_FMP_HXX
00019 # define VCSN_XML_CONTEXTS_FMP_HXX
00020 
00021 # include <vaucanson/xml/builders.hh>
00022 
00023 namespace vcsn
00024 {
00025   namespace xml
00026   {
00030     template <typename T>
00031     FreeMonoidProductHandler<T>::FreeMonoidProductHandler (xercesc::SAX2XMLReader* parser,
00032                                  Handler& root,
00033                                  T& monoid)
00034       : Handler(parser, root),
00035         monoid_(monoid),
00036         unsuph_(parser, *this),
00037         first_(true)
00038     {
00039     }
00040 
00041     template <typename T>
00042     void
00043     FreeMonoidProductHandler<T>::start (const XMLCh* const,
00044                                  const XMLCh* const localname,
00045                                  const XMLCh* const,
00046                                  const xercesc::Attributes& attrs)
00047     {
00048       if (xercesc::XMLString::equals(eq_.monoid, localname))
00049       {
00050         if (first_)
00051         {
00052           monoidh_ = builders::create_monoidh(monoid_.first_monoid(), attrs, parser_, *this);
00053           first_ = false;
00054         }
00055         else
00056         {
00057           delete monoidh_;
00058           monoidh_ = builders::create_monoidh(monoid_.second_monoid(), attrs, parser_, *this);
00059         }
00060         parser_->setContentHandler(monoidh_);
00061       }
00062       else if (xercesc::XMLString::equals(eq_.writingData, localname))
00063         parser_->setContentHandler(&unsuph_);
00064       else
00065         error::token(localname);
00066     }
00067 
00068     template <typename T>
00069     void
00070     FreeMonoidProductHandler<T>::end (const XMLCh* const,
00071                                const XMLCh* const localname,
00072                                const XMLCh* const)
00073     {
00074       delete monoidh_;
00075       if (xercesc::XMLString::equals(eq_.monoid, localname))
00076         parser_->setContentHandler(&root_);
00077       else
00078         error::token(localname);
00079     }
00080 
00081 
00082     namespace builders
00083     {
00084       TParamFMP
00085       typename FMPtype::monoid_t*
00086       create_monoid (FMPtype& param,
00087                      const XMLCh* const localname,
00088                      const xercesc::Attributes& attrs)
00089       {
00090         typename FMPtype::monoid_t::first_monoid_t::alphabet_t  at1;
00091         typename FMPtype::monoid_t::second_monoid_t::alphabet_t at2;
00092         typename FMPtype::monoid_t::first_monoid_t      md1(at1);
00093         typename FMPtype::monoid_t::second_monoid_t     md2(at2);
00094         typedef typename FMPtype::monoid_t              monoid_t;
00095 
00096         monoid_t* monoid = new monoid_t(md1, md2);
00097         builders::check_monoid_consistency(param, localname, attrs);
00098         return monoid;
00099       }
00100 
00101       template <typename M1, typename M2>
00102       Handler*
00103       create_monoidh (vcsn::algebra::FreeMonoidProduct<M1, M2>& monoid,
00104                       const xercesc::Attributes&,
00105                       xercesc::SAX2XMLReader* parser,
00106                       Handler& root)
00107       {
00108         typedef typename vcsn::algebra::FreeMonoidProduct<M1, M2> monoid_t;
00109         return new FreeMonoidProductHandler<monoid_t>(parser, root, monoid);
00110       }
00111     } // !builders
00112 
00113     /*
00114      * ProdMonElmtHandler
00115      */
00116     template <typename T>
00117     ProdMonElmtHandler<T>::ProdMonElmtHandler (xercesc::SAX2XMLReader* parser,
00118                                Handler& root,
00119                                T param)
00120       : RegexpHandler<T>(parser, root, param),
00121         in_(1),
00122         count_(1),
00123         m1_(param.structure().monoid().first_monoid()),
00124         m2_(param.structure().monoid().second_monoid())
00125     {
00126       end_ = eq_.monElmt;
00127     }
00128 
00129     template <typename T>
00130     void
00131     ProdMonElmtHandler<T>::start (const XMLCh* const,
00132                                  const XMLCh* const localname,
00133                                  const XMLCh* const,
00134                                  const xercesc::Attributes& attrs)
00135     {
00136       using namespace xercesc;
00137       if (XMLString::equals(eq_.monElmt, localname))
00138       {
00139         in_++;
00140         count_++;
00141         if (in_ > 2)
00142           error::token(localname);
00143       }
00144       else if (XMLString::equals(eq_.one, localname) && (in_ == 1))
00145       {
00146         in_++;
00147         count_++;
00148         if (count_ == 2)
00149           m1_ = algebra::identity_as<typename T::monoid_elt_t::first_monoid_elt_value_t>
00150 	    ::of(param_.structure().monoid().first_monoid());
00151         else if (count_ == 3)
00152           m2_ = algebra::identity_as<typename T::monoid_elt_t::second_monoid_elt_value_t>
00153 	    ::of(param_.structure().monoid().second_monoid());
00154         else
00155           error::token(localname);
00156       }
00157       else if (XMLString::equals(eq_.monGen, localname))
00158       {
00159         const std::string val(xmlstr(tools::get_attribute(attrs, "value")));
00160         std::set<char> escaped;
00161         std::string::const_iterator i = val.begin();
00162         if (in_ == 2 && count_ == 2)
00163         {
00164           if (!parse_word(m1_, val, i, escaped))
00165             error::attrs(localname, "value", val);
00166         }
00167         else if (in_ == 2 && count_ == 3)
00168         {
00169           if (!parse_word(m2_, val, i, escaped))
00170             error::attrs(localname, "value", val);
00171         }
00172         else
00173           error::token(localname);
00174       }
00175       else
00176         error::token(localname);
00177     }
00178 
00179     template <typename T>
00180     void
00181     ProdMonElmtHandler<T>::end (const XMLCh* const,
00182                                const XMLCh* const localname,
00183                                const XMLCh* const)
00184     {
00185       using namespace xercesc;
00186       if (XMLString::equals(end_, localname))
00187       {
00188         if (in_ == 1 && count_ == 3)
00189         {
00190           // FIXME awful... but didn't find an easier way.
00191           typename T::monoid_elt_value_t m(m1_.value(), m2_.value());
00192           typename T::semiring_elt_value_t w =
00193             algebra::identity_as<typename T::semiring_elt_value_t>::of(param_.structure().semiring()).value();
00194           param_.assoc(m, w);
00195           parser_->setContentHandler(&root_);
00196         }
00197         in_--;
00198       }
00199       else if (XMLString::equals(eq_.one, localname))
00200         in_--;
00201       else if (!XMLString::equals(eq_.monGen, localname))
00202         error::token(localname);
00203     }
00204 
00205 
00206     namespace builders
00207     {
00208       SParamFMP
00209       RegexpHandler<FMPseries >*
00210       create_monElmth(xercesc::SAX2XMLReader* parser,
00211                       RegexpHandler<FMPseries >& root,
00212                       FMPseries param)
00213       {
00214         return new ProdMonElmtHandler<FMPseries >(parser, root,
00215             algebra::zero_as<typename FMPseries::value_t>::of(param.structure()));
00216       }
00217     } // !builders
00218 
00222     namespace builders
00223     {
00224       TParamFMP
00225       void
00226       check_monoid_consistency (FMPtype&,
00227                                 const XMLCh* const localname,
00228                                 const xercesc::Attributes& attrs)
00229       {
00230         std::string val(xmlstr(tools::get_attribute(attrs, "type")));
00231         if (val != "product")
00232           error::attrs(localname, "type", val);
00233         val = xmlstr(tools::get_attribute(attrs, "prodDim"));
00234         if (val != "2")
00235           error::attrs(localname, "prodDim", val);
00236       };
00237     } // !builders
00238 
00242     namespace builders
00243     {
00244       TParamFMP
00245       void
00246       create_monoid_node(const FMPtype& aut,
00247                          xercesc::DOMDocument* doc,
00248                            xercesc::DOMElement* root)
00249       {
00250         xercesc::DOMElement* node = tools::create_element(doc, "monoid");
00251         tools::set_attribute(node, "type", "product");
00252         tools::set_attribute(node, "prodDim", "2");
00253         root->appendChild(node);
00254         xercesc::DOMElement* first = tools::create_element(doc, "monoid");
00255         tools::set_attribute(first, "type", "free");
00256         tools::set_attribute(first, "genDescrip", "enum");
00257         tools::set_attribute(first, "genKind", algebra::letter_traits<typename FMPtype::monoid_t::first_monoid_t::alphabet_t::letter_t>::kind());
00258         node->appendChild(first);
00259         typedef typename FMPtype::monoid_t::first_monoid_t::alphabet_t::const_iterator first_alphabet_iterator;
00260         for_all_letters_(first_, l, aut.structure().series().monoid().first_monoid().alphabet())
00261         {
00262           std::ostringstream letter;
00263           xercesc::DOMElement* gen = tools::create_element(doc, "monGen");
00264           letter << *l;
00265           tools::set_attribute(gen, "value", letter.str());
00266           first->appendChild(gen);
00267         }
00268         tools::set_attribute(first, "genSort", get_monoid_gen_sort(*(aut.structure().series().monoid().first_monoid().alphabet().begin())));
00269         xercesc::DOMElement* second = tools::create_element(doc, "monoid");
00270         tools::set_attribute(second, "type", "free");
00271         tools::set_attribute(second, "genDescrip", "enum");
00272         tools::set_attribute(second, "genKind", algebra::letter_traits<typename FMPtype::monoid_t::second_monoid_t::alphabet_t::letter_t>::kind());
00273         node->appendChild(second);
00274         typedef typename FMPtype::monoid_t::second_monoid_t::alphabet_t::const_iterator second_alphabet_iterator;
00275         for_all_letters_(second_, l, aut.structure().series().monoid().second_monoid().alphabet())
00276         {
00277           std::ostringstream letter;
00278           xercesc::DOMElement* gen = tools::create_element(doc, "monGen");
00279           letter << *l;
00280           tools::set_attribute(gen, "value", letter.str());
00281           second->appendChild(gen);
00282         }
00283         tools::set_attribute(second, "genSort", get_monoid_gen_sort(*(aut.structure().series().monoid().second_monoid().alphabet().begin())));
00284       }
00285 
00286       /* FIXME there should not be 2 but one function here... however,
00287       ** when we add a template T instead of int the function is not called
00288       ** anymore... there is a probleme in the dispatch */
00289       template <>
00290       void
00291       create_monElmt_node(const std::pair<std::string, std::string>& m,
00292                          xercesc::DOMDocument* doc,
00293                            xercesc::DOMElement* root)
00294       {
00295         xercesc::DOMElement* node;
00296         if (m.first.empty() && m.second.empty())
00297           node = tools::create_element(doc, "one");
00298         else
00299         {
00300           node = tools::create_element(doc, "monElmt");
00301           create_monElmt_node(m.first, doc, node);
00302           create_monElmt_node(m.second, doc, node);
00303         }
00304         root->appendChild(node);
00305       }
00306 
00307       template <>
00308       void
00309       create_monElmt_node(const std::pair<std::basic_string<int>, std::basic_string<int> >& m,
00310                          xercesc::DOMDocument* doc,
00311                            xercesc::DOMElement* root)
00312       {
00313         xercesc::DOMElement* node;
00314         if (m.first.empty() && m.second.empty())
00315           node = tools::create_element(doc, "one");
00316         else
00317         {
00318           node = tools::create_element(doc, "monElmt");
00319           create_monElmt_node(m.first, doc, node);
00320           create_monElmt_node(m.second, doc, node);
00321         }
00322         root->appendChild(node);
00323       }
00324     } // !builders
00325   } // !xml
00326 } // !vcsn
00327 
00328 #endif // !VCSN_XML_CONTEXTS_FMP_HXX

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