TXmlFileSerializerComp.h
Go to the documentation of this file.
1 #ifndef ifile_TXmlFileSerializerComp_included
2 #define ifile_TXmlFileSerializerComp_included
3 
4 
5 // Qt includes
6 #include <QtCore/QObject>
7 #include <QtCore/QDir>
8 
9 // ACF includes
10 #include <istd/CChangeNotifier.h>
11 #include <istd/CSystem.h>
12 #include <ibase/IProgressManager.h>
14 
15 
16 namespace ifile
17 {
18 
19 
25 template <class ReadArchive, class WriteArchive>
27 {
28 public:
30 
31  I_BEGIN_COMPONENT(TXmlFileSerializerComp);
32  I_ASSIGN(m_autoCreateDirectoryAttrPtr, "AutoCreatePath", "Create directory/file path automatically if not exists", true, false);
33  I_ASSIGN(m_serializeAcfHeaderAttrPtr, "SerializeAcfHeader", "Enable or disable serializing of standard ACF header with version information", true, true);
34  I_ASSIGN(m_rootTagAttrPtr, "RootTag", "XML tag used as document root", true, "Acf");
35  I_END_COMPONENT;
36 
37  // reimplemented (ifile::IFilePersistence)
38  virtual int LoadFromFile(
39  istd::IChangeable& data,
40  const QString& filePath = QString(),
41  ibase::IProgressManager* progressManagerPtr = NULL) const;
42  virtual int SaveToFile(
43  const istd::IChangeable& data,
44  const QString& filePath = QString(),
45  ibase::IProgressManager* progressManagerPtr = NULL) const;
46 
47 protected:
48  class ReadArchiveEx: public ReadArchive
49  {
50  public:
51  typedef ReadArchive BaseClass;
52 
53  ReadArchiveEx(const QString& filePath,
54  bool serializeHeader,
55  const iser::CArchiveTag& rootTag,
56  const TXmlFileSerializerComp* loggerPtr)
57  : ReadArchive(filePath, serializeHeader, rootTag),
58  m_loggerPtr(loggerPtr)
59  {
60  }
61 
62  protected:
63  // reimplemented (istd::ILogger)
64  virtual bool IsLogConsumed(
66  const int* flagsPtr = NULL) const
67  {
69 
70  return (m_loggerPtr != NULL) && m_loggerPtr->IsLogConsumed(&slaveCategory, flagsPtr);
71  }
72  virtual bool SendLogMessage(istd::IInformationProvider::InformationCategory category, int id, const QString& message, const QString& messageSource, int flags = 0) const
73  {
74  if (m_loggerPtr != NULL){
75  QString correctedMessage = message;
76  QString correctedMessageSource = messageSource;
77 
78  BaseClass::DecorateMessage(category, id, flags, correctedMessage, correctedMessageSource);
79 
80  return m_loggerPtr->SendLogMessage(istd::IInformationProvider::IC_INFO, id, correctedMessage, correctedMessageSource, flags);
81  }
82 
83  return false;
84  }
85 
86  private:
87  const TXmlFileSerializerComp* m_loggerPtr;
88  };
89 
90  class WriteArchiveEx: public WriteArchive
91  {
92  public:
93  typedef WriteArchive BaseClass;
94 
96  const QString& filePath,
97  const iser::IVersionInfo* infoPtr,
98  bool serializeHeader,
99  const iser::CArchiveTag& rootTag,
100  const TXmlFileSerializerComp* loggerPtr)
101  : WriteArchive(filePath, infoPtr, serializeHeader, rootTag),
102  m_loggerPtr(loggerPtr)
103  {
104  }
105 
106  protected:
107  // reimplemented (istd::ILogger)
108  virtual bool IsLogConsumed(
110  const int* flagsPtr = NULL) const
111  {
113 
114  return (m_loggerPtr != NULL) && m_loggerPtr->IsLogConsumed(&slaveCategory, flagsPtr);
115  }
116  virtual bool SendLogMessage(istd::IInformationProvider::InformationCategory category, int id, const QString& message, const QString& messageSource, int flags = 0) const
117  {
118  if (m_loggerPtr != NULL){
119  QString correctedMessage = message;
120  QString correctedMessageSource = messageSource;
121 
122  BaseClass::DecorateMessage(category, id, flags, correctedMessage, correctedMessageSource);
123 
124  return m_loggerPtr->SendLogMessage(category, id, correctedMessage, correctedMessageSource, flags);
125  }
126 
127  return false;
128  }
129 
130  private:
131  const TXmlFileSerializerComp* m_loggerPtr;
132  };
133 
137  virtual void OnReadError(const ReadArchive& archive, const istd::IChangeable& data, const QString& filePath) const;
138 
139 protected:
140  I_ATTR(bool, m_autoCreateDirectoryAttrPtr);
141  I_ATTR(bool, m_serializeAcfHeaderAttrPtr);
142  I_ATTR(QByteArray, m_rootTagAttrPtr);
143 };
144 
145 
146 // public methods
147 
148 // reimplemented (ifile::IFilePersistence)
149 
150 template <class ReadArchive, class WriteArchive>
152  istd::IChangeable& data,
153  const QString& filePath,
154  ibase::IProgressManager* /*progressManagerPtr*/) const
155 {
156  if (IsOperationSupported(&data, &filePath, QF_LOAD | QF_FILE, false)){
157  iser::CArchiveTag rootTag(*m_rootTagAttrPtr, "Root of document", iser::CArchiveTag::TT_GROUP);
158 
159  ReadArchiveEx archive(filePath, *m_serializeAcfHeaderAttrPtr, rootTag, this);
160 
161  Q_ASSERT(!archive.IsStoring());
162 
166  iser::ISerializable* serializablePtr = dynamic_cast<iser::ISerializable*>(&data);
167  if (serializablePtr == NULL){
168  serializablePtr = CompCastPtr<iser::ISerializable>(&data);
169  }
170 
171  Q_ASSERT(serializablePtr != NULL);
172 
173  if (serializablePtr->Serialize(archive)){
174  return OS_OK;
175  }
176  else{
177  OnReadError(archive, data, filePath);
178  }
179  }
180 
181  return OS_FAILED;
182 }
183 
184 
185 template <class ReadArchive, class WriteArchive>
187  const istd::IChangeable& data,
188  const QString& filePath,
189  ibase::IProgressManager* /*progressManagerPtr*/) const
190 {
191  if (*m_autoCreateDirectoryAttrPtr){
192  QFileInfo fileInfo(filePath);
193 
194  if (!istd::CSystem::EnsurePathExists(fileInfo.dir().absolutePath())){
195  SendErrorMessage(MI_FILE_NOT_EXIST, QObject::tr("Cannot create path to file"));
196  }
197  }
198 
199  if (IsOperationSupported(&data, &filePath, QF_SAVE | QF_FILE, false)){
200  iser::CArchiveTag rootTag(*m_rootTagAttrPtr, "Root of document", iser::CArchiveTag::TT_GROUP);
201 
202  WriteArchiveEx archive(filePath, GetVersionInfo(), *m_serializeAcfHeaderAttrPtr, rootTag, this);
203  Q_ASSERT(archive.IsStoring());
204 
208  const iser::ISerializable* serializablePtr = dynamic_cast<const iser::ISerializable*>(&data);
209  if(serializablePtr == NULL){
210  serializablePtr = CompCastPtr<iser::ISerializable>(&data);
211  }
212  Q_ASSERT(serializablePtr != NULL);
213 
214  if (!CheckMinimalVersion(*serializablePtr, archive.GetVersionInfo())){
215  SendWarningMessage(MI_UNSUPPORTED_VERSION, QObject::tr("Archive version is not supported, possible lost of data"));
216  }
217 
218  if ((const_cast<iser::ISerializable*>(serializablePtr))->Serialize(archive)){
219  return OS_OK;
220  }
221  else{
222  SendInfoMessage(MI_CANNOT_SAVE, QObject::tr("Cannot serialize object to file: '%1'").arg(filePath));
223  }
224  }
225 
226  return OS_FAILED;
227 }
228 
229 
230 // protected methods
231 
232 template <class ReadArchive, class WriteArchive>
233 void TXmlFileSerializerComp<ReadArchive, WriteArchive>::OnReadError(const ReadArchive& /*archive*/, const istd::IChangeable& /*data*/, const QString& filePath) const
234 {
235  SendWarningMessage(MI_CANNOT_LOAD, QString(QObject::tr("Cannot load object from file ")) + filePath);
236 }
237 
238 
239 } // namespace ifile
240 
241 
242 #endif // !ifile_TXmlFileSerializerComp_included
243 
244 
static bool EnsurePathExists(const QString &filePath)
Ensure that the given path exists.
virtual void OnReadError(const ReadArchive &archive, const istd::IChangeable &data, const QString &filePath) const
Called if read error is occurred.
Provides access to version information.
Definition: IVersionInfo.h:22
virtual int LoadFromFile(istd::IChangeable &data, const QString &filePath=QString(), ibase::IProgressManager *progressManagerPtr=NULL) const
This function loads data data from file filePath.
Template implementation of file serializer using loading and storing archive implementation.
Base implementation of file serializer.
Common class for all classes which objects can be archived or restored from archive.
Definition: ISerializable.h:23
virtual bool SendLogMessage(istd::IInformationProvider::InformationCategory category, int id, const QString &message, const QString &messageSource, int flags=0) const
Send any message to log.
Normal tag used for grouping of tags or processed elements.
Definition: CArchiveTag.h:37
Common interface for data model objects, which can be changed.
Definition: IChangeable.h:24
virtual bool SendLogMessage(istd::IInformationProvider::InformationCategory category, int id, const QString &message, const QString &messageSource, int flags=0) const
virtual bool SendLogMessage(istd::IInformationProvider::InformationCategory category, int id, const QString &message, const QString &messageSource, int flags=0) const
ReadArchiveEx(const QString &filePath, bool serializeHeader, const iser::CArchiveTag &rootTag, const TXmlFileSerializerComp *loggerPtr)
InformationCategory
Category of information.
#define NULL
Definition: istd.h:64
virtual void DecorateMessage(istd::IInformationProvider::InformationCategory category, int id, int flags, QString &message, QString &messageSource) const
CFileSerializerCompBase BaseClass
virtual bool IsLogConsumed(const istd::IInformationProvider::InformationCategory *, const int *flagsPtr=NULL) const
Process tag used to group data in archive stream.
Definition: CArchiveTag.h:21
virtual bool IsLogConsumed(const istd::IInformationProvider::InformationCategory *, const int *flagsPtr=NULL) const
virtual bool IsLogConsumed(const istd::IInformationProvider::InformationCategory *categoryPtr=NULL, const int *flagsPtr=NULL) const
Check if any log message is consumed.
WriteArchiveEx(const QString &filePath, const iser::IVersionInfo *infoPtr, bool serializeHeader, const iser::CArchiveTag &rootTag, const TXmlFileSerializerComp *loggerPtr)
Consume information about progress of some process.
virtual int SaveToFile(const istd::IChangeable &data, const QString &filePath=QString(), ibase::IProgressManager *progressManagerPtr=NULL) const
This function saves data data to file filePath.

© 2007-2017 Witold Gantzke and Kirill Lepskiy