173#if QT_CONFIG(xmlstreamreader)
174 QMimeTypeXMLData data;
176 QStack<QMimeMagicRule *> currentRules;
177 QList<QMimeMagicRule> rules;
178 QXmlStreamReader reader(dev);
179 ParseState ps = ParseBeginning;
180 while (!reader.atEnd()) {
181 switch (reader.readNext()) {
182 case QXmlStreamReader::StartElement: {
183 ps = nextState(ps, reader.name());
184 const QXmlStreamAttributes atts = reader.attributes();
186 case ParseMimeType: {
187 const QString name = atts.value(QLatin1StringView(mimeTypeAttributeC)).toString();
188 if (name.isEmpty()) {
189 reader.raiseError(QStringLiteral(
"Missing 'type'-attribute"));
195 case ParseGenericIcon:
196 data.genericIconName = atts.value(QLatin1StringView(nameAttributeC)).toString();
199 data.iconName = atts.value(QLatin1StringView(nameAttributeC)).toString();
201 case ParseGlobPattern: {
202 const QString pattern = atts.value(QLatin1StringView(patternAttributeC)).toString();
203 unsigned weight = atts.value(QLatin1StringView(weightAttributeC)).toInt();
204 const bool caseSensitive = atts.value(QLatin1StringView(caseSensitiveAttributeC)) ==
"true"_L1;
207 weight = QMimeGlobPattern::DefaultWeight;
209 Q_ASSERT(!data.name.isEmpty());
210 const QMimeGlobPattern glob(pattern, data.name, weight, caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
211 if (!process(glob, errorMessage))
213 data.addGlobPattern(pattern);
216 case ParseGlobDeleteAll:
217 data.globPatterns.clear();
218 data.hasGlobDeleteAll =
true;
220 case ParseSubClass: {
221 const QString inheritsFrom = atts.value(QLatin1StringView(mimeTypeAttributeC)).toString();
222 if (!inheritsFrom.isEmpty())
223 processParent(data.name, inheritsFrom);
228 QString locale = atts.value(QLatin1StringView(localeAttributeC)).toString();
229 const QString comment = reader.readElementText();
230 if (locale.isEmpty())
231 locale = QString::fromLatin1(
"default");
232 data.localeComments.insert(locale, comment);
236 const QString alias = atts.value(QLatin1StringView(mimeTypeAttributeC)).toString();
237 if (!alias.isEmpty())
238 processAlias(alias, data.name);
243 const auto priorityS = atts.value(QLatin1StringView(priorityAttributeC));
244 if (!priorityS.isEmpty()) {
245 if (!parseNumber(priorityS, &priority, errorMessage))
249 currentRules.clear();
253 case ParseMagicMatchRule: {
254 auto result = createMagicMatchRule(atts);
255 if (Q_UNLIKELY(!result.rule.isValid()))
256 qWarning(
"QMimeDatabase: Error parsing %ls\n%ls",
257 qUtf16Printable(fileName), qUtf16Printable(result.errorMessage));
258 QList<QMimeMagicRule> *ruleList;
259 if (currentRules.isEmpty())
262 ruleList = ¤tRules.top()->m_subMatches;
263 ruleList->append(std::move(result.rule));
265 currentRules.push(&ruleList->last());
269 reader.raiseError(
"Unexpected element <"_L1 + reader.name() + u'>');
277 case QXmlStreamReader::EndElement:
279 const auto elementName = reader.name();
280 if (elementName == QLatin1StringView(mimeTypeTagC)) {
281 if (!process(data, errorMessage))
284 }
else if (elementName == QLatin1StringView(matchTagC)) {
288 }
else if (elementName == QLatin1StringView(magicTagC)) {
291 QMimeMagicRuleMatcher ruleMatcher(data.name, priority);
292 ruleMatcher.addRules(rules);
293 processMagicMatcher(ruleMatcher);
295 ps = ParseOtherMimeTypeSubTag;
304 if (Q_UNLIKELY(reader.hasError())) {
306 *errorMessage = QString::asprintf(
"An error has been encountered at line %lld of %ls: %ls:",
308 qUtf16Printable(fileName),
309 qUtf16Printable(reader.errorString()));
318 *errorMessage =
"QXmlStreamReader is not available, cannot parse '%1'."_L1.arg(fileName);