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 <thiago.macieira@intel.com>
This commit is contained in:
Luca Bellonda 2016-06-01 00:06:13 +02:00
parent 5a15545ee2
commit 767319a5aa
2 changed files with 47 additions and 4 deletions

View File

@ -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

View File

@ -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 = ("<?xml version=\"1.0\" encoding=\"IBM500\"?>");
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