source: XmlTools2/trunk/utilxmltools.cpp@ 922

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

more fixes and updated examples

File size: 5.5 KB
RevLine 
[906]1#include "utilxmltools.h"
2
3namespace UtilXmlTools{
4
5QStringList getAllXmlFilesByWildcard(const QString &wildcard){
6 QStringList validFilesMatching;
7 QStringList filesMatching;
8
9 // Get all files matching the wildcard
10
11 filesMatching=Util::getAllFilesByWildcard(wildcard);
12
13 // Check if all are XmlFiles, only return valid XmlFiles
14
15 for(int i=0; i<filesMatching.size(); i++){
16 if(filesMatching[i].endsWith(".xml",Qt::CaseInsensitive)){
17 validFilesMatching << filesMatching[i];
18 }
19 }
20
21 return validFilesMatching;
22
23}
24
25QStringList getAllPatchFilesByWildcard(const QString &wildcard){
26 QStringList validFilesMatching;
27 QStringList filesMatching;
28
29 // Get all files matching the wildcard
30
31 filesMatching=Util::getAllFilesByWildcard(wildcard);
32
33 // Check if all are PatchFiles, only return valid PatchFiles
34
35 for(int i=0; i<filesMatching.size(); i++){
36 if(filesMatching[i].endsWith(".patch",Qt::CaseInsensitive) || filesMatching[i].endsWith(".oni-patch",Qt::CaseInsensitive)){
37 validFilesMatching << filesMatching[i];
38 }
39 }
40
41 return validFilesMatching;
42
43}
44
45void backupFile(const QString &file){
46 if(!QFile::exists(file+".bak")){
47 if(!Util::backupFile(file)){
48 std::cerr << "Couldn't backup file '" << file.toLatin1().constData() << "'. Aborting." << std::endl;
49 exit(1);
50 }
51 }
52 else{
53 std::cout << "Backup file '" << file.toLatin1().constData() << "'' already exists. Skipping..." << std::endl;
54 }
55}
56
[920]57void getAllXpathElements(const QString &xPathExpression, pugi::xml_document &doc, QList<pugi::xml_node> &result){
58
59 pugi::xpath_node_set selectedNodes;
60 pugi::xpath_node node;
61
62 try
63 {
64 selectedNodes = doc.select_nodes(xPathExpression.toLatin1().constData());
65 }
66
67 catch (const pugi::xpath_exception& e)
68 {
69 displayErrorMessage("XPath element selection","Selection of elements using the XPathExpression: '" + xPathExpression + "' failed:\n" + e.what());
70 }
71
72 for (pugi::xpath_node_set::const_iterator currNode = selectedNodes.begin(); currNode != selectedNodes.end(); ++currNode)
73 {
74 node = *currNode;
75 if(node){ // if node != null
76 result << node.node();
77 }
78 }
79
80 if(result.isEmpty()){
81 result << pugi::xml_node(); // add an empty node if none found
82 }
83
84}
85
86pugi::xml_node getFirstXpathElement(const QString &xPathExpression, pugi::xml_document &doc){
87 pugi::xpath_node selectedNode;
88
89 try
90 {
91 selectedNode = doc.select_single_node(xPathExpression.toLatin1().constData());
92 }
93
94 catch (const pugi::xpath_exception& e)
95 {
96 displayErrorMessage("XPath element selection","Selection of element using the XPathExpression: '" + xPathExpression + "' failed:\n" + e.what());
97 }
98
99 return selectedNode.node();
100}
101
[906]102void getAllNamedElements(pugi::xml_node &node, QList<pugi::xml_node> &result, XmlFilter &filters){
103 for (pugi::xml_node_iterator currNode = node.begin(); currNode != node.end(); ++currNode)
104 {
105
106 if ((*currNode).name() == filters.getElementName() && (filters.getParentElementName() == "" || filters.getParentElementName() == (*currNode).parent().name())
107 && (filters.getAttributeName() == "" ||
108 Util::toQString((*currNode).attribute(filters.getAttributeName().toLatin1().constData()).value()) == filters.getAttributeValue()) ){ // Seems node attribute must be converted to qtsring to remove \r\n needs to check in future!
109
110 result << *currNode;
111 continue;
112 }
113 getAllNamedElements(*currNode,result,filters);
114 }
115}
116
[920]117pugi::xml_node getFirstNamedElement(pugi::xml_node &node, XmlFilter &filters){
[906]118
119 pugi::xml_node foundNode;
120
121 for (pugi::xml_node_iterator currNode = node.begin(); currNode != node.end(); ++currNode)
122 {
123 if ((*currNode).name() == filters.getElementName() && (filters.getParentElementName() == "" || filters.getParentElementName() == (*currNode).parent().name())
124 && (filters.getAttributeName() == "" ||
125 Util::toQString((*currNode).attribute(filters.getAttributeName().toLatin1().constData()).value()) == filters.getAttributeValue()) ){
126 return *currNode;
127 }
128
[920]129 foundNode=getFirstNamedElement(*currNode,filters);
[906]130
131 if(foundNode.type()!=pugi::node_null){
132 return foundNode;
133 }
134
135 }
136
137 return foundNode;
138}
139
140void displaySuccessMessage(const int numberOperations, const QString &operation){
141 std::cout << "------------------------------------------------------------------------" << std::endl;
142 std::cout << numberOperations << " " << operation.toLatin1().constData() << " operation(s) completed with success!" << std::endl;
143 std::cout << "------------------------------------------------------------------------" << std::endl;
144}
145
146void displayErrorMessage(const QString& operation, const QString &message, bool exitProgram){
147 std::cerr << "************************************************************************" << std::endl;
148 std::cerr << operation.toLatin1().constData() << " operation failed!" << std::endl << std::endl;
149 std::cerr << message.toLatin1().constData() << std::endl << std::endl;
150 if(exitProgram) std::cerr << "Aborting..." << std::endl;
151 std::cerr << "************************************************************************" << std::endl;
152 if(exitProgram) exit(1);
153}
154
155}
Note: See TracBrowser for help on using the repository browser.