File: xml_parser.h

package info (click to toggle)
megaglest 3.13.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 13,416 kB
  • sloc: cpp: 144,271; ansic: 11,861; sh: 3,233; perl: 1,904; python: 1,751; objc: 142; asm: 42; makefile: 22
file content (262 lines) | stat: -rw-r--r-- 7,036 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
// ==============================================================
//	This file is part of Glest Shared Library (www.glest.org)
//
//	Copyright (C) 2001-2008 MartiƱo Figueroa
//
//	You can redistribute this code 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
// ==============================================================

#ifndef _SHARED_XML_XMLPARSER_H_
#define _SHARED_XML_XMLPARSER_H_

#include <string>
#include <vector>
#include <map>

#if defined(WANT_XERCES)

#include <xercesc/util/XercesDefs.hpp>

#endif

#include "rapidxml/rapidxml.hpp"
#include "data_types.h"
#include "leak_dumper.h"

using namespace rapidxml;
using namespace std;

#if defined(WANT_XERCES)

namespace XERCES_CPP_NAMESPACE{
	class DOMImplementation;
	class DOMDocument;
	class DOMNode;
	class DOMElement;

#if XERCES_VERSION_MAJOR < 3
	class DOMBuilder;
#else
	class DOMLSParser;
#endif
}

XERCES_CPP_NAMESPACE_USE

#endif

namespace Shared { namespace Xml {

enum xml_engine_parser_type {

#if defined(WANT_XERCES)
	XML_XERCES_ENGINE = 0,
#endif
	XML_RAPIDXML_ENGINE = 1
} ;

const int strSize= 8094;

class XmlIo;
class XmlTree;
class XmlNode;
class XmlAttribute;

#if defined(WANT_XERCES)
// =====================================================
// 	class XmlIo
//
///	Wrapper for Xerces C++
// =====================================================

class XmlIo {
private:
	static bool initialized;
	XERCES_CPP_NAMESPACE::DOMImplementation *implementation;
#if XERCES_VERSION_MAJOR < 3
	XERCES_CPP_NAMESPACE::DOMBuilder *parser;
#else
	XERCES_CPP_NAMESPACE::DOMLSParser *parser;
#endif

protected:
	XmlIo();
	virtual ~XmlIo();

	void init();

#if XERCES_VERSION_MAJOR < 3
	XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument * getRootDOMDocument(const string &path, DOMBuilder *parser, bool noValidation);
#else
	XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument * getRootDOMDocument(const string &path, DOMLSParser *parser, bool noValidation);
#endif

	DOMNode *loadDOMNode(const string &path, bool noValidation=false);
	virtual void releaseDOMParser();

public:
	static XmlIo &getInstance();

	static bool isInitialized();
	void cleanup();

	XmlNode *load(const string &path, const std::map<string,string> &mapTagReplacementValues,bool noValidation=false,bool skipStackTrace=false);
	void save(const string &path, const XmlNode *node);
};

#endif

class XmlIoRapid {
private:
	static bool initialized;

private:
	XmlIoRapid();
	void init();

public:
	static XmlIoRapid &getInstance();
	~XmlIoRapid();

	static bool isInitialized();
	void cleanup();

	XmlNode *load(const string &path, const std::map<string,string> &mapTagReplacementValues,bool noValidation=false,bool skipStackTrace=false,bool skipUpdatePathClimbingParts=false);
	void save(const string &path, const XmlNode *node);
};

// =====================================================
//	class XmlTree
// =====================================================

class XmlTree{
private:
	XmlNode *rootNode;
	string loadPath;
	xml_engine_parser_type engine_type;
	bool skipStackCheck;
	bool skipUpdatePathClimbingParts;
private:
	XmlTree(XmlTree&);
	void operator =(XmlTree&);
	void clearRootNode();

public:
	XmlTree(xml_engine_parser_type engine_type = XML_RAPIDXML_ENGINE);
	~XmlTree();

	void setSkipUpdatePathClimbingParts(bool value);
	void init(const string &name);
	void load(const string &path, const std::map<string,string> &mapTagReplacementValues, bool noValidation=false,bool skipStackCheck=false,bool skipStackTrace=false);
	void save(const string &path);

	XmlNode *getRootNode() const	{return rootNode;}
};

// =====================================================
//	class XmlNode
// =====================================================

class XmlNode {
private:
	string name;
	string text;
	vector<XmlNode*> children;
	vector<XmlAttribute*> attributes;
	mutable const XmlNode* superNode;

private:
	XmlNode(XmlNode&);
	void operator =(XmlNode&);

	string getTreeString() const;
	bool hasChildNoSuper(const string& childName) const;

public:

#if defined(WANT_XERCES)

	XmlNode(XERCES_CPP_NAMESPACE::DOMNode *node, const std::map<string,string> &mapTagReplacementValues);
	XERCES_CPP_NAMESPACE::DOMElement *buildElement(XERCES_CPP_NAMESPACE::DOMDocument *document) const;

#endif

	XmlNode(xml_node<> *node, const std::map<string,string> &mapTagReplacementValues,bool skipUpdatePathClimbingParts=false);
	XmlNode(const string &name);
	~XmlNode();
	
	void setSuper(const XmlNode* superNode) const { this->superNode = superNode; }

	const string &getName() const	{return name;}
	size_t getChildCount() const		{return children.size();}
	size_t getAttributeCount() const	{return attributes.size();}
	const string &getText() const	{return text;}

	XmlAttribute *getAttribute(unsigned int i) const;
	XmlAttribute *getAttribute(const string &name,bool mustExist=true) const;
	bool hasAttribute(const string &name) const;

	XmlNode *getChild(unsigned int i) const;
	XmlNode *getChild(const string &childName, unsigned int childIndex=0) const;
	XmlNode *getChildWithAliases(vector<string> childNameList, unsigned int childIndex=0) const;
	vector<XmlNode *> getChildList(const string &childName) const;
	bool hasChildAtIndex(const string &childName, int childIndex=0) const;
	bool hasChild(const string &childName) const;
	bool hasChildWithAliases(vector<string> childNameList) const;
	int clearChild(const string &childName);


	XmlNode *addChild(const string &name, const string text = "");
	XmlAttribute *addAttribute(const string &name, const string &value, const std::map<string,string> &mapTagReplacementValues);
	xml_node<>* buildElement(xml_document<> *document) const;
};

// =====================================================
//	class XmlAttribute
// =====================================================

class XmlAttribute {
private:
	string value;
	string name;
	bool skipRestrictionCheck;
	bool usesCommondata;
	std::map<string,string> mapTagReplacementValues;

private:
	XmlAttribute(XmlAttribute&);
	void operator =(XmlAttribute&);

public:

#if defined(WANT_XERCES)

	XmlAttribute(XERCES_CPP_NAMESPACE::DOMNode *attribute, const std::map<string,string> &mapTagReplacementValues);

#endif

	XmlAttribute(xml_attribute<> *attribute, const std::map<string,string> &mapTagReplacementValues);
	XmlAttribute(const string &name, const string &value, const std::map<string,string> &mapTagReplacementValues);

public:
	const string getName() const	{return name;}
	const string getValue(string prefixValue="", bool trimValueWithStartingSlash=false) const;

	bool getBoolValue() const;
	int getIntValue() const;
	Shared::Platform::uint32 getUIntValue() const;
	int getIntValue(int min, int max) const;
	float getFloatValue() const;
	float getFloatValue(float min, float max) const;
	const string getRestrictedValue(string prefixValue="", bool trimValueWithStartingSlash=false) const;

	void setValue(string val);
};


}}//end namespace

#endif