r18655 by jghali - rewrite nftrcreader class with QXmlStreamReader instead of SAX to make it more simple

scribus-commit scribus-commit at lists.scribus.net
Mon Dec 16 23:32:14 UTC 2013


Author: jghali
Date: Mon Dec 16 23:32:13 2013
New Revision: 18655

URL: http://scribus.net/websvn/listing.php?repname=Scribus&sc=1&rev=18655
Log:
rewrite nftrcreader class with QXmlStreamReader instead of SAX to make it more simple

Modified:
    trunk/Scribus/scribus/nftrcreader.cpp
    trunk/Scribus/scribus/nftrcreader.h
    trunk/Scribus/scribus/nftsettings.cpp
    trunk/Scribus/scribus/nftsettings.h

Modified: trunk/Scribus/scribus/nftrcreader.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=18655&path=/trunk/Scribus/scribus/nftrcreader.cpp
==============================================================================
--- trunk/Scribus/scribus/nftrcreader.cpp (original)
+++ trunk/Scribus/scribus/nftrcreader.cpp Mon Dec 16 23:32:13 2013
@@ -19,168 +19,122 @@
 	setupCategories();
 }
 
-bool nftrcreader::startDocument()
+bool nftrcreader::parse(QString filePath)
 {
-	inSettings       = false;
-	inTemplate       = false;
-	inName           = false;
-	inFile           = false;
-	inTNail          = false;
-	inImg            = false;
-	inPSize          = false;
-	inColor          = false;
-	inDescr          = false;
-	inUsage          = false;
-	inScribusVersion = false;
-	inDate           = false;
-	inAuthor         = false;
-	inEmail          = false;
-	return true;
+	bool firstElement = true;
+
+	QFile file(filePath);
+	if (!file.open(QFile::ReadOnly))
+		return false;
+
+	QXmlStreamReader reader(&file);
+	while (!reader.atEnd() && !reader.hasError())
+	{
+		QXmlStreamReader::TokenType tType = reader.readNext();
+		if (tType != QXmlStreamReader::StartElement)
+			continue;
+
+		QStringRef tagName = reader.name();
+		if (firstElement)
+		{
+			if (tagName != "templates")
+				return false;
+			firstElement = false;
+			continue;
+		}
+
+		if (tagName == "template")
+		{
+			QXmlStreamAttributes attrs = reader.attributes();
+			QString attrCat  = attrs.value("category").toString();
+			QString category = getCategory(attrCat);
+
+			nfttemplate* tmpTemplate = new nfttemplate(currentFile, category);
+			tmpTemplate->enCategory = attrCat;
+			if (parseTemplate(reader, tmpTemplate))
+				templates->push_back(tmpTemplate);
+			else
+				delete tmpTemplate;
+		}
+		else
+		{
+			reader.skipCurrentElement();
+		}
+	}
+
+	return (!reader.hasError());
 }
 
