Commit 1c70f27a authored by Albert Astals Cid's avatar Albert Astals Cid
Browse files

Make KXmlGuiVersionHandler::findVersionNumber public in KXMLGUIClient

We need it in okular
parent 06a91326
......@@ -148,7 +148,7 @@ void KXmlGui_UnitTest::testFindVersionNumber()
{
QFETCH(QString, xml);
QFETCH(QString, version);
QCOMPARE(KXmlGuiVersionHandler::findVersionNumber(xml), version);
QCOMPARE(KXMLGUIClient::findVersionNumber(xml), version);
}
void KXmlGui_UnitTest::testVersionHandlerSameVersion()
......
......@@ -806,3 +806,77 @@ void KXMLGUIClient::virtual_hook(int, void *)
{
/*BASE::virtual_hook( id, data );*/
}
QString KXMLGUIClient::findVersionNumber(const QString &xml)
{
enum { ST_START, ST_AFTER_OPEN, ST_AFTER_GUI,
ST_EXPECT_VERSION, ST_VERSION_NUM
} state = ST_START;
const int length = xml.length();
for (int pos = 0; pos < length; pos++) {
switch (state) {
case ST_START:
if (xml[pos] == QLatin1Char('<')) {
state = ST_AFTER_OPEN;
}
break;
case ST_AFTER_OPEN: {
//Jump to gui..
const int guipos = xml.indexOf(QLatin1String("gui"), pos, Qt::CaseInsensitive);
if (guipos == -1) {
return QString(); //Reject
}
pos = guipos + 2; //Position at i, so we're moved ahead to the next character by the ++;
state = ST_AFTER_GUI;
break;
}
case ST_AFTER_GUI:
state = ST_EXPECT_VERSION;
break;
case ST_EXPECT_VERSION: {
const int verpos = xml.indexOf(QLatin1String("version"), pos, Qt::CaseInsensitive);
if (verpos == -1) {
return QString(); //Reject
}
pos = verpos + 7; // strlen("version") is 7
while (xml.at(pos).isSpace()) {
++pos;
}
if (xml.at(pos++) != QLatin1Char('=')) {
return QString(); //Reject
}
while (xml.at(pos).isSpace()) {
++pos;
}
state = ST_VERSION_NUM;
break;
}
case ST_VERSION_NUM: {
int endpos;
for (endpos = pos; endpos < length; endpos++) {
const ushort ch = xml[endpos].unicode();
if (ch >= QLatin1Char('0') && ch <= QLatin1Char('9')) {
continue; //Number..
}
if (ch == QLatin1Char('"')) { //End of parameter
break;
} else { //This shouldn't be here..
endpos = length;
}
}
if (endpos != pos && endpos < length) {
const QString matchCandidate = xml.mid(pos, endpos - pos); //Don't include " ".
return matchCandidate;
}
state = ST_EXPECT_VERSION; //Try to match a well-formed version..
break;
} //case..
} //switch
} //for
return QString();
}
......@@ -298,6 +298,13 @@ public:
*/
void replaceXMLFile(const QString &xmlfile, const QString &localxmlfile, bool merge = false);
/**
* Returns the version number of the given xml data (belonging to an xml rc file)
*
* @since 5.73
*/
static QString findVersionNumber(const QString &xml);
protected:
/**
* Sets the component name for this part.
......
......@@ -21,6 +21,7 @@
#include "kxmlguiversionhandler_p.h"
#include "kxmlguiclient.h"
#include "kxmlguifactory.h"
#include <QFile>
......@@ -169,80 +170,6 @@ static void storeActionProperties(QDomDocument &doc,
}
}
QString KXmlGuiVersionHandler::findVersionNumber(const QString &xml)
{
enum { ST_START, ST_AFTER_OPEN, ST_AFTER_GUI,
ST_EXPECT_VERSION, ST_VERSION_NUM
} state = ST_START;
const int length = xml.length();
for (int pos = 0; pos < length; pos++) {
switch (state) {
case ST_START:
if (xml[pos] == QLatin1Char('<')) {
state = ST_AFTER_OPEN;
}
break;
case ST_AFTER_OPEN: {
//Jump to gui..
const int guipos = xml.indexOf(QLatin1String("gui"), pos, Qt::CaseInsensitive);
if (guipos == -1) {
return QString(); //Reject
}
pos = guipos + 2; //Position at i, so we're moved ahead to the next character by the ++;
state = ST_AFTER_GUI;
break;
}
case ST_AFTER_GUI:
state = ST_EXPECT_VERSION;
break;
case ST_EXPECT_VERSION: {
const int verpos = xml.indexOf(QLatin1String("version"), pos, Qt::CaseInsensitive);
if (verpos == -1) {
return QString(); //Reject
}
pos = verpos + 7; // strlen("version") is 7
while (xml.at(pos).isSpace()) {
++pos;
}
if (xml.at(pos++) != QLatin1Char('=')) {
return QString(); //Reject
}
while (xml.at(pos).isSpace()) {
++pos;
}
state = ST_VERSION_NUM;
break;
}
case ST_VERSION_NUM: {
int endpos;
for (endpos = pos; endpos < length; endpos++) {
const ushort ch = xml[endpos].unicode();
if (ch >= QLatin1Char('0') && ch <= QLatin1Char('9')) {
continue; //Number..
}
if (ch == QLatin1Char('"')) { //End of parameter
break;
} else { //This shouldn't be here..
endpos = length;
}
}
if (endpos != pos && endpos < length) {
const QString matchCandidate = xml.mid(pos, endpos - pos); //Don't include " ".
return matchCandidate;
}
state = ST_EXPECT_VERSION; //Try to match a well-formed version..
break;
} //case..
} //switch
} //for
return QString();
}
KXmlGuiVersionHandler::KXmlGuiVersionHandler(const QStringList &files)
{
Q_ASSERT(!files.isEmpty());
......@@ -269,7 +196,7 @@ KXmlGuiVersionHandler::KXmlGuiVersionHandler(const QStringList &files)
QList<DocStruct>::iterator docIt = allDocuments.begin();
const QList<DocStruct>::iterator docEnd = allDocuments.end();
for (; docIt != docEnd; ++docIt) {
const QString versionStr = findVersionNumber((*docIt).data);
const QString versionStr = KXMLGUIClient::findVersionNumber((*docIt).data);
if (versionStr.isEmpty()) {
//qCDebug(DEBUG_KXMLGUI) << "found no version in" << (*docIt).file;
continue;
......
......@@ -49,8 +49,6 @@ public:
return m_doc;
}
static QString findVersionNumber(const QString &xml); // used by the unit test
private:
QString m_file;
QString m_doc;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment