Fix diagnostic messages.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativedirparser.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "private/qdeclarativedirparser_p.h"
43 #include "qdeclarativeerror.h"
44 #include <private/qdeclarativeglobal_p.h>
45 #include <private/qdeclarativeutils_p.h>
46
47 #include <QtCore/QTextStream>
48 #include <QtCore/QFile>
49 #include <QtCore/QtDebug>
50
51 QT_BEGIN_NAMESPACE
52
53 QDeclarativeDirParser::QDeclarativeDirParser()
54     : _isParsed(false)
55 {
56 }
57
58 QDeclarativeDirParser::~QDeclarativeDirParser()
59 {
60 }
61
62 QUrl QDeclarativeDirParser::url() const
63 {
64     return _url;
65 }
66
67 void QDeclarativeDirParser::setUrl(const QUrl &url)
68 {
69     _url = url;
70 }
71
72 QString QDeclarativeDirParser::fileSource() const
73 {
74     return _filePathSouce;
75 }
76
77 void QDeclarativeDirParser::setFileSource(const QString &filePath)
78 {
79     _filePathSouce = filePath;
80 }
81
82 QString QDeclarativeDirParser::source() const
83 {
84     return _source;
85 }
86
87 void QDeclarativeDirParser::setSource(const QString &source)
88 {
89     _isParsed = false;
90     _source = source;
91 }
92
93 bool QDeclarativeDirParser::isParsed() const
94 {
95     return _isParsed;
96 }
97
98 bool QDeclarativeDirParser::parse()
99 {
100     if (_isParsed)
101         return true;
102
103     _isParsed = true;
104     _errors.clear();
105     _plugins.clear();
106     _components.clear();
107
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);
114             return false;
115         } else if (file.open(QFile::ReadOnly)) {
116             _source = QString::fromUtf8(file.readAll());
117         } else {
118             QDeclarativeError error;
119             error.setDescription(QString::fromUtf8("module \"$$URI$$\" definition \"%1\" not readable").arg(_filePathSouce));
120             _errors.prepend(error);
121             return false;
122         }
123     }
124
125     QTextStream stream(&_source);
126     int lineNumber = 0;
127
128     forever {
129         ++lineNumber;
130
131         const QString line = stream.readLine();
132         if (line.isNull())
133             break;
134
135         QString sections[3];
136         int sectionCount = 0;
137
138         int index = 0;
139         const int length = line.length();
140
141         while (index != length) {
142             const QChar ch = line.at(index);
143
144             if (QDeclarativeUtils::isSpace(ch)) {
145                 do { ++index; }
146                 while (index != length && QDeclarativeUtils::isSpace(line.at(index)));
147
148             } else if (ch == QLatin1Char('#')) {
149                 // recognized a comment
150                 break;
151
152             } else {
153                 const int start = index;
154
155                 do { ++index; }
156                 while (index != length && !QDeclarativeUtils::isSpace(line.at(index)));
157
158                 const QString lexeme = line.mid(start, index - start);
159
160                 if (sectionCount >= 3) {
161                     reportError(lineNumber, start, QLatin1String("unexpected token"));
162
163                 } else {
164                     sections[sectionCount++] = lexeme;
165                 }
166             }
167         }
168
169         if (sectionCount == 0) {
170             continue; // no sections, no party.
171
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));
176
177                 continue;
178             }
179
180             const Plugin entry(sections[1], sections[2]);
181
182             _plugins.append(entry);
183
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));
188                 continue;
189             }
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));
197                 continue;
198             }
199 #ifdef QT_CREATOR
200             TypeInfo typeInfo(sections[1]);
201             _typeInfos.append(typeInfo);
202 #endif
203
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('.'));
211
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 '.'"));
216             } else {
217                 bool validVersionNumber = false;
218                 const int majorVersion = version.left(dotIndex).toInt(&validVersionNumber);
219
220                 if (validVersionNumber) {
221                     const int minorVersion = version.mid(dotIndex + 1).toInt(&validVersionNumber);
222
223                     if (validVersionNumber) {
224                         const Component entry(sections[0].toUtf8(), sections[2], majorVersion, minorVersion);
225
226                         _components.append(entry);
227                     }
228                 }
229             }
230         } else {
231             reportError(lineNumber, -1, 
232                         QString::fromUtf8("a component declaration requires two or three arguments, but %1 were provided").arg(sectionCount));
233         }
234     }
235
236     return hasError();
237 }
238
239 void QDeclarativeDirParser::reportError(int line, int column, const QString &description)
240 {
241     QDeclarativeError error;
242     error.setUrl(_url);
243     error.setLine(line);
244     error.setColumn(column);
245     error.setDescription(description);
246     _errors.append(error);
247 }
248
249 bool QDeclarativeDirParser::hasError() const
250 {
251     if (! _errors.isEmpty())
252         return true;
253
254     return false;
255 }
256
257 QList<QDeclarativeError> QDeclarativeDirParser::errors(const QString &uri) const
258 {
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);
265     }
266     return errors;
267 }
268
269 QList<QDeclarativeDirParser::Plugin> QDeclarativeDirParser::plugins() const
270 {
271     return _plugins;
272 }
273
274 QList<QDeclarativeDirParser::Component> QDeclarativeDirParser::components() const
275 {
276     return _components;
277 }
278
279 #ifdef QT_CREATOR
280 QList<QDeclarativeDirParser::TypeInfo> QDeclarativeDirParser::typeInfos() const
281 {
282     return _typeInfos;
283 }
284 #endif
285
286 QT_END_NAMESPACE