Qt 4.8
qdeclarativedirparser.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
46 #include <QtCore/QTextStream>
47 #include <QtCore/QFile>
48 #include <QtCore/QtDebug>
49 
51 
53  : _isParsed(false)
54 {
55 }
56 
58 {
59 }
60 
62 {
63  return _url;
64 }
65 
67 {
68  _url = url;
69 }
70 
72 {
73  return _filePathSouce;
74 }
75 
77 {
78  _filePathSouce = filePath;
79 }
80 
82 {
83  return _source;
84 }
85 
87 {
88  _isParsed = false;
89  _source = source;
90 }
91 
93 {
94  return _isParsed;
95 }
96 
98 {
99  if (_isParsed)
100  return true;
101 
102  _isParsed = true;
103  _errors.clear();
104  _plugins.clear();
105  _components.clear();
106 
107  if (_source.isEmpty() && !_filePathSouce.isEmpty()) {
108  QFile file(_filePathSouce);
111  error.setDescription(QString::fromUtf8("cannot load module \"$$URI$$\": File name case mismatch for \"%1\"").arg(_filePathSouce));
112  _errors.prepend(error);
113  return false;
114  } else if (file.open(QFile::ReadOnly)) {
116  } else {
118  error.setDescription(QString::fromUtf8("module \"$$URI$$\" definition \"%1\" not readable").arg(_filePathSouce));
119  _errors.prepend(error);
120  return false;
121  }
122  }
123 
125  int lineNumber = 0;
126 
127  forever {
128  ++lineNumber;
129 
130  const QString line = stream.readLine();
131  if (line.isNull())
132  break;
133 
134  QString sections[3];
135  int sectionCount = 0;
136 
137  int index = 0;
138  const int length = line.length();
139 
140  while (index != length) {
141  const QChar ch = line.at(index);
142 
143  if (ch.isSpace()) {
144  do { ++index; }
145  while (index != length && line.at(index).isSpace());
146 
147  } else if (ch == QLatin1Char('#')) {
148  // recognized a comment
149  break;
150 
151  } else {
152  const int start = index;
153 
154  do { ++index; }
155  while (index != length && !line.at(index).isSpace());
156 
157  const QString lexeme = line.mid(start, index - start);
158 
159  if (sectionCount >= 3) {
160  reportError(lineNumber, start, QLatin1String("unexpected token"));
161 
162  } else {
163  sections[sectionCount++] = lexeme;
164  }
165  }
166  }
167 
168  if (sectionCount == 0) {
169  continue; // no sections, no party.
170 
171  } else if (sections[0] == QLatin1String("plugin")) {
172  if (sectionCount < 2) {
173  reportError(lineNumber, -1,
174  QString::fromUtf8("plugin directive requires 2 arguments, but %1 were provided").arg(sectionCount + 1));
175 
176  continue;
177  }
178 
179  const Plugin entry(sections[1], sections[2]);
180 
181  _plugins.append(entry);
182 
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));
187  continue;
188  }
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));
196  continue;
197  }
198 #ifdef QT_CREATOR
199  TypeInfo typeInfo(sections[1]);
200  _typeInfos.append(typeInfo);
201 #endif
202 
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('.'));
210 
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 '.'"));
215  } else {
216  bool validVersionNumber = false;
217  const int majorVersion = version.left(dotIndex).toInt(&validVersionNumber);
218 
219  if (validVersionNumber) {
220  const int minorVersion = version.mid(dotIndex + 1).toInt(&validVersionNumber);
221 
222  if (validVersionNumber) {
223  const Component entry(sections[0], sections[2], majorVersion, minorVersion);
224 
225  _components.append(entry);
226  }
227  }
228  }
229  } else {
230  reportError(lineNumber, -1,
231  QString::fromUtf8("a component declaration requires 3 arguments, but %1 were provided").arg(sectionCount + 1));
232  }
233  }
234 
235  return hasError();
236 }
237 
238 void QDeclarativeDirParser::reportError(int line, int column, const QString &description)
239 {
241  error.setUrl(_url);
242  error.setLine(line);
243  error.setColumn(column);
244  error.setDescription(description);
245  _errors.append(error);
246 }
247 
249 {
250  if (! _errors.isEmpty())
251  return true;
252 
253  return false;
254 }
255 
257 {
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);
264  }
265  return errors;
266 }
267 
269 {
270  return _plugins;
271 }
272 
274 {
275  return _components;
276 }
277 
278 #ifdef QT_CREATOR
279 QList<QDeclarativeDirParser::TypeInfo> QDeclarativeDirParser::typeInfos() const
280 {
281  return _typeInfos;
282 }
283 #endif
284 
QList< Component > _components
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QString readLine(qint64 maxlen=0)
Reads one line of text from the stream, and returns it as a QString.
void setDescription(const QString &)
Sets the error description.
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
int toInt(bool *ok=0, int base=10) const
Returns the string converted to an int using base base, which is 10 by default and must be between 2 ...
Definition: qstring.cpp:6090
bool open(OpenMode flags)
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition: qfile.cpp:1064
QList< QDeclarativeError > errors(const QString &uri) const
#define error(msg)
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
void setColumn(int)
Sets the error column number.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
bool isSpace() const
Returns true if the character is a separator character (Separator_* categories); otherwise returns fa...
Definition: qchar.cpp:609
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
bool QDeclarative_isFileCaseCorrect(const QString &fileName)
Returns true if the case of fileName is equivalent to the file case of fileName on disk...
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QList< Component > components() const
void reportError(int line, int column, const QString &message)
static FILE * stream
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
void prepend(const T &t)
Inserts value at the beginning of the list.
Definition: qlist.h:541
QString description() const
Returns the error description.
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
void setFileSource(const QString &filePath)
The QDeclarativeError class encapsulates a QML error.
void clear()
Removes all items from the list.
Definition: qlist.h:764
void setLine(int)
Sets the error line number.
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition: qstring.h:505
QList< QDeclarativeError > _errors
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
void setUrl(const QUrl &)
Sets the url for the file that caused this error.
The QTextStream class provides a convenient interface for reading and writing text.
Definition: qtextstream.h:73
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
QByteArray readAll()
Reads all available data from the device, and returns it as a QByteArray.
Definition: qiodevice.cpp:1025
quint16 index
void setSource(const QString &source)
void setUrl(const QUrl &url)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QList< Plugin > plugins() const
#define forever
This macro is provided for convenience for writing infinite loops.
Definition: qglobal.h:2452