Vaucanson 1.4
xml_exp_visitor.hxx
00001 // xml_exp_visitor.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2006, 2008, 2009 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 #ifndef VCSN_XML_XML_EXP_VISITOR_HXX
00018 # define VCSN_XML_XML_EXP_VISITOR_HXX
00019 
00020 # include <algorithm>
00021 # include <sstream>
00022 
00023 # include <vaucanson/xml/tools.hh>
00024 # include <vaucanson/xml/strings.hh>
00025 # include <vaucanson/xml/builders.hh>
00026 
00027 namespace vcsn {
00028 
00029   namespace rat {
00030 
00031     using xml::transcode;
00032 
00033     template<typename M_, typename W_>
00034     XmlExpVisitor<M_,W_>::XmlExpVisitor(xercesc::DOMDocument* doc, const char* node_name) :
00035       doc_(doc),
00036       label_(xml::tools::create_element(doc_, node_name)),
00037       current_(label_)
00038     {}
00039 
00040 
00041     template<typename M_, typename W_>
00042     void
00043     XmlExpVisitor<M_,W_>::sum_or_product(const Node<M_, W_>* left_,
00044                                          const Node<M_, W_>* right_)
00045     {
00046       left_->accept(*this);
00047       right_->accept(*this);
00048     }
00049 
00050     template<typename M_, typename W_>
00051     void
00052     XmlExpVisitor<M_, W_>::weight_or_star(const Node<M_, W_>* node)
00053     {
00054       node->accept(*this);
00055     }
00056 
00057     template<typename M_, typename W_>
00058     void
00059     XmlExpVisitor<M_, W_>::product(const Node<M_, W_>* left_,
00060                                    const Node<M_, W_>* right_)
00061     {
00062       xercesc::DOMElement* tmp = current_;
00063       current_ = xml::tools::create_element(doc_, "product");
00064       sum_or_product(left_, right_);
00065       tmp->appendChild(current_);
00066       current_ = tmp;
00067     }
00068 
00069     template<typename M_, typename W_>
00070     void
00071     XmlExpVisitor<M_, W_>::sum(const Node<M_, W_>* left_,
00072                                const Node<M_, W_>* right_)
00073     {
00074       xercesc::DOMElement* tmp = current_;
00075       current_ = xml::tools::create_element(doc_, "sum");
00076       sum_or_product(left_, right_);
00077       tmp->appendChild(current_);
00078       current_ = tmp;
00079     }
00080 
00081     template<typename M_, typename W_>
00082     void
00083     XmlExpVisitor<M_, W_>::star(const Node<M_, W_>* node)
00084     {
00085       xercesc::DOMElement* tmp = current_;
00086       current_ = xml::tools::create_element(doc_, "star");
00087       weight_or_star(node);
00088       tmp->appendChild(current_);
00089       current_ = tmp;
00090     }
00091 
00092     template <typename M_, typename W_>
00093     template <typename T>
00094     void
00095     XmlExpVisitor<M_, W_>::set_weight(const T& w, xercesc::DOMElement* node)
00096     {
00097       xercesc::DOMElement* weight = xml::tools::create_element(doc_, "weight");
00098       std::stringstream ss;
00099       ss << w;
00100       weight->setAttribute(transcode("value"), transcode(ss.str()));
00101       node->appendChild(weight);
00102     }
00103 
00104     template <typename M_, typename W_>
00105     template <typename S, typename T>
00106     void
00107     XmlExpVisitor<M_, W_>::set_weight(const rat::exp<S, T>& w, xercesc::DOMElement* node)
00108     {
00109       XmlExpVisitor<S, T> v(doc_, "weight");
00110       w.accept(v);
00111       node->appendChild(v.get());
00112     }
00113 
00114     template<typename M_, typename W_>
00115     void
00116     XmlExpVisitor<M_, W_>::left_weight(const W_& w, const Node<M_, W_>* node)
00117     {
00118       xercesc::DOMElement* tmp = current_;
00119       current_ = xml::tools::create_element(doc_, "leftExtMul");
00120       set_weight(w, current_);
00121       weight_or_star(node);
00122       tmp->appendChild(current_);
00123       current_ = tmp;
00124     }
00125 
00126     template<typename M_, typename W_>
00127     void
00128     XmlExpVisitor<M_, W_>::right_weight(const W_& w, const Node<M_, W_>* node)
00129     {
00130       xercesc::DOMElement* tmp = current_;
00131       current_ = xml::tools::create_element(doc_, "rightExtMul");
00132       set_weight(w, current_);
00133       weight_or_star(node);
00134       tmp->appendChild(current_);
00135       current_ = tmp;
00136     }
00137 
00138     template<typename M_, typename W_>
00139     void
00140     XmlExpVisitor<M_, W_>::constant(const M_& m)
00141     {
00142       xml::builders::create_monElmt_node(m, doc_, current_);
00143     }
00144 
00145     template<typename M_, typename W_>
00146     void XmlExpVisitor<M_, W_>::zero()
00147     {
00148       xercesc::DOMElement* zero = xml::tools::create_element(doc_, "zero");
00149       current_->appendChild(zero);
00150     }
00151 
00152     template<typename M_, typename W_>
00153     void XmlExpVisitor<M_, W_>::one()
00154     {
00155       xercesc::DOMElement* identity = xml::tools::create_element(doc_, "one");
00156       current_->appendChild(identity);
00157     }
00158 
00159     template<typename M_, typename W_>
00160     xercesc::DOMElement*
00161     XmlExpVisitor<M_, W_>::get() const
00162     {
00163       return label_;
00164     }
00165 
00166     template<typename M_, typename W_>
00167     xercesc::DOMDocument*
00168     XmlExpVisitor<M_, W_>::set(xercesc::DOMDocument* v)
00169     {
00170       doc_ = v;
00171       return doc_;
00172     }
00173 
00174 
00175   } // End of namespace rat.
00176 
00177 } // End of namespace vcsn.
00178 
00179 #endif // ! VCSN_XML_XML_EXP_VISITOR_HXX