-bool nftrcreader::startElement(const QString&, const QString&, const QString &name, const QXmlAttributes &attrs) 
+bool nftrcreader::parseTemplate(QXmlStreamReader& reader, nfttemplate* nftTemplate)
 {
-	if (name == "name")
-		inName = true;
-	else if (name == "file")
-		inFile = true;
-	else if (name == "tnail")
-		inTNail = true;
-	else if (name == "img")
-		inImg = true;
-	else if (name == "psize")
-		inPSize = true;
-	else if (name == "color") 
-		inColor = true;
-	else if (name == "descr")
-		inDescr = true;
-	else if (name == "usage")
-		inUsage = true;
-	else if (name == "scribus_version")
-		inScribusVersion = true;
-	else if (name == "date")
-		inDate = true;
-	else if (name == "author")
-		inAuthor = true;
-	else if (name == "email")
-		inEmail = true;
+	if (!reader.isStartElement())
+		return false;
 
+	QStringRef elemName = reader.name();
+	if (elemName != "template")
+		return false;
 
-	if (name == "template") // new template starts here
+	while (!reader.atEnd() && !reader.hasError())
 	{
-		inTemplate = true;
-		QString category;
-		QString enCat;
-		for (int i = 0; i < attrs.count(); i++)
+		QXmlStreamReader::TokenType tType = reader.readNext();
+		if (reader.hasError())
+			return false;
+
+		if (tType == QXmlStreamReader::EndElement)
+			break;
+		if (tType != QXmlStreamReader::StartElement)
+			continue;
+
+		QStringRef tagName = reader.name();
+		if (tagName == "name")
+			nftTemplate->name = reader.readElementText();
+		else if (tagName == "file")
 		{
-			if (attrs.localName(i) == "category")
-			{
-				category = getCategory(attrs.value(i));
-				enCat = attrs.value(i);
-			}
+			QString tmp = reader.readElementText();
+			if (tmp.left(1) == "/")
+				nftTemplate->file = tmp;
+			else 
+				nftTemplate->file = currentDir + "/" + tmp;
 		}
-		tmpTemplate = new nfttemplate(currentFile, category); // create a new template
-		tmpTemplate->enCategory = enCat;
+		else if (tagName == "tnail")
+		{
+			QString tmp = reader.readElementText();
+			if (tmp.left(1) == "/")
+				nftTemplate->tnail = tmp;
+			else 
+				nftTemplate->tnail = currentDir + "/" + tmp;
+		}
+		else if (tagName == "img")
+		{
+			QString tmp = reader.readElementText();
+			if (tmp.left(1) == "/")
+				nftTemplate->img = tmp;
+			else 
+				nftTemplate->img = currentDir + "/" + tmp;
+		}
+		else if (tagName == "psize")
+			nftTemplate->psize = reader.readElementText();
+		else if (tagName == "color")
+			nftTemplate->color = reader.readElementText();
+		else if (tagName == "descr")
+			nftTemplate->descr = reader.readElementText();
+		else if (tagName == "usage")
+			nftTemplate->usage = reader.readElementText();
+		else if (tagName == "scribus_version")
+			nftTemplate->scribusVersion = reader.readElementText();
+		else if (tagName == "date")
+			nftTemplate->date = reader.readElementText();
+		else if (tagName == "author")
+			nftTemplate->author = reader.readElementText();
+		else if (tagName == "email")
+			nftTemplate->email = reader.readElementText();
+		else
+		{
+			reader.skipCurrentElement();
+		}
 	}
 
-	if (name == "settings") 
-		inSettings = true;
-
-	return true;
-}
-
-bool nftrcreader::characters(const QString &ch) 
-{
-	QString tmp(ch);
-	if (inName)
-		tmpTemplate->name = tmp;
-	else if (inFile)
-	{
-		if (tmp.left(1) == "/")
-			tmpTemplate->file = tmp;
-		else 
-			tmpTemplate->file = currentDir + "/" + tmp;
-	} else if (inTNail)
-	{
-		if (tmp.left(1) == "/")
-			tmpTemplate->tnail = tmp;
-		else
-			tmpTemplate->tnail = currentDir + "/" + tmp;
-	} else if (inImg)
-	{
-		if (tmp.left(1) == "/") 
-			tmpTemplate->img = tmp;
-		else
-			tmpTemplate->img = currentDir + "/" + tmp;
-	} else if (inPSize)
-		tmpTemplate->psize = tmp; 
-	else if (inColor)
-		tmpTemplate->color = tmp; 
-	else if (inDescr)
-		tmpTemplate->descr = tmp; 
-	else if (inUsage)
-		tmpTemplate->usage = tmp;
-	else if (inScribusVersion)
-		tmpTemplate->scribusVersion = tmp;
-	else if (inDate)
-		tmpTemplate->date = tmp;
-	else if (inAuthor)
-		tmpTemplate->author = tmp;
-	else if (inEmail)
-		tmpTemplate->email = tmp; 
-
-	return true;
-}
-
-bool nftrcreader::endElement(const QString&, const QString&, const QString &name)
-{
-	if (name == "template")
-	{ // new template starts here
-		inTemplate = false;
-		if (tmpTemplate != NULL) // If we have a template already created
-		{
-			if (tmpTemplate->isValid()) // and the template really exists push
-				templates->push_back(tmpTemplate); // it to the templates vector and start a new one
-			else
-			{
-				delete tmpTemplate;
-				tmpTemplate = 0;
-			}
-		}
-	}
-	if (inTemplate) {
-		if (inName && name == "name")
-			inName = false;
-		else if (inFile && name == "file")
-			inFile = false;
-		else if (inTNail && name == "tnail")
-			inTNail = false;
-		else if (inImg && name == "img")
-			inImg = false;
-		else if (inPSize && name == "psize")
-			inPSize = false;
-		else if (inColor && name == "color") 
-			inColor = false;
-		else if (inDescr && name == "descr")
-			inDescr = false;
-		else if (inUsage && name == "usage")
-			inUsage = false;
-		else if (inScribusVersion && name == "scribus_version")
-			inScribusVersion = false;
-		else if (inDate && name == "date")
-			inDate = false;
-		else if (inAuthor && name == "author")
-			inAuthor = false;
-		else if (inEmail && name == "email")
-			inEmail = false;
-		else if (inTemplate && name == "template")
-			inTemplate = false;
-	}
-	
-	if (inSettings && name == "settings")
-		inSettings = false;
-
-	return true;
+	return (nftTemplate->isValid());
 }
 
 void nftrcreader::setSourceDir(QString source) 

