From 767319a5aac2462df84431917b3724f884091036 Mon Sep 17 00:00:00 2001 From: Luca Bellonda Date: Wed, 1 Jun 2016 00:06:13 +0200 Subject: Add support for 8bit encodings not ASCII compatible in QXMLStreamWriter. When using a 8 bit encoding to write a file, a test discovers if the encoding is really ASCII compatible by examining a letter and one of the XML reserved characters. EBCDIC, in the current base, was not well handled. [ChangeLog][QtCore][QXmlStreamWriter] Fixed a bug that prevented the generation of valid XML files when using encoding with 8 bit per character but not ASCII compatible. QXMLStreamWriter generated XML markup using always ASCII in this case. Change-Id: I9c86a122dd91b2290d50c358638442f99777d4ae Reviewed-by: Thiago Macieira --- src/corelib/xml/qxmlstream.cpp | 12 ++++--- .../auto/corelib/xml/qxmlstream/tst_qxmlstream.cpp | 39 ++++++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/corelib/xml/qxmlstream.cpp b/src/corelib/xml/qxmlstream.cpp index 64a130e45a..a235145669 100644 --- a/src/corelib/xml/qxmlstream.cpp +++ b/src/corelib/xml/qxmlstream.cpp @@ -3019,10 +3019,14 @@ void QXmlStreamWriterPrivate::checkIfASCIICompatibleCodec() { #ifndef QT_NO_TEXTCODEC Q_ASSERT(encoder); - // assumes ASCII-compatibility for all 8-bit encodings - QChar space = QLatin1Char(' '); - const QByteArray bytes = encoder->fromUnicode(&space, 1); - isCodecASCIICompatible = (bytes.count() == 1); + // test ASCII-compatibility using the letter 'a' + QChar letterA = QLatin1Char('a'); + const QByteArray bytesA = encoder->fromUnicode(&letterA, 1); + const bool isCodecASCIICompatibleA = (bytesA.count() == 1) && (bytesA[0] == 0x61) ; + QChar letterLess = QLatin1Char('<'); + const QByteArray bytesLess = encoder->fromUnicode(&letterLess, 1); + const bool isCodecASCIICompatibleLess = (bytesLess.count() == 1) && (bytesLess[0] == 0x3C) ; + isCodecASCIICompatible = isCodecASCIICompatibleA && isCodecASCIICompatibleLess ; #else isCodecASCIICompatible = true; #endif diff --git a/tests/auto/corelib/xml/qxmlstream/tst_qxmlstream.cpp b/tests/auto/corelib/xml/qxmlstream/tst_qxmlstream.cpp index 1da29ac3bd..b0fd1187f5 100644 --- a/tests/auto/corelib/xml/qxmlstream/tst_qxmlstream.cpp +++ b/tests/auto/corelib/xml/qxmlstream/tst_qxmlstream.cpp @@ -561,6 +561,7 @@ private slots: void checkCommentIndentation() const; void checkCommentIndentation_data() const; void crashInXmlStreamReader() const; + void write8bitCodec() const; void hasError() const; private: @@ -1578,5 +1579,43 @@ void tst_QXmlStream::hasError() const } +void tst_QXmlStream::write8bitCodec() const +{ + QBuffer outBuffer; + QVERIFY(outBuffer.open(QIODevice::WriteOnly)); + QXmlStreamWriter writer(&outBuffer); + writer.setAutoFormatting(false); + + QTextCodec *codec = QTextCodec::codecForName("IBM500"); + if (!codec) { + QSKIP("Encoding IBM500 not available."); + } + writer.setCodec(codec); + + writer.writeStartDocument(); + writer.writeStartElement("root"); + writer.writeAttribute("attrib", "1"); + writer.writeEndElement(); + writer.writeEndDocument(); + outBuffer.close(); + + // test 8 bit encoding + QByteArray values = outBuffer.data(); + QVERIFY(values.size() > 1); + // check '<' + QCOMPARE(values[0] & 0x00FF, 0x4c); + // check '?' + QCOMPARE(values[1] & 0x00FF, 0x6F); + + // convert the start of the XML + const QString expected = (""); + QTextDecoder *decoder = codec->makeDecoder(); + QVERIFY(decoder); + QString decodedText = decoder->toUnicode(values); + delete decoder; + QVERIFY(decodedText.startsWith(expected)); +} + + #include "tst_qxmlstream.moc" // vim: et:ts=4:sw=4:sts=4 -- cgit v1.2.3