Changeset 920 for XmlTools2/trunk/xmlpatch.cpp
- Timestamp:
- Feb 2, 2014, 7:50:10 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
XmlTools2/trunk/xmlpatch.cpp
r910 r920 44 44 } 45 45 46 void XmlPatch:: addToOperation(const QString &xmlString, XmlFilter &filters, const QString &filesWildcard){46 void XmlPatch::insertNodesOperation(const QString &xmlString, XmlFilter &filters, const QString &xPathExpression, const QString &filesWildcard){ 47 47 48 48 QStringList filesToProcess; 49 pugi::xml_node nodeToInsertion;49 QList<pugi::xml_node> nodesToInsertion; 50 50 pugi::xml_document newNode; 51 51 pugi::xml_parse_result result; … … 54 54 55 55 if(filesToProcess.isEmpty()){ 56 UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODE ","No XML files were found for the wildcard: "+filesWildcard);56 UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES","No XML files were found for the wildcard: "+filesWildcard); 57 57 } 58 58 … … 60 60 61 61 if(result.status!=pugi::status_ok){ 62 UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODE ", "The xml node to insert is invalid.\n" + Util::toQString(result.description()));62 UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES", "The xml node to insert is invalid.\n" + Util::toQString(result.description())); 63 63 } 64 64 … … 66 66 for(int i=0; i<filesToProcess.size(); i++){ 67 67 68 UtilXmlTools::loadXmlFile(filesToProcess[i],this->document,this->rootNode,this->backupsEnabled,"@ADD_INSIDE_NODE"); 69 70 nodeToInsertion=UtilXmlTools::getFirstNamedElements(this->rootNode,filters); 71 72 if(nodeToInsertion.type()==pugi::node_null){ 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){ 73 79 74 80 QString errMessage; 75 81 76 errMessage = "It wasn't found a node with a ElementName: '" + filters.getElementName() + "'"; 77 if(filters.getParentElementName()!=""){ 78 errMessage += " and a ParentElementName: '" + filters.getParentElementName() + "'"; 79 } 80 if(filters.getAttributeName()!=""){ 81 errMessage += " and an AttributeName: '" + filters.getAttributeName() + "' and an AttributeValue: '" + filters.getAttributeValue() + "'"; 82 } 83 84 UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODE",errMessage); 85 } 86 87 nodeToInsertion.prepend_copy(newNode.first_child()); // append the new node 88 89 90 UtilXmlTools::saveXmlFile(filesToProcess[i],this->document, "@ADD_INSIDE_NODE"); 91 } 92 93 UtilXmlTools::displaySuccessMessage(filesToProcess.size(),"@ADD_INSIDE_NODE"); 94 } 95 96 void XmlPatch::removeNodeOperation(XmlFilter &filters, const QString &filesWildcard){ 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){ 97 110 98 111 QStringList filesToProcess; 99 112 100 pugi::xml_node nodeToDeletion;113 QList<pugi::xml_node> nodesToDeletion; 101 114 102 115 filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard); 103 116 104 117 if(filesToProcess.isEmpty()){ 105 UtilXmlTools::displayErrorMessage("@REMOVE_NODE ","No XML files were found for the wildcard: "+filesWildcard);118 UtilXmlTools::displayErrorMessage("@REMOVE_NODES","No XML files were found for the wildcard: "+filesWildcard); 106 119 } 107 120 … … 109 122 for(int i=0; i<filesToProcess.size(); i++){ 110 123 111 UtilXmlTools::loadXmlFile(filesToProcess[i],this->document,this->rootNode,this->backupsEnabled,"@REMOVE_NODE"); 112 113 nodeToDeletion=UtilXmlTools::getFirstNamedElements(this->rootNode,filters); 114 115 if(nodeToDeletion.type()==pugi::node_null){ 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 116 136 QString errMessage; 117 137 118 errMessage = "It wasn't found a node with a ElementName: '" + filters.getElementName() + "'"; 119 if(filters.getParentElementName()!=""){ 120 errMessage += " and a ParentElementName: '" + filters.getParentElementName() + "'"; 121 } 122 if(filters.getAttributeName()!=""){ 123 errMessage += " and an AttributeName: '" + filters.getAttributeName() + "' and an AttributeValue: '" + filters.getAttributeValue() + "'"; 124 } 125 126 UtilXmlTools::displayErrorMessage("@REMOVE_NODE",errMessage); 127 } 128 129 if(!nodeToDeletion.parent().remove_child(nodeToDeletion)){ // remove the node 130 131 QString errMessage; 132 133 errMessage = "Couldn't remove the node with Element '" + filters.getElementName() + "'"; 134 135 if(filters.getParentElementName()!=""){ 136 errMessage += " and a ParentElement: '" + filters.getParentElementName() + "'"; 137 } 138 139 UtilXmlTools::displayErrorMessage("@REMOVE_NODE",errMessage); 140 } 141 142 UtilXmlTools::saveXmlFile(filesToProcess[i],this->document, "@REMOVE_NODE"); 143 } 144 145 UtilXmlTools::displaySuccessMessage(filesToProcess.size(), "@REMOVE_NODE"); 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"); 146 178 } 147 179 … … 152 184 153 185 // Avoid infinite fork loops 154 if(commandString.contains(" -p") || commandString.contains("--patch-files")){186 if(commandString.contains(" -p ") || commandString.contains(" --patch-files ")){ 155 187 UtilXmlTools::displayErrorMessage("@COMMAND","Use of --patch-files option is not allowed inside a patch file"); 156 188 } 157 189 158 newXmlToolsInstance.start(GlobalVars::App Name + " " + commandString);190 newXmlToolsInstance.start(GlobalVars::AppExecutable + " " + commandString); 159 191 newXmlToolsInstance.waitForFinished(-1); // wait for new instance to finish 160 192 … … 166 198 } 167 199 168 std::cout << "@COMMAND patch operation output:\n" << resultOutput.toLatin1().constData() << std::endl; 200 std::cout << "@COMMAND patch operation output:\n" 201 << "########################################################################\n" 202 << resultOutput.toLatin1().constData() 203 << "########################################################################" 204 << std::endl; 169 205 170 206 UtilXmlTools::displaySuccessMessage(1,"@COMMAND"); … … 289 325 QString line, filesWildcard; 290 326 QString xmlToInsert, command, jsCode; 327 QString xPathExpression; 291 328 XmlFilter filters; 292 329 … … 298 335 continue; 299 336 } 300 else if(line.startsWith("@ADD_INSIDE_NODE")){ 337 else if(line.startsWith("@ADD_INSIDE_NODES")){ 338 xPathExpression=getPatchParameterValue(line,"XPathExpression"); 301 339 filters.setElementName(getPatchParameterValue(line,"ElementName")); 302 340 filters.setParentElementName(getPatchParameterValue(line,"ParentElementName")); … … 311 349 } 312 350 313 // Check attribute filters 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 } 314 358 if(filters.getAttributeName()!="" && filters.getAttributeValue()==""){ 315 UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODE ","attribute-value option is required if using attribute-name option.");359 UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES","AttributeValue option is required if using AttributeName option."); 316 360 } 317 361 318 362 if(filters.getAttributeValue()!="" && filters.getAttributeName()==""){ 319 UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODE ","attribute-name option is required if using attribute-value option.");363 UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODES","AttributeName option is required if using AttributeValue option."); 320 364 } 321 365 … … 328 372 } 329 373 330 addToOperation(xmlToInsert,filters,filesWildcard); 331 332 } 333 else if(line.startsWith("@REMOVE_NODE")){ 334 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"); 335 384 filters.setElementName(getPatchParameterValue(line,"ElementName")); 336 385 filters.setParentElementName(getPatchParameterValue(line,"ParentElementName")); … … 345 394 } 346 395 347 // Check attribute filters 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 348 404 if(filters.getAttributeName()!="" && filters.getAttributeValue()==""){ 349 UtilXmlTools::displayErrorMessage("@REMOVE_NODE ","attribute-value option is required if using attribute-name option.");405 UtilXmlTools::displayErrorMessage("@REMOVE_NODES","AttributeValue option is required if using AttributeName option."); 350 406 } 351 407 352 408 if(filters.getAttributeValue()!="" && filters.getAttributeName()==""){ 353 UtilXmlTools::displayErrorMessage("@REMOVE_NODE","attribute-name option is required if using attribute-value option."); 354 } 355 356 removeNodeOperation(filters,filesWildcard); 357 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(); 358 417 } 359 418 else if(line.startsWith("@COMMAND")){ 360 419 361 command=getPatchParameterValue(line,""); 420 command=getPatchParameterValue(line,"Arguments"); 421 422 command.replace("'","\""); //replace apostrophe by quotes, to avoid problems 362 423 363 424 executeCommandOperation(command); 425 426 command.clear(); 364 427 } 365 428 else if(line.startsWith("@CUSTOM_CODE")){ … … 382 445 383 446 executeCustomCommandOperation(jsCode,filesWildcard); 447 448 jsCode.clear(); 449 filesWildcard.clear(); 384 450 } 385 451 } … … 390 456 int startValueIdx, endValueIdx; 391 457 392 parameter +=" "; // Parameters include a space beforeit's value.458 parameter=" "+parameter+" "; // Parameters include a space before and after it's value. 393 459 394 460 if(!line.contains(parameter)){ 395 if(parameter=="ParentElementName " || parameter=="AttributeName " || parameter=="AttributeValue "){ 461 if(parameter==" ParentElementName " || parameter==" AttributeName " || parameter==" AttributeValue " 462 || parameter==" ElementName " || parameter==" XPathExpression "){ 396 463 return ""; // not mandatory 397 464 }
Note:
See TracChangeset
for help on using the changeset viewer.