~ubuntu-branches/ubuntu/vivid/mygui/vivid

« back to all changes in this revision

Viewing changes to Wrappers/WrapperGenerator/XmlDocument.h

  • Committer: Package Import Robot
  • Author(s): Scott Howard, Bret Curtis, Scott Howard
  • Date: 2014-09-18 17:57:48 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20140918175748-dd8va78mvpw1jbes
Tags: 3.2.1-1
[ Bret Curtis ]
* Updated license for majority of files from LGPL to Expat (MIT)

[ Scott Howard ]
* New upstream release
* Updated patch to add build option for system GLEW libraries
* All patches accepted upstream except shared_libraries.patch
* Bumped SONAME due to dropped symbols, updated *.symbols and package
  names
* Updated license of debian/* to Expat with permission of all authors
* Don't install Doxygen autogenerated md5 and map files (thanks
  lintian)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*!
2
 
        @file
3
 
        @author         Albert Semenov
4
 
        @date           11/2007
5
 
*/
6
 
#ifndef __XML_DOCUMENT_H__
7
 
#define __XML_DOCUMENT_H__
8
 
 
9
 
#include "Enumerator.h"
10
 
#include "StringUtility.h"
11
 
 
12
 
#include <vector>
13
 
#include <string>
14
 
#include <iostream>
15
 
#include <fstream>
16
 
#include <sstream>
17
 
#include <assert.h>
18
 
 
19
 
namespace xml
20
 
