Changeset 926 for XmlTools2


Ignore:
Timestamp:
Feb 5, 2014, 7:31:05 PM (11 years ago)
Author:
s10k
Message:

XmlTools2
Added --aei-patch-files-list for AEI2

Location:
XmlTools2/trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • XmlTools2/trunk/XmlTools.pro

    r906 r926  
    2121    multidimvar.cpp \
    2222    utilxmltools.cpp \
    23     xmlfilter.cpp
     23    xmlfilter.cpp \
     24    optionsparser.cpp
    2425
    2526HEADERS += main.h \
     
    3132    multidimvar.h \
    3233    utilxmltools.h \
    33     xmlfilter.h
     34    xmlfilter.h \
     35    optionsparser.h
    3436
    3537OTHER_FILES +=
  • XmlTools2/trunk/main.cpp

    r923 r926  
    66    QCoreApplication app(argc, argv);
    77
     8    // Set application parameters
    89    QCoreApplication::setApplicationName(GlobalVars::AppName);
    910    QCoreApplication::setApplicationVersion(GlobalVars::AppVersion);
    1011
    11     QCommandLineParser parser;
    12     parser.setApplicationDescription("Additional documentation can be found at: http://wiki.oni2.net/XmlTools");
    13 
    14     XmlTools *myXmlTools;
    15     QString filesWildCard, patchFilesWildCard, forceTargetFilesWildcard;
    16     QString currentVal, newVal, diffOldNewVal, positions;
    17     QString xPathExpression;
    18     XmlFilter filters; // Filters
    19 
    20     bool noBackups=false;
    21     bool noVerbose=false;
    22 
    23     QCommandLineOption addValuesOption(QStringList() << "a" << "add-values", "Add values to a set of XML elements.");
    24     QCommandLineOption removeValuesOption(QStringList() << "remove-values", "Removes values from a set of XML elements.");
    25     QCommandLineOption replaceValueOption(QStringList() << "replace-value", "Replaces 1 value in a set of XML elements.");
    26     QCommandLineOption replaceAllValuesOption(QStringList() << "replace-all-values", "Replaces all values in a set of XML elements.");
    27     QCommandLineOption updateElementsOption(QStringList() << "u" << "update-elements", "Updates all values in a set of XML elements.");
    28     QCommandLineOption invertElementsOption(QStringList() << "i" << "invert-elements", "Inverts a set of XML elements.");
    29 
    30     QCommandLineOption currentValOption(QStringList() << "c" << "current-val", "Current value(s) [use space as separator].","current-val");
    31     QCommandLineOption newValOption(QStringList() << "n" << "new-val", "New value(s) [use space as separator].","new-val");
    32     QCommandLineOption diffOldNewValueOption(QStringList() << "d" << "diff-old-new-val", "Difference between old and new value.","diff-old-new-val");
    33     QCommandLineOption positionsValueOption(QStringList() << "positions", "Positions [use space as separator] [0-index based].","positions");
    34 
    35     QCommandLineOption filesOption(QStringList() << "f" << "files", "XML files to process [wildcards supported].", "files");
    36     QCommandLineOption patchFilesOption(QStringList() << "p" << "patch-files" , "Patch files to process [wildcards supported].", "patch-files");
    37     QCommandLineOption forceTargetFilesOption(QStringList() << "force-target-files" , "Force the patch-files operation in the specified XML files. [wildcards supported].", "force-target-files");
    38     QCommandLineOption elementNameOption(QStringList() << "e" << "element-name", "Name of the XML element(s) where processing will occur.", "element-name");
    39     QCommandLineOption parentElementNameOption(QStringList() << "parent-element-name", "Name of the XML parent element of <element-name> [used as filter].", "parent-element-name");
    40     QCommandLineOption attributeNameOption("attribute-name", "Attribute name of <element-name>  [used as filter].", "attribute-name");
    41     QCommandLineOption attributeValueOption("attribute-value", "Attribute value of <attribute-name>  [used as filter].", "attribute-value");
    42     QCommandLineOption xPathExpressionOption(QStringList() << "x" << "xpath-expression", "XPath 1.0 expression to select elements where processing will occur.", "xpath-expression");
    43     QCommandLineOption noBackupsOption(QStringList()  << "no-backups", "No backups [faster processing].");
    44     QCommandLineOption noVerboseOption(QStringList()  << "no-verbose", "Reduce the number of text messages output [faster processing].");
    45 
    46     parser.addOption(addValuesOption);
    47     parser.addOption(removeValuesOption);
    48     parser.addOption(replaceValueOption);
    49     parser.addOption(replaceAllValuesOption);
    50     parser.addOption(updateElementsOption);
    51     parser.addOption(invertElementsOption);
    52 
    53     parser.addOption(currentValOption);
    54     parser.addOption(newValOption);
    55     parser.addOption(diffOldNewValueOption);
    56     parser.addOption(positionsValueOption);
    57 
    58     parser.addOption(filesOption);
    59     parser.addOption(patchFilesOption);
    60     parser.addOption(forceTargetFilesOption);
    61     parser.addOption(elementNameOption);
    62     parser.addOption(parentElementNameOption);
    63     parser.addOption(attributeNameOption);
    64     parser.addOption(attributeValueOption);
    65     parser.addOption(xPathExpressionOption);
    66     parser.addOption(noBackupsOption);
    67     parser.addOption(noVerboseOption);
    68 
    69     parser.addVersionOption();
    70     parser.addHelpOption();
    71 
    72     // Process the actual command line arguments given by the user
    73     parser.process(app);
    74 
    75     // If no arguments show help option
    76     if(app.arguments().size()==1){
    77         parser.showHelp();
    78     }
    79 
    80     // Check if the user doesn't want backups (it boosts XmlTools peformance, lower disk output)
    81     if(parser.isSet(noBackupsOption)){
    82         noBackups=true;
    83     }
    84 
    85     // Check if the user doesn't want verbose mode (it boosts XmlTools peformance, lower std output)
    86     if(parser.isSet(noVerboseOption)){
    87         noVerbose=true;
    88     }
    89 
    90     // Get patch files wildcard if available
    91     if(parser.isSet(patchFilesOption)){
    92         patchFilesWildCard=parser.value(patchFilesOption);
    93 
    94         // Never reached
    95         //        if(patchFilesWildCard==""){
    96         //            UtilXmlTools::displayErrorMessage("Parameter Parsing", "patch-files option requires 1 value: <patch-files> the patch files to process (wildcards supported).");
    97         //        }
    98 
    99         forceTargetFilesWildcard=parser.value(forceTargetFilesOption);
    100 
    101 
    102         XmlPatch myXmlPatch(patchFilesWildCard,forceTargetFilesWildcard,noBackups,noVerbose);
    103         myXmlPatch.readAndProcessPatchFile(); // beging file patch processing
    104 
    105         return 0;
    106     }
    107 
    108     if(!parser.isSet(elementNameOption) && !parser.isSet(xPathExpressionOption)){
    109         UtilXmlTools::displayErrorMessage("Parameter Parsing","element-name option or xpath-expression option is required if not using patch-files option.");
    110     }
    111     else if(parser.isSet(elementNameOption) && parser.isSet(xPathExpressionOption)){
    112         UtilXmlTools::displayErrorMessage("Parameter Parsing","element-name option and xpath-expression options cannot be used simultaneously.");
    113     }
    114 
    115     // Get element name if available
    116     if(parser.isSet(elementNameOption)){
    117         filters.setElementName(parser.value(elementNameOption));
    118     }
    119 
    120     // Get xpath expression if available
    121     if(parser.isSet(xPathExpressionOption)){
    122         xPathExpression=parser.value(xPathExpressionOption);
    123     }
    124 
    125     // Get current value(s) if avaialabe
    126     if(parser.isSet(currentValOption)){
    127         currentVal=parser.value(currentValOption);
    128     }
    129 
    130     // Get new value(s) if avaialabe
    131     if(parser.isSet(newValOption)){
    132         newVal=parser.value(newValOption);
    133     }
    134 
    135     // Get difference between old and new value if avaialabe
    136     if(parser.isSet(diffOldNewValueOption)){
    137         diffOldNewVal=parser.value(diffOldNewValueOption);
    138     }
    139 
    140     // Get positions if avaialabe
    141     if(parser.isSet(positionsValueOption)){
    142         positions=parser.value(positionsValueOption);
    143     }
    144 
    145     // Get parent element name if available
    146     if(parser.isSet(parentElementNameOption)){
    147         filters.setParentElementName(parser.value(parentElementNameOption));
    148     }
    149 
    150     // Get attribute name if available
    151     if(parser.isSet(attributeNameOption)){
    152         filters.setAttributeName(parser.value(attributeNameOption));
    153     }
    154 
    155     // Get attribute value if available
    156     if(parser.isSet(attributeValueOption)){
    157         filters.setAttributeValue(parser.value(attributeValueOption));
    158     }
    159 
    160     // Check attribute filters
    161     if(filters.getAttributeName()!="" && filters.getAttributeValue()==""){
    162         UtilXmlTools::displayErrorMessage("Parameter Parsing","attribute-value option is required if using attribute-name option.");
    163     }
    164 
    165     if(filters.getAttributeValue()!="" && filters.getAttributeName()==""){
    166         UtilXmlTools::displayErrorMessage("Parameter Parsing","attribute-name option is required if using attribute-value option.");
    167     }
    168 
    169     // Get files wildcard if available
    170     if(parser.isSet(filesOption)){
    171         filesWildCard=parser.value(filesOption);
    172     }
    173     else{
    174         UtilXmlTools::displayErrorMessage("Parameter Parsing", "files option requires 1 value: <files> the XML files to process (wildcards supported).");
    175     }
    176 
    177     if(parser.isSet(elementNameOption)){
    178         myXmlTools=new XmlTools(filesWildCard,filters,noBackups,noVerbose);
    179     }
    180     else{
    181         myXmlTools=new XmlTools(filesWildCard,xPathExpression,noBackups,noVerbose);
    182     }
    183 
    184 
    185     // Users wants an add-option?
    186     if(parser.isSet(addValuesOption)){
    187 
    188         if(newVal==""){
    189             UtilXmlTools::displayErrorMessage("Parameter Parsing", "add-value option requires 1 value: <new-val> the new values to add (space separated).");
    190         }
    191 
    192         myXmlTools->addValues(newVal);
    193     }
    194     else if(parser.isSet(removeValuesOption)){ // Or remove-values option?
    195 
    196         if(currentVal==""){
    197             UtilXmlTools::displayErrorMessage("Parameter Parsing","remove-values option requires 1 value: <current-val> the current values to remove (space separated).");
    198         }
    199 
    200         myXmlTools->removeValues(currentVal);
    201     }
    202     else if(parser.isSet(replaceValueOption)){ // Or replace-value option?
    203 
    204         if(currentVal=="" || newVal==""){
    205             UtilXmlTools::displayErrorMessage("Parameter Parsing","replace-value option requires 2 values: <current-val> the current value and <new-val> the new value.");
    206         }
    207 
    208         myXmlTools->replaceValue(currentVal,newVal);
    209     }
    210     else if(parser.isSet(replaceAllValuesOption)){ // Or replace-all-values option?
    211 
    212         if(newVal=="" && positions==""){
    213             UtilXmlTools::displayErrorMessage("Parameter Parsing","replace-all-values option requires 1 value: <new-val> the new value.\n" +
    214                                               Util::toQString("It has also 1 optional value: <positions> the positions to replace (space separated and 0-index based)."));
    215         }
    216 
    217         myXmlTools->replaceAll(newVal,positions);
    218     }
    219     else if(parser.isSet(updateElementsOption)){ // Or update-elements option?
    220 
    221         if(diffOldNewVal==""){
    222             UtilXmlTools::displayErrorMessage("Parameter Parsing","update-elements option requires 1 value: <diff-old-new-val> the difference between one current element "+
    223                                               Util::toQString("value and one new element value (current value and new value must have the same position)."));
    224         }
    225 
    226         myXmlTools->updateElements(diffOldNewVal);
    227     }
    228     else if(parser.isSet(invertElementsOption)){ // Or invert-elements option?
    229         myXmlTools->invertElements();
    230     }
    231     else{
    232         UtilXmlTools::displayErrorMessage("Parameter Parsing","One operation is required to XmlTools continue. Possible operations are:\n"+
    233                                           Util::toQString("--patch-files\n--add-values\n--remove-values\n--replace-value\n--replace-all-values\n--update-elements\n--invert-elements"));
    234     }
    235 
    236     //delete myXmlTools;
    237     //std::cout << "Started" << std::endl;
     12    OptionsParser myParser(app.arguments());
     13    myParser.parse();
    23814
    23915    //app.exec();
  • XmlTools2/trunk/main.h

    r906 r926  
    11#ifndef MAIN_H
    22#define MAIN_H
    3 
    4 #include "xmlpatch.h"
    53
    64#ifdef __MINGW32__
     
    119unsigned long _CRT_glob = 0; // Magic variable to disable wildcards expansion in mingw
    1210// in our case fixes param parser crashes on windows
     11
     12#include "optionsparser.h"
     13
    1314#endif
    1415
    1516int main(int argc, char *argv[]);
    1617
    17 void invert(std::string elementName, std::string parElementName);
    18 
    19 
    2018#endif // MAIN_H
  • XmlTools2/trunk/readme.txt

    r923 r926  
    6262Change Log:
    6363----------------------------------
    64 2.0, 04-02-2014
     642.0, 05-02-2014
    6565-Rewrite XmlTools fom the scratch from C# to C++ in order to increase (much!) the performance of the program
    6666-The program now uses the following libraries: Qt5, pugixml and jsxml js library
     
    7676-Added option to select xml elements by XPath 1.0 (should reduce further the need of javascript)
    7777-The insertion of xml via patch now support multiple nodes inside <xml></xml> tags
     78-Added exclusive option for AEI which allows AEI to pass a list of .oni-patches to process
  • XmlTools2/trunk/util.h

    r920 r926  
    66#include <QString>
    77#include <QStringList>
     8#include <QRegExp>
    89#include <iostream> // cout, cin etc.
    910
     
    5051}
    5152
     53inline QStringList QStringToArgsArray(const QString &args){
     54    QStringList result;
     55    result=Util::substring(args," ");
     56
     57    for(int i=0; i<result.size(); i++){
     58        result[i].remove('"'); // remove quotes, they are not necessary as we had already splited the spaces
     59    }
     60
     61    return result;
     62}
     63
    5264}
    5365
  • XmlTools2/trunk/xmlpatch.cpp

    r923 r926  
    100100            for (pugi::xml_node currNodeToInsert = newNode.first_child(); currNodeToInsert; currNodeToInsert = currNodeToInsert.next_sibling())
    101101            {
    102                nodesToInsertion[j].append_copy(currNodeToInsert); // append the new node
     102                nodesToInsertion[j].append_copy(currNodeToInsert); // append the new node
    103103            }
    104104
     
    188188void XmlPatch::executeCommandOperation(const QString &commandString){
    189189
    190     QProcess newXmlToolsInstance;
    191     QString resultOutput;
    192 
    193190    // Avoid infinite fork loops
    194     if(commandString.contains(" -p ") || commandString.contains(" --patch-files ")){
     191    if(commandString.contains("-p ") || commandString.contains("--patch-files ")){
    195192        UtilXmlTools::displayErrorMessage("@COMMAND","Use of --patch-files option is not allowed inside a patch file");
    196193    }
    197194
    198     newXmlToolsInstance.start(GlobalVars::AppExecutable + " " + commandString);
    199     newXmlToolsInstance.waitForFinished(-1); // wait for new instance to finish
    200 
    201     resultOutput=newXmlToolsInstance.readAll();
    202 
    203     if(newXmlToolsInstance.exitCode()!=0){
    204         UtilXmlTools::displayErrorMessage("@COMMAND", "An error ocurred:\n" + resultOutput);
    205         exit(1);
     195    // Reserved to AEI
     196    if(commandString.contains("--aei-patch-files-list ")){
     197        UtilXmlTools::displayErrorMessage("@COMMAND","Use of --aei-patch-files-list option is not allowed inside a patch file");
    206198    }
    207199
    208200    std::cout << "@COMMAND patch operation output:\n"
    209               << "########################################################################\n"
    210               << resultOutput.toLatin1().constData()
    211               << "########################################################################"
    212               << std::endl;
     201    << "########################################################################"
     202    << std::endl;
     203
     204    OptionsParser myParser(Util::QStringToArgsArray(commandString));
     205    myParser.parse();
     206
     207    std::cout
     208    << "########################################################################"
     209    << std::endl;
    213210
    214211    UtilXmlTools::displaySuccessMessage(1,"@COMMAND");
     
    426423        else if(line.startsWith("@COMMAND")){
    427424
    428             command=getPatchParameterValue(line,"Arguments");
     425            command=GlobalVars::AppExecutable;
     426
     427            // Append files if forced to
     428            if(!this->forceTargetFilesWildcard.isEmpty()){
     429                command+=" --files '"+this->forceTargetFilesWildcard+"' ";
     430            }
     431
     432            command+=" "+getPatchParameterValue(line,"Options");
     433
     434            // Add --no-backups and --no-verbose if patch was called with that arguments
     435            if(!this->backupsEnabled){
     436                command.append(" --no-backups");
     437            }
     438
     439            if(!this->verboseEnabled){
     440                command.append(" --no-verbose");
     441            }
    429442
    430443            command.replace("'","\""); //replace apostrophe by quotes, to avoid problems
     
    433446
    434447            command.clear();
     448            filesWildcard.clear();
    435449        }
    436450        else if(line.startsWith("@CUSTOM_CODE")){
  • XmlTools2/trunk/xmlpatch.h

    r923 r926  
    33
    44#include "xmltools.h"
     5#include "optionsparser.h"
    56
    67class XmlPatch
    78{
    89public:
    9     XmlPatch(QString patchFilesWildcard, QString forceTargetFilesWildcard, bool backupsEnabled, bool noVerbose);
     10    XmlPatch(QString patchFilesWildcard, QString forceTargetFilesWildcard, bool noBackups, bool noVerbose);
    1011    void readAndProcessPatchFile();
    1112private:
Note: See TracChangeset for help on using the changeset viewer.