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>
46 #include <QtCore/QTextStream>
47 #include <QtCore/QFile>
48 #include <QtCore/QtDebug>
52 QDeclarativeDirParser::QDeclarativeDirParser()
57 QDeclarativeDirParser::~QDeclarativeDirParser()
61 QUrl QDeclarativeDirParser::url() const
66 void QDeclarativeDirParser::setUrl(const QUrl &url)
71 QString QDeclarativeDirParser::fileSource() const
73 return _filePathSouce;
76 void QDeclarativeDirParser::setFileSource(const QString &filePath)
78 _filePathSouce = filePath;
81 QString QDeclarativeDirParser::source() const
86 void QDeclarativeDirParser::setSource(const QString &source)
92 bool QDeclarativeDirParser::isParsed() const
97 bool QDeclarativeDirParser::parse()
107 if (_source.isEmpty() && !_filePathSouce.isEmpty()) {
108 QFile file(_filePathSouce);
109 if (!QDeclarative_isFileCaseCorrect(_filePathSouce)) {
110 QDeclarativeError error;
111 error.setDescription(QString::fromUtf8("cannot load module \"$$URI$$\": File name case mismatch for \"%1\"").arg(_filePathSouce));
112 _errors.prepend(error);
114 } else if (file.open(QFile::ReadOnly)) {
115 _source = QString::fromUtf8(file.readAll());
117 QDeclarativeError error;
118 error.setDescription(QString::fromUtf8("module \"$$URI$$\" definition \"%1\" not readable").arg(_filePathSouce));
119 _errors.prepend(error);
124 QTextStream stream(&_source);
130 const QString line = stream.readLine();
135 int sectionCount = 0;
138 const int length = line.length();
140 while (index != length) {
141 const QChar ch = line.at(index);
145 while (index != length && line.at(index).isSpace());
147 } else if (ch == QLatin1Char('#')) {
148 // recognized a comment
152 const int start = index;
155 while (index != length && !line.at(index).isSpace());
157 const QString lexeme = line.mid(start, index - start);
159 if (sectionCount >= 3) {
160 reportError(lineNumber, start, QLatin1String("unexpected token"));
163 sections[sectionCount++] = lexeme;
168 if (sectionCount == 0) {
169 continue; // no sections, no party.
171 } else if (sections[0] == QLatin1String("plugin")) {
172 if (sectionCount < 2) {
173 reportError(lineNumber, -1,
174 QString::fromUtf8("plugin directive requires one or two arguments, but %1 were provided").arg(sectionCount - 1));
179 const Plugin entry(sections[1], sections[2]);
181 _plugins.append(entry);
183 } else if (sections[0] == QLatin1String("internal")) {
184 if (sectionCount != 3) {
185 reportError(lineNumber, -1,
186 QString::fromUtf8("internal types require 2 arguments, but %1 were provided").arg(sectionCount - 1));
189 Component entry(sections[1], sections[2], -1, -1);
190 entry.internal = true;
191 _components.append(entry);
192 } else if (sections[0] == QLatin1String("typeinfo")) {
193 if (sectionCount != 2) {
194 reportError(lineNumber, -1,
195 QString::fromUtf8("typeinfo requires 1 argument, but %1 were provided").arg(sectionCount - 1));
199 TypeInfo typeInfo(sections[1]);
200 _typeInfos.append(typeInfo);
203 } else if (sectionCount == 2) {
204 // No version specified (should only be used for relative qmldir files)
205 const Component entry(sections[0], sections[1], -1, -1);
206 _components.append(entry);
207 } else if (sectionCount == 3) {
208 const QString &version = sections[1];
209 const int dotIndex = version.indexOf(QLatin1Char('.'));
211 if (dotIndex == -1) {
212 reportError(lineNumber, -1, QLatin1String("expected '.'"));
213 } else if (version.indexOf(QLatin1Char('.'), dotIndex + 1) != -1) {
214 reportError(lineNumber, -1, QLatin1String("unexpected '.'"));
216 bool validVersionNumber = false;
217 const int majorVersion = version.left(dotIndex).toInt(&validVersionNumber);
219 if (validVersionNumber) {
220 const int minorVersion = version.mid(dotIndex + 1).toInt(&validVersionNumber);
222 if (validVersionNumber) {
223 const Component entry(sections[0], sections[2], majorVersion, minorVersion);
225 _components.append(entry);
230 reportError(lineNumber, -1,
231 QString::fromUtf8("a component declaration requires two or three arguments, but %1 were provided").arg(sectionCount));
238 void QDeclarativeDirParser::reportError(int line, int column, const QString &description)
240 QDeclarativeError error;
243 error.setColumn(column);
244 error.setDescription(description);
245 _errors.append(error);
248 bool QDeclarativeDirParser::hasError() const
250 if (! _errors.isEmpty())
256 QList<QDeclarativeError> QDeclarativeDirParser::errors(const QString &uri) const
258 QList<QDeclarativeError> errors = _errors;
259 for (int i = 0; i < errors.size(); ++i) {
260 QDeclarativeError &e = errors[i];
261 QString description = e.description();
262 description.replace(QLatin1String("$$URI$$"), uri);
263 e.setDescription(description);
268 QList<QDeclarativeDirParser::Plugin> QDeclarativeDirParser::plugins() const
273 QList<QDeclarativeDirParser::Component> QDeclarativeDirParser::components() const
279 QList<QDeclarativeDirParser::TypeInfo> QDeclarativeDirParser::typeInfos() const