session.hxx

Go to the documentation of this file.
00001 // session.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_SESSION_HXX
00018 # define VCSN_XML_SESSION_HXX
00019 
00032 namespace vcsn
00033 {
00034   namespace xml
00035   {
00036 
00037     xml_session::xml_session()
00038     {
00039       using namespace xercesc;
00040 
00041       XMLPlatformUtils::Initialize();
00042       impl = DOMImplementationRegistry::getDOMImplementation(transcode("LS"));
00043       doc = impl->createDocument(transcode(VCSN_XMLNS), transcode("session"), 0);
00044       root = doc->getDocumentElement();
00045     }
00046 
00047 
00048     xml_session::~xml_session()
00049     {
00050       xercesc::XMLPlatformUtils::Terminate();
00051     }
00052 
00053   } // ! xml
00054 
00055   template<typename S, typename T>
00056   xml::xml_session&
00057   op_rout(const S& structure,
00058           xml::xml_session& s,
00059           const T& value)
00060   {
00061     xercesc::XMLPlatformUtils::Initialize();
00062 
00063     Element<S, T> a(structure, value);
00064     xml::xml_converter<Element<S, T> > x;
00065     x.create_document(a);
00066     xercesc::DOMNode* node =
00067       s.doc->importNode(static_cast<xercesc::DOMNode*>(x.root_get()), true);
00068 
00069     s.root->appendChild(node);
00070 
00071     return s;
00072   }
00073 
00074 
00075   template <class S, class T>
00076   xml::xml_session& op_rin(S& structure,
00077                            xml::xml_session& s,
00078                            T& value)
00079   {
00080     using namespace xercesc;
00081     using namespace vcsn;
00082     using namespace vcsn::xml;
00083 
00084     XMLPlatformUtils::Initialize();
00085     Element<S, T> a(structure, value);
00086 
00087     // Clear the automaton content.
00088     for (typename Element<S, T>::state_iterator is = a.states().begin();
00089          is != a.states().end(); ++is)
00090       a.del_state(*is);
00091 
00092     if (! s.root->getFirstChild())
00093       FAIL("No more automaton in session");
00094     for (DOMNode* n = s.root->getFirstChild(); n; n = n->getNextSibling())
00095       if (n->getNodeType() == DOMNode::ELEMENT_NODE)
00096       {
00097         DOMElement* elt = static_cast<DOMElement*>(n);
00098 
00099         typedef Element<S, T> automaton_t;
00100         typedef Node<automaton_t> node_t;
00101         Factory<node_t, std::string> f;
00102         register_all_factory(f, automaton_t);
00103         typename node_t::map_t str2state;
00104 
00105         node_t* node = factory_create(f, xml2str(n->getNodeName()));
00106         node->process(elt, a, str2state, f);
00107         s.root->removeChild(n);
00108       }
00109     op_assign(structure.self(), value, a.value());
00110 
00111     return s;
00112   }
00113 
00114 
00115   template <class IStream>
00116   IStream& operator >> (IStream& is, xml::xml_session& s)
00117   {
00118     using namespace xercesc;
00119 
00120     xercesc::XMLPlatformUtils::Initialize();
00121     DOMNode* node = xml::xerces_parser::stream_parser(is);
00122 
00123     for (DOMNode* n = node->getFirstChild(); n; n = n->getNextSibling())
00124       if (n->getNodeType() == DOMNode::ELEMENT_NODE)
00125         (s.root)->appendChild(s.doc->importNode(n, true));
00126 
00127     return is;
00128   }
00129 
00130 
00131   template <class OStream>
00132   OStream& operator << (OStream& out, const xml::xml_session& s)
00133   {
00134     xercesc::XMLPlatformUtils::Initialize();
00135     xml::tools::print_document(s.impl, s.root, out);
00136 
00137     return out;
00138   }
00139 
00140 
00141 } // !vcsn
00142 
00143 #endif // ! VCSN_XML_SESSION_HXX

Generated on Sun Jul 29 19:35:29 2007 for Vaucanson by  doxygen 1.5.2