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 
00032 namespace vcsn
00033 {
00034   namespace xml
00035   {
00036     template <class Auto>
00037     template <class OStream>
00038     void xml_converter<Auto>::save(const Auto& aut, OStream& os,
00039                                    const std::string& name)
00040     {
00041       create_document(aut, name);
00042       tools::print_document(root_, os);
00043     }
00044 
00045     template <class Auto>
00046     void xml_converter<Auto>::create_document(const Auto& aut,
00047                                               const std::string& name)
00048     {
00049       typedef typename Auto::state_iterator state_iterator;
00050       typedef typename Auto::transition_iterator transition_iterator;
00051       typedef typename Auto::initial_iterator initial_iterator;
00052       typedef typename Auto::final_iterator final_iterator;
00053       using namespace xercesc;
00054 
00055       std::map<hstate_t, std::string> state2str;
00056       const char* root_name = chooser_.choose_start_tag();
00057       const char* xml_namespace = "http://vaucanson.lrde.epita.fr";
00058       DOMElement* node;
00059 
00060       // Document creation.
00061       impl_ = DOMImplementationRegistry::getDOMImplementation(STR2XML("LS"));
00062       doc_ = impl_->createDocument(STR2XML(xml_namespace),
00063                                    STR2XML(root_name), 0);
00064       root_ = doc_->getDocumentElement();
00065 
00066       if (aut.geometry().name() != "")
00067         root_->setAttribute(STR2XML("name"),
00068                             STR2XML(aut.geometry().name().c_str()));
00069       if (name != "")
00070         root_->setAttribute(STR2XML("name"), STR2XML(name.c_str()));
00071 
00072       // Create type tag.
00073       chooser_.create_type_tag(aut, doc_, root_);
00074 
00075       DOMElement* content = doc_->createElement(STR2XML("content"));
00076       root_->appendChild(content);
00077 
00078       // Create states.
00079       node = doc_->createElement(STR2XML("states"));
00080       content->appendChild(node);
00081       for_each_state(s, aut)
00082         state2str[*s] = create_state(*s, aut, node);
00083 
00084       // Create transitions.
00085       node = doc_->createElement(STR2XML("transitions"));
00086       content->appendChild(node);
00087       for_each_transition(e, aut)
00088         create_transition(*e, aut, node, state2str);
00089 
00090       // Create initial transitions.
00091       for_each_initial_state(i, aut)
00092         create_initial(*i, aut, node, state2str);
00093 
00094       // Create final transitions.
00095       for_each_final_state(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(STR2XML("state"));
00109       root->appendChild(node);
00110       node->setAttribute(STR2XML("name"), STR2XML(os.str().c_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(STR2XML("transition"));
00127       root->appendChild(node);
00128       node->setAttribute(STR2XML("src"),
00129                          STR2XML(state2str[aut.src_of(e)].c_str()));
00130       node->setAttribute(STR2XML("dst"),
00131                          STR2XML(state2str[aut.dst_of(e)].c_str()));
00132       chooser_.create_label(doc_, e, aut, node, use_label_node_);
00133       add_xml_drawing(aut.geometry().transitions(), e, node);
00134     }
00135 
00136 
00137     // Create an initial state in the XML document.
00138     template <class Auto>
00139     void
00140     xml_converter<Auto>::create_initial(hstate_t s,
00141                                         const Auto& aut,
00142                                         xercesc::DOMElement* root,
00143                                         std::map<hstate_t, std::string>&
00144                                         state2str)
00145     {
00146       xercesc::DOMElement* node = doc_->createElement(STR2XML("initial"));
00147       root->appendChild(node);
00148       node->setAttribute(STR2XML("state"),
00149                          STR2XML(state2str[s].c_str()));
00150       chooser_.create_initial_label(doc_, s, aut, node, use_label_node_);
00151       add_xml_drawing(aut.geometry().initials(), s, node);
00152     }
00153 
00154 
00155     // Create a final state in the XML document.
00156     template <class Auto>
00157     void
00158     xml_converter<Auto>::create_final(hstate_t s,
00159                                       const Auto& aut,
00160                                       xercesc::DOMElement* root,
00161                                       std::map<hstate_t, std::string>&
00162                                       state2str)
00163     {
00164       xercesc::DOMElement* node = doc_->createElement(STR2XML("final"));
00165       root->appendChild(node);
00166       node->setAttribute(STR2XML("state"),
00167                          STR2XML(state2str[s].c_str()));
00168       chooser_.create_final_label(doc_, s, aut, node, use_label_node_);
00169       add_xml_drawing(aut.geometry().finals(), s, node);
00170     }
00171 
00172 
00173     // Add geometry informations in the XML document.
00174     template <class Auto>
00175     template <class Map, class Key>
00176     void xml_converter<Auto>::add_xml_geometry(Map& map,
00177                                                Key& key,
00178                                                xercesc::DOMElement* root)
00179     {
00180       typename Map::const_iterator iter;
00181       if ((iter = map.find(key)) != map.end())
00182       {
00183         std::ostringstream osx, osy;
00184         osx << iter->second.first;
00185         xercesc::DOMElement* nd = doc_->createElement(STR2XML("geometry"));
00186         root->appendChild(nd);
00187         nd->setAttribute(STR2XML("x"),
00188                          STR2XML(osx.str().c_str()));
00189         osy << iter->second.second;
00190         nd->setAttribute(STR2XML("y"),
00191                          STR2XML(osy.str().c_str()));
00192       }
00193     }
00194 
00195 
00196     // Add drawing informations in the XML document.
00197     template <class Auto>
00198     template <class Map, class Key>
00199     void xml_converter<Auto>::add_xml_drawing(Map& map,
00200                                               Key& key,
00201                                               xercesc::DOMElement* root)
00202     {
00203       typename Map::const_iterator iter;
00204       if ((iter = map.find(key)) != map.end())
00205       {
00206         std::ostringstream osx, osy;
00207         osx << iter->second.first;
00208         xercesc::DOMElement* nd = doc_->createElement(STR2XML("drawing"));
00209         root->appendChild(nd);
00210         nd->setAttribute(STR2XML("labelPositionX"),
00211                          STR2XML(osx.str().c_str()));
00212         osy << iter->second.second;
00213         nd->setAttribute(STR2XML("labelPositionY"),
00214                          STR2XML(osy.str().c_str()));
00215       }
00216     }
00217 
00218 
00232     template <class Auto>
00233     template <class IStream>
00234     void xml_converter<Auto>::load(Auto& aut,
00235                                    IStream& in)
00236     {
00237       root_ = xerces_parser::stream_parser(in);
00238 
00239       typedef Node<Auto> node_t;
00240       Factory<node_t, std::string> f;
00241       register_all_factory(f, Auto);
00242       typename node_t::map_t str2state;
00243 
00244       node_t* node = factory_create(f, xml2str(root_->getNodeName()));
00245       node->process(root_, aut, str2state, f);
00246     }
00247 
00248 
00249   } // !xml
00250 
00251 } // !vcsn
00252 
00253 
00254 #endif // ! VCSN_XML_XML_CONVERTER_HXX

Generated on Fri Jul 28 12:18:56 2006 for Vaucanson by  doxygen 1.4.6