[906] | 1 | #include "xmlpatch.h"
|
---|
| 2 |
|
---|
[923] | 3 | XmlPatch::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!=""){
|
---|
[910] | 11 | std::cout << "User forced patch in the target file(s): " << forceTargetFilesWildcard.toLatin1().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 |
|
---|
| 20 | void 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] | 47 | void XmlPatch::insertNodesOperation(const QString &xmlString, XmlFilter &filters, const QString &xPathExpression, const QString &filesWildcard){
|
---|
[906] | 48 |
|
---|
| 49 | QStringList 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 |
|
---|
[906] | 60 | result=newNode.load(xmlString.toLatin1().constData()); // load xml to insert
|
---|
| 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 |
|
---|
[920] | 79 | if(nodesToInsertion[0].type()==pugi::node_null){
|
---|
[906] | 80 |
|
---|
| 81 | QString errMessage;
|
---|
| 82 |
|
---|
[920] | 83 | if(xPathExpression.isEmpty()){
|
---|
| 84 | errMessage = "It wasn't found any node with a ElementName: '" + filters.getElementName() + "'";
|
---|
| 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{
|
---|
| 93 | errMessage = "It wasn't found any node with a 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 | {
|
---|
| 102 | nodesToInsertion[j].append_copy(currNodeToInsert); // append the new node
|
---|
| 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] | 115 | void XmlPatch::removeNodesOperation(XmlFilter &filters, const QString &xPathExpression, const QString &filesWildcard){
|
---|
[906] | 116 |
|
---|
| 117 | QStringList filesToProcess;
|
---|
| 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 |
|
---|
[920] | 140 | if(nodesToDeletion[0].type()==pugi::node_null){
|
---|
| 141 |
|
---|
[906] | 142 | QString errMessage;
|
---|
| 143 |
|
---|
[920] | 144 | if(xPathExpression.isEmpty()){
|
---|
| 145 | errMessage = "It wasn't found any node with a ElementName: '" + filters.getElementName() + "'";
|
---|
| 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{
|
---|
| 154 | errMessage = "It wasn't found any node with a 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 |
|
---|
| 188 | void XmlPatch::executeCommandOperation(const QString &commandString){
|
---|
| 189 |
|
---|
| 190 | QProcess newXmlToolsInstance;
|
---|
| 191 | QString resultOutput;
|
---|
| 192 |
|
---|
| 193 | // Avoid infinite fork loops
|
---|
[920] | 194 | if(commandString.contains(" -p ") || commandString.contains(" --patch-files ")){
|
---|
[906] | 195 | UtilXmlTools::displayErrorMessage("@COMMAND","Use of --patch-files option is not allowed inside a patch file");
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[920] | 198 | newXmlToolsInstance.start(GlobalVars::AppExecutable + " " + commandString);
|
---|
[906] | 199 | newXmlToolsInstance.waitForFinished(-1); // wait for new instance to finish
|
---|
| 200 |
|
---|
| 201 | resultOutput=newXmlToolsInstance.readAll();
|
---|
| 202 |
|
---|
| 203 | if(newXmlToolsInstance.exitCode()!=0){
|
---|
| 204 | UtilXmlTools::displayErrorMessage("@COMMAND", "An error ocurred:\n" + resultOutput);
|
---|
| 205 | exit(1);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[920] | 208 | std::cout << "@COMMAND patch operation output:\n"
|
---|
| 209 | << "########################################################################\n"
|
---|
| 210 | << resultOutput.toLatin1().constData()
|
---|
| 211 | << "########################################################################"
|
---|
| 212 | << std::endl;
|
---|
[906] | 213 |
|
---|
| 214 | UtilXmlTools::displaySuccessMessage(1,"@COMMAND");
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | void XmlPatch::executeCustomCommandOperation(const QString &jsString, const QString &filesWildcard){
|
---|
| 218 |
|
---|
| 219 | QStringList filesToProcess;
|
---|
| 220 | #ifdef _USE_OLD_JS_ENGINE
|
---|
| 221 | QScriptEngine engine;
|
---|
| 222 | QScriptValue engineResult; // variable to check for js_errors
|
---|
| 223 | #else
|
---|
| 224 | QJSEngine engine;
|
---|
| 225 | QJSValue engineResult; // variable to check for js_errors
|
---|
| 226 | #endif
|
---|
| 227 |
|
---|
| 228 | QString rexmlString, jsxmlString, currXmlFileString;
|
---|
| 229 |
|
---|
| 230 |
|
---|
| 231 | QFile rexmlfile(":/resources/libs/rexml.js");
|
---|
| 232 | QFile jsxmlfile(":/resources/libs/jsxml.js");
|
---|
| 233 |
|
---|
| 234 | filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard);
|
---|
| 235 |
|
---|
[910] | 236 | if(filesToProcess.isEmpty()){
|
---|
| 237 | UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","No XML files were found for the wildcard: "+filesWildcard);
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[906] | 240 | rexmlfile.open(QFile::ReadOnly | QFile::Text);
|
---|
| 241 | jsxmlfile.open(QFile::ReadOnly | QFile::Text);
|
---|
| 242 |
|
---|
| 243 | rexmlString=QTextStream(&rexmlfile).readAll();
|
---|
| 244 | jsxmlString=QTextStream(&jsxmlfile).readAll();
|
---|
| 245 |
|
---|
| 246 | engine.evaluate(rexmlString); // load js libraries
|
---|
| 247 | engine.evaluate(jsxmlString);
|
---|
| 248 |
|
---|
| 249 | // Process all XmlFiles
|
---|
| 250 | for(int i=0; i<filesToProcess.size(); i++){
|
---|
| 251 |
|
---|
| 252 | if(this->backupsEnabled){
|
---|
[923] | 253 | UtilXmlTools::backupFile(filesToProcess[i], this->verboseEnabled);
|
---|
[906] | 254 | }
|
---|
| 255 |
|
---|
| 256 | QFile currXmlFile(filesToProcess[i]);
|
---|
| 257 |
|
---|
| 258 | if(!currXmlFile.open(QFile::ReadOnly | QFile::Text)){
|
---|
| 259 | UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Error loading '" + filesToProcess[i] + "' file for read operation.");
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | currXmlFileString=QTextStream(&currXmlFile).readAll();
|
---|
| 263 |
|
---|
| 264 | currXmlFile.close(); // close reading
|
---|
| 265 |
|
---|
| 266 | engine.globalObject().setProperty("$xmlData",currXmlFileString);
|
---|
| 267 |
|
---|
| 268 | engineResult=engine.evaluate(jsString);
|
---|
| 269 |
|
---|
| 270 | #ifdef _USE_OLD_JS_ENGINE
|
---|
| 271 | if (engine.hasUncaughtException()) {
|
---|
| 272 | displayJsException(engine,engineResult);
|
---|
| 273 | }
|
---|
| 274 | #else
|
---|
| 275 | checkForJsException(engineResult);
|
---|
| 276 | #endif
|
---|
| 277 |
|
---|
| 278 | if(!currXmlFile.open(QFile::WriteOnly | QFile::Text | QIODevice::Truncate)){
|
---|
| 279 | UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Error loading '" + filesToProcess[i] + "' file for @CUSTOM_CODE write operation.");
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | engineResult=engine.globalObject().property("$xmlData");
|
---|
| 283 |
|
---|
| 284 | #ifdef _USE_OLD_JS_ENGINE
|
---|
| 285 | if (engine.hasUncaughtException()) {
|
---|
| 286 | displayJsException(engine,engineResult);
|
---|
| 287 | }
|
---|
| 288 | #else
|
---|
| 289 | checkForJsException(engineResult);
|
---|
| 290 | #endif
|
---|
| 291 |
|
---|
| 292 | QTextStream(&currXmlFile) << engineResult.toString(); // retreive the modified xml by javascript and save it to the file
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | UtilXmlTools::displaySuccessMessage(filesToProcess.size(), "@CUSTOM_CODE");
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | void XmlPatch::checkPatchVersion(const QString &file, QTextStream &fileStream){
|
---|
| 299 |
|
---|
| 300 | QString line, patchVersion="";
|
---|
| 301 |
|
---|
| 302 | // First get the patch version and check it validity
|
---|
| 303 | while ( !fileStream.atEnd() ){
|
---|
| 304 | line = fileStream.readLine();
|
---|
| 305 |
|
---|
| 306 | if(line.startsWith('#')){ // Ignore comments
|
---|
| 307 | continue;
|
---|
| 308 | }
|
---|
| 309 | else if(line.startsWith("@XML_TOOLS")){
|
---|
| 310 |
|
---|
| 311 | patchVersion=getPatchParameterValue(line,"Version");
|
---|
| 312 |
|
---|
| 313 | if(!patchVersion.startsWith("2.0")){
|
---|
| 314 | QString errMessage;
|
---|
| 315 |
|
---|
| 316 | errMessage = "The current patch version is incompatible with this XmlTools version:\n";
|
---|
| 317 | errMessage += "Patch file name: '" + file + "'\n";
|
---|
| 318 | errMessage += "XmlTools version: " + GlobalVars::AppVersion + "\n" + "CurrPatch version: " + patchVersion + "";
|
---|
| 319 | UtilXmlTools::displayErrorMessage("@XML_TOOLS",errMessage);
|
---|
| 320 | }
|
---|
| 321 | break; // We have got what we wanted
|
---|
| 322 | }
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | if(patchVersion==""){
|
---|
| 326 | UtilXmlTools::displayErrorMessage("@XML_TOOLS","Patch version not found.");
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | void XmlPatch::checkAndProcessValidCommands(QTextStream &fileStream){
|
---|
| 332 |
|
---|
[910] | 333 | QString line, filesWildcard;
|
---|
[906] | 334 | QString xmlToInsert, command, jsCode;
|
---|
[920] | 335 | QString xPathExpression;
|
---|
[906] | 336 | XmlFilter filters;
|
---|
| 337 |
|
---|
| 338 | // Process the rest of the commands in patch file
|
---|
| 339 | while ( !fileStream.atEnd() ){
|
---|
| 340 | line = fileStream.readLine();
|
---|
| 341 |
|
---|
| 342 | if(line.startsWith('#')){ // Ignore comments
|
---|
| 343 | continue;
|
---|
| 344 | }
|
---|
[920] | 345 | else if(line.startsWith("@ADD_INSIDE_NODES")){
|
---|
| 346 | xPathExpression=getPatchParameterValue(line,"XPathExpression");
|
---|
[906] | 347 | filters.setElementName(getPatchParameterValue(line,"ElementName"));
|
---|
| 348 | filters.setParentElementName(getPatchParameterValue(line,"ParentElementName"));
|
---|
| 349 | filters.setAttributeName(getPatchParameterValue(line,"AttributeName"));
|
---|
| 350 | filters.setAttributeValue(getPatchParameterValue(line,"AttributeValue"));
|
---|
[910] | 351 |
|
---|
| 352 | if(this->forceTargetFilesWildcard!=""){
|
---|
| 353 | filesWildcard=this->forceTargetFilesWildcard;
|
---|
[906] | 354 | }
|
---|
| 355 | else{
|
---|
[910] | 356 | filesWildcard=getPatchParameterValue(line,"Files");
|
---|
[906] | 357 | }
|
---|
| 358 |
|
---|
[920] | 359 | // Check options
|
---|
| 360 | if(xPathExpression.isEmpty() && filters.getElementName().isEmpty()){
|
---|
| 361 | UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES","ElementName option or XPathExpression option is required.");
|
---|
| 362 | }
|
---|
| 363 | else if(!xPathExpression.isEmpty() && !filters.getElementName().isEmpty()){
|
---|
| 364 | UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES","ElementName option and XPathExpression options cannot be used simultaneously.");
|
---|
| 365 | }
|
---|
[906] | 366 | if(filters.getAttributeName()!="" && filters.getAttributeValue()==""){
|
---|
[920] | 367 | UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES","AttributeValue option is required if using AttributeName option.");
|
---|
[906] | 368 | }
|
---|
| 369 |
|
---|
| 370 | if(filters.getAttributeValue()!="" && filters.getAttributeName()==""){
|
---|
[920] | 371 | UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES","AttributeName option is required if using AttributeValue option.");
|
---|
[906] | 372 | }
|
---|
| 373 |
|
---|
| 374 | while ( !fileStream.atEnd() && !line.startsWith("</xml>")){
|
---|
| 375 | line = fileStream.readLine();
|
---|
| 376 |
|
---|
| 377 | if(!line.startsWith("<xml>") && !line.startsWith("</xml>")){
|
---|
| 378 | xmlToInsert += line + "\n";
|
---|
| 379 | }
|
---|
| 380 | }
|
---|
| 381 |
|
---|
[920] | 382 | insertNodesOperation(xmlToInsert,filters,xPathExpression,filesWildcard);
|
---|
[906] | 383 |
|
---|
[920] | 384 | xmlToInsert.clear();
|
---|
| 385 | filters.clear();
|
---|
| 386 | xPathExpression.clear();
|
---|
| 387 | filesWildcard.clear();
|
---|
[906] | 388 | }
|
---|
[920] | 389 | else if(line.startsWith("@REMOVE_NODES")){
|
---|
[906] | 390 |
|
---|
[920] | 391 | xPathExpression=getPatchParameterValue(line,"XPathExpression");
|
---|
[906] | 392 | filters.setElementName(getPatchParameterValue(line,"ElementName"));
|
---|
| 393 | filters.setParentElementName(getPatchParameterValue(line,"ParentElementName"));
|
---|
| 394 | filters.setAttributeName(getPatchParameterValue(line,"AttributeName"));
|
---|
| 395 | filters.setAttributeValue(getPatchParameterValue(line,"AttributeValue"));
|
---|
[910] | 396 |
|
---|
| 397 | if(this->forceTargetFilesWildcard!=""){
|
---|
| 398 | filesWildcard=this->forceTargetFilesWildcard;
|
---|
[906] | 399 | }
|
---|
| 400 | else{
|
---|
[910] | 401 | filesWildcard=getPatchParameterValue(line,"Files");
|
---|
[906] | 402 | }
|
---|
| 403 |
|
---|
[920] | 404 | // Check options
|
---|
| 405 | if(xPathExpression.isEmpty() && filters.getElementName().isEmpty()){
|
---|
| 406 | UtilXmlTools::displayErrorMessage("@REMOVE_NODES","ElementName option or XPathExpression option is required.");
|
---|
| 407 | }
|
---|
| 408 | else if(!xPathExpression.isEmpty() && !filters.getElementName().isEmpty()){
|
---|
| 409 | UtilXmlTools::displayErrorMessage("@REMOVE_NODES","ElementName option and XPathExpression options cannot be used simultaneously.");
|
---|
| 410 | }
|
---|
| 411 |
|
---|
[906] | 412 | if(filters.getAttributeName()!="" && filters.getAttributeValue()==""){
|
---|
[920] | 413 | UtilXmlTools::displayErrorMessage("@REMOVE_NODES","AttributeValue option is required if using AttributeName option.");
|
---|
[906] | 414 | }
|
---|
| 415 |
|
---|
| 416 | if(filters.getAttributeValue()!="" && filters.getAttributeName()==""){
|
---|
[920] | 417 | UtilXmlTools::displayErrorMessage("@REMOVE_NODES","AttributeName option is required if using AttributeValue option.");
|
---|
[906] | 418 | }
|
---|
| 419 |
|
---|
[920] | 420 | removeNodesOperation(filters,xPathExpression,filesWildcard);
|
---|
[906] | 421 |
|
---|
[920] | 422 | filters.clear();
|
---|
| 423 | xPathExpression.clear();
|
---|
| 424 | filesWildcard.clear();
|
---|
[906] | 425 | }
|
---|
| 426 | else if(line.startsWith("@COMMAND")){
|
---|
| 427 |
|
---|
[920] | 428 | command=getPatchParameterValue(line,"Arguments");
|
---|
[906] | 429 |
|
---|
[920] | 430 | command.replace("'","\""); //replace apostrophe by quotes, to avoid problems
|
---|
| 431 |
|
---|
[906] | 432 | executeCommandOperation(command);
|
---|
[920] | 433 |
|
---|
| 434 | command.clear();
|
---|
[906] | 435 | }
|
---|
| 436 | else if(line.startsWith("@CUSTOM_CODE")){
|
---|
| 437 |
|
---|
[910] | 438 | if(this->forceTargetFilesWildcard!=""){
|
---|
| 439 | filesWildcard=this->forceTargetFilesWildcard;
|
---|
[906] | 440 | }
|
---|
| 441 | else{
|
---|
[910] | 442 | filesWildcard=getPatchParameterValue(line,"Files");
|
---|
[906] | 443 | }
|
---|
| 444 |
|
---|
| 445 | while ( !fileStream.atEnd() && !line.startsWith("</code>")){
|
---|
| 446 |
|
---|
| 447 | line = fileStream.readLine();
|
---|
| 448 |
|
---|
| 449 | if(!line.startsWith("<code>") && !line.startsWith("</code>")){
|
---|
| 450 | jsCode += line + "\n";
|
---|
| 451 | }
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 | executeCustomCommandOperation(jsCode,filesWildcard);
|
---|
[920] | 455 |
|
---|
| 456 | jsCode.clear();
|
---|
| 457 | filesWildcard.clear();
|
---|
[906] | 458 | }
|
---|
| 459 | }
|
---|
| 460 | }
|
---|
| 461 |
|
---|
| 462 | QString XmlPatch::getPatchParameterValue(const QString& line, QString parameter){
|
---|
| 463 |
|
---|
| 464 | int startValueIdx, endValueIdx;
|
---|
| 465 |
|
---|
[920] | 466 | parameter=" "+parameter+" "; // Parameters include a space before and after it's value.
|
---|
[906] | 467 |
|
---|
| 468 | if(!line.contains(parameter)){
|
---|
[920] | 469 | if(parameter==" ParentElementName " || parameter==" AttributeName " || parameter==" AttributeValue "
|
---|
| 470 | || parameter==" ElementName " || parameter==" XPathExpression "){
|
---|
[906] | 471 | return ""; // not mandatory
|
---|
| 472 | }
|
---|
| 473 | parameter.remove(" "); // just remove the space added so it doesn't look weird when outputted to the user
|
---|
| 474 |
|
---|
| 475 | UtilXmlTools::displayErrorMessage("Read patch file parameter","Couldn't retreive '" + parameter + "' parameter.");
|
---|
| 476 | }
|
---|
| 477 |
|
---|
| 478 | startValueIdx=line.indexOf(parameter); // get the position where parameter is defined
|
---|
| 479 | endValueIdx=line.indexOf("\"",startValueIdx)+1; // get the position where parameter value begins (+1 to ignore mandotory quote)
|
---|
| 480 |
|
---|
| 481 | startValueIdx=endValueIdx;
|
---|
| 482 | endValueIdx=line.indexOf("\"",startValueIdx); // get the last mandatory quote of the value
|
---|
| 483 |
|
---|
| 484 | return line.mid(startValueIdx,endValueIdx-startValueIdx); // return the value of the parameter (is in the middle of the mandatory quotes)
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | #ifdef _USE_OLD_JS_ENGINE
|
---|
| 488 | void XmlPatch::displayJsException(QScriptEngine &engine, QScriptValue &engineResult){
|
---|
| 489 | if (engine.hasUncaughtException()) {
|
---|
| 490 | UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Uncaught js exception (user code) at line " +QString::number(engine.uncaughtExceptionLineNumber()) + ":\n" + engineResult.toString());
|
---|
| 491 | }
|
---|
| 492 | }
|
---|
| 493 | #else
|
---|
| 494 | void XmlPatch::checkForJsException(QJSValue &engineResult){
|
---|
| 495 | if (engineResult.isError()) {
|
---|
| 496 | UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Uncaught js exception (user code):\n" + engineResult.toString());
|
---|
| 497 | }
|
---|
| 498 | }
|
---|
| 499 | #endif
|
---|