qmimetypeparser.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/corelib/mimetypes/qmimetypeparser.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2016 The Qt Company Ltd.-
4** Contact: https://www.qt.io/licensing/-
5**-
6** This file is part of the QtCore 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 The Qt Company. For licensing terms-
14** and conditions see https://www.qt.io/terms-conditions. For further-
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software-
20** Foundation and appearing in the file LICENSE.LGPL3 included in the-
21** packaging of this file. Please review the following information to-
22** ensure the GNU Lesser General Public License version 3 requirements-
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.-
24**-
25** GNU General Public License Usage-
26** Alternatively, this file may be used under the terms of the GNU-
27** General Public License version 2.0 or (at your option) the GNU General-
28** Public license version 3 or any later version approved by the KDE Free-
29** Qt Foundation. The licenses are as published by the Free Software-
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3-
31** included in the packaging of this file. Please review the following-
32** information to ensure the GNU General Public License requirements will-
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and-
34** https://www.gnu.org/licenses/gpl-3.0.html.-
35**-
36** $QT_END_LICENSE$-
37**-
38****************************************************************************/-
39-
40#define QT_NO_CAST_FROM_ASCII-
41-
42#include "qmimetypeparser_p.h"-
43-
44#ifndef QT_NO_MIMETYPE-
45-
46#include "qmimetype_p.h"-
47#include "qmimemagicrulematcher_p.h"-
48-
49#include <QtCore/QCoreApplication>-
50#include <QtCore/QDebug>-
51#include <QtCore/QDir>-
52#include <QtCore/QPair>-
53#include <QtCore/QXmlStreamReader>-
54#include <QtCore/QXmlStreamWriter>-
55#include <QtCore/QStack>-
56-
57QT_BEGIN_NAMESPACE-
58-
59// XML tags in MIME files-
60static const char mimeInfoTagC[] = "mime-info";-
61static const char mimeTypeTagC[] = "mime-type";-
62static const char mimeTypeAttributeC[] = "type";-
63static const char subClassTagC[] = "sub-class-of";-
64static const char commentTagC[] = "comment";-
65static const char genericIconTagC[] = "generic-icon";-
66static const char iconTagC[] = "icon";-
67static const char nameAttributeC[] = "name";-
68static const char globTagC[] = "glob";-
69static const char aliasTagC[] = "alias";-
70static const char patternAttributeC[] = "pattern";-
71static const char weightAttributeC[] = "weight";-
72static const char caseSensitiveAttributeC[] = "case-sensitive";-
73static const char localeAttributeC[] = "xml:lang";-
74-
75static const char magicTagC[] = "magic";-
76static const char priorityAttributeC[] = "priority";-
77-
78static const char matchTagC[] = "match";-
79static const char matchValueAttributeC[] = "value";-
80static const char matchTypeAttributeC[] = "type";-
81static const char matchOffsetAttributeC[] = "offset";-
82static const char matchMaskAttributeC[] = "mask";-
83-
84/*!-
85 \class QMimeTypeParser-
86 \inmodule QtCore-
87 \internal-
88 \brief The QMimeTypeParser class parses MIME types, and builds a MIME database hierarchy by adding to QMimeDatabasePrivate.-
89-
90 Populates QMimeDataBase-
91-
92 \sa QMimeDatabase, QMimeMagicRuleMatcher, MagicRule, MagicStringRule, MagicByteRule, GlobPattern-
93 \sa QMimeTypeParser-
94*/-
95-
96/*!-
97 \class QMimeTypeParserBase-
98 \inmodule QtCore-
99 \internal-
100 \brief The QMimeTypeParserBase class parses for a sequence of <mime-type> in a generic way.-
101-
102 Calls abstract handler function process for QMimeType it finds.-
103-
104 \sa QMimeDatabase, QMimeMagicRuleMatcher, MagicRule, MagicStringRule, MagicByteRule, GlobPattern-
105 \sa QMimeTypeParser-
106*/-
107-
108/*!-
109 \fn virtual bool QMimeTypeParserBase::process(const QMimeType &t, QString *errorMessage) = 0;-
110 Overwrite to process the sequence of parsed data-
111*/-
112-
113QMimeTypeParserBase::ParseState QMimeTypeParserBase::nextState(ParseState currentState, const QStringRef &startElement)-
114{-
115 switch (currentState) {-
116 case ParseBeginning:-
117 if (startElement == QLatin1String(mimeInfoTagC))-
118 return ParseMimeInfo;-
119 if (startElement == QLatin1String(mimeTypeTagC))-
120 return ParseMimeType;-
121 return ParseError;-
122 case ParseMimeInfo:-
123 return startElement == QLatin1String(mimeTypeTagC) ? ParseMimeType : ParseError;-
124 case ParseMimeType:-
125 case ParseComment:-
126 case ParseGenericIcon:-
127 case ParseIcon:-
128 case ParseGlobPattern:-
129 case ParseSubClass:-
130 case ParseAlias:-
131 case ParseOtherMimeTypeSubTag:-
132 case ParseMagicMatchRule:-
133 if (startElement == QLatin1String(mimeTypeTagC)) // Sequence of <mime-type>-
134 return ParseMimeType;-
135 if (startElement == QLatin1String(commentTagC ))-
136 return ParseComment;-
137 if (startElement == QLatin1String(genericIconTagC))-
138 return ParseGenericIcon;-
139 if (startElement == QLatin1String(iconTagC))-
140 return ParseIcon;-
141 if (startElement == QLatin1String(globTagC))-
142 return ParseGlobPattern;-
143 if (startElement == QLatin1String(subClassTagC))-
144 return ParseSubClass;-
145 if (startElement == QLatin1String(aliasTagC))-
146 return ParseAlias;-
147 if (startElement == QLatin1String(magicTagC))-
148 return ParseMagic;-
149 if (startElement == QLatin1String(matchTagC))-
150 return ParseMagicMatchRule;-
151 return ParseOtherMimeTypeSubTag;-
152 case ParseMagic:-
153 if (startElement == QLatin1String(matchTagC))-
154 return ParseMagicMatchRule;-
155 break;-
156 case ParseError:-
157 break;-
158 }-
159 return ParseError;-
160}-
161-
162// Parse int number from an (attribute) string-
163bool QMimeTypeParserBase::parseNumber(const QStringQStringRef &n, int *target, QString *errorMessage)-
164{-
165 bool ok;-
166 *target = n.toInt(&ok);-
167 if (Q_UNLIKELY(!ok))) {
__builtin_expe...!(!ok), false)Description
TRUEnever evaluated
FALSEevaluated 9733 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
0-9733
168 *errorMessage = QString::fromLatin1QLatin1String("Not a number '%1''") + n + QLatin1String("'.").arg(n);-
169 return false;
never executed: return false;
0
170 }-
171 return true;
executed 9733 times by 1 test: return true;
Executed by:
  • tst_QMimeDatabase
