printers.hxx

00001 // printers.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2005, 2006, 2007, 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 
00018 #ifndef VCSN_XML_PRINTERS_HXX
00019 # define VCSN_XML_PRINTERS_HXX
00020 
00021 # include <fstream>
00022 
00023 # include <vaucanson/xml/xml_xerces_stream.hh>
00024 # include <vaucanson/xml/strings.hh>
00025 # include <vaucanson/xml/tools.hh>
00026 # include <vaucanson/xml/builders.hh>
00027 
00028 namespace vcsn
00029 {
00030   namespace xml
00031   {
00032     /*
00033      * Printer class.
00034      */
00035 
00036     Printer::Printer ()
00037     {
00038     };
00039 
00040     Printer::~Printer ()
00041     {
00042     }
00043 
00044     /*
00045      * AutPrinter class.
00046      */
00047     template <typename Auto>
00048     AutPrinter<Auto>::AutPrinter (const Auto& aut, const std::string& name)
00049       : aut_(aut), name_(name)
00050     {
00051     }
00052 
00053     template <typename Auto>
00054     AutPrinter<Auto>::~AutPrinter ()
00055     {
00056     }
00057 
00058     template <typename Auto>
00059     void
00060     AutPrinter<Auto>::print (std::ostream& out)
00061     {
00062       typedef typename Auto::state_iterator state_iterator;
00063       typedef typename Auto::transition_iterator transition_iterator;
00064       typedef typename Auto::initial_iterator initial_iterator;
00065       typedef typename Auto::final_iterator final_iterator;
00066       using namespace xercesc;
00067 
00068       state2str_.clear();
00069 
00070       // Document creation.
00071       impl_ = DOMImplementationRegistry::getDOMImplementation(transcode("LS"));
00072       doc_ = impl_->createDocument(transcode(VCSN_XMLNS),
00073                                    transcode("fsmxml"), 0);
00074       root_ = doc_->getDocumentElement();
00075 
00076       tools::set_attribute(root_, "version", "1.0"); // FIXME should be... a macro?
00077 
00078       DOMElement* automaton = tools::create_element(doc_, "automaton");
00079       root_->appendChild(automaton);
00080       tools::set_attribute(automaton, "name", aut_.geometry().name());
00081 
00082       DOMElement* valueType = tools::create_element(doc_, "valueType");
00083       automaton->appendChild(valueType);
00084       builders::create_semiring_node(aut_, doc_, valueType);
00085       builders::create_monoid_node(aut_, doc_, valueType);
00086 
00087       DOMElement* content = tools::create_element(doc_, "automatonStruct");
00088       automaton->appendChild(content);
00089 
00090       // Create states.
00091       DOMElement* node = tools::create_element(doc_, "states");
00092       content->appendChild(node);
00093       for_all_states(s, aut_)
00094         state2str_[*s] = create_state(*s, node);
00095 
00096       // Create transitions.
00097       node = tools::create_element(doc_, "transitions");
00098       content->appendChild(node);
00099       for_all_transitions(e, aut_)
00100         create_transition(*e, node);
00101 
00102       // Create initial transitions.
00103       for_all_initial_states(i, aut_)
00104         create_initial(*i, node);
00105 
00106       // Create final transitions.
00107       for_all_final_states(f, aut_)
00108         create_final(*f, node);
00109 
00110       // Print it!
00111 
00112       XMLXercesStream<std::ostream>* target = new XMLXercesStream<std::ostream>(out);
00113       DOMWriter* writer = impl_->createDOMWriter();
00114 
00115       if (writer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true))
00116         writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00117       writer->writeNode(target, *root_);
00118       writer->release();
00119       out << std::endl;
00120       delete doc_;
00121       delete target;
00122     }
00123 
00124     template <class Auto>
00125     void
00126     AutPrinter<Auto>::create_geometry(hstate_t& key,
00127                         xercesc::DOMElement* root)
00128     {
00129       typedef typename Auto::geometry_t::states_geometry_map_t gmap_t;
00130       typename gmap_t::const_iterator iter;
00131       gmap_t map = aut_.geometry().states();
00132       if ((iter = map.find(key)) != map.end())
00133       {
00134         std::ostringstream osx, osy;
00135         osx << iter->second.first;
00136         xercesc::DOMElement* nd = tools::create_element(doc_, "geometricData");
00137         root->appendChild(nd);
00138         tools::set_attribute(nd, "x", osx.str());
00139         osy << iter->second.second;
00140         tools::set_attribute(nd, "y", osy.str());
00141       }
00142     }
00143 
00144     template <class Auto>
00145     std::string
00146     AutPrinter<Auto>::create_state(hstate_t s,
00147                                    xercesc::DOMElement* root)
00148     {
00149       xercesc::DOMElement* node = tools::create_element(doc_, "state");
00150       root->appendChild(node);
00151       std::ostringstream os;
00152       os << "s" << s;
00153       tools::set_attribute(node, "id", os.str());
00154       create_geometry(s, node);
00155       return os.str();
00156     }
00157 
00158     template <class Auto>
00159     void
00160     AutPrinter<Auto>::create_transition(htransition_t e,
00161                                         xercesc::DOMElement* root)
00162     {
00163       xercesc::DOMElement* node = tools::create_element(doc_, "transition");
00164       root->appendChild(node);
00165       tools::set_attribute(node, "src", state2str_[aut_.src_of(e)]);
00166       tools::set_attribute(node, "target", state2str_[aut_.dst_of(e)]);
00167       builders::create_regexp_node(aut_.series_of(e), doc_, node);
00168     }
00169     template <class Auto>
00170     void
00171     AutPrinter<Auto>::create_initial(hstate_t s,
00172                                      xercesc::DOMElement* root)
00173     {
00174       xercesc::DOMElement* node = tools::create_element(doc_, "initial");
00175       root->appendChild(node);
00176       tools::set_attribute(node, "state", state2str_[s]);
00177       typename Auto::series_set_elt_t tmp = aut_.get_initial(s);
00178       if (tmp != algebra::identity_as<typename Auto::series_set_elt_t::value_t>::of(aut_.structure().series()).value())
00179         builders::create_regexp_node(tmp, doc_, node);
00180     }
00181     template <class Auto>
00182     void
00183     AutPrinter<Auto>::create_final(hstate_t s,
00184                                      xercesc::DOMElement* root)
00185     {
00186       xercesc::DOMElement* node = tools::create_element(doc_, "final");
00187       root->appendChild(node);
00188       tools::set_attribute(node, "state", state2str_[s]);
00189       typename Auto::series_set_elt_t tmp = aut_.get_final(s);
00190       if (tmp != algebra::identity_as<typename Auto::series_set_elt_t::value_t>::of(aut_.structure().series()).value())
00191         builders::create_regexp_node(tmp, doc_, node);
00192     }
00193   } // !xml
00194 } // !vcsn
00195 
00196 #endif // !VCSN_XML_PRINTERS_HXX

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