⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qxml.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    d->inputDevice = 0;    d->inputStream = 0;    setData(QString());#ifndef QT_NO_TEXTCODEC    d->encMapper = 0;#endif    d->nextReturnedEndOfData = true; // first call to next() will call fetchData()    d->encodingDeclBytes.clear();    d->encodingDeclChars.clear();    d->lookingForEncodingDecl = true;}/*!    Constructs an input source which contains no data.    \sa setData()*/QXmlInputSource::QXmlInputSource(){    init();}/*!    Constructs an input source and gets the data from device \a dev.    If \a dev is not open, it is opened in read-only mode. If \a dev    is 0 or it is not possible to read from the device, the input    source will contain no data.    \sa setData() fetchData() QIODevice*/QXmlInputSource::QXmlInputSource(QIODevice *dev){    init();    d->inputDevice = dev;    d->inputDevice->setTextModeEnabled(false);}#ifdef QT3_SUPPORT/*!    Use the QXmlInputSource(QIODevice *) constructor instead, with    the device used by \a stream.    \sa QTextStream::device()*/QXmlInputSource::QXmlInputSource(QTextStream& stream){    init();    d->inputStream = &stream;}/*!    Use QXmlInputSource(&\a file) instead.*/QXmlInputSource::QXmlInputSource(QFile& file){    init();    d->inputDevice = &file;}#endif/*!    Destructor.*/QXmlInputSource::~QXmlInputSource(){#ifndef QT_NO_TEXTCODEC    delete d->encMapper;#endif    delete d;}/*!Returns the next character of the input source. If this functionreaches the end of available data, it returnsQXmlInputSource::EndOfData. If you call next() after that, ittries to fetch more data by calling fetchData(). If thefetchData() call results in new data, this function returns thefirst character of that data; otherwise it returnsQXmlInputSource::EndOfDocument.Readers, such as QXmlSimpleReader, will assume that the end ofthe XML document has been reached if the this function returnsQXmlInputSource::EndOfDocument, and will check that thesupplied input is well-formed. Therefore, when reimplementingthis function, it is important to ensure that this behavior isduplicated.\sa reset() fetchData() QXmlSimpleReader::parse() QXmlSimpleReader::parseContinue()*/QChar QXmlInputSource::next(){    if (d->pos >= d->length) {        if (d->nextReturnedEndOfData) {            d->nextReturnedEndOfData = false;            fetchData();            if (d->pos >= d->length) {                return EndOfDocument;            }            return next();        }        d->nextReturnedEndOfData = true;        return EndOfData;    }    // QXmlInputSource has no way to signal encoding errors. The best we can do    // is return EndOfDocument. We do *not* return EndOfData, because the reader    // will then just call this function again to get the next char.    QChar c = d->unicode[d->pos++];    if (c.unicode() == EndOfData)        c = EndOfDocument;    return c;}/*!    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.    \sa next()*/void QXmlInputSource::reset(){    d->nextReturnedEndOfData = false;    d->pos = 0;}/*!    Returns the data the input source contains or an empty string if the    input source does not contain any data.    \sa setData() QXmlInputSource() fetchData()*/QString QXmlInputSource::data() const{    if (d->nextReturnedEndOfData) {        QXmlInputSource *that = const_cast<QXmlInputSource*>(this);        that->d->nextReturnedEndOfData = false;        that->fetchData();    }    return d->str;}/*!    Sets the data of the input source to \a dat.    If the input source already contains data, this function deletes    that data first.    \sa data()*/void QXmlInputSource::setData(const QString& dat){    d->str = dat;    d->unicode = dat.unicode();    d->pos = 0;    d->length = d->str.length();    d->nextReturnedEndOfData = false;}/*!    \overload    The data \a dat is passed through the correct text-codec, before    it is set.*/void QXmlInputSource::setData(const QByteArray& dat){    setData(fromRawData(dat));}/*!    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().    \sa data() next() QXmlInputSource()*/void QXmlInputSource::fetchData(){    enum    {        BufferSize = 1024    };    QByteArray rawData;    if (d->inputDevice || d->inputStream) {        QIODevice *device = d->inputDevice ? d->inputDevice : d->inputStream->device();        if (!device) {            if (d->inputStream && d->inputStream->string()) {                QString *s = d->inputStream->string();                rawData = QByteArray((const char *) s->constData(), s->size() * sizeof(QChar));            }        } else if (device->isOpen() || device->open(QIODevice::ReadOnly)) {            rawData.resize(BufferSize);            qint64 size = device->read(rawData.data(), BufferSize);            if (size != -1) {                // We don't want to give fromRawData() less than four bytes if we can avoid it.                while (size < 4) {                    if (!device->waitForReadyRead(-1))                        break;                    int ret = device->read(rawData.data() + size, BufferSize - size);                    if (ret <= 0)                        break;                    size += ret;                }            }            rawData.resize(qMax(qint64(0), size));        }        /* We do this inside the "if (d->inputDevice ..." scope         * because if we're not using a stream or device, that is,         * the user set a QString manually, we don't want to set         * d->str. */        setData(fromRawData(rawData));    }}#ifndef QT_NO_TEXTCODECstatic QString extractEncodingDecl(const QString &text, bool *needMoreText){    *needMoreText = false;    int l = text.length();    QString snip = QString::fromLatin1("<?xml").left(l);    if (l > 0 && !text.startsWith(snip))        return QString();    int endPos = text.indexOf(QLatin1Char('>'));    if (endPos == -1) {        *needMoreText = l < 255; // we won't look forever        return QString();    }    int pos = text.indexOf(QLatin1String("encoding"));    if (pos == -1 || pos >= endPos)        return QString();    while (pos < endPos) {        ushort uc = text.at(pos).unicode();        if (uc == '\'' || uc == '"')            break;        ++pos;    }    if (pos == endPos)        return QString();    QString encoding;    ++pos;    while (pos < endPos) {        ushort uc = text.at(pos).unicode();        if (uc == '\'' || uc == '"')            break;        encoding.append(uc);        ++pos;    }    return encoding;}#endif // QT_NO_TEXTCODEC/*!    This function reads the XML file from \a data and tries to    recognize the encoding. It converts the raw data \a data into a    QString and returns it. It tries its best to get the correct    encoding for the XML file.    If \a 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 \a beginning is false, it converts the    raw data using the encoding determined from prior calls.*/QString QXmlInputSource::fromRawData(const QByteArray &data, bool beginning){#ifdef QT_NO_TEXTCODEC    Q_UNUSED(beginning);    return QString::fromAscii(data.constData(), data.size());#else    if (data.size() == 0)        return QString();    if (beginning) {        delete d->encMapper;        d->encMapper = 0;    }    int mib = 106; // UTF-8    // This is the initial UTF codec we will read the encoding declaration with    if (d->encMapper == 0) {        d->encodingDeclBytes.clear();        d->encodingDeclChars.clear();        d->lookingForEncodingDecl = true;        // look for byte order mark and read the first 5 characters        if (data.size() >= 2) {            uchar ch1 = data.at(0);            uchar ch2 = data.at(1);            if (ch1 == 0xfe && ch2 == 0xff || ch1 == 0xff && ch2 == 0xfe)                mib = 1015; // UTF-16 with byte order mark            else if (ch1 == 0x3c && ch2 == 0x00)                mib = 1014; // UTF-16LE            else if (ch1 == 0x00 && ch2 == 0x3c)                mib = 1013; // UTF-16BE        }        QTextCodec *codec = QTextCodec::codecForMib(mib);        Q_ASSERT(codec);        d->encMapper = codec->makeDecoder();    }    QString input = d->encMapper->toUnicode(data, data.size());    if (d->lookingForEncodingDecl) {        d->encodingDeclChars += input;        bool needMoreText;        QString encoding = extractEncodingDecl(d->encodingDeclChars, &needMoreText);        if (!encoding.isEmpty()) {            if (QTextCodec *codec = QTextCodec::codecForName(encoding.toLatin1())) {                /* If the encoding is the same, we don't have to do toUnicode() all over again. */                if(codec->mibEnum() != mib) {                    delete d->encMapper;                    d->encMapper = codec->makeDecoder();                    /* The variable input can potentially be large, so we deallocate                     * it before calling toUnicode() in order to avoid having two                     * large QStrings in memory simultaneously. */                    input.clear();                    // prime the decoder with the data so far                    d->encMapper->toUnicode(d->encodingDeclBytes, d->encodingDeclBytes.size());                    // now feed it the new data                    input = d->encMapper->toUnicode(data, data.size());                }            }        }        d->encodingDeclBytes += data;        d->lookingForEncodingDecl = needMoreText;    }    return input;#endif}/********************************************* * * QXmlDefaultHandler * *********************************************//*!    \class QXmlContentHandler    \reentrant    \brief The QXmlContentHandler class provides an interface to    report the logical content of XML data.    \module XML    \ingroup xml-tools    If the application needs to be informed of basic parsing events,    it can implement this interface and activate it using    QXmlReader::setContentHandler(). The reader can then report basic    document-related events like the start and end of elements and    character data through this interface.    The order of events in this interface is very important, and    mirrors the order of information in the document itself. For    example, all of an element's content (character data, processing    instructions, and sub-elements) appears, in order, between the    startElement() event and the corresponding endElement() event.    The class QXmlDefaultHandler provides a default implementation for    this interface; subclassing from the QXmlDefaultHandler class is    very convenient if you only want to be informed of some parsing    events.    The startDocument() function is called at the start of the    document, and endDocument() is called at the end. Before parsing    begins setDocumentLocator() is called. For each element    startElement() is called, with endElement() being called at the    end of each element. The characters() function is called with    chunks of character data; ignorableWhitespace() is called with    chunks of whitespace and processingInstruction() is called with    processing instructions. If an entity is skipped skippedEntity()    is called. At the beginning of prefix-URI scopes    startPrefixMapping() is called.    \sa QXmlDTDHandler, QXmlDeclHandler, QXmlEntityResolver, QXmlErrorHandler,        QXmlLexicalHandler, {Introduction to SAX2}*//*!    \fn QXmlContentHandler::~QXmlContentHandler()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -