source: s10k/XmlTools/xmlpatch.cpp@ 1113

Last change on this file since 1113 was 1111, checked in by s10k, 6 years ago

Added version 2.0e (bugfix in patch file for paths with apostrophes)

File size: 18.3 KB
Line 
1#include "xmlpatch.h"
2
3XmlPatch::XmlPatch(QString patchFilesWildcard, QString forceTargetFilesWildcard, bool noBackups, bool noVerbose)
4{
5 this->patchFilesToProcess=UtilXmlTools::getAllPatchFilesByWildcard(patchFilesWildcard);
6 this->forceTargetFilesWildcard=forceTargetFilesWildcard;
7 this->backupsEnabled=!noBackups;
8 this->verboseEnabled=!noVerbose;
9
10 if(forceTargetFilesWildcard!=""){
11 std::cout << "User forced patch in the target file(s): " << forceTargetFilesWildcard.toUtf8().constData() << std::endl;
12 }
13
14 if(this->patchFilesToProcess.isEmpty()){
15 UtilXmlTools::displayErrorMessage("Loading patch files","No .patch or .oni-patch files were found for the wildcard: "+patchFilesWildcard);
16 }
17
18}
19
20void XmlPatch::readAndProcessPatchFile(){
21
22 // Process all PatchFiles
23 for(int i=0; i<this->patchFilesToProcess.size(); i++){
24
25 QFile inputFile(this->patchFilesToProcess[i]);
26
27 if (inputFile.open(QIODevice::ReadOnly))
28 {
29
30 QTextStream fileStream(&inputFile);
31
32 checkPatchVersion(this->patchFilesToProcess[i], fileStream);
33 checkAndProcessValidCommands(fileStream);
34
35 inputFile.close();
36 }
37 else{
38 UtilXmlTools::displayErrorMessage("Read patch file", "Error opening patch file: '" + this->patchFilesToProcess[i] + "'.\n" + inputFile.errorString());
39 }
40
41 }
42
43 UtilXmlTools::displaySuccessMessage(this->patchFilesToProcess.size(),"Patch File(s)");
44
45}
46
47void XmlPatch::insertNodesOperation(const QString &xmlString, XmlFilter &filters, const QString &xPathExpression, const QString &filesWildcard){
48
49 QVector<QString> filesToProcess;
50 QList<pugi::xml_node> nodesToInsertion;
51 pugi::xml_document newNode;
52 pugi::xml_parse_result result;
53
54 try{
55
56 filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard);
57
58 if(filesToProcess.isEmpty()){
59 UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES","No XML files were found for the wildcard: "+filesWildcard);
60 }
61
62 result=newNode.load(xmlString.toUtf8().constData()); // load xml to insert
63
64 if(result.status!=pugi::status_ok){
65 UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES", "The xml node to insert is invalid.\n" + QString(result.description()));
66 }
67
68 // Process all XmlFiles
69 for(int i=0; i<filesToProcess.size(); i++){
70
71 UtilXmlTools::loadXmlFile(filesToProcess[i],this->document,this->rootNode,this->backupsEnabled,this->verboseEnabled,"@ADD_INSIDE_NODES");
72
73 // Check how the element will be fetched via element name or xpath expression
74 if(xPathExpression.isEmpty()){
75 UtilXmlTools::getAllNamedElements(this->rootNode,nodesToInsertion,filters);
76 }
77 else{
78 UtilXmlTools::getAllXpathElements(xPathExpression,this->document,nodesToInsertion);
79 }
80
81 if(nodesToInsertion.isEmpty() || nodesToInsertion[0].type()==pugi::node_null){
82
83 QString errMessage;
84
85 if(xPathExpression.isEmpty()){
86 errMessage = "No node was found with an ElementName: '" + filters.getElementName() + "'";
87 if(filters.getParentElementName()!=""){
88 errMessage += " and a ParentElementName: '" + filters.getParentElementName() + "'";
89 }
90 if(filters.getAttributeName()!=""){
91 errMessage += " and an AttributeName: '" + filters.getAttributeName() + "' and an AttributeValue: '" + filters.getAttributeValue() + "'";
92 }
93 }
94 else{
95 errMessage = "No node was found with an XPathExpression: '" + xPathExpression + "'";
96 }
97
98 UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES",errMessage);
99 }
100
101 for(int j=0; j<nodesToInsertion.size(); j++){
102 for (pugi::xml_node currNodeToInsert = newNode.first_child(); currNodeToInsert; currNodeToInsert = currNodeToInsert.next_sibling())
103 {
104 nodesToInsertion[j].append_copy(currNodeToInsert); // append the new node
105 }
106
107 }
108
109 UtilXmlTools::saveXmlFile(filesToProcess[i],this->document, "@ADD_INSIDE_NODES");
110
111 nodesToInsertion.clear();
112 }
113 }
114 catch(const std::exception &e){
115 UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES", QString("An exception was caught: ") + e.what());
116 }
117
118 UtilXmlTools::displaySuccessMessage(filesToProcess.size(),"@ADD_INSIDE_NODES");
119}
120
121void XmlPatch::removeNodesOperation(XmlFilter &filters, const QString &xPathExpression, const QString &filesWildcard){
122
123 QVector<QString> filesToProcess;
124
125 QList<pugi::xml_node> nodesToDeletion;
126
127 try{
128
129 filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard);
130
131 if(filesToProcess.isEmpty()){
132 UtilXmlTools::displayErrorMessage("@REMOVE_NODES","No XML files were found for the wildcard: "+filesWildcard);
133 }
134
135 // Process all XmlFiles
136 for(int i=0; i<filesToProcess.size(); i++){
137
138 UtilXmlTools::loadXmlFile(filesToProcess[i],this->document,this->rootNode,this->backupsEnabled,this->verboseEnabled,"@REMOVE_NODES");
139
140 // Check how the element will be fetched via element name or xpath expression
141 if(xPathExpression.isEmpty()){
142 UtilXmlTools::getAllNamedElements(this->rootNode,nodesToDeletion,filters);
143 }
144 else{
145 UtilXmlTools::getAllXpathElements(xPathExpression,this->document,nodesToDeletion);
146 }
147
148 if(nodesToDeletion.isEmpty() || nodesToDeletion[0].type()==pugi::node_null){
149
150 QString errMessage;
151
152 if(xPathExpression.isEmpty()){
153 errMessage = "No node was found with an ElementName: '" + filters.getElementName() + "'";
154 if(filters.getParentElementName()!=""){
155 errMessage += " and a ParentElementName: '" + filters.getParentElementName() + "'";
156 }
157 if(filters.getAttributeName()!=""){
158 errMessage += " and an AttributeName: '" + filters.getAttributeName() + "' and an AttributeValue: '" + filters.getAttributeValue() + "'";
159 }
160 }
161 else{
162 errMessage = "No node was found with an XPathExpression: '" + xPathExpression + "'";
163 }
164
165 UtilXmlTools::displayErrorMessage("@REMOVE_NODES",errMessage);
166 }
167
168 // Delete all the specified nodes
169 for(int j=0; j<nodesToDeletion.size(); j++){
170 if(!nodesToDeletion[j].parent().remove_child(nodesToDeletion[j])){ // remove the node
171
172 QString errMessage;
173 if(xPathExpression.isEmpty()){
174 errMessage = "Couldn't remove the node with Element '" + filters.getElementName() + "'";
175
176 if(filters.getParentElementName()!=""){
177 errMessage += " and a ParentElement: '" + filters.getParentElementName() + "'";
178 }
179 }
180 else{
181 errMessage = "Couldn't remove the node with the XPathExpression '" + xPathExpression + "'";
182 }
183
184 UtilXmlTools::displayErrorMessage("@REMOVE_NODES",errMessage);
185 }
186 }
187
188 UtilXmlTools::saveXmlFile(filesToProcess[i],this->document, "@REMOVE_NODES");
189
190 nodesToDeletion.clear();
191 }
192 }
193 catch(const std::exception &e){
194 UtilXmlTools::displayErrorMessage("@REMOVE_NODES", QString("An exception was caught: ") + e.what());
195 }
196
197 UtilXmlTools::displaySuccessMessage(filesToProcess.size(), "@REMOVE_NODES");
198}
199
200void XmlPatch::executeCommandOperation(const QString &commandString){
201
202 // Avoid infinite fork loops
203 if(commandString.contains("-p ") || commandString.contains("--patch-files ")){
204 UtilXmlTools::displayErrorMessage("@COMMAND","Use of --patch-files option is not allowed inside a patch file");
205 }
206
207 // Reserved to AEI
208 if(commandString.contains("--aei-patch-files-list ")){
209 UtilXmlTools::displayErrorMessage("@COMMAND","Use of --aei-patch-files-list option is not allowed inside a patch file");
210 }
211
212 std::cout << "@COMMAND patch operation output:\n"
213 << "########################################################################"
214 << std::endl;
215
216 OptionsParser myParser(UtilXmlTools::QStringToArgsArray(commandString));
217 myParser.parse();
218
219 std::cout
220 << "########################################################################"
221 << std::endl;
222
223 UtilXmlTools::displaySuccessMessage(1,"@COMMAND");
224}
225
226void XmlPatch::executeCustomCommandOperation(const QString &jsString, const QString &filesWildcard){
227
228 QVector<QString> filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard);
229
230 try{
231 if(filesToProcess.isEmpty()){
232 UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","No XML files were found for the wildcard: "+filesWildcard);
233 }
234
235 XmlCustomCode::getInstance()->executeCustomCode(jsString,filesToProcess,this->backupsEnabled,this->verboseEnabled);
236 }
237 catch(const std::exception &e){
238 UtilXmlTools::displayErrorMessage("@CUSTOM_CODE", QString("An exception was caught: ") + e.what());
239 }
240
241 UtilXmlTools::displaySuccessMessage(filesToProcess.size(), "@CUSTOM_CODE");
242}
243
244void XmlPatch::checkPatchVersion(const QString &file, QTextStream &fileStream){
245
246 QString line, patchVersion="";
247
248 // First get the patch version and check its validity
249 while ( !fileStream.atEnd() ){
250 line = fileStream.readLine();
251
252 if(line.startsWith('#')){ // Ignore comments
253 continue;
254 }
255 else if(line.startsWith("@XML_TOOLS")){
256
257 patchVersion=getPatchParameterValue(line,"Version");
258
259 if(!patchVersion.startsWith("2.0")){
260 QString errMessage;
261
262 errMessage = "The current patch version is incompatible with this XmlTools version:\n";
263 errMessage += "Patch file name: '" + file + "'\n";
264 errMessage += "XmlTools version: " + GlobalVars::AppVersion + "\n" + "CurrPatch version: " + patchVersion + "";
265 UtilXmlTools::displayErrorMessage("@XML_TOOLS",errMessage);
266 }
267 break; // We have got what we wanted
268 }
269 }
270
271 if(patchVersion==""){
272 UtilXmlTools::displayErrorMessage("@XML_TOOLS","Patch version not found.");
273 }
274
275}
276
277void XmlPatch::checkAndProcessValidCommands(QTextStream &fileStream){
278
279 QString line, filesWildcard;
280 QString xmlToInsert, command, jsCode;
281 QString xPathExpression;
282 XmlFilter filters;
283
284 // Process the rest of the commands in patch file
285 while ( !fileStream.atEnd() ){
286 line = fileStream.readLine();
287
288 if(line.startsWith('#')){ // Ignore comments
289 continue;
290 }
291 else if(line.startsWith("@ADD_INSIDE_NODES")){
292 xPathExpression=getPatchParameterValue(line,"XPathExpression");
293 filters.setElementName(getPatchParameterValue(line,"ElementName"));
294 filters.setParentElementName(getPatchParameterValue(line,"ParentElementName"));
295 filters.setAttributeName(getPatchParameterValue(line,"AttributeName"));
296 filters.setAttributeValue(getPatchParameterValue(line,"AttributeValue"));
297
298 if(this->forceTargetFilesWildcard!=""){
299 filesWildcard=this->forceTargetFilesWildcard;
300 }
301 else{
302 filesWildcard=getPatchParameterValue(line,"Files");
303 }
304
305 // Check options
306 if(xPathExpression.isEmpty() && filters.getElementName().isEmpty()){
307 UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES","ElementName option or XPathExpression option is required.");
308 }
309 else if(!xPathExpression.isEmpty() && !filters.getElementName().isEmpty()){
310 UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES","ElementName option and XPathExpression option cannot be used simultaneously.");
311 }
312 if(filters.getAttributeName()!="" && filters.getAttributeValue()==""){
313 UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES","AttributeValue option is required if using AttributeName option.");
314 }
315
316 if(filters.getAttributeValue()!="" && filters.getAttributeName()==""){
317 UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES","AttributeName option is required if using AttributeValue option.");
318 }
319
320 while ( !fileStream.atEnd() && !line.startsWith("</xml>")){
321 line = fileStream.readLine();
322
323 if(!line.startsWith("<xml>") && !line.startsWith("</xml>")){
324 xmlToInsert += line + "\n";
325 }
326 }
327
328 insertNodesOperation(xmlToInsert,filters,xPathExpression,filesWildcard);
329
330 xmlToInsert.clear();
331 filters.clear();
332 xPathExpression.clear();
333 filesWildcard.clear();
334 }
335 else if(line.startsWith("@REMOVE_NODES")){
336
337 xPathExpression=getPatchParameterValue(line,"XPathExpression");
338 filters.setElementName(getPatchParameterValue(line,"ElementName"));
339 filters.setParentElementName(getPatchParameterValue(line,"ParentElementName"));
340 filters.setAttributeName(getPatchParameterValue(line,"AttributeName"));
341 filters.setAttributeValue(getPatchParameterValue(line,"AttributeValue"));
342
343 if(this->forceTargetFilesWildcard!=""){
344 filesWildcard=this->forceTargetFilesWildcard;
345 }
346 else{
347 filesWildcard=getPatchParameterValue(line,"Files");
348 }
349
350 // Check options
351 if(xPathExpression.isEmpty() && filters.getElementName().isEmpty()){
352 UtilXmlTools::displayErrorMessage("@REMOVE_NODES","ElementName option or XPathExpression option is required.");
353 }
354 else if(!xPathExpression.isEmpty() && !filters.getElementName().isEmpty()){
355 UtilXmlTools::displayErrorMessage("@REMOVE_NODES","ElementName option and XPathExpression option cannot be used simultaneously.");
356 }
357
358 if(filters.getAttributeName()!="" && filters.getAttributeValue()==""){
359 UtilXmlTools::displayErrorMessage("@REMOVE_NODES","AttributeValue option is required if using AttributeName option.");
360 }
361
362 if(filters.getAttributeValue()!="" && filters.getAttributeName()==""){
363 UtilXmlTools::displayErrorMessage("@REMOVE_NODES","AttributeName option is required if using AttributeValue option.");
364 }
365
366 removeNodesOperation(filters,xPathExpression,filesWildcard);
367
368 filters.clear();
369 xPathExpression.clear();
370 filesWildcard.clear();
371 }
372 else if(line.startsWith("@COMMAND")){
373
374 command=GlobalVars::AppExecutable;
375
376 command+=" "+getPatchParameterValue(line,"Options");
377
378 // Add --no-backups and --no-verbose if patch was called with that arguments
379 if(!this->backupsEnabled){
380 command.append(" --no-backups ");
381 }
382
383 if(!this->verboseEnabled){
384 command.append(" --no-verbose ");
385 }
386
387 command.replace("'","\""); //replace apostrophe by quotes, to avoid problems
388 command.replace("\"\"","'"); // this allow to use '' as ' ('' is transformed in "" and then in ')
389
390 // Append files if forced to
391
392 // This is added at the end so the apostrophe isn't done here (the path can contain it)
393 if(!this->forceTargetFilesWildcard.isEmpty()){
394 command+=" --files \""+this->forceTargetFilesWildcard+"\" ";
395 }
396
397 executeCommandOperation(command);
398
399 command.clear();
400 filesWildcard.clear();
401 }
402 else if(line.startsWith("@CUSTOM_CODE")){
403
404 if(this->forceTargetFilesWildcard!=""){
405 filesWildcard=this->forceTargetFilesWildcard;
406 }
407 else{
408 filesWildcard=getPatchParameterValue(line,"Files");
409 }
410
411 while ( !fileStream.atEnd() && !line.startsWith("</code>")){
412
413 line = fileStream.readLine();
414
415 if(!line.startsWith("<code>") && !line.startsWith("</code>")){
416 jsCode += line + "\n";
417 }
418 }
419
420 executeCustomCommandOperation(jsCode,filesWildcard);
421
422 jsCode.clear();
423 filesWildcard.clear();
424 }
425 }
426}
427
428QString XmlPatch::getPatchParameterValue(const QString& line, QString parameter){
429
430 int startValueIdx, endValueIdx;
431
432 parameter=" "+parameter+" "; // Parameters include a space before and after it's value.
433
434 if(!line.contains(parameter)){
435 if(parameter==" ParentElementName " || parameter==" AttributeName " || parameter==" AttributeValue "
436 || parameter==" ElementName " || parameter==" XPathExpression "){
437 return ""; // not mandatory
438 }
439 parameter.remove(" "); // just remove the space added so it doesn't look weird when outputted to the user
440
441 UtilXmlTools::displayErrorMessage("Read patch file parameter","Couldn't retrieve '" + parameter + "' parameter.");
442 }
443
444 startValueIdx=line.indexOf(parameter); // get the position where parameter is defined
445 endValueIdx=line.indexOf("\"",startValueIdx)+1; // get the position where parameter value begins (+1 to ignore mandotory quote)
446
447 startValueIdx=endValueIdx;
448 endValueIdx=line.indexOf("\"",startValueIdx); // get the last mandatory quote of the value
449
450 return line.mid(startValueIdx,endValueIdx-startValueIdx); // return the value of the parameter (is in the middle of the mandatory quotes)
451}
Note: See TracBrowser for help on using the repository browser.