00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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 {
00064 algebra::monoid_rep<T> rep;
00065 if (tools::has_attribute(attrs, eq_.identitySymbol))
00066 rep.empty = xmlstr(tools::get_attribute(attrs, eq_.identitySymbol));
00067 if (tools::has_attribute(attrs, eq_.concat))
00068 rep.empty = xmlstr(tools::get_attribute(attrs, eq_.concat));
00069 monoid_.set_representation(rep);
00070 parser_->setContentHandler(&unsuph_);
00071 }
00072 else
00073 error::token(localname);
00074 }
00075
00076 template <typename T>
00077 void
00078 FreeMonoidProductHandler<T>::end (const XMLCh* const,
00079 const XMLCh* const localname,
00080 const XMLCh* const)
00081 {
00082 delete monoidh_;
00083 if (xercesc::XMLString::equals(eq_.monoid, localname))
00084 parser_->setContentHandler(&root_);
00085 else
00086 error::token(localname);
00087 }
00088
00089
00090 namespace builders
00091 {
00092 TParamFMP
00093 typename FMPtype::monoid_t*
00094 create_monoid (FMPtype& param,
00095 const XMLCh* const localname,
00096 const xercesc::Attributes& attrs)
00097 {
00098 typename FMPtype::monoid_t::first_monoid_t::alphabet_t at1;
00099 typename FMPtype::monoid_t::second_monoid_t::alphabet_t at2;
00100 typename FMPtype::monoid_t::first_monoid_t md1(at1, *(param.structure().series().monoid().first_monoid().representation()));
00101 typename FMPtype::monoid_t::second_monoid_t md2(at2, *(param.structure().series().monoid().second_monoid().representation()));
00102 typedef typename FMPtype::monoid_t monoid_t;
00103
00104 monoid_t* monoid = new monoid_t(md1, md2, *(param.structure().series().monoid().representation()));
00105 builders::check_monoid_consistency(param, localname, attrs);
00106 return monoid;
00107 }
00108
00109 template <typename M1, typename M2>
00110 Handler*
00111 create_monoidh (vcsn::algebra::FreeMonoidProduct<M1, M2>& monoid,
00112 const xercesc::Attributes&,
00113 xercesc::SAX2XMLReader* parser,
00114 Handler& root)
00115 {
00116 typedef typename vcsn::algebra::FreeMonoidProduct<M1, M2> monoid_t;
00117 return new FreeMonoidProductHandler<monoid_t>(parser, root, monoid);
00118 }
00119 }
00120
00121
00122
00123
00124 template <typename T>
00125 ProdMonElmtHandler<T>::ProdMonElmtHandler (xercesc::SAX2XMLReader* parser,
00126 Handler& root,
00127 T param)
00128 : RegexpHandler<T>(parser, root, param),
00129 in_(1),
00130 count_(1),
00131 m1_(param.structure().monoid().first_monoid()),
00132 m2_(param.structure().monoid().second_monoid())
00133 {
00134 end_ = eq_.monElmt;
00135 }
00136
00137 template <typename T>
00138 void
00139 ProdMonElmtHandler<T>::start (const XMLCh* const,
00140 const XMLCh* const localname,
00141 const XMLCh* const,
00142 const xercesc::Attributes& attrs)
00143 {
00144 using namespace xercesc;
00145 if (XMLString::equals(eq_.monElmt, localname))
00146 {
00147 in_++;
00148 count_++;
00149 if (in_ > 2)
00150 error::token(localname);
00151 }
00152 else if (XMLString::equals(eq_.one, localname) && (in_ == 1))
00153 {
00154 in_++;
00155 count_++;
00156 if (count_ == 2)
00157 m1_ = algebra::identity_as<typename T::monoid_elt_t::first_monoid_elt_value_t>
00158 ::of(param_.structure().monoid().first_monoid());
00159 else if (count_ == 3)
00160 m2_ = algebra::identity_as<typename T::monoid_elt_t::second_monoid_elt_value_t>
00161 ::of(param_.structure().monoid().second_monoid());
00162 else
00163 error::token(localname);
00164 }
00165 else if (XMLString::equals(eq_.monGen, localname))
00166 {
00167 const std::string val(xmlstr(tools::get_attribute(attrs, "value")));
00168 if (in_ == 2 && count_ == 2)
00169 {
00170 if (!parse_word(m1_, val))
00171 error::attrs(localname, "value", val);
00172 }
00173 else if (in_ == 2 && count_ == 3)
00174 {
00175 if (!parse_word(m2_, val))
00176 error::attrs(localname, "value", val);
00177 }
00178 else
00179 error::token(localname);
00180 }
00181 else
00182 error::token(localname);
00183 }
00184
00185 template <typename T>
00186 void
00187 ProdMonElmtHandler<T>::end (const XMLCh* const,
00188 const XMLCh* const localname,
00189 const XMLCh* const)
00190 {
00191 using namespace xercesc;
00192 if (XMLString::equals(end_, localname))
00193 {
00194 if (in_ == 1 && count_ == 3)
00195 {
00196
00197 typename T::monoid_elt_value_t m(m1_.value(), m2_.value());
00198 typename T::semiring_elt_value_t w =
00199 algebra::identity_as<typename T::semiring_elt_value_t>::of(param_.structure().semiring()).value();
00200 param_.assoc(m, w);
00201 parser_->setContentHandler(&root_);
00202 }
00203 in_--;
00204 }
00205 else if (XMLString::equals(eq_.one, localname))
00206 in_--;
00207 else if (!XMLString::equals(eq_.monGen, localname))
00208 error::token(localname);
00209 }
00210
00211
00212 namespace builders
00213 {
00214 SParamFMP
00215 RegexpHandler<FMPseries >*
00216 create_monElmth(xercesc::SAX2XMLReader* parser,
00217 RegexpHandler<FMPseries >& root,
00218 FMPseries param)
00219 {
00220 return new ProdMonElmtHandler<FMPseries >(parser, root,
00221 algebra::zero_as<typename FMPseries::value_t>::of(param.structure()));
00222 }
00223 }
00224
00228 namespace builders
00229 {
00230 TParamFMP
00231 void
00232 check_monoid_consistency (FMPtype&,
00233 const XMLCh* const localname,
00234 const xercesc::Attributes& attrs)
00235 {
00236 std::string val(xmlstr(tools::get_attribute(attrs, "type")));
00237 if (val != "product")
00238 error::attrs(localname, "type", val);
00239 val = xmlstr(tools::get_attribute(attrs, "prodDim"));
00240 if (val != "2")
00241 error::attrs(localname, "prodDim", val);
00242 };
00243 }
00244
00248 namespace builders
00249 {
00250 TParamFMP
00251 void
00252 create_monoid_node(const FMPtype& aut,
00253 xercesc::DOMDocument* doc,
00254 xercesc::DOMElement* root)
00255 {
00256 xercesc::DOMElement* node = tools::create_element(doc, "monoid");
00257 tools::set_attribute(node, "type", "product");
00258 tools::set_attribute(node, "prodDim", "2");
00259 root->appendChild(node);
00260
00261 xercesc::DOMElement* writingData = tools::create_element(doc, "writingData");
00262 tools::set_attribute(writingData, "identitySym", aut.series().monoid().representation()->empty);
00263 tools::set_attribute(writingData, "timesSym", aut.series().monoid().representation()->concat);
00264 node->appendChild(writingData);
00265
00266 xercesc::DOMElement* first = tools::create_element(doc, "monoid");
00267 tools::set_attribute(first, "type", "free");
00268 tools::set_attribute(first, "genDescrip", "enum");
00269 tools::set_attribute(first, "genKind", algebra::letter_traits<typename FMPtype::monoid_t::first_monoid_t::alphabet_t::letter_t>::kind());
00270 node->appendChild(first);
00271
00272 xercesc::DOMElement* writingData1 = tools::create_element(doc, "writingData");
00273 tools::set_attribute(writingData1, "identitySym", aut.series().monoid().first_monoid().representation()->empty);
00274 tools::set_attribute(writingData1, "timesSym", aut.series().monoid().first_monoid().representation()->concat);
00275 first->appendChild(writingData1);
00276
00277 typedef typename FMPtype::monoid_t::first_monoid_t::alphabet_t::const_iterator first_alphabet_iterator;
00278 for_all_letters_(first_, l, aut.structure().series().monoid().first_monoid().alphabet())
00279 {
00280 std::ostringstream letter;
00281 xercesc::DOMElement* gen = tools::create_element(doc, "monGen");
00282 letter << *l;
00283 tools::set_attribute(gen, "value", letter.str());
00284 first->appendChild(gen);
00285 }
00286 tools::set_attribute(first, "genSort", get_monoid_gen_sort(*(aut.structure().series().monoid().first_monoid().alphabet().begin())));
00287 xercesc::DOMElement* second = tools::create_element(doc, "monoid");
00288 tools::set_attribute(second, "type", "free");
00289 tools::set_attribute(second, "genDescrip", "enum");
00290 tools::set_attribute(second, "genKind", algebra::letter_traits<typename FMPtype::monoid_t::second_monoid_t::alphabet_t::letter_t>::kind());
00291 node->appendChild(second);
00292
00293 xercesc::DOMElement* writingData2 = tools::create_element(doc, "writingData");
00294 tools::set_attribute(writingData2, "identitySym", aut.series().monoid().second_monoid().representation()->empty);
00295 tools::set_attribute(writingData2, "timesSym", aut.series().monoid().second_monoid().representation()->concat);
00296 second->appendChild(writingData2);
00297
00298 typedef typename FMPtype::monoid_t::second_monoid_t::alphabet_t::const_iterator second_alphabet_iterator;
00299 for_all_letters_(second_, l, aut.structure().series().monoid().second_monoid().alphabet())
00300 {
00301 std::ostringstream letter;
00302 xercesc::DOMElement* gen = tools::create_element(doc, "monGen");
00303 letter << *l;
00304 tools::set_attribute(gen, "value", letter.str());
00305 second->appendChild(gen);
00306 }
00307 tools::set_attribute(second, "genSort", get_monoid_gen_sort(*(aut.structure().series().monoid().second_monoid().alphabet().begin())));
00308 }
00309
00310
00311
00312
00313 template <>
00314 void
00315 create_monElmt_node(const std::pair<std::string, std::string>& m,
00316 xercesc::DOMDocument* doc,
00317 xercesc::DOMElement* root)
00318 {
00319 xercesc::DOMElement* node;
00320 if (m.first.empty() && m.second.empty())
00321 node = tools::create_element(doc, "one");
00322 else
00323 {
00324 node = tools::create_element(doc, "monElmt");
00325 create_monElmt_node(m.first, doc, node);
00326 create_monElmt_node(m.second, doc, node);
00327 }
00328 root->appendChild(node);
00329 }
00330
00331 template <>
00332 void
00333 create_monElmt_node(const std::pair<std::basic_string<int>, std::basic_string<int> >& m,
00334 xercesc::DOMDocument* doc,
00335 xercesc::DOMElement* root)
00336 {
00337 xercesc::DOMElement* node;
00338 if (m.first.empty() && m.second.empty())
00339 node = tools::create_element(doc, "one");
00340 else
00341 {
00342 node = tools::create_element(doc, "monElmt");
00343 create_monElmt_node(m.first, doc, node);
00344 create_monElmt_node(m.second, doc, node);
00345 }
00346 root->appendChild(node);
00347 }
00348 }
00349 }
00350 }
00351
00352 #endif // !VCSN_XML_CONTEXTS_FMP_HXX