qt6windows7/examples/corelib/serialization/convert/xmlconverter.cpp

467 lines
15 KiB
C++
Raw Normal View History

2023-10-30 06:33:08 +08:00
// Copyright (C) 2018 Intel Corporation.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "xmlconverter.h"
#include <QBitArray>
#include <QtCborCommon>
#include <QFile>
#include <QFloat16>
#include <QMetaType>
#include <QRegularExpression>
#include <QUrl>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
2023-11-02 01:02:52 +08:00
using namespace Qt::StringLiterals;
static const char xmlOptionHelp[] = "compact=no|yes Use compact XML form.\n";
2023-10-30 06:33:08 +08:00
static XmlConverter xmlConverter;
static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options);
static QVariantList listFromXml(QXmlStreamReader &xml, Converter::Options options)
{
QVariantList list;
2023-11-02 01:02:52 +08:00
while (!xml.atEnd() && !(xml.isEndElement() && xml.name() == "list"_L1)) {
2023-10-30 06:33:08 +08:00
xml.readNext();
switch (xml.tokenType()) {
case QXmlStreamReader::StartElement:
list << variantFromXml(xml, options);
continue;
case QXmlStreamReader::EndElement:
continue;
case QXmlStreamReader::Comment:
// ignore comments
continue;
case QXmlStreamReader::Characters:
// ignore whitespace
if (xml.isWhitespace())
continue;
Q_FALLTHROUGH();
default:
break;
}
2023-11-02 01:02:52 +08:00
fprintf(stderr, "%lld:%lld: Invalid XML %s '%s'.\n", xml.lineNumber(), xml.columnNumber(),
2023-10-30 06:33:08 +08:00
qPrintable(xml.tokenString()), qPrintable(xml.name().toString()));
exit(EXIT_FAILURE);
}
xml.readNext();
return list;
}
2023-11-02 01:02:52 +08:00
static VariantOrderedMap::value_type mapEntryFromXml(QXmlStreamReader &xml,
Converter::Options options)
2023-10-30 06:33:08 +08:00
{
QVariant key, value;
2023-11-02 01:02:52 +08:00
while (!xml.atEnd() && !(xml.isEndElement() && xml.name() == "entry"_L1)) {
2023-10-30 06:33:08 +08:00
xml.readNext();
switch (xml.tokenType()) {
case QXmlStreamReader::StartElement:
if (value.isValid())
break;
if (key.isValid())
value = variantFromXml(xml, options);
else
key = variantFromXml(xml, options);
continue;
case QXmlStreamReader::EndElement:
continue;
case QXmlStreamReader::Comment:
// ignore comments
continue;
case QXmlStreamReader::Characters:
// ignore whitespace
if (xml.isWhitespace())
continue;
Q_FALLTHROUGH();
default:
break;
}
2023-11-02 01:02:52 +08:00
fprintf(stderr, "%lld:%lld: Invalid XML %s '%s'.\n", xml.lineNumber(), xml.columnNumber(),
2023-10-30 06:33:08 +08:00
qPrintable(xml.tokenString()), qPrintable(xml.name().toString()));
exit(EXIT_FAILURE);
}
return { key, value };
}
static QVariant mapFromXml(QXmlStreamReader &xml, Converter::Options options)
{
QVariantMap map1;
VariantOrderedMap map2;
2023-11-02 01:02:52 +08:00
while (!xml.atEnd() && !(xml.isEndElement() && xml.name() == "map"_L1)) {
2023-10-30 06:33:08 +08:00
xml.readNext();
switch (xml.tokenType()) {
case QXmlStreamReader::StartElement:
2023-11-02 01:02:52 +08:00
if (xml.name() == "entry"_L1) {
2023-10-30 06:33:08 +08:00
auto pair = mapEntryFromXml(xml, options);
if (options & Converter::SupportsArbitraryMapKeys)
map2.append(pair);
else
map1.insert(pair.first.toString(), pair.second);
continue;
}
break;
case QXmlStreamReader::EndElement:
continue;
case QXmlStreamReader::Comment:
// ignore comments
continue;
case QXmlStreamReader::Characters:
// ignore whitespace
if (xml.isWhitespace())
continue;
Q_FALLTHROUGH();
default:
break;
}
2023-11-02 01:02:52 +08:00
fprintf(stderr, "%lld:%lld: Invalid XML %s '%s'.\n", xml.lineNumber(), xml.columnNumber(),
2023-10-30 06:33:08 +08:00
qPrintable(xml.tokenString()), qPrintable(xml.name().toString()));
exit(EXIT_FAILURE);
}
xml.readNext();
if (options & Converter::SupportsArbitraryMapKeys)
return QVariant::fromValue(map2);
return map1;
}
static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options)
{
QStringView name = xml.name();
2023-11-02 01:02:52 +08:00
if (name == "list"_L1)
2023-10-30 06:33:08 +08:00
return listFromXml(xml, options);
2023-11-02 01:02:52 +08:00
if (name == "map"_L1)
2023-10-30 06:33:08 +08:00
return mapFromXml(xml, options);
2023-11-02 01:02:52 +08:00
if (name != "value"_L1) {
2023-10-30 06:33:08 +08:00
fprintf(stderr, "%lld:%lld: Invalid XML key '%s'.\n",
xml.lineNumber(), xml.columnNumber(), qPrintable(name.toString()));
exit(EXIT_FAILURE);
}
QXmlStreamAttributes attrs = xml.attributes();
2023-11-02 01:02:52 +08:00
QStringView type = attrs.value("type"_L1);
2023-10-30 06:33:08 +08:00
forever {
xml.readNext();
if (xml.isComment())
continue;
if (xml.isCDATA() || xml.isCharacters() || xml.isEndElement())
break;
2023-11-02 01:02:52 +08:00
fprintf(stderr, "%lld:%lld: Invalid XML %s '%s'.\n", xml.lineNumber(), xml.columnNumber(),
2023-10-30 06:33:08 +08:00
qPrintable(xml.tokenString()), qPrintable(name.toString()));
exit(EXIT_FAILURE);
}
QStringView text = xml.text();
if (!xml.isCDATA())
text = text.trimmed();
QVariant result;
if (type.isEmpty()) {
// ok
2023-11-02 01:02:52 +08:00
} else if (type == "number"_L1) {
2023-10-30 06:33:08 +08:00
// try integer first
2023-11-02 01:02:52 +08:00
bool ok;
2023-10-30 06:33:08 +08:00
qint64 v = text.toLongLong(&ok);
if (ok) {
result = v;
} else {
// let's see floating point
double d = text.toDouble(&ok);
if (!ok) {
fprintf(stderr, "%lld:%lld: Invalid XML: could not interpret '%s' as a number.\n",
xml.lineNumber(), xml.columnNumber(), qPrintable(text.toString()));
exit(EXIT_FAILURE);
}
2023-11-02 01:02:52 +08:00
result = d;
2023-10-30 06:33:08 +08:00
}
2023-11-02 01:02:52 +08:00
} else if (type == "bytes"_L1) {
2023-10-30 06:33:08 +08:00
QByteArray data = text.toLatin1();
QStringView encoding = attrs.value("encoding");
2023-11-02 01:02:52 +08:00
if (encoding == "base64url"_L1) {
2023-10-30 06:33:08 +08:00
result = QByteArray::fromBase64(data, QByteArray::Base64UrlEncoding);
2023-11-02 01:02:52 +08:00
} else if (encoding == "hex"_L1) {
2023-10-30 06:33:08 +08:00
result = QByteArray::fromHex(data);
2023-11-02 01:02:52 +08:00
} else if (encoding.isEmpty() || encoding == "base64"_L1) {
2023-10-30 06:33:08 +08:00
result = QByteArray::fromBase64(data);
} else {
fprintf(stderr, "%lld:%lld: Invalid XML: unknown encoding '%s' for bytes.\n",
xml.lineNumber(), xml.columnNumber(), qPrintable(encoding.toString()));
exit(EXIT_FAILURE);
}
2023-11-02 01:02:52 +08:00
} else if (type == "string"_L1) {
2023-10-30 06:33:08 +08:00
result = text.toString();
2023-11-02 01:02:52 +08:00
} else if (type == "null"_L1) {
2023-10-30 06:33:08 +08:00
result = QVariant::fromValue(nullptr);
2023-11-02 01:02:52 +08:00
} else if (type == "CBOR simple type"_L1) {
2023-10-30 06:33:08 +08:00
result = QVariant::fromValue(QCborSimpleType(text.toShort()));
2023-11-02 01:02:52 +08:00
} else if (type == "bits"_L1) {
2023-10-30 06:33:08 +08:00
QBitArray ba;
ba.resize(text.size());
qsizetype n = 0;
for (qsizetype i = 0; i < text.size(); ++i) {
QChar c = text.at(i);
if (c == '1') {
ba.setBit(n++);
} else if (c == '0') {
++n;
} else if (!c.isSpace()) {
fprintf(stderr, "%lld:%lld: Invalid XML: invalid bit string '%s'.\n",
xml.lineNumber(), xml.columnNumber(), qPrintable(text.toString()));
exit(EXIT_FAILURE);
}
}
ba.resize(n);
result = ba;
} else {
int id = QMetaType::UnknownType;
2023-11-02 01:02:52 +08:00
if (type == "datetime"_L1)
2023-10-30 06:33:08 +08:00
id = QMetaType::QDateTime;
2023-11-02 01:02:52 +08:00
else if (type == "url"_L1)
2023-10-30 06:33:08 +08:00
id = QMetaType::QUrl;
2023-11-02 01:02:52 +08:00
else if (type == "uuid"_L1)
2023-10-30 06:33:08 +08:00
id = QMetaType::QUuid;
2023-11-02 01:02:52 +08:00
else if (type == "regex"_L1)
2023-10-30 06:33:08 +08:00
id = QMetaType::QRegularExpression;
else
id = QMetaType::fromName(type.toLatin1()).id();
if (id == QMetaType::UnknownType) {
fprintf(stderr, "%lld:%lld: Invalid XML: unknown type '%s'.\n",
xml.lineNumber(), xml.columnNumber(), qPrintable(type.toString()));
exit(EXIT_FAILURE);
}
result = text.toString();
if (!result.convert(QMetaType(id))) {
fprintf(stderr, "%lld:%lld: Invalid XML: could not parse content as type '%s'.\n",
xml.lineNumber(), xml.columnNumber(), qPrintable(type.toString()));
exit(EXIT_FAILURE);
}
}
do {
xml.readNext();
} while (xml.isComment() || xml.isWhitespace());
if (!xml.isEndElement()) {
2023-11-02 01:02:52 +08:00
fprintf(stderr, "%lld:%lld: Invalid XML %s '%s'.\n", xml.lineNumber(), xml.columnNumber(),
2023-10-30 06:33:08 +08:00
qPrintable(xml.tokenString()), qPrintable(name.toString()));
exit(EXIT_FAILURE);
}
xml.readNext();
return result;
}
static void variantToXml(QXmlStreamWriter &xml, const QVariant &v)
{
int type = v.userType();
if (type == QMetaType::QVariantList) {
QVariantList list = v.toList();
xml.writeStartElement("list");
for (const QVariant &v : list)
variantToXml(xml, v);
xml.writeEndElement();
} else if (type == QMetaType::QVariantMap || type == qMetaTypeId<VariantOrderedMap>()) {
2023-11-02 01:02:52 +08:00
const VariantOrderedMap map = (type == QMetaType::QVariantMap)
? VariantOrderedMap(v.toMap())
: qvariant_cast<VariantOrderedMap>(v);
2023-10-30 06:33:08 +08:00
xml.writeStartElement("map");
for (const auto &pair : map) {
xml.writeStartElement("entry");
variantToXml(xml, pair.first);
variantToXml(xml, pair.second);
xml.writeEndElement();
}
xml.writeEndElement();
} else {
xml.writeStartElement("value");
2023-11-02 01:02:52 +08:00
QString typeString = "type"_L1;
2023-10-30 06:33:08 +08:00
switch (type) {
case QMetaType::Short:
case QMetaType::UShort:
case QMetaType::Int:
case QMetaType::UInt:
case QMetaType::Long:
case QMetaType::ULong:
case QMetaType::LongLong:
case QMetaType::ULongLong:
case QMetaType::Float:
case QMetaType::Double:
xml.writeAttribute(typeString, "number");
xml.writeCharacters(v.toString());
break;
case QMetaType::QByteArray:
xml.writeAttribute(typeString, "bytes");
xml.writeAttribute("encoding", "base64");
xml.writeCharacters(QString::fromLatin1(v.toByteArray().toBase64()));
break;
case QMetaType::QString:
xml.writeAttribute(typeString, "string");
xml.writeCDATA(v.toString());
break;
case QMetaType::Bool:
xml.writeAttribute(typeString, "bool");
xml.writeCharacters(v.toString());
break;
case QMetaType::Nullptr:
xml.writeAttribute(typeString, "null");
break;
case QMetaType::UnknownType:
break;
case QMetaType::QDate:
case QMetaType::QTime:
case QMetaType::QDateTime:
xml.writeAttribute(typeString, "dateime");
xml.writeCharacters(v.toString());
break;
case QMetaType::QUrl:
xml.writeAttribute(typeString, "url");
xml.writeCharacters(v.toUrl().toString(QUrl::FullyEncoded));
break;
case QMetaType::QUuid:
xml.writeAttribute(typeString, "uuid");
xml.writeCharacters(v.toString());
break;
case QMetaType::QBitArray:
xml.writeAttribute(typeString, "bits");
xml.writeCharacters([](const QBitArray &ba) {
QString result;
for (qsizetype i = 0; i < ba.size(); ++i) {
if (i && i % 72 == 0)
result += '\n';
result += QLatin1Char(ba.testBit(i) ? '1' : '0');
}
return result;
}(v.toBitArray()));
break;
case QMetaType::QRegularExpression:
xml.writeAttribute(typeString, "regex");
xml.writeCharacters(v.toRegularExpression().pattern());
break;
default:
if (type == qMetaTypeId<qfloat16>()) {
xml.writeAttribute(typeString, "number");
xml.writeCharacters(QString::number(float(qvariant_cast<qfloat16>(v))));
} else if (type == qMetaTypeId<QCborSimpleType>()) {
xml.writeAttribute(typeString, "CBOR simple type");
xml.writeCharacters(QString::number(int(qvariant_cast<QCborSimpleType>(v))));
} else {
// does this convert to string?
const char *typeName = v.typeName();
QVariant copy = v;
if (copy.convert(QMetaType(QMetaType::QString))) {
xml.writeAttribute(typeString, QString::fromLatin1(typeName));
xml.writeCharacters(copy.toString());
} else {
fprintf(stderr, "XML: don't know how to serialize type '%s'.\n", typeName);
exit(EXIT_FAILURE);
}
}
}
xml.writeEndElement();
}
}
2023-11-02 01:02:52 +08:00
QString XmlConverter::name() const
2023-10-30 06:33:08 +08:00
{
2023-11-02 01:02:52 +08:00
return "xml"_L1;
2023-10-30 06:33:08 +08:00
}
2023-11-02 01:02:52 +08:00
Converter::Directions XmlConverter::directions() const
2023-10-30 06:33:08 +08:00
{
2023-11-02 01:02:52 +08:00
return Direction::InOut;
2023-10-30 06:33:08 +08:00
}
2023-11-02 01:02:52 +08:00
Converter::Options XmlConverter::outputOptions() const
2023-10-30 06:33:08 +08:00
{
return SupportsArbitraryMapKeys;
}
2023-11-02 01:02:52 +08:00
const char *XmlConverter::optionsHelp() const
2023-10-30 06:33:08 +08:00
{
return xmlOptionHelp;
}
2023-11-02 01:02:52 +08:00
bool XmlConverter::probeFile(QIODevice *f) const
2023-10-30 06:33:08 +08:00
{
if (QFile *file = qobject_cast<QFile *>(f)) {
2023-11-02 01:02:52 +08:00
if (file->fileName().endsWith(".xml"_L1))
2023-10-30 06:33:08 +08:00
return true;
}
return f->isReadable() && f->peek(5) == "<?xml";
}
2023-11-02 01:02:52 +08:00
QVariant XmlConverter::loadFile(QIODevice *f, const Converter *&outputConverter) const
2023-10-30 06:33:08 +08:00
{
if (!outputConverter)
outputConverter = this;
QXmlStreamReader xml(f);
xml.readNextStartElement();
QVariant v = variantFromXml(xml, outputConverter->outputOptions());
if (xml.hasError()) {
fprintf(stderr, "XML error: %s", qPrintable(xml.errorString()));
exit(EXIT_FAILURE);
}
return v;
}
2023-11-02 01:02:52 +08:00
void XmlConverter::saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const
2023-10-30 06:33:08 +08:00
{
bool compact = false;
for (const QString &s : options) {
2023-11-02 01:02:52 +08:00
if (s == "compact=no"_L1) {
2023-10-30 06:33:08 +08:00
compact = false;
2023-11-02 01:02:52 +08:00
} else if (s == "compact=yes"_L1) {
2023-10-30 06:33:08 +08:00
compact = true;
} else {
fprintf(stderr, "Unknown option '%s' to XML output. Valid options are:\n%s",
qPrintable(s), xmlOptionHelp);
exit(EXIT_FAILURE);
}
}
QXmlStreamWriter xml(f);
xml.setAutoFormatting(!compact);
xml.writeStartDocument();
variantToXml(xml, contents);
xml.writeEndDocument();
}