{
21
 
 
22
 
        struct ElementType
23
 
        {
24
 
                enum Enum
25
 
                {
26
 
                        Comment,
27
 
                        Declaration,
28
 
                        Normal,
29
 
                        MAX
30
 
                };
31
 
                ElementType(Enum _value = MAX) : value(_value) { }
32
 
                friend bool operator == (ElementType const& a, ElementType const& b)
33
 
                {
34
 
                        return a.value == b.value;
35
 
                }
36
 
                friend bool operator != (ElementType const& a, ElementType const& b)
37
 
                {
38
 
                        return a.value != b.value;
39
 
                }
40
 
        private:
41
 
                Enum value;
42
 
        };
43
 
 
44
 
        struct ErrorType
45
 
        {
46
 
                enum Enum
47
 
                {
48
 
                        OpenFileFail,
49
 
                        CreateFileFail,
50
 
                        IncorrectContent,
51
 
                        NotClosedElements,
52
 
                        NoXMLDeclaration,
53
 
                        CloseNotOpenedElement,
54
 
                        InconsistentOpenCloseElements,
55
 
                        MoreThanOneXMLDeclaration,
56
 
                        MoreThanOneRootElement,
57
 
                        IncorrectAttribute,
58
 
                        MAX
59
 
                };
60
 
                ErrorType(Enum _value = MAX) : value(_value) { }
61
 
 
62
 
                std::string print() const
63
 
                {
64
 
                        return getValueName(value);
65
 
                }
66
 
 
67
 
        private:
68
 
                const char* getValueName(int _index) const
69
 
                {
70
 
                        static const char* values[MAX + 1] =
71
 
                        {
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",
82
 
                                ""
83
 
                        };
84
 
                        return values[(_index < MAX && _index >= 0) ? _index : MAX];
85
 
                }
86
 
        private:
87
 
                Enum value;
88
 
        };
89
 
 
90
 
        class Element;
91
 
        class Document;
92
 
 
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;
97
 
 
98
 
        //----------------------------------------------------------------------//
99
 
        // class ElementEnumerator
100
 
        //----------------------------------------------------------------------//
101
 
        class ElementEnumerator
102
 
        {
103
 
                friend class Element;
104
 
 
105
 
        private:
106
 
                ElementEnumerator(VectorElement::iterator _begin, VectorElement::iterator _end);
107
 
 
108
 
        public:
109
 
                bool next();
110
 
                bool next(const std::string& _name);
111
 
 
112
 
                ElementPtr operator->() const
113
 
                {
114
 
                        assert(m_current != m_end);
115
 
                        return (*m_current);
116
 
                }
117
 
                ElementPtr current()
118
 
                {
119
 
                        assert(m_current != m_end);
120
 
                        return (*m_current);
121
 
                }
122
 
 
123
 
        private:
124
 
                bool m_first;
125
 
                VectorElement::iterator m_current, m_end;
126
 
        };
127
 
 
128
 
 
129
 
        //----------------------------------------------------------------------//
130
 
        // class Element
131
 
        //----------------------------------------------------------------------//
132
 
        class Element
133
 
        {
134
 
                friend class  Document;
135
 
 
136
 
        public:
137
 
                ~Element();
138
 
 
139
 
        private:
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);
142
 
 
143
 
        public:
144
 
                ElementPtr createChild(const std::string& _name, const std::string& _content = "");
145
 
 
146
 
                template <typename T>
147
 
                void addAttribute(const std::string& _key, const T& _value)
148
 
                {
149
 
                        mAttributes.push_back(PairAttribute(_key, utility::toString(_value)));
150
 
                }
151
 
 
152
 
                void addAttribute(const std::string& _key, const std::string& _value)
153
 
                {
154
 
                        mAttributes.push_back(PairAttribute(_key, _value));
155
 
                }
156
 
 
157
 
                void removeAttribute(const std::string& _key)
158
 
                {
159
 
                        for (size_t index = 0; index < mAttributes.size(); ++index)
160
 
                        {
161
 
                                if (mAttributes[index].first == _key)
162
 
                                {
163
 
                                        mAttributes.erase(mAttributes.begin() + index);
164
 
                                        return;
165
 
                                }
166
 
                        }
167
 
                }
168
 
 
169
 
                void setAttribute(const std::string& _key, const std::string& _value)
170
 
                {
171
 
                        for (size_t index = 0; index < mAttributes.size(); ++index)
172
 
                        {
173
 
                                if (mAttributes[index].first == _key)
174
 
                                {
175
 
                                        mAttributes[index].second = _value;
176
 
                                        return;
177
 
                                }
178
 
                        }
179
 
                        mAttributes.push_back(PairAttribute(_key, _value));
180
 
                }
181
 
 
182
 
                template <typename T>
183
 
                void addContent(const T& _content)
184
 
                {
185
 
                        mContent.empty() ? mContent = utility::toString(_content) : mContent += utility::toString(" ", _content);
186
 
                }
187
 
 
188
 
                void addContent(const std::string& _content)
189
 
                {
190
 
                        if (mContent.empty()) mContent = _content;
191
 
                        else
192
 
                        {
193
 
                                mContent += " ";
194
 
                                mContent += _content;
195
 
                        }
196
 
                }
197
 
 
198
 
                template <typename T>
199
 
                void setContent(const T& _content)
200
 
                {
201
 
                        mContent = utility::toString(_content);
202
 
                }
203
 
 
204
 
                void setContent(const std::string& _content)
205
 
                {
206
 
                        mContent = _content;
207
 
                }
208
 
 
209
 
                void setContent2(const std::string& _content)
210
 
                {
211
 
                        mContent2 = _content;
212
 
                }
213
 
 
214
 
                void clear();
215
 
 
216
 
                bool findAttribute(const std::string& _name, std::string& _value);
217
 
                std::string findAttribute(const std::string& _name);
218
 
 
219
 
                const std::string& getName() const
220
 
                {
221
 
                        return mName;
222
 
                }
223
 
                const std::string& getContent() const
224
 
                {
225
 
                        return mContent;
226
 
                }
227
 
                const std::string& getContent2() const
228
 
                {
229
 
                        return mContent2;
230
 
                }
231
 
                const VectorAttributes& getAttributes() const
232
 
                {
233
 
                        return mAttributes;
234
 
                }
235
 
                ElementPtr getParent() const
236
 
                {
237
 
                        return mParent;
238
 
                }
239
 
 
240
 
                ElementEnumerator getElementEnumerator()
241
 
                {
242
 
                        return ElementEnumerator(mChilds.begin(), mChilds.end());
243
 
                }
244
 
 
245
 
                ElementType getType() const
246
 
                {
247
 
                        return mType;
248
 
                }
249
 
 
250
 
        private:
251
 
                std::string mName;
252
 
                std::string mContent;
253
 
                std::string mContent2;
254
 
                VectorAttributes mAttributes;
255
 
                VectorElement mChilds;
256
 
                ElementPtr mParent;
257
 
                ElementType mType;
258
 
        };
