OSDN Git Service

7205ab91b8538d49b326fa8246da8e915ca3eff6
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / designercore / metainfo / metainfoparser.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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 **
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, 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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "metainfoparser.h"
35 #include "metainfo.h"
36
37 #include "model/propertyparser.h"
38 #include <QXmlStreamReader>
39 #include <QString>
40 #include <QFile>
41 #include <QtDebug>
42 #include <QIcon>
43
44 namespace QmlDesigner {
45 namespace Internal {
46
47
48 MetaInfoParser::MetaInfoParser(const MetaInfo &metaInfo)
49         : m_metaInfo(metaInfo)
50 {
51 }
52
53 void MetaInfoParser::parseFile(const QString &path)
54 {
55     QFile file;
56     file.setFileName(path);
57     if (!file.open(QIODevice::ReadOnly))
58         throw new InvalidMetaInfoException(__LINE__, __FUNCTION__, __FILE__);
59
60     QXmlStreamReader reader;
61     reader.setDevice(&file);
62
63     while (!reader.atEnd()) {
64         reader.readNext();
65         tokenHandler(reader);
66     }
67     errorHandling(reader, file);
68 }
69
70 void MetaInfoParser::tokenHandler(QXmlStreamReader &reader)
71 {
72     if (reader.isStartElement() && reader.name() == "metainfo")
73         handleMetaInfoElement(reader);
74 }
75
76 void MetaInfoParser::handleMetaInfoElement(QXmlStreamReader &reader)
77 {
78     while (!reader.atEnd() && !(reader.isEndElement() && reader.name() == "metainfo")) {
79         reader.readNext();
80         metaInfoHandler(reader);
81     }
82 }
83
84 void MetaInfoParser::metaInfoHandler(QXmlStreamReader &reader)
85 {
86     if (reader.isStartElement())
87     {
88         if (reader.name() == "node")
89             handleNodeElement(reader);
90     }
91 }
92
93 void MetaInfoParser::handleNodeElement(QXmlStreamReader &reader)
94 {
95     const QXmlStreamAttributes attributes = reader.attributes();
96
97     const QString className = attributes.value("name").toString();
98     const QIcon icon = QIcon(attributes.value("icon").toString());
99
100     if (className.isEmpty()) {
101         reader.raiseError("Invalid element 'node' - mandatory attribute 'name' is missing");
102         return;
103     }
104
105     while (!reader.atEnd() && !(reader.isEndElement() && reader.name() == "node")) {
106         reader.readNext();
107
108         handleNodeItemLibraryEntryElement(reader, className, icon);
109     }
110 }
111
112 void MetaInfoParser::handleNodeItemLibraryEntryElement(QXmlStreamReader &reader, const QString &className, const QIcon &icon)
113 {
114     if (reader.isStartElement() && reader.name() == "itemlibraryentry")
115     {
116         const QString versionNumber = reader.attributes().value("version").toString();
117
118         int major = 1;
119         int minor = 0;
120
121         if (!versionNumber.isEmpty()) {
122             int val;
123             bool ok;
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;
129             } else {
130                 val = versionNumber.toInt(&ok);
131                 major = ok ? val : major;
132             }
133         }
134
135         const QString name = reader.attributes().value("name").toString();
136
137         ItemLibraryEntry entry;
138         entry.setType(className, major, minor);
139         entry.setName(name);
140         entry.setIcon(icon);
141
142         QString iconPath = reader.attributes().value("libraryIcon").toString();
143         if (!iconPath.isEmpty()) 
144             entry.setIconPath(iconPath);
145
146         QString category = reader.attributes().value("category").toString();
147         if (!category.isEmpty())
148             entry.setCategory(category);
149
150         QString requiredImport = reader.attributes().value("requiredImport").toString();
151         if (!requiredImport.isEmpty())
152             entry.setRequiredImport(requiredImport);
153
154         while (!reader.atEnd() && !(reader.isEndElement() && reader.name() == "itemlibraryentry")) {
155             reader.readNext();
156             handleItemLibraryEntryPropertyElement(reader, entry);
157         }
158
159         m_metaInfo.itemLibraryInfo()->addEntry(entry);
160     }
161 }
162
163 void MetaInfoParser::handleItemLibraryEntryPropertyElement(QXmlStreamReader &reader, ItemLibraryEntry &itemLibraryEntry)
164 {
165     if (reader.isStartElement() && reader.name() == "property")
166     {
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);
172
173         reader.readNext();
174     }
175 }
176
177 void MetaInfoParser::errorHandling(QXmlStreamReader &reader, QFile &file)
178 {
179     if (!reader.hasError())
180         return;
181
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());
187
188     file.reset();
189
190     QString fileString = file.readAll();
191     QString snippetString;
192     int lineCount = 0;
193     int position = reader.characterOffset();
194     while (position >= 0)
195     {
196         if (fileString[position] == '\n') {
197             if (lineCount > 3)
198                 break;
199             lineCount++;
200         }
201
202         snippetString.prepend(fileString[position]);
203         position--;
204     }
205
206     lineCount = 0;
207     position = reader.characterOffset();
208     while (position >= 0)
209     {
210         position++;
211         if (fileString[position] == '\n') {
212             if (lineCount > 1)
213                 break;
214             lineCount++;
215         }
216
217         snippetString.append(fileString[position]);
218     }
219
220     qDebug() << snippetString;
221
222 }
223
224
225 }
226 }