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