2005-11-04 16:37:58 +01:00
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
#include <map>
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include "SlotsNode.h"
|
|
|
|
#include "ClassesNode.h"
|
|
|
|
#include "InstancesNode.h"
|
|
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char* XML_SCHEMA_NAME = "protegekb";
|
|
|
|
|
|
|
|
|
|
|
|
class FacetNode: public Node
|
|
|
|
{
|
|
|
|
virtual void printNode();
|
|
|
|
virtual int qualifyNode();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FacetsNode: public Node
|
|
|
|
{
|
|
|
|
NodeGroup facets;
|
|
|
|
virtual void printNode();
|
|
|
|
virtual int qualifyNode();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MainNode: public Node
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
MainNode(void){node_type = MAIN_NODE;};
|
|
|
|
int node_type;
|
2006-01-26 17:14:48 +01:00
|
|
|
list<pair<const char* , const char* > > headers;
|
2005-11-04 16:37:58 +01:00
|
|
|
|
2006-01-26 17:14:48 +01:00
|
|
|
void addHeaders(const char* str, const char* val);
|
2005-11-04 16:37:58 +01:00
|
|
|
virtual void printNode();
|
|
|
|
virtual int qualifyNode();
|
|
|
|
};
|
|
|
|
|
2005-12-02 01:47:15 +01:00
|
|
|
void MainNode::addHeaders(const char* str, const char*val)
|
2005-11-04 16:37:58 +01:00
|
|
|
{
|
2005-12-02 01:47:15 +01:00
|
|
|
headers.push_back(pair<const char*, const char*>(str,val));
|
2005-11-04 16:37:58 +01:00
|
|
|
}
|
|
|
|
void MainNode::printNode()
|
|
|
|
{
|
2005-12-19 15:38:38 +01:00
|
|
|
cout << "MainNode: node_type is " << node_type << "\n";
|
2005-11-04 16:37:58 +01:00
|
|
|
|
2005-12-02 01:47:15 +01:00
|
|
|
list<pair<const char*,const char*> >::iterator it;
|
2005-12-19 15:38:38 +01:00
|
|
|
|
2005-11-04 16:37:58 +01:00
|
|
|
for(it = headers.begin(); it!= headers.end(); ++it)
|
|
|
|
{
|
2005-12-19 15:38:38 +01:00
|
|
|
cout << "MainNode: First element is " << it->first << "\n";
|
|
|
|
cout << "MainNode: Second element is " << it->second << "\n";
|
2005-11-04 16:37:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int MainNode::qualifyNode()
|
|
|
|
{return node_type;}
|
|
|
|
|
|
|
|
|
|
|
|
class XMLTree
|
|
|
|
{
|
2005-12-19 15:38:38 +01:00
|
|
|
private:
|
|
|
|
|
|
|
|
static const char *DEFAULT_NAME;
|
|
|
|
string filename;
|
|
|
|
bool verbose;
|
|
|
|
|
2005-11-04 16:37:58 +01:00
|
|
|
public:
|
2005-12-19 15:38:38 +01:00
|
|
|
XMLTree(void){verbose = false;}
|
|
|
|
~XMLTree(void){}
|
2005-11-04 16:37:58 +01:00
|
|
|
NodeGroup root;
|
|
|
|
NodeGroup ng;
|
|
|
|
SlotNode sn;
|
|
|
|
|
|
|
|
|
|
|
|
// methods
|
2005-12-19 15:38:38 +01:00
|
|
|
void setName(const char *name);
|
|
|
|
void setVerboseMode(bool flag);
|
2005-11-04 16:37:58 +01:00
|
|
|
void initializeMain();
|
|
|
|
void finalizeMain();
|
2005-12-02 01:47:15 +01:00
|
|
|
void addHeaders(const char* str, const char*val);
|
2005-11-04 16:37:58 +01:00
|
|
|
|
|
|
|
void addSlots(SlotNode* sn);
|
2005-12-19 15:38:38 +01:00
|
|
|
//void addFacets();
|
2005-11-04 16:37:58 +01:00
|
|
|
void addClasses(ClassNode* cn);
|
|
|
|
void addInstances(InstanceNode* in);
|
2005-12-02 01:47:15 +01:00
|
|
|
void addNode(const char* s, int value_type, const char* name);
|
2005-11-04 16:37:58 +01:00
|
|
|
|
|
|
|
void printXMLTree();
|
|
|
|
|
|
|
|
};
|
2005-12-19 15:38:38 +01:00
|
|
|
const char * XMLTree::DEFAULT_NAME = "XmlTree.xml";
|
|
|
|
|
|
|
|
void XMLTree::setName(const char *name)
|
|
|
|
{
|
|
|
|
filename = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void XMLTree::setVerboseMode(bool flag)
|
|
|
|
{
|
|
|
|
verbose = flag;
|
|
|
|
}
|
2005-11-04 16:37:58 +01:00
|
|
|
|
|
|
|
void XMLTree::initializeMain()
|
|
|
|
{
|
|
|
|
MainNode* mn = new MainNode;
|
|
|
|
|
2006-01-26 17:14:48 +01:00
|
|
|
mn->headers.push_back(pair<const char*, const char*>("protegekb", ""));
|
2005-11-04 16:37:58 +01:00
|
|
|
|
|
|
|
char* s1 = "http://www.w3.org/2001/XMLSchema-instance";
|
2006-01-26 17:14:48 +01:00
|
|
|
char* s2 = new char[100];
|
2005-11-04 16:37:58 +01:00
|
|
|
sprintf(s2,"\"%s\"",s1);
|
|
|
|
mn->addHeaders(" xmlns:xsi=", s2);
|
|
|
|
|
|
|
|
s1 = "http://protege.stanford.edu/plugins/xmlbackend/protege_xml_backend.xsd";
|
|
|
|
sprintf(s2,"\"%s\"",s1);
|
|
|
|
mn->addHeaders(" xsi:noNamespaceSchemaLocation=", s2);
|
|
|
|
root.Sons.push_back(mn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void XMLTree::finalizeMain()
|
|
|
|
{
|
|
|
|
addSlots(&sn);
|
|
|
|
|
|
|
|
OwnSlotsNode* ossn = new OwnSlotsNode;
|
|
|
|
ossn->addOwnSlot(&ng);
|
|
|
|
ClassNode* cn = new ClassNode;
|
|
|
|
cn->addOwnSlots(ossn);
|
|
|
|
|
|
|
|
addClasses(cn);
|
|
|
|
|
|
|
|
InstanceNode* in = new InstanceNode;
|
|
|
|
in->addOwnSlots(ossn);
|
|
|
|
|
|
|
|
addInstances(in);
|
|
|
|
MainNode* mn = new MainNode;
|
|
|
|
|
2005-12-02 01:47:15 +01:00
|
|
|
mn->headers.push_back(pair<const char*,const char*>("/",XML_SCHEMA_NAME));
|
2005-11-04 16:37:58 +01:00
|
|
|
root.Sons.push_back(mn);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-02 01:47:15 +01:00
|
|
|
void XMLTree::addHeaders(const char* str, const char*val)
|
2005-11-04 16:37:58 +01:00
|
|
|
{
|
2006-01-27 14:35:10 +01:00
|
|
|
MainNode* mn = dynamic_cast<MainNode *>(root.Sons.front());
|
2005-12-02 01:47:15 +01:00
|
|
|
mn->headers.push_back(pair<const char*,const char*>(str,val));
|
2005-11-04 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void XMLTree::addSlots(SlotNode* sn)
|
|
|
|
{
|
|
|
|
SlotsNode* sn0 = new SlotsNode; // 1 solo
|
|
|
|
|
|
|
|
sn0->addSlot(sn);
|
|
|
|
root.Sons.push_back(sn0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void XMLTree::addClasses(ClassNode* cn)
|
|
|
|
{
|
|
|
|
ClassesNode* cn0 = new ClassesNode; // 1 solo
|
|
|
|
|
|
|
|
cn0->addClass(cn);
|
|
|
|
root.Sons.push_back(cn0);
|
|
|
|
}
|
|
|
|
|
2005-12-02 01:47:15 +01:00
|
|
|
void XMLTree::addNode(const char* s, int value_type, const char* name)
|
2005-11-04 16:37:58 +01:00
|
|
|
{
|
|
|
|
ValueNode* vn = new ValueNode;
|
|
|
|
EntryNode* en = new EntryNode;
|
|
|
|
OwnSlotNode* osn = new OwnSlotNode;
|
|
|
|
|
|
|
|
switch(value_type)
|
|
|
|
{
|
2006-01-27 10:15:14 +01:00
|
|
|
case VALUE_INTEGER:
|
|
|
|
en->type = "Integer";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VALUE_FLOAT:
|
|
|
|
en->type = "Float";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VALUE_BOOL:
|
|
|
|
en->type = "Bool";
|
|
|
|
break;
|
2005-11-04 16:37:58 +01:00
|
|
|
|
2006-01-27 10:15:14 +01:00
|
|
|
case VALUE_STRING:
|
|
|
|
en->type = "String";
|
|
|
|
break;
|
2005-11-04 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vn->setValue(s);
|
|
|
|
en->addValue(*vn);
|
|
|
|
osn->setName(name);
|
|
|
|
osn->addEntry(*en);
|
|
|
|
sn.addOwnSlot(osn);
|
|
|
|
ng.addNode(osn);
|
|
|
|
|
|
|
|
}
|
|
|
|
void XMLTree::addInstances(InstanceNode* in)
|
|
|
|
{
|
|
|
|
InstancesNode* in0 = new InstancesNode; // 1 solo
|
|
|
|
|
|
|
|
in0->addInstance(in);
|
|
|
|
root.Sons.push_back(in0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void XMLTree::printXMLTree()
|
|
|
|
{
|
2005-12-19 15:38:38 +01:00
|
|
|
FILE *fp;
|
2005-11-04 16:37:58 +01:00
|
|
|
|
2005-12-19 15:38:38 +01:00
|
|
|
if (filename.empty())
|
|
|
|
fp = fopen(DEFAULT_NAME, "w");
|
|
|
|
else
|
|
|
|
fp = fopen(filename.c_str(), "w");
|
2005-11-04 16:37:58 +01:00
|
|
|
|
|
|
|
list<Node*>::iterator it;
|
|
|
|
list<Node*>::iterator it2;
|
|
|
|
list<Node*>::iterator it3;
|
2005-12-02 01:47:15 +01:00
|
|
|
list<pair<const char*,const char*> >::iterator lit;
|
2005-11-04 16:37:58 +01:00
|
|
|
MainNode* mn;
|
|
|
|
SlotsNode* sns;
|
|
|
|
SlotNode* sn;
|
|
|
|
OwnSlotNode* osn;
|
|
|
|
ClassesNode* csn;
|
|
|
|
ClassNode* cn;
|
|
|
|
InstancesNode* isn;
|
|
|
|
InstanceNode* in;
|
|
|
|
int nn = 0;
|
2006-01-27 10:15:14 +01:00
|
|
|
|
2005-12-19 15:38:38 +01:00
|
|
|
for(it = root.Sons.begin(); it!=root.Sons.end(); ++it)
|
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
cout<<"Creating Node #"<< nn<<"\n";
|
|
|
|
cout<<"Node Type is "<< (*it)->qualifyNode()<<"\n";
|
|
|
|
}
|
|
|
|
|
2005-11-04 16:37:58 +01:00
|
|
|
switch((*it)->qualifyNode())
|
|
|
|
{
|
2006-01-27 10:15:14 +01:00
|
|
|
|
|
|
|
// MAIN NODE
|
2005-11-04 16:37:58 +01:00
|
|
|
case MAIN_NODE:
|
2006-01-27 10:15:14 +01:00
|
|
|
mn = dynamic_cast<MainNode *>(*it);
|
2005-11-04 16:37:58 +01:00
|
|
|
fprintf(fp,"<");
|
|
|
|
for(lit = mn->headers.begin(); lit!= mn->headers.end(); ++lit)
|
|
|
|
fprintf(fp,"%s%s", lit->first,lit->second );
|
|
|
|
fprintf(fp,"> \n");
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2006-01-27 10:15:14 +01:00
|
|
|
// SLOTS
|
2005-11-04 16:37:58 +01:00
|
|
|
case SLOTS_NODE:
|
2006-01-27 10:15:14 +01:00
|
|
|
sns = dynamic_cast<SlotsNode *>(*it);
|
|
|
|
fprintf(fp,"\t<slots>\n");
|
2005-11-04 16:37:58 +01:00
|
|
|
|
|
|
|
for(it2 = sns->slot.Sons.begin(); it2!=sns->slot.Sons.end(); ++it2)
|
|
|
|
{
|
2006-01-27 10:15:14 +01:00
|
|
|
sn = dynamic_cast<SlotNode *>(*it2);
|
|
|
|
fprintf(fp,"\t\t<slot>\n");
|
2005-11-04 16:37:58 +01:00
|
|
|
for(it3 = sn->own_slot.Sons.begin(); it3!=sn->own_slot.Sons.end(); ++it3)
|
|
|
|
{
|
2006-01-27 10:15:14 +01:00
|
|
|
osn = dynamic_cast<OwnSlotNode *>(*it3);
|
|
|
|
fprintf(fp,"\t\t\t<own-slot slot-name=\":%s\">\n",osn->name);
|
|
|
|
fprintf(fp,"\t\t\t\t<entry type=\"%s\">\n",osn->entry.type);
|
2006-01-27 14:35:10 +01:00
|
|
|
fprintf(fp,"\t\t\t\t\t<value>%s", osn->entry.value.value.c_str());
|
2006-01-27 10:15:14 +01:00
|
|
|
fprintf(fp,"</value>\n");
|
|
|
|
fprintf(fp,"\t\t\t\t</entry>\n");
|
|
|
|
fprintf(fp,"\t\t\t</own-slot>\n");
|
2005-11-04 16:37:58 +01:00
|
|
|
}
|
2006-01-27 10:15:14 +01:00
|
|
|
fprintf(fp,"\t\t</slot>\n");
|
2005-11-04 16:37:58 +01:00
|
|
|
}
|
2006-01-27 10:15:14 +01:00
|
|
|
fprintf(fp,"\t</slots>\n");
|
2005-11-04 16:37:58 +01:00
|
|
|
|
2006-01-27 10:15:14 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
// CLASSES
|
|
|
|
case CLASSES_NODE:
|
|
|
|
csn = dynamic_cast<ClassesNode *>(*it);
|
|
|
|
fprintf(fp,"\t<classes>\n");
|
2005-11-04 16:37:58 +01:00
|
|
|
|
|
|
|
for(it2 = csn->classn.Sons.begin(); it2!=csn->classn.Sons.end(); ++it2)
|
|
|
|
{
|
2006-01-27 10:15:14 +01:00
|
|
|
cn = dynamic_cast<ClassNode *>(*it2);
|
|
|
|
fprintf(fp,"\t\t<class>\n");
|
|
|
|
fprintf(fp,"\t\t\t<own-slots>\n");
|
|
|
|
for(it3 = cn->own_slots.own_slot.Sons.begin();
|
|
|
|
it3!=cn->own_slots.own_slot.Sons.end(); ++it3)
|
2005-11-04 16:37:58 +01:00
|
|
|
{
|
2006-01-27 10:15:14 +01:00
|
|
|
osn = dynamic_cast<OwnSlotNode *>(*it3);
|
|
|
|
fprintf(fp,"\t\t\t\t<own-slot slot-name=\":%s\">\n",osn->name);
|
|
|
|
fprintf(fp,"\t\t\t\t\t<entry type=\"%s\">\n",osn->entry.type);
|
2006-01-27 14:35:10 +01:00
|
|
|
fprintf(fp,"\t\t\t\t\t\t<value>%s", osn->entry.value.value.c_str());
|
2006-01-27 10:15:14 +01:00
|
|
|
fprintf(fp,"</value>\n");
|
|
|
|
fprintf(fp,"\t\t\t\t\t</entry>\n");
|
|
|
|
fprintf(fp,"\t\t\t\t</own-slot>\n");
|
2005-11-04 16:37:58 +01:00
|
|
|
}
|
2006-01-27 10:15:14 +01:00
|
|
|
fprintf(fp,"\t\t\t</own-slots>\n");
|
|
|
|
fprintf(fp,"\t\t</class>\n");
|
2005-11-04 16:37:58 +01:00
|
|
|
}
|
2006-01-27 10:15:14 +01:00
|
|
|
fprintf(fp,"\t</classes>\n");
|
2005-11-04 16:37:58 +01:00
|
|
|
|
|
|
|
break;
|
2006-01-27 10:15:14 +01:00
|
|
|
|
|
|
|
// INSTANCES
|
|
|
|
case INSTANCES_NODE:
|
|
|
|
isn = dynamic_cast<InstancesNode *>(*it);
|
|
|
|
fprintf(fp,"\t<instances>\n");
|
2005-11-04 16:37:58 +01:00
|
|
|
|
|
|
|
for(it2 = isn->instances.Sons.begin(); it2!=isn->instances.Sons.end(); ++it2)
|
|
|
|
{
|
2006-01-27 10:15:14 +01:00
|
|
|
in = dynamic_cast<InstanceNode *>(*it2);
|
|
|
|
fprintf(fp,"\t\t<instance>\n");
|
|
|
|
fprintf(fp,"\t\t\t<id>\n");
|
|
|
|
fprintf(fp,"\t\t\t%s\n", in->id);
|
|
|
|
fprintf(fp,"\t\t\t</id>\n");
|
|
|
|
fprintf(fp,"\t\t\t<type>\n");
|
|
|
|
fprintf(fp,"\t\t\t%s\n", in->type);
|
|
|
|
fprintf(fp,"\t\t\t</type>\n");
|
|
|
|
fprintf(fp,"\t\t\t<own-slots>\n");
|
|
|
|
|
|
|
|
for(it3 = in->own_slots.own_slot.Sons.begin();
|
|
|
|
it3!=in->own_slots.own_slot.Sons.end(); ++it3)
|
2005-11-04 16:37:58 +01:00
|
|
|
{
|
2006-01-27 10:15:14 +01:00
|
|
|
osn = dynamic_cast<OwnSlotNode *>(*it3);
|
|
|
|
fprintf(fp,"\t\t\t\t<own-slot slot-name=\":%s\">\n",osn->name);
|
|
|
|
fprintf(fp,"\t\t\t\t\t<entry type=\"%s\">\n",osn->entry.type);
|
2006-01-27 14:35:10 +01:00
|
|
|
fprintf(fp,"\t\t\t\t\t\t<value>%s", osn->entry.value.value.c_str());
|
2006-01-27 10:15:14 +01:00
|
|
|
fprintf(fp,"</value>\n");
|
|
|
|
fprintf(fp,"\t\t\t\t\t</entry>\n");
|
|
|
|
fprintf(fp,"\t\t\t\t</own-slot>\n");
|
2005-11-04 16:37:58 +01:00
|
|
|
}
|
2006-01-27 10:15:14 +01:00
|
|
|
fprintf(fp,"\t\t\t</own-slots>\n");
|
|
|
|
fprintf(fp,"\t\t</instance>\n");
|
2005-11-04 16:37:58 +01:00
|
|
|
}
|
2006-01-27 10:15:14 +01:00
|
|
|
fprintf(fp,"\t</instances>\n");
|
2005-11-04 16:37:58 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++nn;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
2005-12-02 01:47:15 +01:00
|
|
|
}
|