File: xmldocumentmanaging.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (216 lines) | stat: -rw-r--r-- 5,199 bytes parent folder | download | duplicates (3)
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
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004-2008                                           \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program 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.                                       *
*                                                                           *
* This program 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 (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/
#ifndef _XML_DOCUMENT_MANAGING_H
#define _XML_DOCUMENT_MANAGING_H

#include <vector>
#include <set>
#include <QDomDocument>
#include<QFile>
#include<QVector>
#include <QXmlStreamWriter>
#include <QString>

class XMLTag
{
public:
	typedef std::pair<QString,QString> TagAttribute;
	typedef QVector<TagAttribute> TagAttributes;
	QString _tagname;
	TagAttributes _attributes;

	XMLTag(const QString& tagname = QString(),const TagAttributes& attr = TagAttributes())
		:_tagname(tagname),_attributes(attr)
	{
	}

	virtual ~XMLTag()
	{
	}
};

class XMLLeafTag : public XMLTag
{
public:
		QVector<QString> _text;

		XMLLeafTag(const QString& tagname = QString(),const QVector<QString>& text = QVector<QString>())
			:XMLTag(tagname),_text(text)
		{
		}

		virtual ~XMLLeafTag()
		{
		}
};

class XMLDocumentWriter;

class XMLVisitor;

class XMLNode
{
public:
	XMLNode(XMLTag* tag);
	virtual ~XMLNode();

	virtual void applyProcedure(XMLVisitor& v) = 0;

	XMLTag* _tag;
};

class XMLInteriorNode : public XMLNode
{
public:
	XMLInteriorNode(XMLTag* tag);

	XMLNode* son(int ii);

	QVector< XMLNode* > sons();

	void applyProcedure(XMLVisitor& v);

	~XMLInteriorNode();

	QVector< XMLNode* > _sons; 
};

class XMLLeafNode : public XMLNode
{
public:
	XMLLeafNode(XMLLeafTag* leaftag);

	void applyProcedure(XMLVisitor& v);
	virtual ~XMLLeafNode();
};

class XMLDocument
{
public:
	XMLDocument(XMLInteriorNode* root)
		:_root(root)
	{
	}

	~XMLDocument()
	{
		delete (_root);
	}

	XMLInteriorNode* _root;
};


class XMLVisitor
{
public:
	virtual void operator()(XMLLeafNode& leaf) = 0;
	virtual void operator()(XMLInteriorNode& intnode) = 0;
};


class XMLDocumentWriter : public XMLVisitor
{
private:
	QXmlStreamWriter _stream;
	QFile _file;
	bool _error;

	void writeText(XMLLeafNode& node)
	{
		XMLLeafTag* leaftag = static_cast<XMLLeafTag*>(node._tag);
		for(QVector<QString>::iterator it = leaftag->_text.begin();it != leaftag->_text.end();++it)
		{
			QString tmp = "";
			if (it != leaftag->_text.begin()) 
				tmp = QString(" ");
			_stream.writeCharacters(tmp + *it);
		}
	}

	void writeAttributes(XMLNode& node)
	{
		QXmlStreamAttributes attr;
		for(XMLTag::TagAttributes::iterator it = node._tag->_attributes.begin();it != node._tag->_attributes.end();++it)
			attr.append(it->first,it->second);
		_stream.writeAttributes(attr);
	}

	void recursiveStep(XMLInteriorNode& intnode)
	{
		QVector< XMLNode* > sons = intnode.sons();
		for(QVector< XMLNode* >::iterator its = sons.begin();its != sons.end();++its)
			(*its)->applyProcedure(*this);
	}

public:

	void operator()(XMLLeafNode& node)
	{
		_stream.writeStartElement(node._tag->_tagname);
		writeAttributes(node);
		writeText(node);
		_stream.writeEndElement();
	}

	void operator()(XMLInteriorNode& intnode)
	{
		_stream.writeStartElement(intnode._tag->_tagname);
		writeAttributes(intnode);
		recursiveStep(intnode);
		_stream.writeEndElement();
	}

	void operator()(XMLDocument& doc)
	{
		_stream.writeStartDocument();
		operator()(*(doc._root));
		_stream.writeEndDocument();
	}

	void write(XMLDocument& doc)
	{
		(*this)(doc);
	}

	bool isReliable() const 
	{
		return !_error;
	}

	XMLDocumentWriter(const char* filename,const bool autoformatting = true)
		:_stream(),_file(filename),_error(false)
	{
		if (!_file.open(QIODevice::WriteOnly | QIODevice::Text))
			_error = true;
		_stream.setDevice(&_file);
		_stream.setAutoFormatting(autoformatting);
	}

  virtual ~XMLDocumentWriter()
	{
		_file.close();
	}
};

#endif