| 1 |
|
#include "xmltools.h" |
| 2 |
|
|
| 3 |
+ |
// Filters constructor |
| 4 |
|
XmlTools::XmlTools(QString filesWildcard, XmlFilter filter, bool noBackups) |
| 5 |
|
{ |
| 6 |
|
this->filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard); |
| 12 |
|
} |
| 13 |
|
} |
| 14 |
|
|
| 15 |
+ |
// XPath constructor |
| 16 |
+ |
XmlTools::XmlTools(QString filesWildcard, QString xPathExpression, bool noBackups) |
| 17 |
+ |
{ |
| 18 |
+ |
this->filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard); |
| 19 |
+ |
this->xPathExpression=xPathExpression; |
| 20 |
+ |
this->backupsEnabled=!noBackups; |
| 21 |
+ |
} |
| 22 |
+ |
|
| 23 |
|
// Adds new values to an element |
| 24 |
|
void XmlTools::addValues(QString newValues){ |
| 25 |
|
|
| 32 |
|
UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled,"add-values"); |
| 33 |
|
|
| 34 |
|
newValuesList=Util::qStringListFromSpacedString(newValues); |
| 35 |
< |
UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); |
| 35 |
> |
|
| 36 |
> |
// Check how the elements will be fetched via element name or xpath expression |
| 37 |
> |
if(this->xPathExpression==""){ |
| 38 |
> |
UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); |
| 39 |
> |
} |
| 40 |
> |
else{ |
| 41 |
> |
UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements); |
| 42 |
> |
} |
| 43 |
|
|
| 44 |
|
for(int j=0; j<elements.size(); j++){ |
| 45 |
|
|
| 72 |
|
|
| 73 |
|
UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, "remove-values"); |
| 74 |
|
|
| 75 |
< |
UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); |
| 75 |
> |
// Check how the elements will be fetched via element name or xpath expression |
| 76 |
> |
if(this->xPathExpression==""){ |
| 77 |
> |
UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); |
| 78 |
> |
} |
| 79 |
> |
else{ |
| 80 |
> |
UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements); |
| 81 |
> |
} |
| 82 |
|
|
| 83 |
|
valuesToRemoveList=Util::qStringListFromSpacedString(valuesToRemove); |
| 84 |
|
|
| 119 |
|
|
| 120 |
|
UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, "replace-value"); |
| 121 |
|
|
| 122 |
< |
UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); |
| 122 |
> |
// Check how the elements will be fetched via element name or xpath expression |
| 123 |
> |
if(this->xPathExpression==""){ |
| 124 |
> |
UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); |
| 125 |
> |
} |
| 126 |
> |
else{ |
| 127 |
> |
UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements); |
| 128 |
> |
} |
| 129 |
|
|
| 130 |
|
for(int j=0; j<elements.size(); j++){ |
| 131 |
|
currValuesList=Util::qStringListFromSpacedString(Util::toQString(elements[j].text().as_string())); // convert each element in a list (uses space as separator) |
| 159 |
|
|
| 160 |
|
UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, "replace-all"); |
| 161 |
|
|
| 162 |
< |
UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); |
| 162 |
> |
// Check how the elements will be fetched via element name or xpath expression |
| 163 |
> |
if(this->xPathExpression==""){ |
| 164 |
> |
UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); |
| 165 |
> |
} |
| 166 |
> |
else{ |
| 167 |
> |
UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements); |
| 168 |
> |
} |
| 169 |
|
|
| 170 |
|
|
| 171 |
|
// Let's start the override |
| 199 |
|
|
| 200 |
|
UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, "update-elements"); |
| 201 |
|
|
| 202 |
< |
UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); |
| 202 |
> |
// Check how the elements will be fetched via element name or xpath expression |
| 203 |
> |
if(this->xPathExpression==""){ |
| 204 |
> |
UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); |
| 205 |
> |
} |
| 206 |
> |
else{ |
| 207 |
> |
UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements); |
| 208 |
> |
} |
| 209 |
|
|
| 210 |
|
|
| 211 |
|
if(elements.size()>1){ |
| 246 |
|
QList<pugi::xml_node> elements; |
| 247 |
|
QStringList invertedElements; //Inverting the element order |
| 248 |
|
|
| 249 |
< |
UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); |
| 249 |
> |
// Check how the elements will be fetched via element name or xpath expression |
| 250 |
> |
if(this->xPathExpression==""){ |
| 251 |
> |
UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); |
| 252 |
> |
} |
| 253 |
> |
else{ |
| 254 |
> |
UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements); |
| 255 |
> |
} |
| 256 |
|
|
| 257 |
|
// Read all elements and save to the list |
| 258 |
|
for(int j=elements.size()-1; j>=0; j--){ |