6
#ifndef __XML_DOCUMENT_H__
7
#define __XML_DOCUMENT_H__
9
#include "Enumerator.h"
10
#include "StringUtility.h"
31
ElementType(Enum _value = MAX) : value(_value) { }
32
friend bool operator == (ElementType const& a, ElementType const& b)
34
return a.value == b.value;
36
friend bool operator != (ElementType const& a, ElementType const& b)
38
return a.value != b.value;
53
CloseNotOpenedElement,
54
InconsistentOpenCloseElements,
55
MoreThanOneXMLDeclaration,
56
MoreThanOneRootElement,
60
ErrorType(Enum _value = MAX) : value(_value) { }
62
std::string print() const
64
return getValueName(value);
68
const char* getValueName(int _index) const
70
static const char* values[MAX + 1] =
72
"Failed to open XML file",
73
"Failed to ceate XML file",
74
"XML file contain incorrect content",
75
"XML file contain not closed elements",
76
"XML file without declaration",
77
"XML file contain closed but not opened element",
78
"XML file contain inconsistent elements",
79
"XML file contain more than one declaration",
80
"XML file contain more than one root element",
81
"XML file contain incorrect attribute",
84
return values[(_index < MAX && _index >= 0) ? _index : MAX];
93
typedef Element* ElementPtr;
94
typedef std::pair<std::string, std::string> PairAttribute;
95
typedef std::vector<PairAttribute> VectorAttributes;
96
typedef std::vector<ElementPtr> VectorElement;
98
//----------------------------------------------------------------------//
99
// class ElementEnumerator
100
//----------------------------------------------------------------------//
101
class ElementEnumerator
103
friend class Element;
106
ElementEnumerator(VectorElement::iterator _begin, VectorElement::iterator _end);
110
bool next(const std::string& _name);
112
ElementPtr operator->() const
114
assert(m_current != m_end);
119
assert(m_current != m_end);
125
VectorElement::iterator m_current, m_end;
129
//----------------------------------------------------------------------//
131
//----------------------------------------------------------------------//
134
friend class Document;
140
Element(const std::string& _name, ElementPtr _parent, ElementType _type = ElementType::Normal, const std::string& _content = "");
141
void save(std::ofstream& _stream, size_t _level);
144
ElementPtr createChild(const std::string& _name, const std::string& _content = "");
146
template <typename T>
147
void addAttribute(const std::string& _key, const T& _value)
149
mAttributes.push_back(PairAttribute(_key, utility::toString(_value)));
152
void addAttribute(const std::string& _key, const std::string& _value)
154
mAttributes.push_back(PairAttribute(_key, _value));
157
void removeAttribute(const std::string& _key)
159
for (size_t index = 0; index < mAttributes.size(); ++index)
161
if (mAttributes[index].first == _key)
163
mAttributes.erase(mAttributes.begin() + index);
169
void setAttribute(const std::string& _key, const std::string& _value)
171
for (size_t index = 0; index < mAttributes.size(); ++index)
173
if (mAttributes[index].first == _key)
175
mAttributes[index].second = _value;
179
mAttributes.push_back(PairAttribute(_key, _value));
182
template <typename T>
183
void addContent(const T& _content)
185
mContent.empty() ? mContent = utility::toString(_content) : mContent += utility::toString(" ", _content);
188
void addContent(const std::string& _content)
190
if (mContent.empty()) mContent = _content;
194
mContent += _content;
198
template <typename T>
199
void setContent(const T& _content)
201
mContent = utility::toString(_content);
204
void setContent(const std::string& _content)
209
void setContent2(const std::string& _content)
211
mContent2 = _content;
216
bool findAttribute(const std::string& _name, std::string& _value);
217
std::string findAttribute(const std::string& _name);
219
const std::string& getName() const
223
const std::string& getContent() const
227
const std::string& getContent2() const
231
const VectorAttributes& getAttributes() const
235
ElementPtr getParent() const
240
ElementEnumerator getElementEnumerator()
242
return ElementEnumerator(mChilds.begin(), mChilds.end());
245
ElementType getType() const
252
std::string mContent;
253
std::string mContent2;
254
VectorAttributes mAttributes;
255
VectorElement mChilds;
260
//----------------------------------------------------------------------//
262
//----------------------------------------------------------------------//
269
// открывает обычным файлом, имя файла в utf8
270
bool open(const std::string& _filename);
272
// открывает обычным файлом, имя файла в utf16 или utf32
273
bool open(const std::wstring& _filename);
275
// открывает обычным потоком
276
bool open(std::ifstream& _stream);
278
// сохраняет файл, имя файла в кодировке utf8
279
bool save(const std::string& _filename);
281
// сохраняет файл, имя файла в кодировке utf16 или utf32
282
bool save(const std::wstring& _filename);
284
bool save(std::ofstream& _stream);
287
// если группа есть, то открывается огровским потоком, если нет, то просто как файл
288
bool open(const std::string& _filename, const std::string& _group);
292
std::string getLastError()
294
const std::string& error = mLastError.print();
295
if (error.empty()) return error;
296
return utility::toString("'", error, "' , file='", mLastErrorFile, "' , line=", mLine, " , col=", mCol);
299
void clearLastError()
301
mLastError = ErrorType::MAX;
306
void setLastFileError(const std::string& _filename)
308
mLastErrorFile = _filename;
311
bool parseTag(ElementPtr& _currentNode, std::string _content);
313
bool checkPair(std::string& _key, std::string& _value);
315
bool parseLine(std::string& _line, ElementPtr& _element);
317
// ищет символ без учета ковычек
318
size_t find(const std::string& _text, char _char, size_t _start = 0);
320
void clearDeclaration();
324
ElementPtr createDeclaration(const std::string& _version = "1.0", const std::string& _encoding = "UTF-8");
325
ElementPtr createRoot(const std::string& _name);
327
ElementPtr getRoot() const
334
ElementPtr mDeclaration;
335
ErrorType mLastError;
336
std::string mLastErrorFile;
344
#endif // __XML_DOCUMENT_H__