[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!=""){
|
---|
[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 |
|
---|
| 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 |
|
---|
[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 |
|
---|
[920] | 79 | if(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] | 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()){
|
---|
[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 |
|
---|
| 188 | void 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 |
|
---|
[947] | 214 | QScriptValue echo(QScriptContext *context, QScriptEngine *engine)
|
---|
| 215 | {
|
---|
| 216 | std::cout << context->argument(0).toString().toUtf8().constData() << std::endl;
|
---|
| 217 |
|
---|
| 218 | return "";
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[906] | 221 | void XmlPatch::executeCustomCommandOperation(const QString &jsString, const QString &filesWildcard){
|
---|
| 222 |
|
---|
[953] | 223 | QString rexmlString, jsxmlString;
|
---|
| 224 | QStringList filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard);
|
---|
[906] | 225 |
|
---|
[953] | 226 | if(filesToProcess.isEmpty()){
|
---|
| 227 | UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","No XML files were found for the wildcard: "+filesWildcard);
|
---|
| 228 | }
|
---|
[906] | 229 |
|
---|
| 230 | QFile rexmlfile(":/resources/libs/rexml.js");
|
---|
| 231 | QFile jsxmlfile(":/resources/libs/jsxml.js");
|
---|
| 232 |
|
---|
| 233 | rexmlfile.open(QFile::ReadOnly | QFile::Text);
|
---|
| 234 | jsxmlfile.open(QFile::ReadOnly | QFile::Text);
|
---|
| 235 |
|
---|
| 236 | rexmlString=QTextStream(&rexmlfile).readAll();
|
---|
| 237 | jsxmlString=QTextStream(&jsxmlfile).readAll();
|
---|
| 238 |
|
---|
| 239 | // Process all XmlFiles
|
---|
[953] | 240 | #pragma omp parallel for
|
---|
[906] | 241 | for(int i=0; i<filesToProcess.size(); i++){
|
---|
| 242 |
|
---|
[953] | 243 | QString currXmlFileString;
|
---|
| 244 |
|
---|
| 245 | QScriptEngine engine;
|
---|
| 246 | QScriptValue engineResult; // variable to check for js_errors
|
---|
[958] | 247 | double elapsed_secs; // elapsed seconds that a user script took
|
---|
| 248 | clock_t begin; // seconds that a script started
|
---|
[953] | 249 |
|
---|
| 250 | // Add echo function so user can debug the code
|
---|
| 251 | QScriptValue echoFunction = engine.newFunction(echo);
|
---|
| 252 | engine.globalObject().setProperty("echo", echoFunction);
|
---|
| 253 |
|
---|
| 254 | engine.evaluate(rexmlString); // load js libraries
|
---|
| 255 | engine.evaluate(jsxmlString);
|
---|
| 256 |
|
---|
[906] | 257 | if(this->backupsEnabled){
|
---|
[923] | 258 | UtilXmlTools::backupFile(filesToProcess[i], this->verboseEnabled);
|
---|
[906] | 259 | }
|
---|
| 260 |
|
---|
| 261 | QFile currXmlFile(filesToProcess[i]);
|
---|
| 262 |
|
---|
| 263 | if(!currXmlFile.open(QFile::ReadOnly | QFile::Text)){
|
---|
| 264 | UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Error loading '" + filesToProcess[i] + "' file for read operation.");
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | currXmlFileString=QTextStream(&currXmlFile).readAll();
|
---|
| 268 |
|
---|
| 269 | currXmlFile.close(); // close reading
|
---|
| 270 |
|
---|
| 271 | engine.globalObject().setProperty("$xmlData",currXmlFileString);
|
---|
| 272 |
|
---|
[960] | 273 | begin = clock();
|
---|
[958] | 274 |
|
---|
[947] | 275 | // main needs to be called so the user code is evaluated
|
---|
[958] | 276 | // alternatively you can do: myFunc=engine.evaluate('(function main(){})'); myFunc.call();
|
---|
| 277 | // Note the () around the function
|
---|
[947] | 278 | engineResult=engine.evaluate("main(); function main() {"+jsString+"}"); // main funtion allows to use return to exit prematurely from user code
|
---|
[906] | 279 |
|
---|
[958] | 280 | if(this->verboseEnabled){
|
---|
| 281 | elapsed_secs = double(clock() - begin) / CLOCKS_PER_SEC;
|
---|
| 282 |
|
---|
| 283 | // Warn the user if the script took much time
|
---|
| 284 | if(elapsed_secs>SLOW_SCRIPT_TIME){
|
---|
| 285 | std::cout << "Warning: Slow javascript code detected.\n" <<
|
---|
| 286 | "Warning: Script execution seconds: " << elapsed_secs
|
---|
| 287 | << std::endl;
|
---|
| 288 | }
|
---|
| 289 | }
|
---|
| 290 |
|
---|
[906] | 291 | if (engine.hasUncaughtException()) {
|
---|
| 292 | displayJsException(engine,engineResult);
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | if(!currXmlFile.open(QFile::WriteOnly | QFile::Text | QIODevice::Truncate)){
|
---|
| 296 | UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Error loading '" + filesToProcess[i] + "' file for @CUSTOM_CODE write operation.");
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | engineResult=engine.globalObject().property("$xmlData");
|
---|
| 300 |
|
---|
| 301 | if (engine.hasUncaughtException()) {
|
---|
| 302 | displayJsException(engine,engineResult);
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | QTextStream(&currXmlFile) << engineResult.toString(); // retreive the modified xml by javascript and save it to the file
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | UtilXmlTools::displaySuccessMessage(filesToProcess.size(), "@CUSTOM_CODE");
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | void XmlPatch::checkPatchVersion(const QString &file, QTextStream &fileStream){
|
---|
| 312 |
|
---|
| 313 | QString line, patchVersion="";
|
---|
| 314 |
|
---|
| 315 | // First get the patch version and check it validity
|
---|
| 316 | while ( !fileStream.atEnd() ){
|
---|
| 317 | line = fileStream.readLine();
|
---|
| 318 |
|
---|
| 319 | if(line.startsWith('#')){ // Ignore comments
|
---|
| 320 | continue;
|
---|
| 321 | }
|
---|
| 322 | else if(line.startsWith("@XML_TOOLS")){
|
---|
| 323 |
|
---|
| 324 | patchVersion=getPatchParameterValue(line,"Version");
|
---|
| 325 |
|
---|
| 326 | if(!patchVersion.startsWith("2.0")){
|
---|
| 327 | QString errMessage;
|
---|
| 328 |
|
---|
| 329 | errMessage = "The current patch version is incompatible with this XmlTools version:\n";
|
---|
| 330 | errMessage += "Patch file name: '" + file + "'\n";
|
---|
| 331 | errMessage += "XmlTools version: " + GlobalVars::AppVersion + "\n" + "CurrPatch version: " + patchVersion + "";
|
---|
| 332 | UtilXmlTools::displayErrorMessage("@XML_TOOLS",errMessage);
|
---|
| 333 | }
|
---|
| 334 | break; // We have got what we wanted
|
---|
| 335 | }
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | if(patchVersion==""){
|
---|
| 339 | UtilXmlTools::displayErrorMessage("@XML_TOOLS","Patch version not found.");
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | void XmlPatch::checkAndProcessValidCommands(QTextStream &fileStream){
|
---|
| 345 |
|
---|
[910] | 346 | QString line, filesWildcard;
|
---|
[906] | 347 | QString xmlToInsert, command, jsCode;
|
---|
[920] | 348 | QString xPathExpression;
|
---|
[906] | 349 | XmlFilter filters;
|
---|
| 350 |
|
---|
| 351 | // Process the rest of the commands in patch file
|
---|
| 352 | while ( !fileStream.atEnd() ){
|
---|
| 353 | line = fileStream.readLine();
|
---|
| 354 |
|
---|
| 355 | if(line.startsWith('#')){ // Ignore comments
|
---|
| 356 | continue;
|
---|
| 357 | }
|
---|
[920] | 358 | else if(line.startsWith("@ADD_INSIDE_NODES")){
|
---|
| 359 | xPathExpression=getPatchParameterValue(line,"XPathExpression");
|
---|
[906] | 360 | filters.setElementName(getPatchParameterValue(line,"ElementName"));
|
---|
| 361 | filters.setParentElementName(getPatchParameterValue(line,"ParentElementName"));
|
---|
| 362 | filters.setAttributeName(getPatchParameterValue(line,"AttributeName"));
|
---|
| 363 | filters.setAttributeValue(getPatchParameterValue(line,"AttributeValue"));
|
---|
[910] | 364 |
|
---|
| 365 | if(this->forceTargetFilesWildcard!=""){
|
---|
| 366 | filesWildcard=this->forceTargetFilesWildcard;
|
---|
[906] | 367 | }
|
---|
| 368 | else{
|
---|
[910] | 369 | filesWildcard=getPatchParameterValue(line,"Files");
|
---|
[906] | 370 | }
|
---|
| 371 |
|
---|
[920] | 372 | // Check options
|
---|
| 373 | if(xPathExpression.isEmpty() && filters.getElementName().isEmpty()){
|
---|
| 374 | UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES","ElementName option or XPathExpression option is required.");
|
---|
| 375 | }
|
---|
| 376 | else if(!xPathExpression.isEmpty() && !filters.getElementName().isEmpty()){
|
---|
[955] | 377 | UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES","ElementName option and XPathExpression option cannot be used simultaneously.");
|
---|
[920] | 378 | }
|
---|
[906] | 379 | if(filters.getAttributeName()!="" && filters.getAttributeValue()==""){
|
---|
[920] | 380 | UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES","AttributeValue option is required if using AttributeName option.");
|
---|
[906] | 381 | }
|
---|
| 382 |
|
---|
| 383 | if(filters.getAttributeValue()!="" && filters.getAttributeName()==""){
|
---|
[920] | 384 | UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES","AttributeName option is required if using AttributeValue option.");
|
---|
[906] | 385 | }
|
---|
| 386 |
|
---|
| 387 | while ( !fileStream.atEnd() && !line.startsWith("</xml>")){
|
---|
| 388 | line = fileStream.readLine();
|
---|
| 389 |
|
---|
| 390 | if(!line.startsWith("<xml>") && !line.startsWith("</xml>")){
|
---|
| 391 | xmlToInsert += line + "\n";
|
---|
| 392 | }
|
---|
| 393 | }
|
---|
| 394 |
|
---|
[920] | 395 | insertNodesOperation(xmlToInsert,filters,xPathExpression,filesWildcard);
|
---|
[906] | 396 |
|
---|
[920] | 397 | xmlToInsert.clear();
|
---|
| 398 | filters.clear();
|
---|
| 399 | xPathExpression.clear();
|
---|
| 400 | filesWildcard.clear();
|
---|
[906] | 401 | }
|
---|
[920] | 402 | else if(line.startsWith("@REMOVE_NODES")){
|
---|
[906] | 403 |
|
---|
[920] | 404 | xPathExpression=getPatchParameterValue(line,"XPathExpression");
|
---|
[906] | 405 | filters.setElementName(getPatchParameterValue(line,"ElementName"));
|
---|
| 406 | filters.setParentElementName(getPatchParameterValue(line,"ParentElementName"));
|
---|
| 407 | filters.setAttributeName(getPatchParameterValue(line,"AttributeName"));
|
---|
| 408 | filters.setAttributeValue(getPatchParameterValue(line,"AttributeValue"));
|
---|
[910] | 409 |
|
---|
| 410 | if(this->forceTargetFilesWildcard!=""){
|
---|
| 411 | filesWildcard=this->forceTargetFilesWildcard;
|
---|
[906] | 412 | }
|
---|
| 413 | else{
|
---|
[910] | 414 | filesWildcard=getPatchParameterValue(line,"Files");
|
---|
[906] | 415 | }
|
---|
| 416 |
|
---|
[920] | 417 | // Check options
|
---|
| 418 | if(xPathExpression.isEmpty() && filters.getElementName().isEmpty()){
|
---|
| 419 | UtilXmlTools::displayErrorMessage("@REMOVE_NODES","ElementName option or XPathExpression option is required.");
|
---|
| 420 | }
|
---|
| 421 | else if(!xPathExpression.isEmpty() && !filters.getElementName().isEmpty()){
|
---|
[955] | 422 | UtilXmlTools::displayErrorMessage("@REMOVE_NODES","ElementName option and XPathExpression option cannot be used simultaneously.");
|
---|
[920] | 423 | }
|
---|
| 424 |
|
---|
[906] | 425 | if(filters.getAttributeName()!="" && filters.getAttributeValue()==""){
|
---|
[920] | 426 | UtilXmlTools::displayErrorMessage("@REMOVE_NODES","AttributeValue option is required if using AttributeName option.");
|
---|
[906] | 427 | }
|
---|
| 428 |
|
---|
| 429 | if(filters.getAttributeValue()!="" && filters.getAttributeName()==""){
|
---|
[920] | 430 | UtilXmlTools::displayErrorMessage("@REMOVE_NODES","AttributeName option is required if using AttributeValue option.");
|
---|
[906] | 431 | }
|
---|
| 432 |
|
---|
[920] | 433 | removeNodesOperation(filters,xPathExpression,filesWildcard);
|
---|
[906] | 434 |
|
---|
[920] | 435 | filters.clear();
|
---|
| 436 | xPathExpression.clear();
|
---|
| 437 | filesWildcard.clear();
|
---|
[906] | 438 | }
|
---|
| 439 | else if(line.startsWith("@COMMAND")){
|
---|
| 440 |
|
---|
[926] | 441 | command=GlobalVars::AppExecutable;
|
---|
[906] | 442 |
|
---|
[926] | 443 | // Append files if forced to
|
---|
| 444 | if(!this->forceTargetFilesWildcard.isEmpty()){
|
---|
| 445 | command+=" --files '"+this->forceTargetFilesWildcard+"' ";
|
---|
| 446 | }
|
---|
| 447 |
|
---|
| 448 | command+=" "+getPatchParameterValue(line,"Options");
|
---|
| 449 |
|
---|
| 450 | // Add --no-backups and --no-verbose if patch was called with that arguments
|
---|
| 451 | if(!this->backupsEnabled){
|
---|
[958] | 452 | command.append(" --no-backups ");
|
---|
[926] | 453 | }
|
---|
| 454 |
|
---|
| 455 | if(!this->verboseEnabled){
|
---|
[958] | 456 | command.append(" --no-verbose ");
|
---|
[926] | 457 | }
|
---|
| 458 |
|
---|
[920] | 459 | command.replace("'","\""); //replace apostrophe by quotes, to avoid problems
|
---|
[960] | 460 | command.replace("\"\"","'"); // this allow to use '' as ' ('' is transformed in "" and then in ')
|
---|
[920] | 461 |
|
---|
[906] | 462 | executeCommandOperation(command);
|
---|
[920] | 463 |
|
---|
| 464 | command.clear();
|
---|
[926] | 465 | filesWildcard.clear();
|
---|
[906] | 466 | }
|
---|
| 467 | else if(line.startsWith("@CUSTOM_CODE")){
|
---|
| 468 |
|
---|
[910] | 469 | if(this->forceTargetFilesWildcard!=""){
|
---|
| 470 | filesWildcard=this->forceTargetFilesWildcard;
|
---|
[906] | 471 | }
|
---|
| 472 | else{
|
---|
[910] | 473 | filesWildcard=getPatchParameterValue(line,"Files");
|
---|
[906] | 474 | }
|
---|
| 475 |
|
---|
| 476 | while ( !fileStream.atEnd() && !line.startsWith("</code>")){
|
---|
| 477 |
|
---|
| 478 | line = fileStream.readLine();
|
---|
| 479 |
|
---|
| 480 | if(!line.startsWith("<code>") && !line.startsWith("</code>")){
|
---|
| 481 | jsCode += line + "\n";
|
---|
| 482 | }
|
---|
| 483 | }
|
---|
| 484 |
|
---|
| 485 | executeCustomCommandOperation(jsCode,filesWildcard);
|
---|
[920] | 486 |
|
---|
| 487 | jsCode.clear();
|
---|
| 488 | filesWildcard.clear();
|
---|
[906] | 489 | }
|
---|
| 490 | }
|
---|
| 491 | }
|
---|
| 492 |
|
---|
| 493 | QString XmlPatch::getPatchParameterValue(const QString& line, QString parameter){
|
---|
| 494 |
|
---|
| 495 | int startValueIdx, endValueIdx;
|
---|
| 496 |
|
---|
[920] | 497 | parameter=" "+parameter+" "; // Parameters include a space before and after it's value.
|
---|
[906] | 498 |
|
---|
| 499 | if(!line.contains(parameter)){
|
---|
[920] | 500 | if(parameter==" ParentElementName " || parameter==" AttributeName " || parameter==" AttributeValue "
|
---|
| 501 | || parameter==" ElementName " || parameter==" XPathExpression "){
|
---|
[906] | 502 | return ""; // not mandatory
|
---|
| 503 | }
|
---|
| 504 | parameter.remove(" "); // just remove the space added so it doesn't look weird when outputted to the user
|
---|
| 505 |
|
---|
[955] | 506 | UtilXmlTools::displayErrorMessage("Read patch file parameter","Couldn't retrieve '" + parameter + "' parameter.");
|
---|
[906] | 507 | }
|
---|
| 508 |
|
---|
| 509 | startValueIdx=line.indexOf(parameter); // get the position where parameter is defined
|
---|
| 510 | endValueIdx=line.indexOf("\"",startValueIdx)+1; // get the position where parameter value begins (+1 to ignore mandotory quote)
|
---|
| 511 |
|
---|
| 512 | startValueIdx=endValueIdx;
|
---|
| 513 | endValueIdx=line.indexOf("\"",startValueIdx); // get the last mandatory quote of the value
|
---|
| 514 |
|
---|
| 515 | return line.mid(startValueIdx,endValueIdx-startValueIdx); // return the value of the parameter (is in the middle of the mandatory quotes)
|
---|
| 516 | }
|
---|
| 517 |
|
---|
| 518 | void XmlPatch::displayJsException(QScriptEngine &engine, QScriptValue &engineResult){
|
---|
| 519 | if (engine.hasUncaughtException()) {
|
---|
| 520 | UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Uncaught js exception (user code) at line " +QString::number(engine.uncaughtExceptionLineNumber()) + ":\n" + engineResult.toString());
|
---|
| 521 | }
|
---|
| 522 | }
|
---|