File: XMLTree.h

package info (click to toggle)
meshlab 1.3.2%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 21,060 kB
  • ctags: 33,549
  • sloc: cpp: 224,813; ansic: 8,170; xml: 119; makefile: 80
file content (354 lines) | stat: -rw-r--r-- 7,781 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354

#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;
	list<pair<const char* , const char* > > headers;

	void addHeaders(const char* str, const char* val);
	virtual void printNode();
	virtual int qualifyNode();
};

void MainNode::addHeaders(const char* str, const char*val)
{
	headers.push_back(pair<const char*, const char*>(str,val));
}
void MainNode::printNode()
{
	cout << "MainNode: node_type is " << node_type << "\n";

	list<pair<const char*,const char*> >::iterator it;

	for(it = headers.begin(); it!= headers.end(); ++it)
	{
		cout << "MainNode: First element is " << it->first << "\n";
		cout << "MainNode: Second element is " << it->second << "\n";
	}
}

int MainNode::qualifyNode()
{return node_type;}


class XMLTree
{
private:

	static const char *DEFAULT_NAME;
	string filename;
	bool verbose;

public:
	XMLTree(void){verbose = false;}
	~XMLTree(void){}
	NodeGroup root;
	NodeGroup ng;	
	SlotNode sn;
	

	// methods
	void setName(const char *name);
	void setVerboseMode(bool flag);
	void initializeMain();
	void finalizeMain();
	void addHeaders(const char* str, const char*val);

	void addSlots(SlotNode* sn);
//void addFacets();
	void addClasses(ClassNode* cn);
	void addInstances(InstanceNode* in);
	void addNode(const char* s, int value_type, const char* name);

	void printXMLTree();

};
const char * XMLTree::DEFAULT_NAME = "XmlTree.xml";

void XMLTree::setName(const char *name)
{
	filename = name;
}

void XMLTree::setVerboseMode(bool flag)
{
	verbose = flag;
}

void XMLTree::initializeMain()
{
	MainNode*	mn = new MainNode;
	
	mn->headers.push_back(pair<const char*, const char*>("protegekb", ""));

	char* s1 = "http://www.w3.org/2001/XMLSchema-instance";
	char* s2 = new char[100];
	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;
	
	mn->headers.push_back(pair<const char*,const char*>("/",XML_SCHEMA_NAME));
	root.Sons.push_back(mn);

}


void XMLTree::addHeaders(const char* str, const char*val)
{
	MainNode* mn = dynamic_cast<MainNode *>(root.Sons.front());
	mn->headers.push_back(pair<const char*,const char*>(str,val));
}

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);
}

void XMLTree::addNode(const char* s, int value_type, const char* name)
{
	ValueNode* vn = new ValueNode;
	EntryNode* en = new EntryNode;
	OwnSlotNode* osn = new OwnSlotNode;

	switch(value_type)
	{
	case VALUE_INTEGER:
		en->type = "Integer";
	break;

	case VALUE_FLOAT:
		en->type = "Float";
	break;

	case VALUE_BOOL:
		en->type = "Bool";
	break;

	case VALUE_STRING:
		en->type = "String";
	break;
	}

	
	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()
{	
	FILE *fp;

	if (filename.empty())
		fp = fopen(DEFAULT_NAME, "w");
	else
		fp = fopen(filename.c_str(), "w");

	list<Node*>::iterator it;
	list<Node*>::iterator it2;
	list<Node*>::iterator it3;
	list<pair<const char*,const char*> >::iterator lit;
	MainNode* mn;
	SlotsNode* sns;
	SlotNode* sn;
	OwnSlotNode* osn;	
	ClassesNode* csn;	
	ClassNode* cn;	
	InstancesNode* isn;	
	InstanceNode* in;	
	int nn = 0;

	for(it = root.Sons.begin(); it!=root.Sons.end(); ++it)
	{
		if (verbose)
		{
			cout<<"Creating Node #"<< nn<<"\n";
			cout<<"Node Type is "<< (*it)->qualifyNode()<<"\n";
		}
		
		switch((*it)->qualifyNode())
		{
		
		// MAIN NODE
		case MAIN_NODE:
			mn = dynamic_cast<MainNode *>(*it);
			fprintf(fp,"<");
			for(lit = mn->headers.begin(); lit!= mn->headers.end(); ++lit)
					fprintf(fp,"%s%s", lit->first,lit->second );
				fprintf(fp,"> \n");
			
			break;
		
		// SLOTS
		case SLOTS_NODE:
			sns = dynamic_cast<SlotsNode *>(*it);
			fprintf(fp,"\t<slots>\n");

			for(it2 = sns->slot.Sons.begin(); it2!=sns->slot.Sons.end(); ++it2)
			{
				sn = dynamic_cast<SlotNode *>(*it2);
				fprintf(fp,"\t\t<slot>\n");
				for(it3 = sn->own_slot.Sons.begin(); it3!=sn->own_slot.Sons.end(); ++it3)
				{
					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);
					fprintf(fp,"\t\t\t\t\t<value>%s", osn->entry.value.value.c_str());
					fprintf(fp,"</value>\n");
					fprintf(fp,"\t\t\t\t</entry>\n");
					fprintf(fp,"\t\t\t</own-slot>\n");
				}
				fprintf(fp,"\t\t</slot>\n");
			}
			fprintf(fp,"\t</slots>\n");

		break;

		// CLASSES 
		case CLASSES_NODE:
			csn = dynamic_cast<ClassesNode *>(*it);
			fprintf(fp,"\t<classes>\n");

			for(it2 = csn->classn.Sons.begin(); it2!=csn->classn.Sons.end(); ++it2)
			{
				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)
				{
					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);
					fprintf(fp,"\t\t\t\t\t\t<value>%s", osn->entry.value.value.c_str());
					fprintf(fp,"</value>\n");
					fprintf(fp,"\t\t\t\t\t</entry>\n");
					fprintf(fp,"\t\t\t\t</own-slot>\n");
				}
				fprintf(fp,"\t\t\t</own-slots>\n");
				fprintf(fp,"\t\t</class>\n");
			}
			fprintf(fp,"\t</classes>\n");

			break;

		// INSTANCES
		case INSTANCES_NODE:
			isn = dynamic_cast<InstancesNode *>(*it);
			fprintf(fp,"\t<instances>\n");

			for(it2 = isn->instances.Sons.begin(); it2!=isn->instances.Sons.end(); ++it2)
			{
				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)
				{
					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);
					fprintf(fp,"\t\t\t\t\t\t<value>%s", osn->entry.value.value.c_str());
					fprintf(fp,"</value>\n");
					fprintf(fp,"\t\t\t\t\t</entry>\n");
					fprintf(fp,"\t\t\t\t</own-slot>\n");
				}
				fprintf(fp,"\t\t\t</own-slots>\n");
				fprintf(fp,"\t\t</instance>\n");
			}
			fprintf(fp,"\t</instances>\n");

			break;
		}
		++nn;
	}
	fclose(fp);
}