1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qqmldirparser_p.h"
43 #include "qqmlerror.h"
44 #include "qqmlglobal_p.h"
46 #include <QtCore/QTextStream>
47 #include <QtCore/QFile>
48 #include <QtCore/QtDebug>
52 QQmlDirParser::QQmlDirParser()
57 QQmlDirParser::~QQmlDirParser()
61 QUrl QQmlDirParser::url() const
66 void QQmlDirParser::setUrl(const QUrl &url)
71 QString QQmlDirParser::fileSource() const
73 return _filePathSouce;
76 void QQmlDirParser::setFileSource(const QString &filePath)
78 _filePathSouce = filePath;
81 QString QQmlDirParser::source() const
86 void QQmlDirParser::setSource(const QString &source)
92 bool QQmlDirParser::isParsed() const
97 bool QQmlDirParser::parse()
108 if (_source.isEmpty() && !_filePathSouce.isEmpty()) {
109 QFile file(_filePathSouce);
110 if (!QQml_isFileCaseCorrect(_filePathSouce)) {
112 error.setDescription(QString::fromUtf8("cannot load module \"$$URI$$\": File name case mismatch for \"%1\"").arg(_filePathSouce));
113 _errors.prepend(error);
115 } else if (file.open(QFile::ReadOnly)) {
116 _source = QString::fromUtf8(file.readAll());
119 error.setDescription(QString::fromUtf8("module \"$$URI$$\" definition \"%1\" not readable").arg(_filePathSouce));
120 _errors.prepend(error);
125 QTextStream stream(&_source);
131 const QString line = stream.readLine();
136 int sectionCount = 0;
139 const int length = line.length();
141 while (index != length) {
142 const QChar ch = line.at(index);
146 while (index != length && line.at(index).isSpace());
148 } else if (ch == QLatin1Char('#')) {
149 // recognized a comment
153 const int start = index;
156 while (index != length && !line.at(index).isSpace());
158 const QString lexeme = line.mid(start, index - start);
160 if (sectionCount >= 3) {
161 reportError(lineNumber, start, QLatin1String("unexpected token"));
164 sections[sectionCount++] = lexeme;
169 if (sectionCount == 0) {
170 continue; // no sections, no party.
172 } else if (sections[0] == QLatin1String("plugin")) {
173 if (sectionCount < 2) {
174 reportError(lineNumber, -1,
175 QString::fromUtf8("plugin directive requires one or two arguments, but %1 were provided").arg(sectionCount - 1));
180 const Plugin entry(sections[1], sections[2]);
182 _plugins.append(entry);
184 } else if (sections[0] == QLatin1String("internal")) {
185 if (sectionCount != 3) {
186 reportError(lineNumber, -1,
187 QString::fromUtf8("internal types require 2 arguments, but %1 were provided").arg(sectionCount - 1));
190 Component entry(sections[1], sections[2], -1, -1);
191 entry.internal = true;
192 _components.append(entry);
193 } else if (sections[0] == QLatin1String("typeinfo")) {
194 if (sectionCount != 2) {
195 reportError(lineNumber, -1,
196 QString::fromUtf8("typeinfo requires 1 argument, but %1 were provided").arg(sectionCount - 1));
200 TypeInfo typeInfo(sections[1]);
201 _typeInfos.append(typeInfo);
204 } else if (sectionCount == 2) {
205 // No version specified (should only be used for relative qmldir files)
206 const Component entry(sections[0], sections[1], -1, -1);
207 _components.append(entry);
208 } else if (sectionCount == 3) {
209 const QString &version = sections[1];
210 const int dotIndex = version.indexOf(QLatin1Char('.'));
212 if (dotIndex == -1) {
213 reportError(lineNumber, -1, QLatin1String("expected '.'"));
214 } else if (version.indexOf(QLatin1Char('.'), dotIndex + 1) != -1) {
215 reportError(lineNumber, -1, QLatin1String("unexpected '.'"));
217 bool validVersionNumber = false;
218 const int majorVersion = version.left(dotIndex).toInt(&validVersionNumber);
220 if (validVersionNumber) {
221 const int minorVersion = version.mid(dotIndex + 1).toInt(&validVersionNumber);
223 if (validVersionNumber) {
224 const QString &fileName = sections[2];
226 if (fileName.endsWith(QLatin1String(".js"))) {
227 // A 'js' extension indicates a namespaced script import
228 const Script entry(sections[0], fileName, majorVersion, minorVersion);
229 _scripts.append(entry);
231 const Component entry(sections[0], fileName, majorVersion, minorVersion);
232 _components.append(entry);
238 reportError(lineNumber, -1,
239 QString::fromUtf8("a component declaration requires two or three arguments, but %1 were provided").arg(sectionCount));
246 void QQmlDirParser::reportError(int line, int column, const QString &description)
251 error.setColumn(column);
252 error.setDescription(description);
253 _errors.append(error);
256 bool QQmlDirParser::hasError() const
258 if (! _errors.isEmpty())
264 QList<QQmlError> QQmlDirParser::errors(const QString &uri) const
266 QList<QQmlError> errors = _errors;
267 for (int i = 0; i < errors.size(); ++i) {
268 QQmlError &e = errors[i];
269 QString description = e.description();
270 description.replace(QLatin1String("$$URI$$"), uri);
271 e.setDescription(description);
276 QList<QQmlDirParser::Plugin> QQmlDirParser::plugins() const
281 QList<QQmlDirParser::Component> QQmlDirParser::components() const
286 QList<QQmlDirParser::Script> QQmlDirParser::scripts() const
292 QList<QQmlDirParser::TypeInfo> QQmlDirParser::typeInfos() const
298 QDebug &operator<< (QDebug &debug, const QQmlDirParser::Component &component)
300 const QString output = QString::fromLatin1("{%1 %2.%3}").
301 arg(component.typeName).arg(component.majorVersion).arg(component.minorVersion);
302 return debug << qPrintable(output);
305 QDebug &operator<< (QDebug &debug, const QQmlDirParser::Script &script)
307 const QString output = QString::fromLatin1("{%1 %2.%3}").
308 arg(script.nameSpace).arg(script.majorVersion).arg(script.minorVersion);
309 return debug << qPrintable(output);