Qt 4.8
Public Functions | Static Public Variables | Protected Functions | Private Functions | Properties | List of all members
QXmlInputSource Class Reference

The QXmlInputSource class provides the input data for the QXmlReader subclasses. More...

#include <qxml.h>

Public Functions

virtual QString data () const
 Returns the data the input source contains or an empty string if the input source does not contain any data. More...
 
virtual void fetchData ()
 This function reads more data from the device that was set during construction. More...
 
virtual QChar next ()
 Returns the next character of the input source. More...
 
 QXmlInputSource ()
 Constructs an input source which contains no data. More...
 
 QXmlInputSource (QIODevice *dev)
 Constructs an input source and gets the data from device dev. More...
 
virtual void reset ()
 This function sets the position used by next() to the beginning of the data returned by data(). More...
 
virtual void setData (const QString &dat)
 Sets the data of the input source to dat. More...
 
virtual void setData (const QByteArray &dat)
 The data dat is passed through the correct text-codec, before it is set. More...
 
virtual ~QXmlInputSource ()
 Destructor. More...
 

Static Public Variables

static const ushort EndOfData = 0xfffe
 
static const ushort EndOfDocument = 0xffff
 

Protected Functions

virtual QString fromRawData (const QByteArray &data, bool beginning=false)
 This function reads the XML file from data and tries to recognize the encoding. More...
 

Private Functions

void init ()
 

Properties

QXmlInputSourcePrivated
 

Detailed Description

The QXmlInputSource class provides the input data for the QXmlReader subclasses.

Note
This class or function is reentrant.
Attention
Module: QtXml

All subclasses of QXmlReader read the input XML document from this class.

This class recognizes the encoding of the data by reading the encoding declaration in the XML file if it finds one, and reading the data using the corresponding encoding. If it does not find an encoding declaration, then it assumes that the data is either in UTF-8 or UTF-16, depending on whether it can find a byte-order mark.

There are two ways to populate the input source with data: you can construct it with a QIODevice* so that the input source reads the data from that device. Or you can set the data explicitly with one of the setData() functions.

Usually you either construct a QXmlInputSource that works on a QIODevice* or you construct an empty QXmlInputSource and set the data with setData(). There are only rare occasions where you would want to mix both methods.

The QXmlReader subclasses use the next() function to read the input character by character. If you want to start from the beginning again, use reset().

The functions data() and fetchData() are useful if you want to do something with the data other than parsing, e.g. displaying the raw XML file. The benefit of using the QXmlInputClass in such cases is that it tries to use the correct encoding.

See also
QXmlReader QXmlSimpleReader

Definition at line 158 of file qxml.h.

Constructors and Destructors

◆ QXmlInputSource() [1/2]

QXmlInputSource::QXmlInputSource ( )

Constructs an input source which contains no data.

See also
setData()

Definition at line 1370 of file qxml.cpp.

Referenced by QXmlInputSource().

1371 {
1372  init();
1373 }
void init()
Definition: qxml.cpp:1342

◆ QXmlInputSource() [2/2]

QXmlInputSource::QXmlInputSource ( QIODevice dev)

Constructs an input source and gets the data from device dev.

If dev is not open, it is opened in read-only mode. If dev is 0 or it is not possible to read from the device, the input source will contain no data.

See also
setData() fetchData() QIODevice

Definition at line 1383 of file qxml.cpp.

1384 {
1385  init();
1386  d->inputDevice = dev;
1387  if (dev->isOpen())
1388  d->inputDevice->setTextModeEnabled(false);
1389 }
QIODevice * inputDevice
Definition: qxml.cpp:229
void setTextModeEnabled(bool enabled)
If enabled is true, this function sets the Text flag on the device; otherwise the Text flag is remove...
Definition: qiodevice.cpp:499
bool isOpen() const
Returns true if the device is open; otherwise returns false.
Definition: qiodevice.cpp:530
void init()
Definition: qxml.cpp:1342
QXmlInputSourcePrivate * d
Definition: qxml.h:185

◆ ~QXmlInputSource()

QXmlInputSource::~QXmlInputSource ( )
virtual

Destructor.

Definition at line 1417 of file qxml.cpp.

