GME
13
|
00001 #ifndef MGA_COREXMLFILE_H 00002 #define MGA_COREXMLFILE_H 00003 00004 #include "resource.h" 00005 #include <fstream.h> 00006 #include <list> 00007 #include <map> 00008 #include <hash_map> 00009 #include <set> 00010 #include <xercesc/dom/dom.hpp> 00011 #include "..\Mga\MgaGeneric.h" 00012 00013 const metaobjidpair_type ROOT_ID_PAIR = {METAID_ROOT, OBJID_ROOT}; 00014 00015 struct GUID_less 00016 { 00017 bool operator()(const GUID& a, const GUID& b) const 00018 { 00019 if( a.Data1 < b.Data1 ) 00020 return true; 00021 else if( a.Data1 > b.Data1 ) 00022 return false; 00023 else 00024 { 00025 DWORD * adw = (DWORD*)&a; 00026 DWORD * bdw = (DWORD*)&b; 00027 if( *(adw+1) < *(bdw+1) ) 00028 return true; 00029 else if( *(adw+1) > *(bdw+1) ) 00030 return false; 00031 else if( *(adw+2) < *(bdw+2) ) 00032 return true; 00033 else if( *(adw+2) > *(bdw+2) ) 00034 return false; 00035 else if( *(adw+3) < *(bdw+3) ) 00036 return true; 00037 else 00038 return false; 00039 } 00040 } 00041 }; 00042 00043 class XmlObject; 00044 00045 typedef set<XmlObject*> XmlObjSet; 00046 typedef XmlObjSet::iterator XmlObjSetIter; 00047 typedef vector<XmlObject*> XmlObjVec; 00048 typedef XmlObjVec::iterator XmlObjVecIter; 00049 typedef map<metaid_type,attrid_type> ParentMap; 00050 typedef map<GUID, XmlObject*,GUID_less> GUIDToXmlObjectMap; 00051 typedef GUIDToXmlObjectMap::iterator GUIDToXmlObjectMapIter; 00052 00053 00055 // XmlAttrBase class 00057 class XmlAttrBase 00058 { 00059 public: 00060 static XmlAttrBase * create (valtype_type type); 00061 00062 XmlAttrBase (); 00063 virtual ~XmlAttrBase (); 00064 00065 00066 virtual valtype_type getType () const = 0; 00067 virtual void fromVariant (VARIANT p) {} 00068 virtual void toVariant (VARIANT *p) const {} 00069 virtual void fromString (const char * str) {} 00070 virtual void toString (string& str) const {} 00071 }; 00072 00073 typedef map<attrid_type,XmlAttrBase*> AttribMap; 00074 typedef AttribMap::iterator AttribMapIter; 00075 00076 00078 // XmlAttrLong class 00080 00081 class XmlAttrLong: public XmlAttrBase 00082 { 00083 public: 00084 XmlAttrLong (); 00085 00086 virtual valtype_type getType () const; 00087 virtual void fromVariant (VARIANT p); 00088 virtual void toVariant (VARIANT *p) const; 00089 virtual void fromString (const char * str); 00090 virtual void toString (string& str) const; 00091 protected: 00092 long m_value; 00093 }; 00094 00096 // XmlAttrReal class 00098 00099 class XmlAttrReal: public XmlAttrBase 00100 { 00101 public: 00102 XmlAttrReal (); 00103 00104 virtual valtype_type getType () const; 00105 virtual void fromVariant (VARIANT p); 00106 virtual void toVariant (VARIANT *p) const; 00107 virtual void fromString (const char * str); 00108 virtual void toString (string& str) const; 00109 protected: 00110 double m_value; 00111 }; 00112 00114 // XmlAttrString class 00116 00117 class XmlAttrString: public XmlAttrBase 00118 { 00119 public: 00120 virtual valtype_type getType () const; 00121 virtual void fromVariant (VARIANT p); 00122 virtual void toVariant (VARIANT *p) const; 00123 virtual void fromString (const char * str); 00124 virtual void toString (string& str) const; 00125 protected: 00126 string m_value; 00127 }; 00128 00130 // XmlAttrBinary class 00132 00133 class XmlAttrBinary: public XmlAttrBase 00134 { 00135 public: 00136 virtual valtype_type getType () const; 00137 virtual void fromVariant (VARIANT p); 00138 virtual void toVariant (VARIANT *p) const; 00139 virtual void fromString (const char * str); 00140 virtual void toString (string& str) const; 00141 protected: 00142 bindata m_value; 00143 }; 00144 00146 // XmlAttrLock class 00148 00149 class XmlAttrLock: public XmlAttrBase 00150 { 00151 public: 00152 XmlAttrLock (); 00153 00154 virtual valtype_type getType () const; 00155 virtual void fromVariant (VARIANT p); 00156 virtual void toVariant (VARIANT *p) const; 00157 virtual void fromString (const char * str); 00158 virtual void toString (string& str) const; 00159 protected: 00160 lockval_type m_value; 00161 }; 00162 00164 // XmlAttrPointer class 00166 00167 class XmlAttrPointer: public XmlAttrBase 00168 { 00169 public: 00170 XmlAttrPointer (); 00171 00172 virtual valtype_type getType () const; 00173 protected: 00174 XmlObject * m_parent; 00175 00176 friend class CCoreXmlFile; 00177 }; 00178 00180 // XmlAttrCollection class 00182 00183 class XmlAttrCollection: public XmlAttrBase 00184 { 00185 public: 00186 virtual valtype_type getType () const; 00187 protected: 00188 XmlObjSet m_children; 00189 00190 friend class CCoreXmlFile; 00191 }; 00192 00193 00195 // XmlObject class 00197 00198 class XmlObject 00199 { 00200 public: 00201 XmlObject (ICoreMetaObject *metaobject, bool createAllAttributes=true); 00202 ~XmlObject (); 00203 00204 protected: 00205 void createAttributes (ICoreMetaObject *metaobject, bool all=false); 00206 void deleteSecondaryAttribs (); 00207 00208 protected: 00209 long m_metaid; 00210 GUID m_guid; 00211 int m_index; // index in the m_objects array 00212 AttribMap m_attributes; 00213 bool m_loaded; // true if all attributes are loded (otherwise only pointer, collections and locks stored) 00214 00215 bool m_deleted; 00216 //bool m_readOnly; 00217 bool m_modified; 00218 00219 //GUID m_fileLevelObject; // point to the ascendent which stores this, or GUID_NULL if this is a file level object 00220 00221 friend class CCoreXmlFile; 00222 }; 00223 00224 struct UnresolvedPointer 00225 { 00226 XmlObject * m_object; // whose attribute is this 00227 attrid_type m_attrib; 00228 GUID m_pointedObjGuid; 00229 }; 00230 00231 typedef vector<UnresolvedPointer> UnresolvedPointerVec; 00232 typedef UnresolvedPointerVec::iterator UnresolvedPointerVecIt; 00233 00234 00235 class ATL_NO_VTABLE CCoreXmlFile : 00236 public CComObjectRootEx<CComSingleThreadModel>, 00237 public IDispatchImpl<ICoreStorage, &IID_ICoreStorage, &LIBID_CORELib>, 00238 public ISupportErrorInfoImpl<&IID_ICoreStorage>, 00239 public CComCoClass<CCoreXmlFile, &CLSID_CoreBinFile> 00240 { 00241 public: 00242 CCoreXmlFile(); 00243 ~CCoreXmlFile(); 00244 00245 BEGIN_COM_MAP(CCoreXmlFile) 00246 COM_INTERFACE_ENTRY(ICoreStorage) 00247 COM_INTERFACE_ENTRY(IDispatch) 00248 COM_INTERFACE_ENTRY(ISupportErrorInfo) 00249 END_COM_MAP() 00250 00251 DECLARE_REGISTRY_RESOURCEID(IDR_COREXMLFILE) 00252 00253 public: 00254 STDMETHODIMP get_MetaProject (ICoreMetaProject **p); 00255 STDMETHODIMP put_MetaProject (ICoreMetaProject *p); 00256 STDMETHODIMP get_MetaObject (ICoreMetaObject **p); 00257 STDMETHODIMP put_MetaObject (ICoreMetaObject *p); 00258 STDMETHODIMP get_MetaID (metaid_type *p); 00259 STDMETHODIMP put_MetaID (metaid_type p); 00260 STDMETHODIMP get_MetaAttribute (ICoreMetaAttribute **p); 00261 STDMETHODIMP put_MetaAttribute (ICoreMetaAttribute *p); 00262 STDMETHODIMP get_AttrID (attrid_type *p); 00263 STDMETHODIMP put_AttrID (attrid_type p); 00264 STDMETHODIMP get_AttributeValue (VARIANT *p); 00265 STDMETHODIMP put_AttributeValue (VARIANT p); 00266 STDMETHODIMP OpenObject (objid_type objid); 00267 STDMETHODIMP CreateObject (objid_type *objid); 00268 STDMETHODIMP CloseObject (); 00269 STDMETHODIMP DeleteObject (); 00270 STDMETHODIMP LockObject (); 00271 STDMETHODIMP OpenProject (BSTR connection, VARIANT_BOOL *ro_mode); 00272 STDMETHODIMP CreateProject (BSTR connection); 00273 STDMETHODIMP SaveProject (BSTR connection, VARIANT_BOOL keepoldname); 00274 STDMETHODIMP CloseProject (VARIANT_BOOL abort = VARIANT_FALSE); 00275 STDMETHODIMP DeleteProject (); 00276 STDMETHODIMP BeginTransaction (); 00277 STDMETHODIMP CommitTransaction (); 00278 STDMETHODIMP AbortTransaction (); 00279 STDMETHODIMP get_StorageType (long *p); 00280 00281 protected: 00282 void fillParentMap (); 00283 00284 void closeMetaProject (); 00285 void openMetaObject (); 00286 void closeMetaObject (); 00287 void openMetaAttribute (); 00288 void closeMetaAttribute (); 00289 00290 void setFolderPathOnCreate (BSTR connection); 00291 void setFolderPathOnOpen (BSTR connection); 00292 00293 // graph operatrions 00294 void clearAll (); 00295 00296 void addObject (XmlObject * obj); 00297 00298 void removeObject (XmlObject * obj); 00299 00300 void setPointer (XmlObject * obj, attrid_type attribId, XmlObject * pointed); 00301 00302 void setPointer (XmlAttrPointer * attr, VARIANT p); 00303 00304 void updateCollections (); 00305 00306 void resolvePointers (UnresolvedPointerVec& pointers); 00307 00308 // getting connections 00309 void getPointer (XmlAttrPointer * attr, VARIANT * p); 00310 00311 void getCollection (XmlAttrCollection * attr, VARIANT * p); 00312 00313 // object pointer and id conversion 00314 XmlObject * objectFromObjId (metaobjidpair_type idpair); 00315 00316 void objIdFromObject (XmlObject * obj, metaobjidpair_type& idpair); 00317 00318 // serialization 00319 void writeBinaryCache (); 00320 00321 void readBinaryCache (); 00322 00323 void writeAll (); 00324 00325 void writeXMLFile (XmlObject * container); 00326 00327 void writeObject (XmlObject * obj, FILE * file, bool container, const char * prefix); 00328 00329 void readXMLFile (const char * fileName, UnresolvedPointerVec& pointers ); 00330 00331 void readObject (XERCES_CPP_NAMESPACE::DOMElement * e, UnresolvedPointerVec& pointers, XmlObject * parent); 00332 00333 void readAll (); 00334 00335 00336 00337 void incrementalUpdate (); 00338 00339 00340 protected: 00341 // meta project 00342 CComObjPtr<ICoreMetaProject> m_metaProject; 00343 00344 // meta object 00345 CComObjPtr<ICoreMetaObject> m_metaObject; 00346 metaid_type m_metaObjectId; 00347 00348 // meta attribute 00349 CComObjPtr<ICoreMetaAttribute> m_metaAttribute; 00350 attrid_type m_metaAttributeId; 00351 valtype_type m_metaAttributeValType; 00352 00353 ParentMap m_parentMap; 00354 00355 // project 00356 bool m_opened; 00357 bool m_modified; 00358 string m_folderPath; // absolute path of the folder contains the project files 00359 string m_cacheFileName; 00360 bool m_inTransaction; 00361 00362 // objects 00363 XmlObjVec m_objects; 00364 GUIDToXmlObjectMap m_objectsByGUID; 00365 XmlObject * m_openedObject; 00366 XmlObject * m_root; 00367 }; 00368 00369 #endif//MGA_COREXMLFILE_H