9733
172}-
173-
174#ifndef QT_NO_XMLSTREAMREADER-
staticstruct CreateMagicMatchRuleResult {
176 QString errorMessage; // must be first-
177 QMimeMagicRule *rule;-
178-
179 CreateMagicMatchRuleResult(const QStringRef &type, const QStringRef &value, const QStringRef &offsets, const QStringRef &mask)-
180 : errorMessage(), rule(type.toString(), value.toUtf8(), offsets.toString(), mask.toLatin1(), &errorMessage)-
181 {-
182-
183 }
executed 4058 times by 1 test: end of block
Executed by:
  • tst_QMimeDatabase
4058
184};-
185-
186static CreateMagicMatchRuleResult createMagicMatchRule(const QXmlStreamAttributes &atts, QString *errorMessage)-
187{-
188 const QStringQStringRef type = atts.value(QLatin1String(matchTypeAttributeC)).toString();));-
189 const QStringQStringRef value = atts.value(QLatin1String(matchValueAttributeC)).toString();));-
190 const QStringQStringRef offsets = atts.value(QLatin1String(matchOffsetAttributeC)).toString();));-
191 const QStringQStringRef mask = atts.value(QLatin1String(matchMaskAttributeC)).toString();));-
192 return new QMimeMagicRuleCreateMagicMatchRuleResult(type, value.toUtf8(),, offsets, mask.toLatin1(), errorMessage);
executed 4058 times by 1 test: return CreateMagicMatchRuleResult(type, value, offsets, mask);
Executed by:
  • tst_QMimeDatabase
