TypeDescriptionReader: Check for files that are not UTF8 encoded

We had a qmltypes file that was encoded in UTF16 and did not work.
We should at least warn about it.

Change-Id: I42555782ee16ddd25552f919845aa85ff1f3f636
Reviewed-by: Fawzi Mohamed <fawzi.mohamed@digia.com>
This commit is contained in:
Thomas Hartmann
2013-05-15 11:11:32 +02:00
parent 3604bdbad2
commit 7d25b90908
3 changed files with 24 additions and 9 deletions

View File

@@ -39,6 +39,7 @@
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <QApplication>
#include <QFile> #include <QFile>
#include <QDir> #include <QDir>
#include <QString> #include <QString>
@@ -1253,7 +1254,8 @@ CppQmlTypesLoader::BuiltinObjects CppQmlTypesLoader::loadQmlTypes(const QFileInf
QByteArray contents = file.readAll(); QByteArray contents = file.readAll();
file.close(); file.close();
parseQmlTypeDescriptions(contents, &newObjects, 0, &error, &warning);
parseQmlTypeDescriptions(contents, &newObjects, 0, &error, &warning, qmlTypeFile.absoluteFilePath());
} else { } else {
error = file.errorString(); error = file.errorString();
} }
@@ -1272,15 +1274,28 @@ CppQmlTypesLoader::BuiltinObjects CppQmlTypesLoader::loadQmlTypes(const QFileInf
return newObjects; return newObjects;
} }
void CppQmlTypesLoader::parseQmlTypeDescriptions(const QByteArray &xml, void CppQmlTypesLoader::parseQmlTypeDescriptions(const QByteArray &contents,
BuiltinObjects *newObjects, BuiltinObjects *newObjects,
QList<ModuleApiInfo> *newModuleApis, QList<ModuleApiInfo> *newModuleApis,
QString *errorMessage, QString *errorMessage,
QString *warningMessage) QString *warningMessage, const QString &fileName)
{ {
if (!contents.isEmpty()) {
unsigned char c = contents.at(0);
switch (c) {
case 0xfe:
case 0xef:
case 0xff:
case 0xee:
case 0x00:
qWarning() << QApplication::translate("CppQmlTypesLoader", "%1 seems not to be encoded in UTF8 or has a BOM.").arg(fileName);
default: break;
}
}
errorMessage->clear(); errorMessage->clear();
warningMessage->clear(); warningMessage->clear();
TypeDescriptionReader reader(QString::fromUtf8(xml)); TypeDescriptionReader reader(QString::fromUtf8(contents));
if (!reader(newObjects, newModuleApis)) { if (!reader(newObjects, newModuleApis)) {
if (reader.errorMessage().isEmpty()) if (reader.errorMessage().isEmpty())
*errorMessage = QLatin1String("unknown error"); *errorMessage = QLatin1String("unknown error");

View File

@@ -608,10 +608,9 @@ public:
static BuiltinObjects defaultLibraryObjects; static BuiltinObjects defaultLibraryObjects;
// parses the contents of a qmltypes file and fills the newObjects map // parses the contents of a qmltypes file and fills the newObjects map
static void parseQmlTypeDescriptions( static void parseQmlTypeDescriptions(const QByteArray &contents,
const QByteArray &qmlTypes,
BuiltinObjects *newObjects, BuiltinObjects *newObjects,
QList<ModuleApiInfo> *newModuleApis, QString *errorMessage, QString *warningMessage); QList<ModuleApiInfo> *newModuleApis, QString *errorMessage, QString *warningMessage, const QString &fileName);
}; };
class QMLJS_EXPORT CppQmlTypes class QMLJS_EXPORT CppQmlTypes

View File

@@ -326,7 +326,8 @@ void PluginDumper::qmlPluginTypeDumpDone(int exitCode)
QString warning; QString warning;
CppQmlTypesLoader::BuiltinObjects objectsList; CppQmlTypesLoader::BuiltinObjects objectsList;
QList<ModuleApiInfo> moduleApis; QList<ModuleApiInfo> moduleApis;
CppQmlTypesLoader::parseQmlTypeDescriptions(output, &objectsList, &moduleApis, &error, &warning); CppQmlTypesLoader::parseQmlTypeDescriptions(output, &objectsList, &moduleApis, &error, &warning,
QLatin1String("<dump of ") + libraryPath + QLatin1String(">"));
if (exitCode == 0) { if (exitCode == 0) {
if (!error.isEmpty()) { if (!error.isEmpty()) {
libraryInfo.setPluginTypeInfoStatus(LibraryInfo::DumpError, libraryInfo.setPluginTypeInfoStatus(LibraryInfo::DumpError,
@@ -397,7 +398,7 @@ void PluginDumper::loadQmltypesFile(const QStringList &qmltypesFilePaths,
QString warning; QString warning;
CppQmlTypesLoader::BuiltinObjects newObjects; CppQmlTypesLoader::BuiltinObjects newObjects;
QList<ModuleApiInfo> newModuleApis; QList<ModuleApiInfo> newModuleApis;
CppQmlTypesLoader::parseQmlTypeDescriptions(reader.data(), &newObjects, &newModuleApis, &error, &warning); CppQmlTypesLoader::parseQmlTypeDescriptions(reader.data(), &newObjects, &newModuleApis, &error, &warning, qmltypesFilePath);
if (!error.isEmpty()) { if (!error.isEmpty()) {
errors += tr("Failed to parse '%1'.\nError: %2").arg(qmltypesFilePath, error); errors += tr("Failed to parse '%1'.\nError: %2").arg(qmltypesFilePath, error);
} else { } else {