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 
19 #include "mlBase.h"
21 #include "mlXMLParserSystem.h"
22 
24 
25  class DOMElement;
26  class DOMNode;
27 
28 }
29 
30 using namespace XERCES_CPP_NAMESPACE;
31 
32 
33 ML_START_NAMESPACE
34 
35 //--------------------------------------------------------------------------------
36 
40  {
41  public:
43  {
44  ListStackEntry(const char* anItemName, bool aSuppressScope)
45  : itemName(anItemName), suppressScope(aSuppressScope) {}
46 
47  const char* itemName;
49  };
50 
51  protected:
53 
54  std::list<ListStackEntry> _listStack;
55  };
56 
57 
58 //--------------------------------------------------------------------------------
59 
62  {
63  public:
67 
70  XMLPersistenceOutputStream(DOMElement* container);
71 
74 
76  virtual void writeToFile(const std::string& fileName);
77 
79  virtual void writeToString(std::string& str);
80 
83  bool isBinary() const override { return false; }
84 
85  void writeString(const char* name, const std::string& value) override;
86 
87  void writeData(const void* data, size_t len, const char* name) override;
88 
89  protected:
90  void startListImpl(const char* name, const char* xmlItemName, bool xmlSuppressScope) override;
91  void endListImpl() override;
92  void startStructImpl(const char* name) override;
93  void endStructImpl() override;
94 
95  void setObjectID(int id) override;
96  void setObjectType(const char* type) override;
97  void setObjectVersion(int version) override;
98 
99  void writeValues(const char* name, const MLint32* values, size_t n, bool fixedList) override;
100  void writeValues(const char* name, const MLuint32* values, size_t n, bool fixedList) override;
101  void writeValues(const char* name, const MLint64* values, size_t n, bool fixedList) override;
102  void writeValues(const char* name, const MLuint64* values, size_t n, bool fixedList) override;
103  void writeValues(const char* name, const MLfloat* values, size_t n, bool fixedList) override;
104  void writeValues(const char* name, const MLdouble* values, size_t n, bool fixedList) override;
106 
107  private:
109  template<typename T>
110  void _writeValuesT(const char* name, const T* values, size_t n, bool fixedList);
111 
113  void _setAttribute(const char* attrName, const char* value);
114  void _setAttribute(const char* attrName, int value);
115 
118  void _createScope(const char* tagName);
119 
121  void _closeScope();
122 
124  DOMElement* _rootNode;
126  DOMElement* _currentNode;
127  };
128 
129 //--------------------------------------------------------------------------------
130 
133  {
134  public:
137 
139  XMLPersistenceInputStream(DOMElement* container);
140 
143 
145  virtual void readFromFile(const std::string& fileName);
146 
148  virtual void readFromString(const std::string& str);
149 
151  static void normalizeWindowsFileName(std::string& str);
152 
155  bool isBinary() const override { return false; }
156 
157  void readString(const char* name, std::string& value) override;
158 
159  void readData(std::string& value, const char* name) override;
160 
161  protected:
162  void startListImpl(const char* name, const char* xmlItemName, bool xmlSuppressScope) override;
163  void endListImpl() override;
164  void startStructImpl(const char* name) override;
165  void endStructImpl() override;
166 
167  bool hasNextInStructImpl(std::string& name) override;
168  bool hasNextListItemImpl() override;
169 
170  bool getObjectID(int& id) override;
171  bool getObjectType(std::string& type) override;
172  bool getObjectVersion(int& version) override;
173 
174  void readValues(const char* name, size_t expected, std::vector<MLint32>& values) override;
175  void readValues(const char* name, size_t expected, std::vector<MLuint32>& values) override;
176  void readValues(const char* name, size_t expected, std::vector<MLint64>& values) override;
177  void readValues(const char* name, size_t expected, std::vector<MLuint64>& values) override;
178  void readValues(const char* name, size_t expected, std::vector<MLfloat>& values) override;
179  void readValues(const char* name, size_t expected, std::vector<MLdouble>& values) override;
180 
182  void formatError(const std::string& msg) override;
184 
185  private:
187  template<typename T>
188  void _readValuesT(const char* name, size_t expected, std::vector<T>& values);
189 
191  bool _getAttribute(const char* attrName, std::string& value);
192  bool _getAttribute(const char* attrName, int& value);
193 
195  bool _checkElement(const char* name, bool throwError = true);
196 
198  void _toNextElement(bool reset = false);
199 
201  void _enterScope(const char* tagName);
202 
204  void _leaveScope();
205 
207  DOMElement* _rootNode;
209  DOMElement* _currentNode;
211  DOMElement* _nextNode;
212  };
213 
214 //--------------------------------------------------------------------------------
215 
216 ML_END_NAMESPACE
217 
218 #endif //__mlXMLPersistenceStream_H
219 
@ T
Definition: SoKeyGrabber.h:71
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
Check if 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, need 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 structget unique id of obj...
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 above for binary data (std::string misused as binary data container)
bool getObjectVersion(int &version) override
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
base type name
static void normalizeWindowsFileName(std::string &str)
replace / with \ character
bool hasNextInStructImpl(std::string &name) override
Check if 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
Write 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, need to be implemented in derived classes.
void setObjectID(int id) override
Implementation of Base object persistence, set 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
persistence version
void setObjectType(const char *type) override
base type name
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:513
unsigned int MLuint32
Definition: mlTypeDefs.h:191
double MLdouble
Definition: mlTypeDefs.h:223
INT64 MLint64
Include 64 bit integer support for Windows or Unix.
Definition: mlTypeDefs.h:500
signed int MLint32
Definition: mlTypeDefs.h:167
float MLfloat
Definition: mlTypeDefs.h:207
Implement the AbstractPersistence(Input/Output)Stream classes with XML backend.
ListStackEntry(const char *anItemName, bool aSuppressScope)