259
 
 
260
 
        //----------------------------------------------------------------------//
261
 
        // class Document
262
 
        //----------------------------------------------------------------------//
263
 
        class Document
264
 
        {
265
 
        public:
266
 
                Document();
267
 
                ~Document();
268
 
 
269
 
                // открывает обычным файлом, имя файла в utf8
270
 
                bool open(const std::string& _filename);
271
 
 
272
 
                // открывает обычным файлом, имя файла в utf16 или utf32
273
 
                bool open(const std::wstring& _filename);
274
 
 
275
 
                // открывает обычным потоком
276
 
                bool open(std::ifstream& _stream);
277
 
 
278
 
                // сохраняет файл, имя файла в кодировке utf8
279
 
                bool save(const std::string& _filename);
280
 
 
281
 
                // сохраняет файл, имя файла в кодировке utf16 или utf32
282
 
                bool save(const std::wstring& _filename);
283
 
 
284
 
                bool save(std::ofstream& _stream);
285
 
 
286
 
 
287
 
                // если группа есть, то открывается огровским потоком, если нет, то просто как файл
288
 
                bool open(const std::string& _filename, const std::string& _group);
289
 
 
290
 
                void clear();
291
 
 
292
 
                std::string getLastError()
293
 
                {
294
 
                        const std::string& error = mLastError.print();
295
 
                        if (error.empty()) return error;
296
 
                        return utility::toString("'", error, "' ,  file='", mLastErrorFile, "' ,  line=", mLine, " ,  col=", mCol);
297
 
                }
298
 
 
299
 
                void clearLastError()
300
 
                {
301
 
                        mLastError = ErrorType::MAX;
302
 
                }
303
 
 
304
 
        private:
305
 
 
306
 
                void setLastFileError(const std::string& _filename)
307
 
                {
308
 
                        mLastErrorFile = _filename;
309
 
                }
310
 
 
311
 
                bool parseTag(ElementPtr& _currentNode, std::string _content);
312
 
 
313
 
                bool checkPair(std::string& _key, std::string& _value);
314
 
 
315
 
                bool parseLine(std::string& _line, ElementPtr& _element);
316
 
 
317
 
                // ищет символ без учета ковычек
318
 
                size_t find(const std::string& _text, char _char, size_t _start = 0);
319
 
 
320
 
                void clearDeclaration();
321
 
                void clearRoot();
322
 
 
323
 
        public:
324
 
                ElementPtr createDeclaration(const std::string& _version = "1.0", const std::string& _encoding = "UTF-8");
325
 
                ElementPtr createRoot(const std::string& _name);
326
 
 
327
 
                ElementPtr getRoot() const
328
 
                {
329
 
                        return mRoot;
330
 
                }
331
 
 
332
 
        private:
333
 
                ElementPtr mRoot;
334
 
                ElementPtr mDeclaration;
335
 
                ErrorType mLastError;
336
 
                std::string mLastErrorFile;
337
 
                size_t mLine;
338
 
                size_t mCol;
339
 
 
340
 
        }; // class Document
341
 
 
342
 
} // namespace xml
343
 
 
344
 
#endif // __XML_DOCUMENT_H__