diff options
author | David Boddie <dboddie@trolltech.com> | 2009-08-19 14:36:23 (GMT) |
---|---|---|
committer | David Boddie <dboddie@trolltech.com> | 2009-08-19 14:36:23 (GMT) |
commit | f599d1fc571b392ab374cfe8b4952e9536e0bff2 (patch) | |
tree | 33ae1d9b6b3f28279bcd3f26b0f8253d0968a86c /examples/xml/streambookmarks/xbelreader.cpp | |
parent | 551a03486262804567e726962b93b41ded002205 (diff) | |
parent | 555fafe46e26b352e9d8ab5586910591761ab5ad (diff) | |
download | Qt-f599d1fc571b392ab374cfe8b4952e9536e0bff2.zip Qt-f599d1fc571b392ab374cfe8b4952e9536e0bff2.tar.gz Qt-f599d1fc571b392ab374cfe8b4952e9536e0bff2.tar.bz2 |
Merge branch 'master' of git@scm.dev.nokia.troll.no:qt/qt
Diffstat (limited to 'examples/xml/streambookmarks/xbelreader.cpp')
-rw-r--r-- | examples/xml/streambookmarks/xbelreader.cpp | 129 |
1 files changed, 50 insertions, 79 deletions
diff --git a/examples/xml/streambookmarks/xbelreader.cpp b/examples/xml/streambookmarks/xbelreader.cpp index 47c8c3d..0770643 100644 --- a/examples/xml/streambookmarks/xbelreader.cpp +++ b/examples/xml/streambookmarks/xbelreader.cpp @@ -60,61 +60,43 @@ XbelReader::XbelReader(QTreeWidget *treeWidget) //! [1] bool XbelReader::read(QIODevice *device) { - setDevice(device); + xml.setDevice(device); - while (!atEnd()) { - readNext(); - - if (isStartElement()) { - if (name() == "xbel" && attributes().value("version") == "1.0") - readXBEL(); - else - raiseError(QObject::tr("The file is not an XBEL version 1.0 file.")); - } + if (xml.readNextStartElement()) { + if (xml.name() == "xbel" && xml.attributes().value("version") == "1.0") + readXBEL(); + else + xml.raiseError(QObject::tr("The file is not an XBEL version 1.0 file.")); } - return !error(); + return !xml.error(); } //! [1] //! [2] -void XbelReader::readUnknownElement() +QString XbelReader::errorString() const { - Q_ASSERT(isStartElement()); - - while (!atEnd()) { - readNext(); - - if (isEndElement()) - break; - - if (isStartElement()) - readUnknownElement(); - } + return QObject::tr("%1\nLine %2, column %3") + .arg(xml.errorString()) + .arg(xml.lineNumber()) + .arg(xml.columnNumber()); } //! [2] //! [3] void XbelReader::readXBEL() { - Q_ASSERT(isStartElement() && name() == "xbel"); - - while (!atEnd()) { - readNext(); - - if (isEndElement()) - break; - - if (isStartElement()) { - if (name() == "folder") - readFolder(0); - else if (name() == "bookmark") - readBookmark(0); - else if (name() == "separator") - readSeparator(0); - else - readUnknownElement(); - } + Q_ASSERT(xml.isStartElement() && xml.name() == "xbel"); + + while (xml.readNextStartElement()) { + if (xml.name() == "folder") + readFolder(0); + else if (xml.name() == "bookmark") + readBookmark(0); + else if (xml.name() == "separator") + readSeparator(0); + else + xml.skipCurrentElement(); } } //! [3] @@ -122,9 +104,9 @@ void XbelReader::readXBEL() //! [4] void XbelReader::readTitle(QTreeWidgetItem *item) { - Q_ASSERT(isStartElement() && name() == "title"); + Q_ASSERT(xml.isStartElement() && xml.name() == "title"); - QString title = readElementText(); + QString title = xml.readElementText(); item->setText(0, title); } //! [4] @@ -132,63 +114,52 @@ void XbelReader::readTitle(QTreeWidgetItem *item) //! [5] void XbelReader::readSeparator(QTreeWidgetItem *item) { + Q_ASSERT(xml.isStartElement() && xml.name() == "separator"); + QTreeWidgetItem *separator = createChildItem(item); separator->setFlags(item->flags() & ~Qt::ItemIsSelectable); separator->setText(0, QString(30, 0xB7)); - readElementText(); + xml.skipCurrentElement(); } //! [5] void XbelReader::readFolder(QTreeWidgetItem *item) { - Q_ASSERT(isStartElement() && name() == "folder"); + Q_ASSERT(xml.isStartElement() && xml.name() == "folder"); QTreeWidgetItem *folder = createChildItem(item); - bool folded = (attributes().value("folded") != "no"); + bool folded = (xml.attributes().value("folded") != "no"); treeWidget->setItemExpanded(folder, !folded); - while (!atEnd()) { - readNext(); - - if (isEndElement()) - break; - - if (isStartElement()) { - if (name() == "title") - readTitle(folder); - else if (name() == "folder") - readFolder(folder); - else if (name() == "bookmark") - readBookmark(folder); - else if (name() == "separator") - readSeparator(folder); - else - readUnknownElement(); - } + while (xml.readNextStartElement()) { + if (xml.name() == "title") + readTitle(folder); + else if (xml.name() == "folder") + readFolder(folder); + else if (xml.name() == "bookmark") + readBookmark(folder); + else if (xml.name() == "separator") + readSeparator(folder); + else + xml.skipCurrentElement(); } } void XbelReader::readBookmark(QTreeWidgetItem *item) { - Q_ASSERT(isStartElement() && name() == "bookmark"); + Q_ASSERT(xml.isStartElement() && xml.name() == "bookmark"); QTreeWidgetItem *bookmark = createChildItem(item); bookmark->setFlags(bookmark->flags() | Qt::ItemIsEditable); bookmark->setIcon(0, bookmarkIcon); bookmark->setText(0, QObject::tr("Unknown title")); - bookmark->setText(1, attributes().value("href").toString()); - while (!atEnd()) { - readNext(); - - if (isEndElement()) - break; - - if (isStartElement()) { - if (name() == "title") - readTitle(bookmark); - else - readUnknownElement(); - } + bookmark->setText(1, xml.attributes().value("href").toString()); + + while (xml.readNextStartElement()) { + if (xml.name() == "title") + readTitle(bookmark); + else + xml.skipCurrentElement(); } } @@ -200,6 +171,6 @@ QTreeWidgetItem *XbelReader::createChildItem(QTreeWidgetItem *item) } else { childItem = new QTreeWidgetItem(treeWidget); } - childItem->setData(0, Qt::UserRole, name().toString()); + childItem->setData(0, Qt::UserRole, xml.name().toString()); return childItem; } |