4058
193}-
194#endif-
195-
196bool QMimeTypeParserBase::parse(QIODevice *dev, const QString &fileName, QString *errorMessage)-
197{-
198#ifdef QT_NO_XMLSTREAMREADER-
199 if (errorMessage)-
200 *errorMessage = QString::fromLatin1("QXmlStreamReader is not available, cannot parse.");-
201 return false;-
202#else-
203 QMimeTypePrivate data;-
204 int priority = 50;-
205 QStack<QMimeMagicRule *> currentRules; // stack for the nesting of rules-
206 QList<QMimeMagicRule> rules; // toplevel rules-
207 QXmlStreamReader reader(dev);-
208 ParseState ps = ParseBeginning;-
QXmlStreamAttributes atts;
209 while (!reader.atEnd()) {
!reader.atEnd()Description
TRUEevaluated 333413 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 17 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
17-333413
210 switch (reader.readNext()) {-
211 case QXmlStreamReader::StartElement: {
executed 153309 times by 1 test: case QXmlStreamReader::StartElement:
Executed by:
  • tst_QMimeDatabase
153309
212 ps = nextState(ps, reader.name());-
213 const QXmlStreamAttributes atts = reader.attributes();-
214 switch (ps) {-
215 case ParseMimeType: { // start parsing a MIME type name
executed 3319 times by 1 test: case ParseMimeType:
Executed by:
  • tst_QMimeDatabase
3319
216 const QString name = atts.value(QLatin1String(mimeTypeAttributeC)).toString();-
217 if (name.isEmpty()) {
name.isEmpty()Description
TRUEnever evaluated
FALSEevaluated 3319 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
0-3319
218 reader.raiseError(QString::fromLatin1QStringLiteral("Missing '%1''type'-attribute").arg(QString::fromLatin1(mimeTypeAttributeC)));));
never executed: return qstring_literal_temp;
0
219 } else {
never executed: end of block
0
220 data.name = name;-
221 }
executed 3319 times by 1 test: end of block
Executed by:
  • tst_QMimeDatabase
3319
222 }-
223 break;
executed 3319 times by 1 test: break;
Executed by:
  • tst_QMimeDatabase
3319
224 case ParseGenericIcon:
executed 1575 times by 1 test: case ParseGenericIcon:
Executed by:
  • tst_QMimeDatabase
1575
225 data.genericIconName = atts.value(QLatin1String(nameAttributeC)).toString();-
226 break;
executed 1575 times by 1 test: break;
Executed by:
  • tst_QMimeDatabase
1575
227 case ParseIcon:
never executed: case ParseIcon:
0
228 data.iconName = atts.value(QLatin1String(nameAttributeC)).toString();-
229 break;
never executed: break;
0
230 case ParseGlobPattern: {
executed 4136 times by 1 test: case ParseGlobPattern:
Executed by:
  • tst_QMimeDatabase
4136
231 const QString pattern = atts.value(QLatin1String(patternAttributeC)).toString();-
232 unsigned weight = atts.value(QLatin1String(weightAttributeC)).toString().toInt();-
233 const bool caseSensitive = atts.value(QLatin1String(caseSensitiveAttributeC)).toString())) == QLatin1String("true");-
234-
235 if (weight == 0)
weight == 0Description
TRUEevaluated 4126 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 10 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
10-4126
236 weight = QMimeGlobPattern::DefaultWeight;
executed 4126 times by 1 test: weight = QMimeGlobPattern::DefaultWeight;
Executed by:
  • tst_QMimeDatabase
4126
237-
238 Q_ASSERT(!data.name.isEmpty());-
239 const QMimeGlobPattern glob(pattern, data.name, weight, caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);-
240 if (!process(glob, errorMessage)) // for actual glob matching
!process(glob, errorMessage)Description
TRUEnever evaluated
FALSEevaluated 4136 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
0-4136
241 return false;
never executed: return false;
0
242 data.addGlobPattern(pattern); // just for QMimeType::globPatterns()-
243 }-
244 break;
executed 4136 times by 1 test: break;
Executed by:
  • tst_QMimeDatabase
4136
245 case ParseSubClass: {
executed 1560 times by 1 test: case ParseSubClass:
Executed by:
  • tst_QMimeDatabase
1560
246 const QString inheritsFrom = atts.value(QLatin1String(mimeTypeAttributeC)).toString();-
247 if (!inheritsFrom.isEmpty())
!inheritsFrom.isEmpty()Description
TRUEevaluated 1560 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEnever evaluated
0-1560
248 processParent(data.name, inheritsFrom);
executed 1560 times by 1 test: processParent(data.name, inheritsFrom);
Executed by:
  • tst_QMimeDatabase
1560
249 }-
250 break;
executed 1560 times by 1 test: break;
Executed by:
  • tst_QMimeDatabase
1560
251 case ParseComment: {
executed 134104 times by 1 test: case ParseComment:
Executed by:
  • tst_QMimeDatabase
134104
252 // comments have locale attributes. We want the default, English one-
253 QString locale = atts.value(QLatin1String(localeAttributeC)).toString();-
254 const QString comment = reader.readElementText();-
255 if (locale.isEmpty())
locale.isEmpty()Description
TRUEevaluated 3319 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 130785 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
3319-130785
256 locale = QString::fromLatin1("en_US");
executed 3319 times by 1 test: locale = QString::fromLatin1("en_US");
Executed by:
  • tst_QMimeDatabase
3319
257 data.localeComments.insert(locale, comment);-
258 }-
259 break;
executed 134104 times by 1 test: break;
Executed by:
  • tst_QMimeDatabase
134104
260 case ParseAlias: {
executed 975 times by 1 test: case ParseAlias:
Executed by:
  • tst_QMimeDatabase
975
261 const QString alias = atts.value(QLatin1String(mimeTypeAttributeC)).toString();-
262 if (!alias.isEmpty())
!alias.isEmpty()Description
TRUEevaluated 975 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEnever evaluated
0-975
263 processAlias(alias, data.name);
executed 975 times by 1 test: processAlias(alias, data.name);
Executed by:
  • tst_QMimeDatabase
975
264 }-
265 break;
executed 975 times by 1 test: break;
Executed by:
  • tst_QMimeDatabase
975
266 case ParseMagic: {
executed 1733 times by 1 test: case ParseMagic:
Executed by:
  • tst_QMimeDatabase
1733
267 priority = 50;-
268 const QStringQStringRef priorityS = atts.value(QLatin1String(priorityAttributeC)).toString();));-
269 if (!priorityS.isEmpty()) {
!priorityS.isEmpty()Description
TRUEevaluated 1617 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 116 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
116-1617
270 if (!parseNumber(priorityS, &priority, errorMessage))
!parseNumber(p... errorMessage)Description
TRUEnever evaluated
FALSEevaluated 1617 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
0-1617
271 return false;
never executed: return false;
0
272-
273 }
executed 1617 times by 1 test: end of block
Executed by:
  • tst_QMimeDatabase
