MyGUI: MyGUI_XmlDocument.h Source File

MyGUI 3.4.3

MyGUI_XmlDocument.h
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#ifndef MYGUI_XML_DOCUMENT_H_
8#define MYGUI_XML_DOCUMENT_H_
9
10#include "MyGUI_Prerequest.h"
11#include "MyGUI_UString.h"
12#include "MyGUI_Diagnostic.h"
13#include "MyGUI_DataStream.h"
14
15#include <vector>
16#include <string>
17#include <iostream>
18#include <fstream>
19#include <sstream>
20#include <cassert>
21#include <type_traits>
22#include <memory>
23
24 namespace MyGUI::xml
25{
26
27 struct ElementType
28 {
29 enum Enum
30 {
31 Comment,
32 Declaration,
33 Normal,
34 MAX
35 };
36
37 ElementType(Enum _value = MAX) :
38 mValue(_value)
39 {
40 }
41 friend bool operator==(ElementType const& a, ElementType const& b)
42 {
43 return a.mValue == b.mValue;
44 }
45 friend bool operator!=(ElementType const& a, ElementType const& b)
46 {
47 return a.mValue != b.mValue;
48 }
49
50 int getValue() const
51 {
52 return mValue;
53 }
54
55 private:
56 Enum mValue;
57 };
58
59 struct ErrorType
60 {
61 enum Enum
62 {
63 OpenFileFail,
64 CreateFileFail,
65 IncorrectContent,
66 NotClosedElements,
67 NoXMLDeclaration,
68 CloseNotOpenedElement,
69 InconsistentOpenCloseElements,
70 MoreThanOneXMLDeclaration,
71 MoreThanOneRootElement,
72 IncorrectAttribute,
73 MAX
74 };
75
76 ErrorType(Enum _value = MAX) :
77 mValue(_value)
78 {
79 }
80
81 std::string_view print() const
82 {
83 return getValueName(mValue);
84 }
85
86 private:
87 std::string_view getValueName(int _index) const
88 {
89 if (_index < 0 || _index >= MAX)
90 return {};
91 static const std::string_view values[MAX] = {
92 "Failed to open XML file",
93 "Failed to create XML file",
94 "XML file contain incorrect content",
95 "XML file contain not closed elements",
96 "XML file without declaration",
97 "XML file contain closed but not opened element",
98 "XML file contain inconsistent elements",
99 "XML file contain more than one declaration",
100 "XML file contain more than one root element",
101 "XML file contain incorrect attribute"};
102 return values[_index];
103 }
104
105 private:
106 Enum mValue;
107 };
108
109 class Element;
110 class Document;
111
112 using ElementPtr = Element*;
113 using PairAttribute = std::pair<std::string, std::string>;
114 using VectorAttributes = std::vector<PairAttribute>;
115 using VectorElement = std::vector<std::unique_ptr<Element>>;
116
117 //----------------------------------------------------------------------//
118 // class ElementEnumerator
119 //----------------------------------------------------------------------//
120 class MYGUI_EXPORT ElementEnumerator
121 {
122 friend class Element;
123
124 private:
125 ElementEnumerator(VectorElement::iterator _begin, VectorElement::iterator _end);
126
127 public:
128 bool next();
129 bool next(std::string_view _name);
130
131 ElementPtr operator->() const;
132 ElementPtr current();
133
134 /*obsolete:*/
135#ifndef MYGUI_DONT_USE_OBSOLETE
136
137 MYGUI_OBSOLETE("use : bool ElementEnumerator::next()")
138 bool nextNode()
139 {
140 return next();
141 }
142 MYGUI_OBSOLETE("use : bool ElementEnumerator::next(std::string_view _name)")
143 bool nextNode(std::string_view _name)
144 {
145 return next(_name);
146 }
147 MYGUI_OBSOLETE("use : ElementPtr ElementEnumerator::current()")
148 ElementPtr currentNode()
149 {
150 return current();
151 }
152
153#endif // MYGUI_DONT_USE_OBSOLETE
154
155 private:
156 bool m_first{true};
157 VectorElement::iterator m_current, m_end;
158 };
159
160
161 //----------------------------------------------------------------------//
162 // class Element
163 //----------------------------------------------------------------------//
164 class MYGUI_EXPORT Element
165 {
166 friend class Document;
167
168 public:
169 Element(
170 std::string_view _name,
171 ElementPtr _parent,
172 ElementType _type = ElementType::Normal,
173 std::string_view _content = {});
174 Element(Element&&) = default;
175
176 private:
177 void save(std::ostream& _stream, size_t _level);
178
179 public:
180 ElementPtr createChild(
181 std::string_view _name,
182 std::string_view _content = {},
183 ElementType _type = ElementType::Normal);
184 void removeChild(ElementPtr _child);
185
186 template<typename T, typename = std::enable_if_t<!std::is_convertible_v<T, std::string_view>>>
187 void addAttribute(std::string_view _key, const T& _value)
188 {
189 addAttribute(_key, MyGUI::utility::toString(_value));
190 }
191
192 void addAttribute(std::string_view _key, std::string_view _value);
193
194 void removeAttribute(std::string_view _key);
195
196 void setAttribute(std::string_view _key, std::string_view _value);
197
198 template<typename T, typename = std::enable_if_t<!std::is_convertible_v<T, std::string_view>>>
199 void addContent(const T& _content)
200 {
201 addContent(MyGUI::utility::toString(_content));
202 }
203
204 void addContent(std::string_view _content);
205
206 template<typename T, typename = std::enable_if_t<!std::is_convertible_v<T, std::string_view>>>
207 void setContent(const T& _content)
208 {
209 setContent(MyGUI::utility::toString(_content));
210 }
211
212 void setContent(std::string_view _content);
213
214 void clear();
215
216 bool findAttribute(std::string_view _name, std::string& _value);
217 std::string_view findAttribute(std::string_view _name);
218
219 const std::string& getName() const;
220
221 const std::string& getContent() const;
222
223 const VectorAttributes& getAttributes() const;
224
225 ElementPtr getParent() const;
226
227 ElementEnumerator getElementEnumerator();
228
229 ElementType getType() const;
230
231 std::unique_ptr<Element> createCopy();
232
233 /*obsolete:*/
234#ifndef MYGUI_DONT_USE_OBSOLETE
235
236 template<typename T>
237 MYGUI_OBSOLETE(
238 "use : template <typename T> void Element::addAttribute(const std::string &_key, const T& _value)")
239 void addAttributes(std::string_view _key, const T& _value)
240 {
241 addAttribute<T>(_key, _value);
242 }
243 MYGUI_OBSOLETE("use : void Element::addAttribute(std::string_view _key, std::string_view _value)")
244 void addAttributes(std::string_view _key, std::string_view _value)
245 {
246 addAttribute(_key, _value);
247 }
248
249 template<typename T>
250 MYGUI_OBSOLETE("use : template <typename T> void Element::addContent(const T& _content)")
251 void addBody(const T& _content)
252 {
253 addContent<T>(_content);
254 }
255 MYGUI_OBSOLETE("use : void Element::addContent(std::string_view _content)")
256 void addBody(std::string_view _content)
257 {
258 addContent(_content);
259 }
260 template<typename T>
261 MYGUI_OBSOLETE("use : template <typename T> void Element::setContent(const T& _content)")
262 void setBody(const T& _content)
263 {
264 setContent<T>(_content);
265 }
266 MYGUI_OBSOLETE("use : void Element::setContent(std::string_view _content)")
267 void setBody(std::string_view _content)
268 {
269 setContent(_content);
270 }
271
272 MYGUI_OBSOLETE("use : const std::string& Element::getContent()")
273 const std::string& getBody() const
274 {
275 return getContent();
276 }
277 MYGUI_OBSOLETE("use : ElementEnumerator Element::getElementEnumerator()")
278 ElementEnumerator getNodeIterator()
279 {
280 return getElementEnumerator();
281 }
282
283#endif // MYGUI_DONT_USE_OBSOLETE
284
285 private:
286 std::string mName;
287 std::string mContent;
288 VectorAttributes mAttributes;
289 VectorElement mChildren;
290 ElementPtr mParent;
291 ElementType mType;
292 };
293
294 //----------------------------------------------------------------------//
295 // class Document
296 //----------------------------------------------------------------------//
297 class MYGUI_EXPORT Document
298 {
299 public:
300 // открывает обычным файлом, имя файла в utf8
301 bool open(const std::string& _filename);
302
303 // открывает обычным файлом, имя файла в utf16 или utf32
304 bool open(const std::wstring& _filename);
305
306 // открывает обычным потоком
307 bool open(std::istream& _stream);
308
309 bool open(const UString& _filename);
310
311 bool open(IDataStream* _stream);
312
313 // сохраняет файл
314 bool save(const std::string& _filename);
315
316 // сохраняет файл
317 bool save(const std::wstring& _filename);
318
319 bool save(std::ostream& _stream);
320
321 bool save(const UString& _filename);
322
323 void clear();
324
325 std::string getLastError() const;
326
327 void clearLastError();
328
329 ElementPtr createDeclaration(std::string_view _version = "1.0", std::string_view _encoding = "UTF-8");
330 ElementPtr createRoot(std::string_view _name);
331
332 ElementPtr getRoot() const;
333
334 /*obsolete:*/
335#ifndef MYGUI_DONT_USE_OBSOLETE
336
337 MYGUI_OBSOLETE(
338 "use : ElementPtr Document::createDeclaration(std::string_view _version, std::string_view _encoding)")
339 ElementPtr createInfo(std::string_view _version = "1.0", std::string_view _encoding = "UTF-8")
340 {
341 return createDeclaration(_version, _encoding);
342 }
343
344#endif // MYGUI_DONT_USE_OBSOLETE
345
346 private:
347 void setLastFileError(std::string_view _filename);
348 void setLastFileError(const std::wstring& _filename);
349
350 bool parseTag(ElementPtr& _currentNode, std::string _content);
351
352 bool checkPair(std::string& _key, std::string& _value);
353
354 bool parseLine(std::string& _line, ElementPtr& _element);
355
356 // ищет символ без учета ковычек
357 size_t find(std::string_view _text, char _char, size_t _start = 0);
358
359 void clearDeclaration();
360 void clearRoot();
361
362 private:
363 std::unique_ptr<Element> mRoot;
364 std::unique_ptr<Element> mDeclaration;
365 ErrorType mLastError;
366 std::string mLastErrorFile;
367 size_t mLine{0};
368 size_t mCol{0};
369
370 }; // class Document
371
372 using xmlNodeIterator MYGUI_OBSOLETE("use : class MyGUI::xml::ElementEnumerator") = ElementEnumerator;
373 using xmlNodePtr MYGUI_OBSOLETE("use : class MyGUI::xml::ElementPtr") = ElementPtr;
374 using xmlDocument MYGUI_OBSOLETE("use : class MyGUI::xml::Document") = Document;
375
376} // namespace MyGUI
377
378#endif // MYGUI_XML_DOCUMENT_H_
MyGUI_DataStream.h
MyGUI_Diagnostic.h
MYGUI_OBSOLETE
#define MYGUI_OBSOLETE(text)
Definition MyGUI_Diagnostic.h:68
MYGUI_EXPORT
#define MYGUI_EXPORT
Definition MyGUI_Platform.h:77
MyGUI_Prerequest.h
MyGUI_UString.h
MyGUI::IDataStream
Definition MyGUI_IDataStream.h:17
MyGUI::UString
A UTF-16 string with implicit conversion to/from std::string and std::wstring.
Definition MyGUI_UString.h:144
MyGUI::xml::Document
Definition MyGUI_XmlDocument.h:298
MyGUI::xml::Document::save
bool save(const std::string &_filename)
Definition MyGUI_XmlDocument.cpp:430
MyGUI::xml::Document::clearLastError
void clearLastError()
Definition MyGUI_XmlDocument.cpp:916
MyGUI::xml::Document::getLastError
std::string getLastError() const
Definition MyGUI_XmlDocument.cpp:890
MyGUI::xml::Document::getRoot
ElementPtr getRoot() const
Definition MyGUI_XmlDocument.cpp:921
MyGUI::xml::Document::createRoot
ElementPtr createRoot(std::string_view _name)
Definition MyGUI_XmlDocument.cpp:827
MyGUI::xml::Document::createDeclaration
ElementPtr createDeclaration(std::string_view _version="1.0", std::string_view _encoding="UTF-8")
Definition MyGUI_XmlDocument.cpp:818
MyGUI::xml::Document::open
bool open(const std::string &_filename)
Definition MyGUI_XmlDocument.cpp:383
MyGUI::xml::Document::createInfo
ElementPtr createInfo(std::string_view _version="1.0", std::string_view _encoding="UTF-8")
Definition MyGUI_XmlDocument.h:339
MyGUI::xml::Document::clear
void clear()
Definition MyGUI_XmlDocument.cpp:542
MyGUI::xml::ElementEnumerator
Definition MyGUI_XmlDocument.h:121
MyGUI::xml::ElementEnumerator::Element
friend class Element
Definition MyGUI_XmlDocument.h:122
MyGUI::xml::ElementEnumerator::next
bool next()
Definition MyGUI_XmlDocument.cpp:107
MyGUI::xml::ElementEnumerator::currentNode
ElementPtr currentNode()
Definition MyGUI_XmlDocument.h:148
MyGUI::xml::ElementEnumerator::operator->
ElementPtr operator->() const
Definition MyGUI_XmlDocument.cpp:130
MyGUI::xml::ElementEnumerator::current
ElementPtr current()
Definition MyGUI_XmlDocument.cpp:136
MyGUI::xml::ElementEnumerator::nextNode
bool nextNode()
Definition MyGUI_XmlDocument.h:138
MyGUI::xml::Element
Definition MyGUI_XmlDocument.h:165
MyGUI::xml::Element::getContent
const std::string & getContent() const
Definition MyGUI_XmlDocument.cpp:333
MyGUI::xml::Element::getNodeIterator
ElementEnumerator getNodeIterator()
Definition MyGUI_XmlDocument.h:278
MyGUI::xml::Element::getElementEnumerator
ElementEnumerator getElementEnumerator()
Definition MyGUI_XmlDocument.cpp:348
MyGUI::xml::Element::Element
Element(Element &&)=default
MyGUI::xml::Element::getBody
const std::string & getBody() const
Definition MyGUI_XmlDocument.h:273
MyGUI::xml::Element::Element
Element(std::string_view _name, ElementPtr _parent, ElementType _type=ElementType::Normal, std::string_view _content={})
Definition MyGUI_XmlDocument.cpp:145
MyGUI::xml::Element::addBody
void addBody(const T &_content)
Definition MyGUI_XmlDocument.h:251
MyGUI::xml::Element::Document
friend class Document
Definition MyGUI_XmlDocument.h:166
MyGUI::xml::Element::addAttribute
void addAttribute(std::string_view _key, const T &_value)
Definition MyGUI_XmlDocument.h:187
MyGUI::xml::Element::addAttributes
void addAttributes(std::string_view _key, const T &_value)
Definition MyGUI_XmlDocument.h:239
MyGUI::xml::Element::setContent
void setContent(const T &_content)
Definition MyGUI_XmlDocument.h:207
MyGUI::xml::Element::setBody
void setBody(const T &_content)
Definition MyGUI_XmlDocument.h:262
MyGUI::xml::Element::addContent
void addContent(const T &_content)
Definition MyGUI_XmlDocument.h:199
MyGUI::xml::Element::createChild
ElementPtr createChild(std::string_view _name, std::string_view _content={}, ElementType _type=ElementType::Normal)
Definition MyGUI_XmlDocument.cpp:218
MyGUI::utility::toString
std::string toString(T _value)
Definition MyGUI_StringUtility.h:26
MyGUI::xml
Definition MyGUI_XmlDocument.h:25
MyGUI::xml::ElementPtr
Element * ElementPtr
Definition MyGUI_XmlDocument.h:112
MyGUI::xml::PairAttribute
std::pair< std::string, std::string > PairAttribute
Definition MyGUI_XmlDocument.h:113
MyGUI::xml::VectorAttributes
std::vector< PairAttribute > VectorAttributes
Definition MyGUI_XmlDocument.h:114
MyGUI::xml::VectorElement
std::vector< std::unique_ptr< Element > > VectorElement
Definition MyGUI_XmlDocument.h:115
MyGUI::xml::ElementType
Definition MyGUI_XmlDocument.h:28
MyGUI::xml::ElementType::Enum
Enum
Definition MyGUI_XmlDocument.h:30
MyGUI::xml::ElementType::Comment
@ Comment
Definition MyGUI_XmlDocument.h:31
MyGUI::xml::ElementType::MAX
@ MAX
Definition MyGUI_XmlDocument.h:34
MyGUI::xml::ElementType::Normal
@ Normal
Definition MyGUI_XmlDocument.h:33
MyGUI::xml::ElementType::Declaration
@ Declaration
Definition MyGUI_XmlDocument.h:32
MyGUI::xml::ElementType::operator==
friend bool operator==(ElementType const &a, ElementType const &b)
Definition MyGUI_XmlDocument.h:41
MyGUI::xml::ElementType::operator!=
friend bool operator!=(ElementType const &a, ElementType const &b)
Definition MyGUI_XmlDocument.h:45
MyGUI::xml::ElementType::getValue
int getValue() const
Definition MyGUI_XmlDocument.h:50
MyGUI::xml::ElementType::ElementType
ElementType(Enum _value=MAX)
Definition MyGUI_XmlDocument.h:37
MyGUI::xml::ErrorType
Definition MyGUI_XmlDocument.h:60
MyGUI::xml::ErrorType::print
std::string_view print() const
Definition MyGUI_XmlDocument.h:81
MyGUI::xml::ErrorType::Enum
Enum
Definition MyGUI_XmlDocument.h:62
MyGUI::xml::ErrorType::CloseNotOpenedElement
@ CloseNotOpenedElement
Definition MyGUI_XmlDocument.h:68
MyGUI::xml::ErrorType::MAX
@ MAX
Definition MyGUI_XmlDocument.h:73
MyGUI::xml::ErrorType::OpenFileFail
@ OpenFileFail
Definition MyGUI_XmlDocument.h:63
MyGUI::xml::ErrorType::IncorrectContent
@ IncorrectContent
Definition MyGUI_XmlDocument.h:65
MyGUI::xml::ErrorType::NoXMLDeclaration
@ NoXMLDeclaration
Definition MyGUI_XmlDocument.h:67
MyGUI::xml::ErrorType::MoreThanOneRootElement
@ MoreThanOneRootElement
Definition MyGUI_XmlDocument.h:71
MyGUI::xml::ErrorType::MoreThanOneXMLDeclaration
@ MoreThanOneXMLDeclaration
Definition MyGUI_XmlDocument.h:70
MyGUI::xml::ErrorType::CreateFileFail
@ CreateFileFail
Definition MyGUI_XmlDocument.h:64
MyGUI::xml::ErrorType::NotClosedElements
@ NotClosedElements
Definition MyGUI_XmlDocument.h:66
MyGUI::xml::ErrorType::IncorrectAttribute
@ IncorrectAttribute
Definition MyGUI_XmlDocument.h:72
MyGUI::xml::ErrorType::InconsistentOpenCloseElements
@ InconsistentOpenCloseElements
Definition MyGUI_XmlDocument.h:69
MyGUI::xml::ErrorType::ErrorType
ErrorType(Enum _value=MAX)
Definition MyGUI_XmlDocument.h:76