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, 2007 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     xml_converter<Auto>::xml_converter (bool use_label_node)
00041         : impl_(0), doc_(0), root_(0), use_label_node_(use_label_node)
00042     {}
00043 
00044     template <class Auto>
00045     template <class OStream>
00046     void xml_converter<Auto>::save(const Auto& aut, OStream& os,
00047                                    const std::string& name)
00048     {
00049       create_document(aut, name);
00050       tools::print_document(impl_, root_, os);
00051     }
00052 
00053     template <class Auto>
00054     void xml_converter<Auto>::create_document(const Auto& aut,
00055                                               const std::string& name)
00056     {
00057       typedef typename Auto::state_iterator state_iterator;
00058       typedef typename Auto::transition_iterator transition_iterator;
00059       typedef typename Auto::initial_iterator initial_iterator;
00060       typedef typename Auto::final_iterator final_iterator;
00061       using namespace xercesc;
00062 
00063       // Document creation.
00064       impl_ = DOMImplementationRegistry::getDOMImplementation(transcode("LS"));
00065       doc_ = impl_->createDocument(transcode(VCSN_XMLNS),
00066                                    transcode("automaton"), 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 = tools::create_element(doc_, "content");
00076       root_->appendChild(content);
00077 
00078       // Create states.
00079       DOMElement* node = tools::create_element(doc_, "states");
00080       content->appendChild(node);
00081       map_t state2str;
00082       for_all_states(s, aut)
00083         state2str[*s] = create_state(*s, aut, node);
00084 
00085       // Create transitions.
00086       node = tools::create_element(doc_, "transitions");
00087       content->appendChild(node);
00088       for_all_transitions(e, aut)
00089         create_transition(*e, aut, node, state2str);
00090 
00091       // Create initial transitions.
00092       for_all_initial_states(i, aut)
00093         create_initial(*i, aut, node, state2str);
00094 
00095       // Create final transitions.
00096       for_all_final_states(f, aut)
00097         create_final(*f, aut, node, state2str);
00098     }
00099 
00100 
00101     template <class Auto>
00102     xercesc::DOMElement*
00103     xml_converter<Auto>::root_get()
00104     {
00105       return root_;
00106     }
00107 
00108     // Create a state in the XML document.
00109     template <class Auto>
00110     std::string
00111     xml_converter<Auto>::create_state(hstate_t s,
00112                                       const Auto& aut,
00113                                       xercesc::DOMElement* root)
00114     {
00115       xercesc::DOMElement* node = tools::create_element(doc_, "state");
00116       root->appendChild(node);
00117       std::ostringstream os;
00118       os << "s" << s;
00119       tools::set_attribute(node, "name", os.str());
00120       add_xml_geometry(aut.geometry().states(), s, node);
00121 
00122       return os.str();
00123     }
00124 
00125 
00126     // Create a transition in the XML document.
00127     template <class Auto>
00128     void
00129     xml_converter<Auto>::create_transition(htransition_t e,
00130                                            const Auto& aut,
00131                                            xercesc::DOMElement* root,
00132                                            map_t& state2str)
00133     {
00134       xercesc::DOMElement* node = tools::create_element(doc_, "transition");
00135       root->appendChild(node);
00136       tools::set_attribute(node, "src", state2str[aut.src_of(e)]);
00137       tools::set_attribute(node, "dst", state2str[aut.dst_of(e)]);
00138       chooser_.create_label(doc_, e, aut, node, use_label_node_);
00139       add_xml_drawing(aut.geometry().transitions(), e, node);
00140     }
00141 
00142 
00143     // Create an initial state in the XML document.
00144     template <class Auto>
00145     void
00146     xml_converter<Auto>::create_initial(hstate_t s,
00147                                         const Auto& aut,
00148                                         xercesc::DOMElement* root,
00149                                         map_t& state2str)
00150     {
00151       xercesc::DOMElement* node = tools::create_element(doc_, "initial");
00152       root->appendChild(node);
00153       tools::set_attribute(node, "state", state2str[s]);
00154       chooser_.create_initial_label(doc_, s, aut, node, use_label_node_);
00155       add_xml_drawing(aut.geometry().initials(), s, node);
00156     }
00157 
00158 
00159     // Create a final state in the XML document.
00160     template <class Auto>
00161     void
00162     xml_converter<Auto>::create_final(hstate_t s,
00163                                       const Auto& aut,
00164                                       xercesc::DOMElement* root,
00165                                       map_t& state2str)
00166     {
00167       xercesc::DOMElement* node = tools::create_element(doc_, "final");
00168       root->appendChild(node);
00169       tools::set_attribute(node, "state", state2str[s]);
00170       chooser_.create_final_label(doc_, s, aut, node, use_label_node_);
00171       add_xml_drawing(aut.geometry().finals(), s, node);
00172     }
00173 
00174 
00175     // Add geometry informations in the XML document.
00176     template <class Auto>
00177     template <class Map, class Key>
00178     void
00179     xml_converter<Auto>::add_xml_geometry(Map& map,
00180                                           Key& key,
00181                                           xercesc::DOMElement* root)
00182     {
00183       typename Map::const_iterator iter;
00184       if ((iter = map.find(key)) != map.end())
00185       {
00186         std::ostringstream osx, osy;
00187         osx << iter->second.first;
00188         xercesc::DOMElement* nd = tools::create_element(doc_, "geometry");
00189         root->appendChild(nd);
00190         tools::set_attribute(nd, "x", osx.str());
00191         osy << iter->second.second;
00192         tools::set_attribute(nd, "y", osy.str());
00193       }
00194     }
00195 
00196 
00197     // Add drawing informations in the XML document.
00198     template <class Auto>
00199     template <class Map, class Key>
00200     void
00201     xml_converter<Auto>::add_xml_drawing(Map& map,
00202                                          Key& key,
00203                                          xercesc::DOMElement* root)
00204     {
00205       typename Map::const_iterator iter;
00206       if ((iter = map.find(key)) != map.end())
00207       {
00208         std::ostringstream osx, osy;
00209         osx << iter->second.first;
00210         xercesc::DOMElement* nd = tools::create_element(doc_, "drawing");
00211         root->appendChild(nd);
00212         tools::set_attribute(nd, "labelPositionX", osx.str());
00213         osy << iter->second.second;
00214         tools::set_attribute(nd, "labelPositionY", osy.str());
00215       }
00216     }
00217 
00218 
00232     template <class Auto>
00233     template <class IStream>
00234     void
00235     xml_converter<Auto>::load(Auto& aut, 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 Sun Jul 29 19:35:31 2007 for Vaucanson by  doxygen 1.5.2