1617
274 currentRules.clear();-
275 //qDebug() << "MAGIC start for mimetype" << data.name;-
276 }-
277 break;
executed 1733 times by 1 test: break;
Executed by:
  • tst_QMimeDatabase
1733
278 case ParseMagicMatchRule: {
executed 4058 times by 1 test: case ParseMagicMatchRule:
Executed by:
  • tst_QMimeDatabase
4058
279 QString magicErrorMessage;-
QMimeMagicRule *ruleauto result = createMagicMatchRule(atts, &magicErrorMessage);
280 if (Q_UNLIKELY(!result.rule->.isValid())()))
__builtin_expe...lid()), false)Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 4052 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
6-4052
281 qWarning("QMimeDatabase: Error parsing %s\n%s"%ls\n%ls",
executed 6 times by 1 test: QMessageLogger(__FILE__, 281, __PRETTY_FUNCTION__).warning("QMimeDatabase: Error parsing %ls\n%ls", static_cast<const wchar_t*>(static_cast<const void*>(QString(fileName).utf16())), static_cast<const wchar_t*>(static_cast<const void*>(QString(result.errorMessage).utf16())));
Executed by:
  • tst_QMimeDatabase
6
282 qPrintableqUtf16Printable(fileName), qPrintableqUtf16Printable(magicErrorMessageresult.errorMessage));
executed 6 times by 1 test: QMessageLogger(__FILE__, 281, __PRETTY_FUNCTION__).warning("QMimeDatabase: Error parsing %ls\n%ls", static_cast<const wchar_t*>(static_cast<const void*>(QString(fileName).utf16())), static_cast<const wchar_t*>(static_cast<const void*>(QString(result.errorMessage).utf16())));
Executed by:
  • tst_QMimeDatabase
6
283 QList<QMimeMagicRule> *ruleList;-
284 if (currentRules.isEmpty())
currentRules.isEmpty()Description
TRUEevaluated 3018 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 1040 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
1040-3018
285 ruleList = &rules;
executed 3018 times by 1 test: ruleList = &rules;
Executed by:
  • tst_QMimeDatabase
