1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the Qt Assistant of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "docuparser.h"
|
---|
43 | #include "profile.h"
|
---|
44 |
|
---|
45 | #include <QDir>
|
---|
46 | #include <QFile>
|
---|
47 | #include <QFileInfo>
|
---|
48 | #include <QRegExp>
|
---|
49 | #include <QString>
|
---|
50 | #include <QDataStream>
|
---|
51 |
|
---|
52 | QT_BEGIN_NAMESPACE
|
---|
53 |
|
---|
54 | QDataStream &operator>>(QDataStream &s, ContentItem &ci)
|
---|
55 | {
|
---|
56 | s >> ci.title;
|
---|
57 | s >> ci.reference;
|
---|
58 | s >> ci.depth;
|
---|
59 | return s;
|
---|
60 | }
|
---|
61 |
|
---|
62 | QDataStream &operator<<(QDataStream &s, const ContentItem &ci)
|
---|
63 | {
|
---|
64 | s << ci.title;
|
---|
65 | s << ci.reference;
|
---|
66 | s << ci.depth;
|
---|
67 | return s;
|
---|
68 | }
|
---|
69 |
|
---|
70 | const QString DocuParser::DocumentKey = QLatin1String("/Qt Assistant/") + QLatin1String(QT_VERSION_STR) + QLatin1String("/");
|
---|
71 |
|
---|
72 | DocuParser *DocuParser::createParser(const QString &fileName)
|
---|
73 | {
|
---|
74 | QFile file(fileName);
|
---|
75 | if(!file.open(QFile::ReadOnly)) {
|
---|
76 | return 0;
|
---|
77 | }
|
---|
78 |
|
---|
79 | QString str;
|
---|
80 | int maxlen = 1024;
|
---|
81 | int majVer = 0, minVer = 0, serVer = 0;
|
---|
82 | static QRegExp re(QLatin1String("assistantconfig +version=\"(\\d)\\.(\\d)\\.(\\d)\""), Qt::CaseInsensitive);
|
---|
83 | Q_ASSERT(re.isValid());
|
---|
84 | while(!(str = QLatin1String(file.readLine(maxlen))).isEmpty()) {
|
---|
85 | if(re.indexIn(str) >= 0) {
|
---|
86 | majVer = re.cap(1).toInt();
|
---|
87 | minVer = re.cap(2).toInt();
|
---|
88 | serVer = re.cap(3).toInt();
|
---|
89 | break;
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (majVer < 3 || (majVer == 3 && minVer < 2)) {
|
---|
94 | return new DocuParser310;
|
---|
95 | }
|
---|
96 |
|
---|
97 | return new DocuParser320;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | bool DocuParser::parse(QFile *file)
|
---|
102 | {
|
---|
103 | QXmlInputSource source(file);
|
---|
104 | QXmlSimpleReader reader;
|
---|
105 | reader.setContentHandler(this);
|
---|
106 | reader.setErrorHandler(this);
|
---|
107 | setFileName(QFileInfo(*file).absoluteFilePath());
|
---|
108 | return reader.parse(source);
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | QString DocuParser::errorProtocol() const
|
---|
113 | {
|
---|
114 | return errorProt;
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | QList<ContentItem> DocuParser::getContentItems()
|
---|
119 | {
|
---|
120 | return contentList;
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | QList<IndexItem*> DocuParser::getIndexItems()
|
---|
125 | {
|
---|
126 | return indexList;
|
---|
127 | }
|
---|
128 |
|
---|
129 | QString DocuParser::absolutify(const QString &name, bool makeUrl) const
|
---|
130 | {
|
---|
131 | if (!name.isEmpty()) {
|
---|
132 | QString s = name;
|
---|
133 | s.replace(QLatin1String("\\"), QLatin1String("/"));
|
---|
134 | QFileInfo orgPath(name);
|
---|
135 | if(orgPath.isRelative())
|
---|
136 | s = QFileInfo(fname).path() + QLatin1Char('/') + name;
|
---|
137 | if (makeUrl)
|
---|
138 | s.prepend(QLatin1String("file:"));
|
---|
139 | return s;
|
---|
140 | }
|
---|
141 | return name;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | void DocuParser310::addTo(Profile *p)
|
---|
146 | {
|
---|
147 | p->addDCFTitle(fname, docTitle);
|
---|
148 | p->addDCFIcon(docTitle, iconName);
|
---|
149 | p->addDCFIndexPage(docTitle, conURL);
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | bool DocuParser310::startDocument()
|
---|
154 | {
|
---|
155 | state = StateInit;
|
---|
156 | errorProt = QLatin1String("");
|
---|
157 |
|
---|
158 | contentRef = QLatin1String("");
|
---|
159 | indexRef = QLatin1String("");
|
---|
160 | depth = 0;
|
---|
161 |
|
---|
162 | contentList.clear();
|
---|
163 | qDeleteAll(indexList);
|
---|
164 | indexList.clear();
|
---|
165 |
|
---|
166 | return true;
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | bool DocuParser310::startElement(const QString &, const QString &,
|
---|
171 | const QString &qname,
|
---|
172 | const QXmlAttributes &attr)
|
---|
173 | {
|
---|
174 | if (qname == QLatin1String("DCF") && state == StateInit) {
|
---|
175 | state = StateContent;
|
---|
176 | contentRef = absolutify(attr.value(QLatin1String("ref")), false);
|
---|
177 | conURL = contentRef;
|
---|
178 | docTitle = attr.value(QLatin1String("title"));
|
---|
179 | iconName = absolutify(attr.value(QLatin1String("icon")), false);
|
---|
180 | contentList.append(ContentItem(docTitle, absolutify(contentRef), depth));
|
---|
181 | } else if (qname == QLatin1String("section") && (state == StateContent || state == StateSect)) {
|
---|
182 | state = StateSect;
|
---|
183 | contentRef = absolutify(attr.value(QLatin1String("ref")));
|
---|
184 | title = attr.value(QLatin1String("title"));
|
---|
185 | depth++;
|
---|
186 | contentList.append(ContentItem(title, contentRef, depth));
|
---|
187 | } else if (qname == QLatin1String("keyword") && state == StateSect) {
|
---|
188 | state = StateKeyword;
|
---|
189 | indexRef = absolutify(attr.value(QLatin1String("ref")));
|
---|
190 | } else
|
---|
191 | return false;
|
---|
192 | return true;
|
---|
193 | }
|
---|
194 |
|
---|
195 | bool DocuParser310::endElement(const QString &nameSpace, const QString &localName,
|
---|
196 | const QString &qName)
|
---|
197 | {
|
---|
198 | Q_UNUSED(nameSpace);
|
---|
199 | Q_UNUSED(localName);
|
---|
200 | Q_UNUSED(qName);
|
---|
201 |
|
---|
202 | switch(state) {
|
---|
203 | case StateInit:
|
---|
204 | break;
|
---|
205 | case StateContent:
|
---|
206 | state = StateInit;
|
---|
207 | break;
|
---|
208 | case StateSect:
|
---|
209 | state = --depth ? StateSect : StateContent;
|
---|
210 | break;
|
---|
211 | case StateKeyword:
|
---|
212 | state = StateSect;
|
---|
213 | break;
|
---|
214 | default:
|
---|
215 | break;
|
---|
216 | }
|
---|
217 | return true;
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | bool DocuParser310::characters(const QString& ch)
|
---|
222 | {
|
---|
223 | QString str = ch.simplified();
|
---|
224 | if (str.isEmpty())
|
---|
225 | return true;
|
---|
226 |
|
---|
227 | switch (state) {
|
---|
228 | case StateInit:
|
---|
229 | case StateContent:
|
---|
230 | case StateSect:
|
---|
|
---|