1418 {
1419  // ### Qt 5: close the input device. See task 153111
1420 #ifndef QT_NO_TEXTCODEC
1421  delete d->encMapper;
1422 #endif
1423  delete d;
1424 }
QTextDecoder * encMapper
Definition: qxml.cpp:238
QXmlInputSourcePrivate * d
Definition: qxml.h:185

Functions

◆ data()

QString QXmlInputSource::data ( ) const
virtual

Returns the data the input source contains or an empty string if the input source does not contain any data.

See also
setData() QXmlInputSource() fetchData()

Definition at line 1491 of file qxml.cpp.

Referenced by QXmlSimpleReaderPrivate::parsePEReference(), and QXmlSimpleReaderPrivate::processReference().

1492 {
1493  if (d->nextReturnedEndOfData) {
1494  QXmlInputSource *that = const_cast<QXmlInputSource*>(this);
1495  that->d->nextReturnedEndOfData = false;
1496  that->fetchData();
1497  }
1498  return d->str;
1499 }
virtual void fetchData()
This function reads more data from the device that was set during construction.
Definition: qxml.cpp:1551
bool nextReturnedEndOfData
Definition: qxml.cpp:236
QXmlInputSourcePrivate * d
Definition: qxml.h:185
The QXmlInputSource class provides the input data for the QXmlReader subclasses.
Definition: qxml.h:158

◆ fetchData()

void QXmlInputSource::fetchData ( )
virtual

This function reads more data from the device that was set during construction.

If the input source already contained data, this function deletes that data first.

This object contains no data after a call to this function if the object was constructed without a device to read data from or if this function was not able to get more data from the device.

There are two occasions where a fetch is done implicitly by another function call: during construction (so that the object starts out with some initial data where available), and during a call to next() (if the data had run out).

You don't normally need to use this function if you use next().

See also
data() next() QXmlInputSource()

Definition at line 1551 of file qxml.cpp.

Referenced by data(), and QXmlSimpleReaderPrivate::processReference().

1552 {
1553  enum
1554  {
1555  BufferSize = 1024
1556  };
1557 
1558  QByteArray rawData;
1559 
1560  if (d->inputDevice || d->inputStream) {
1561  QIODevice *device = d->inputDevice ? d->inputDevice : d->inputStream->device();
1562 
1563  if (!device) {
1564  if (d->inputStream && d->inputStream->string()) {
1565  QString *s = d->inputStream->string();
1566  rawData = QByteArray((const char *) s->constData(), s->size() * sizeof(QChar));
1567  }
1568  } else if (device->isOpen() || device->open(QIODevice::ReadOnly)) {
1569  rawData.resize(BufferSize);
1570  qint64 size = device->read(rawData.data(), BufferSize);
1571 
1572  if (size != -1) {
1573  // We don't want to give fromRawData() less than four bytes if we can avoid it.
1574  while (size < 4) {
1575  if (!device->waitForReadyRead(-1))
1576  break;
1577  int ret = device->read(rawData.data() + size, BufferSize - size);
1578  if (ret <= 0)
1579  break;
1580  size += ret;
1581  }
1582  }
1583 
1584  rawData.resize(qMax(qint64(0), size));
1585  }
1586 
1587  /* We do this inside the "if (d->inputDevice ..." scope
1588  * because if we're not using a stream or device, that is,
1589  * the user set a QString manually, we don't want to set
1590  * d->str. */
1591  setData(fromRawData(rawData));
1592  }
1593 }
QString * string() const
Returns the current string assigned to the QTextStream, or 0 if no string has been assigned...
virtual bool waitForReadyRead(int msecs)
Blocks until new data is available for reading and the readyRead() signal has been emitted...
Definition: qiodevice.cpp:1616
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QIODevice * inputDevice
Definition: qxml.cpp:229
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QString class provides a Unicode character string.
Definition: qstring.h:83
QTextStream * inputStream
Definition: qxml.cpp:230
virtual QString fromRawData(const QByteArray &data, bool beginning=false)
This function reads the XML file from data and tries to recognize the encoding.
Definition: qxml.cpp:1650
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read...
Definition: qiodevice.cpp:791
bool isOpen() const
Returns true if the device is open; otherwise returns false.
Definition: qiodevice.cpp:530
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
virtual void setData(const QString &dat)
Sets the data of the input source to dat.
Definition: qxml.cpp:1509
__int64 qint64
Definition: qglobal.h:942
QIODevice * device() const
Returns the current device associated with the QTextStream, or 0 if no device has been assigned...
void resize(int size)
Sets the size of the byte array to size bytes.
QXmlInputSourcePrivate * d
Definition: qxml.h:185
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
Definition: qiodevice.cpp:570
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712