3018
286 else // nest this rule into the proper parent-
287 ruleList = &currentRules.top()->m_subMatches;
executed 1040 times by 1 test: ruleList = &currentRules.top()->m_subMatches;
Executed by:
  • tst_QMimeDatabase
1040
288 ruleList->append(*(std::move(result.rule);));-
289 //qDebug() << " MATCH added. Stack size was" << currentRules.size();-
290 currentRules.push(&ruleList->last());-
291 delete rule;break;
executed 4058 times by 1 test: break;
Executed by:
  • tst_QMimeDatabase
4058
292 }-
293 case ParseError:
never executed: case ParseError:
0
294 reader.raiseError(QString::fromLatin1QLatin1String("Unexpected element <%1>").-
arg() + reader.name().toString()));() + QLatin1Char('>'));
295 break;
never executed: break;
0
296 default:
executed 1849 times by 1 test: default:
Executed by:
  • tst_QMimeDatabase
1849
297 break;
executed 1849 times by 1 test: break;
Executed by:
  • tst_QMimeDatabase
1849
298 }-
299 }-
300 break;
executed 153309 times by 1 test: break;
Executed by:
  • tst_QMimeDatabase
153309
301 // continue switch QXmlStreamReader::Token...-
302 case QXmlStreamReader::EndElement: // Finished element
executed 19205 times by 1 test: case QXmlStreamReader::EndElement:
Executed by:
  • tst_QMimeDatabase
19205
303 {-
304 const QStringRef elementName = reader.name();-
305 if (elementName == QLatin1String(mimeTypeTagC)) {
elementName ==...(mimeTypeTagC)Description
TRUEevaluated 3319 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 15886 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
3319-15886
306 if (!process(QMimeType(data), errorMessage))
!process(QMime... errorMessage)Description
TRUEnever evaluated
FALSEevaluated 3319 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
0-3319
307 return false;
never executed: return false;
0
308 data.clear();-
309 } else if (elementName == QLatin1String(matchTagC)) {
executed 3319 times by 1 test: end of block
Executed by:
  • tst_QMimeDatabase
elementName ==...ing(matchTagC)Description
TRUEevaluated 4058 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 11828 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
3319-11828
310 // Closing a <match> tag, pop stack-
311 currentRules.pop();-
312 //qDebug() << " MATCH closed. Stack size is now" << currentRules.size();-
313 } else if (elementName == QLatin1String(magicTagC)) {
executed 4058 times by 1 test: end of block
Executed by:
  • tst_QMimeDatabase
elementName ==...ing(magicTagC)Description
TRUEevaluated 1733 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
FALSEevaluated 10095 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
1733-10095
314 //qDebug() << "MAGIC ended, we got" << rules.count() << "rules, with prio" << priority;-
315 // Finished a <magic> sequence-
316 QMimeMagicRuleMatcher ruleMatcher(data.name, priority);-
317 ruleMatcher.addRules(rules);-
318 processMagicMatcher(ruleMatcher);-
319 rules.clear();-
320 }
executed 1733 times by 1 test: end of block
Executed by:
  • tst_QMimeDatabase
1733
321 break;
executed 19205 times by 1 test: break;
Executed by:
  • tst_QMimeDatabase
19205
322 }-
323 default:
executed 160899 times by 1 test: default:
Executed by:
  • tst_QMimeDatabase
160899
324 break;
executed 160899 times by 1 test: break;
Executed by:
  • tst_QMimeDatabase
160899
325 }-
326 }-
327-
328 if (Q_UNLIKELY(reader.hasError())())) {
__builtin_expe...ror()), false)Description
TRUEnever evaluated
FALSEevaluated 17 times by 1 test
Evaluated by:
  • tst_QMimeDatabase
0-17
329 if (errorMessage) {
errorMessageDescription
TRUEnever evaluated
FALSEnever evaluated
0
330 *errorMessage = QString::fromLatin1asprintf("An error has been encountered at line %1%lld of %2%ls: %3%ls:").arg(,-
331 reader.lineNumber()).arg(),-
332 qUtf16Printable(fileName,),-
333 qUtf16Printable(reader.errorString());()));-
334 }
never executed: end of block
0
335 return false;
never executed: return false;
0
336 }-
337-
338 return true;
executed 17 times by 1 test: return true;
Executed by:
  • tst_QMimeDatabase
17
339#endif //QT_NO_XMLSTREAMREADER-
340}-
341-
342QT_END_NAMESPACE-
343-
344#endif // QT_NO_MIMETYPE-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9