MeVisLabToolboxReference
MeVisLab/Standard/Sources/ML/MLParser/mlXMLParser/mlXMLPersistenceStream.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00004 
00009 //--------------------------------------------------------------------------------
00010 
00011 #ifndef __mlXMLPersistenceStream_H
00012 #define __mlXMLPersistenceStream_H
00013 
00014 #include "mlBase.h"
00015 #include "mlBasics.h"
00016 #include "mlAbstractPersistenceStream.h"
00017 #include "../mlParserSystem.h"
00018 #include "mlXMLParserSystem.h"
00019 //#include <xercesc/util/XercesDefs.hpp>
00020 
00021 namespace XERCES_CPP_NAMESPACE {
00022 
00023   class DOMElement;
00024   class DOMNode;
00025 
00026 }
00027 
00028 using namespace XERCES_CPP_NAMESPACE;
00029 
00030 
00031 ML_START_NAMESPACE
00032 
00033 //--------------------------------------------------------------------------------
00034 
00037   class XMLPersistenceStream
00038   {
00039   public:
00040     struct ListStackEntry
00041     {
00042       ListStackEntry(const char* anItemName, bool aSuppressScope) 
00043         : itemName(anItemName), suppressScope(aSuppressScope) {}
00044 
00045       const char* itemName;
00046       bool suppressScope;
00047     };
00048 
00049   protected:
00050     XMLPersistenceStream() {}
00051 
00052     std::list<ListStackEntry> _listStack;
00053   };
00054 
00055 
00056 //--------------------------------------------------------------------------------
00057 
00059   class XMLPersistenceOutputStream : public AbstractPersistenceOutputStream, private XMLPersistenceStream
00060   {
00061   public:
00064     XMLPersistenceOutputStream();
00065 
00068     XMLPersistenceOutputStream(DOMElement* container);
00069 
00071     virtual ~XMLPersistenceOutputStream();
00072 
00074     virtual void writeToFile(const std::string& fileName);
00075 
00077     virtual void writeToString(std::string& str);
00078 
00080 
00081     virtual bool isBinary() const { return false; }
00082 
00083     virtual void writeString(const char* name, const std::string& value);
00084 
00085     virtual void writeData(const void* data, size_t len, const char* name);
00086 
00087   protected:
00088     virtual void startListImpl(const char* name, const char* xmlItemName, bool xmlSuppressScope);
00089     virtual void endListImpl();
00090     virtual void startStructImpl(const char* name);
00091     virtual void endStructImpl();
00092 
00093     virtual void setObjectID(int id);
00094     virtual void setObjectType(const char* type);
00095     virtual void setObjectVersion(int version);
00096 
00097     virtual void writeValues(const char* name, const MLint32*  values, size_t n, bool fixedList);
00098     virtual void writeValues(const char* name, const MLuint32* values, size_t n, bool fixedList);
00099     virtual void writeValues(const char* name, const MLint64*  values, size_t n, bool fixedList);
00100     virtual void writeValues(const char* name, const MLuint64* values, size_t n, bool fixedList);
00101     virtual void writeValues(const char* name, const MLfloat*  values, size_t n, bool fixedList);
00102     virtual void writeValues(const char* name, const MLdouble* values, size_t n, bool fixedList);
00104 
00105   private:
00107     template<typename T>
00108     void _writeValuesT(const char* name, const T* values, size_t n, bool fixedList);
00109 
00111     void _setAttribute(const char* attrName, const char* value);
00112     void _setAttribute(const char* attrName, int value);
00113 
00116     void _createScope(const char* tagName);
00117 
00119     void _closeScope();
00120 
00122     DOMElement* _rootNode;
00124     DOMElement* _currentNode;
00125   };
00126 
00127 //--------------------------------------------------------------------------------
00128 
00130   class XMLPersistenceInputStream : public AbstractPersistenceInputStream, private XMLPersistenceStream
00131   {
00132   public:
00134     XMLPersistenceInputStream();
00135 
00137     XMLPersistenceInputStream(DOMElement* container);
00138 
00140     virtual ~XMLPersistenceInputStream();
00141 
00143     virtual void readFromFile(const std::string& fileName);
00144 
00146     virtual void readFromString(const std::string& str);
00147 
00149     static void normalizeWindowsFileName(std::string& str);
00150 
00152 
00153     virtual bool isBinary() const { return false; }
00154 
00155     virtual void readString(const char* name, std::string& value);
00156 
00157     virtual void readData(std::string& value, const char* name);
00158 
00159   protected:
00160     virtual void startListImpl(const char* name, const char* xmlItemName, bool xmlSuppressScope);
00161     virtual void endListImpl();
00162     virtual void startStructImpl(const char* name);
00163     virtual void endStructImpl();
00164 
00165     virtual bool hasNextInStructImpl(std::string& name);
00166     virtual bool hasNextListItemImpl();
00167 
00168     virtual bool getObjectID(int& id);
00169     virtual bool getObjectType(std::string& type);
00170     virtual bool getObjectVersion(int& version);
00171 
00172     virtual void readValues(const char* name, size_t expected, std::vector<MLint32>&  values);
00173     virtual void readValues(const char* name, size_t expected, std::vector<MLuint32>& values);
00174     virtual void readValues(const char* name, size_t expected, std::vector<MLint64>&  values);
00175     virtual void readValues(const char* name, size_t expected, std::vector<MLuint64>& values);
00176     virtual void readValues(const char* name, size_t expected, std::vector<MLfloat>&  values);
00177     virtual void readValues(const char* name, size_t expected, std::vector<MLdouble>& values);
00178 
00180     void formatError(const std::string& msg);
00182 
00183   private:
00185     template<typename T>
00186     void _readValuesT(const char* name, size_t expected, std::vector<T>& values);
00187 
00189     bool _getAttribute(const char* attrName, std::string& value);
00190     bool _getAttribute(const char* attrName, int& value);
00191 
00193     bool _checkElement(const char* name, bool throwError = true);
00194 
00196     void _toNextElement(bool reset = false);
00197 
00199     void _enterScope(const char* tagName);
00200 
00202     void _leaveScope();
00203 
00205     DOMElement* _rootNode;
00207     DOMElement* _currentNode;
00209     DOMElement* _nextNode;
00210   };
00211 
00212 //--------------------------------------------------------------------------------
00213 
00214 ML_END_NAMESPACE
00215 
00216 #endif //__mlXMLPersistenceStream_H
00217