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 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 }
00112
00113
00114
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 this->end_ = this->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(this->eq_.monElmt, localname))
00138 {
00139 in_++;
00140 count_++;
00141 if (in_ > 2)
00142 error::token(localname);
00143 }
00144 else if (XMLString::equals(this->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(this->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(this->param_.structure().monoid().second_monoid());
00154 else
00155 error::token(localname);
00156 }
00157 else if (XMLString::equals(this->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(this->end_, localname))
00187 {
00188 if (in_ == 1 && count_ == 3)
00189 {
00190
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(this->param_.structure().semiring()).value();
00194 this->param_.assoc(m, w);
00195 this->parser_->setContentHandler(&this->root_);
00196 }
00197 in_--;
00198 }
00199 else if (XMLString::equals(this->eq_.one, localname))
00200 in_--;
00201 else if (!XMLString::equals(this->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 }
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 }
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", "simple");
00258 tools::set_attribute(first, "genSort", "letters");
00259 node->appendChild(first);
00260 typedef typename FMPtype::monoid_t::first_monoid_t::alphabet_t::const_iterator first_alphabet_iterator;
00261 for_all_letters_(first_, l, aut.structure().series().monoid().first_monoid().alphabet())
00262 {
00263 std::ostringstream letter;
00264 xercesc::DOMElement* gen = tools::create_element(doc, "monGen");
00265 letter << *l;
00266 tools::set_attribute(gen, "value", letter.str());
00267 first->appendChild(gen);
00268 }
00269 tools::set_attribute(first, "genSort", get_monoid_gen_sort(*(aut.structure().series().monoid().first_monoid().alphabet().begin())));
00270 xercesc::DOMElement* second = tools::create_element(doc, "monoid");
00271 tools::set_attribute(second, "type", "free");
00272 tools::set_attribute(second, "genDescrip", "enum");
00273 tools::set_attribute(second, "genKind", "simple");
00274 tools::set_attribute(second, "genSort", "letters");
00275 node->appendChild(second);
00276 typedef typename FMPtype::monoid_t::second_monoid_t::alphabet_t::const_iterator second_alphabet_iterator;
00277 for_all_letters_(second_, l, aut.structure().series().monoid().second_monoid().alphabet())
00278 {
00279 std::ostringstream letter;
00280 xercesc::DOMElement* gen = tools::create_element(doc, "monGen");
00281 letter << *l;
00282 tools::set_attribute(gen, "value", letter.str());
00283 second->appendChild(gen);
00284 }
00285 tools::set_attribute(second, "genSort", get_monoid_gen_sort(*(aut.structure().series().monoid().second_monoid().alphabet().begin())));
00286 }
00287
00288
00289
00290
00291 template <>
00292 void
00293 create_monElmt_node(const std::pair<std::string, std::string>& m,
00294 xercesc::DOMDocument* doc,
00295 xercesc::DOMElement* root)
00296 {
00297 xercesc::DOMElement* node;
00298 if (m.first.empty() && m.second.empty())
00299 node = tools::create_element(doc, "one");
00300 else
00301 {
00302 node = tools::create_element(doc, "monElmt");
00303 create_monElmt_node(m.first, doc, node);
00304 create_monElmt_node(m.second, doc, node);
00305 }
00306 root->appendChild(node);
00307 }
00308
00309 template <>
00310 void
00311 create_monElmt_node(const std::pair<std::basic_string<int>, std::basic_string<int> >& m,
00312 xercesc::DOMDocument* doc,
00313 xercesc::DOMElement* root)
00314 {
00315 xercesc::DOMElement* node;
00316 if (m.first.empty() && m.second.empty())
00317 node = tools::create_element(doc, "one");
00318 else
00319 {
00320 node = tools::create_element(doc, "monElmt");
00321 create_monElmt_node(m.first, doc, node);
00322 create_monElmt_node(m.second, doc, node);
00323 }
00324 root->appendChild(node);
00325 }
00326 }
00327 }
00328 }
00329
00330 #endif // !VCSN_XML_CONTEXTS_FMP_HXX