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

more fixes and updated examples

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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        }
Note: See TracChangeset for help on using the changeset viewer.