00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef VCSN_XML_BUILDERS_HXX
00019 # define VCSN_XML_BUILDERS_HXX
00020
00021 # include <vaucanson/algebra/concept/letter.hh>
00022 # include <vaucanson/algebra/implementation/series/rat/exp.hh>
00023 # include <vaucanson/design_pattern/element.hh>
00024 # include <vaucanson/xml/xml_exp_visitor.hh>
00025 # include <vaucanson/algebra/concept/monoid_base.hh>
00026
00027 namespace vcsn
00028 {
00029 namespace xml
00030 {
00034 template <typename T>
00035 monGenHandler<T>::monGenHandler (xercesc::SAX2XMLReader* parser,
00036 Handler& root,
00037 T& monoid,
00038 const XMLCh* value)
00039 : Handler(parser, root),
00040 monoid_(monoid),
00041 value_(value)
00042 {
00043 }
00044
00045 template <typename T>
00046 void
00047 monGenHandler<T>::start (const XMLCh* const,
00048 const XMLCh* const localname,
00049 const XMLCh* const,
00050 const xercesc::Attributes&)
00051 {
00052 error::token(localname);
00053 }
00054
00055 template <typename T>
00056 void
00057 monGenHandler<T>::end (const XMLCh* const,
00058 const XMLCh* const localname,
00059 const XMLCh* const)
00060 {
00061 if (xercesc::XMLString::equals(eq_.monGen, localname))
00062 {
00063 if (value_)
00064 {
00065 std::string letter = xmlstr(value_);
00066 builders::insert_generator(monoid_.alphabet(), letter);
00067 parser_->setContentHandler(&root_);
00068 }
00069 else
00070 {
00071 error::missattrs(localname, "value");
00072 }
00073 }
00074 else
00075 error::token(localname);
00076 }
00077
00081 template <typename T>
00082 FreeMonoidHandler<T>::FreeMonoidHandler (xercesc::SAX2XMLReader* parser,
00083 Handler& root,
00084 T& monoid)
00085 : Handler(parser, root),
00086 monoid_(monoid),
00087 mongenh_(0),
00088 unsuph_(parser, *this)
00089 {
00090 }
00091
00092 template <typename T>
00093 void
00094 FreeMonoidHandler<T>::start (const XMLCh* const,
00095 const XMLCh* const localname,
00096 const XMLCh* const,
00097 const xercesc::Attributes& attrs)
00098 {
00099 if (xercesc::XMLString::equals(eq_.monGen, localname))
00100 {
00101 const XMLCh* value = tools::get_attribute(attrs, "value");
00102 if (mongenh_)
00103 delete mongenh_;
00104 mongenh_ = new monGenHandler<T>(parser_, *this, monoid_, value);
00105 parser_->setContentHandler(mongenh_);
00106 }
00107 else if (xercesc::XMLString::equals(eq_.genSort, localname))
00108 {
00109
00110 parser_->setContentHandler(&unsuph_);
00111 }
00112 else if (xercesc::XMLString::equals(eq_.writingData, localname))
00113 parser_->setContentHandler(&unsuph_);
00114 else
00115 error::token(localname);
00116 }
00117
00118 template <typename T>
00119 void
00120 FreeMonoidHandler<T>::end (const XMLCh* const,
00121 const XMLCh* const localname,
00122 const XMLCh* const)
00123 {
00124 if (xercesc::XMLString::equals(eq_.monoid, localname))
00125 {
00126 if (mongenh_)
00127 delete mongenh_;
00128 parser_->setContentHandler(&root_);
00129 }
00130 else if (!xercesc::XMLString::equals(eq_.monGen, localname))
00131 error::token(localname);
00132 }
00133
00134 namespace builders
00135 {
00136 template <typename T>
00137 typename T::monoid_t*
00138 create_monoid (T& param,
00139 const XMLCh* const localname,
00140 const xercesc::Attributes& attrs)
00141 {
00142 typename T::monoid_t::alphabet_t at;
00143 typedef typename T::monoid_t monoid_t;
00144
00145 monoid_t* monoid = new monoid_t(at);
00146 builders::check_monoid_consistency(param, localname, attrs);
00147 return monoid;
00148 }
00149
00150 template <typename T>
00151 Handler*
00152 create_monoidh (T& monoid,
00153 const xercesc::Attributes&,
00154 xercesc::SAX2XMLReader* parser,
00155 Handler& root)
00156 {
00157 return new vcsn::xml::FreeMonoidHandler<T>(parser, root, monoid);
00158 }
00159
00160
00161 template <typename T>
00162 void
00163 insert_generator(T& monoid,
00164 const std::string& str)
00165 {
00166 monoid.insert(str);
00167 }
00168 }
00172 template <typename T>
00173 NumSemiringHandler<T>::NumSemiringHandler (xercesc::SAX2XMLReader* parser,
00174 Handler& root,
00175 T& semiring)
00176 : Handler(parser, root),
00177 semiring_(semiring),
00178 unsuph_(parser, *this)
00179 {
00180 }
00181
00182 template <typename T>
00183 void
00184 NumSemiringHandler<T>::start (const XMLCh* const,
00185 const XMLCh* const localname,
00186 const XMLCh* const,
00187 const xercesc::Attributes&)
00188 {
00189 if (xercesc::XMLString::equals(eq_.writingData, localname))
00190 parser_->setContentHandler(&unsuph_);
00191 else
00192 error::token(localname);
00193 }
00194
00195 template <typename T>
00196 void
00197 NumSemiringHandler<T>::end (const XMLCh* const,
00198 const XMLCh* const localname,
00199 const XMLCh* const)
00200 {
00201 if (xercesc::XMLString::equals(eq_.semiring, localname))
00202 parser_->setContentHandler(&root_);
00203 else
00204 error::token(localname);
00205 }
00206
00207 namespace builders
00208 {
00209 template <typename T>
00210 typename T::semiring_t*
00211 create_semiring (T&,
00212 const XMLCh* const localname,
00213 const xercesc::Attributes& attrs)
00214 {
00215 typedef typename T::semiring_t semiring_t;
00216 semiring_t* semiring = new semiring_t();
00217
00218 typedef typename T::semiring_elt_t semiring_elt_t;
00219 semiring_elt_t elt;
00220 builders::check_semiring_consistency(elt, localname, attrs);
00221
00222 return semiring;
00223 }
00224
00225 template <typename T>
00226 Handler*
00227 create_semiringh(T& semiring,
00228 const xercesc::Attributes&,
00229 xercesc::SAX2XMLReader* parser,
00230 Handler& root)
00231 {
00232 return new NumSemiringHandler<T>(parser, root, semiring);
00233 }
00234
00235 }
00236
00240 template <typename T>
00241 class MonElmtHandler;
00242
00243 template <typename T>
00244 class WeightHandler;
00245
00246 namespace builders
00247 {
00248
00249 template <typename S, typename T>
00250 RegexpHandler<S>*
00251 create_monElmth(xercesc::SAX2XMLReader* parser,
00252 RegexpHandler<T>& root,
00253 S param)
00254 {
00255 return new MonElmtHandler<S>(parser, root, param);
00256 }
00257
00258 template <typename T>
00259 RegexpHandler<T>*
00260 create_weighth(xercesc::SAX2XMLReader* parser,
00261 RegexpHandler<T>& root,
00262 T param,
00263 const xercesc::Attributes& attrs)
00264 {
00265 typename T::monoid_elt_value_t m =
00266 vcsn::algebra::identity_as<typename T::monoid_elt_value_t>::of(param.structure().monoid()).value();
00267 const std::string val(xmlstr(tools::get_attribute(attrs, "value")));
00268 std::string::const_iterator i = val.begin();
00269 typename T::semiring_elt_t w(param.structure().semiring());
00270 if (!parse_weight(w, val, i))
00271 error::attrs(tools::get_attribute(attrs, "localname"), "value", val);
00272 param.assoc(m, w.value());
00273 return new WeightHandler<T>(parser, root, param);
00274 }
00275 }
00276
00280 namespace builders
00281 {
00282
00283 template <class T>
00284 const char* get_semiring_set(const T&)
00285 { return "undefined"; }
00286
00287 # define GET_SEMIRING_SET(T, Value) \
00288 const char* get_semiring_set(const T&) \
00289 { return Value; }
00290
00291 GET_SEMIRING_SET(bool, "B")
00292 GET_SEMIRING_SET(double, "R")
00293 GET_SEMIRING_SET(float, "R")
00294 GET_SEMIRING_SET(int, "Z")
00295 # undef GET_SEMIRING_SET
00296
00297 template <class S>
00298 const char* get_semiring_operations(const S&)
00299 { return "classical"; }
00300
00301 template <typename T>
00302 void
00303 check_monoid_consistency (T&,
00304 const XMLCh* const localname,
00305 const xercesc::Attributes& attrs)
00306 {
00307 std::string val(xmlstr(tools::get_attribute(attrs, "type")));
00308 if (val != "free")
00309 error::attrs(localname, "type", val);
00310 };
00311
00312 template <typename T>
00313 void
00314 check_semiring_consistency (T& param,
00315 const XMLCh* const localname,
00316 const xercesc::Attributes& attrs)
00317 {
00318 std::string set(xmlstr(tools::get_attribute(attrs, "set")));
00319 if (builders::get_semiring_set(param.value()) != set)
00320 error::attrs(localname, "set", set);
00321 std::string op(xmlstr(tools::get_attribute(attrs, "operations")));
00322 if (builders::get_semiring_operations(param.structure()) != op)
00323 error::attrs(localname, "operations", op);
00324 };
00325
00326 template <class T>
00327 const char* get_monoid_gen_sort(const T&)
00328 { return "undefined"; }
00329 # define GET_MONOID_GEN_SORT(T, Value) \
00330 const char* get_monoid_gen_sort(const T&) \
00331 { return Value; }
00332
00333 GET_MONOID_GEN_SORT(char, "letters")
00334 GET_MONOID_GEN_SORT(int, "integers")
00335 # undef GET_MONOID_GEN_SORT
00336 }
00337
00338 namespace builders
00339 {
00340 template <typename T>
00341 void
00342 create_semiring_node(const T& aut,
00343 xercesc::DOMDocument* doc,
00344 xercesc::DOMElement* root)
00345 {
00346 typedef typename T::semiring_elt_t semiring_elt_t;
00347 semiring_elt_t semiring(aut.structure().series().semiring());
00348 xercesc::DOMElement* node = tools::create_element(doc, "semiring");
00349 tools::set_attribute(node, "type", "numerical");
00350 tools::set_attribute(node, "set", get_semiring_set(semiring.value()));
00351 tools::set_attribute(node, "operations", get_semiring_operations(semiring.structure()));
00352 root->appendChild(node);
00353 }
00354 template <typename T>
00355 void
00356 create_monoid_node(const T& aut,
00357 xercesc::DOMDocument* doc,
00358 xercesc::DOMElement* root)
00359 {
00360 xercesc::DOMElement* node = tools::create_element(doc, "monoid");
00361 tools::set_attribute(node, "type", "free");
00362 tools::set_attribute(node, "genDescrip", "enum");
00363 tools::set_attribute(node, "genKind", "simple");
00364 root->appendChild(node);
00365 typedef typename T::monoid_t::alphabet_t::const_iterator alphabet_iterator;
00366 for_all_letters(l, aut.structure().series().monoid().alphabet())
00367 {
00368 std::ostringstream letter;
00369 xercesc::DOMElement* gen = tools::create_element(doc, "monGen");
00370 letter << *l;
00371 tools::set_attribute(gen, "value", letter.str());
00372 node->appendChild(gen);
00373 }
00374 tools::set_attribute(node, "genSort", get_monoid_gen_sort(*(aut.structure().series().monoid().alphabet().begin())));
00375 }
00376
00377 template <typename T>
00378 void
00379 create_regexp_node(const T& e,
00380 xercesc::DOMDocument* doc,
00381 xercesc::DOMElement* root)
00382 {
00383 typedef typename T::value_t::monoid_elt_value_t monoid_elt_value_t;
00384 typedef typename T::value_t::semiring_elt_value_t semiring_elt_value_t;
00385 typedef typename rat::exp<monoid_elt_value_t, semiring_elt_value_t> krat_exp_impl_t;
00386
00387 typedef Element<typename T::set_t, krat_exp_impl_t > krat_exp_t;
00388
00389 krat_exp_t res(e);
00390 rat::XmlExpVisitor<monoid_elt_value_t, semiring_elt_value_t> v(doc, "label");
00391 res.value().accept(v);
00392 root->appendChild(v.get());
00393 }
00394
00395 template <typename U>
00396 void
00397 create_monElmt_node(const U& word,
00398 xercesc::DOMDocument* doc,
00399 xercesc::DOMElement* root)
00400 {
00401 xercesc::DOMElement* node;
00402
00403 if (word.empty())
00404 node = tools::create_element(doc, "one");
00405 else
00406 {
00407 node = tools::create_element(doc, "monElmt");
00408 for (typename U::const_iterator i = word.begin(); i != word.end(); ++i)
00409 {
00410 xercesc::DOMElement* gen = tools::create_element(doc, "monGen");
00411 tools::set_attribute(gen, "value",
00412 algebra::letter_traits<typename U::value_type>::
00413 letter_to_literal(*i));
00414 node->appendChild(gen);
00415 }
00416 }
00417 root->appendChild(node);
00418 }
00419 }
00420 }
00421 }
00422
00423 #endif // !VCSN_XML_BUILDERS_HXX