source: XmlTools2/trunk/main.cpp@ 920

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

more fixes and updated examples

File size: 10.6 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
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
44 parser.addOption(addValuesOption);
45 parser.addOption(removeValuesOption);
46 parser.addOption(replaceValueOption);
47 parser.addOption(replaceAllValuesOption);
48 parser.addOption(updateElementsOption);
49 parser.addOption(invertElementsOption);
50
51 parser.addOption(currentValOption);
52 parser.addOption(newValOption);
53 parser.addOption(diffOldNewValueOption);
54 parser.addOption(positionsValueOption);
55
56 parser.addOption(filesOption);
57 parser.addOption(patchFilesOption);
58 parser.addOption(forceTargetFilesOption);
59 parser.addOption(elementNameOption);
60 parser.addOption(parentElementNameOption);
61 parser.addOption(attributeNameOption);
62 parser.addOption(attributeValueOption);
63 parser.addOption(xPathExpressionOption);
64 parser.addOption(noBackupsOption);
65
66 parser.addVersionOption();
67 parser.addHelpOption();
68
69 // Process the actual command line arguments given by the user
70 parser.process(app);
71
72 // If no arguments show help option
73 if(app.arguments().size()==1){
74 parser.showHelp();
75 }
76
77 // Check if the user doesn't want backups (it boosts XmlTools peformance, lower disk output)
78 if(parser.isSet(noBackupsOption)){
79 noBackups=true;
80 }
81
82 // Get patch files wildcard if available
83 if(parser.isSet(patchFilesOption)){
84 patchFilesWildCard=parser.value(patchFilesOption);
85
86 // Never reached
87 // if(patchFilesWildCard==""){
88 // UtilXmlTools::displayErrorMessage("Parameter Parsing", "patch-files option requires 1 value: <patch-files> the patch files to process (wildcards supported).");
89 // }
90
91 forceTargetFilesWildcard=parser.value(forceTargetFilesOption);
92
93
94 XmlPatch myXmlPatch(patchFilesWildCard,forceTargetFilesWildcard,noBackups);
95 myXmlPatch.readAndProcessPatchFile(); // beging file patch processing
96
97 return 0;
98 }
99
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
107 // Get element name if available
108 if(parser.isSet(elementNameOption)){
109 filters.setElementName(parser.value(elementNameOption));
110 }
111
112 // Get xpath expression if available
113 if(parser.isSet(xPathExpressionOption)){
114 xPathExpression=parser.value(xPathExpressionOption);
115 }
116
117 // Get current value(s) if avaialabe
118 if(parser.isSet(currentValOption)){
119 currentVal=parser.value(currentValOption);
120 }
121
122 // Get new value(s) if avaialabe
123 if(parser.isSet(newValOption)){
124 newVal=parser.value(newValOption);
125 }
126
127 // Get difference between old and new value if avaialabe
128 if(parser.isSet(diffOldNewValueOption)){
129 diffOldNewVal=parser.value(diffOldNewValueOption);
130 }
131
132 // Get positions if avaialabe
133 if(parser.isSet(positionsValueOption)){
134 positions=parser.value(positionsValueOption);
135 }
136
137 // Get parent element name if available
138 if(parser.isSet(parentElementNameOption)){
139 filters.setParentElementName(parser.value(parentElementNameOption));
140 }
141
142 // Get attribute name if available
143 if(parser.isSet(attributeNameOption)){
144 filters.setAttributeName(parser.value(attributeNameOption));
145 }
146
147 // Get attribute value if available
148 if(parser.isSet(attributeValueOption)){
149 filters.setAttributeValue(parser.value(attributeValueOption));
150 }
151
152 // Check attribute filters
153 if(filters.getAttributeName()!="" && filters.getAttributeValue()==""){
154 UtilXmlTools::displayErrorMessage("Parameter Parsing","attribute-value option is required if using attribute-name option.");
155 }
156
157 if(filters.getAttributeValue()!="" && filters.getAttributeName()==""){
158 UtilXmlTools::displayErrorMessage("Parameter Parsing","attribute-name option is required if using attribute-value option.");
159 }
160
161 // Get files wildcard if available
162 if(parser.isSet(filesOption)){
163 filesWildCard=parser.value(filesOption);
164 }
165 else{
166 UtilXmlTools::displayErrorMessage("Parameter Parsing", "files option requires 1 value: <files> the XML files to process (wildcards supported).");
167 }
168
169 if(parser.isSet(elementNameOption)){
170 myXmlTools=new XmlTools(filesWildCard,filters,noBackups);
171 }
172 else{
173 myXmlTools=new XmlTools(filesWildCard,xPathExpression,noBackups);
174 }
175
176
177 // Users wants an add-option?
178 if(parser.isSet(addValuesOption)){
179
180 if(newVal==""){
181 UtilXmlTools::displayErrorMessage("Parameter Parsing", "add-value option requires 1 value: <new-val> the new values to add (space separated).");
182 }
183
184 myXmlTools->addValues(newVal);
185 }
186 else if(parser.isSet(removeValuesOption)){ // Or remove-values option?
187
188 if(currentVal==""){
189 UtilXmlTools::displayErrorMessage("Parameter Parsing","remove-values option requires 1 value: <current-val> the current values to remove (space separated).");
190 }
191
192 myXmlTools->removeValues(currentVal);
193 }
194 else if(parser.isSet(replaceValueOption)){ // Or replace-value option?
195
196 if(currentVal=="" || newVal==""){
197 UtilXmlTools::displayErrorMessage("Parameter Parsing","replace-value option requires 2 values: <current-val> the current value and <new-val> the new value.");
198 }
199
200 myXmlTools->replaceValue(currentVal,newVal);
201 }
202 else if(parser.isSet(replaceAllValuesOption)){ // Or replace-all-values option?
203
204 if(newVal=="" && positions==""){
205 UtilXmlTools::displayErrorMessage("Parameter Parsing","replace-all-values option requires 1 value: <new-val> the new value.\n" +
206 Util::toQString("It has also 1 optional value: <positions> the positions to replace (space separated and 0-index based)."));
207 }
208
209 myXmlTools->replaceAll(newVal,positions);
210 }
211 else if(parser.isSet(updateElementsOption)){ // Or update-elements option?
212
213 if(diffOldNewVal==""){
214 UtilXmlTools::displayErrorMessage("Parameter Parsing","update-elements option requires 1 value: <diff-old-new-val> the difference between one current element "+
215 Util::toQString("value and one new element value (current value and new value must have the same position)."));
216 }
217
218 myXmlTools->updateElements(diffOldNewVal);
219 }
220 else if(parser.isSet(invertElementsOption)){ // Or invert-elements option?
221 myXmlTools->invertElements();
222 }
223 else{
224 UtilXmlTools::displayErrorMessage("Parameter Parsing","One operation is required to XmlTools continue. Possible operations are:\n"+
225 Util::toQString("--patch-files\n--add-values\n--remove-values\n--replace-value\n--replace-all-values\n--update-elements\n--invert-elements"));
226 }
227
228 //delete myXmlTools;
229 //std::cout << "Started" << std::endl;
230
231 //app.exec();
232 return 0;
233}
Note: See TracBrowser for help on using the repository browser.