Changeset 920 for XmlTools2/trunk


Ignore:
Timestamp:
Feb 2, 2014, 7:50:10 PM (11 years ago)
Author:
s10k
Message:

more fixes and updated examples

Location:
XmlTools2/trunk
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • XmlTools2/trunk/main.cpp

    r906 r920  
    1212    parser.setApplicationDescription("Additional documentation can be found at: http://wiki.oni2.net/XmlTools");
    1313
     14    XmlTools *myXmlTools;
    1415    QString filesWildCard, patchFilesWildCard, forceTargetFilesWildcard;
    1516    QString currentVal, newVal, diffOldNewVal, positions;
     17    QString xPathExpression;
    1618    XmlFilter filters; // Filters
     19
    1720    bool noBackups=false;
    18 
    1921
    2022    QCommandLineOption addValuesOption(QStringList() << "a" << "add-values", "Add values to a set of XML elements.");
     
    2628
    2729    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");
    2931    QCommandLineOption diffOldNewValueOption(QStringList() << "d" << "diff-old-new-val", "Difference between old and new value.","diff-old-new-val");
    3032    QCommandLineOption positionsValueOption(QStringList() << "positions", "Positions [use space as separator] [0-index based].","positions");
     
    3739    QCommandLineOption attributeNameOption("attribute-name", "Attribute name of <element-name>  [used as filter].", "attribute-name");
    3840    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");
    3942    QCommandLineOption noBackupsOption(QStringList()  << "no-backups", "No backups [faster processing].");
    4043
     
    5861    parser.addOption(attributeNameOption);
    5962    parser.addOption(attributeValueOption);
     63    parser.addOption(xPathExpressionOption);
    6064    parser.addOption(noBackupsOption);
    6165
     
    7175    }
    7276
    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)
    7478    if(parser.isSet(noBackupsOption)){
    7579        noBackups=true;
     
    9498    }
    9599
     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
    96107    // Get element name if available
    97108    if(parser.isSet(elementNameOption)){
     
    99110    }
    100111
    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);
    103115    }
    104116
     
    155167    }
    156168
    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    }
    158175
    159176
     
    165182        }
    166183
    167         myXmlTools.addValues(newVal);
     184        myXmlTools->addValues(newVal);
    168185    }
    169186    else if(parser.isSet(removeValuesOption)){ // Or remove-values option?
     
    173190        }
    174191
    175         myXmlTools.removeValues(currentVal);
     192        myXmlTools->removeValues(currentVal);
    176193    }
    177194    else if(parser.isSet(replaceValueOption)){ // Or replace-value option?
     
    181198        }
    182199
    183         myXmlTools.replaceValue(currentVal,newVal);
     200        myXmlTools->replaceValue(currentVal,newVal);
    184201    }
    185202    else if(parser.isSet(replaceAllValuesOption)){ // Or replace-all-values option?
     
    190207        }
    191208
    192         myXmlTools.replaceAll(newVal,positions);
     209        myXmlTools->replaceAll(newVal,positions);
    193210    }
    194211    else if(parser.isSet(updateElementsOption)){ // Or update-elements option?
     
    199216        }
    200217
    201         myXmlTools.updateElements(diffOldNewVal);
     218        myXmlTools->updateElements(diffOldNewVal);
    202219    }
    203220    else if(parser.isSet(invertElementsOption)){ // Or invert-elements option?
    204         myXmlTools.invertElements();
     221        myXmlTools->invertElements();
    205222    }
    206223    else{
     
    209226    }
    210227
     228    //delete myXmlTools;
    211229    //std::cout << "Started" << std::endl;
    212230
  • XmlTools2/trunk/readme.txt

    r906 r920  
    3535
    3636-Remove XML from existing files (patch only).
     37
     38-Select elements using element name, parent element name, attribute name/value and XPath 1.0.
    3739
    3840-Powerful custom XML editing using javascript (for what you can't do with native operations) (patch only).
     
    7577REMOVE -> REMOVE_NODE
    7678CUSTOMCODE -> 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  
    33namespace GlobalVars{
    44QString AppName="XmlTools";
     5#ifdef Q_OS_WIN
     6QString AppExecutable=AppName+".exe";
     7#else
     8QString AppExecutable="./"+AppName; // Mac OS needs unix like executing
     9#endif
    510QString AppVersion="2.0";
    611}
  • XmlTools2/trunk/util.h

    r906 r920  
    1010namespace GlobalVars{
    1111extern QString AppName;
     12extern QString AppExecutable;
    1213extern QString AppVersion;
    1314}
  • XmlTools2/trunk/utilxmltools.cpp

    r906 r920  
    5555}
    5656
     57void 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
     86pugi::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
    57102void getAllNamedElements(pugi::xml_node &node, QList<pugi::xml_node> &result, XmlFilter &filters){
    58103    for (pugi::xml_node_iterator currNode = node.begin(); currNode != node.end(); ++currNode)
     
    70115}
    71116
    72 pugi::xml_node getFirstNamedElements(pugi::xml_node &node, XmlFilter &filters){
     117pugi::xml_node getFirstNamedElement(pugi::xml_node &node, XmlFilter &filters){
    73118
    74119    pugi::xml_node foundNode;
     
    82127        }
    83128
    84         foundNode=getFirstNamedElements(*currNode,filters);
     129        foundNode=getFirstNamedElement(*currNode,filters);
    85130
    86131        if(foundNode.type()!=pugi::node_null){
  • XmlTools2/trunk/utilxmltools.h

    r906 r920  
    1414void backupFile(const QString &file);
    1515void getAllNamedElements(pugi::xml_node &node, QList<pugi::xml_node> &result, XmlFilter &filters);
     16void getAllXpathElements(const QString &xPathExpression, pugi::xml_document &doc, QList<pugi::xml_node> &result);
    1617void displaySuccessMessage(const int numberOperations, const QString &operation);
    1718void displayErrorMessage(const QString& operation, const QString &message, bool exitProgram=true);
    18 pugi::xml_node getFirstNamedElements(pugi::xml_node &node, XmlFilter &filters);
     19pugi::xml_node getFirstNamedElement(pugi::xml_node &node, XmlFilter &filters);
     20pugi::xml_node getFirstXpathElement(const QString &xPathExpression, pugi::xml_document &doc);
    1921
    2022//// inline functions
  • XmlTools2/trunk/xmlfilter.cpp

    r906 r920  
    4646    this->attributeValue=attributeValue;
    4747}
     48
     49// Clears filter
     50void 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  
    2121    void setAttributeName(QString attributeName);
    2222    void setAttributeValue(QString attributeValue);
     23
     24    void clear();
    2325private:
    2426    QString elementName;
  • XmlTools2/trunk/xmlpatch.cpp

    r910 r920  
    4444}
    4545
    46 void XmlPatch::addToOperation(const QString &xmlString, XmlFilter &filters, const QString &filesWildcard){
     46void XmlPatch::insertNodesOperation(const QString &xmlString, XmlFilter &filters, const QString &xPathExpression, const QString &filesWildcard){
    4747
    4848    QStringList filesToProcess;
    49     pugi::xml_node nodeToInsertion;
     49    QList<pugi::xml_node> nodesToInsertion;
    5050    pugi::xml_document newNode;
    5151    pugi::xml_parse_result result;
     
    5454
    5555    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);
    5757    }
    5858
     
    6060
    6161    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()));
    6363    }
    6464
     
    6666    for(int i=0; i<filesToProcess.size(); i++){
    6767
    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){
    7379
    7480            QString errMessage;
    7581
    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
     109void XmlPatch::removeNodesOperation(XmlFilter &filters, const QString &xPathExpression, const QString &filesWildcard){
    97110
    98111    QStringList filesToProcess;
    99112
    100     pugi::xml_node nodeToDeletion;
     113    QList<pugi::xml_node> nodesToDeletion;
    101114
    102115    filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard);
    103116
    104117    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);
    106119    }
    107120
     
    109122    for(int i=0; i<filesToProcess.size(); i++){
    110123
    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
    116136            QString errMessage;
    117137
    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");
    146178}
    147179
     
    152184
    153185    // Avoid infinite fork loops
    154     if(commandString.contains("-p") || commandString.contains("--patch-files")){
     186    if(commandString.contains(" -p ") || commandString.contains(" --patch-files ")){
    155187        UtilXmlTools::displayErrorMessage("@COMMAND","Use of --patch-files option is not allowed inside a patch file");
    156188    }
    157189
    158     newXmlToolsInstance.start(GlobalVars::AppName + " " + commandString);
     190    newXmlToolsInstance.start(GlobalVars::AppExecutable + " " + commandString);
    159191    newXmlToolsInstance.waitForFinished(-1); // wait for new instance to finish
    160192
     
    166198    }
    167199
    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;
    169205
    170206    UtilXmlTools::displaySuccessMessage(1,"@COMMAND");
     
    289325    QString line, filesWildcard;
    290326    QString xmlToInsert, command, jsCode;
     327    QString xPathExpression;
    291328    XmlFilter filters;
    292329
     
    298335            continue;
    299336        }
    300         else if(line.startsWith("@ADD_INSIDE_NODE")){
     337        else if(line.startsWith("@ADD_INSIDE_NODES")){
     338            xPathExpression=getPatchParameterValue(line,"XPathExpression");
    301339            filters.setElementName(getPatchParameterValue(line,"ElementName"));
    302340            filters.setParentElementName(getPatchParameterValue(line,"ParentElementName"));
     
    311349            }
    312350
    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            }
    314358            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.");
    316360            }
    317361
    318362            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.");
    320364            }
    321365
     
    328372            }
    329373
    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");
    335384            filters.setElementName(getPatchParameterValue(line,"ElementName"));
    336385            filters.setParentElementName(getPatchParameterValue(line,"ParentElementName"));
     
    345394            }
    346395
    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
    348404            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.");
    350406            }
    351407
    352408            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();
    358417        }
    359418        else if(line.startsWith("@COMMAND")){
    360419
    361             command=getPatchParameterValue(line,"");
     420            command=getPatchParameterValue(line,"Arguments");
     421
     422            command.replace("'","\""); //replace apostrophe by quotes, to avoid problems
    362423
    363424            executeCommandOperation(command);
     425
     426            command.clear();
    364427        }
    365428        else if(line.startsWith("@CUSTOM_CODE")){
     
    382445
    383446            executeCustomCommandOperation(jsCode,filesWildcard);
     447
     448            jsCode.clear();
     449            filesWildcard.clear();
    384450        }
    385451    }
     
    390456    int startValueIdx, endValueIdx;
    391457
    392     parameter+=" "; // Parameters include a space before it's value.
     458    parameter=" "+parameter+" "; // Parameters include a space before and after it's value.
    393459
    394460    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 "){
    396463            return ""; // not mandatory
    397464        }
  • XmlTools2/trunk/xmlpatch.h

    r906 r920  
    1616    bool backupsEnabled;
    1717    QString getPatchParameterValue(const QString& line, QString parameter);
    18     void addToOperation(const QString &xmlString, XmlFilter &filters, const QString &filesWildcard="");
    19     void removeNodeOperation(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="");
    2020    void executeCommandOperation(const QString &commandString);
    2121    void executeCustomCommandOperation(const QString &jsString, const QString &filesWildcard="");
  • XmlTools2/trunk/xmltools.cpp

    r910 r920  
    11#include "xmltools.h"
    22
     3// Filters constructor
    34XmlTools::XmlTools(QString filesWildcard, XmlFilter filter, bool noBackups)
    45{
     
    1213}
    1314
     15// XPath constructor
     16XmlTools::XmlTools(QString filesWildcard, QString xPathExpression, bool noBackups)
     17{
     18    this->filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard);
     19    this->xPathExpression=xPathExpression;
     20    this->backupsEnabled=!noBackups;
     21}
     22
    1423// Adds new values to an element
    1524void XmlTools::addValues(QString newValues){
     
    2433
    2534        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        }
    2743
    2844        for(int j=0; j<elements.size(); j++){
     
    5773        UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, "remove-values");
    5874
    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        }
    6082
    6183        valuesToRemoveList=Util::qStringListFromSpacedString(valuesToRemove);
     
    98120        UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, "replace-value");
    99121
    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        }
    101129
    102130        for(int j=0; j<elements.size(); j++){
     
    132160        UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, "replace-all");
    133161
    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        }
    135169
    136170
     
    166200        UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, "update-elements");
    167201
    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        }
    169209
    170210
     
    207247        QStringList invertedElements; //Inverting the element order
    208248
    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        }
    210256
    211257        // Read all elements and save to the list
  • XmlTools2/trunk/xmltools.h

    r906 r920  
    2626public:
    2727    XmlTools(QString filesWildcard, XmlFilter filter, bool noBackups);
     28    XmlTools(QString filesWildcard, QString xPathExpression, bool noBackups);
    2829    void addValues(QString newValues);
    2930    void removeValues(QString valuesToRemove);
     
    3738    pugi::xml_node rootNode;
    3839    QStringList filesToProcess;
     40    QString xPathExpression;
    3941    XmlFilter filters;
    4042    bool backupsEnabled;
Note: See TracChangeset for help on using the changeset viewer.