source: XmlTools2/trunk/utilxmltools.h@ 922

Last change on this file since 922 was 920, checked in by s10k, 11 years ago

more fixes and updated examples

File size: 2.4 KB
Line 
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
10namespace UtilXmlTools{
11
12QStringList getAllXmlFilesByWildcard(const QString &wildcard);
13QStringList getAllPatchFilesByWildcard(const QString &wildcard);
14void backupFile(const QString &file);
15void getAllNamedElements(pugi::xml_node &node, QList<pugi::xml_node> &result, XmlFilter &filters);
16void getAllXpathElements(const QString &xPathExpression, pugi::xml_document &doc, QList<pugi::xml_node> &result);
17void displaySuccessMessage(const int numberOperations, const QString &operation);
18void displayErrorMessage(const QString& operation, const QString &message, bool exitProgram=true);
19pugi::xml_node getFirstNamedElement(pugi::xml_node &node, XmlFilter &filters);
20pugi::xml_node getFirstXpathElement(const QString &xPathExpression, pugi::xml_document &doc);
21
22//// inline functions
23
24inline void loadXmlFile(const QString &file, pugi::xml_document &document, pugi::xml_node &rootNode, bool backupsEnabled, const QString &operationForErrorMessage){
25
26 pugi::xml_parse_result result = document.load_file(file.toLatin1().constData());
27 rootNode=document.root();
28
29 if(result.status==pugi::status_ok){
30 std::cout << "File '" << file.toLatin1().constData() << "' loaded with sucess." << std::endl;
31 }
32 else{
33 UtilXmlTools::displayErrorMessage(operationForErrorMessage,"An error ocurred while loading '" +file + "' XML file\n" + result.description());
34 }
35
36 if(backupsEnabled){
37 UtilXmlTools::backupFile(file); // bake a backup of the file.
38 }
39
40}
41
42inline void saveXmlFile(const QString &file, pugi::xml_document &document, const QString &operationForErrorMessage){
43 if(!document.save_file(file.toLatin1().constData(), "\t", pugi::format_indent | pugi::format_save_file_text | pugi::format_no_escapes)){ // output as the system new lines ending
44 UtilXmlTools::displayErrorMessage(operationForErrorMessage,"An error ocurred while saving '" + file + "' XML file");
45 }
46}
47
48//inline void checkIfValidXmlFile(const QString &file){
49// if(!file.endsWith(".xml",Qt::CaseInsensitive)){
50// std::cout << "Tried to load a non xml file: '" << Util::toCstr(file) << "'. XmlTools only work over XmlFiles." << std::endl;
51// exit(1);
52// }
53//}
54
55}
56
57#endif // UTILXMLTOOLS_H
Note: See TracBrowser for help on using the repository browser.