OGS
XMLQtInterface.cpp
Go to the documentation of this file.
1
13#include "XMLQtInterface.h"
14
15#include <QByteArray>
16#include <QCoreApplication>
17#include <QCryptographicHash>
18#include <QDir>
19#include <QFileInfo>
20#include <QXmlSchema>
21#include <QXmlSchemaValidator>
22#include <QXmlStreamReader>
23#include <fstream>
24#include <utility>
25
26#include "BaseLib/Logging.h"
27
28namespace BaseLib
29{
30namespace IO
31{
33 : schemaFile_(std::move(schemaFile))
34{
35}
36
37int XMLQtInterface::readFile(const QString& fileName)
38{
39 fileName_ = fileName;
40 QFile file(fileName);
41 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
42 {
43 ERR("XMLQtInterface::readFile(): Can't open xml-file {:s}.",
44 fileName.toStdString());
45 return 0;
46 }
47 fileData_ = file.readAll();
48 file.close();
49
50 if (!checkHash())
51 {
52 return 0;
53 }
54
55 return 1;
56}
57
59{
60 QXmlSchema schema;
61 if (schemaFile_.length() > 0)
62 {
63 auto path =
64 QDir(QCoreApplication::applicationDirPath()).filePath(schemaFile_);
65 auto url = QUrl::fromLocalFile(path);
66 schema.load(url);
67 }
68 if (schema.isValid())
69 {
70 QXmlSchemaValidator validator(schema);
71 if (validator.validate(fileData_))
72 {
73 return 1;
74 }
75
76 INFO(
77 "XMLQtInterface::isValid(): XML file {:s} is invalid (in reference "
78 "to schema {:s}).",
79 fileName_.toStdString(), schemaFile_.toStdString());
80 }
81 else
82 {
83 // The following validator (without constructor arguments) automatically
84 // searches for the xsd given in the xml file.
85 QXmlSchemaValidator validator;
86 if (validator.validate(fileData_))
87 {
88 return 1;
89 }
90
91 INFO(
92 "XMLQtInterface::isValid(): XML file {:s} is invalid (in reference "
93 "to its schema).",
94 fileName_.toStdString());
95 }
96 return 0;
97}
98
100{
101 QString md5FileName(fileName_ + ".md5");
102 QByteArray fileHash =
103 QCryptographicHash::hash(fileData_, QCryptographicHash::Md5);
104
105 QFile file(md5FileName);
106 if (file.open(QIODevice::ReadOnly))
107 {
108 QByteArray referenceHash = file.readAll();
109 file.close();
110 if (referenceHash == fileHash)
111 {
112 return true;
113 }
114 INFO("Hashfile does not match data ... checking file ...");
115 }
116
117 if (!this->isValid())
118 {
119 return false;
120 }
121
122 QFile fileMD5(md5FileName);
123 if (fileMD5.open(QIODevice::WriteOnly))
124 {
125 fileMD5.write(fileHash);
126 fileMD5.close();
127 INFO("File is valid, hashfile written.");
128 }
129 else
130 {
131 WARN("File is valid but could not write hashfile!");
132 }
133 return true;
134}
135
136QByteArray const& XMLQtInterface::getContent() const
137{
138 return fileData_;
139}
140} // namespace IO
141} // namespace BaseLib
void INFO(char const *fmt, Args const &... args)
Definition: Logging.h:34
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:44
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:39
QByteArray fileData_
Caches the actual file contents when reading.
QByteArray const & getContent() const
virtual int readFile(const QString &fileName)
XMLQtInterface(QString schemaFile="")
QString fileName_
The actual file name when reading.