1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
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.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "metainfoparser.h"
37 #include "model/propertyparser.h"
38 #include <QXmlStreamReader>
44 namespace QmlDesigner {
48 MetaInfoParser::MetaInfoParser(const MetaInfo &metaInfo)
49 : m_metaInfo(metaInfo)
53 void MetaInfoParser::parseFile(const QString &path)
56 file.setFileName(path);
57 if (!file.open(QIODevice::ReadOnly))
58 throw new InvalidMetaInfoException(__LINE__, __FUNCTION__, __FILE__);
60 QXmlStreamReader reader;
61 reader.setDevice(&file);
63 while (!reader.atEnd()) {
67 errorHandling(reader, file);
70 void MetaInfoParser::tokenHandler(QXmlStreamReader &reader)
72 if (reader.isStartElement() && reader.name() == "metainfo")
73 handleMetaInfoElement(reader);
76 void MetaInfoParser::handleMetaInfoElement(QXmlStreamReader &reader)
78 while (!reader.atEnd() && !(reader.isEndElement() && reader.name() == "metainfo")) {
80 metaInfoHandler(reader);
84 void MetaInfoParser::metaInfoHandler(QXmlStreamReader &reader)
86 if (reader.isStartElement())
88 if (reader.name() == "node")
89 handleNodeElement(reader);
93 void MetaInfoParser::handleNodeElement(QXmlStreamReader &reader)
95 const QXmlStreamAttributes attributes = reader.attributes();
97 const QString className = attributes.value("name").toString();
98 const QIcon icon = QIcon(attributes.value("icon").toString());
100 if (className.isEmpty()) {
101 reader.raiseError("Invalid element 'node' - mandatory attribute 'name' is missing");
105 while (!reader.atEnd() && !(reader.isEndElement() && reader.name() == "node")) {
108 handleNodeItemLibraryEntryElement(reader, className, icon);
112 void MetaInfoParser::handleNodeItemLibraryEntryElement(QXmlStreamReader &reader, const QString &className, const QIcon &icon)
114 if (reader.isStartElement() && reader.name() == "itemlibraryentry")
116 const QString versionNumber = reader.attributes().value("version").toString();
121 if (!versionNumber.isEmpty()) {
124 if (versionNumber.contains('.')) {
125 val = versionNumber.split('.').first().toInt(&ok);
126 major = ok ? val : major;
127 val = versionNumber.split('.').last().toInt(&ok);
128 minor = ok ? val : minor;
130 val = versionNumber.toInt(&ok);
131 major = ok ? val : major;
135 const QString name = reader.attributes().value("name").toString();
137 ItemLibraryEntry entry;
138 entry.setType(className, major, minor);
142 QString iconPath = reader.attributes().value("libraryIcon").toString();
143 if (!iconPath.isEmpty())
144 entry.setIconPath(iconPath);
146 QString category = reader.attributes().value("category").toString();
147 if (!category.isEmpty())
148 entry.setCategory(category);
150 QString requiredImport = reader.attributes().value("requiredImport").toString();
151 if (!requiredImport.isEmpty())
152 entry.setRequiredImport(requiredImport);
154 while (!reader.atEnd() && !(reader.isEndElement() && reader.name() == "itemlibraryentry")) {
156 handleItemLibraryEntryPropertyElement(reader, entry);
159 m_metaInfo.itemLibraryInfo()->addEntry(entry);
163 void MetaInfoParser::handleItemLibraryEntryPropertyElement(QXmlStreamReader &reader, ItemLibraryEntry &itemLibraryEntry)
165 if (reader.isStartElement() && reader.name() == "property")
167 QXmlStreamAttributes attributes(reader.attributes());
168 QString name = attributes.value("name").toString();
169 QString type = attributes.value("type").toString();
170 QString value = attributes.value("value").toString();
171 itemLibraryEntry.addProperty(name, type, value);
177 void MetaInfoParser::errorHandling(QXmlStreamReader &reader, QFile &file)
179 if (!reader.hasError())
182 qDebug() << QString("Error at %1, %2:%3: %4")
183 .arg(file.fileName())
184 .arg(reader.lineNumber())
185 .arg(reader.columnNumber())
186 .arg(reader.errorString());
190 QString fileString = file.readAll();
191 QString snippetString;
193 int position = reader.characterOffset();
194 while (position >= 0)
196 if (fileString[position] == '\n') {
202 snippetString.prepend(fileString[position]);
207 position = reader.characterOffset();
208 while (position >= 0)
211 if (fileString[position] == '\n') {
217 snippetString.append(fileString[position]);
220 qDebug() << snippetString;