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 |
17 |
– |
bool noBackups=false; |
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."); |
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."); |
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 |
|
|
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); |
60 |
|
parser.addOption(parentElementNameOption); |
61 |
|
parser.addOption(attributeNameOption); |
62 |
|
parser.addOption(attributeValueOption); |
63 |
+ |
parser.addOption(xPathExpressionOption); |
64 |
|
parser.addOption(noBackupsOption); |
65 |
|
|
66 |
|
parser.addVersionOption(); |
74 |
|
parser.showHelp(); |
75 |
|
} |
76 |
|
|
77 |
< |
// Check if the user doesn't want backups (it boost XmlTools peformance, lower disk output) |
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 |
|
} |
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 |
< |
if(filters.getElementName()==""){ |
113 |
< |
UtilXmlTools::displayErrorMessage("Parameter Parsing","element-name option is required if not using patch-files option."); |
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 |
166 |
|
UtilXmlTools::displayErrorMessage("Parameter Parsing", "files option requires 1 value: <files> the XML files to process (wildcards supported)."); |
167 |
|
} |
168 |
|
|
169 |
< |
XmlTools myXmlTools(filesWildCard,filters,noBackups); |
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? |
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); |
184 |
> |
myXmlTools->addValues(newVal); |
185 |
|
} |
186 |
|
else if(parser.isSet(removeValuesOption)){ // Or remove-values option? |
187 |
|
|
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); |
192 |
> |
myXmlTools->removeValues(currentVal); |
193 |
|
} |
194 |
|
else if(parser.isSet(replaceValueOption)){ // Or replace-value option? |
195 |
|
|
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); |
200 |
> |
myXmlTools->replaceValue(currentVal,newVal); |
201 |
|
} |
202 |
|
else if(parser.isSet(replaceAllValuesOption)){ // Or replace-all-values option? |
203 |
|
|
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); |
209 |
> |
myXmlTools->replaceAll(newVal,positions); |
210 |
|
} |
211 |
|
else if(parser.isSet(updateElementsOption)){ // Or update-elements option? |
212 |
|
|
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); |
218 |
> |
myXmlTools->updateElements(diffOldNewVal); |
219 |
|
} |
220 |
|
else if(parser.isSet(invertElementsOption)){ // Or invert-elements option? |
221 |
< |
myXmlTools.invertElements(); |
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(); |