source: XmlTools2/trunk/utilxmltools.cpp@ 1009

Last change on this file since 1009 was 964, checked in by s10k, 11 years ago

XmlTools
More speedup, about 3 times faster now than the current AEI build

File size: 5.7 KB
RevLine 
[906]1#include "utilxmltools.h"
2
3namespace UtilXmlTools{
4
[964]5// As this will not likely be modified we use QVector instead of QList since it is more cache friendly
6QVector<QString> getAllXmlFilesByWildcard(const QString &wildcard){
[906]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
[964]22 return validFilesMatching.toVector();
[906]23
24}
25
[964]26QVector<QString> getAllPatchFilesByWildcard(const QString &wildcard){
[906]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
[964]42 return validFilesMatching.toVector();
[906]43
44}
45
[923]46void backupFile(const QString &file, bool verboseEnabled){
[906]47 if(!QFile::exists(file+".bak")){
48 if(!Util::backupFile(file)){
[955]49 std::cerr << "Couldn't back up file '" << file.toUtf8().constData() << "'. Aborting." << std::endl;
[906]50 exit(1);
51 }
52 }
53 else{
[923]54 if(verboseEnabled){
[935]55 std::cout << "Backup file '" << file.toUtf8().constData() << "'' already exists. Skipping..." << std::endl;
[923]56 }
[906]57 }
58}
59
[920]60void 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 {
[935]67 selectedNodes = doc.select_nodes(xPathExpression.toUtf8().constData());
[920]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
89pugi::xml_node getFirstXpathElement(const QString &xPathExpression, pugi::xml_document &doc){
90 pugi::xpath_node selectedNode;
91
92 try
93 {
[935]94 selectedNode = doc.select_single_node(xPathExpression.toUtf8().constData());
[920]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
[906]105void 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() == "" ||
[935]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!
[906]112
113 result << *currNode;
114 continue;
115 }
116 getAllNamedElements(*currNode,result,filters);
117 }
118}
119
[920]120pugi::xml_node getFirstNamedElement(pugi::xml_node &node, XmlFilter &filters){
[906]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() == "" ||
[935]128 Util::toQString((*currNode).attribute(filters.getAttributeName().toUtf8().constData()).value()) == filters.getAttributeValue()) ){
[906]129 return *currNode;
130 }
131
[920]132 foundNode=getFirstNamedElement(*currNode,filters);
[906]133
134 if(foundNode.type()!=pugi::node_null){
135 return foundNode;
136 }
137
138 }
139
140 return foundNode;
141}
142
143void displaySuccessMessage(const int numberOperations, const QString &operation){
144 std::cout << "------------------------------------------------------------------------" << std::endl;
[935]145 std::cout << numberOperations << " " << operation.toUtf8().constData() << " operation(s) completed with success!" << std::endl;
[906]146 std::cout << "------------------------------------------------------------------------" << std::endl;
147}
148
149void displayErrorMessage(const QString& operation, const QString &message, bool exitProgram){
150 std::cerr << "************************************************************************" << std::endl;
[935]151 std::cerr << operation.toUtf8().constData() << " operation failed!" << std::endl << std::endl;
152 std::cerr << message.toUtf8().constData() << std::endl << std::endl;
[906]153 if(exitProgram) std::cerr << "Aborting..." << std::endl;
154 std::cerr << "************************************************************************" << std::endl;
155 if(exitProgram) exit(1);
156}
157
158}
Note: See TracBrowser for help on using the repository browser.