#include "xmltools.h" // Filters constructor XmlTools::XmlTools(QString filesWildcard, XmlFilter filter, bool noBackups, bool noVerbose) { this->filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard); this->filters=filter; this->backupsEnabled=!noBackups; this->verboseEnabled=!noVerbose; if(this->filesToProcess.isEmpty()){ UtilXmlTools::displayErrorMessage("Loading xml files","No XML files were found for the wildcard: "+filesWildcard); } } // XPath constructor XmlTools::XmlTools(QString filesWildcard, QString xPathExpression, bool noBackups, bool noVerbose) { this->filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard); this->xPathExpression=xPathExpression; this->backupsEnabled=!noBackups; this->verboseEnabled=!noVerbose; } // Adds new values to an element void XmlTools::addValues(QString newValues){ // Process all XmlFiles for(int i=0; ifilesToProcess.size(); i++){ QStringList newValuesList, currValuesList; QList elements; UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled,this->verboseEnabled,"add-values"); newValuesList=Util::qStringListFromSpacedString(newValues); // Check how the elements will be fetched via element name or xpath expression if(this->xPathExpression==""){ UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); } else{ UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements); } for(int j=0; jfilesToProcess[i],this->document,"add-values"); } UtilXmlTools::displaySuccessMessage(this->filesToProcess.size(), "add-values"); } void XmlTools::removeValues(QString valuesToRemove){ // Process all XmlFiles for(int i=0; ifilesToProcess.size(); i++){ QList elements; QStringList valuesToRemoveList, currValuesList; bool elementChanged=false; UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, this->verboseEnabled, "remove-values"); // Check how the elements will be fetched via element name or xpath expression if(this->xPathExpression==""){ UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); } else{ UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements); } valuesToRemoveList=Util::qStringListFromSpacedString(valuesToRemove); for(int j=0; jfilesToProcess[i],this->document, "remove-values"); } UtilXmlTools::displaySuccessMessage(this->filesToProcess.size(),"remove-values"); } void XmlTools::replaceValue(QString oldValue, QString newValue){ // Process all XmlFiles for(int i=0; ifilesToProcess.size(); i++){ QList elements; QStringList currValuesList; bool elementChanged=false; UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, this->verboseEnabled, "replace-value"); // Check how the elements will be fetched via element name or xpath expression if(this->xPathExpression==""){ UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); } else{ UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements); } for(int j=0; jfilesToProcess[i],this->document, "replace-value"); } UtilXmlTools::displaySuccessMessage(this->filesToProcess.size(), "replace-value"); } // Replaces all current values of an element by another (can be specified only specific positions) void XmlTools::replaceAll(QString value, QString valuePositions){ // Process all XmlFiles for(int i=0; ifilesToProcess.size(); i++){ QList elements; UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, this->verboseEnabled, "replace-all"); // Check how the elements will be fetched via element name or xpath expression if(this->xPathExpression==""){ UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); } else{ UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements); } // Let's start the override for(int j=0; jfilesToProcess[i],this->document, "replace-all"); } UtilXmlTools::displaySuccessMessage(this->filesToProcess.size(), "replace-all"); } // Update a set of XML elements values based in the difference between the old and new value // This can be used in multiple files if the difference between one file and other are the same e.g. increment to all current object positions 100 in y (A difference of -100). // E.g. oldValue=5 , newValue=7; diffBetweenOldAndNewValue=-2 void XmlTools::updateElements(QString diffBetweenOldAndNewValue){ // Process all XmlFiles for(int i=0; ifilesToProcess.size(); i++){ QList elements; MultiDimVar lastXmlValue(0); // inicialize with any value or dimension MultiDimVar currXmlValue(0); MultiDimVar newXmlValue(0); // value that will update currValue UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled,this->verboseEnabled, "update-elements"); // Check how the elements will be fetched via element name or xpath expression if(this->xPathExpression==""){ UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); } else{ UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements); } if(elements.size()>1){ lastXmlValue=MultiDimVar(Util::toQString(elements[0].text().as_string())); // the lastXmlValue will begin to be the first one of the node currXmlValue=MultiDimVar(Util::toQString(elements[1].text().as_string())); // the currXmlValue will begin to be the second one of the node newXmlValue=MultiDimVar::sub(lastXmlValue, MultiDimVar(diffBetweenOldAndNewValue)); elements[0].text() = newXmlValue.toString().toLatin1().constData(); // update the first eblement with the new one already } // Let's start the node update for(int j=1; jfilesToProcess[i],this->document, "update-elements"); } UtilXmlTools::displaySuccessMessage(this->filesToProcess.size(),"updateNode"); } // Invert a set of XML elements with specified name (and optionally a parent name) void XmlTools::invertElements(){ // Process all XmlFiles for(int i=0; ifilesToProcess.size(); i++){ UtilXmlTools::loadXmlFile(this->filesToProcess[i],this->document,this->rootNode,this->backupsEnabled, this->verboseEnabled, "invert-elements"); QList elements; QStringList invertedElements; //Inverting the element order // Check how the elements will be fetched via element name or xpath expression if(this->xPathExpression==""){ UtilXmlTools::getAllNamedElements(this->rootNode,elements,this->filters); } else{ UtilXmlTools::getAllXpathElements(this->xPathExpression,this->document,elements); } // Read all elements and save to the list for(int j=elements.size()-1; j>=0; j--){ invertedElements << Util::toQString(elements[j].text().as_string()); } // Override the tree with the inverted order for(int j=0; jfilesToProcess[i],this->document, "invert-elements"); } UtilXmlTools::displaySuccessMessage(this->filesToProcess.size(),"invert-elements"); } // Replaces specific values (given the position) for a new value // [ currValues / positionsToReplaced are strings with composed by another strings space separated ] // Returns a new string (space separated) will values replaced QString XmlTools::replaceSpecificPositions(const QString &newValue, const QString &currValues, const QString &positionsToReplace){ QList positionsToReplaceList; QStringList currValuesList; positionsToReplaceList=Util::qListIntFromSpacedString(positionsToReplace); currValuesList=Util::qStringListFromSpacedString(currValues); // Make some validation before the replacing if(currValuesList.size()currValuesList.size()-1 || pos < 0){ //Are positions valid for the current values? //-1 because starts at 0 UtilXmlTools::displayErrorMessage("replaceSpecificPositions","One or more of the specified positions to replace are out of range."); } } // // Finally replace the specified values foreach(int pos, positionsToReplaceList){ currValuesList[pos]=newValue; } return currValuesList.join(" "); }