MeVisLab Toolbox Reference
mlXMLTreeNode.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2007, 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_XMLTREE_NODE_H
14 #define ML_XMLTREE_NODE_H
15 
16 
19 
20 #include "mlBase.h"
21 #include "mlTreeNode.h"
22 #include "../mlParserSystem.h"
23 #include "mlXMLParserSystem.h"
24 
25 #include <mlTypeDefs.h>
26 
27 namespace XERCES_CPP_NAMESPACE {
28 
29  class DOMElement;
30  class DOMNode;
31  class XercesDOMParser;
32 }
33 
34 using namespace XERCES_CPP_NAMESPACE;
35 
36 
37 ML_START_NAMESPACE
38 
40  enum {
46 
49  };
50 
54 
55  public:
56 
58  XMLTreeNodeException(int errorCode = 0, const char* msg = nullptr);
59 
61 
63  const char* getMessage() const override;
64 
65  private:
66 
67  std::string _message;
68 
70  static const char* _stdErrorMsg[];
71 
75 
76  };
77 
78 
83  {
84  public:
85 
87 
97  XMLTreeNode(TreeNode::ConstructionMode mode = TreeNode::CM_writerRoot);
98 
100 
102  ~XMLTreeNode() override;
103 
106 
108  void writeToFile (const char* fileName) override;
109 
110 #if !ML_DEPRECATED_SINCE(3,5,0)
111  private:
112 #endif
114  [[deprecated]] void writeToString (char*& str) override;
115 #if !ML_DEPRECATED_SINCE(3,5,0)
116  public:
117 #endif
118 
119  void writeToString(std::string& str) override;
121 
124 
126  void readFromFile (const char* fileName) override;
127 
128 #if !ML_DEPRECATED_SINCE(3,5,0)
129  private:
130 #endif
132  [[deprecated]] void readFromString (const char* str) override;
133 #if !ML_DEPRECATED_SINCE(3,5,0)
134  public:
135 #endif
136 
137  void readFromString(const std::string& str) override;
138 
140 
153 
154  //virtual void addChild (bool val, const char* tagName);
155  void addChild (unsigned long val, const char* tagName) override;
156  void addChild (long val, const char* tagName) override;
157  void addChild (MLuint64 val, const char* tagName) override;
158  void addChild (MLint64 val, const char* tagName) override;
159  void addChild (long double val, const char* tagName) override;
160  void addChild (const Vector2& vec, const char* tagName) override;
161  void addChild (const Vector3& vec, const char* tagName) override;
162  void addChild (const Vector4& vec, const char* tagName) override;
163  void addChild (const Vector6& vec, const char* tagName) override;
164  void addChild (const ImageVector& vec, const char* tagName) override;
165  void addChild (const Matrix3& mat, const char* tagName) override;
166  void addChild (const Matrix4& mat, const char* tagName) override;
167  void addChild (const SubImageBox& box, const char* tagName) override;
168  void addChild (const SubImageBoxd& box, const char* tagName) override;
169 #if !ML_DEPRECATED_SINCE(3,5,0)
170  private:
171 #endif
172  [[deprecated]] void addChild (const char* const text, const char* tagName) override;
173 #if !ML_DEPRECATED_SINCE(3,5,0)
174  public:
175 #endif
176 
177  void addChild(const std::string& str, const char* tagName) override;
178  void addChild (const void* const ptr, size_t noBytes, const char* tagName) override;
179 
180 
183 
188  void addChild (const Base* const obj, const char* tagName, bool generic = true) override;
189 
192  TreeNode* addChild ( const char* tagName) override;
193 
194 
196 
209 
214  bool hasChild (const char* tagName = nullptr) override;
215 
216  //virtual void readChild (bool& val, const char* tagName = NULL);
217  void readChild (unsigned long& val, const char* tagName = nullptr) override;
218  void readChild (long& val, const char* tagName = nullptr) override;
219  void readChild (MLuint64& val, const char* tagName = nullptr) override;
220  void readChild (MLint64& val, const char* tagName = nullptr) override;
221  void readChild (long double& val, const char* tagName = nullptr) override;
222  void readChild (Vector2& val, const char* tagName = nullptr) override;
223  void readChild (Vector3& val, const char* tagName = nullptr) override;
224  void readChild (Vector4& val, const char* tagName = nullptr) override;
225  void readChild (Vector6& val, const char* tagName = nullptr) override;
226  void readChild (ImageVector& val, const char* tagName = nullptr) override;
227  void readChild (Matrix3& val, const char* tagName = nullptr) override;
228  void readChild (Matrix4& val, const char* tagName = nullptr) override;
229  void readChild (SubImageBox& val, const char* tagName = nullptr) override;
230  void readChild (SubImageBoxd& val, const char* tagName = nullptr) override;
231  void readChild (void*& ptr, unsigned long& noBytes, const char* tagName = nullptr) override;
232 
235 #if !ML_DEPRECATED_SINCE(3,5,0)
236 private:
237 #endif
238 
239  [[deprecated]] void readChild (char*& val, const char* tagName = nullptr) override;
240 #if !ML_DEPRECATED_SINCE(3,5,0)
241 public:
242 #endif
243 
244  void readChild(std::string& val, const char* tagName = nullptr) override;
245 
249  void readChild (Base*& val, const char* tagName = nullptr) override;
250 
254  void readChild (Base& val, const char* tagName = nullptr) override;
255 
257  void readChild (TreeNode*& val, const char* tagName = nullptr) override;
258 
259 #if !ML_DEPRECATED_SINCE(3,5,0)
260 private:
261 #endif
262 
266  [[deprecated]] void deleteString (char* str) const override;
267 #if !ML_DEPRECATED_SINCE(3,5,0)
268 public:
269 #endif
270 
272 
275 
277  void setVersion(const char* className, int version) override;
278 
280  int getVersion(const char* /*className*/) override;
281 
283 
285  const char* getLastReadChildName() const override;
286 
291  static char* normalizeFileName(const char* fileName);
292 
295 
296  static void initXMLSystem();
297  static void terminateXMLSystem();
298 
300 
301  protected:
302 
303  //------------------------------------------------
306  //------------------------------------------------
307 
310  XMLTreeNode(DOMElement* parent);
311 
313  static char* _getTextNodeString(DOMElement* parentNode);
314 
317  DOMElement* _findElemByTagName(const char* tagName, DOMNode* prevNode = nullptr);
318 
320  XMLTreeNode* _addTreeNodeChild(DOMElement* child);
321 
323  DOMElement* _parentDOMNode;
324 
325  // count the number of active root instances, so we can correctly
326  // terminate the XML framework after deleting the last one
328 
330  static bool _xercesInitialized;
331 
333 
334  private:
336  void _initFromParser(XercesDOMParser& parser);
337 
339  DOMNode* _lastNode;
340 
342  DOMNode* _lastNodeRead;
343 
345  mutable StringX _lastName;
346 
350 
351  }; // class XMLTreeNode
352 
353 ML_END_NAMESPACE
354 
359 
360 
361 #if ML_DEPRECATED_SINCE(3, 5, 0)
362 
363 #define ML_PERSISTENCE_SUPPORT_VIA_XML \
364  inline std::string persistentState() override const \
365  { \
366  std::string str; \
367  try \
368  { \
369  auto ioRootNode = std::make_unique<XMLTreeNode>(TreeNode::CM_writerRoot); \
370  addStateToTree(ioRootNode.get()); \
371  ioRootNode->writeToString(str); \
372  } \
373  catch (const TreeNodeException &e) \
374  { \
375  ML_PRINT_ERROR("ML_PERSISTENCE_SUPPORT_VIA_XML - persistentState()", \
376  std::string("Exception ") + e.getMessage() + " occurred.", \
377  "Returning empty string."); \
378  str = ""; \
379  } \
380  return str; \
381  } \
382  inline void setPersistentState(const std::string &state) override \
383  { \
384  if (state.empty()) \
385  return; \
386  try \
387  { \
388  auto ioRootNode = std::make_unique<XMLTreeNode>(TreeNode::CM_readerRoot); \
389  this->readStateFromTree(ioRootNode.get()); \
390  ioRootNode->readFromString(state); \
391  } \
392  catch (const TreeNodeException &e) \
393  { \
394  ML_PRINT_ERROR("ML_PERSISTENCE_SUPPORT_VIA_XML - setPersistentState()", \
395  std::string("Exception ") + e.getMessage() + " occurred.", \
396  "Continuing. Warning: Object state may be undefined!"); \
397  } \
398  } \
399  inline [[nodiscard]] [[deprecated]] char * getPersistentState() override const \
400  { \
401  char *cstr = nullptr; \
402  try \
403  { \
404  auto ioRootNode = std::make_unique<XMLTreeNode>(TreeNode::CM_writerRoot); \
405  addStateToTree(ioRootNode.get()); \
406  ioRootNode->writeToString(cstr); \
407  } \
408  catch (const TreeNodeException &e) \
409  { \
410  ML_PRINT_ERROR("ML_PERSISTENCE_SUPPORT_VIA_XML - getPersistentState()", \
411  std::string("Exception ") + e.getMessage() + " occurred.", \
412  "Returning nullptr."); \
413  cstr = nullptr; \
414  } \
415  return cstr; \
416  } \
417  inline [[deprecated]] void setPersistentState(const char *state) override \
418  { \
419  if (!state || std::string(state).empty()) \
420  return; \
421  try \
422  { \
423  auto ioRootNode = std::make_unique<XMLTreeNode>(TreeNode::CM_readerRoot); \
424  this->readStateFromTree(ioRootNode.get()); \
425  ioRootNode->readFromString(state); \
426  } \
427  catch (const TreeNodeException &e) \
428  { \
429  ML_PRINT_ERROR("ML_PERSISTENCE_SUPPORT_VIA_XML - setPersistentState()", \
430  std::string("Exception ") + e.getMessage() + " occurred.", \
431  "Continuing. Warning: Object state may be undefined!"); \
432  } \
433  } \
434  inline [[deprecated]] virtual void clearPersistentState(char *state) const \
435  { \
436  if (!state || std::string(state).empty()) \
437  return; \
438  try \
439  { \
440  auto ioRootNode = std::make_unique<XMLTreeNode>(TreeNode::CM_readerRoot); \
441  ioRootNode->deleteString(state); \
442  } \
443  catch (const TreeNodeException &e) \
444  { \
445  ML_PRINT_ERROR("ML_PERSISTENCE_SUPPORT_VIA_XML - clearPersistentState()", \
446  std::string("Exception ") + e.getMessage() + " occurred.", \
447  "Continuing. Warning: Object state may be undefined!"); \
448  } \
449  } \
450 
451 #else
452 
453 #define ML_PERSISTENCE_SUPPORT_VIA_XML \
454  inline std::string persistentState() override const \
455  { \
456  std::string str; \
457  try \
458  { \
459  auto ioRootNode = std::make_unique<XMLTreeNode>(TreeNode::CM_writerRoot); \
460  addStateToTree(ioRootNode.get()); \
461  ioRootNode->writeToString(str); \
462  } \
463  catch (const TreeNodeException &e) \
464  { \
465  ML_PRINT_ERROR("ML_PERSISTENCE_SUPPORT_VIA_XML - persistentState()", \
466  std::string("Exception ") + e.getMessage() + " occurred.", \
467  "Returning empty string."); \
468  str = ""; \
469  } \
470  return str; \
471  } \
472  inline void setPersistentState(const std::string &state) override \
473  { \
474  if (state.empty()) \
475  return; \
476  try \
477  { \
478  auto ioRootNode = std::make_unique<XMLTreeNode>(TreeNode::CM_readerRoot); \
479  this->readStateFromTree(ioRootNode.get()); \
480  ioRootNode->readFromString(state); \
481  } \
482  catch (const TreeNodeException &e) \
483  { \
484  ML_PRINT_ERROR("ML_PERSISTENCE_SUPPORT_VIA_XML - setPersistentState()", \
485  std::string("Exception ") + e.getMessage() + " occurred.", \
486  "Continuing. Warning: Object state may be undefined!"); \
487  } \
488  } \
489  \
490 private: \
491  inline [[nodiscard]] [[deprecated]] char *getPersistentState() override const \
492  { \
493  char *cstr = nullptr; \
494  try \
495  { \
496  auto ioRootNode = std::make_unique<XMLTreeNode>(TreeNode::CM_writerRoot); \
497  addStateToTree(ioRootNode.get()); \
498  ioRootNode->writeToString(cstr); \
499  } \
500  catch (const TreeNodeException &e) \
501  { \
502  ML_PRINT_ERROR("ML_PERSISTENCE_SUPPORT_VIA_XML - getPersistentState()", \
503  std::string("Exception ") + e.getMessage() + " occurred.", \
504  "Returning nullptr."); \
505  cstr = nullptr; \
506  } \
507  return cstr; \
508  } \
509  inline [[deprecated]] void setPersistentState(const char *state) override \
510  { \
511  if (!state || std::string(state).empty()) \
512  return; \
513  try \
514  { \
515  auto ioRootNode = std::make_unique<XMLTreeNode>(TreeNode::CM_readerRoot); \
516  this->readStateFromTree(ioRootNode.get()); \
517  ioRootNode->readFromString(state); \
518  } \
519  catch (const TreeNodeException &e) \
520  { \
521  ML_PRINT_ERROR("ML_PERSISTENCE_SUPPORT_VIA_XML - setPersistentState()", \
522  std::string("Exception ") + e.getMessage() + " occurred.", \
523  "Continuing. Warning: Object state may be undefined!"); \
524  } \
525  } \
526  inline [[deprecated]] virtual void clearPersistentState(char *state) const \
527  { \
528  if (!state || std::string(state).empty()) \
529  return; \
530  try \
531  { \
532  auto ioRootNode = std::make_unique<XMLTreeNode>(TreeNode::CM_readerRoot); \
533  ioRootNode->deleteString(state); \
534  } \
535  catch (const TreeNodeException &e) \
536  { \
537  ML_PRINT_ERROR("ML_PERSISTENCE_SUPPORT_VIA_XML - clearPersistentState()", \
538  std::string("Exception ") + e.getMessage() + " occurred.", \
539  "Continuing. Warning: Object state may be undefined!"); \
540  } \
541  } \
542  \
543 public:
544 
545 #endif
546 
547 
548 #endif
549 
Defines library specific stuff, mostly for system independence.
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition: mlBase.h:62
SubImageBoxd - SubImageBox with coordinates of float data type.
The class TreeNodeException is the base class for all exceptions thrown by the class TreeNode and all...
Definition: mlTreeNode.h:111
The class TreeNode is the abstract base class for the import/export of ML objects.
Definition: mlTreeNode.h:170
ConstructionMode
TreeNode construction modes:
Definition: mlTreeNode.h:213
The XMLTreeNodeException class extends the exceptions already provided by class TreeNodeException.
Definition: mlXMLTreeNode.h:53
const char * getMessage() const override
Returns an error message containing error code and message (if present).
XMLTreeNodeException(int errorCode=0, const char *msg=nullptr)
Constructor.
The class XMLTreeNode implements the abstract class TreeNode to allow export and import of ML objects...
Definition: mlXMLTreeNode.h:83
static char * normalizeFileName(const char *fileName)
normalizes the given filename so that it can be used by Xerces to read or write a file with the given...
void readFromString(const std::string &str) override
Reads the subtree represented by this node from a string.
void readChild(SubImageBox &val, const char *tagName=nullptr) override
void readChild(Matrix4 &val, const char *tagName=nullptr) override
static void terminateXMLSystem()
void setVersion(const char *className, int version) override
Set version for current node. className is currently not used.
void addChild(const Matrix4 &mat, const char *tagName) override
void readChild(Base *&val, const char *tagName=nullptr) override
The type of the object here is read from the XML file.
void addChild(const void *const ptr, size_t noBytes, const char *tagName) override
void readChild(Vector2 &val, const char *tagName=nullptr) override
void writeToString(std::string &str) override
Generates a string representation of the subtree represented by this node to a file.
static void initXMLSystem()
void addChild(const std::string &str, const char *tagName) override
void addChild(const SubImageBox &box, const char *tagName) override
void readChild(long double &val, const char *tagName=nullptr) override
XMLTreeNode(TreeNode::ConstructionMode mode=TreeNode::CM_writerRoot)
Constructor.
void addChild(const Vector3 &vec, const char *tagName) override
void addChild(const Vector6 &vec, const char *tagName) override
void readChild(ImageVector &val, const char *tagName=nullptr) override
void addChild(unsigned long val, const char *tagName) override
Factory method adding a child encapsulating a variable of type long.
void readChild(SubImageBoxd &val, const char *tagName=nullptr) override
void readChild(unsigned long &val, const char *tagName=nullptr) override
void readChild(Vector6 &val, const char *tagName=nullptr) override
void addChild(const Vector4 &vec, const char *tagName) override
static int _numActiveRootInstances
void addChild(const SubImageBoxd &box, const char *tagName) override
void readChild(Base &val, const char *tagName=nullptr) override
Opposed to the readChild(Base*& ...) method, you have to create the object yourself BEFORE calling th...
TreeNode * addChild(const char *tagName) override
Adds a group or container node with the specified tag name.
DOMElement * _parentDOMNode
pointer to the DOM node actually wrapped by this class
void readFromFile(const char *fileName) override
Generates a tree parsing a file. Throws TNE_FileNotFound, TNE_ReadingFile, TNE_XML_ParserError.
void readChild(Vector3 &val, const char *tagName=nullptr) override
void addChild(const Vector2 &vec, const char *tagName) override
XMLTreeNode * _addTreeNodeChild(DOMElement *child)
Adds a new node containing the given child.
const char * getLastReadChildName() const override
Returns the name of the child most recently read.
void readChild(MLint64 &val, const char *tagName=nullptr) override
void addChild(const ImageVector &vec, const char *tagName) override
void readChild(long &val, const char *tagName=nullptr) override
XMLTreeNode(DOMElement *parent)
void readChild(Matrix3 &val, const char *tagName=nullptr) override
static bool _xercesInitialized
Indicates whether the Xerces XML SubSystem is already initialized.
DOMElement * _findElemByTagName(const char *tagName, DOMNode *prevNode=nullptr)
Starting from the element after node (or the first element, if node is NULL), returns the first DOM e...
~XMLTreeNode() override
Destructor.
void readChild(Vector4 &val, const char *tagName=nullptr) override
void addChild(long double val, const char *tagName) override
Factory method adding a child encapsulating a variable of type long double.
void addChild(MLuint64 val, const char *tagName) override
Factory method adding a child encapsulating a variable of type MLuint64.
void addChild(long val, const char *tagName) override
Factory method adding a child encapsulating a variable of type long.
void addChild(const Base *const obj, const char *tagName, bool generic=true) override
Adds an child representing an object whose class is derived from base and thus implements the addStat...
void writeToFile(const char *fileName) override
Writes the complete tree to a file. Throws TNE_WritingFile.
void readChild(MLuint64 &val, const char *tagName=nullptr) override
void readChild(TreeNode *&val, const char *tagName=nullptr) override
Returns the group or container node with the specified tag name.
bool hasChild(const char *tagName=nullptr) override
returns true when a child of the given tagName exists.
void readChild(void *&ptr, unsigned long &noBytes, const char *tagName=nullptr) override
void readChild(std::string &val, const char *tagName=nullptr) override
void addChild(const Matrix3 &mat, const char *tagName) override
void addChild(MLint64 val, const char *tagName) override
Factory method adding a child encapsulating a variable of type MLint64.
int getVersion(const char *) override
Get version of current node. className is currently not used.
static char * _getTextNodeString(DOMElement *parentNode)
Returns the string contained by the TextNode child of parentNode.
#define PARSER_EXPORT
Definiert systemspezifische Macros, die f"ur diese DLL gelten sollen.
#define ML_CLASS_HEADER(className)
Same like ML_CLASS_HEADER_EXPORTED with a non existing export symbol.
UINT64 MLuint64
Introduce platform independent 64 bit unsigned integer type.
Definition: mlTypeDefs.h:513
INT64 MLint64
Include 64 bit integer support for Windows or Unix.
Definition: mlTypeDefs.h:500
Implement the AbstractPersistence(Input/Output)Stream classes with XML backend.
@ TNE_COUNT
Definition: mlTreeNode.h:82
@ TNE_XML_Unknown
Definition: mlXMLTreeNode.h:44
@ TNE_XML_SystemInit
Definition: mlXMLTreeNode.h:41
@ TNE_XML_ParserError
Definition: mlXMLTreeNode.h:42
@ TNE_XML_COUNT
Definition: mlXMLTreeNode.h:48
@ TNE_XML_LAST_MESSAGE_IDX
Only for number calculations; do not use.
Definition: mlXMLTreeNode.h:47
@ TNE_XML_ReadingRawDataSize
Definition: mlXMLTreeNode.h:43
@ TNE_XML_Other
Definition: mlXMLTreeNode.h:45