00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef VCSN_XML_BUILDERS_HXX
00018 # define VCSN_XML_BUILDERS_HXX
00019
00020 # include <sstream>
00021
00022 # include <vaucanson/algebra/concept/letter.hh>
00023 # include <vaucanson/algebra/implementation/series/rat/exp.hh>
00024 # include <vaucanson/design_pattern/element.hh>
00025 # include <vaucanson/xml/xml_exp_visitor.hh>
00026 # include <vaucanson/algebra/concept/monoid_base.hh>
00027 # include <vaucanson/algebra/implementation/monoid/monoid_rep.hh>
00028
00029 namespace vcsn
00030 {
00031 namespace xml
00032 {
00036 template <typename T>
00037 monGenAction<T>::monGenAction(const T&)
00038 {
00039 static_error(need_to_specialize_monGenAction_for_T)
00040 }
00041
00042 template <typename T>
00043 monGenAction<vcsn::algebra::FreeMonoid<T> >::
00044 monGenAction(self_t& monoid)
00045 : alphabet_(monoid.alphabet())
00046 {
00047 }
00048
00049 template <typename T, typename U, typename V>
00050 monGenAction<vcsn::Element<vcsn::algebra::Series<T, U>, V> >::
00051 monGenAction(self_t& s)
00052 : s_(s)
00053 {
00054 }
00055
00056
00057 template <typename T>
00058 void
00059 monGenAction<vcsn::algebra::FreeMonoid<T> >::
00060 operator () (const std::string& str)
00061 {
00062 alphabet_.insert(str);
00063 }
00064
00065
00066 template <typename T, typename U, typename V>
00067 void
00068 monGenAction<vcsn::Element<vcsn::algebra::Series<T, U>, V> >::
00069 operator () (const std::string& str)
00070 {
00071 typename self_t::monoid_elt_t m(s_.structure().monoid(), str);
00072 self_t tmp(s_.structure(), m);
00073
00074 s_ = s_ * tmp;
00075 }
00076
00080 template <typename T, typename U>
00081 monGenHandler<T, U>::monGenHandler (xercesc::SAX2XMLReader* parser,
00082 Handler& root,
00083 const monGenAction<U>& action,
00084 const XMLCh* value)
00085 : Handler(parser, root),
00086 value_(value),
00087 action_(action)
00088 {
00089 }
00090
00091 template <typename T, typename U>
00092 void
00093 monGenHandler<T, U>::start (const XMLCh* const,
00094 const XMLCh* const localname,
00095 const XMLCh* const,
00096 const xercesc::Attributes&)
00097 {
00098 error::token(localname);
00099 }
00100
00101 template <typename T, typename U>
00102 void
00103 monGenHandler<T, U>::end (const XMLCh* const,
00104 const XMLCh* const localname,
00105 const XMLCh* const)
00106 {
00107 if (xercesc::XMLString::equals(eq_.monGen, localname))
00108 {
00109 if (value_)
00110 {
00111 std::string letter = xmlstr(value_);
00112 action_(letter);
00113 parser_->setContentHandler(&root_);
00114 }
00115 else
00116 {
00117 error::missattrs(localname, "value");
00118 }
00119 }
00120 else
00121 error::token(localname);
00122 }
00123
00127 template <typename T, typename U>
00128 monGenTupleHandler<T, U>::monGenTupleHandler (xercesc::SAX2XMLReader* parser,
00129 Handler& root,
00130 const monGenAction<U>& action)
00131 : Handler(parser, root),
00132 value_("("),
00133 wait_begin_(true),
00134 count_(0),
00135 action_(action)
00136 {
00137 }
00138
00139 template <typename T, typename U>
00140 void
00141 monGenTupleHandler<T, U>::start (const XMLCh* const,
00142 const XMLCh* const localname,
00143 const XMLCh* const,
00144 const xercesc::Attributes& attrs)
00145 {
00146 if (xercesc::XMLString::equals(eq_.monCompGen, localname) && wait_begin_)
00147 {
00148 wait_begin_ = false;
00149 const XMLCh* attr = tools::get_attribute(attrs, eq_.value);
00150 if (!attr)
00151 error::missattrs(localname, "value");
00152 value_ += xmlstr(attr);
00153 if (count_ == algebra::letter_traits<typename T::alphabet_t::letter_t>::dim() - 2)
00154 value_ += ",";
00155 }
00156 else
00157 error::token(localname);
00158 }
00159
00160 template <typename T, typename U>
00161 void
00162 monGenTupleHandler<T, U>::end (const XMLCh* const,
00163 const XMLCh* const localname,
00164 const XMLCh* const)
00165 {
00166 int dim = algebra::letter_traits<typename T::alphabet_t::letter_t>::
00167 dim();
00168
00169 if (xercesc::XMLString::equals(eq_.monGen, localname)
00170 && count_ == dim)
00171 {
00172 value_ += ")";
00173 action_(value_);
00174 parser_->setContentHandler(&root_);
00175 }
00176 else if (xercesc::XMLString::equals(eq_.monCompGen, localname)
00177 && !wait_begin_ && count_ < dim)
00178 {
00179 wait_begin_ = true;
00180 count_++;
00181 }
00182 else
00183 error::token(localname);
00184 }
00185
00189 template <typename T>
00190 FreeMonoidHandler<T>::FreeMonoidHandler (xercesc::SAX2XMLReader* parser,
00191 Handler& root,
00192 T& monoid)
00193 : Handler(parser, root),
00194 monoid_(monoid),
00195 mongenh_(0),
00196 unsuph_(parser, *this)
00197 {
00198 }
00199
00200 template <typename T>
00201 void
00202 FreeMonoidHandler<T>::start (const XMLCh* const,
00203 const XMLCh* const localname,
00204 const XMLCh* const,
00205 const xercesc::Attributes& attrs)
00206 {
00207 using namespace xercesc;
00208
00209 if (XMLString::equals(eq_.monGen, localname))
00210 {
00211
00212 monGenAction<T> action(monoid_);
00213
00214
00215 if (mongenh_)
00216 delete mongenh_;
00217
00218
00219 if (algebra::letter_traits<typename T::alphabet_t::letter_t>::kind() == "simple")
00220 {
00221 const XMLCh* value = tools::get_attribute(attrs, eq_.value);
00222 mongenh_ = new monGenHandler<T, T>(parser_, *this, action, value);
00223 }
00224 else
00225 mongenh_ = new monGenTupleHandler<T, T>(parser_, *this, action);
00226
00227
00228 parser_->setContentHandler(mongenh_);
00229 }
00230 else if (XMLString::equals(eq_.genSort, localname))
00231 {
00232
00233 parser_->setContentHandler(&unsuph_);
00234 }
00235 else if (XMLString::equals(eq_.writingData, localname))
00236 {
00237 algebra::monoid_rep<T> rep;
00238 if (tools::has_attribute(attrs, eq_.identitySymbol))
00239 rep.empty = xmlstr(tools::get_attribute(attrs, eq_.identitySymbol));
00240 if (tools::has_attribute(attrs, eq_.concat))
00241 rep.concat = xmlstr(tools::get_attribute(attrs, eq_.concat));
00242 monoid_.set_representation(rep);
00243 parser_->setContentHandler(&unsuph_);
00244 }
00245 else
00246 error::token(localname);
00247 }
00248
00249 template <typename T>
00250 void
00251 FreeMonoidHandler<T>::end (const XMLCh* const,
00252 const XMLCh* const localname,
00253 const XMLCh* const)
00254 {
00255 using namespace xercesc;
00256
00257 if (XMLString::equals(eq_.monoid, localname))
00258 {
00259
00260 if (mongenh_)
00261 delete mongenh_;
00262
00263
00264 parser_->setContentHandler(&root_);
00265 }
00266 else if (!XMLString::equals(eq_.monGen, localname))
00267 error::token(localname);
00268 }
00269
00270 namespace builders
00271 {
00272 template <typename T>
00273 typename T::monoid_t*
00274 create_monoid (T& param,
00275 const XMLCh* const localname,
00276 const xercesc::Attributes& attrs)
00277 {
00278 typename T::monoid_t::alphabet_t at;
00279 typedef typename T::monoid_t monoid_t;
00280
00281 monoid_t* monoid = new monoid_t(at, *(param.structure().series().monoid().representation()));
00282 builders::check_monoid_consistency(param, localname, attrs);
00283 return monoid;
00284 }
00285
00286 template <typename T>
00287 Handler*
00288 create_monoidh (T& monoid,
00289 const xercesc::Attributes&,
00290 xercesc::SAX2XMLReader* parser,
00291 Handler& root)
00292 {
00293 return new vcsn::xml::FreeMonoidHandler<T>(parser, root, monoid);
00294 }
00295
00296 }
00300 template <typename T>
00301 NumSemiringHandler<T>::NumSemiringHandler (xercesc::SAX2XMLReader* parser,
00302 Handler& root,
00303 T& semiring)
00304 : Handler(parser, root),
00305 semiring_(semiring),
00306 unsuph_(parser, *this)
00307 {
00308 }
00309
00310 template <typename T>
00311 void
00312 NumSemiringHandler<T>::start (const XMLCh* const,
00313 const XMLCh* const localname,
00314 const XMLCh* const,
00315 const xercesc::Attributes&)
00316 {
00317 if (xercesc::XMLString::equals(eq_.writingData, localname))
00318 parser_->setContentHandler(&unsuph_);
00319 else
00320 error::token(localname);
00321 }
00322
00323 template <typename T>
00324 void
00325 NumSemiringHandler<T>::end (const XMLCh* const,
00326 const XMLCh* const localname,
00327 const XMLCh* const)
00328 {
00329 if (xercesc::XMLString::equals(eq_.semiring, localname))
00330 parser_->setContentHandler(&root_);
00331 else
00332 error::token(localname);
00333 }
00334
00335 namespace builders
00336 {
00337 template <typename T>
00338 typename T::semiring_t*
00339 create_semiring (T&,
00340 const XMLCh* const localname,
00341 const xercesc::Attributes& attrs)
00342 {
00343 typedef typename T::semiring_t semiring_t;
00344 semiring_t* semiring = new semiring_t();
00345
00346 typedef typename T::semiring_elt_t semiring_elt_t;
00347 semiring_elt_t elt;
00348 builders::check_semiring_consistency(elt, localname, attrs);
00349
00350 return semiring;
00351 }
00352
00353 template <typename T>
00354 Handler*
00355 create_semiringh(T& semiring,
00356 const xercesc::Attributes&,
00357 xercesc::SAX2XMLReader* parser,
00358 Handler& root)
00359 {
00360 return new NumSemiringHandler<T>(parser, root, semiring);
00361 }
00362
00363 }
00364
00368 template <typename T>
00369 class MonElmtHandler;
00370
00371 template <typename T>
00372 class WeightHandler;
00373
00374 namespace builders
00375 {
00376
00377 template <typename S, typename T>
00378 RegexpHandler<S>*
00379 create_monElmth(xercesc::SAX2XMLReader* parser,
00380 RegexpHandler<T>& root,
00381 S param)
00382 {
00383 return new MonElmtHandler<S>(parser, root, param);
00384 }
00385
00386 template <typename T>
00387 RegexpHandler<T>*
00388 create_weighth(xercesc::SAX2XMLReader* parser,
00389 RegexpHandler<T>& root,
00390 T param,
00391 const xercesc::Attributes& attrs)
00392 {
00393 typename T::monoid_elt_value_t m =
00394 vcsn::algebra::identity_as<typename T::monoid_elt_value_t>::of(param.structure().monoid()).value();
00395 const std::string val(xmlstr(tools::get_attribute(attrs, root.eq().value)));
00396 std::string::const_iterator i = val.begin();
00397 typename T::semiring_elt_t w(param.structure().semiring());
00398 if (!parse_weight(w, val, i))
00399 error::attrs(tools::get_attribute(attrs, "localname"), "value", val);
00400 param.assoc(m, w.value());
00401 return new WeightHandler<T>(parser, root, param);
00402 }
00403 }
00404
00408 namespace builders
00409 {
00410
00411 template <class T>
00412 const char* get_semiring_set(const T&)
00413 { return "undefined"; }
00414
00415 # define GET_SEMIRING_SET(T, Value) \
00416 const char* get_semiring_set(const T&) \
00417 { return Value; }
00418
00419 GET_SEMIRING_SET(bool, "B")
00420 GET_SEMIRING_SET(double, "R")
00421 GET_SEMIRING_SET(float, "R")
00422 GET_SEMIRING_SET(int, "Z")
00423 # undef GET_SEMIRING_SET
00424
00425 template <class S>
00426 const char* get_semiring_operations(const S&)
00427 { return "classical"; }
00428
00429 template <typename T>
00430 void
00431 check_monoid_consistency (T&,
00432 const XMLCh* const localname,
00433 const xercesc::Attributes& attrs)
00434 {
00435 std::string val(xmlstr(tools::get_attribute(attrs, "type")));
00436 if (val != "free")
00437 error::attrs(localname, "type", val);
00438 };
00439
00440 template <typename T>
00441 void
00442 check_semiring_consistency (T& param,
00443 const XMLCh* const localname,
00444 const xercesc::Attributes& attrs)
00445 {
00446 std::string set(xmlstr(tools::get_attribute(attrs, "set")));
00447 if (builders::get_semiring_set(param.value()) != set)
00448 error::attrs(localname, "set", set);
00449 std::string op(xmlstr(tools::get_attribute(attrs, "operations")));
00450 if (builders::get_semiring_operations(param.structure()) != op)
00451 error::attrs(localname, "operations", op);
00452 };
00453
00454 template <class T>
00455 const char* get_monoid_gen_sort(const T&)
00456 { return "undefined"; }
00457 # define GET_MONOID_GEN_SORT(T, Value) \
00458 const char* get_monoid_gen_sort(const T&) \
00459 { return Value; }
00460
00461 GET_MONOID_GEN_SORT(char, "letters")
00462 GET_MONOID_GEN_SORT(int, "integers")
00463 # undef GET_MONOID_GEN_SORT
00464
00465 template <class T>
00466 const char* get_monoid_gen_sort(const T&, int)
00467 { return "undefined"; }
00468
00469 template <class U, class V>
00470 const char* get_monoid_gen_sort(const std::pair<U,V>& a, int i)
00471 {
00472 return i ? get_monoid_gen_sort(a.second) : get_monoid_gen_sort(a.first);
00473 }
00474 }
00475
00476 namespace builders
00477 {
00478 template <typename T>
00479 void
00480 create_semiring_node(const T& aut,
00481 xercesc::DOMDocument* doc,
00482 xercesc::DOMElement* root)
00483 {
00484 typedef typename T::semiring_elt_t semiring_elt_t;
00485 semiring_elt_t semiring(aut.structure().series().semiring());
00486 xercesc::DOMElement* node = tools::create_element(doc, "semiring");
00487 tools::set_attribute(node, "type", "numerical");
00488 tools::set_attribute(node, "set", get_semiring_set(semiring.value()));
00489 tools::set_attribute(node, "operations", get_semiring_operations(semiring.structure()));
00490 root->appendChild(node);
00491 }
00492 template <typename T>
00493 void
00494 create_monoid_node(const T& aut,
00495 xercesc::DOMDocument* doc,
00496 xercesc::DOMElement* root)
00497 {
00498 std::string letter_kind = algebra::letter_traits<typename T::monoid_t::alphabet_t::letter_t>::kind();
00499 xercesc::DOMElement* node = tools::create_element(doc, "monoid");
00500 tools::set_attribute(node, "type", "free");
00501 tools::set_attribute(node, "genDescrip", "enum");
00502 tools::set_attribute(node, "genKind", letter_kind);
00503 root->appendChild(node);
00504
00505 xercesc::DOMElement* writingData = tools::create_element(doc, "writingData");
00506 tools::set_attribute(writingData, "identitySym", aut.series().monoid().representation()->empty);
00507 tools::set_attribute(writingData, "timesSym", aut.series().monoid().representation()->concat);
00508 node->appendChild(writingData);
00509
00510 typedef typename T::monoid_t::alphabet_t::const_iterator alphabet_iterator;
00511
00512 if (letter_kind == "simple")
00513 tools::set_attribute(node, "genSort", get_monoid_gen_sort(*(aut.structure().series().monoid().alphabet().begin())));
00514 else
00515 {
00516 std::stringstream genDim;
00517 int dim = algebra::letter_traits<typename T::monoid_t::alphabet_t::letter_t>::dim();
00518 genDim << dim;
00519 tools::set_attribute(node, "genDim", genDim.str());
00520 xercesc::DOMElement* genSort = tools::create_element(doc, "genSort");
00521 node->appendChild(genSort);
00522 xercesc::DOMElement* genCompSort;
00523 for (int i = 0; i != dim; i++)
00524 {
00525 genCompSort = tools::create_element(doc, "genCompSort");
00526 tools::set_attribute(genCompSort, "value", get_monoid_gen_sort(*(aut.structure().series().monoid().alphabet().begin()), i));
00527 genSort->appendChild(genCompSort);
00528 }
00529 }
00530
00531 create_monGen_node<typename T::monoid_t::alphabet_t::letter_t> monGen_maker;
00532 for_all_letters(l, aut.structure().series().monoid().alphabet())
00533 monGen_maker(*l, doc, node);
00534
00535 }
00536
00537 template <typename T>
00538 void
00539 create_regexp_node(const T& e,
00540 xercesc::DOMDocument* doc,
00541 xercesc::DOMElement* root)
00542 {
00543 typedef typename T::value_t::monoid_elt_value_t monoid_elt_value_t;
00544 typedef typename T::value_t::semiring_elt_value_t semiring_elt_value_t;
00545 typedef typename rat::exp<monoid_elt_value_t, semiring_elt_value_t> krat_exp_impl_t;
00546
00547 typedef Element<typename T::set_t, krat_exp_impl_t > krat_exp_t;
00548
00549 krat_exp_t res(e);
00550 rat::XmlExpVisitor<monoid_elt_value_t, semiring_elt_value_t> v(doc, "label");
00551 res.value().accept(v);
00552 root->appendChild(v.get());
00553 }
00554
00555 template <typename U>
00556 void
00557 create_monElmt_node(const U& word,
00558 xercesc::DOMDocument* doc,
00559 xercesc::DOMElement* root)
00560 {
00561 xercesc::DOMElement* node;
00562
00563 if (word.empty())
00564 node = tools::create_element(doc, "one");
00565 else
00566 {
00567 node = tools::create_element(doc, "monElmt");
00568 create_monGen_node<typename U::value_type> monGen_maker;
00569 for (typename U::const_iterator i = word.begin(); i != word.end(); ++i)
00570 monGen_maker(*i, doc, node);
00571 }
00572 root->appendChild(node);
00573 }
00574
00575 template <typename T>
00576 void
00577 create_type_writingData_node(const T& aut,
00578 xercesc::DOMDocument* doc,
00579 xercesc::DOMElement* root)
00580 {
00581 xercesc::DOMElement* writingData = tools::create_element(doc, "writingData");
00582 tools::set_attribute(writingData, "plusSym", aut.series().representation()->plus);
00583 tools::set_attribute(writingData, "timesSym", aut.series().representation()->times);
00584 tools::set_attribute(writingData, "starSym", aut.series().representation()->star);
00585 tools::set_attribute(writingData, "zeroSym", aut.series().representation()->zero);
00586 tools::set_attribute(writingData, "weightOpening", aut.series().representation()->open_weight);
00587 tools::set_attribute(writingData, "weightClosing", aut.series().representation()->close_weight);
00588 tools::set_attribute(writingData, "openPar", aut.series().representation()->open_par);
00589 tools::set_attribute(writingData, "closePar", aut.series().representation()->close_par);
00590 tools::set_attribute(writingData, "spacesSym", aut.series().representation()->spaces.front());
00591 root->appendChild(writingData);
00592 }
00593
00594
00595
00596 template <typename U>
00597 struct create_monGen_node
00598 {
00599 void
00600 operator()(const U& letter,
00601 xercesc::DOMDocument* doc,
00602 xercesc::DOMElement* root)
00603 {
00604 xercesc::DOMElement* gen = tools::create_element(doc, "monGen");
00605
00606 tools::set_attribute(gen, "value",
00607 algebra::letter_traits<U>::
00608 letter_to_literal(letter));
00609
00610 root->appendChild(gen);
00611 }
00612 };
00613
00614
00615
00616
00617 template <typename U, typename V>
00618 struct create_monGen_node<std::pair<U, V> >
00619 {
00620 void
00621 operator()(const std::pair<U, V>& letter,
00622 xercesc::DOMDocument* doc,
00623 xercesc::DOMElement* root)
00624 {
00625 xercesc::DOMElement* gen = tools::create_element(doc, "monGen");
00626
00627 std::stringstream sstr_first;
00628 std::stringstream sstr_second;
00629 xercesc::DOMElement* first = tools::create_element(doc, "monCompGen");
00630 xercesc::DOMElement* second = tools::create_element(doc, "monCompGen");
00631 sstr_first << letter.first;
00632 sstr_second << letter.second;
00633 tools::set_attribute(first, "value", sstr_first.str());
00634 tools::set_attribute(second, "value", sstr_second.str());
00635 gen->appendChild(first);
00636 gen->appendChild(second);
00637
00638 root->appendChild(gen);
00639 }
00640 };
00641
00642 }
00643
00644 }
00645
00646 }
00647
00648 #endif // ! VCSN_XML_BUILDERS_HXX