1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "private/qdeclarativedirparser_p.h"
43 #include "qdeclarativeerror.h"
44 #include <private/qdeclarativeglobal_p.h>
45 #include <private/qdeclarativeutils_p.h>
47 #include <QtCore/QTextStream>
48 #include <QtCore/QFile>
49 #include <QtCore/QtDebug>
53 QDeclarativeDirParser::QDeclarativeDirParser()
58 QDeclarativeDirParser::~QDeclarativeDirParser()
62 QUrl QDeclarativeDirParser::url() const
67 void QDeclarativeDirParser::setUrl(const QUrl &url)
72 QString QDeclarativeDirParser::fileSource() const
74 return _filePathSouce;
77 void QDeclarativeDirParser::setFileSource(const QString &filePath)
79 _filePathSouce = filePath;
82 QString QDeclarativeDirParser::source() const
87 void QDeclarativeDirParser::setSource(const QString &source)
93 bool QDeclarativeDirParser::isParsed() const
98 bool QDeclarativeDirParser::parse()
108 if (_source.isEmpty() && !_filePathSouce.isEmpty()) {
109 QFile file(_filePathSouce);
110 if (!QDeclarative_isFileCaseCorrect(_filePathSouce)) {
111 QDeclarativeError error;
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());
118 QDeclarativeError error;
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);
144 if (QDeclarativeUtils::isSpace(ch)) {
146 while (index != length && QDeclarativeUtils::isSpace(line.at(index)));
148 } else if (ch == QLatin1Char('#')) {
149 // recognized a comment
153 const int start = index;
156 while (index != length && !QDeclarativeUtils::isSpace(line.at(index)));
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].toUtf8(), 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].toUtf8(), 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 Component entry(sections[0].toUtf8(), sections[2], majorVersion, minorVersion);
226 _components.append(entry);
231 reportError(lineNumber, -1,
232 QString::fromUtf8("a component declaration requires two or three arguments, but %1 were provided").arg(sectionCount));
239 void QDeclarativeDirParser::reportError(int line, int column, const QString &description)
241 QDeclarativeError error;
244 error.setColumn(column);
245 error.setDescription(description);
246 _errors.append(error);
249 bool QDeclarativeDirParser::hasError() const
251 if (! _errors.isEmpty())
257 QList<QDeclarativeError> QDeclarativeDirParser::errors(const QString &uri) const
259 QList<QDeclarativeError> errors = _errors;
260 for (int i = 0; i < errors.size(); ++i) {
261 QDeclarativeError &e = errors[i];
262 QString description = e.description();
263 description.replace(QLatin1String("$$URI$$"), uri);
264 e.setDescription(description);
269 QList<QDeclarativeDirParser::Plugin> QDeclarativeDirParser::plugins() const
274 QList<QDeclarativeDirParser::Component> QDeclarativeDirParser::components() const
280 QList<QDeclarativeDirParser::TypeInfo> QDeclarativeDirParser::typeInfos() const