[906] | 1 | #ifndef UTILXMLTOOLS_H
|
---|
| 2 | #define UTILXMLTOOLS_H
|
---|
| 3 |
|
---|
| 4 | // Utilities functions specific used for Xml Tools (not general functions)
|
---|
| 5 |
|
---|
| 6 | #include "util.h"
|
---|
| 7 | #include "xmlfilter.h"
|
---|
| 8 | #include "libs/pugixml.hpp"
|
---|
| 9 |
|
---|
| 10 | namespace UtilXmlTools{
|
---|
| 11 |
|
---|
| 12 | QStringList getAllXmlFilesByWildcard(const QString &wildcard);
|
---|
| 13 | QStringList getAllPatchFilesByWildcard(const QString &wildcard);
|
---|
[923] | 14 | void backupFile(const QString &file, bool verboseEnabled);
|
---|
[906] | 15 | void getAllNamedElements(pugi::xml_node &node, QList<pugi::xml_node> &result, XmlFilter &filters);
|
---|
[920] | 16 | void getAllXpathElements(const QString &xPathExpression, pugi::xml_document &doc, QList<pugi::xml_node> &result);
|
---|
[906] | 17 | void displaySuccessMessage(const int numberOperations, const QString &operation);
|
---|
| 18 | void displayErrorMessage(const QString& operation, const QString &message, bool exitProgram=true);
|
---|
[920] | 19 | pugi::xml_node getFirstNamedElement(pugi::xml_node &node, XmlFilter &filters);
|
---|
| 20 | pugi::xml_node getFirstXpathElement(const QString &xPathExpression, pugi::xml_document &doc);
|
---|
[906] | 21 |
|
---|
| 22 | //// inline functions
|
---|
| 23 |
|
---|
[923] | 24 | inline void loadXmlFile(const QString &file, pugi::xml_document &document, pugi::xml_node &rootNode, bool backupsEnabled, bool verboseEnabled, const QString &operationForErrorMessage){
|
---|
[906] | 25 |
|
---|
[935] | 26 | pugi::xml_parse_result result = document.load_file(file.toUtf8().constData());
|
---|
[906] | 27 | rootNode=document.root();
|
---|
| 28 |
|
---|
| 29 | if(result.status==pugi::status_ok){
|
---|
[923] | 30 | if(verboseEnabled){
|
---|
[935] | 31 | std::cout << "File '" << file.toUtf8().constData() << "' loaded with sucess." << std::endl;
|
---|
[923] | 32 | }
|
---|
[906] | 33 | }
|
---|
| 34 | else{
|
---|
| 35 | UtilXmlTools::displayErrorMessage(operationForErrorMessage,"An error ocurred while loading '" +file + "' XML file\n" + result.description());
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | if(backupsEnabled){
|
---|
[923] | 39 | UtilXmlTools::backupFile(file,verboseEnabled); // bake a backup of the file.
|
---|
[906] | 40 | }
|
---|
| 41 |
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | inline void saveXmlFile(const QString &file, pugi::xml_document &document, const QString &operationForErrorMessage){
|
---|
[935] | 45 | if(!document.save_file(file.toUtf8().constData(), "\t", pugi::format_indent | pugi::format_save_file_text | pugi::format_no_escapes)){ // output as the system new lines ending
|
---|
[906] | 46 | UtilXmlTools::displayErrorMessage(operationForErrorMessage,"An error ocurred while saving '" + file + "' XML file");
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | //inline void checkIfValidXmlFile(const QString &file){
|
---|
| 51 | // if(!file.endsWith(".xml",Qt::CaseInsensitive)){
|
---|
| 52 | // std::cout << "Tried to load a non xml file: '" << Util::toCstr(file) << "'. XmlTools only work over XmlFiles." << std::endl;
|
---|
| 53 | // exit(1);
|
---|
| 54 | // }
|
---|
| 55 | //}
|
---|
| 56 |
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | #endif // UTILXMLTOOLS_H
|
---|