sgpemv2/plugins/xmlsave/src/xml_serializer.cc

149 lines
4.4 KiB
C++
Raw Normal View History

// src/backend/serialize_visitor.cc - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied
// Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SGPEMv2 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "xml_serializer.hh"
#include "xml_visitor.hh"
#include "backend/string_utils.hh"
#include "backend/environment.hh"
#include "backend/history.hh"
#include "backend/concrete_environment.hh"
#include "backend/concrete_history.hh"
#include "backend/process.hh"
#include "backend/serializer_error.hh"
using namespace sgpem;
XMLSerializer::~XMLSerializer()
{
}
XMLSerializer::XMLSerializer()
{
}
void XMLSerializer::save_snapshot(const Glib::ustring& filename, const History& hist)
{
/* COMPAT: Do not genrate nodes for formatting spaces */
LIBXML_TEST_VERSION
xmlKeepBlanksDefault(0);
xmlDocPtr doc;
doc = xmlNewDoc((const xmlChar *)"1.0");
if(doc!=NULL){
fill_doc(doc, hist);
int nwritten = xmlSaveFormatFile (filename.c_str(), doc, 1);
if(nwritten<0)
{
throw SerializerError("Error writing xml doc to output stream.");
}
}
else
{
throw SerializerError("Internal Error creating xml doc.");
}
/* Clean up everything else before quitting. */
xmlCleanupParser();
}
void XMLSerializer::restore_snapshot(const Glib::ustring& filename, History& hist)
{
// TODO - all to do!!
// DEBUG - remove me when finished
}
const Glib::ustring XMLSerializer::get_filename_extension()
{
return Glib::ustring("ocio");
}
const Glib::ustring XMLSerializer::get_filename_description()
{
return Glib::ustring("SGPEMv2 XML formatted snapshot save file");
}
void XMLSerializer::fill_doc(xmlDocPtr doc, const History& hist)
{
xmlNodePtr root_node = NULL;/* node pointers */
/*
* Creates a new document, a node and set it as a root node
*/
root_node = xmlNewNode(NULL, (const xmlChar *) "sgpem");
xmlDocSetRootElement(doc, root_node);
/*
* Creates a DTD declaration. Isn't mandatory.
*/
// dtd = xmlCreateIntSubset(doc, BAD_CAST "root", NULL, BAD_CAST "tree2.dtd");
/*
* xmlNewChild() creates a new node, which is "attached" as child node
* of root_node node.
*/
xmlNodePtr resources_node = xmlNewChild(root_node, NULL, (const xmlChar *) "resources", NULL);
/*
* The same as above, but the new child node doesn't have a content
*/
xmlNodePtr schedulables_node = xmlNewChild(root_node, NULL, (const xmlChar *) "schedulables", NULL);
fill_resources(resources_node, hist);
fill_schedulables(schedulables_node, hist);
}
void XMLSerializer::fill_resources(xmlNodePtr resources, const History& hist)
{
}
void XMLSerializer::fill_schedulables(xmlNodePtr schedulables, const History& hist)
{
const Environment& env = hist.get_last_environment();
const Environment::Processes& pvect = env.get_processes();
typedef std::vector<Process*>::const_iterator proc_iterator;
proc_iterator iter = pvect.begin();
proc_iterator end = pvect.end();
while(iter!=end)
{
XMLVisitor xvisit(schedulables);
xvisit.from_process(*(*iter));
/*
Glib::ustring strPriority;
Glib::ustring strArrivalTime;
Process* p = (*iter);
int_to_string(p->get_base_priority(), strPriority);
int_to_string(p->get_arrival_time(), strArrivalTime);
xmlNodePtr process_node = xmlNewChild(schedulables, NULL, (const xmlChar *) p->get_name().c_str(), NULL);
xmlNewProp(process_node, (const xmlChar *) "name", (const xmlChar *) p->get_name().c_str());
xmlNewProp(process_node, (const xmlChar *) "priority", (const xmlChar *) strPriority.c_str());
xmlNewProp(process_node, (const xmlChar *) "arrival-time", (const xmlChar *) strArrivalTime.c_str());
*/
iter++;
}
}