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

Generated on Thu Oct 9 20:22:42 2008 for Vaucanson by  doxygen 1.5.1