OGS
BaseLib::IO::XMLQtInterface Class Reference

Detailed Description

Definition at line 28 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. More...
 
QString schemaFile_
 
QByteArray fileData_
 Caches the actual file contents when reading. More...
 

Constructor & Destructor Documentation

◆ XMLQtInterface()

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

Definition at line 32 of file XMLQtInterface.cpp.

33 : schemaFile_(std::move(schemaFile))
34{
35}

◆ ~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 99 of file XMLQtInterface.cpp.

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}
void INFO(char const *fmt, Args const &... args)
Definition: Logging.h:34
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:39
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 136 of file XMLQtInterface.cpp.

137{
138 return fileData_;
139}

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 58 of file XMLQtInterface.cpp.

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}

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 37 of file XMLQtInterface.cpp.

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}
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:44

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 60 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 55 of file XMLQtInterface.h.

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

◆ schemaFile_

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

Definition at line 57 of file XMLQtInterface.h.

Referenced by isValid().


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