Initial import from the monolithic Qt.
[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 ** 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
14 ** this package.
15 **
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.
23 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "private/qdeclarativedirparser_p.h"
43 #include "qdeclarativeerror.h"
44
45 #include <QtCore/QTextStream>
46 #include <QtCore/QtDebug>
47
48 QT_BEGIN_NAMESPACE
49
50 QDeclarativeDirParser::QDeclarativeDirParser()
51     : _isParsed(false)
52 {
53 }
54
55 QDeclarativeDirParser::~QDeclarativeDirParser()
56 {
57 }
58
59 QUrl QDeclarativeDirParser::url() const
60 {
61     return _url;
62 }
63
64 void QDeclarativeDirParser::setUrl(const QUrl &url)
65 {
66     _url = url;
67 }
68
69 QString QDeclarativeDirParser::source() const
70 {
71     return _source;
72 }
73
74 void QDeclarativeDirParser::setSource(const QString &source)
75 {
76     _isParsed = false;
77     _source = source;
78 }
79
80 bool QDeclarativeDirParser::isParsed() const
81 {
82     return _isParsed;
83 }
84
85 bool QDeclarativeDirParser::parse()
86 {
87     if (_isParsed)
88         return true;
89
90     _isParsed = true;
91     _errors.clear();
92     _plugins.clear();
93     _components.clear();
94
95     QTextStream stream(&_source);
96     int lineNumber = 0;
97
98     forever {
99         ++lineNumber;
100
101         const QString line = stream.readLine();
102         if (line.isNull())
103             break;
104
105         QString sections[3];
106         int sectionCount = 0;
107
108         int index = 0;
109         const int length = line.length();
110
111         while (index != length) {
112             const QChar ch = line.at(index);
113
114             if (ch.isSpace()) {
115                 do { ++index; }
116                 while (index != length && line.at(index).isSpace());
117
118             } else if (ch == QLatin1Char('#')) {
119                 // recognized a comment
120                 break;
121
122             } else {
123                 const int start = index;
124
125                 do { ++index; }
126                 while (index != length && !line.at(index).isSpace());
127
128                 const QString lexeme = line.mid(start, index - start);
129
130                 if (sectionCount >= 3) {
131                     reportError(lineNumber, start, QLatin1String("unexpected token"));
132
133                 } else {
134                     sections[sectionCount++] = lexeme;
135                 }
136             }
137         }
138
139         if (sectionCount == 0) {
140             continue; // no sections, no party.
141
142         } else if (sections[0] == QLatin1String("plugin")) {
143             if (sectionCount < 2) {
144                 reportError(lineNumber, -1,
145                             QString::fromUtf8("plugin directive requires 2 arguments, but %1 were provided").arg(sectionCount + 1));
146
147                 continue;
148             }
149
150             const Plugin entry(sections[1], sections[2]);
151
152             _plugins.append(entry);
153
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));
158                 continue;
159             }
160             Component entry(sections[1], sections[2], -1, -1);
161             entry.internal = true;
162             _components.append(entry);
163
164         } else if (sectionCount == 2) {
165             // No version specified (should only be used for relative qmldir files)
166             const Component entry(sections[0], sections[1], -1, -1);
167             _components.append(entry);
168         } else if (sectionCount == 3) {
169             const QString &version = sections[1];
170             const int dotIndex = version.indexOf(QLatin1Char('.'));
171
172             if (dotIndex == -1) {
173                 reportError(lineNumber, -1, QLatin1String("expected '.'"));
174             } else if (version.indexOf(QLatin1Char('.'), dotIndex + 1) != -1) {
175                 reportError(lineNumber, -1, QLatin1String("unexpected '.'"));
176             } else {
177                 bool validVersionNumber = false;
178                 const int majorVersion = version.left(dotIndex).toInt(&validVersionNumber);
179
180                 if (validVersionNumber) {
181                     const int minorVersion = version.mid(dotIndex + 1).toInt(&validVersionNumber);
182
183                     if (validVersionNumber) {
184                         const Component entry(sections[0], sections[2], majorVersion, minorVersion);
185
186                         _components.append(entry);
187                     }
188                 }
189             }
190         } else {
191             reportError(lineNumber, -1, 
192                         QString::fromUtf8("a component declaration requires 3 arguments, but %1 were provided").arg(sectionCount + 1));
193         }
194     }
195
196     return hasError();
197 }
198
199 void QDeclarativeDirParser::reportError(int line, int column, const QString &description)
200 {
201     QDeclarativeError error;
202     error.setUrl(_url);
203     error.setLine(line);
204     error.setColumn(column);
205     error.setDescription(description);
206     _errors.append(error);
207 }
208
209 bool QDeclarativeDirParser::hasError() const
210 {
211     if (! _errors.isEmpty())
212         return true;
213
214     return false;
215 }
216
217 QList<QDeclarativeError> QDeclarativeDirParser::errors() const
218 {
219     return _errors;
220 }
221
222 QList<QDeclarativeDirParser::Plugin> QDeclarativeDirParser::plugins() const
223 {
224     return _plugins;
225 }
226
227 QList<QDeclarativeDirParser::Component> QDeclarativeDirParser::components() const
228 {
229     return _components;
230 }
231
232 QT_END_NAMESPACE