Modified: trunk/Scribus/scribus/nftrcreader.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=18655&path=/trunk/Scribus/scribus/nftrcreader.h
==============================================================================
--- trunk/Scribus/scribus/nftrcreader.h (original)
+++ trunk/Scribus/scribus/nftrcreader.h Mon Dec 16 23:32:13 2013
@@ -12,47 +12,37 @@
 
 #include <vector>
 #include <utility>
-#include <QObject>
+
 #include <QDir>
 #include <QMap>
+#include <QObject>
 #include <QString>
 #include <QStringList>
-#include <QXmlDefaultHandler>
+#include <QXmlStreamReader>
+
 #include "nfttemplate.h"
 
-class nftrcreader : public QXmlDefaultHandler
+class nftrcreader
 {
+public:
+	nftrcreader(std::vector<nfttemplate*> *tmplts, QString sourceDir);
+
+	bool parse(QString filePath);
+
+	void setSourceDir(QString source);
+	void setSourceFile(QString sourceFile);
+
 private:
-	bool inSettings;
-	bool inTemplate;
-	bool inName;
-	bool inFile;
-	bool inTNail;
-	bool inImg;
-	bool inPSize;
-	bool inColor;
-	bool inDescr;
-	bool inUsage;
-	bool inScribusVersion;
-	bool inDate;
-	bool inAuthor;
-	bool inEmail;
 	QString currentDir;
 	QString currentFile;
-	QString templateCategory;
-	nfttemplate* tmpTemplate;
+
 	std::vector<nfttemplate*> *templates;
 	QMap<QString, QString> cats;
+
+	bool parseTemplate(QXmlStreamReader& reader, nfttemplate* nftTemplate);
+
 	QString getCategory(QString cat);
 	void setupCategories();
-public:
-	nftrcreader(std::vector<nfttemplate*> *tmplts,QString sourceDir);
-	bool startDocument();
-	bool startElement(const QString&, const QString&, const QString &name, const QXmlAttributes &attrs);
-	bool endElement(const QString&, const QString&, const QString &name);
-	bool characters(const QString &ch);
-	void setSourceDir(QString source);
-	void setSourceFile(QString sourceFile);
 };
 
 #endif

