ICF 3.1.1.10
Technical documentation of ICF Libraries
TXmlFileSerializerComp.h
Go to the documentation of this file.
1/********************************************************************************
2** This file is part of the ICF Framework. Copyright (C) Witold Gantzke & Kirill Lepskiy
3** ICF Framework may be used under the terms of the LGPL License v. 2.1 by the Free Software Foundation.
4********************************************************************************/
5
6#pragma once
7
8
9// Qt includes
10#include <QtCore/QObject>
11#include <QtCore/QDir>
12
13// ICF includes
15#include <istd/CSystem.h>
18
19
20namespace ifile
21{
22
23
27template <class ReadArchive, class WriteArchive>
29{
30public:
32
33 I_BEGIN_COMPONENT(TXmlFileSerializerComp);
34 I_ASSIGN(m_autoCreateDirectoryAttrPtr, "AutoCreatePath", "Create directory/file path automatically if not exists", true, false);
35 I_ASSIGN(m_serializeAcfHeaderAttrPtr, "SerializeIcfHeader", "Enable or disable serializing of standard ICF header with version information", true, true);
36 I_ASSIGN(m_rootTagAttrPtr, "RootTag", "XML tag used as document root", true, "Icf");
37 I_END_COMPONENT;
38
39 // reimplemented (ifile::IFilePersistence)
42 const QString& filePath = QString(),
43 iproc::IProgressManager* progressManagerPtr = nullptr) const override;
45 const istd::IChangeable& data,
46 const QString& filePath = QString(),
47 iproc::IProgressManager* progressManagerPtr = nullptr) const override;
48
49protected:
50 class ReadArchiveEx: public ReadArchive
51 {
52 public:
53 typedef ReadArchive BaseClass;
54
55 ReadArchiveEx(const QString& filePath,
56 bool serializeHeader,
57 const iser::CArchiveTag& rootTag,
58 const TXmlFileSerializerComp* loggerPtr)
59 : ReadArchive(filePath, serializeHeader, rootTag),
60 m_loggerPtr(loggerPtr)
61 {
62 }
63
64 protected:
65 // reimplemented (istd::ILogger)
66 virtual bool IsLogConsumed(
68 const int* flagsPtr = nullptr) const override
69 {
71
72 return (m_loggerPtr != nullptr) && m_loggerPtr->IsLogConsumed(&slaveCategory, flagsPtr);
73 }
74 virtual bool SendLogMessage(
76 int id,
77 const QString& message,
78 const QString& messageSource,
79 int flags = 0) const override
80 {
81 if (m_loggerPtr != nullptr){
82 QString correctedMessage = message;
83 QString correctedMessageSource = messageSource;
84
85 BaseClass::DecorateMessage(category, id, flags, correctedMessage, correctedMessageSource);
86
87 return m_loggerPtr->SendLogMessage(istd::IInformationProvider::IC_INFO, id, correctedMessage, correctedMessageSource, flags);
88 }
89
90 return false;
91 }
92
93 private:
94 const TXmlFileSerializerComp* m_loggerPtr;
95 };
96
97 class WriteArchiveEx: public WriteArchive
98 {
99 public:
100 typedef WriteArchive BaseClass;
101
103 const QString& filePath,
104 const iser::IVersionInfo* infoPtr,
105 bool serializeHeader,
106 const iser::CArchiveTag& rootTag,
107 const TXmlFileSerializerComp* loggerPtr)
108 : WriteArchive(filePath, infoPtr, serializeHeader, rootTag),
109 m_loggerPtr(loggerPtr)
110 {
111 }
112
113 protected:
114 // reimplemented (istd::ILogger)
115 virtual bool IsLogConsumed(
117 const int* flagsPtr = nullptr) const override
118 {
120
121 return (m_loggerPtr != nullptr) && m_loggerPtr->IsLogConsumed(&slaveCategory, flagsPtr);
122 }
123 virtual bool SendLogMessage(
125 int id,
126 const QString& message,
127 const QString& messageSource,
128 int flags = 0) const override
129 {
130 if (m_loggerPtr != nullptr){
131 QString correctedMessage = message;
132 QString correctedMessageSource = messageSource;
133
134 BaseClass::DecorateMessage(category, id, flags, correctedMessage, correctedMessageSource);
135
136 return m_loggerPtr->SendLogMessage(category, id, correctedMessage, correctedMessageSource, flags);
137 }
138
139 return false;
140 }
141
142 private:
143 const TXmlFileSerializerComp* m_loggerPtr;
144 };
145
149 virtual void OnReadError(const ReadArchive& archive, const istd::IChangeable& data, const QString& filePath) const;
150
151protected:
152 I_ATTR(bool, m_autoCreateDirectoryAttrPtr);
153 I_ATTR(bool, m_serializeAcfHeaderAttrPtr);
154 I_ATTR(QByteArray, m_rootTagAttrPtr);
155};
156
157
158// public methods
159
160// reimplemented (ifile::IFilePersistence)
161
162template <class ReadArchive, class WriteArchive>
164 istd::IChangeable& data,
165 const QString& filePath,
166 iproc::IProgressManager* /*progressManagerPtr*/) const
167{
168 if (IsOperationSupported(&data, &filePath, QF_LOAD | QF_FILE, false)){
169 iser::CArchiveTag rootTag(*m_rootTagAttrPtr, "Root of document", iser::CArchiveTag::Type::Group);
170
171 ReadArchiveEx archive(filePath, *m_serializeAcfHeaderAttrPtr, rootTag, this);
172
173 Q_ASSERT(archive.IsChanging());
174
178 iser::ISerializable* serializablePtr = dynamic_cast<iser::ISerializable*>(&data);
179 if (serializablePtr == nullptr){
180 serializablePtr = CompCastPtr<iser::ISerializable>(&data);
181 }
182
183 Q_ASSERT(serializablePtr != nullptr);
184
185 if (serializablePtr->Serialize(archive)){
186 return iproc::CTaskState::Status::Ready;
187 }
188 else{
189 OnReadError(archive, data, filePath);
190 }
191 }
192
193 return iproc::CTaskState::Status::Failed;
194}
195
196
197template <class ReadArchive, class WriteArchive>
199 const istd::IChangeable& data,
200 const QString& filePath,
201 iproc::IProgressManager* /*progressManagerPtr*/) const
202{
203 if (*m_autoCreateDirectoryAttrPtr){
204 QFileInfo fileInfo(filePath);
205
206 if (!istd::CSystem::EnsurePathExists(fileInfo.dir().absolutePath())){
207 SendErrorMessage(MI_FILE_NOT_EXIST, QObject::tr("Cannot create path to file"));
208 }
209 }
210
211 if (IsOperationSupported(&data, &filePath, QF_SAVE | QF_FILE, false)){
212 iser::CArchiveTag rootTag(*m_rootTagAttrPtr, "Root of document", iser::CArchiveTag::Type::Group);
213
214 WriteArchiveEx archive(filePath, GetVersionInfo(), *m_serializeAcfHeaderAttrPtr, rootTag, this);
215 Q_ASSERT(!archive.IsChanging());
216
220 const iser::ISerializable* serializablePtr = dynamic_cast<const iser::ISerializable*>(&data);
221 if (serializablePtr == nullptr){
222 serializablePtr = CompCastPtr<iser::ISerializable>(&data);
223 }
224 Q_ASSERT(serializablePtr != nullptr);
225
226 if (!CheckMinimalVersion(*serializablePtr, archive.GetVersionInfo())){
227 SendWarningMessage(MI_UNSUPPORTED_VERSION, QObject::tr("Archive version is not supported, possible lost of data"));
228 }
229
230 if ((const_cast<iser::ISerializable*>(serializablePtr))->Serialize(archive)){
231 return iproc::CTaskState::Status::Ready;
232 }
233 else{
234 SendInfoMessage(MI_CANNOT_SAVE, QObject::tr("Cannot serialize object to file: '%1'").arg(filePath));
235 }
236 }
237
238 return iproc::CTaskState::Status::Failed;
239}
240
241
242// protected methods
243
244template <class ReadArchive, class WriteArchive>
245void TXmlFileSerializerComp<ReadArchive, WriteArchive>::OnReadError(const ReadArchive& /*archive*/, const istd::IChangeable& /*data*/, const QString& filePath) const
246{
247 SendWarningMessage(MI_CANNOT_LOAD, QObject::tr("Cannot load object from XML file %1").arg(filePath));
248}
249
250
251} // namespace ifile
252
253
Dest * CompCastPtr(istd::IPolymorphic *objectPtr)
Cast to specified interface trying to use component interface query.
Base implementation of file serializer.
virtual bool IsLogConsumed(const istd::IInformationProvider::InformationCategory *, const int *flagsPtr=nullptr) const override
ReadArchiveEx(const QString &filePath, bool serializeHeader, const iser::CArchiveTag &rootTag, const TXmlFileSerializerComp *loggerPtr)
virtual bool SendLogMessage(istd::IInformationProvider::InformationCategory category, int id, const QString &message, const QString &messageSource, int flags=0) const override
WriteArchiveEx(const QString &filePath, const iser::IVersionInfo *infoPtr, bool serializeHeader, const iser::CArchiveTag &rootTag, const TXmlFileSerializerComp *loggerPtr)
virtual bool SendLogMessage(istd::IInformationProvider::InformationCategory category, int id, const QString &message, const QString &messageSource, int flags=0) const override
virtual bool IsLogConsumed(const istd::IInformationProvider::InformationCategory *, const int *flagsPtr=nullptr) const override
Template implementation of file serializer using loading and storing archive implementation.
virtual void OnReadError(const ReadArchive &archive, const istd::IChangeable &data, const QString &filePath) const
Called if read error is occurred.
virtual iproc::CTaskState LoadFromFile(istd::IChangeable &data, const QString &filePath=QString(), iproc::IProgressManager *progressManagerPtr=nullptr) const override
This function loads data data from file filePath.
virtual iproc::CTaskState SaveToFile(const istd::IChangeable &data, const QString &filePath=QString(), iproc::IProgressManager *progressManagerPtr=nullptr) const override
This function saves data data to file filePath.
bool SendLogMessage(istd::IInformationProvider::InformationCategory category, int id, const QString &message, const QString &messageSource, int flags=0) const override
Send any message to log.
void DecorateMessage(istd::IInformationProvider::InformationCategory category, int id, int flags, QString &message, QString &messageSource) const override
Decorate message parts before outputting.
bool IsLogConsumed(const istd::IInformationProvider::InformationCategory *categoryPtr=nullptr, const int *flagsPtr=nullptr) const override
Check if any log message is consumed.
Represent state of asynchronous operation.
Definition CTaskState.h:28
Consume information about progress of some process.
Process tag used to group data in archive stream.
Definition CArchiveTag.h:25
@ Group
Normal tag used for grouping of tags or processed elements.
Common class for all classes which objects can be archived or restored from archive.
virtual bool Serialize(IArchive &archive)=0
Load or store state of this object as a archive stream.
Provides access to version information.
static bool EnsurePathExists(const QString &filePath)
Ensure that the given path exists.
Common interface for data model objects, which can be changed.
Definition IChangeable.h:32
InformationCategory
Category of information.
@ IC_INFO
Normal information level, can be also interpreted as OK.
Contains interfaces and implementations of file system related components.

© Witold Gantzke and Kirill Lepskiy