MeVisLabToolboxReference
MeVisLab/Standard/Sources/ML/MLParser/mlXMLParser/mlXMLTreeNode.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00005 
00010 //--------------------------------------------------------------------------------
00011 
00012 #include "mlBase.h"
00013 #include "mlBasics.h"
00014 #include "mlTreeNode.h"
00015 #include "../mlParserSystem.h"
00016 #include "mlXMLParserSystem.h"
00017 //#include <xercesc/util/XercesDefs.hpp>
00018 
00019 #ifndef __mlXMLTreeNode_H
00020 #define __mlXMLTreeNode_H
00021 
00022 namespace XERCES_CPP_NAMESPACE {
00023 
00024   class DOMElement;
00025   class DOMNode;
00026 
00027 }
00028 
00029 using namespace XERCES_CPP_NAMESPACE;
00030 
00031 
00032 ML_START_NAMESPACE
00033 
00035   enum {
00036       TNE_XML_SystemInit = TNE_COUNT
00037     , TNE_XML_ParserError
00038     , TNE_XML_ReadingRawDataSize
00039     , TNE_XML_Unknown
00040     , TNE_XML_Other
00041 
00042     , TNE_XML_LAST_MESSAGE_IDX   
00043     , TNE_XML_COUNT = TNE_XML_LAST_MESSAGE_IDX - TNE_COUNT
00044   };
00045 
00048   class PARSER_EXPORT XMLTreeNodeException : public TreeNodeException {
00049 
00050     public:
00051 
00053       XMLTreeNodeException(int errorCode = 0, const char* msg = NULL) : TreeNodeException(errorCode, msg) {;}
00054 
00056       virtual const char* getMessage() const;
00057 
00058     private:
00059 
00061       static const char* _stdErrorMsg[];
00062 
00065       ML_CLASS_HEADER(XMLTreeNodeException);
00066 
00067   };
00068 
00069 
00073   class PARSER_EXPORT XMLTreeNode : public TreeNode {
00074 
00075     public:
00076 
00078 
00088       XMLTreeNode(TreeNode::ConstructionMode mode = TreeNode::CM_writerRoot);
00089 
00091 
00093       virtual ~XMLTreeNode();
00094 
00096 
00097 
00099       virtual void writeToFile    (const char* fileName);
00100 
00102       virtual void writeToString  (char*&      str);
00104 
00106 
00107 
00109       virtual void readFromFile   (const char* fileName);
00110 
00112       virtual void readFromString (const char* str);
00113 
00115 
00128 
00129       //virtual void addChild (bool              val, const char* tagName);
00130       virtual void addChild (unsigned long     val, const char* tagName);
00131       virtual void addChild (long              val, const char* tagName);
00132       virtual void addChild (MLuint64          val, const char* tagName);
00133       virtual void addChild (MLint64           val, const char* tagName);
00134       virtual void addChild (long double       val, const char* tagName);
00135       virtual void addChild (const vec2&       vec, const char* tagName);
00136       virtual void addChild (const vec3&       vec, const char* tagName);
00137       virtual void addChild (const vec4&       vec, const char* tagName);
00138       virtual void addChild (const vec6&       vec, const char* tagName);
00139       virtual void addChild (const Vector&     vec, const char* tagName);
00140       virtual void addChild (const mat3&       mat, const char* tagName);
00141       virtual void addChild (const mat4&       mat, const char* tagName);
00142       virtual void addChild (const SubImgBox&  box, const char* tagName);
00143       virtual void addChild (const SubImgBoxf& box, const char* tagName);
00144       virtual void addChild (const char* const text,const char* tagName);
00145       virtual void addChild (const void* const ptr, size_t noBytes, const char* tagName);
00146 
00147 
00150 
00155       virtual void addChild (const Base* const obj, const char* tagName, bool generic = true);
00156 
00159       virtual TreeNode* addChild (                   const char* tagName);
00160 
00161 
00163 
00176 
00181       virtual bool hasChild  (const char* tagName = NULL);
00182 
00183       //virtual void readChild (bool& val, const char* tagName = NULL);
00184       virtual void readChild (unsigned long&  val, const char* tagName = NULL);
00185       virtual void readChild (long&           val, const char* tagName = NULL);
00186       virtual void readChild (MLuint64&       val, const char* tagName = NULL);
00187       virtual void readChild (MLint64&        val, const char* tagName = NULL);
00188       virtual void readChild (long double&    val, const char* tagName = NULL);
00189       virtual void readChild (vec2&           val, const char* tagName = NULL);
00190       virtual void readChild (vec3&           val, const char* tagName = NULL);
00191       virtual void readChild (vec4&           val, const char* tagName = NULL);
00192       virtual void readChild (vec6&           val, const char* tagName = NULL);
00193       virtual void readChild (Vector&         val, const char* tagName = NULL);
00194       virtual void readChild (mat3&           val, const char* tagName = NULL);
00195       virtual void readChild (mat4&           val, const char* tagName = NULL);
00196       virtual void readChild (SubImgBox&      val, const char* tagName = NULL);
00197       virtual void readChild (SubImgBoxf&     val, const char* tagName = NULL);
00198       virtual void readChild (void*&          ptr, unsigned long& noBytes, const char* tagName = NULL);
00199 
00202       virtual void readChild (char*&          val, const char* tagName = NULL);
00203 
00207       virtual void readChild (Base*&          val, const char* tagName = NULL);
00208 
00212       virtual void readChild (Base&           val, const char* tagName = NULL);
00213 
00215       virtual void readChild (TreeNode*&      val, const char* tagName = NULL);
00216 
00220       virtual void deleteString (char* str) const;
00221 
00223 
00225 
00226 
00228       virtual void setVersion(const char* className, int version);
00229 
00231       virtual int getVersion(const char* /*className*/);
00232 
00234 
00236       virtual const char* getLastReadChildName() const;
00237 
00242       static char* normalizeFileName(const char* fileName);
00243 
00245 
00246 
00247       static void initXMLSystem();
00248       static void terminateXMLSystem();
00249 
00251 
00252     protected:
00253 
00254       //------------------------------------------------
00257       //------------------------------------------------
00258 
00260 
00261       XMLTreeNode(DOMElement* parent);
00262 
00264       static char*        _getTextNodeString(DOMElement* parentNode);
00265 
00268       DOMElement*         _findElemByTagName(const char* tagName, DOMNode* prevNode = NULL);
00269 
00271       XMLTreeNode*        _addTreeNodeChild(DOMElement* child);
00272 
00274       DOMElement*         _parentDOMNode;
00275 
00277       static bool         _xercesInitialized;
00278 
00280 
00281     private:
00283       DOMNode*            _lastNode;
00284 
00286       DOMNode*            _lastNodeRead;
00287 
00289       mutable StringX     _lastName;
00290 
00293       ML_CLASS_HEADER(XMLTreeNode);
00294 
00295   }; // class XMLTreeNode
00296 
00297 ML_END_NAMESPACE
00298 
00303 #define ML_PERSISTENCE_SUPPORT_VIA_XML \
00304 virtual char* getPersistentState() const { \
00305   char* cstr = NULL; \
00306   try { \
00307     XMLTreeNode* ioRootNode = new XMLTreeNode(TreeNode::CM_writerRoot); \
00308     addStateToTree(ioRootNode); \
00309     ioRootNode->writeToString(cstr); \
00310     delete ioRootNode; \
00311   } catch (const TreeNodeException& e) { \
00312     ML_PRINT_ERROR("ML_PERSISTENCE_SUPPORT_VIA_XML - getPersistentState()", \
00313       std::string("Exception ") + e.getMessage() + " occurred.", "Returning empty string."); \
00314     return ""; \
00315   } \
00316   return cstr; \
00317 } \
00318 \
00319 virtual void setPersistentState(const char* state) { \
00320   if (!state || std::string(state).empty()) return; \
00321   try { \
00322     XMLTreeNode* ioRootNode = new XMLTreeNode(TreeNode::CM_readerRoot); \
00323     this->readStateFromTree(ioRootNode); \
00324     ioRootNode->readFromString(state); \
00325     delete ioRootNode; \
00326   } catch (const TreeNodeException& e) { \
00327     ML_PRINT_ERROR("ML_PERSISTENCE_SUPPORT_VIA_XML - setPersistentState()", \
00328       std::string("Exception ") + e.getMessage() + " occurred.", \
00329       "Continuing. Warning: Object state may be undefined!"); \
00330   } \
00331 } \
00332 \
00333 \
00334 virtual void clearPersistentState(char* state) const { \
00335   if (!state || std::string(state).empty()) return; \
00336   try { \
00337     XMLTreeNode* ioRootNode = new XMLTreeNode(TreeNode::CM_readerRoot); \
00338     ioRootNode->deleteString(state); \
00339     delete ioRootNode; \
00340   } catch (const TreeNodeException& e) { \
00341     ML_PRINT_ERROR("ML_PERSISTENCE_SUPPORT_VIA_XML - clearPersistentState()", \
00342       std::string("Exception ") + e.getMessage() + " occurred.", \
00343       "Continuing. Warning: Object state may be undefined!"); \
00344   } \
00345 }
00346 
00347 
00348 #endif //__mlXMLTreeNode_H
00349