/**************************************************************************** ** ** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies). ** Contact: Qt Software Information (qt-info@nokia.com) ** ** This file is part of the $MODULE$ of the Qt Toolkit. ** ** $TROLLTECH_DUAL_LICENSE$ ** ****************************************************************************/ #include #include "qabstractxmlnodemodel_p.h" #include "qitemmappingiterator_p.h" #include "qitem_p.h" #include "qxmlname.h" #include "qxmlquery_p.h" #include "qpullbridge_p.h" QT_BEGIN_NAMESPACE using namespace QPatternist; /*! \brief Bridges a QPatternist::SequenceIterator to QAbstractXmlPullProvider. \class QPatternist::PullBridge \internal \reentrant \ingroup xml-tools The approach of this class is rather straight forward since QPatternist::SequenceIterator and QAbstractXmlPullProvider are conceptually similar. While QPatternist::SequenceIterator only delivers top level items(since it's not an event stream, it's a list of items), PullBridge needs to recursively iterate the children of nodes too, which is achieved through the stack m_iterators. */ AbstractXmlPullProvider::Event PullBridge::next() { m_index = m_iterators.top().second->next(); if(!m_index.isNull()) { Item item(m_index); if(item && item.isAtomicValue()) m_current = AtomicValue; else { Q_ASSERT(item.isNode()); switch(m_index.kind()) { case QXmlNodeModelIndex::Attribute: { m_current = Attribute; break; } case QXmlNodeModelIndex::Comment: { m_current = Comment; break; } case QXmlNodeModelIndex::Element: { m_iterators.push(qMakePair(StartElement, m_index.iterate(QXmlNodeModelIndex::AxisChild))); m_current = StartElement; break; } case QXmlNodeModelIndex::Document: { m_iterators.push(qMakePair(StartDocument, m_index.iterate(QXmlNodeModelIndex::AxisChild))); m_current = StartDocument; break; } case QXmlNodeModelIndex::Namespace: { m_current = Namespace; break; } case QXmlNodeModelIndex::ProcessingInstruction: { m_current = ProcessingInstruction; break; } case QXmlNodeModelIndex::Text: { m_current = Text; break; } } } } else { if(m_iterators.isEmpty()) m_current = EndOfInput; else { switch(m_iterators.top().first) { case StartOfInput: { m_current = EndOfInput; break; } case StartElement: { m_current = EndElement; m_iterators.pop(); break; } case StartDocument: { m_current = EndDocument; m_iterators.pop(); break; } default: { Q_ASSERT_X(false, Q_FUNC_INFO, "Invalid value."); m_current = EndOfInput; } } } } return m_current; } AbstractXmlPullProvider::Event PullBridge::current() const { return m_current; } QXmlNodeModelIndex PullBridge::index() const { return m_index; } QSourceLocation PullBridge::sourceLocation() const { return m_index.model()->sourceLocation(m_index); } QXmlName PullBridge::name() const { return m_index.name(); } QVariant PullBridge::atomicValue() const { return QVariant(); } QString PullBridge::stringValue() const { return QString(); } QHash PullBridge::attributes() { Q_ASSERT(m_current == StartElement); QHash attributes; QXmlNodeModelIndex::Iterator::Ptr it = m_index.iterate(QXmlNodeModelIndex::AxisAttribute); QXmlNodeModelIndex index = it->next(); while (!index.isNull()) { const Item attribute(index); attributes.insert(index.name(), index.stringValue()); index = it->next(); } return attributes; } QHash PullBridge::attributeItems() { Q_ASSERT(m_current == StartElement); QHash attributes; QXmlNodeModelIndex::Iterator::Ptr it = m_index.iterate(QXmlNodeModelIndex::AxisAttribute); QXmlNodeModelIndex index = it->next(); while (!index.isNull()) { const Item attribute(index); attributes.insert(index.name(), QXmlItem(index)); index = it->next(); } return attributes; } QT_END_NAMESPACE