Modified: trunk/Scribus/scribus/nftsettings.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=18655&path=/trunk/Scribus/scribus/nftsettings.cpp
==============================================================================
--- trunk/Scribus/scribus/nftsettings.cpp (original)
+++ trunk/Scribus/scribus/nftsettings.cpp Mon Dec 16 23:32:13 2013
@@ -23,34 +23,26 @@
 
 void nftsettings::read()
 {
-	handler = new nftrcreader(&templates,scribusUserHome);
-	reader = new QXmlSimpleReader();
-	reader->setContentHandler(handler);
+	nftrcreader reader(&templates ,scribusUserHome);
 
-	addTemplates(scribusShare);
-	addTemplates(scribusUserHome+"/templates");
+	addTemplates(reader, scribusShare);
+	addTemplates(reader, scribusUserHome+"/templates");
 	QString userTemplateDir(PrefsManager::instance()->appPrefs.pathPrefs.documentTemplates);
 	if (userTemplateDir.right(1) == "/")
 		userTemplateDir.chop(1);
 	if ((!userTemplateDir.isNull()) && (!userTemplateDir.isEmpty()))
-		addTemplates(userTemplateDir);
+		addTemplates(reader, userTemplateDir);
 }
 
-void nftsettings::addTemplates(QString dir) // dir will be searched for a sub folder called templates
+void nftsettings::addTemplates(nftrcreader& reader, QString dir) // dir will be searched for a sub folder called templates
 {
 	// Add templates from the dir itself
 	QString tmplFile = findTemplateXml(dir);
-	QFile* tmplxml = new QFile(QDir::toNativeSeparators(tmplFile));
-	handler->setSourceDir(dir);
-	handler->setSourceFile(tmplFile);
-	if (tmplxml->exists())
-	{
-		QXmlInputSource* source = new QXmlInputSource(tmplxml);
-		reader->parse(source);
-		delete source;
-	}
-	delete tmplxml;
-	
+	QString tmplFilePath = QDir::toNativeSeparators(tmplFile);
+	reader.setSourceDir(dir);
+	reader.setSourceFile(tmplFile);
+	if (QFile::exists(tmplFilePath))
+		reader.parse(tmplFilePath);
 	
 	// And from all the subdirectories. template.xml file is only searched one dir level deeper than the dir
 	QDir tmpldir(dir);
@@ -61,16 +53,11 @@
 		for (int i = 0; i < dirs.size(); ++i)
 		{
 			tmplFile = findTemplateXml(dir + "/" + dirs[i]);
-			QFile* tmplxml = new QFile(QDir::toNativeSeparators(tmplFile));
-			handler->setSourceDir(dir+"/"+dirs[i]);
-			handler->setSourceFile(tmplFile);
-			if (tmplxml->exists())
-			{
-				QXmlInputSource* source = new QXmlInputSource(tmplxml);
-				reader->parse(source);
-				delete source;
-			}
-			delete tmplxml;
+			tmplFilePath = QDir::toNativeSeparators(tmplFile);
+			reader.setSourceDir(dir+"/"+dirs[i]);
+			reader.setSourceFile(tmplFile);
+			if (QFile::exists(tmplFilePath))
+				reader.parse(tmplFilePath);
 		}
 	}
 }
@@ -92,8 +79,6 @@
 
 nftsettings::~ nftsettings()
 {
-	delete reader;
-	delete handler;
 	for (uint i = 0; i < templates.size(); ++i)
 	{
 		if (templates[i] != NULL)

Modified: trunk/Scribus/scribus/nftsettings.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=18655&path=/trunk/Scribus/scribus/nftsettings.h
==============================================================================
--- trunk/Scribus/scribus/nftsettings.h (original)
+++ trunk/Scribus/scribus/nftsettings.h Mon Dec 16 23:32:13 2013
@@ -12,9 +12,11 @@
 
 #include "scconfig.h"
 #include <vector>
-#include <QString>
+
 #include <QDir>
 #include <QFile>
+#include <QString>
+
 #include "nfttemplate.h"
 #include "nftrcreader.h"
  
@@ -24,15 +26,16 @@
 	QString scribusUserHome;
 	QString nftHomeDir;
 	QString lang;
-	nftrcreader* handler;
-	QXmlSimpleReader* reader;
+
 	void read();
-	void addTemplates(QString dir);
+	void addTemplates(nftrcreader& reader, QString dir);
 	QString findTemplateXml(QString dir);
+
 public:
-	std::vector<nfttemplate*> templates;
 	nftsettings(QString guilang);
 	~nftsettings();
+
+	std::vector<nfttemplate*> templates;
 };
 
 #endif // NFTSETTINGS_H




More information about the scribus-commit mailing list