Skip to content
Snippets Groups Projects
Commit 11ff458c authored by Tom Fischer's avatar Tom Fischer
Browse files

Moving Qt dependencies out of XMLInterface to XMLQtInterface.

parent 0fdb8281
No related branches found
No related tags found
No related merge requests found
...@@ -12,131 +12,13 @@ ...@@ -12,131 +12,13 @@
* *
*/ */
#include <fstream>
// ThirdParty/logog
#include "logog/include/logog.hpp"
#include "ProjectData.h"
#include "XMLInterface.h" #include "XMLInterface.h"
//#include "Configure.h"
#include <QCryptographicHash>
#include <QFileInfo>
#include <QtXmlPatterns/QXmlSchema>
#include <QtXmlPatterns/QXmlSchemaValidator>
namespace FileIO namespace FileIO
{ {
XMLInterface::XMLInterface(const std::string &schemaFile) :
_exportName(""), _schemaName(schemaFile)
{
}
int XMLInterface::isValid(const QString &fileName) const
{
QXmlSchema schema;
schema.load( QUrl::fromLocalFile((QString::fromStdString(_schemaName))) );
if ( schema.isValid() )
{
QXmlSchemaValidator validator( schema );
if ( validator.validate( QUrl::fromLocalFile((fileName))) )
return 1;
else
{
INFO("XMLInterface::isValid(): XML file %s is invalid (in reference to schema %s).",
fileName.data(), _schemaName.c_str());
return 0;
}
}
else
{
INFO("XMLInterface::isValid(): Schema %s is invalid.", _schemaName.c_str());
return 0;
}
}
void XMLInterface::setSchema(const std::string &schemaName)
{
_schemaName = schemaName;
}
int XMLInterface::insertStyleFileDefinition(const QString &fileName) const
{
std::string path = fileName.toStdString();
std::fstream stream(path.c_str());
std::string line;
std::string styleDef("\n<?xml-stylesheet type=\"text/xsl\" href=\"OpenGeoSysGLI.xsl\"?>");
if (!stream.is_open()) XMLInterface::XMLInterface() :
{ _exportName("")
WARN("XMLInterface::insertStyleFileDefinition(): Could not open file %s.", {}
path.c_str());
return 0;
}
stream.seekp(43 * sizeof(char),std::ios_base::beg); // go to the correct position in the stream
stream.write(styleDef.c_str(), 60 * sizeof(char)); // write new line with xml-stylesheet definition
stream.close();
return 1;
}
bool XMLInterface::checkHash(const QString &fileName) const
{
QFileInfo fi(fileName);
QString md5FileName(fileName + ".md5");
std::ifstream md5( md5FileName.toStdString().c_str() );
if (md5.is_open())
{
char* md5HashStr = new char[16];
md5.read(md5HashStr, 16);
QByteArray md5Hash(md5HashStr, 16);
delete[] md5HashStr;
if (hashIsGood(fileName, md5Hash))
return true;
}
if (!this->isValid(fileName))
return false;
INFO("File is valid, writing hashfile.");
QByteArray hash = calcHash(fileName);
std::ofstream out( md5FileName.toStdString().c_str(), std::ios::out );
out.write(hash.data(), 16);
out.close();
return true;
}
bool XMLInterface::hashIsGood(const QString &fileName, const QByteArray &hash) const
{
int hashLength = hash.length();
QByteArray fileHash = calcHash(fileName);
if (fileHash.length() != hashLength)
return false;
for (int i = 0; i < hashLength; i++)
if (fileHash[i] != hash[i])
{
INFO("Hashfile does not match data ... checking file ...");
return false;
}
return true;
}
QByteArray XMLInterface::calcHash(const QString &fileName) const
{
std::ifstream is(fileName.toStdString().c_str(), std::ios::binary );
is.seekg (0, std::ios::end);
int length = is.tellg();
is.seekg (0, std::ios::beg);
char* buffer = new char [length];
is.read (buffer,length);
is.close();
QByteArray hash = QCryptographicHash::hash(buffer, QCryptographicHash::Md5);
delete [] buffer;
return hash;
}
} }
...@@ -17,16 +17,8 @@ ...@@ -17,16 +17,8 @@
#include "OGS/ProjectData.h" #include "OGS/ProjectData.h"
#include <QXmlStreamReader>
#include "Writer.h" #include "Writer.h"
class FEMCondition;
class QFile;
class QDomDocument;
class QDomNode;
class QDomElement;
namespace FileIO namespace FileIO
{ {
/** /**
...@@ -35,44 +27,15 @@ namespace FileIO ...@@ -35,44 +27,15 @@ namespace FileIO
class XMLInterface : public Writer class XMLInterface : public Writer
{ {
public: public:
/** XMLInterface();
* Constructor
* \param schemaFile An XML schema file (*.xsd) that defines the structure of a valid data file.
*/
XMLInterface(const std::string &schemaFile);
virtual ~XMLInterface() {} virtual ~XMLInterface() {}
/// As QXMLStreamWriter seems currently unable to include style-file links into xml-files, this method will workaround this issue and include the stylefile link.
int insertStyleFileDefinition(const QString &fileName) const;
/// Check if the given xml-file is valid considering the schema-file used in the constructor
int isValid(const QString &fileName) const;
void setNameForExport(std::string const& name) { _exportName = name; } void setNameForExport(std::string const& name) { _exportName = name; }
/// Sets the schema filename used to check if xml files are valid.
void setSchema(const std::string &schemaName);
/// Reads an xml-file.
virtual int readFile(const QString &fileName) = 0;
protected: 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.
bool checkHash(const QString &fileName) const;
/// Calculates an MD5 hash of the given file.
QByteArray calcHash(const QString &fileName) const;
/// Checks if the given file is conform to the given hash.
bool hashIsGood(const QString &fileName, const QByteArray &hash) const;
std::string _exportName; std::string _exportName;
std::string _schemaName;
std::map<std::size_t, std::size_t> _idx_map;
}; };
} }
#endif // XMLINTERFACE_H #endif // XMLINTERFACE_H
/**
* @file
* @author git blame XMLQtInterface.cpp
* @date Oct 15, 2013
* @brief Base part of implementation of reading XML files using Qt stuff.
*
* @copyright
* Copyright (c) 2013, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/LICENSE.txt
*/
#include "XmlIO/XMLQtInterface.h"
#include <fstream>
#include <QXmlStreamReader>
#include <QXmlSchema>
#include <QXmlSchemaValidator>
#include <QFileInfo>
#include <QCryptographicHash>
// ThirdParty/logog
#include "logog/include/logog.hpp"
namespace FileIO
{
XMLQtInterface::XMLQtInterface(const std::string &schemaFile) :
_schemaName(schemaFile)
{}
int XMLQtInterface::isValid(const QString &fileName) const
{
QXmlSchema schema;
schema.load( QUrl::fromLocalFile((QString::fromStdString(_schemaName))) );
if ( schema.isValid() )
{
QXmlSchemaValidator validator( schema );
if ( validator.validate( QUrl::fromLocalFile((fileName))) )
return 1;
else
{
INFO("XMLQtInterface::isValid(): XML file %s is invalid (in reference to schema %s).",
fileName.data(), _schemaName.c_str());
return 0;
}
}
else
{
INFO("XMLQtInterface::isValid(): Schema %s is invalid.", _schemaName.c_str());
return 0;
}
}
void XMLQtInterface::setSchema(const std::string &schemaName)
{
_schemaName = schemaName;
}
int XMLQtInterface::insertStyleFileDefinition(const QString &fileName) const
{
std::string path = fileName.toStdString();
std::fstream stream(path.c_str());
std::string line;
std::string styleDef("\n<?xml-stylesheet type=\"text/xsl\" href=\"OpenGeoSysGLI.xsl\"?>");
if (!stream.is_open())
{
WARN("XMLQtInterface::insertStyleFileDefinition(): Could not open file %s.",
path.c_str());
return 0;
}
stream.seekp(43 * sizeof(char),std::ios_base::beg); // go to the correct position in the stream
stream.write(styleDef.c_str(), 60 * sizeof(char)); // write new line with xml-stylesheet definition
stream.close();
return 1;
}
bool XMLQtInterface::checkHash(const QString &fileName) const
{
QFileInfo fi(fileName);
QString md5FileName(fileName + ".md5");
std::ifstream md5( md5FileName.toStdString().c_str() );
if (md5.is_open())
{
char* md5HashStr = new char[16];
md5.read(md5HashStr, 16);
QByteArray md5Hash(md5HashStr, 16);
delete[] md5HashStr;
if (isHashGood(fileName, md5Hash))
return true;
}
if (!this->isValid(fileName))
return false;
INFO("File is valid, writing hashfile.");
QByteArray hash = calcHash(fileName);
std::ofstream out( md5FileName.toStdString().c_str(), std::ios::out );
out.write(hash.data(), 16);
out.close();
return true;
}
bool XMLQtInterface::isHashGood(const QString &fileName, const QByteArray &hash) const
{
int hashLength = hash.length();
QByteArray fileHash = calcHash(fileName);
if (fileHash.length() != hashLength)
return false;
for (int i = 0; i < hashLength; i++)
if (fileHash[i] != hash[i])
{
INFO("Hashfile does not match data ... checking file ...");
return false;
}
return true;
}
QByteArray XMLQtInterface::calcHash(const QString &fileName) const
{
std::ifstream is(fileName.toStdString().c_str(), std::ios::binary );
is.seekg (0, std::ios::end);
int length = is.tellg();
is.seekg (0, std::ios::beg);
char* buffer = new char [length];
is.read (buffer,length);
is.close();
QByteArray hash = QCryptographicHash::hash(buffer, QCryptographicHash::Md5);
delete [] buffer;
return hash;
}
} // end namespace FileIO
/**
* @file
* @author git blame XMLQtInterface.h
* @date Oct 15, 2013
* @brief
*
* @copyright
* Copyright (c) 2013, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/LICENSE.txt
*/
#ifndef XMLQTINTERFACE_H_
#define XMLQTINTERFACE_H_
#include <string>
#include <QString>
#include <QByteArray>
class QFile;
class QDomDocument;
class QDomNode;
class QDomElement;
namespace FileIO
{
class XMLQtInterface
{
public:
XMLQtInterface(const std::string &schemaFile);
virtual ~XMLQtInterface() {}
/// As QXMLStreamWriter seems currently unable to include style-file links into xml-files, this method will workaround this issue and include the stylefile link.
int insertStyleFileDefinition(const QString &fileName) const;
/// Check if the given xml-file is valid considering the schema-file used in the constructor
int isValid(const QString &fileName) const;
/// Sets the schema filename used to check if xml files are valid.
void setSchema(const std::string &schemaName);
/// Reads an xml-file.
virtual int readFile(const QString &fileName) = 0;
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.
bool checkHash(const QString &fileName) const;
/// Calculates an MD5 hash of the given file.
QByteArray calcHash(const QString &fileName) const;
/// Checks if the given file is conform to the given hash.
bool isHashGood(const QString &fileName, const QByteArray &hash) const;
std::string _schemaName;
};
} // end namespace FileIO
#endif /* XMLQTINTERFACE_H_ */
...@@ -12,18 +12,21 @@ ...@@ -12,18 +12,21 @@
* *
*/ */
#include "FEMCondition.h"
#include "XmlCndInterface.h" #include "XmlCndInterface.h"
#include <QFile> #include <QFile>
#include <QTextCodec> #include <QTextCodec>
#include <QtXml/QDomDocument> #include <QtXml/QDomDocument>
#include <QStringList> #include <QStringList>
#include "FEMCondition.h"
#include "ProjectData.h"
namespace FileIO namespace FileIO
{ {
XmlCndInterface::XmlCndInterface(ProjectData* project, const std::string &schemaFile) XmlCndInterface::XmlCndInterface(ProjectData* project, const std::string &schemaFile)
: XMLInterface(schemaFile), _type(FEMCondition::UNSPECIFIED), _project(project) : XMLInterface(), XMLQtInterface(schemaFile), _type(FEMCondition::UNSPECIFIED), _project(project)
{ {
} }
......
...@@ -15,19 +15,22 @@ ...@@ -15,19 +15,22 @@
#ifndef XMLCNDINTERFACE_H #ifndef XMLCNDINTERFACE_H
#define XMLCNDINTERFACE_H #define XMLCNDINTERFACE_H
#include "XMLInterface.h"
// ThirdParty/logog // ThirdParty/logog
#include "logog/include/logog.hpp" #include "logog/include/logog.hpp"
// FileIO/XmlIO
#include "XMLInterface.h"
#include "XMLQtInterface.h"
class FEMCondition; class FEMCondition;
class ProjectData;
namespace FileIO namespace FileIO
{ {
/** /**
* \brief Reads and writes FEM Conditions to and from XML files. * \brief Reads and writes FEM Conditions to and from XML files.
*/ */
class XmlCndInterface : public XMLInterface class XmlCndInterface : public XMLInterface, public XMLQtInterface
{ {
public: public:
/** /**
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
namespace FileIO namespace FileIO
{ {
XmlGmlInterface::XmlGmlInterface(GeoLib::GEOObjects& geo_objs, const std::string &schemaFile) : XmlGmlInterface::XmlGmlInterface(GeoLib::GEOObjects& geo_objs, const std::string &schemaFile) :
XMLInterface(schemaFile), _geo_objs(geo_objs) XMLInterface(), XMLQtInterface(schemaFile), _geo_objs(geo_objs)
{ {
} }
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#define XMLGMLINTERFACE_H #define XMLGMLINTERFACE_H
#include "XMLInterface.h" #include "XMLInterface.h"
#include "XMLQtInterface.h"
namespace FileIO namespace FileIO
{ {
...@@ -23,7 +24,7 @@ namespace FileIO ...@@ -23,7 +24,7 @@ namespace FileIO
/** /**
* \brief Reads and writes GeoObjects to and from XML files. * \brief Reads and writes GeoObjects to and from XML files.
*/ */
class XmlGmlInterface : public XMLInterface class XmlGmlInterface : public XMLInterface, public XMLQtInterface
{ {
public: public:
/** /**
...@@ -62,6 +63,7 @@ private: ...@@ -62,6 +63,7 @@ private:
std::map<std::string, std::size_t>* sfc_names ); std::map<std::string, std::size_t>* sfc_names );
GeoLib::GEOObjects& _geo_objs; GeoLib::GEOObjects& _geo_objs;
std::map<std::size_t, std::size_t> _idx_map;
}; };
} }
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
namespace FileIO namespace FileIO
{ {
XmlGspInterface::XmlGspInterface(ProjectData* project, const std::string &schemaFile) : XmlGspInterface::XmlGspInterface(ProjectData* project, const std::string &schemaFile) :
XMLInterface(schemaFile), _project(project) XMLInterface(), XMLQtInterface(schemaFile), _project(project)
{ {
} }
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#define XMLGSPINTERFACE_H #define XMLGSPINTERFACE_H
#include "XMLInterface.h" #include "XMLInterface.h"
#include "XMLQtInterface.h"
namespace FileIO namespace FileIO
{ {
...@@ -23,7 +24,7 @@ namespace FileIO ...@@ -23,7 +24,7 @@ namespace FileIO
/** /**
* \brief Reads and writes project information to and from XML files. * \brief Reads and writes project information to and from XML files.
*/ */
class XmlGspInterface : public XMLInterface class XmlGspInterface : public XMLInterface, public XMLQtInterface
{ {
public: public:
/** /**
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
namespace FileIO namespace FileIO
{ {
XmlStnInterface::XmlStnInterface(GeoLib::GEOObjects& geo_objs, const std::string &schemaFile) : XmlStnInterface::XmlStnInterface(GeoLib::GEOObjects& geo_objs, const std::string &schemaFile) :
XMLInterface(schemaFile), _geo_objs(geo_objs) XMLInterface(), XMLQtInterface(schemaFile), _geo_objs(geo_objs)
{ {
} }
......
...@@ -15,10 +15,11 @@ ...@@ -15,10 +15,11 @@
#ifndef XMLSTNINTERFACE_H #ifndef XMLSTNINTERFACE_H
#define XMLSTNINTERFACE_H #define XMLSTNINTERFACE_H
#include "XMLInterface.h"
#include "RapidXML/rapidxml.hpp" #include "RapidXML/rapidxml.hpp"
#include "XMLInterface.h"
#include "XMLQtInterface.h"
namespace GeoLib { namespace GeoLib {
class StationBorehole; class StationBorehole;
} }
...@@ -29,7 +30,7 @@ namespace FileIO ...@@ -29,7 +30,7 @@ namespace FileIO
/** /**
* \brief Reads and writes Observation Sites to and from XML files. * \brief Reads and writes Observation Sites to and from XML files.
*/ */
class XmlStnInterface : public XMLInterface class XmlStnInterface : public XMLInterface, public XMLQtInterface
{ {
public: public:
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment