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