source: XmlTools2/trunk/utilxmltools.cpp@ 952

Last change on this file since 952 was 935, checked in by s10k, 11 years ago

XmlTools
Oni's Mac folder use a special character (U+0192), because of this I had to change all encoding transformations to UTF8

File size: 5.6 KB
Line 
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, bool verboseEnabled){
46 if(!QFile::exists(file+".bak")){
47 if(!Util::backupFile(file)){
48 std::cerr << "Couldn't backup file '" << file.toUtf8().constData() << "'. Aborting." << std::endl;
49 exit(1);
50 }
51 }
52 else{
53 if(verboseEnabled){
54 std::cout << "Backup file '" << file.toUtf8().constData() << "'' already exists. Skipping..." << std::endl;
55 }
56 }
57}
58
59void getAllXpathElements(const QString &xPathExpression, pugi::xml_document &doc, QList<pugi::xml_node> &result){
60
61 pugi::xpath_node_set selectedNodes;
62 pugi::xpath_node node;
63
64 try
65 {
66 selectedNodes = doc.select_nodes(xPathExpression.toUtf8().constData());
67 }
68
69 catch (const pugi::xpath_exception& e)
70 {
71 displayErrorMessage("XPath element selection","Selection of elements using the XPathExpression: '" + xPathExpression + "' failed:\n" + e.what());
72 }
73
74 for (pugi::xpath_node_set::const_iterator currNode = selectedNodes.begin(); currNode != selectedNodes.end(); ++currNode)
75 {
76 node = *currNode;
77 if(node){ // if node != null
78 result << node.node();
79 }
80 }
81
82 if(result.isEmpty()){
83 result << pugi::xml_node(); // add an empty node if none found
84 }
85
86}
87
88pugi::xml_node getFirstXpathElement(const QString &xPathExpression, pugi::xml_document &doc){
89 pugi::xpath_node selectedNode;
90
91 try
92 {
93 selectedNode = doc.select_single_node(xPathExpression.toUtf8().constData());
94 }
95
96 catch (const pugi::xpath_exception& e)
97 {
98 displayErrorMessage("XPath element selection","Selection of element using the XPathExpression: '" + xPathExpression + "' failed:\n" + e.what());
99 }
100
101 return selectedNode.node();
102}
103
104void getAllNamedElements(pugi::xml_node &node, QList<pugi::xml_node> &result, XmlFilter &filters){
105 for (pugi::xml_node_iterator currNode = node.begin(); currNode != node.end(); ++currNode)
106 {
107
108 if ((*currNode).name() == filters.getElementName() && (filters.getParentElementName() == "" || filters.getParentElementName() == (*currNode).parent().name())
109 && (filters.getAttributeName() == "" ||
110 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!
111
112 result << *currNode;
113 continue;
114 }
115 getAllNamedElements(*currNode,result,filters);
116 }
117}
118
119pugi::xml_node getFirstNamedElement(pugi::xml_node &node, XmlFilter &filters){
120
121 pugi::xml_node foundNode;
122
123 for (pugi::xml_node_iterator currNode = node.begin(); currNode != node.end(); ++currNode)
124 {
125 if ((*currNode).name() == filters.getElementName() && (filters.getParentElementName() == "" || filters.getParentElementName() == (*currNode).parent().name())
126 && (filters.getAttributeName() == "" ||
127 Util::toQString((*currNode).attribute(filters.getAttributeName().toUtf8().constData()).value()) == filters.getAttributeValue()) ){
128 return *currNode;
129 }
130
131 foundNode=getFirstNamedElement(*currNode,filters);
132
133 if(foundNode.type()!=pugi::node_null){
134 return foundNode;
135 }
136
137 }
138
139 return foundNode;
140}
141
142void displaySuccessMessage(const int numberOperations, const QString &operation){
143 std::cout << "------------------------------------------------------------------------" << std::endl;
144 std::cout << numberOperations << " " << operation.toUtf8().constData() << " operation(s) completed with success!" << std::endl;
145 std::cout << "------------------------------------------------------------------------" << std::endl;
146}
147
148void displayErrorMessage(const QString& operation, const QString &message, bool exitProgram){
149 std::cerr << "************************************************************************" << std::endl;
150 std::cerr << operation.toUtf8().constData() << " operation failed!" << std::endl << std::endl;
151 std::cerr << message.toUtf8().constData() << std::endl << std::endl;
152 if(exitProgram) std::cerr << "Aborting..." << std::endl;
153 std::cerr << "************************************************************************" << std::endl;
154 if(exitProgram) exit(1);
155}
156
157}
Note: See TracBrowser for help on using the repository browser.