| 1 | #include "optionsparser.h" | 
 
 
 
 
 | 2 |  | 
 
 
 
 
 | 3 | OptionsParser::OptionsParser(QStringList args) | 
 
 
 
 
 | 4 | { | 
 
 
 
 
 | 5 | this->args=args; | 
 
 
 
 
 | 6 | } | 
 
 
 
 
 | 7 |  | 
 
 
 
 
 | 8 | void OptionsParser::parse(){ | 
 
 
 
 
 | 9 |  | 
 
 
 
 
 | 10 | QCommandLineParser parser; | 
 
 
 
 
 | 11 | parser.setApplicationDescription("Additional documentation can be found at: http://wiki.oni2.net/XmlTools"); | 
 
 
 
 
 | 12 |  | 
 
 
 
 
 | 13 | std::unique_ptr<XmlTools> myXmlTools; | 
 
 
 
 
 | 14 | QString filesWildCard, patchFilesWildCard, forceTargetFilesWildcard; | 
 
 
 
 
 | 15 | QString currentVal, newVal, diffOldNewVal, positions; | 
 
 
 
 
 | 16 | QString xPathExpression; | 
 
 
 
 
 | 17 | XmlFilter filters; // Filters | 
 
 
 
 
 | 18 |  | 
 
 
 
 
 | 19 | bool noBackups=false; | 
 
 
 
 
 | 20 | bool noVerbose=false; | 
 
 
 
 
 | 21 |  | 
 
 
 
 
 | 22 | QCommandLineOption addValuesOption(QStringList() << "a" << "add-values", "Add values to a set of XML elements."); | 
 
 
 
 
 | 23 | QCommandLineOption removeValuesOption(QStringList() << "remove-values", "Removes values from a set of XML elements."); | 
 
 
 
 
 | 24 | QCommandLineOption replaceValueOption(QStringList() << "replace-value", "Replaces 1 value in a set of XML elements."); | 
 
 
 
 
 | 25 | QCommandLineOption replaceAllValuesOption(QStringList() << "replace-all-values", "Replaces all values in a set of XML elements."); | 
 
 
 
 
 | 26 | QCommandLineOption updateElementsOption(QStringList() << "u" << "update-elements", "Updates all values in a set of XML elements."); | 
 
 
 
 
 | 27 | QCommandLineOption invertElementsOption(QStringList() << "i" << "invert-elements", "Inverts a set of XML elements."); | 
 
 
 
 
 | 28 |  | 
 
 
 
 
 | 29 | QCommandLineOption currentValOption(QStringList() << "c" << "current-val", "Current value(s) [use space as separator].","current-val"); | 
 
 
 
 
 | 30 | QCommandLineOption newValOption(QStringList() << "n" << "new-val", "New value(s) [use space as separator].","new-val"); | 
 
 
 
 
 | 31 | QCommandLineOption diffOldNewValueOption(QStringList() << "d" << "diff-old-new-val", "Difference between old and new value.","diff-old-new-val"); | 
 
 
 
 
 | 32 | QCommandLineOption positionsValueOption(QStringList() << "positions", "Positions [use space as separator] [0-index based].","positions"); | 
 
 
 
 
 | 33 |  | 
 
 
 
 
 | 34 | QCommandLineOption filesOption(QStringList() << "f" << "files", "XML files to process [wildcards supported].", "files"); | 
 
 
 
 
 | 35 | QCommandLineOption patchFilesOption(QStringList() << "p" << "patch-files" , "Patch files to process [wildcards supported].", "patch-files"); | 
 
 
 
 
 | 36 | QCommandLineOption forceTargetFilesOption(QStringList() << "force-target-files" , "Force the patch-files operation in the specified XML files. [wildcards supported].", "force-target-files"); | 
 
 
 
 
 | 37 | QCommandLineOption elementNameOption(QStringList() << "e" << "element-name", "Name of the XML element(s) where processing will occur.", "element-name"); | 
 
 
 
 
 | 38 | QCommandLineOption parentElementNameOption(QStringList() << "parent-element-name", "Name of the XML parent element of <element-name> [used as filter].", "parent-element-name"); | 
 
 
 
 
 | 39 | QCommandLineOption attributeNameOption("attribute-name", "Attribute name of <element-name>  [used as filter].", "attribute-name"); | 
 
 
 
 
 | 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"); | 
 
 
 
 
 | 42 | QCommandLineOption noBackupsOption(QStringList()  << "no-backups", "No backups [faster processing]."); | 
 
 
 
 
 | 43 | QCommandLineOption noVerboseOption(QStringList()  << "no-verbose", "Reduce the number of text messages output [faster processing]."); | 
 
 
 
 
 | 44 | QCommandLineOption aeiPatchFilesListOption(QStringList() << "aei-patch-files-list" , "Exclusive option for AEI. Provide a list of patches to process.", "aei-patch-files-list"); | 
 
 
 
 
 | 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 | parser.addOption(aeiPatchFilesListOption); | 
 
 
 
 
 | 69 |  | 
 
 
 
 
 | 70 | parser.addVersionOption(); | 
 
 
 
 
 | 71 | parser.addHelpOption(); | 
 
 
 
 
 | 72 |  | 
 
 
 
 
 | 73 | // Process the actual command line arguments given by the user | 
 
 
 
 
 | 74 | parser.process(this->args); | 
 
 
 
 
 | 75 |  | 
 
 
 
 
 | 76 | // If no arguments show help option | 
 
 
 
 
 | 77 | if(this->args.size()==1){ | 
 
 
 
 
 | 78 | parser.showHelp(); | 
 
 
 
 
 | 79 | } | 
 
 
 
 
 | 80 |  | 
 
 
 
 
 | 81 | // Begin by processing AEI special parameter | 
 
 
 
 
 | 82 | if(parser.isSet(aeiPatchFilesListOption)){ | 
 
 
 
 
 | 83 |  | 
 
 
 
 
 | 84 | QFile inputFile(parser.value(aeiPatchFilesListOption)); | 
 
 
 
 
 | 85 | QStringList temp; // contains the complete line. first in | 
 
 
 
 
 | 86 | QStringList patchFiles, targetXmlFiles; | 
 
 
 
 
 | 87 | QString line; | 
 
 
 
 
 | 88 |  | 
 
 
 
 
 | 89 | if (inputFile.open(QIODevice::ReadOnly)) | 
 
 
 
 
 | 90 | { | 
 
 
 
 
 | 91 |  | 
 
 
 
 
 | 92 | QTextStream fileStream(&inputFile); | 
 
 
 
 
 | 93 |  | 
 
 
 
 
 | 94 | // Process all the patches until the end of file | 
 
 
 
 
 | 95 | while ( !fileStream.atEnd() ){ | 
 
 
 
 
 | 96 | line = fileStream.readLine(); | 
 
 
 
 
 | 97 |  | 
 
 
 
 
 | 98 | if(line.startsWith('"')){ // Only read when starting with quotes | 
 
 
 
 
 | 99 | temp=line.split('"',QString::SkipEmptyParts); // We need to use more than space, because if paths contains spaces... | 
 
 
 
 
 | 100 | patchFiles << temp[0]; | 
 
 
 
 
 | 101 | targetXmlFiles << temp[2]; // space is in idx 1 | 
 
 
 
 
 | 102 | } | 
 
 
 
 
 | 103 | } | 
 
 
 
 
 | 104 |  | 
 
 
 
 
 | 105 | inputFile.close(); | 
 
 
 
 
 | 106 |  | 
 
 
 
 
 | 107 | // Now let's process each patch file and target file | 
 
 
 
 
 | 108 | for(int i=0; i<patchFiles.size(); i++){ | 
 
 
 
 
 | 109 | XmlPatch myXmlPatch(patchFiles[i],targetXmlFiles[i],true,true); // use --no-backups and --no-verbose for AEI | 
 
 
 
 
 | 110 | myXmlPatch.readAndProcessPatchFile(); // process current file | 
 
 
 
 
 | 111 | } | 
 
 
 
 
 | 112 |  | 
 
 
 
 
 | 113 | UtilXmlTools::displaySuccessMessage(patchFiles.size(),"AEI patches"); | 
 
 
 
 
 | 114 |  | 
 
 
 
 
 | 115 | } | 
 
 
 
 
 | 116 | else{ | 
 
 
 
 
 | 117 | UtilXmlTools::displayErrorMessage("Read file", "Error opening AEI-patch-files-list file: '" + parser.value(aeiPatchFilesListOption) + "'.\n" + inputFile.errorString()); | 
 
 
 
 
 | 118 | } | 
 
 
 
 
 | 119 |  | 
 
 
 
 
 | 120 | return; | 
 
 
 
 
 | 121 | } | 
 
 
 
 
 | 122 |  | 
 
 
 
 
 | 123 |  | 
 
 
 
 
 | 124 | // Check if the user doesn't want backups (it boosts XmlTools peformance, lower disk output) | 
 
 
 
 
 | 125 | if(parser.isSet(noBackupsOption)){ | 
 
 
 
 
 | 126 | noBackups=true; | 
 
 
 
 
 | 127 | } | 
 
 
 
 
 | 128 |  | 
 
 
 
 
 | 129 | // Check if the user doesn't want verbose mode (it boosts XmlTools peformance, lower std output) | 
 
 
 
 
 | 130 | if(parser.isSet(noVerboseOption)){ | 
 
 
 
 
 | 131 | noVerbose=true; | 
 
 
 
 
 | 132 | } | 
 
 
 
 
 | 133 |  | 
 
 
 
 
 | 134 | // Get patch files wildcard if available | 
 
 
 
 
 | 135 | if(parser.isSet(patchFilesOption)){ | 
 
 
 
 
 | 136 | patchFilesWildCard=parser.value(patchFilesOption); | 
 
 
 
 
 | 137 |  | 
 
 
 
 
 | 138 | // Never reached | 
 
 
 
 
 | 139 | //        if(patchFilesWildCard==""){ | 
 
 
 
 
 | 140 | //            UtilXmlTools::displayErrorMessage("Parameter Parsing", "patch-files option requires 1 value: <patch-files> the patch files to process (wildcards supported)."); | 
 
 
 
 
 | 141 | //        } | 
 
 
 
 
 | 142 |  | 
 
 
 
 
 | 143 | forceTargetFilesWildcard=parser.value(forceTargetFilesOption); | 
 
 
 
 
 | 144 |  | 
 
 
 
 
 | 145 | XmlPatch myXmlPatch(patchFilesWildCard,forceTargetFilesWildcard,noBackups,noVerbose); | 
 
 
 
 
 | 146 | myXmlPatch.readAndProcessPatchFile(); // beging file patch processing | 
 
 
 
 
 | 147 |  | 
 
 
 
 
 | 148 | return; | 
 
 
 
 
 | 149 | } | 
 
 
 
 
 | 150 |  | 
 
 
 
 
 | 151 | if(!parser.isSet(elementNameOption) && !parser.isSet(xPathExpressionOption)){ | 
 
 
 
 
 | 152 | UtilXmlTools::displayErrorMessage("Parameter Parsing","element-name option or xpath-expression option is required if not using patch-files option."); | 
 
 
 
 
 | 153 | } | 
 
 
 
 
 | 154 | else if(parser.isSet(elementNameOption) && parser.isSet(xPathExpressionOption)){ | 
 
 
 
 
 | 155 | UtilXmlTools::displayErrorMessage("Parameter Parsing","element-name option and xpath-expression options cannot be used simultaneously."); | 
 
 
 
 
 | 156 | } | 
 
 
 
 
 | 157 |  | 
 
 
 
 
 | 158 | // Get element name if available | 
 
 
 
 
 | 159 | if(parser.isSet(elementNameOption)){ | 
 
 
 
 
 | 160 | filters.setElementName(parser.value(elementNameOption)); | 
 
 
 
 
 | 161 | } | 
 
 
 
 
 | 162 |  | 
 
 
 
 
 | 163 | // Get xpath expression if available | 
 
 
 
 
 | 164 | if(parser.isSet(xPathExpressionOption)){ | 
 
 
 
 
 | 165 | xPathExpression=parser.value(xPathExpressionOption); | 
 
 
 
 
 | 166 | } | 
 
 
 
 
 | 167 |  | 
 
 
 
 
 | 168 | // Get current value(s) if avaialabe | 
 
 
 
 
 | 169 | if(parser.isSet(currentValOption)){ | 
 
 
 
 
 | 170 | currentVal=parser.value(currentValOption); | 
 
 
 
 
 | 171 | } | 
 
 
 
 
 | 172 |  | 
 
 
 
 
 | 173 | // Get new value(s) if avaialabe | 
 
 
 
 
 | 174 | if(parser.isSet(newValOption)){ | 
 
 
 
 
 | 175 | newVal=parser.value(newValOption); | 
 
 
 
 
 | 176 | } | 
 
 
 
 
 | 177 |  | 
 
 
 
 
 | 178 | // Get difference between old and new value if avaialabe | 
 
 
 
 
 | 179 | if(parser.isSet(diffOldNewValueOption)){ | 
 
 
 
 
 | 180 | diffOldNewVal=parser.value(diffOldNewValueOption); | 
 
 
 
 
 | 181 | } | 
 
 
 
 
 | 182 |  | 
 
 
 
 
 | 183 | // Get positions if avaialabe | 
 
 
 
 
 | 184 | if(parser.isSet(positionsValueOption)){ | 
 
 
 
 
 | 185 | positions=parser.value(positionsValueOption); | 
 
 
 
 
 | 186 | } | 
 
 
 
 
 | 187 |  | 
 
 
 
 
 | 188 | // Get parent element name if available | 
 
 
 
 
 | 189 | if(parser.isSet(parentElementNameOption)){ | 
 
 
 
 
 | 190 | filters.setParentElementName(parser.value(parentElementNameOption)); | 
 
 
 
 
 | 191 | } | 
 
 
 
 
 | 192 |  | 
 
 
 
 
 | 193 | // Get attribute name if available | 
 
 
 
 
 | 194 | if(parser.isSet(attributeNameOption)){ | 
 
 
 
 
 | 195 | filters.setAttributeName(parser.value(attributeNameOption)); | 
 
 
 
 
 | 196 | } | 
 
 
 
 
 | 197 |  | 
 
 
 
 
 | 198 | // Get attribute value if available | 
 
 
 
 
 | 199 | if(parser.isSet(attributeValueOption)){ | 
 
 
 
 
 | 200 | filters.setAttributeValue(parser.value(attributeValueOption)); | 
 
 
 
 
 | 201 | } | 
 
 
 
 
 | 202 |  | 
 
 
 
 
 | 203 | // Check attribute filters | 
 
 
 
 
 | 204 | if(filters.getAttributeName()!="" && filters.getAttributeValue()==""){ | 
 
 
 
 
 | 205 | UtilXmlTools::displayErrorMessage("Parameter Parsing","attribute-value option is required if using attribute-name option."); | 
 
 
 
 
 | 206 | } | 
 
 
 
 
 | 207 |  | 
 
 
 
 
 | 208 | if(filters.getAttributeValue()!="" && filters.getAttributeName()==""){ | 
 
 
 
 
 | 209 | UtilXmlTools::displayErrorMessage("Parameter Parsing","attribute-name option is required if using attribute-value option."); | 
 
 
 
 
 | 210 | } | 
 
 
 
 
 | 211 |  | 
 
 
 
 
 | 212 | // Get files wildcard if available | 
 
 
 
 
 | 213 | if(parser.isSet(filesOption)){ | 
 
 
 
 
 | 214 | filesWildCard=parser.value(filesOption); | 
 
 
 
 
 | 215 | } | 
 
 
 
 
 | 216 | else{ | 
 
 
 
 
 | 217 | UtilXmlTools::displayErrorMessage("Parameter Parsing", "files option requires 1 value: <files> the XML files to process (wildcards supported)."); | 
 
 
 
 
 | 218 | } | 
 
 
 
 
 | 219 |  | 
 
 
 
 
 | 220 | if(parser.isSet(elementNameOption)){ | 
 
 
 
 
 | 221 | myXmlTools = std::make_unique<XmlTools>(filesWildCard,filters,noBackups,noVerbose); | 
 
 
 
 
 | 222 | } | 
 
 
 
 
 | 223 | else{ | 
 
 
 
 
 | 224 | myXmlTools = std::make_unique<XmlTools>(filesWildCard,xPathExpression,noBackups,noVerbose); | 
 
 
 
 
 | 225 | } | 
 
 
 
 
 | 226 |  | 
 
 
 
 
 | 227 |  | 
 
 
 
 
 | 228 | // Users wants an add-option? | 
 
 
 
 
 | 229 | if(parser.isSet(addValuesOption)){ | 
 
 
 
 
 | 230 |  | 
 
 
 
 
 | 231 | if(newVal==""){ | 
 
 
 
 
 | 232 | UtilXmlTools::displayErrorMessage("Parameter Parsing", "add-value option requires 1 value: <new-val> the new values to add (space separated)."); | 
 
 
 
 
 | 233 | } | 
 
 
 
 
 | 234 |  | 
 
 
 
 
 | 235 | myXmlTools->addValues(newVal); | 
 
 
 
 
 | 236 | } | 
 
 
 
 
 | 237 | else if(parser.isSet(removeValuesOption)){ // Or remove-values option? | 
 
 
 
 
 | 238 |  | 
 
 
 
 
 | 239 | if(currentVal==""){ | 
 
 
 
 
 | 240 | UtilXmlTools::displayErrorMessage("Parameter Parsing","remove-values option requires 1 value: <current-val> the current values to remove (space separated)."); | 
 
 
 
 
 | 241 | } | 
 
 
 
 
 | 242 |  | 
 
 
 
 
 | 243 | myXmlTools->removeValues(currentVal); | 
 
 
 
 
 | 244 | } | 
 
 
 
 
 | 245 | else if(parser.isSet(replaceValueOption)){ // Or replace-value option? | 
 
 
 
 
 | 246 |  | 
 
 
 
 
 | 247 | if(currentVal=="" || newVal==""){ | 
 
 
 
 
 | 248 | UtilXmlTools::displayErrorMessage("Parameter Parsing","replace-value option requires 2 values: <current-val> the current value and <new-val> the new value."); | 
 
 
 
 
 | 249 | } | 
 
 
 
 
 | 250 |  | 
 
 
 
 
 | 251 | myXmlTools->replaceValue(currentVal,newVal); | 
 
 
 
 
 | 252 | } | 
 
 
 
 
 | 253 | else if(parser.isSet(replaceAllValuesOption)){ // Or replace-all-values option? | 
 
 
 
 
 | 254 |  | 
 
 
 
 
 | 255 | if(newVal=="" && positions==""){ | 
 
 
 
 
 | 256 | UtilXmlTools::displayErrorMessage("Parameter Parsing","replace-all-values option requires 1 value: <new-val> the new value.\n" + | 
 
 
 
 
 | 257 | QString("It has also 1 optional value: <positions> the positions to replace (space separated and 0-index based).")); | 
 
 
 
 
 | 258 | } | 
 
 
 
 
 | 259 |  | 
 
 
 
 
 | 260 | myXmlTools->replaceAll(newVal,positions); | 
 
 
 
 
 | 261 | } | 
 
 
 
 
 | 262 | else if(parser.isSet(updateElementsOption)){ // Or update-elements option? | 
 
 
 
 
 | 263 |  | 
 
 
 
 
 | 264 | if(diffOldNewVal==""){ | 
 
 
 
 
 | 265 | UtilXmlTools::displayErrorMessage("Parameter Parsing","update-elements option requires 1 value: <diff-old-new-val> the difference between one current element "+ | 
 
 
 
 
 | 266 | QString("value and one new element value (current value and new value must have the same position).")); | 
 
 
 
 
 | 267 | } | 
 
 
 
 
 | 268 |  | 
 
 
 
 
 | 269 | myXmlTools->updateElements(diffOldNewVal); | 
 
 
 
 
 | 270 | } | 
 
 
 
 
 | 271 | else if(parser.isSet(invertElementsOption)){ // Or invert-elements option? | 
 
 
 
 
 | 272 | myXmlTools->invertElements(); | 
 
 
 
 
 | 273 | } | 
 
 
 
 
 | 274 | else{ | 
 
 
 
 
 | 275 | UtilXmlTools::displayErrorMessage("Parameter Parsing","XmlTools needs an operation to perform. Possible operations are:\n"+ | 
 
 
 
 
 | 276 | QString("--patch-files\n--add-values\n--remove-values\n--replace-value\n--replace-all-values\n--update-elements\n--invert-elements")); | 
 
 
 
 
 | 277 | } | 
 
 
 
 
 | 278 | } |