source: XmlTools2/trunk/xmltools.cpp@ 1005

Last change on this file since 1005 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: 13.0 KB
Line 
1#include "xmltools.h"
2
3// Filters constructor
4XmlTools::XmlTools(QString filesWildcard, XmlFilter filter, bool noBackups, bool noVerbose)
5{
6 this->filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard);
7 this->filters=filter;
8 this->backupsEnabled=!noBackups;
9 this->verboseEnabled=!noVerbose;
10
11 if(this->filesToProcess.isEmpty()){
12 UtilXmlTools::displayErrorMessage("Loading xml files","No XML files were found for the wildcard: "+filesWildcard);
13 }
14}
15
16// XPath constructor
17XmlTools::XmlTools(QString filesWildcard, QString xPathExpression, bool noBackups, bool noVerbose)
18{
19 this->filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard);
20 this->xPathExpression=xPathExpression;
21 this->backupsEnabled=!noBackups;
22 this->verboseEnabled=!noVerbose;
23}
24
25// Adds new values to an element
26void XmlTools::addValues(QString newValues){
27
28 // Process all XmlFiles
29 for(int i=0; i<this->filesToProcess.size(); i++){
30
31 QStringList newValuesList, currValuesList;
32 QList<pugi::xml_node> elements;
33
34 UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled,this->verboseEnabled,"add-values");
35
36 newValuesList=Util::qStringListFromSpacedString(newValues);
37
38 // Check how the elements will be fetched via element name or xpath expression
39 if(this->xPathExpression==""){
40 UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters);
41 }
42 else{
43 UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements);
44 }
45
46 for(int j=0; j<elements.size(); j++){
47
48 currValuesList=Util::qStringListFromSpacedString(Util::toQString(elements[j].text().as_string())); // convert each element in a list (uses space as separator)
49
50 for(int k=0; k<newValuesList.size(); k++){
51 if(currValuesList.contains(newValuesList[k])){ // If the current element already contains this value proceed to the next
52 continue;
53 }
54
55 elements[j].text()=QString(Util::toQString(elements[j].text().as_string()) + " " + newValuesList[k]).toUtf8().constData(); // If it doesn't exists yet let's add it
56 }
57 }
58
59 UtilXmlTools::saveXmlFile(this->filesToProcess[i],this->document,"add-values");
60 }
61
62 UtilXmlTools::displaySuccessMessage(this->filesToProcess.size(), "add-values");
63
64}
65
66void XmlTools::removeValues(QString valuesToRemove){
67
68 // Process all XmlFiles
69 for(int i=0; i<this->filesToProcess.size(); i++){
70
71 QList<pugi::xml_node> elements;
72 QStringList valuesToRemoveList, currValuesList;
73 bool elementChanged=false;
74
75 UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, this->verboseEnabled, "remove-values");
76
77 // Check how the elements will be fetched via element name or xpath expression
78 if(this->xPathExpression==""){
79 UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters);
80 }
81 else{
82 UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements);
83 }
84
85 valuesToRemoveList=Util::qStringListFromSpacedString(valuesToRemove);
86
87 for(int j=0; j<elements.size(); j++){ // O(3)... Optimization may be necessary.
88 currValuesList=Util::qStringListFromSpacedString(Util::toQString(elements[j].text().as_string())); // convert each element in a list (uses space as separator)
89
90 for(int k=0; k<currValuesList.size(); k++){
91 for(int m=0; m<valuesToRemoveList.size(); m++){
92 if(currValuesList[k]==valuesToRemoveList[m]){
93 currValuesList[k]=""; // flag to deletion
94 elementChanged=true;
95 }
96 }
97 }
98
99 if(elementChanged){ // If curr element changed update the XML
100 currValuesList.removeAll(""); // remove all files flagged to deletion
101 elements[j].text()=currValuesList.join(' ').toUtf8().constData();
102 elementChanged=false;
103 }
104 }
105
106 UtilXmlTools::saveXmlFile(this->filesToProcess[i],this->document, "remove-values");
107 }
108
109
110 UtilXmlTools::displaySuccessMessage(this->filesToProcess.size(),"remove-values");
111}
112
113void XmlTools::replaceValue(QString oldValue, QString newValue){
114
115 // Process all XmlFiles
116 for(int i=0; i<this->filesToProcess.size(); i++){
117
118 QList<pugi::xml_node> elements;
119 QStringList currValuesList;
120 bool elementChanged=false;
121
122 UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, this->verboseEnabled, "replace-value");
123
124 // Check how the elements will be fetched via element name or xpath expression
125 if(this->xPathExpression==""){
126 UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters);
127 }
128 else{
129 UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements);
130 }
131
132 for(int j=0; j<elements.size(); j++){
133 currValuesList=Util::qStringListFromSpacedString(Util::toQString(elements[j].text().as_string())); // convert each element in a list (uses space as separator)
134
135 for(int k=0; k<currValuesList.size(); k++){
136 if(currValuesList[k]==oldValue){ // Found a match with the old value?
137 currValuesList[k]=newValue; // If found replace it with the new value
138 elementChanged=true;
139 }
140 }
141
142 if(elementChanged){ // If curr element changed update the XML
143 elements[j].text()=currValuesList.join(" ").toUtf8().constData();
144 }
145 elementChanged=false;
146 }
147
148 UtilXmlTools::saveXmlFile(this->filesToProcess[i],this->document, "replace-value");
149 }
150
151 UtilXmlTools::displaySuccessMessage(this->filesToProcess.size(), "replace-value");
152}
153
154// Replaces all current values of an element by another (can be specified only specific positions)
155void XmlTools::replaceAll(QString value, QString valuePositions){
156
157 // Process all XmlFiles
158 for(int i=0; i<this->filesToProcess.size(); i++){
159
160 QList<pugi::xml_node> elements;
161
162 UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, this->verboseEnabled, "replace-all");
163
164 // Check how the elements will be fetched via element name or xpath expression
165 if(this->xPathExpression==""){
166 UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters);
167 }
168 else{
169 UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements);
170 }
171
172
173 // Let's start the override
174 for(int j=0; j<elements.size(); j++){
175 if(valuePositions!=""){
176 elements[j].text()=replaceSpecificPositions(value, Util::toQString(elements[j].text().as_string()),valuePositions).toUtf8().constData();
177 }
178 else{
179 elements[j].text()=value.toUtf8().constData();
180 }
181 }
182
183 UtilXmlTools::saveXmlFile(this->filesToProcess[i],this->document, "replace-all");
184 }
185
186 UtilXmlTools::displaySuccessMessage(this->filesToProcess.size(), "replace-all");
187}
188
189// Update a set of XML elements values based in the difference between the old and new value
190// This can be used in multiple files if the difference between one file and other are the same e.g. increment to all current object positions 100 in y (A difference of -100).
191// E.g. oldValue=5 , newValue=7; diffBetweenOldAndNewValue=-2
192void XmlTools::updateElements(QString diffBetweenOldAndNewValue){
193
194 // Process all XmlFiles
195 for(int i=0; i<this->filesToProcess.size(); i++){
196
197 QList<pugi::xml_node> elements;
198 MultiDimVar lastXmlValue(0); // inicialize with any value or dimension
199 MultiDimVar currXmlValue(0);
200 MultiDimVar newXmlValue(0); // value that will update currValue
201
202 UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled,this->verboseEnabled, "update-elements");
203
204 // Check how the elements will be fetched via element name or xpath expression
205 if(this->xPathExpression==""){
206 UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters);
207 }
208 else{
209 UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements);
210 }
211
212
213 if(elements.size()>1){
214 lastXmlValue=MultiDimVar(Util::toQString(elements[0].text().as_string())); // the lastXmlValue will begin to be the first one of the node
215 currXmlValue=MultiDimVar(Util::toQString(elements[1].text().as_string())); // the currXmlValue will begin to be the second one of the node
216 newXmlValue=MultiDimVar::sub(lastXmlValue, MultiDimVar(diffBetweenOldAndNewValue));
217 elements[0].text() = newXmlValue.toString().toUtf8().constData(); // update the first eblement with the new one already
218 }
219
220 // Let's start the node update
221 for(int j=1; j<elements.size()-1; j++){ // We start in 1 because the 0 is already saved in lastXmlValue // -1 because we will also work with the next one in the current
222
223 newXmlValue=MultiDimVar::sum(newXmlValue,MultiDimVar::sub(currXmlValue,lastXmlValue));
224 elements[j].text() = newXmlValue.toString().toUtf8().constData(); // update element with the new value
225 lastXmlValue=currXmlValue;
226 currXmlValue=MultiDimVar(Util::toQString(elements[j+1].text().as_string()));
227
228 }
229
230 // To update too last element (avoid out of bound because i+1)
231 newXmlValue=MultiDimVar::sum(newXmlValue,MultiDimVar::sub(currXmlValue,lastXmlValue));
232 elements[elements.size()-1].text() = newXmlValue.toString().toUtf8().constData(); // update element with the new value
233
234 UtilXmlTools::saveXmlFile(this->filesToProcess[i],this->document, "update-elements");
235 }
236
237 UtilXmlTools::displaySuccessMessage(this->filesToProcess.size(),"updateNode");
238
239}
240
241// Invert a set of XML elements with specified name (and optionally a parent name)
242void XmlTools::invertElements(){
243
244 // Process all XmlFiles
245 for(int i=0; i<this->filesToProcess.size(); i++){
246 UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, this->verboseEnabled, "invert-elements");
247
248 QList<pugi::xml_node> elements;
249 QStringList invertedElements; //Inverting the element order
250
251 // Check how the elements will be fetched via element name or xpath expression
252 if(this->xPathExpression==""){
253 UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters);
254 }
255 else{
256 UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements);
257 }
258
259 // Read all elements and save to the list
260 for(int j=elements.size()-1; j>=0; j--){
261 invertedElements << Util::toQString(elements[j].text().as_string());
262 }
263
264 // Override the tree with the inverted order
265 for(int j=0; j<elements.size(); j++){
266 elements[j].text()= invertedElements[j].toUtf8().constData();
267 }
268
269 UtilXmlTools::saveXmlFile(this->filesToProcess[i],this->document, "invert-elements");
270 }
271
272 UtilXmlTools::displaySuccessMessage(this->filesToProcess.size(),"invert-elements");
273
274}
275
276// Replaces specific values (given the position) for a new value
277// [ currValues / positionsToReplaced are strings with composed by another strings space separated ]
278// Returns a new string (space separated) will values replaced
279QString XmlTools::replaceSpecificPositions(const QString &newValue, const QString &currValues, const QString &positionsToReplace){
280
281 QList<int> positionsToReplaceList;
282 QStringList currValuesList;
283
284 positionsToReplaceList=Util::qListIntFromSpacedString(positionsToReplace);
285 currValuesList=Util::qStringListFromSpacedString(currValues);
286
287 // Make some validation before the replacing
288 if(currValuesList.size()<positionsToReplaceList.size()){
289 UtilXmlTools::displayErrorMessage("replaceSpecificPositions","There are more positions to replace than the current xml values.");
290 }
291
292 foreach(int pos, positionsToReplaceList){
293 if(pos>currValuesList.size()-1 || pos < 0){ //Are positions valid for the current values? //-1 because starts at 0
294 UtilXmlTools::displayErrorMessage("replaceSpecificPositions","One or more of the specified positions to replace are out of range.");
295 }
296 }
297 //
298
299 // Finally replace the specified values
300 foreach(int pos, positionsToReplaceList){
301 currValuesList[pos]=newValue;
302 }
303
304 return currValuesList.join(" ");
305
306}
Note: See TracBrowser for help on using the repository browser.