source: XmlTools2/trunk/main.cpp@ 925

Last change on this file since 925 was 923, checked in by s10k, 11 years ago

XmlTools2
added --no-verbose mode
The insertion of xml via patch now support multiple nodes inside <xml></xml> tags

File size: 11.0 KB
Line 
1#include "main.h"
2
3int main(int argc, char *argv[])
4{
5
6 QCoreApplication app(argc, argv);
7
8 QCoreApplication::setApplicationName(GlobalVars::AppName);
9 QCoreApplication::setApplicationVersion(GlobalVars::AppVersion);
10
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;
238
239 //app.exec();
240 return 0;
241}
Note: See TracBrowser for help on using the repository browser.