◆ fromRawData()

QString QXmlInputSource::fromRawData ( const QByteArray data,
bool  beginning = false 
)
protectedvirtual

This function reads the XML file from data and tries to recognize the encoding.

It converts the raw data data into a QString and returns it. It tries its best to get the correct encoding for the XML file.

If beginning is true, this function assumes that the data starts at the beginning of a new XML document and looks for an encoding declaration. If beginning is false, it converts the raw data using the encoding determined from prior calls.

Definition at line 1650 of file qxml.cpp.

1651 {
1652 #ifdef QT_NO_TEXTCODEC
1653  Q_UNUSED(beginning);
1654  return QString::fromAscii(data.constData(), data.size());
1655 #else
1656  if (data.size() == 0)
1657  return QString();
1658  if (beginning) {
1659  delete d->encMapper;
1660  d->encMapper = 0;
1661  }
1662 
1663  int mib = 106; // UTF-8
1664 
1665  // This is the initial UTF codec we will read the encoding declaration with
1666  if (d->encMapper == 0) {
1669  d->lookingForEncodingDecl = true;
1670 
1671  // look for byte order mark and read the first 5 characters
1672  if (data.size() >= 4) {
1673  uchar ch1 = data.at(0);
1674  uchar ch2 = data.at(1);
1675  uchar ch3 = data.at(2);
1676  uchar ch4 = data.at(3);
1677 
1678  if ((ch1 == 0 && ch2 == 0 && ch3 == 0xfe && ch4 == 0xff) ||
1679  (ch1 == 0xff && ch2 == 0xfe && ch3 == 0 && ch4 == 0))
1680  mib = 1017; // UTF-32 with byte order mark
1681  else if (ch1 == 0x3c && ch2 == 0x00 && ch3 == 0x00 && ch4 == 0x00)
1682  mib = 1019; // UTF-32LE
1683  else if (ch1 == 0x00 && ch2 == 0x00 && ch3 == 0x00 && ch4 == 0x3c)
1684  mib = 1018; // UTF-32BE
1685  }
1686  if (mib == 106 && data.size() >= 2) {
1687  uchar ch1 = data.at(0);
1688  uchar ch2 = data.at(1);
1689 
1690  if ((ch1 == 0xfe && ch2 == 0xff) || (ch1 == 0xff && ch2 == 0xfe))
1691  mib = 1015; // UTF-16 with byte order mark
1692  else if (ch1 == 0x3c && ch2 == 0x00)
1693  mib = 1014; // UTF-16LE
1694  else if (ch1 == 0x00 && ch2 == 0x3c)
1695  mib = 1013; // UTF-16BE
1696  }
1697 
1699  Q_ASSERT(codec);
1700 
1701  d->encMapper = codec->makeDecoder();
1702  }
1703 
1704  QString input = d->encMapper->toUnicode(data, data.size());
1705 
1706  if (d->lookingForEncodingDecl) {
1707  d->encodingDeclChars += input;
1708 
1709  bool needMoreText;
1710  QString encoding = extractEncodingDecl(d->encodingDeclChars, &needMoreText);
1711 
1712  if (!encoding.isEmpty()) {
1713  if (QTextCodec *codec = QTextCodec::codecForName(encoding.toLatin1())) {
1714  /* If the encoding is the same, we don't have to do toUnicode() all over again. */
1715  if(codec->mibEnum() != mib) {
1716  delete d->encMapper;
1717  d->encMapper = codec->makeDecoder();
1718 
1719  /* The variable input can potentially be large, so we deallocate
1720  * it before calling toUnicode() in order to avoid having two
1721  * large QStrings in memory simultaneously. */
1722  input.clear();
1723 
1724  // prime the decoder with the data so far
1726  // now feed it the new data
1727  input = d->encMapper->toUnicode(data, data.size());
1728  }
1729  }
1730  }
1731 
1732  d->encodingDeclBytes += data;
1733  d->lookingForEncodingDecl = needMoreText;
1734  }
1735 
1736  return input;
1737 #endif
1738 }
QByteArray encodingDeclBytes
Definition: qxml.cpp:241
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
QTextDecoder * encMapper
Definition: qxml.cpp:238
QTextDecoder * makeDecoder() const
Creates a QTextDecoder which stores enough state to decode chunks of char * data to create chunks of ...
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString extractEncodingDecl(const QString &text, bool *needMoreText)
Definition: qxml.cpp:1596
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
unsigned char uchar
Definition: qglobal.h:994
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
bool lookingForEncodingDecl
Definition: qxml.cpp:243
virtual int mibEnum() const =0
Subclasses of QTextCodec must reimplement this function.
static QTextCodec * codec(MYSQL *mysql)
Definition: qsql_mysql.cpp:220
int mib
static QTextCodec * codecForMib(int mib)
Returns the QTextCodec which matches the MIBenum mib.
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
virtual QString data() const
Returns the data the input source contains or an empty string if the input source does not contain an...
Definition: qxml.cpp:1491
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
QString toUnicode(const char *chars, int len)
Converts the first len bytes in chars to Unicode, returning the result.
static QTextCodec * codecForName(const QByteArray &name)
Searches all installed QTextCodec objects and returns the one which best matches name; the match is c...
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
QXmlInputSourcePrivate * d
Definition: qxml.h:185
QString encodingDeclChars
Definition: qxml.cpp:242
The QTextCodec class provides conversions between text encodings.
Definition: qtextcodec.h:62
char at(int i) const
Returns the character at index position i in the byte array.
Definition: qbytearray.h:413
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
void clear()
Clears the contents of the byte array and makes it empty.

