Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativedirparser.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qdeclarativedirparser_p.h"
43 #include "qdeclarativeerror.h"
44 #include "qdeclarativeglobal_p.h"
45
46 #include <QtCore/QTextStream>
47 #include <QtCore/QFile>
48 #include <QtCore/QtDebug>
49
50 QT_BEGIN_NAMESPACE
51
52 QDeclarativeDirParser::QDeclarativeDirParser()
53     : _isParsed(false)
54 {
55 }
56
57 QDeclarativeDirParser::~QDeclarativeDirParser()
58 {
59 }
60
61 QUrl QDeclarativeDirParser::url() const
62 {
63     return _url;
64 }
65
66 void QDeclarativeDirParser::setUrl(const QUrl &url)
67 {
68     _url = url;
69 }
70
71 QString QDeclarativeDirParser::fileSource() const
72 {
73     return _filePathSouce;
74 }
75
76 void QDeclarativeDirParser::setFileSource(const QString &filePath)
77 {
78     _filePathSouce = filePath;
79 }
80
81 QString QDeclarativeDirParser::source() const
82 {
83     return _source;
84 }
85
86 void QDeclarativeDirParser::setSource(const QString &source)
87 {
88     _isParsed = false;
89     _source = source;
90 }
91
92 bool QDeclarativeDirParser::isParsed() const
93 {
94     return _isParsed;
95 }
96
97 bool QDeclarativeDirParser::parse()
98 {
99     if (_isParsed)
100         return true;
101
102     _isParsed = true;
103     _errors.clear();
104     _plugins.clear();
105     _components.clear();
106     _scripts.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 (ch.isSpace()) {
145                 do { ++index; }
146                 while (index != length && line.at(index).isSpace());
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 && !line.at(index).isSpace());
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], 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], 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 QString &fileName = sections[2];
225
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);
230                         } else {
231                             const Component entry(sections[0], fileName, majorVersion, minorVersion);
232                             _components.append(entry);
233                         }
234                     }
235                 }
236             }
237         } else {
238             reportError(lineNumber, -1, 
239                         QString::fromUtf8("a component declaration requires two or three arguments, but %1 were provided").arg(sectionCount));
240         }
241     }
242
243     return hasError();
244 }
245
246 void QDeclarativeDirParser::reportError(int line, int column, const QString &description)
247 {
248     QDeclarativeError error;
249     error.setUrl(_url);
250     error.setLine(line);
251     error.setColumn(column);
252     error.setDescription(description);
253     _errors.append(error);
254 }
255
256 bool QDeclarativeDirParser::hasError() const
257 {
258     if (! _errors.isEmpty())
259         return true;
260
261     return false;
262 }
263
264 QList<QDeclarativeError> QDeclarativeDirParser::errors(const QString &uri) const
265 {
266     QList<QDeclarativeError> errors = _errors;
267     for (int i = 0; i < errors.size(); ++i) {
268         QDeclarativeError &e = errors[i];
269         QString description = e.description();
270         description.replace(QLatin1String("$$URI$$"), uri);
271         e.setDescription(description);
272     }
273     return errors;
274 }
275
276 QList<QDeclarativeDirParser::Plugin> QDeclarativeDirParser::plugins() const
277 {
278     return _plugins;
279 }
280
281 QList<QDeclarativeDirParser::Component> QDeclarativeDirParser::components() const
282 {
283     return _components;
284 }
285
286 QList<QDeclarativeDirParser::Script> QDeclarativeDirParser::scripts() const
287 {
288     return _scripts;
289 }
290
291 #ifdef QT_CREATOR
292 QList<QDeclarativeDirParser::TypeInfo> QDeclarativeDirParser::typeInfos() const
293 {
294     return _typeInfos;
295 }
296 #endif
297
298 QT_END_NAMESPACE