source: XmlTools2/trunk/xmlpatch.cpp@ 997

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

XmlTools: fixed bug http://www.bugs.oni2.net/ticket/71

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