◆ init()

void QXmlInputSource::init ( )
private

Definition at line 1342 of file qxml.cpp.

1343 {
1344  d = new QXmlInputSourcePrivate;
1345 
1346  QT_TRY {
1347  d->inputDevice = 0;
1348  d->inputStream = 0;
1349 
1350  setData(QString());
1351 #ifndef QT_NO_TEXTCODEC
1352  d->encMapper = 0;
1353 #endif
1354  d->nextReturnedEndOfData = true; // first call to next() will call fetchData()
1355 
1358  d->lookingForEncodingDecl = true;
1359  } QT_CATCH(...) {
1360  delete(d);
1361  QT_RETHROW;
1362  }
1363 }
QByteArray encodingDeclBytes
Definition: qxml.cpp:241
QIODevice * inputDevice
Definition: qxml.cpp:229
QTextDecoder * encMapper
Definition: qxml.cpp:238
The QString class provides a Unicode character string.
Definition: qstring.h:83
QTextStream * inputStream
Definition: qxml.cpp:230
bool nextReturnedEndOfData
Definition: qxml.cpp:236
#define QT_RETHROW
Definition: qglobal.h:1539
virtual void setData(const QString &dat)
Sets the data of the input source to dat.
Definition: qxml.cpp:1509
bool lookingForEncodingDecl
Definition: qxml.cpp:243
#define QT_CATCH(A)
Definition: qglobal.h:1537
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
QXmlInputSourcePrivate * d
Definition: qxml.h:185
QString encodingDeclChars
Definition: qxml.cpp:242
#define QT_TRY
Definition: qglobal.h:1536
void clear()
Clears the contents of the byte array and makes it empty.

◆ next()

QChar QXmlInputSource::next ( )
virtual

Returns the next character of the input source.

If this function reaches the end of available data, it returns QXmlInputSource::EndOfData. If you call next() after that, it tries to fetch more data by calling fetchData(). If the fetchData() call results in new data, this function returns the first character of that data; otherwise it returns QXmlInputSource::EndOfDocument.

Readers, such as QXmlSimpleReader, will assume that the end of the XML document has been reached if the this function returns QXmlInputSource::EndOfDocument, and will check that the supplied input is well-formed. Therefore, when reimplementing this function, it is important to ensure that this behavior is duplicated.

