xml_converter.hxx

Go to the documentation of this file.
00001 // xml_converter.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 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_CONVERTER_HXX
00018 # define VCSN_XML_XML_CONVERTER_HXX
00019 
00020 # include <vaucanson/xml/tools.hh>
00021 
00034 namespace vcsn
00035 {
00036   namespace xml
00037   {
00038 
00039     template <class Auto>
00040     template <class OStream>
00041     void xml_converter<Auto>::save(const Auto& aut, OStream& os,
00042                                    const std::string& name)
00043     {
00044       create_document(aut, name);
00045       tools::print_document(root_, os);
00046     }
00047 
00048     template <class Auto>
00049     void xml_converter<Auto>::create_document(const Auto& aut,
00050                                               const std::string& name)
00051     {
00052       typedef typename Auto::state_iterator state_iterator;
00053       typedef typename Auto::transition_iterator transition_iterator;
00054       typedef typename Auto::initial_iterator initial_iterator;
00055       typedef typename Auto::final_iterator final_iterator;
00056       using namespace xercesc;
00057 
00058       std::map<hstate_t, std::string> state2str;
00059       const char* root_name = chooser_.choose_start_tag();
00060       const char* xml_namespace = "http://vaucanson.lrde.epita.fr";
00061       DOMElement* node;
00062 
00063       // Document creation.
00064       impl_ = DOMImplementationRegistry::getDOMImplementation(transcode("LS"));
00065       doc_ = impl_->createDocument(transcode(xml_namespace),
00066                                    transcode(root_name), 0);
00067       root_ = doc_->getDocumentElement();
00068 
00069       tools::xset_attribute(root_, "name", aut.geometry().name());
00070       tools::xset_attribute(root_, "name", name);
00071 
00072       // Create type tag.
00073       chooser_.create_type_tag(aut, doc_, root_);
00074 
00075       DOMElement* content = doc_->createElement(transcode("content"));
00076       root_->appendChild(content);
00077 
00078       // Create states.
00079       node = doc_->createElement(transcode("states"));
00080       content->appendChild(node);
00081       for_all_states(s, aut)
00082         state2str[*s] = create_state(*s, aut, node);
00083 
00084       // Create transitions.
00085       node = doc_->createElement(transcode("transitions"));
00086       content->appendChild(node);
00087       for_all_transitions(e, aut)
00088         create_transition(*e, aut, node, state2str);
00089 
00090       // Create initial transitions.
00091       for_all_initial_states(i, aut)
00092         create_initial(*i, aut, node, state2str);
00093 
00094       // Create final transitions.
00095       for_all_final_states(f, aut)
00096         create_final(*f, aut, node, state2str);
00097     }
00098 
00099 
00100     // Create a state in the XML document.
00101     template <class Auto>
00102     std::string xml_converter<Auto>::create_state(hstate_t s,
00103                                                   const Auto& aut,
00104                                                   xercesc::DOMElement* root)
00105     {
00106       std::ostringstream os;
00107       os << "s" << s;
00108       xercesc::DOMElement* node = doc_->createElement(transcode("state"));
00109       root->appendChild(node);
00110       tools::set_attribute(node, "name", os.str());
00111       add_xml_geometry(aut.geometry().states(), s, node);
00112 
00113       return os.str();
00114     }
00115 
00116 
00117     // Create a transition in the XML document.
00118     template <class Auto>
00119     void
00120     xml_converter<Auto>::create_transition(htransition_t e,
00121                                            const Auto& aut,
00122                                            xercesc::DOMElement* root,
00123                                            std::map<hstate_t, std::string>&
00124                                            state2str)
00125     {
00126       xercesc::DOMElement* node = doc_->createElement(transcode("transition"));
00127       root->appendChild(node);
00128       tools::set_attribute(node, "src", state2str[aut.src_of(e)]);
00129       tools::set_attribute(node, "dst", state2str[aut.dst_of(e)]);
00130       chooser_.create_label(doc_, e, aut, node, use_label_node_);
00131       add_xml_drawing(aut.geometry().transitions(), e, node);
00132     }
00133 
00134 
00135     // Create an initial state in the XML document.
00136     template <class Auto>
00137     void
00138     xml_converter<Auto>::create_initial(hstate_t s,
00139                                         const Auto& aut,
00140                                         xercesc::DOMElement* root,
00141                                         std::map<hstate_t, std::string>&
00142                                         state2str)
00143     {
00144       xercesc::DOMElement* node = doc_->createElement(transcode("initial"));
00145       root->appendChild(node);
00146       tools::set_attribute(node, "state", state2str[s]);
00147       chooser_.create_initial_label(doc_, s, aut, node, use_label_node_);
00148       add_xml_drawing(aut.geometry().initials(), s, node);
00149     }
00150 
00151 
00152     // Create a final state in the XML document.
00153     template <class Auto>
00154     void
00155     xml_converter<Auto>::create_final(hstate_t s,
00156                                       const Auto& aut,
00157                                       xercesc::DOMElement* root,
00158                                       std::map<hstate_t, std::string>&
00159                                       state2str)
00160     {
00161       xercesc::DOMElement* node = doc_->createElement(transcode("final"));
00162       root->appendChild(node);
00163       tools::set_attribute(node, "state", state2str[s]);
00164       chooser_.create_final_label(doc_, s, aut, node, use_label_node_);
00165       add_xml_drawing(aut.geometry().finals(), s, node);
00166     }
00167 
00168 
00169     // Add geometry informations in the XML document.
00170     template <class Auto>
00171     template <class Map, class Key>
00172     void xml_converter<Auto>::add_xml_geometry(Map& map,
00173                                                Key& key,
00174                                                xercesc::DOMElement* root)
00175     {
00176       typename Map::const_iterator iter;
00177       if ((iter = map.find(key)) != map.end())
00178       {
00179         std::ostringstream osx, osy;
00180         osx << iter->second.first;
00181         xercesc::DOMElement* nd = doc_->createElement(transcode("geometry"));
00182         root->appendChild(nd);
00183         tools::set_attribute(nd, "x", osx.str());
00184         osy << iter->second.second;
00185         tools::set_attribute(nd, "y", osy.str());
00186       }
00187     }
00188 
00189 
00190     // Add drawing informations in the XML document.
00191     template <class Auto>
00192     template <class Map, class Key>
00193     void xml_converter<Auto>::add_xml_drawing(Map& map,
00194                                               Key& key,
00195                                               xercesc::DOMElement* root)
00196     {
00197       typename Map::const_iterator iter;
00198       if ((iter = map.find(key)) != map.end())
00199       {
00200         std::ostringstream osx, osy;
00201         osx << iter->second.first;
00202         xercesc::DOMElement* nd = doc_->createElement(transcode("drawing"));
00203         root->appendChild(nd);
00204         tools::set_attribute(nd, "labelPositionX", osx.str());
00205         osy << iter->second.second;
00206         tools::set_attribute(nd, "labelPositionY", osy.str());
00207       }
00208     }
00209 
00210 
00224     template <class Auto>
00225     template <class IStream>
00226     void xml_converter<Auto>::load(Auto& aut,
00227                                    IStream& in)
00228     {
00229       root_ = xerces_parser::stream_parser(in);
00230 
00231       typedef Node<Auto> node_t;
00232       Factory<node_t, std::string> f;
00233       register_all_factory(f, Auto);
00234       typename node_t::map_t str2state;
00235 
00236       node_t* node = factory_create(f, xml2str(root_->getNodeName()));
00237       node->process(root_, aut, str2state, f);
00238     }
00239 
00240 
00241   } // !xml
00242 
00243 } // !vcsn
00244 
00245 
00246 #endif // ! VCSN_XML_XML_CONVERTER_HXX

Generated on Sat Jul 29 17:13:13 2006 for Vaucanson by  doxygen 1.4.6