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