OGS
BaseLib::IO::XMLQtInterface Class Reference

Detailed Description

Definition at line 21 of file XMLQtInterface.h.

#include <XMLQtInterface.h>

Inheritance diagram for BaseLib::IO::XMLQtInterface:
[legend]

Public Member Functions

 XMLQtInterface (QString schemaFile="")
virtual ~XMLQtInterface ()=default
virtual int readFile (const QString &fileName)

Protected Member Functions

bool checkHash () const
QByteArray const & getContent () const

Private Member Functions

int isValid () const

Private Attributes

QString fileName_
 The actual file name when reading.
QString schemaFile_
QByteArray fileData_
 Caches the actual file contents when reading.

Constructor & Destructor Documentation

◆ XMLQtInterface()

BaseLib::IO::XMLQtInterface::XMLQtInterface ( QString schemaFile = "")
explicit

◆ ~XMLQtInterface()

virtual BaseLib::IO::XMLQtInterface::~XMLQtInterface ( )
virtualdefault

Member Function Documentation

◆ checkHash()

bool BaseLib::IO::XMLQtInterface::checkHash ( ) const
protected

Checks if a hash for the given data file exists to skip the time-consuming validation part. If a hash file exists and the hash of the data file is the same as the content of the hash file the validation is skipped If no hash file exists, the xml-file is validated and a hash file is written if the xml-file was valid.

Definition at line 90 of file XMLQtInterface.cpp.

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}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
QByteArray fileData_
Caches the actual file contents when reading.
QString fileName_
The actual file name when reading.

References fileData_, fileName_, INFO(), isValid(), and WARN().

Referenced by readFile().

◆ getContent()

QByteArray const & BaseLib::IO::XMLQtInterface::getContent ( ) const
protected

Read access to the content of the read file. Must be used after readFile has been called.

Definition at line 127 of file XMLQtInterface.cpp.

128{
129 return fileData_;
130}

References fileData_.

Referenced by FileIO::XmlPrjInterface::readFile(), GeoLib::IO::XmlGmlInterface::readFile(), and GeoLib::IO::XmlStnInterface::readFile().

◆ isValid()

int BaseLib::IO::XMLQtInterface::isValid ( ) const
private

Check if the given xml-file is valid considering the schema-file used in the constructor

Definition at line 49 of file XMLQtInterface.cpp.

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}

References fileData_, fileName_, INFO(), and schemaFile_.

Referenced by checkHash().

◆ readFile()

int BaseLib::IO::XMLQtInterface::readFile ( const QString & fileName)
virtual

Reads the file. In an overridden function in the child class be sure to call XMLQtInterface::readFile(fileName).

Reimplemented in FileIO::XmlPrjInterface, GeoLib::IO::XmlGmlInterface, and GeoLib::IO::XmlStnInterface.

Definition at line 28 of file XMLQtInterface.cpp.

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}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40

References checkHash(), ERR(), fileData_, and fileName_.

Member Data Documentation

◆ fileData_

QByteArray BaseLib::IO::XMLQtInterface::fileData_
private

Caches the actual file contents when reading.

Definition at line 53 of file XMLQtInterface.h.

Referenced by checkHash(), getContent(), isValid(), and readFile().

◆ fileName_

QString BaseLib::IO::XMLQtInterface::fileName_
private

The actual file name when reading.

Definition at line 48 of file XMLQtInterface.h.

Referenced by checkHash(), isValid(), and readFile().

◆ schemaFile_

QString BaseLib::IO::XMLQtInterface::schemaFile_
private

Definition at line 50 of file XMLQtInterface.h.

Referenced by XMLQtInterface(), and isValid().


The documentation for this class was generated from the following files: