MeVisLab Toolbox Reference
mlXMLPersistenceStream.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2012, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_XMLPERSISTENCE_STREAM_H
14#define ML_XMLPERSISTENCE_STREAM_H
15
16
18
20#include "mlBase.h"
21
22#include <xercesc/dom/DOMElement.hpp>
23#include <xercesc/dom/DOMNode.hpp>
24#include <xercesc/util/XercesVersion.hpp>
25
26using namespace XERCES_CPP_NAMESPACE;
27
28
29ML_START_NAMESPACE
30
31//--------------------------------------------------------------------------------
32
36{
37public:
39 {
40 ListStackEntry(const char* anItemName, bool aSuppressScope)
41 : itemName(anItemName)
42 , suppressScope(aSuppressScope)
43 {}
44
45 const char* itemName;
47 };
48
49protected:
52
53 std::list<ListStackEntry> _listStack;
54};
55
56
57//--------------------------------------------------------------------------------
58
61{
62public:
66
69 XMLPersistenceOutputStream(DOMElement* container);
70
73
75 virtual void writeToFile(const std::string& fileName);
76
78 virtual void writeToString(std::string& str);
79
82 bool isBinary() const override
83 {
84 return false;
85 }
86
87 void writeString(const char* name, const std::string& value) override;
88
89 void writeData(const void* data, size_t len, const char* name) override;
90
91protected:
92 void startListImpl(const char* name, const char* xmlItemName, bool xmlSuppressScope) override;
93 void endListImpl() override;
94 void startStructImpl(const char* name) override;
95 void endStructImpl() override;
96
97 void setObjectID(int id) override;
98 void setObjectType(const char* type) override;
99 void setObjectVersion(int version) override;
100
101 void writeValues(const char* name, const MLint32* values, size_t n, bool fixedList) override;
102 void writeValues(const char* name, const MLuint32* values, size_t n, bool fixedList) override;
103 void writeValues(const char* name, const MLint64* values, size_t n, bool fixedList) override;
104 void writeValues(const char* name, const MLuint64* values, size_t n, bool fixedList) override;
105 void writeValues(const char* name, const MLfloat* values, size_t n, bool fixedList) override;
106 void writeValues(const char* name, const MLdouble* values, size_t n, bool fixedList) override;
108
109private:
111 template <typename T> void _writeValuesT(const char* name, const T* values, size_t n, bool fixedList);
112
114 void _setAttribute(const char* attrName, const char* value);
115 void _setAttribute(const char* attrName, int value);
116
119 void _createScope(const char* tagName);
120
122 void _closeScope();
123
125 DOMElement* _rootNode;
127 DOMElement* _currentNode;
128};
129
130//--------------------------------------------------------------------------------
131
134{
135public:
138
140 XMLPersistenceInputStream(DOMElement* container);
141
144
146 virtual void readFromFile(const std::string& fileName);
147
149 virtual void readFromString(const std::string& str);
150
152 static void normalizeWindowsFileName(std::string& str);
153
156 bool isBinary() const override
157 {
158 return false;
159 }
160
161 void readString(const char* name, std::string& value) override;
162
163 void readData(std::string& value, const char* name) override;
164
165protected:
166 void startListImpl(const char* name, const char* xmlItemName, bool xmlSuppressScope) override;
167 void endListImpl() override;
168 void startStructImpl(const char* name) override;
169 void endStructImpl() override;
170
171 bool hasNextInStructImpl(std::string& name) override;
172 bool hasNextListItemImpl() override;
173
174 bool getObjectID(int& id) override;
175 bool getObjectType(std::string& type) override;
176 bool getObjectVersion(int& version) override;
177
178 void readValues(const char* name, size_t expected, std::vector<MLint32>& values) override;
179 void readValues(const char* name, size_t expected, std::vector<MLuint32>& values) override;
180 void readValues(const char* name, size_t expected, std::vector<MLint64>& values) override;
181 void readValues(const char* name, size_t expected, std::vector<MLuint64>& values) override;
182 void readValues(const char* name, size_t expected, std::vector<MLfloat>& values) override;
183 void readValues(const char* name, size_t expected, std::vector<MLdouble>& values) override;
184
186 void formatError(const std::string& msg) override;
188
189private:
191 template <typename T> void _readValuesT(const char* name, size_t expected, std::vector<T>& values);
192
194 bool _getAttribute(const char* attrName, std::string& value);
195 bool _getAttribute(const char* attrName, int& value);
196
198 bool _checkElement(const char* name, bool throwError = true);
199
201 void _toNextElement(bool reset = false);
202
204 void _enterScope(const char* tagName);
205
207 void _leaveScope();
208
210 DOMElement* _rootNode;
212 DOMElement* _currentNode;
214 DOMElement* _nextNode;
215};
216
217//--------------------------------------------------------------------------------
218
219ML_END_NAMESPACE
220
221#endif //__mlXMLPersistenceStream_H
@ T
Class for reading object data from a stream.
Class for writing object data to a stream.
Implementation for AbstractPersistenceInputStream using Xerces DOM Parser.
virtual ~XMLPersistenceInputStream()
the destructor will free the DOM document if readFromFile or readFromString was used
virtual void readFromFile(const std::string &fileName)
Read the complete tree from a file.
void readValues(const char *name, size_t expected, std::vector< MLint32 > &values) override
Abstract reading methods that need to be implemented by derived classes.
void startStructImpl(const char *name) override
void readValues(const char *name, size_t expected, std::vector< MLint64 > &values) override
bool hasNextListItemImpl() override
Checks whether there is another item in the current list.
XMLPersistenceInputStream(DOMElement *container)
this constructor will read from the given XML element
XMLPersistenceInputStream()
this is the default constructor, use readFromFile or readFromString to fill the DOM
void readString(const char *name, std::string &value) override
Abstract reading method that needs to be implemented by derived classes.
void formatError(const std::string &msg) override
overload, improves message with current parsing location
void startListImpl(const char *name, const char *xmlItemName, bool xmlSuppressScope) override
Abstract methods called by above methods, needs to be implemented in derived classes.
bool isBinary() const override
overridden methods
bool getObjectID(int &id) override
Implementation of Base object persistence, get attribute of currently open struct.
void readValues(const char *name, size_t expected, std::vector< MLdouble > &values) override
void readValues(const char *name, size_t expected, std::vector< MLuint32 > &values) override
void readData(std::string &value, const char *name) override
Same as readObject for binary data (std::string misused as binary data container).
bool getObjectVersion(int &version) override
Returns persistence version.
void readValues(const char *name, size_t expected, std::vector< MLfloat > &values) override
void readValues(const char *name, size_t expected, std::vector< MLuint64 > &values) override
virtual void readFromString(const std::string &str)
Read the complete tree from a string.
bool getObjectType(std::string &type) override
Returns base type.
static void normalizeWindowsFileName(std::string &str)
replace / with \ character
bool hasNextInStructImpl(std::string &name) override
Checks whether an item with the given name comes next in the stream.
Implementation for AbstractPersistenceOutputStream using Xerces DOM Parser.
virtual void writeToFile(const std::string &fileName)
Writes the complete tree to a file.
XMLPersistenceOutputStream()
this constructor creates a new XML document that can be written afterwards with writeToFile or writeT...
void writeValues(const char *name, const MLuint32 *values, size_t n, bool fixedList) override
void writeData(const void *data, size_t len, const char *name) override
Writes binary data to stream.
void writeValues(const char *name, const MLdouble *values, size_t n, bool fixedList) override
void writeValues(const char *name, const MLint32 *values, size_t n, bool fixedList) override
Abstract writing methods that need to be implemented by derived classes.
void startStructImpl(const char *name) override
bool isBinary() const override
overridden methods
virtual void writeToString(std::string &str)
Writes the subtree rooted by this node to a string.
void writeString(const char *name, const std::string &value) override
Abstract writing method that needs to be implemented by derived classes.
XMLPersistenceOutputStream(DOMElement *container)
this constructor takes an existing XML element - everything will be written relative to this element
virtual ~XMLPersistenceOutputStream()
this will free the XML document if the first constructor was used
void startListImpl(const char *name, const char *xmlItemName, bool xmlSuppressScope) override
Abstract methods called by above methods, needs to be implemented in derived classes.
void setObjectID(int id) override
Implementation of Base object persistence, sets attribute on currently open struct Implementation hin...
void writeValues(const char *name, const MLfloat *values, size_t n, bool fixedList) override
void writeValues(const char *name, const MLint64 *values, size_t n, bool fixedList) override
void setObjectVersion(int version) override
Sets persistence version.
void setObjectType(const char *type) override
Sets base type.
void writeValues(const char *name, const MLuint64 *values, size_t n, bool fixedList) override
mix-in class that manages a stack of names for list elements and if the parent scope of the list is s...
std::list< ListStackEntry > _listStack
UINT64 MLuint64
Introduce platform-independent 64-bit unsigned integer type.
Definition mlTypeDefs.h:424
unsigned int MLuint32
Definition mlTypeDefs.h:184
double MLdouble
Definition mlTypeDefs.h:216
INT64 MLint64
Include 64-bit integer support for Windows or Unix.
Definition mlTypeDefs.h:411
signed int MLint32
Definition mlTypeDefs.h:160
float MLfloat
Definition mlTypeDefs.h:200
Custom deleters around Xerces resources with std::unique_ptr.
ListStackEntry(const char *anItemName, bool aSuppressScope)