Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

xml/tools.hxx

00001 // tools.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2005, 2006 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_TOOLS_HXX
00019 # define VCSN_XML_TOOLS_HXX
00020 
00032 namespace vcsn
00033 {
00034   namespace xml
00035   {
00036 
00037     namespace tools
00038     {
00039 
00040     /*--------------------------------------.
00041     | Extract information about the types.  |
00042     `--------------------------------------*/
00043 
00049       // Default.
00050       template <class S, class T>
00051       const char* get_semiring_set(const S&, const T&)
00052       { return "undefined"; }
00053 
00054 # define GET_SEMIRING_SET(T, Value)                     \
00055       template <class S>                                \
00056       const char* get_semiring_set(const S&, const T&)  \
00057       { return Value; }
00058 
00059       GET_SEMIRING_SET(bool, "B")
00060       GET_SEMIRING_SET(double, "R")
00061       GET_SEMIRING_SET(float, "R")
00062       GET_SEMIRING_SET(int, "Z")
00063 # undef GET_SEMIRING_SET
00064 
00065       // Transducer on P(B*).
00066       template <class S, class U, class V>
00067       const char* get_semiring_set(const S&,
00068                                    const vcsn::rat::exp<U, V>&)
00069       { return "ratSeries"; }
00070 
00071 
00072 
00073       // Deals with the "operation" attribute of <semiring> tag.
00074       // Default.
00075       template <class S>
00076       const char* get_semiring_operations(const S&)
00077       { return "undefined"; }
00078 
00079 # define GET_SEMIRING_OPERATIONS(S, Value)                      \
00080       template <>                                               \
00081       inline const char* get_semiring_operations<S>(const S&)   \
00082       { return Value; }
00083 
00084       GET_SEMIRING_OPERATIONS(vcsn::algebra::NumericalSemiring,
00085                               "numerical")
00086       GET_SEMIRING_OPERATIONS(vcsn::z_max_plus_automaton::semiring_t,
00087                               "tropicalMax")
00088       GET_SEMIRING_OPERATIONS(vcsn::z_min_plus_automaton::semiring_t,
00089                               "tropicalMin")
00090 # undef GET_SEMIRING_OPERATIONS
00091 
00092 
00093 
00094       // Deal with the "type" attribute of <monoid> tag.
00095       // Default.
00096       template <class S>
00097       const char* get_monoid_type(const S&)
00098       { return "undefined"; }
00099 
00100       template <class S>
00101       const char* get_monoid_type(const vcsn::algebra::FreeMonoid<S>&)
00102       { return "free"; }
00103 
00104       template <class S1, class S2>
00105       const char*
00106       get_monoid_type(const vcsn::algebra::FreeMonoidProduct<S1, S2>&)
00107       { return "product"; }
00108 
00109 
00110     /*---------.
00111     | Automata |
00112     `---------*/
00113 
00114     // Useful abbreviation to circumvent the impossibility to define
00115     // parametric typedefs.
00116 # define AUTtype                                \
00117     Element<Automata<S>, T>
00118 # define TRANStype                              \
00119     Element<Transducer<S>, T>
00120 # define FMPtype                                                          \
00121     Element<                                                              \
00122       Automata<                                                           \
00123       vcsn::algebra::Series<S, vcsn::algebra::FreeMonoidProduct<M1, M2> > \
00124       >, T                                                                \
00125     >
00126 
00127       inline
00128       void
00129       set_attribute (xercesc::DOMElement* e, std::string k, std::string v)
00130       {
00131         e->setAttribute (transcode(k), transcode(v));
00132       }
00133 
00134       inline
00135       void
00136       xset_attribute (xercesc::DOMElement* e, std::string k, std::string v)
00137       {
00138         if (v != "")
00139           set_attribute (e, k, v);
00140       }
00141 
00142 
00143       // Add the label as a string attribute.
00144       template <class S, class T, class U>
00145       void add_label(xercesc::DOMElement* elt,
00146                      const Element<S, T>&,
00147                      const U& series)
00148       {
00149         xset_attribute(elt, "label", get_label(series));
00150       }
00151 
00152       // Add the label as an xml node
00153       template <class S, class T, class U>
00154       void add_xml_label(xercesc::DOMDocument* doc,
00155                          xercesc::DOMElement* elt,
00156                          const Element<S, T>& a,
00157                          const U& series)
00158       {
00159         typedef Element<S,T> automaton_t;
00160         typedef typename
00161           rat::exp<typename automaton_t::monoid_elt_value_t,
00162           typename automaton_t::semiring_elt_value_t> krat_exp_impl_t;
00163         typedef Element<typename automaton_t::series_set_t, krat_exp_impl_t> krat_exp_t;
00164 
00165 
00166         std::string label = get_label(series);
00167         if (label.size())
00168         {
00169           krat_exp_t res (a.structure().series());
00170           parse(label, res);
00171           elt->appendChild(res.value().xml_tree(doc, "label"));
00172         }
00173       }
00174 
00175 
00176       /*----------------.
00177       | FMP Transducers |
00178       `----------------*/
00179 
00180       // Add the label as a string attribute
00181       template <class S, class T, class U, class M1, class M2>
00182       void add_label(xercesc::DOMElement* elt,
00183                      const FMPtype&,
00184                      const U& series)
00185       {
00186         if (series.supp().size() > 1)
00187         {
00188           xset_attribute(elt, "label", get_label(series));
00189         }
00190         else
00191         {
00192           std::string in_word = get_label((*(series.supp().begin())).first);
00193           std::string out_word = get_label((*(series.supp().begin())).second);
00194           std::string mult = get_label(series.get(*(series.supp().begin())));
00195           std::string out;
00196           if (mult.size())
00197             out = mult;
00198           if (out != "" && out_word.size())
00199             out += " ";
00200           if (out_word.size())
00201             out += out_word;
00202 
00203           xset_attribute(elt, "in", in_word);
00204           xset_attribute(elt, "out", out);
00205         }
00206       }
00207 
00208       // Add the label as an xml node
00209       template <class S, class T, class U, class M1, class M2>
00210       void add_xml_label(xercesc::DOMDocument* doc,
00211                          xercesc::DOMElement* elt,
00212                          const FMPtype& a,
00213                          const U& series)
00214       {
00215         std::string out;
00216         if (series.supp().size() > 1)
00217         {
00218           xset_attribute(elt, "label", get_label(series));
00219         }
00220         else
00221         {
00222           std::string in_word = get_label((*(series.supp())).first);
00223           std::string out_word = get_label((*(series.supp())).second);
00224           std::string mult = get_label(series.get(*(series.supp().begin())));
00225 
00226           if (in_word.size() && in_word != "1")
00227           {
00228             using namespace vcsn::r_automaton;
00229             automaton_t bin = make_automaton(
00230               a.structure().series().monoid().first_monoid().alphabet());
00231             rat_exp_t res_in(bin.structure().series());
00232             parse(in_word, res_in);
00233             elt->appendChild(res_in.value().xml_tree(doc, "in"));
00234           }
00235           if (out_word.size() && out_word != "1")
00236           {
00237             using namespace vcsn::r_automaton;
00238             automaton_t bout = make_automaton(
00239               a.structure().series().monoid().second_monoid().alphabet());
00240             rat_exp_t res_out(bout.structure().series());
00241             parse(out_word, res_out);
00242             xercesc::DOMElement* out_node =
00243               res_out.value().xml_tree(doc, "out");
00244             xset_attribute(out_node, "weight", mult);
00245             elt->appendChild(out_node);
00246           }
00247         }
00248       }
00249 
00250 
00251       /*---------------------.
00252       | Transducers on P(B*) |
00253       `---------------------*/
00254 
00255       // Add the label as a string attribute
00256       template <class S, class T, class U>
00257       void add_label(xercesc::DOMElement* elt,
00258                      const Element<Transducer<S>, T>&,
00259                      const U& series)
00260       {
00261         std::string in = get_label(*(series.supp().begin()));
00262         std::string out = get_label(series.get(*(series.supp().begin())));
00263 
00264         if (in.size() && in != "1")
00265           set_attribute(elt, "in", in);
00266         if (out.size() && out != "1")
00267           set_attribute(elt, "out", out);
00268       }
00269 
00270       // Add the label as an xml node
00271       template <class S, class T, class U>
00272       void add_xml_label(xercesc::DOMDocument* doc,
00273                          xercesc::DOMElement* elt,
00274                          const Element<Transducer<S>, T>& a,
00275                          const U& series)
00276       {
00277         std::string in = get_label(*(series.supp().begin()));
00278         std::string out = get_label(series.get(*(series.supp().begin())));
00279 
00280         if (in.size() && in != "1")
00281         {
00282           using namespace vcsn::r_automaton;
00283           automaton_t bin = make_automaton(
00284             a.structure().series().monoid().alphabet());
00285           rat_exp_t res_in(bin.structure().series());
00286           parse(in, res_in);
00287           elt->appendChild(res_in.value().xml_tree(doc, "in"));
00288         }
00289         if (out.size() && out != "1")
00290         {
00291           using namespace vcsn::r_automaton;
00292           automaton_t bout = make_automaton(
00293             a.structure().series().semiring().monoid().alphabet());
00294           rat_exp_t res_out(bout.structure().series());
00295           parse(out, res_out);
00296           elt->appendChild(res_out.value().xml_tree(doc, "out"));
00297         }
00298       }
00299 
00300 
00301       template <class L>
00302       const std::string get_label(const L& l)
00303       {
00304         std::ostringstream os;
00305         os << l;
00306         return os.str();
00307       }
00308 
00309       inline const std::string get_label(const bool& l)
00310       {
00311         if (l)
00312           return "";
00313         return "0";
00314       }
00315 
00316 
00317       inline const std::string get_label(const std::pair<std::string, std::string>& l)
00318       {
00319         std::ostringstream os;
00320         os << "(" << l.first << ", " << l.second << ")";
00321         return os.str().c_str();
00322       }
00323 
00324       template <class S, class Series>
00325       const std::string get_label(const Element<Series,
00326                                   vcsn::algebra::polynom<S, bool> >& l)
00327       {
00328         std::ostringstream os;
00329         os << l;
00330         if (os.str() == "1")
00331           return "";
00332         return os.str();
00333       }
00334 
00335 
00336       template <class A>
00337       void create_alphabet(const A& alphabet, xercesc::DOMDocument* doc,
00338                            xercesc::DOMElement* root)
00339       {
00340         typedef typename A::const_iterator alphabet_iterator;
00341         for_all_letters(l, alphabet)
00342         {
00343           std::ostringstream letter;
00344           xercesc::DOMElement* gen =
00345             doc->createElement(transcode("generator"));
00346           letter << *l;
00347           set_attribute(gen, "value", letter.str().c_str());
00348           root->appendChild(gen);
00349         }
00350       }
00351 
00352       template <class M>
00353       xercesc::DOMElement* create_monoid(const M& monoid,
00354                                          xercesc::DOMDocument* doc,
00355                                          xercesc::DOMElement* elt)
00356       {
00357         xercesc::DOMElement* m = doc->createElement(transcode("monoid"));
00358         set_attribute(m, "type", get_monoid_type(monoid));
00359         set_attribute(m, "generators", "letters");
00360         elt->appendChild(m);
00361 
00362         return m;
00363       }
00364 
00365       template <class A, class S>
00366       xercesc::DOMElement* create_semiring(const A&,
00367                                            const S& semiring,
00368                                            xercesc::DOMDocument* doc,
00369                                            xercesc::DOMElement* elt)
00370       {
00371         typedef typename A::series_set_elt_t::semiring_elt_t::value_t value_t;
00372 
00373         xercesc::DOMElement* s = doc->createElement(transcode("semiring"));
00374 
00375         if (get_semiring_set(semiring, value_t()) != "ratSeries")
00376           set_attribute(s, "operations", get_semiring_operations(semiring));
00377         set_attribute(s, "set", get_semiring_set(semiring, value_t()));
00378         elt->appendChild(s);
00379 
00380         return s;
00381       }
00382 
00383       template <class S, class T>
00384       xercesc::DOMElement*
00385       create_semiring(const Element<Transducer<S>, T>&,
00386                       const vcsn::algebra::NumericalSemiring& semiring,
00387                       xercesc::DOMDocument* doc,
00388                       xercesc::DOMElement* elt)
00389       {
00390         typedef typename
00391           Element<Transducer<S>, T>::series_set_elt_t::semiring_elt_t::semiring_elt_t::value_t value_t;
00392 
00393         xercesc::DOMElement* s = doc->createElement(transcode("semiring"));
00394         set_attribute(s, "operations", "numerical");
00395         set_attribute(s, "set", get_semiring_set(semiring, value_t()));
00396         elt->appendChild(s);
00397 
00398         return s;
00399       }
00400 
00401       template <class T>
00402       Element<typename T::series_set_t, rat::exp<typename T::monoid_elt_value_t,
00403                                                  typename T::semiring_elt_value_t> >
00404       get_rec_xml_series(xercesc::DOMNode* n, T& aut)
00405       {
00406         typedef  rat::exp<typename T::monoid_elt_value_t,
00407           typename T::semiring_elt_value_t> krat_exp_impl_t;
00408         typedef Element<typename T::series_set_t, krat_exp_impl_t> krat_exp_t;
00409         krat_exp_t krat_exp (aut.structure().series());
00410         typename T::semiring_elt_value_t weight =
00411           typename T::semiring_elt_value_t();
00412 
00413         xercesc::DOMElement* element_n;
00414         xercesc::DOMNode* ntmp;
00415 
00416         if (!n)
00417           return vcsn::algebra::identity_as<krat_exp_impl_t>::
00418 	    of(krat_exp.structure());
00419 
00420         for (; n ; n = n->getNextSibling())
00421         {
00422           if (n->getNodeType() == xercesc::DOMNode::ELEMENT_NODE)
00423           {
00424             element_n = (static_cast<xercesc::DOMElement*>(n));
00425 
00426             if ((xml2str(n->getNodeName()) == "label") ||
00427                 (xml2str(n->getNodeName()) == "in") ||
00428                 (xml2str(n->getNodeName()) == "out"))
00429               return get_rec_xml_series(n->getFirstChild(), aut);
00430 
00431             // Add weight
00432             if (element_n->hasAttribute(transcode("weight")))
00433             {
00434               std::stringstream ss;
00435               ss << xml2str(element_n->getAttribute(transcode("weight")));
00436               ss >> weight;
00437             }
00438 
00439             // Explore the terms of a sum
00440             if (xml2str(n->getNodeName()) == "sum")
00441             {
00442               ntmp = n->getFirstChild()->getNextSibling();
00443               krat_exp = get_rec_xml_series(ntmp, aut);
00444               if (ntmp)
00445                 for (ntmp = ntmp->getNextSibling();
00446                      ntmp && ntmp->getNextSibling();
00447                      ntmp = ntmp->getNextSibling()->getNextSibling())
00448                   krat_exp += get_rec_xml_series(ntmp, aut);
00449               return (weight != typename T::semiring_elt_t ())?
00450                 typename T::semiring_elt_t (weight) * krat_exp : krat_exp;
00451             }
00452 
00453             // Explore the terms of a product
00454             if (xml2str(n->getNodeName()) == "product")
00455             {
00456               ntmp = n->getFirstChild()->getNextSibling();
00457               krat_exp = get_rec_xml_series(ntmp, aut);
00458               if (ntmp)
00459                 for (ntmp = ntmp->getNextSibling();
00460                      ntmp && ntmp->getNextSibling();
00461                      ntmp = ntmp->getNextSibling()->getNextSibling())
00462                   krat_exp *= get_rec_xml_series(ntmp, aut);
00463               return (weight != typename T::semiring_elt_t ())?
00464                 typename T::semiring_elt_t (weight) * krat_exp : krat_exp;
00465             }
00466 
00467             // Word, zero or identity
00468             if (xml2str(n->getNodeName()) == "word")
00469               krat_exp = typename T::monoid_elt_t (
00470                 krat_exp.structure().monoid(),
00471                 xml2str(element_n->getAttribute(transcode("value"))));
00472 
00473             if (xml2str(n->getNodeName()) == "zero")
00474               krat_exp = vcsn::algebra::
00475 		zero_as<krat_exp_impl_t>::of(krat_exp.structure());
00476 
00477             if (xml2str(n->getNodeName()) == "identity")
00478               krat_exp =  vcsn::algebra::
00479 		identity_as<krat_exp_impl_t>::of(krat_exp.structure());
00480 
00481             // Add star
00482             if (xml2str(n->getNodeName()) == "star")
00483               krat_exp = (get_rec_xml_series(n->getFirstChild(), aut)).star();
00484 
00485             return (weight != typename T::semiring_elt_t ())?
00486               typename T::semiring_elt_t (weight) * krat_exp : krat_exp;
00487           }
00488         }
00489         return krat_exp;
00490       }
00491 
00492 
00504       template <class T>
00505       typename T::series_set_elt_t
00506       get_series(xercesc::DOMElement* node, T& aut)
00507       {
00508         typedef typename
00509           rat::exp<typename T::monoid_elt_value_t,
00510           typename T::semiring_elt_value_t> krat_exp_impl_t;
00511         typedef Element<typename T::series_set_t, krat_exp_impl_t> krat_exp_t;
00512         krat_exp_t res (aut.structure().series());
00513         xercesc::DOMNode* n = node->getFirstChild();
00514         if (n && n->getNextSibling()
00515             && (xml2str(n->getNextSibling()->getNodeName()) == "label"))
00516           res = get_rec_xml_series(n, aut);
00517         if (res == vcsn::algebra::
00518             zero_as<krat_exp_impl_t>::of(res.structure()))
00519         {
00520           if (xml2str(node->getAttribute(transcode("label"))) == "")
00521             return
00522               vcsn::algebra::identity_as<typename T::series_set_elt_t::value_t>
00523 	      ::of(aut.structure().series());
00524           else
00525             parse(xml2str(node->getAttribute(transcode("label"))), res);
00526         }
00527         return res;
00528       }
00529 
00530 
00531       template <class S, class T, class M1, class M2>
00532       typename FMPtype::series_set_elt_t
00533       get_series(xercesc::DOMElement* node, FMPtype& a)
00534       {
00535         typename FMPtype::series_set_elt_t res(a.structure().series());
00536 
00537         parse_label(node, a,
00538                     a.structure().series().monoid().first_monoid().alphabet(),
00539                     a.structure().series().monoid().second_monoid().alphabet(),
00540                     res);
00541 
00542         return res;
00543       }
00544 
00545 
00546       template <class S, class T>
00547       typename TRANStype::series_set_elt_t
00548       get_series(xercesc::DOMElement* node,
00549                  TRANStype& a)
00550       {
00551         typename TRANStype::series_set_elt_t res(a.structure().series());
00552 
00553         parse_label(node, a,
00554                     a.structure().series().monoid().alphabet(),
00555                     a.structure().series().semiring().monoid().alphabet(),
00556                     res);
00557 
00558         return res;
00559       }
00560 
00561 
00562       template <class S, class T, class E1, class E2, class R>
00563       void assoc_exp(TRANStype& a, E1& i_exp, E2& o_exp, R& res,
00564                      bool i_res, bool o_res)
00565       {
00566         typedef typename TRANStype::monoid_elt_t::value_t md_value_t;
00567         typedef typename TRANStype::semiring_elt_t::value_t sg_value_t;
00568         typename TRANStype::monoid_elt_t m(a.structure().series().monoid());
00569 
00570         if (! i_res && i_exp.supp().size())
00571           m = *(i_exp.supp().begin());
00572         else
00573           m = vcsn::algebra::identity_as<md_value_t>
00574 	    ::of(a.structure().series().monoid());
00575 
00576         if (! o_res && ! o_exp.supp().size())
00577           res.assoc(m,
00578                     vcsn::algebra::identity_as<sg_value_t>
00579                     ::of(a.structure().series().semiring()));
00580         else
00581           res.assoc(m, o_exp);
00582       }
00583 
00584 
00585       template <class S, class T, class M1, class M2,
00586                 class E1, class E2, class R>
00587       void assoc_exp(FMPtype& a, E1& i_exp, E2& o_exp, R& res,
00588                      bool i_res, bool o_res)
00589       {
00590         typename FMPtype::monoid_elt_t m(a.structure().series().monoid());
00591         typename FMPtype::monoid_elt_t::first_monoid_elt_value_t m1("");
00592         typename FMPtype::monoid_elt_t::second_monoid_elt_value_t m2("");
00593         typename FMPtype::semiring_elt_value_t sem = 1;
00594         typedef typename FMPtype::monoid_elt_t::first_monoid_elt_t::value_t
00595           md_value_t;
00596         typedef typename FMPtype::semiring_elt_value_t sg_value_t;
00597 
00598         if (! i_res && i_exp.supp().size())
00599           m1 = *(i_exp.supp().begin());
00600         else
00601           m1 = vcsn::algebra::identity_as<md_value_t>
00602 	    ::of(a.structure().series().monoid().first_monoid()).value();
00603 
00604         if (! o_res && o_exp.supp().size())
00605         {
00606           m2 = *(o_exp.supp().begin());
00607           sem = o_exp.get(m2);
00608         }
00609         else
00610         {
00611           m2 = vcsn::algebra::identity_as<md_value_t>
00612 	    ::of(a.structure().series().monoid().second_monoid()).value();
00613           sem = vcsn::algebra::identity_as<sg_value_t>
00614 	    ::of(a.structure().series().semiring()).value();
00615         }
00616         m = std::make_pair(m1, m2);
00617         res.assoc(m, sem);
00618       }
00619 
00620 
00621       template <class A, class A1, class A2, class S>
00622       void parse_label(xercesc::DOMElement* node, A& a,
00623                        const A1& alphabet1, const A2& alphabet2, S& res)
00624       {
00625         using namespace vcsn::r_automaton;
00626         automaton_t bin = make_automaton(alphabet1);
00627         automaton_t bout = make_automaton(alphabet2);
00628         rat_exp_t i_exp(bin.structure().series());
00629         rat_exp_t o_exp(bout.structure().series());
00630         std::string in, out;
00631 
00632         std::pair<bool, std::string> i_res;
00633         std::pair<bool, std::string> o_res;
00634 
00635         xercesc::DOMNode* n = node->getFirstChild();
00636 
00637         if (n)
00638           for (; n; n = n->getNextSibling())
00639           {
00640             if (xml2str(n->getNodeName()) == "in" && n->getFirstChild())
00641               i_exp = get_rec_xml_series(n->getFirstChild(), bin);
00642             if (xml2str(n->getNodeName()) == "out" && n->getFirstChild())
00643               o_exp = get_rec_xml_series(n->getFirstChild(), bout);
00644           }
00645         // No expression tag
00646         else
00647         {
00648           if (node->hasAttribute(transcode("label")))
00649           {
00650             std::string label = xml2str(node->getAttribute(transcode("label")));
00651             unsigned int pos = label.find("|");
00652             if (pos != std::string::npos)
00653             {
00654               in = label.substr(0, pos);
00655               out = label.substr(pos + 1);
00656               i_res = parse(in, i_exp);
00657               o_res = parse(out, o_exp);
00658             }
00659             else
00660               i_res = parse(label, i_exp);
00661             if (node->hasAttribute(transcode("weight")))
00662               o_res = parse(xml2str(node->getAttribute(transcode("weight"))),
00663                             o_exp);
00664           }
00665           // No expression tag, no label attribute.
00666           else
00667           {
00668             if (node->hasAttribute(transcode("in")))
00669               i_res = parse(xml2str(node->getAttribute(transcode("in"))),
00670                             i_exp);
00671             if (node->hasAttribute(transcode("out")))
00672               o_res = parse(xml2str(node->getAttribute(transcode("out"))),
00673                             o_exp);
00674           }
00675         }
00676         assoc_exp(a, i_exp, o_exp, res, i_res.first, o_res.first);
00677       }
00678 
00679 
00690       template <class U, class V>
00691       void insert_letter(Element<vcsn::algebra::AlphabetSet<U>, V>&,
00692                          const std::string&)
00693       {
00694         FAIL("No available implementation for this type of letter");
00695       }
00696 
00697       template <class V>
00698       void insert_letter(Element<vcsn::algebra::AlphabetSet<char>, V>& a,
00699                          const std::string& str)
00700       {
00701         a.insert(str[0]);
00702       }
00703 
00704 
00716       template <class T, class U>
00717       void ensure_semiring_type(const xercesc::DOMElement* node,
00718                                 const T&, const U& param)
00719       {
00720         typedef typename T::series_set_elt_t::semiring_elt_t::value_t value_t;
00721 
00722         std::string set(tools::get_semiring_set(param, value_t()));
00723         std::string set_ref;
00724 
00725         if (node && node->hasAttribute(transcode("set")))
00726           set_ref = xml2str(node->getAttribute(transcode("set")));
00727         else
00728           set_ref = "B";
00729         if (set_ref != set)
00730           FAIL("Bad semiring");
00731       }
00732 
00733 
00734       template <class S, class T>
00735       void ensure_semiring_type(const xercesc::DOMElement* node,
00736                                 const Element<Transducer<S>, T>&,
00737                                 const typename Element<Transducer<S>, T>
00738                                 ::semiring_t::semiring_t& param)
00739       {
00740         typedef Element<Transducer<S>, T> trans_t;
00741         typedef typename
00742           trans_t::series_set_elt_t::semiring_elt_t::semiring_elt_t::value_t
00743           value_t;
00744 
00745         std::string set(tools::get_semiring_set(param, value_t()));
00746         std::string set_ref;
00747 
00748         if (node && node->hasAttribute(transcode("set")))
00749           set_ref = xml2str(node->getAttribute(transcode("set")));
00750         else
00751           set_ref = "B";
00752         if (set_ref != set)
00753           FAIL("Bad semiring");
00754       }
00755 
00756 
00757       template <class S, class T, class U>
00758       void ensure_semiring_type(const xercesc::DOMElement* node,
00759                                 const Element<Transducer<S>, T>&,
00760                                 const typename Element<Transducer<S>, T>
00761                                 ::semiring_t& param)
00762       {
00763         typedef Element<Transducer<S>, T> trans_t;
00764         typedef typename
00765           trans_t::series_set_elt_t::semiring_elt_t::value_t value_t;
00766 
00767         std::string set(tools::get_semiring_set(param, value_t()));
00768         std::string set_ref;
00769 
00770         if (node && node->hasAttribute(transcode("set")))
00771           set_ref = xml2str(node->getAttribute(transcode("set")));
00772         else
00773           set_ref = "ratSeries";
00774         if (set_ref != set)
00775           FAIL("Bad semiring");
00776       }
00777 
00778 
00788       template <class U>
00789       void ensure_monoid_type(const xercesc::DOMElement* node, const U& param)
00790       {
00791         std::string monoid_type = tools::get_monoid_type(param);
00792         std::string monoid_type_ref;
00793 
00794         if (! node || ! node->hasAttribute(transcode("type")))
00795           monoid_type_ref = "free";
00796         else
00797           monoid_type_ref = xml2str(node->getAttribute(transcode("type")));
00798         if (monoid_type_ref != monoid_type)
00799           FAIL("Bad monoid type");
00800       }
00801 
00802 
00803       template <class S, class T, class U, class M1, class M2>
00804       void ensure_monoid_type(const xercesc::DOMElement* node,
00805                               const FMPtype& param)
00806       {
00807         std::string monoid_type = tools::get_monoid_type(param);
00808         std::string monoid_type_ref;
00809 
00810         if (! node || ! node->hasAttribute(transcode("type")))
00811           monoid_type_ref = "product";
00812         else
00813           monoid_type_ref = xml2str(node->getAttribute(transcode("type")));
00814         if (monoid_type_ref != monoid_type)
00815           FAIL("Bad monoid type");
00816       }
00817 
00818 
00819       template <class OStream>
00820       void print_transition(const xercesc::DOMElement* n,
00821                             OStream& os,
00822                             std::string& spacing)
00823       {
00824         os << spacing << "<" << xml2str(n->getNodeName());
00825 # define VCSN_TRANS_OUTPUT(What)                                        \
00826         if (n->hasAttribute(transcode(What)))                           \
00827           os << " " What "=\""                                          \
00828              << xml2str(n->getAttribute(transcode(What))) << "\""
00829         VCSN_TRANS_OUTPUT("src");
00830         VCSN_TRANS_OUTPUT("dst");
00831         VCSN_TRANS_OUTPUT("label");
00832         VCSN_TRANS_OUTPUT("weight");
00833         VCSN_TRANS_OUTPUT("in");
00834         VCSN_TRANS_OUTPUT("out");
00835 # undef VCSN_TRANS_OUTPUT
00836       }
00837 
00838 
00839       template <class OStream>
00840       void print_tree(const xercesc::DOMElement* node,
00841                       OStream& os,
00842                       std::string spacing)
00843       {
00844         using namespace xercesc;
00845         unsigned i;
00846         DOMNamedNodeMap* m;
00847 
00848         if (xml2str(node->getNodeName()) == "transition")
00849           print_transition(static_cast<const DOMElement*>(node), os, spacing);
00850         else
00851         {
00852           os << spacing << "<" << xml2str(node->getNodeName());
00853           for (m = node->getAttributes(), i = 0;
00854                i < m->getLength(); ++i)
00855           {
00856             os << " " << xml2str(m->item(i)->getNodeName())
00857                << "=\"" << xml2str(m->item(i)->getNodeValue())
00858                << "\"";
00859           }
00860         }
00861         if (node->hasChildNodes())
00862           os << ">";
00863         else
00864           os << "/>";
00865         os << std::endl;
00866         for (DOMNode* n = node->getFirstChild(); n; n = n->getNextSibling())
00867           if (n->getNodeType() == DOMNode::ELEMENT_NODE)
00868             print_tree(static_cast<const DOMElement*>(n), os, spacing + "  ");
00869         if (node->hasChildNodes())
00870           os << spacing << "</" << xml2str(node->getNodeName()) << ">"
00871              << std::endl;
00872       }
00873 
00874 
00884       template <class OStream>
00885       void print_document(xercesc::DOMElement* node, OStream& os)
00886       {
00887         set_attribute(node, "xmlns", "http://vaucanson.lrde.epita.fr");
00888         print_tree(node, os, "");
00889       }
00890 
00891 
00892     } // ! tools
00893 
00894   } // ! xml
00895 
00896 } // ! vcsn
00897 
00898 
00899 # undef AUTtype
00900 # undef TRANStype
00901 # undef FMPtype
00902 
00903 #endif // ! VCSN_XML_TOOLS_HXX

Generated on Mon Jul 24 16:56:39 2006 for Vaucanson by  doxygen 1.4.4