00001
00002 #include <utility>
00003 #include <map>
00004 #include <list>
00005
00006 #include <string>
00007 #include <iostream>
00008 #include "SlotsNode.h"
00009 #include "ClassesNode.h"
00010 #include "InstancesNode.h"
00011
00012
00013 using namespace std;
00014
00015
00016
00017 static char* XML_SCHEMA_NAME = "protegekb";
00018
00019
00020 class FacetNode: public Node
00021 {
00022 virtual void printNode();
00023 virtual int qualifyNode();
00024 };
00025
00026
00027
00028 class FacetsNode: public Node
00029 {
00030 NodeGroup facets;
00031 virtual void printNode();
00032 virtual int qualifyNode();
00033 };
00034
00035
00036
00037 class MainNode: public Node
00038 {
00039 public:
00040
00041 MainNode(void){node_type = MAIN_NODE;};
00042 int node_type;
00043 list<pair<const char* , const char* > > headers;
00044
00045 void addHeaders(const char* str, const char* val);
00046 virtual void printNode();
00047 virtual int qualifyNode();
00048 };
00049
00050 void MainNode::addHeaders(const char* str, const char*val)
00051 {
00052 headers.push_back(pair<const char*, const char*>(str,val));
00053 }
00054 void MainNode::printNode()
00055 {
00056 cout << "MainNode: node_type is " << node_type << "\n";
00057
00058 list<pair<const char*,const char*> >::iterator it;
00059
00060 for(it = headers.begin(); it!= headers.end(); ++it)
00061 {
00062 cout << "MainNode: First element is " << it->first << "\n";
00063 cout << "MainNode: Second element is " << it->second << "\n";
00064 }
00065 }
00066
00067 int MainNode::qualifyNode()
00068 {return node_type;}
00069
00070
00071 class XMLTree
00072 {
00073 private:
00074
00075 static const char *DEFAULT_NAME;
00076 string filename;
00077 bool verbose;
00078
00079 public:
00080 XMLTree(void){verbose = false;}
00081 ~XMLTree(void){}
00082 NodeGroup root;
00083 NodeGroup ng;
00084 SlotNode sn;
00085
00086
00087
00088 void setName(const char *name);
00089 void setVerboseMode(bool flag);
00090 void initializeMain();
00091 void finalizeMain();
00092 void addHeaders(const char* str, const char*val);
00093
00094 void addSlots(SlotNode* sn);
00095
00096 void addClasses(ClassNode* cn);
00097 void addInstances(InstanceNode* in);
00098 void addNode(const char* s, int value_type, const char* name);
00099
00100 void printXMLTree();
00101
00102 };
00103 const char * XMLTree::DEFAULT_NAME = "XmlTree.xml";
00104
00105 void XMLTree::setName(const char *name)
00106 {
00107 filename = name;
00108 }
00109
00110 void XMLTree::setVerboseMode(bool flag)
00111 {
00112 verbose = flag;
00113 }
00114
00115 void XMLTree::initializeMain()
00116 {
00117 MainNode* mn = new MainNode;
00118
00119 mn->headers.push_back(pair<const char*, const char*>("protegekb", ""));
00120
00121 char* s1 = "http://www.w3.org/2001/XMLSchema-instance";
00122 char* s2 = new char[100];
00123 sprintf(s2,"\"%s\"",s1);
00124 mn->addHeaders(" xmlns:xsi=", s2);
00125
00126 s1 = "http://protege.stanford.edu/plugins/xmlbackend/protege_xml_backend.xsd";
00127 sprintf(s2,"\"%s\"",s1);
00128 mn->addHeaders(" xsi:noNamespaceSchemaLocation=", s2);
00129 root.Sons.push_back(mn);
00130 }
00131
00132 void XMLTree::finalizeMain()
00133 {
00134 addSlots(&sn);
00135
00136 OwnSlotsNode* ossn = new OwnSlotsNode;
00137 ossn->addOwnSlot(&ng);
00138 ClassNode* cn = new ClassNode;
00139 cn->addOwnSlots(ossn);
00140
00141 addClasses(cn);
00142
00143 InstanceNode* in = new InstanceNode;
00144 in->addOwnSlots(ossn);
00145
00146 addInstances(in);
00147 MainNode* mn = new MainNode;
00148
00149 mn->headers.push_back(pair<const char*,const char*>("/",XML_SCHEMA_NAME));
00150 root.Sons.push_back(mn);
00151
00152 }
00153
00154
00155 void XMLTree::addHeaders(const char* str, const char*val)
00156 {
00157 MainNode* mn = dynamic_cast<MainNode *>(root.Sons.front());
00158 mn->headers.push_back(pair<const char*,const char*>(str,val));
00159 }
00160
00161 void XMLTree::addSlots(SlotNode* sn)
00162 {
00163 SlotsNode* sn0 = new SlotsNode;
00164
00165 sn0->addSlot(sn);
00166 root.Sons.push_back(sn0);
00167 }
00168
00169 void XMLTree::addClasses(ClassNode* cn)
00170 {
00171 ClassesNode* cn0 = new ClassesNode;
00172
00173 cn0->addClass(cn);
00174 root.Sons.push_back(cn0);
00175 }
00176
00177 void XMLTree::addNode(const char* s, int value_type, const char* name)
00178 {
00179 ValueNode* vn = new ValueNode;
00180 EntryNode* en = new EntryNode;
00181 OwnSlotNode* osn = new OwnSlotNode;
00182
00183 switch(value_type)
00184 {
00185 case VALUE_INTEGER:
00186 en->type = "Integer";
00187 break;
00188
00189 case VALUE_FLOAT:
00190 en->type = "Float";
00191 break;
00192
00193 case VALUE_BOOL:
00194 en->type = "Bool";
00195 break;
00196
00197 case VALUE_STRING:
00198 en->type = "String";
00199 break;
00200 }
00201
00202
00203 vn->setValue(s);
00204 en->addValue(*vn);
00205 osn->setName(name);
00206 osn->addEntry(*en);
00207 sn.addOwnSlot(osn);
00208 ng.addNode(osn);
00209
00210 }
00211 void XMLTree::addInstances(InstanceNode* in)
00212 {
00213 InstancesNode* in0 = new InstancesNode;
00214
00215 in0->addInstance(in);
00216 root.Sons.push_back(in0);
00217 }
00218
00219 void XMLTree::printXMLTree()
00220 {
00221 FILE *fp;
00222
00223 if (filename.empty())
00224 fp = fopen(DEFAULT_NAME, "w");
00225 else
00226 fp = fopen(filename.c_str(), "w");
00227
00228 list<Node*>::iterator it;
00229 list<Node*>::iterator it2;
00230 list<Node*>::iterator it3;
00231 list<pair<const char*,const char*> >::iterator lit;
00232 MainNode* mn;
00233 SlotsNode* sns;
00234 SlotNode* sn;
00235 OwnSlotNode* osn;
00236 ClassesNode* csn;
00237 ClassNode* cn;
00238 InstancesNode* isn;
00239 InstanceNode* in;
00240 int nn = 0;
00241
00242 for(it = root.Sons.begin(); it!=root.Sons.end(); ++it)
00243 {
00244 if (verbose)
00245 {
00246 cout<<"Creating Node #"<< nn<<"\n";
00247 cout<<"Node Type is "<< (*it)->qualifyNode()<<"\n";
00248 }
00249
00250 switch((*it)->qualifyNode())
00251 {
00252
00253
00254 case MAIN_NODE:
00255 mn = dynamic_cast<MainNode *>(*it);
00256 fprintf(fp,"<");
00257 for(lit = mn->headers.begin(); lit!= mn->headers.end(); ++lit)
00258 fprintf(fp,"%s%s", lit->first,lit->second );
00259 fprintf(fp,"> \n");
00260
00261 break;
00262
00263
00264 case SLOTS_NODE:
00265 sns = dynamic_cast<SlotsNode *>(*it);
00266 fprintf(fp,"\t<slots>\n");
00267
00268 for(it2 = sns->slot.Sons.begin(); it2!=sns->slot.Sons.end(); ++it2)
00269 {
00270 sn = dynamic_cast<SlotNode *>(*it2);
00271 fprintf(fp,"\t\t<slot>\n");
00272 for(it3 = sn->own_slot.Sons.begin(); it3!=sn->own_slot.Sons.end(); ++it3)
00273 {
00274 osn = dynamic_cast<OwnSlotNode *>(*it3);
00275 fprintf(fp,"\t\t\t<own-slot slot-name=\":%s\">\n",osn->name);
00276 fprintf(fp,"\t\t\t\t<entry type=\"%s\">\n",osn->entry.type);
00277 fprintf(fp,"\t\t\t\t\t<value>%s", osn->entry.value.value.c_str());
00278 fprintf(fp,"</value>\n");
00279 fprintf(fp,"\t\t\t\t</entry>\n");
00280 fprintf(fp,"\t\t\t</own-slot>\n");
00281 }
00282 fprintf(fp,"\t\t</slot>\n");
00283 }
00284 fprintf(fp,"\t</slots>\n");
00285
00286 break;
00287
00288
00289 case CLASSES_NODE:
00290 csn = dynamic_cast<ClassesNode *>(*it);
00291 fprintf(fp,"\t<classes>\n");
00292
00293 for(it2 = csn->classn.Sons.begin(); it2!=csn->classn.Sons.end(); ++it2)
00294 {
00295 cn = dynamic_cast<ClassNode *>(*it2);
00296 fprintf(fp,"\t\t<class>\n");
00297 fprintf(fp,"\t\t\t<own-slots>\n");
00298 for(it3 = cn->own_slots.own_slot.Sons.begin();
00299 it3!=cn->own_slots.own_slot.Sons.end(); ++it3)
00300 {
00301 osn = dynamic_cast<OwnSlotNode *>(*it3);
00302 fprintf(fp,"\t\t\t\t<own-slot slot-name=\":%s\">\n",osn->name);
00303 fprintf(fp,"\t\t\t\t\t<entry type=\"%s\">\n",osn->entry.type);
00304 fprintf(fp,"\t\t\t\t\t\t<value>%s", osn->entry.value.value.c_str());
00305 fprintf(fp,"</value>\n");
00306 fprintf(fp,"\t\t\t\t\t</entry>\n");
00307 fprintf(fp,"\t\t\t\t</own-slot>\n");
00308 }
00309 fprintf(fp,"\t\t\t</own-slots>\n");
00310 fprintf(fp,"\t\t</class>\n");
00311 }
00312 fprintf(fp,"\t</classes>\n");
00313
00314 break;
00315
00316
00317 case INSTANCES_NODE:
00318 isn = dynamic_cast<InstancesNode *>(*it);
00319 fprintf(fp,"\t<instances>\n");
00320
00321 for(it2 = isn->instances.Sons.begin(); it2!=isn->instances.Sons.end(); ++it2)
00322 {
00323 in = dynamic_cast<InstanceNode *>(*it2);
00324 fprintf(fp,"\t\t<instance>\n");
00325 fprintf(fp,"\t\t\t<id>\n");
00326 fprintf(fp,"\t\t\t%s\n", in->id);
00327 fprintf(fp,"\t\t\t</id>\n");
00328 fprintf(fp,"\t\t\t<type>\n");
00329 fprintf(fp,"\t\t\t%s\n", in->type);
00330 fprintf(fp,"\t\t\t</type>\n");
00331 fprintf(fp,"\t\t\t<own-slots>\n");
00332
00333 for(it3 = in->own_slots.own_slot.Sons.begin();
00334 it3!=in->own_slots.own_slot.Sons.end(); ++it3)
00335 {
00336 osn = dynamic_cast<OwnSlotNode *>(*it3);
00337 fprintf(fp,"\t\t\t\t<own-slot slot-name=\":%s\">\n",osn->name);
00338 fprintf(fp,"\t\t\t\t\t<entry type=\"%s\">\n",osn->entry.type);
00339 fprintf(fp,"\t\t\t\t\t\t<value>%s", osn->entry.value.value.c_str());
00340 fprintf(fp,"</value>\n");
00341 fprintf(fp,"\t\t\t\t\t</entry>\n");
00342 fprintf(fp,"\t\t\t\t</own-slot>\n");
00343 }
00344 fprintf(fp,"\t\t\t</own-slots>\n");
00345 fprintf(fp,"\t\t</instance>\n");
00346 }
00347 fprintf(fp,"\t</instances>\n");
00348
00349 break;
00350 }
00351 ++nn;
00352 }
00353 fclose(fp);
00354 }