source: XmlTools2/trunk/xmltools.cpp@ 917

Last change on this file since 917 was 910, checked in by s10k, 11 years ago

some more fixes

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