See also
reset() fetchData() QXmlSimpleReader::parse() QXmlSimpleReader::parseContinue()

Definition at line 1444 of file qxml.cpp.

Referenced by QXmlSimpleReaderPrivate::next().

1445 {
1446  if (d->pos >= d->length) {
1447  if (d->nextReturnedEndOfData) {
1448  d->nextReturnedEndOfData = false;
1449  fetchData();
1450  if (d->pos >= d->length) {
1451  return EndOfDocument;
1452  }
1453  return next();
1454  }
1455  d->nextReturnedEndOfData = true;
1456  return EndOfData;
1457  }
1458 
1459  // QXmlInputSource has no way to signal encoding errors. The best we can do
1460  // is return EndOfDocument. We do *not* return EndOfData, because the reader
1461  // will then just call this function again to get the next char.
1462  QChar c = d->unicode[d->pos++];
1463  if (c.unicode() == EndOfData)
1464  c = EndOfDocument;
1465  return c;
1466 }
static const ushort EndOfData
Definition: qxml.h:172
virtual void fetchData()
This function reads more data from the device that was set during construction.
Definition: qxml.cpp:1551
unsigned char c[8]
Definition: qnumeric_p.h:62
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
bool nextReturnedEndOfData
Definition: qxml.cpp:236
QXmlInputSourcePrivate * d
Definition: qxml.h:185
const QChar * unicode
Definition: qxml.cpp:233
static const ushort EndOfDocument
Definition: qxml.h:173
virtual QChar next()
Returns the next character of the input source.
Definition: qxml.cpp:1444

◆ reset()

void QXmlInputSource::reset ( )
virtual

This function sets the position used by next() to the beginning of the data returned by data().

This is useful if you want to use the input source for more than one parse.

Note
In the case that the underlying data source is a QIODevice, the current position in the device is not automatically set to the start of input. Call QIODevice::seek(0) on the device to do this.
See also
next()

Definition at line 1479 of file qxml.cpp.

1480 {
1481  d->nextReturnedEndOfData = false;
1482  d->pos = 0;
1483 }
bool nextReturnedEndOfData
Definition: qxml.cpp:236
QXmlInputSourcePrivate * d
Definition: qxml.h:185

◆ setData() [1/2]

void QXmlInputSource::setData ( const QString dat)
virtual

Sets the data of the input source to dat.

If the input source already contains data, this function deletes that data first.

See also
data()

Definition at line 1509 of file qxml.cpp.

Referenced by QDomDocument::setContent().

1510 {
1511  d->str = dat;
1512  d->unicode = dat.unicode();
1513  d->pos = 0;
1514  d->length = d->str.length();
1515  d->nextReturnedEndOfData = false;
1516 }
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
bool nextReturnedEndOfData
Definition: qxml.cpp:236
const QChar * unicode() const
Returns a &#39;\0&#39;-terminated Unicode representation of the string.
Definition: qstring.h:706
QXmlInputSourcePrivate * d
Definition: qxml.h:185
const QChar * unicode
Definition: qxml.cpp:233

◆ setData() [2/2]

void QXmlInputSource::setData ( const QByteArray dat)
virtual

The data dat is passed through the correct text-codec, before it is set.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 1527 of file qxml.cpp.

1528 {
1529  setData(fromRawData(dat));
1530 }
virtual QString fromRawData(const QByteArray &data, bool beginning=false)
This function reads the XML file from data and tries to recognize the encoding.
Definition: qxml.cpp:1650
virtual void setData(const QString &dat)
Sets the data of the input source to dat.
Definition: qxml.cpp:1509

Properties

◆ d

QXmlInputSourcePrivate* QXmlInputSource::d
private

Definition at line 185 of file qxml.h.

Referenced by data().

◆ EndOfData

const ushort QXmlInputSource::EndOfData = 0xfffe
static

Definition at line 172 of file qxml.h.

Referenced by QXmlAttributes::append(), and QXmlSimpleReaderPrivate::initData().

◆ EndOfDocument

const ushort QXmlInputSource::EndOfDocument = 0xffff
static

Definition at line 173 of file qxml.h.

Referenced by QXmlAttributes::append().


The documentation for this class was generated from the following files: