Changeset 920 for XmlTools2/trunk
- Timestamp:
- Feb 2, 2014, 7:50:10 PM (11 years ago)
- Location:
- XmlTools2/trunk
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
XmlTools2/trunk/main.cpp
r906 r920 12 12 parser.setApplicationDescription("Additional documentation can be found at: http://wiki.oni2.net/XmlTools"); 13 13 14 XmlTools *myXmlTools; 14 15 QString filesWildCard, patchFilesWildCard, forceTargetFilesWildcard; 15 16 QString currentVal, newVal, diffOldNewVal, positions; 17 QString xPathExpression; 16 18 XmlFilter filters; // Filters 19 17 20 bool noBackups=false; 18 19 21 20 22 QCommandLineOption addValuesOption(QStringList() << "a" << "add-values", "Add values to a set of XML elements."); … … 26 28 27 29 QCommandLineOption currentValOption(QStringList() << "c" << "current-val", "Current value(s) [use space as separator].","current-val"); 28 QCommandLineOption newValOption(QStringList() << "n" << "new-val", "New value(s) [use space as separator] ","new-val.");30 QCommandLineOption newValOption(QStringList() << "n" << "new-val", "New value(s) [use space as separator].","new-val"); 29 31 QCommandLineOption diffOldNewValueOption(QStringList() << "d" << "diff-old-new-val", "Difference between old and new value.","diff-old-new-val"); 30 32 QCommandLineOption positionsValueOption(QStringList() << "positions", "Positions [use space as separator] [0-index based].","positions"); … … 37 39 QCommandLineOption attributeNameOption("attribute-name", "Attribute name of <element-name> [used as filter].", "attribute-name"); 38 40 QCommandLineOption attributeValueOption("attribute-value", "Attribute value of <attribute-name> [used as filter].", "attribute-value"); 41 QCommandLineOption xPathExpressionOption(QStringList() << "x" << "xpath-expression", "XPath 1.0 expression to select elements where processing will occur.", "xpath-expression"); 39 42 QCommandLineOption noBackupsOption(QStringList() << "no-backups", "No backups [faster processing]."); 40 43 … … 58 61 parser.addOption(attributeNameOption); 59 62 parser.addOption(attributeValueOption); 63 parser.addOption(xPathExpressionOption); 60 64 parser.addOption(noBackupsOption); 61 65 … … 71 75 } 72 76 73 // Check if the user doesn't want backups (it boost XmlTools peformance, lower disk output)77 // Check if the user doesn't want backups (it boosts XmlTools peformance, lower disk output) 74 78 if(parser.isSet(noBackupsOption)){ 75 79 noBackups=true; … … 94 98 } 95 99 100 if(!parser.isSet(elementNameOption) && !parser.isSet(xPathExpressionOption)){ 101 UtilXmlTools::displayErrorMessage("Parameter Parsing","element-name option or xpath-expression option is required if not using patch-files option."); 102 } 103 else if(parser.isSet(elementNameOption) && parser.isSet(xPathExpressionOption)){ 104 UtilXmlTools::displayErrorMessage("Parameter Parsing","element-name option and xpath-expression options cannot be used simultaneously."); 105 } 106 96 107 // Get element name if available 97 108 if(parser.isSet(elementNameOption)){ … … 99 110 } 100 111 101 if(filters.getElementName()==""){ 102 UtilXmlTools::displayErrorMessage("Parameter Parsing","element-name option is required if not using patch-files option."); 112 // Get xpath expression if available 113 if(parser.isSet(xPathExpressionOption)){ 114 xPathExpression=parser.value(xPathExpressionOption); 103 115 } 104 116 … … 155 167 } 156 168 157 XmlTools myXmlTools(filesWildCard,filters,noBackups); 169 if(parser.isSet(elementNameOption)){ 170 myXmlTools=new XmlTools(filesWildCard,filters,noBackups); 171 } 172 else{ 173 myXmlTools=new XmlTools(filesWildCard,xPathExpression,noBackups); 174 } 158 175 159 176 … … 165 182 } 166 183 167 myXmlTools .addValues(newVal);184 myXmlTools->addValues(newVal); 168 185 } 169 186 else if(parser.isSet(removeValuesOption)){ // Or remove-values option? … … 173 190 } 174 191 175 myXmlTools .removeValues(currentVal);192 myXmlTools->removeValues(currentVal); 176 193 } 177 194 else if(parser.isSet(replaceValueOption)){ // Or replace-value option? … … 181 198 } 182 199 183 myXmlTools .replaceValue(currentVal,newVal);200 myXmlTools->replaceValue(currentVal,newVal); 184 201 } 185 202 else if(parser.isSet(replaceAllValuesOption)){ // Or replace-all-values option? … … 190 207 } 191 208 192 myXmlTools .replaceAll(newVal,positions);209 myXmlTools->replaceAll(newVal,positions); 193 210 } 194 211 else if(parser.isSet(updateElementsOption)){ // Or update-elements option? … … 199 216 } 200 217 201 myXmlTools .updateElements(diffOldNewVal);218 myXmlTools->updateElements(diffOldNewVal); 202 219 } 203 220 else if(parser.isSet(invertElementsOption)){ // Or invert-elements option? 204 myXmlTools .invertElements();221 myXmlTools->invertElements(); 205 222 } 206 223 else{ … … 209 226 } 210 227 228 //delete myXmlTools; 211 229 //std::cout << "Started" << std::endl; 212 230 -
XmlTools2/trunk/readme.txt
r906 r920 35 35 36 36 -Remove XML from existing files (patch only). 37 38 -Select elements using element name, parent element name, attribute name/value and XPath 1.0. 37 39 38 40 -Powerful custom XML editing using javascript (for what you can't do with native operations) (patch only). … … 75 77 REMOVE -> REMOVE_NODE 76 78 CUSTOMCODE -> CUSTOM_CODE 79 -Added option to select xml elements by attribute name and value 80 -Added option to select xml elements by XPath 1.0 (should reduce the need of javascript) -
XmlTools2/trunk/util.cpp
r906 r920 3 3 namespace GlobalVars{ 4 4 QString AppName="XmlTools"; 5 #ifdef Q_OS_WIN 6 QString AppExecutable=AppName+".exe"; 7 #else 8 QString AppExecutable="./"+AppName; // Mac OS needs unix like executing 9 #endif 5 10 QString AppVersion="2.0"; 6 11 } -
XmlTools2/trunk/util.h
r906 r920 10 10 namespace GlobalVars{ 11 11 extern QString AppName; 12 extern QString AppExecutable; 12 13 extern QString AppVersion; 13 14 } -
XmlTools2/trunk/utilxmltools.cpp
r906 r920 55 55 } 56 56 57 void getAllXpathElements(const QString &xPathExpression, pugi::xml_document &doc, QList<pugi::xml_node> &result){ 58 59 pugi::xpath_node_set selectedNodes; 60 pugi::xpath_node node; 61 62 try 63 { 64 selectedNodes = doc.select_nodes(xPathExpression.toLatin1().constData()); 65 } 66 67 catch (const pugi::xpath_exception& e) 68 { 69 displayErrorMessage("XPath element selection","Selection of elements using the XPathExpression: '" + xPathExpression + "' failed:\n" + e.what()); 70 } 71 72 for (pugi::xpath_node_set::const_iterator currNode = selectedNodes.begin(); currNode != selectedNodes.end(); ++currNode) 73 { 74 node = *currNode; 75 if(node){ // if node != null 76 result << node.node(); 77 } 78 } 79 80 if(result.isEmpty()){ 81 result << pugi::xml_node(); // add an empty node if none found 82 } 83 84 } 85 86 pugi::xml_node getFirstXpathElement(const QString &xPathExpression, pugi::xml_document &doc){ 87 pugi::xpath_node selectedNode; 88 89 try 90 { 91 selectedNode = doc.select_single_node(xPathExpression.toLatin1().constData()); 92 } 93 94 catch (const pugi::xpath_exception& e) 95 { 96 displayErrorMessage("XPath element selection","Selection of element using the XPathExpression: '" + xPathExpression + "' failed:\n" + e.what()); 97 } 98 99 return selectedNode.node(); 100 } 101 57 102 void getAllNamedElements(pugi::xml_node &node, QList<pugi::xml_node> &result, XmlFilter &filters){ 58 103 for (pugi::xml_node_iterator currNode = node.begin(); currNode != node.end(); ++currNode) … … 70 115 } 71 116 72 pugi::xml_node getFirstNamedElement s(pugi::xml_node &node, XmlFilter &filters){117 pugi::xml_node getFirstNamedElement(pugi::xml_node &node, XmlFilter &filters){ 73 118 74 119 pugi::xml_node foundNode; … … 82 127 } 83 128 84 foundNode=getFirstNamedElement s(*currNode,filters);129 foundNode=getFirstNamedElement(*currNode,filters); 85 130 86 131 if(foundNode.type()!=pugi::node_null){ -
XmlTools2/trunk/utilxmltools.h
r906 r920 14 14 void backupFile(const QString &file); 15 15 void getAllNamedElements(pugi::xml_node &node, QList<pugi::xml_node> &result, XmlFilter &filters); 16 void getAllXpathElements(const QString &xPathExpression, pugi::xml_document &doc, QList<pugi::xml_node> &result); 16 17 void displaySuccessMessage(const int numberOperations, const QString &operation); 17 18 void displayErrorMessage(const QString& operation, const QString &message, bool exitProgram=true); 18 pugi::xml_node getFirstNamedElements(pugi::xml_node &node, XmlFilter &filters); 19 pugi::xml_node getFirstNamedElement(pugi::xml_node &node, XmlFilter &filters); 20 pugi::xml_node getFirstXpathElement(const QString &xPathExpression, pugi::xml_document &doc); 19 21 20 22 //// inline functions -
XmlTools2/trunk/xmlfilter.cpp
r906 r920 46 46 this->attributeValue=attributeValue; 47 47 } 48 49 // Clears filter 50 void XmlFilter::clear(){ 51 this->elementName.clear(); 52 this->parentElementName.clear(); 53 this->attributeName.clear(); 54 this->attributeValue.clear(); 55 } -
XmlTools2/trunk/xmlfilter.h
r906 r920 21 21 void setAttributeName(QString attributeName); 22 22 void setAttributeValue(QString attributeValue); 23 24 void clear(); 23 25 private: 24 26 QString elementName; -
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 } -
XmlTools2/trunk/xmlpatch.h
r906 r920 16 16 bool backupsEnabled; 17 17 QString getPatchParameterValue(const QString& line, QString parameter); 18 void addToOperation(const QString &xmlString, XmlFilter &filters, const QString &filesWildcard="");19 void removeNode Operation(XmlFilter &filters, const QString &filesWildcard="");18 void insertNodesOperation(const QString &xmlString, XmlFilter &filters, const QString &xPathExpression, const QString &filesWildcard=""); 19 void removeNodesOperation(XmlFilter &filters, const QString &xPathExpression, const QString &filesWildcard=""); 20 20 void executeCommandOperation(const QString &commandString); 21 21 void executeCustomCommandOperation(const QString &jsString, const QString &filesWildcard=""); -
XmlTools2/trunk/xmltools.cpp
r910 r920 1 1 #include "xmltools.h" 2 2 3 // Filters constructor 3 4 XmlTools::XmlTools(QString filesWildcard, XmlFilter filter, bool noBackups) 4 5 { … … 12 13 } 13 14 15 // XPath constructor 16 XmlTools::XmlTools(QString filesWildcard, QString xPathExpression, bool noBackups) 17 { 18 this->filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard); 19 this->xPathExpression=xPathExpression; 20 this->backupsEnabled=!noBackups; 21 } 22 14 23 // Adds new values to an element 15 24 void XmlTools::addValues(QString newValues){ … … 24 33 25 34 newValuesList=Util::qStringListFromSpacedString(newValues); 26 UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); 35 36 // Check how the elements will be fetched via element name or xpath expression 37 if(this->xPathExpression==""){ 38 UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); 39 } 40 else{ 41 UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements); 42 } 27 43 28 44 for(int j=0; j<elements.size(); j++){ … … 57 73 UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, "remove-values"); 58 74 59 UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); 75 // Check how the elements will be fetched via element name or xpath expression 76 if(this->xPathExpression==""){ 77 UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); 78 } 79 else{ 80 UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements); 81 } 60 82 61 83 valuesToRemoveList=Util::qStringListFromSpacedString(valuesToRemove); … … 98 120 UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, "replace-value"); 99 121 100 UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); 122 // Check how the elements will be fetched via element name or xpath expression 123 if(this->xPathExpression==""){ 124 UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); 125 } 126 else{ 127 UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements); 128 } 101 129 102 130 for(int j=0; j<elements.size(); j++){ … … 132 160 UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, "replace-all"); 133 161 134 UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); 162 // Check how the elements will be fetched via element name or xpath expression 163 if(this->xPathExpression==""){ 164 UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); 165 } 166 else{ 167 UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements); 168 } 135 169 136 170 … … 166 200 UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, "update-elements"); 167 201 168 UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); 202 // Check how the elements will be fetched via element name or xpath expression 203 if(this->xPathExpression==""){ 204 UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); 205 } 206 else{ 207 UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements); 208 } 169 209 170 210 … … 207 247 QStringList invertedElements; //Inverting the element order 208 248 209 UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); 249 // Check how the elements will be fetched via element name or xpath expression 250 if(this->xPathExpression==""){ 251 UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); 252 } 253 else{ 254 UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements); 255 } 210 256 211 257 // Read all elements and save to the list -
XmlTools2/trunk/xmltools.h
r906 r920 26 26 public: 27 27 XmlTools(QString filesWildcard, XmlFilter filter, bool noBackups); 28 XmlTools(QString filesWildcard, QString xPathExpression, bool noBackups); 28 29 void addValues(QString newValues); 29 30 void removeValues(QString valuesToRemove); … … 37 38 pugi::xml_node rootNode; 38 39 QStringList filesToProcess; 40 QString xPathExpression; 39 41 XmlFilter filters; 40 42 bool backupsEnabled;
Note:
See TracChangeset
for help on using the changeset viewer.