322 lines
8.2 KiB
C++
322 lines
8.2 KiB
C++
// 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 "xml_serializer_factory.hh"
|
|
|
|
#include "string_utils.hh"
|
|
#include "environment.hh"
|
|
#include "history.hh"
|
|
/*
|
|
#include "backend/concrete_environment.hh"
|
|
#include "backend/concrete_history.hh"
|
|
*/
|
|
#include "backend/process.hh"
|
|
#include "backend/serializer_error.hh"
|
|
|
|
using namespace sgpem;
|
|
|
|
|
|
|
|
#include <iostream>
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
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
|
|
XMLSerializerFactory fact(hist);
|
|
|
|
#ifdef LIBXML_SAX1_ENABLED
|
|
xmlDocPtr doc;
|
|
|
|
LIBXML_TEST_VERSION
|
|
xmlKeepBlanksDefault(0);
|
|
|
|
/*
|
|
* build an XML tree from a the file;
|
|
*/
|
|
doc = xmlParseFile(filename.c_str());
|
|
if (doc == NULL) {
|
|
xmlCleanupParser();
|
|
throw SerializerError("Parsing Error: doc is invalid.");
|
|
}
|
|
// read all elements and fill hist
|
|
read_doc(doc, fact);
|
|
|
|
// frees all xml related data
|
|
xmlFreeDoc(doc);
|
|
|
|
/* Clean up everything else before quitting. */
|
|
xmlCleanupParser();
|
|
|
|
#else
|
|
/*
|
|
* the library has been compiled without some of the old interfaces
|
|
*/
|
|
throw SerializerError("ERROR: Compilation with SAX1 must be enabled! (?)");
|
|
#endif /* LIBXML_SAX1_ENABLED */
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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_node, const History& hist)
|
|
{
|
|
|
|
|
|
const Environment& env = hist.get_last_environment();
|
|
|
|
const Environment::Resources& rvect = env.get_resources();
|
|
typedef Environment::Resources::const_iterator res_iterator;
|
|
|
|
res_iterator iter = rvect.begin();
|
|
res_iterator end = rvect.end();
|
|
while(iter!=end)
|
|
{
|
|
XMLVisitor xvisit(resources_node);
|
|
Glib::ustring key;
|
|
int_to_string((int)(*iter).first, key);
|
|
xvisit.from_resource(*((*iter).second), key);
|
|
iter++;
|
|
}
|
|
}
|
|
|
|
void XMLSerializer::fill_schedulables(xmlNodePtr schedulables_node, 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_node);
|
|
xvisit.from_process(*(*iter));
|
|
iter++;
|
|
}
|
|
}
|
|
|
|
void XMLSerializer::read_doc(xmlDocPtr doc, XMLSerializerFactory& fact)
|
|
{
|
|
/*
|
|
* Check the document is of the right kind
|
|
*/
|
|
xmlNodePtr root;
|
|
root = xmlDocGetRootElement(doc);
|
|
if (root == NULL) {
|
|
xmlFreeDoc(doc);
|
|
xmlCleanupParser();
|
|
throw SerializerError("Reading Error: xml doc is empty.");
|
|
}
|
|
|
|
cout << "ROOT: " << root->name << endl;
|
|
|
|
xmlNodePtr cur;
|
|
cur = root->children;
|
|
while(cur!=NULL)
|
|
{
|
|
cout << "NODE: " << cur->name << endl;
|
|
|
|
Glib::ustring name((const char *)cur->name);
|
|
if(name=="resources")
|
|
{
|
|
read_resources(cur, fact);
|
|
}
|
|
if(name=="schedulables")
|
|
{
|
|
read_schedulables(cur, fact);
|
|
}
|
|
cur = cur->next;
|
|
}
|
|
}
|
|
|
|
XMLSerializerFactory::Parameters* read_properties(xmlAttrPtr prop)
|
|
{
|
|
if(prop==NULL)
|
|
return NULL;
|
|
|
|
XMLSerializerFactory::Parameters* par=new XMLSerializerFactory::Parameters();
|
|
while (prop != NULL) {
|
|
// cout << "PROP: " << prop->name;
|
|
|
|
if(prop->children && xmlNodeIsText(prop->children)){
|
|
xmlChar *key = xmlNodeGetContent (prop->children);
|
|
// xmlChar *key = xmlNodeListGetString(doc, prop->children, 1);
|
|
if(key!=NULL)
|
|
{
|
|
// cout << " VALUE: " << key;
|
|
std::pair<Glib::ustring, Glib::ustring> key_value(Glib::ustring((const char *)prop->name), Glib::ustring((const char *)key));
|
|
par->insert(key_value);
|
|
// c.insert(typename Cont::value_type(new_key, pos->second));
|
|
cout << " pair PROP: " << key_value.first << " VALUE: " << key_value.second << endl;
|
|
|
|
xmlFree(key);
|
|
}
|
|
else
|
|
cout << " !VALUE IS NULL! ";
|
|
}
|
|
cout << endl;
|
|
prop = prop->next;
|
|
}
|
|
return par;
|
|
}
|
|
|
|
void XMLSerializer::read_resources(xmlNodePtr resources_node, XMLSerializerFactory& fact)
|
|
{
|
|
xmlNodePtr cur;
|
|
cur = resources_node->children;
|
|
while(cur!=NULL)
|
|
{
|
|
Glib::ustring node_name((const char *)cur->name);
|
|
if(node_name=="resource")
|
|
{
|
|
cout << "NODE: " << cur->name << endl;
|
|
|
|
xmlAttrPtr prop = cur->properties;
|
|
XMLSerializerFactory::Parameters* par=read_properties(prop);
|
|
if(par!=NULL)
|
|
{
|
|
fact.factory_method(Glib::ustring("Resource"), *par);
|
|
}
|
|
// fact.create_resource(*par);
|
|
}
|
|
|
|
cur = cur->next;
|
|
}
|
|
}
|
|
void XMLSerializer::read_schedulables(xmlNodePtr schedulables_node, XMLSerializerFactory& fact)
|
|
{
|
|
xmlNodePtr cur;
|
|
cur = schedulables_node->children;
|
|
while(cur!=NULL)
|
|
{
|
|
cout << "NODE: " << cur->name << endl;
|
|
|
|
xmlAttrPtr prop = cur->properties;
|
|
while (prop != NULL) {
|
|
cout << "PROP: " << prop->name;
|
|
|
|
if(prop->children && xmlNodeIsText(prop->children)){
|
|
xmlChar *key = xmlNodeGetContent (prop->children);
|
|
// xmlChar *key = xmlNodeListGetString(doc, prop->children, 1);
|
|
if(key!=NULL)
|
|
{
|
|
cout << " VALUE: " << key;
|
|
xmlFree(key);
|
|
}
|
|
else
|
|
cout << " !VALUE IS NULL! ";
|
|
}
|
|
cout << endl;
|
|
prop = prop->next;
|
|
}
|
|
|
|
cur = cur->next;
|
|
}
|
|
}
|
|
|
|
|
|
/* comment */
|