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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "private/qdeclarativedirparser_p.h"
43 #include "qdeclarativeerror.h"
45 #include <QtCore/QTextStream>
46 #include <QtCore/QtDebug>
50 QDeclarativeDirParser::QDeclarativeDirParser()
55 QDeclarativeDirParser::~QDeclarativeDirParser()
59 QUrl QDeclarativeDirParser::url() const
64 void QDeclarativeDirParser::setUrl(const QUrl &url)
69 QString QDeclarativeDirParser::source() const
74 void QDeclarativeDirParser::setSource(const QString &source)
80 bool QDeclarativeDirParser::isParsed() const
85 bool QDeclarativeDirParser::parse()
95 QTextStream stream(&_source);
101 const QString line = stream.readLine();
106 int sectionCount = 0;
109 const int length = line.length();
111 while (index != length) {
112 const QChar ch = line.at(index);
116 while (index != length && line.at(index).isSpace());
118 } else if (ch == QLatin1Char('#')) {
119 // recognized a comment
123 const int start = index;
126 while (index != length && !line.at(index).isSpace());
128 const QString lexeme = line.mid(start, index - start);
130 if (sectionCount >= 3) {
131 reportError(lineNumber, start, QLatin1String("unexpected token"));
134 sections[sectionCount++] = lexeme;
139 if (sectionCount == 0) {
140 continue; // no sections, no party.
142 } else if (sections[0] == QLatin1String("plugin")) {
143 if (sectionCount < 2) {
144 reportError(lineNumber, -1,
145 QString::fromUtf8("plugin directive requires one or two arguments, but %1 were provided").arg(sectionCount - 1));
150 const Plugin entry(sections[1], sections[2]);
152 _plugins.append(entry);
154 } else if (sections[0] == QLatin1String("internal")) {
155 if (sectionCount != 3) {
156 reportError(lineNumber, -1,
157 QString::fromUtf8("internal types require 2 arguments, but %1 were provided").arg(sectionCount - 1));
160 Component entry(sections[1], sections[2], -1, -1);
161 entry.internal = true;
162 _components.append(entry);
163 } else if (sections[0] == QLatin1String("typeinfo")) {
164 if (sectionCount != 2) {
165 reportError(lineNumber, -1,
166 QString::fromUtf8("typeinfo requires 1 argument, but %1 were provided").arg(sectionCount - 1));
170 TypeInfo typeInfo(sections[1]);
171 _typeInfos.append(typeInfo);
174 } else if (sectionCount == 2) {
175 // No version specified (should only be used for relative qmldir files)
176 const Component entry(sections[0], sections[1], -1, -1);
177 _components.append(entry);
178 } else if (sectionCount == 3) {
179 const QString &version = sections[1];
180 const int dotIndex = version.indexOf(QLatin1Char('.'));
182 if (dotIndex == -1) {
183 reportError(lineNumber, -1, QLatin1String("expected '.'"));
184 } else if (version.indexOf(QLatin1Char('.'), dotIndex + 1) != -1) {
185 reportError(lineNumber, -1, QLatin1String("unexpected '.'"));
187 bool validVersionNumber = false;
188 const int majorVersion = version.left(dotIndex).toInt(&validVersionNumber);
190 if (validVersionNumber) {
191 const int minorVersion = version.mid(dotIndex + 1).toInt(&validVersionNumber);
193 if (validVersionNumber) {
194 const Component entry(sections[0], sections[2], majorVersion, minorVersion);
196 _components.append(entry);
201 reportError(lineNumber, -1,
202 QString::fromUtf8("a component declaration requires two or three arguments, but %1 were provided").arg(sectionCount));
209 void QDeclarativeDirParser::reportError(int line, int column, const QString &description)
211 QDeclarativeError error;
214 error.setColumn(column);
215 error.setDescription(description);
216 _errors.append(error);
219 bool QDeclarativeDirParser::hasError() const
221 if (! _errors.isEmpty())
227 QList<QDeclarativeError> QDeclarativeDirParser::errors() const
232 QList<QDeclarativeDirParser::Plugin> QDeclarativeDirParser::plugins() const
237 QList<QDeclarativeDirParser::Component> QDeclarativeDirParser::components() const
243 QList<QDeclarativeDirParser::TypeInfo> QDeclarativeDirParser::typeInfos() const