diff options
Diffstat (limited to 'src/xmlpatterns/iterators')
29 files changed, 3575 insertions, 0 deletions
diff --git a/src/xmlpatterns/iterators/iterators.pri b/src/xmlpatterns/iterators/iterators.pri new file mode 100644 index 0000000..3850334 --- /dev/null +++ b/src/xmlpatterns/iterators/iterators.pri @@ -0,0 +1,29 @@ +HEADERS += $$PWD/qcachingiterator_p.h \ + $$PWD/qdeduplicateiterator_p.h \ + $$PWD/qdistinctiterator_p.h \ + $$PWD/qemptyiterator_p.h \ + $$PWD/qexceptiterator_p.h \ + $$PWD/qindexofiterator_p.h \ + $$PWD/qinsertioniterator_p.h \ + $$PWD/qintersectiterator_p.h \ + $$PWD/qitemmappingiterator_p.h \ + $$PWD/qrangeiterator_p.h \ + $$PWD/qremovaliterator_p.h \ + $$PWD/qsequencemappingiterator_p.h \ + $$PWD/qsingletoniterator_p.h \ + $$PWD/qsubsequenceiterator_p.h \ + $$PWD/qtocodepointsiterator_p.h \ + $$PWD/qunioniterator_p.h + +SOURCES += $$PWD/qcachingiterator.cpp \ + $$PWD/qdeduplicateiterator.cpp \ + $$PWD/qdistinctiterator.cpp \ + $$PWD/qexceptiterator.cpp \ + $$PWD/qindexofiterator.cpp \ + $$PWD/qinsertioniterator.cpp \ + $$PWD/qintersectiterator.cpp \ + $$PWD/qrangeiterator.cpp \ + $$PWD/qremovaliterator.cpp \ + $$PWD/qsubsequenceiterator.cpp \ + $$PWD/qtocodepointsiterator.cpp \ + $$PWD/qunioniterator.cpp diff --git a/src/xmlpatterns/iterators/qcachingiterator.cpp b/src/xmlpatterns/iterators/qcachingiterator.cpp new file mode 100644 index 0000000..a1f6ade --- /dev/null +++ b/src/xmlpatterns/iterators/qcachingiterator.cpp @@ -0,0 +1,130 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + + + +#include "qcachingiterator_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +CachingIterator::CachingIterator(ItemSequenceCacheCell::Vector &cacheCells, + const VariableSlotID slot, + const DynamicContext::Ptr &context) : m_position(0), + m_varSlot(slot), + m_context(context), + m_cacheCells(cacheCells), + m_usingCache(true) +{ + Q_ASSERT(m_varSlot > -1); + Q_ASSERT(m_context); + Q_ASSERT(m_cacheCells.at(m_varSlot).sourceIterator); + Q_ASSERT_X((m_cacheCells.at(m_varSlot).cachedItems.isEmpty() && m_cacheCells.at(m_varSlot).cacheState == ItemSequenceCacheCell::Empty) || + m_cacheCells.at(m_varSlot).cacheState == ItemSequenceCacheCell::PartiallyPopulated, + Q_FUNC_INFO, + "It makes no sense to construct a CachingIterator for a cache that is ItemSequenceCacheCell::Full."); +} + +Item CachingIterator::next() +{ + ItemSequenceCacheCell &cell = m_cacheCells[m_varSlot]; + if(m_position == -1) + return Item(); + + if(m_usingCache) + { + ++m_position; + + /* QAbstractXmlForwardIterator::position() starts at 1, while Qt's container classes + * starts at 0. */ + if(m_position - 1 < cell.cachedItems.count()) + { + m_current = cell.cachedItems.at(m_position - 1); + return m_current; + } + else + { + cell.cacheState = ItemSequenceCacheCell::PartiallyPopulated; + m_usingCache = false; + /* We decrement here so we don't have to add a branch for this + * when using the source QAbstractXmlForwardIterator below. */ + --m_position; + } + } + + m_current = cell.sourceIterator->next(); + + if(m_current) + { + cell.cachedItems.append(m_current); + Q_ASSERT(cell.cacheState == ItemSequenceCacheCell::PartiallyPopulated); + ++m_position; + return m_current; + } + else + { + m_position = -1; + cell.cacheState = ItemSequenceCacheCell::Full; + return Item(); + } +} + +Item CachingIterator::current() const +{ + return m_current; +} + +xsInteger CachingIterator::position() const +{ + return m_position; +} + +Item::Iterator::Ptr CachingIterator::copy() const +{ + const ItemSequenceCacheCell &cell = m_cacheCells.at(m_varSlot); + if(cell.cacheState == ItemSequenceCacheCell::Full) + return makeListIterator(cell.cachedItems); + else + return Item::Iterator::Ptr(new CachingIterator(m_cacheCells, m_varSlot, m_context)); +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/iterators/qcachingiterator_p.h b/src/xmlpatterns/iterators/qcachingiterator_p.h new file mode 100644 index 0000000..ebf5fda --- /dev/null +++ b/src/xmlpatterns/iterators/qcachingiterator_p.h @@ -0,0 +1,129 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef Patternist_CachingIterator_H +#define Patternist_CachingIterator_H + +#include <QList> +#include <QVector> + +#include "qdynamiccontext_p.h" +#include "qitem_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short An QAbstractXmlForwardIterator that gets its item from a cache unless its empty, in + * which case it continues to populate the cache as well as deliver on its + * own from a source QAbstractXmlForwardIterator. + * + * @author Frans Englich <frans.fenglich@trolltech.com> + * @ingroup Patternist_iterators + */ + class CachingIterator : public Item::Iterator + { + public: + /** + * We always use the same cache cell so why don't we use it directly, + * instead of passing the slot and ItemSequenceCacheCell::Vector to + * this class? Because the GenericDynamicContext might decide to resize + * the vector and that would invalidate the reference. + * + * We intentionally pass in a non-const reference here. + */ + CachingIterator(ItemSequenceCacheCell::Vector &cacheCells, + const VariableSlotID slot, + const DynamicContext::Ptr &context); + + virtual Item next(); + virtual Item current() const; + virtual xsInteger position() const; + virtual Item::Iterator::Ptr copy() const; + + private: + Item m_current; + xsInteger m_position; + + /** + * This variable cannot be called m_slot, because + * /usr/include/sys/sysmacros.h on hpuxi-acc defines it. + */ + const VariableSlotID m_varSlot; + + /** + * We don't use the context. We only keep a reference such that it + * doesn't get deleted, and m_cacheCells starts to dangle. + */ + const DynamicContext::Ptr m_context; + + /** + * We intentionally store a reference here such that we are able to + * modify the item. + */ + ItemSequenceCacheCell::Vector &m_cacheCells; + + /** + * Whether this CachingIterator is delivering items from + * m_cacheCell.cacheItems or from m_cacheCell.sourceIterator. + */ + bool m_usingCache; + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/iterators/qdeduplicateiterator.cpp b/src/xmlpatterns/iterators/qdeduplicateiterator.cpp new file mode 100644 index 0000000..43088d5 --- /dev/null +++ b/src/xmlpatterns/iterators/qdeduplicateiterator.cpp @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qdeduplicateiterator_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +DeduplicateIterator::DeduplicateIterator(const Item::List &source) : ListIterator<Item>(source) + , m_listPos(0) +{ + Q_ASSERT(!Item()); + Q_ASSERT(!Item().isNode()); + Q_ASSERT(!Item().isAtomicValue()); +} + +Item DeduplicateIterator::next() +{ + if(m_listPos == m_list.count()) + { + m_current.reset(); + m_position = -1; + return Item(); + } + + Item next(m_list.at(m_listPos)); + + while(next.asNode().is(m_current.asNode())) + { + ++m_listPos; + if(m_listPos == m_list.count()) + { + m_current.reset(); + m_position = -1; + return Item(); + } + else + next = m_list.at(m_listPos); + } + + ++m_position; + m_current = next; + return next; +} + +xsInteger DeduplicateIterator::count() +{ + return QAbstractXmlForwardIterator<Item>::count(); +} + +Item::Iterator::Ptr DeduplicateIterator::copy() const +{ + return Item::Iterator::Ptr(new DeduplicateIterator(m_list)); +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/iterators/qdeduplicateiterator_p.h b/src/xmlpatterns/iterators/qdeduplicateiterator_p.h new file mode 100644 index 0000000..07881c5 --- /dev/null +++ b/src/xmlpatterns/iterators/qdeduplicateiterator_p.h @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef Patternist_DeduplicateIterator_H +#define Patternist_DeduplicateIterator_H + +#include <QList> + +#include "qexpression_p.h" +#include "qitem_p.h" +#include "qatomiccomparator_p.h" +#include "qcomparisonplatform_p.h" +#include "qsourcelocationreflection_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + + /** + * @short Performs deduplication of the nodes on its source list. + * + * @note The nodes in the source list must be in document order. + * + * @author Frans Englich <fenglich@trolltech.com> + * @ingroup Patternist_iterators + */ + class DeduplicateIterator : public ListIterator<Item> + { + public: + DeduplicateIterator(const Item::List &source); + + virtual Item next(); + virtual Item::Iterator::Ptr copy() const; + virtual xsInteger count(); + + private: + /** + * m_position in ListIteratorPlatform is the position that we + * show to the outside through position) but do not correspond + * to the position in m_list, since we skip entries in that one. + * + * However, this guy, m_listPos, is the position into m_list. + */ + int m_listPos; + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/iterators/qdistinctiterator.cpp b/src/xmlpatterns/iterators/qdistinctiterator.cpp new file mode 100644 index 0000000..43951ea --- /dev/null +++ b/src/xmlpatterns/iterators/qdistinctiterator.cpp @@ -0,0 +1,112 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include "qdistinctiterator_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +DistinctIterator::DistinctIterator(const Item::Iterator::Ptr &seq, + const AtomicComparator::Ptr &comp, + const Expression::ConstPtr &expression, + const DynamicContext::Ptr &context) + : m_seq(seq) + , m_context(context) + , m_expr(expression) + , m_position(0) +{ + Q_ASSERT(seq); + prepareComparison(comp); +} + +Item DistinctIterator::next() +{ + if(m_position == -1) + return Item(); + + const Item nitem(m_seq->next()); + if(!nitem) + { + m_position = -1; + m_current.reset(); + return Item(); + } + + const Item::List::const_iterator end(m_processed.constEnd()); + Item::List::const_iterator it(m_processed.constBegin()); + + for(; it != end; ++it) + { + if(flexibleCompare(*it, nitem, m_context)) + { + return next(); + } + } + + m_current = nitem; + ++m_position; + m_processed.append(nitem); + return nitem; +} + +Item DistinctIterator::current() const +{ + return m_current; +} + +xsInteger DistinctIterator::position() const +{ + return m_position; +} + +Item::Iterator::Ptr DistinctIterator::copy() const +{ + return Item::Iterator::Ptr(new DistinctIterator(m_seq->copy(), comparator(), m_expr, m_context)); +} + +const SourceLocationReflection *DistinctIterator::actualReflection() const +{ + return m_expr.data(); +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/iterators/qdistinctiterator_p.h b/src/xmlpatterns/iterators/qdistinctiterator_p.h new file mode 100644 index 0000000..86fbd79 --- /dev/null +++ b/src/xmlpatterns/iterators/qdistinctiterator_p.h @@ -0,0 +1,128 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef Patternist_DistinctIterator_H +#define Patternist_DistinctIterator_H + +#include <QList> + +#include "qexpression_p.h" +#include "qitem_p.h" +#include "qatomiccomparator_p.h" +#include "qcomparisonplatform_p.h" +#include "qsourcelocationreflection_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + + /** + * @short Filters another sequence by removing duplicates such that the items are unique. + * + * DistinctIterator takes an input sequence, and returns a sequence where each + * item is unique. Thus, DistinctIterator removes the duplicates of items + * in a sequence. DistinctIterator is central in the implementation of the + * <tt>fn:distinct-values()</tt> function. + * + * @see <a href="http://www.w3.org/TR/xpath-functions/#func-distinct-values">XQuery 1.0 + * and XPath 2.0 Functions and Operators, 15.1.6 fn:distinct-values</a> + * @author Frans Englich <fenglich@trolltech.com> + * @ingroup Patternist_iterators + */ + class DistinctIterator : public Item::Iterator + , public ComparisonPlatform<DistinctIterator, false> + , public SourceLocationReflection + { + public: + /** + * Creates a DistinctIterator. + * @param comp the AtomicComparator to be used for comparing values. This may be @c null, + * meaning the IndexOfIterator iterator will dynamically determine what comparator to use + * @param seq the sequence whose duplicates should be filtered out + * @param context the usual context, used for error reporting and by AtomicComparators. + * @param expression the Expression that this DistinctIterator is + * evaluating for. It is used for error reporting, via + * actualReflection(). + */ + DistinctIterator(const Item::Iterator::Ptr &seq, + const AtomicComparator::Ptr &comp, + const Expression::ConstPtr &expression, + const DynamicContext::Ptr &context); + + virtual Item next(); + virtual Item current() const; + virtual xsInteger position() const; + virtual Item::Iterator::Ptr copy() const; + virtual const SourceLocationReflection *actualReflection() const; + + inline AtomicComparator::Operator operatorID() const + { + return AtomicComparator::OperatorEqual; + } + + private: + const Item::Iterator::Ptr m_seq; + const DynamicContext::Ptr m_context; + const Expression::ConstPtr m_expr; + Item m_current; + xsInteger m_position; + Item::List m_processed; + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/iterators/qemptyiterator_p.h b/src/xmlpatterns/iterators/qemptyiterator_p.h new file mode 100644 index 0000000..310512e --- /dev/null +++ b/src/xmlpatterns/iterators/qemptyiterator_p.h @@ -0,0 +1,146 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef Patternist_EmptyIterator_H +#define Patternist_EmptyIterator_H + +#include "qabstractxmlforwarditerator_p.h" +#include "qprimitives_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + + /** + * @short An QAbstractXmlForwardIterator which always is empty. + * + * EmptyIterator is an QAbstractXmlForwardIterator over the type @c T, which always is empty. Other + * iterators can also be empty(or, at least behave as they are empty), but this + * class is special designed for this purpose and is therefore fast. + * + * EmptyIterator's constructor is protected, instances is retrieved from CommonValues. + * + * @author Frans Englich <fenglich@trolltech.com> + * @ingroup Patternist_iterators + */ + template<typename T> class EmptyIterator : public QAbstractXmlForwardIterator<T> + { + public: + /** + * @returns always a default constructed value, T(). + */ + virtual T next() + { + return T(); + } + + /** + * @returns always a default constructed value, T(). + */ + virtual T current() const + { + return T(); + } + + /** + * @returns always 0. + */ + virtual xsInteger position() const + { + return 0; + } + + /** + * @returns always @c this, the reverse of <tt>()</tt> is <tt>()</tt>. + */ + virtual typename QAbstractXmlForwardIterator<T>::Ptr toReversed() + { + return typename QAbstractXmlForwardIterator<T>::Ptr(const_cast<EmptyIterator<T> *>(this)); + } + + /** + * @returns always 0 + */ + virtual xsInteger count() + { + return 0; + } + + /** + * @returns @c this + */ + virtual typename QAbstractXmlForwardIterator<T>::Ptr copy() const + { + return typename QAbstractXmlForwardIterator<T>::Ptr(const_cast<EmptyIterator *>(this)); + } + + protected: + friend class CommonValues; + }; + + template<typename T> + static inline + typename QAbstractXmlForwardIterator<T>::Ptr + makeEmptyIterator() + { + return typename QAbstractXmlForwardIterator<T>::Ptr(new EmptyIterator<T>()); + } + +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/iterators/qexceptiterator.cpp b/src/xmlpatterns/iterators/qexceptiterator.cpp new file mode 100644 index 0000000..68609f7 --- /dev/null +++ b/src/xmlpatterns/iterators/qexceptiterator.cpp @@ -0,0 +1,122 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qitem_p.h" + +#include "qexceptiterator_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +ExceptIterator::ExceptIterator(const Item::Iterator::Ptr &it1, + const Item::Iterator::Ptr &it2) : m_it1(it1) + , m_it2(it2) + , m_position(0) + , m_node1(m_it1->next()) + , m_node2(m_it2->next()) +{ + Q_ASSERT(m_it1); + Q_ASSERT(m_it2); +} + +Item ExceptIterator::fromFirstOperand() +{ + ++m_position; + m_current = m_node1; + m_node1 = m_it1->next(); + + return m_current; +} + +Item ExceptIterator::next() +{ + while(true) + { + if(!m_node1) + { + m_position = -1; + m_current = Item(); + return Item(); + } + else if(!m_node2) + return fromFirstOperand(); + + if(m_node1.asNode().model() != m_node2.asNode().model()) + return fromFirstOperand(); + + switch(m_node1.asNode().compareOrder(m_node2.asNode())) + { + case QXmlNodeModelIndex::Precedes: + return fromFirstOperand(); + case QXmlNodeModelIndex::Follows: + { + m_node2 = m_it2->next(); + if(m_node2) + continue; + else + return fromFirstOperand(); + } + default: + { + m_node1 = m_it1->next(); + m_node2 = m_it2->next(); + } + } + } +} + +Item ExceptIterator::current() const +{ + return m_current; +} + +xsInteger ExceptIterator::position() const +{ + return m_position; +} + +Item::Iterator::Ptr ExceptIterator::copy() const +{ + return Item::Iterator::Ptr(new ExceptIterator(m_it1->copy(), m_it2->copy())); +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/iterators/qexceptiterator_p.h b/src/xmlpatterns/iterators/qexceptiterator_p.h new file mode 100644 index 0000000..7528ef6 --- /dev/null +++ b/src/xmlpatterns/iterators/qexceptiterator_p.h @@ -0,0 +1,101 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef Patternist_ExceptIterator_H +#define Patternist_ExceptIterator_H + +#include "qitem_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short Implements the @c except operator. That is, the computation + * of the sequence of nodes from one sequence, that doesn't appear in the + * other. + * + * @ingroup Patternist_iterators + */ + class ExceptIterator : public Item::Iterator + { + public: + /** + * It is assumed that @p it1 and @p it2 are in document order and + * without duplicates. + */ + ExceptIterator(const Item::Iterator::Ptr &it1, + const Item::Iterator::Ptr &it2); + + virtual Item next(); + virtual Item current() const; + virtual xsInteger position() const; + virtual Item::Iterator::Ptr copy() const; + + private: + inline Item fromFirstOperand(); + + const Item::Iterator::Ptr m_it1; + const Item::Iterator::Ptr m_it2; + Item m_current; + xsInteger m_position; + Item m_node1; + Item m_node2; + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/iterators/qindexofiterator.cpp b/src/xmlpatterns/iterators/qindexofiterator.cpp new file mode 100644 index 0000000..df7edeb --- /dev/null +++ b/src/xmlpatterns/iterators/qindexofiterator.cpp @@ -0,0 +1,115 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qinteger_p.h" + +#include "qindexofiterator_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +IndexOfIterator::IndexOfIterator(const Item::Iterator::Ptr &seq, + const Item &searchParam, + const AtomicComparator::Ptr &comp, + const DynamicContext::Ptr &context, + const Expression::ConstPtr &expr) + : m_seq(seq) + , m_searchParam(searchParam) + , m_context(context) + , m_expr(expr) + , m_position(0) + , m_seqPos(0) +{ + Q_ASSERT(seq); + Q_ASSERT(searchParam); + prepareComparison(comp); +} + +Item IndexOfIterator::next() +{ + if(m_position == -1) + return Item(); + + const Item item(m_seq->next()); + ++m_seqPos; + + if(!item) + { + m_current.reset(); + m_position = -1; + return Item(); + } + + if(flexibleCompare(item, m_searchParam, m_context)) + { + ++m_position; + return Integer::fromValue(m_seqPos); + } + + return next(); +} + +Item IndexOfIterator::current() const +{ + return m_current; +} + +xsInteger IndexOfIterator::position() const +{ + return m_position; +} + +Item::Iterator::Ptr IndexOfIterator::copy() const +{ + return Item::Iterator::Ptr(new IndexOfIterator(m_seq->copy(), + m_searchParam, + comparator(), + m_context, + m_expr)); +} + +const SourceLocationReflection *IndexOfIterator::actualReflection() const +{ + return m_expr.data(); +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/iterators/qindexofiterator_p.h b/src/xmlpatterns/iterators/qindexofiterator_p.h new file mode 100644 index 0000000..62594f9 --- /dev/null +++ b/src/xmlpatterns/iterators/qindexofiterator_p.h @@ -0,0 +1,129 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef Patternist_IndexOfIterator_H +#define Patternist_IndexOfIterator_H + +#include "qitem_p.h" +#include "qatomiccomparator_p.h" +#include "qcomparisonplatform_p.h" +#include "qdynamiccontext_p.h" +#include "qexpression_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short Forms an QAbstractXmlForwardIterator over a sequence of integers, which each is the position + * of where a search parameter appeared in another QAbstractXmlForwardIterator. + * + * @see <a href="http://www.w3.org/TR/xpath-functions/#func-index-of">XQuery 1.0 + * and XPath 2.0 Functions and Operators, 15.1.3 fn:index-of</a> + * @author Frans Englich <fenglich@trolltech.com> + * @ingroup Patternist_iterators + */ + class IndexOfIterator : public Item::Iterator + , public ComparisonPlatform<IndexOfIterator, false> + , public SourceLocationReflection + { + public: + + /** + * Creates an IndexOfIterator, whose next() function returns integers being + * the index positions of where @p searchParam was found in @p inputSequence. + * + * @param comp the AtomicComparator to be used for comparing values. This may be @c null, + * meaning the IndexOfIterator iterator will dynamically determine what comparator to use + * on an item per item basis, which is slower. + * @param searchParam the item which should be compared to the items in @p inputSequence. + * @param inputSequence the input sequence which indexes of the @p searchParam should + * be returned for. + * @param context the usual DynamicContext + * @param expr the Expression that this IndexOfIterator is evaluating + * for. It is used for error reporting, via actualReflection(). + */ + IndexOfIterator(const Item::Iterator::Ptr &inputSequence, + const Item &searchParam, + const AtomicComparator::Ptr &comp, + const DynamicContext::Ptr &context, + const Expression::ConstPtr &expr); + + virtual Item next(); + virtual Item current() const; + virtual xsInteger position() const; + virtual Item::Iterator::Ptr copy() const; + + inline AtomicComparator::Operator operatorID() const + { + return AtomicComparator::OperatorEqual; + } + + virtual const SourceLocationReflection *actualReflection() const; + + private: + const Item::Iterator::Ptr m_seq; + const Item m_searchParam; + const DynamicContext::Ptr m_context; + const Expression::ConstPtr m_expr; + Item m_current; + xsInteger m_position; + xsInteger m_seqPos; + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/iterators/qinsertioniterator.cpp b/src/xmlpatterns/iterators/qinsertioniterator.cpp new file mode 100644 index 0000000..7d4eb98 --- /dev/null +++ b/src/xmlpatterns/iterators/qinsertioniterator.cpp @@ -0,0 +1,128 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include "qinsertioniterator_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +InsertionIterator::InsertionIterator(const Item::Iterator::Ptr &target, + const xsInteger pos, + const Item::Iterator::Ptr &inserts) + : m_target(target), + m_insertPos(pos), + m_inserts(inserts), + m_position(0), + m_isInserting(pos == 1) +{ + Q_ASSERT(target); + Q_ASSERT(inserts); + Q_ASSERT(m_insertPos >= 1); +} + +Item InsertionIterator::next() +{ + if(m_isInserting) + { + m_current = m_inserts->next(); + + if(m_current) + { + ++m_position; + return m_current; + } + } + else if(m_position == (m_insertPos - 1) && !m_isInserting) + { /* Entered only the first time insertion starts. */ + m_isInserting = true; + return next(); + } + + ++m_position; + m_current = m_target->next(); + + if(m_current) + return m_current; + else if(m_inserts->position() == -1) /* We're at the end of the both iterators. */ + { + m_position = -1; + m_current.reset(); + return Item(); + } + + /* Insert the insertion iterator, since it's still left. */ + Q_ASSERT(m_target->position() < m_insertPos); + m_isInserting = true; + m_current = m_inserts->next(); + + if(m_current) + return m_current; + else + { + /* m_current is already null, so no need to reset it. */ + m_position = -1; + return Item(); + } +} + +xsInteger InsertionIterator::count() +{ + return m_target->count() + m_inserts->count(); +} + +Item InsertionIterator::current() const +{ + return m_current; +} + +xsInteger InsertionIterator::position() const +{ + return m_position; +} + +Item::Iterator::Ptr InsertionIterator::copy() const +{ + return Item::Iterator::Ptr(new InsertionIterator(m_target->copy(), m_insertPos, m_inserts->copy())); +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/iterators/qinsertioniterator_p.h b/src/xmlpatterns/iterators/qinsertioniterator_p.h new file mode 100644 index 0000000..4d89c26 --- /dev/null +++ b/src/xmlpatterns/iterators/qinsertioniterator_p.h @@ -0,0 +1,120 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef Patternist_InsertionIterator_H +#define Patternist_InsertionIterator_H + +#include "qabstractxmlforwarditerator_p.h" +#include "qitem_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short Conceptually inserts one QAbstractXmlForwardIterator into another, make two QAbstractXmlForwardIterator instances appear as one. + * + * An InsertionIterator represents a sequence that is the merge of two + * sequences, where one of the iterators is conceptually inserted at a + * given position. This is done while retaining the characteristic of being + * pull-based. + * + * InsertionIterator contains the logic for the implementation of the <tt>fn:insert-before()</tt> + * function, whose definition therefore specifies the detailed behaviors of the + * InsertionIterator. + * + * @see <a href="http://www.w3.org/TR/xpath-functions/#func-insert-before">XQuery 1.0 + * and XPath 2.0 Functions and Operators, 15.1.7 fn:insert-before</a> + * @author Frans Englich <fenglich@trolltech.com> + * @ingroup Patternist_iterators + */ + class InsertionIterator : public Item::Iterator + { + public: + + /** + * Creates an InsertionIterator whose result is a merge of the + * iterator @p insertIterator into the iterator @p target at position @p + * position. + * + * @param target the iterator containing the items that the + * item in @p insertIterator will be inserted into. + * @param position the insertion position. Must be 1 or larger + * @param insertIterator the iterator containing the items to insert + * at position @p position + */ + InsertionIterator(const Item::Iterator::Ptr &target, + const xsInteger position, + const Item::Iterator::Ptr &insertIterator); + + virtual Item next(); + virtual Item current() const; + virtual xsInteger position() const; + virtual xsInteger count(); + virtual Item::Iterator::Ptr copy() const; + + private: + const Item::Iterator::Ptr m_target; + const xsInteger m_insertPos; + const Item::Iterator::Ptr m_inserts; + Item m_current; + xsInteger m_position; + bool m_isInserting; + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/iterators/qintersectiterator.cpp b/src/xmlpatterns/iterators/qintersectiterator.cpp new file mode 100644 index 0000000..3073810 --- /dev/null +++ b/src/xmlpatterns/iterators/qintersectiterator.cpp @@ -0,0 +1,115 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qitem_p.h" + +#include "qintersectiterator_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +IntersectIterator::IntersectIterator(const Item::Iterator::Ptr &it1, + const Item::Iterator::Ptr &it2) : m_it1(it1), + m_it2(it2), + m_position(0), + m_node1(m_it1->next()), + m_node2(m_it2->next()) +{ + Q_ASSERT(m_it1); + Q_ASSERT(m_it2); +} + +Item IntersectIterator::next() +{ + if(!m_node1 || !m_node2) + return closedExit(); + + do + { + if(m_node1.asNode().model() == m_node2.asNode().model()) + { + switch(m_node1.asNode().compareOrder(m_node2.asNode())) + { + case QXmlNodeModelIndex::Precedes: + { + m_node1 = m_it1->next(); + break; + } + case QXmlNodeModelIndex::Follows: + { + m_node2 = m_it2->next(); + break; + } + default: + { + m_current = m_node2; + m_node1 = m_it1->next(); + m_node2 = m_it2->next(); + ++m_position; + return m_current; + } + } + } + else + m_node2 = m_it2->next(); + } + while(m_node1 && m_node2); + + return Item(); +} + +Item IntersectIterator::current() const +{ + return m_current; +} + +xsInteger IntersectIterator::position() const +{ + return m_position; +} + +Item::Iterator::Ptr IntersectIterator::copy() const +{ + return Item::Iterator::Ptr(new IntersectIterator(m_it1->copy(), m_it2->copy())); +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/iterators/qintersectiterator_p.h b/src/xmlpatterns/iterators/qintersectiterator_p.h new file mode 100644 index 0000000..c8ada57 --- /dev/null +++ b/src/xmlpatterns/iterators/qintersectiterator_p.h @@ -0,0 +1,107 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef Patternist_IntersectIterator_H +#define Patternist_IntersectIterator_H + +#include "qitem_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short Implements the @c intersect operator. That is, the computation + * of the intersection between two sequences of nodes. + * + * The @c intersect operator can be seen as logical @c AND of the two sets. + * + * @ingroup Patternist_iterators + */ + class IntersectIterator : public Item::Iterator + { + public: + /** + * It is assumed that @p it1 and @p it2 are in document order and + * without duplicates. + */ + IntersectIterator(const Item::Iterator::Ptr &it1, + const Item::Iterator::Ptr &it2); + + virtual Item next(); + virtual Item current() const; + virtual xsInteger position() const; + virtual Item::Iterator::Ptr copy() const; + + private: + inline Item closedExit() + { + m_position = -1; + m_current = Item(); + return Item(); + } + + const Item::Iterator::Ptr m_it1; + const Item::Iterator::Ptr m_it2; + Item m_current; + xsInteger m_position; + Item m_node1; + Item m_node2; + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/iterators/qitemmappingiterator_p.h b/src/xmlpatterns/iterators/qitemmappingiterator_p.h new file mode 100644 index 0000000..284296c --- /dev/null +++ b/src/xmlpatterns/iterators/qitemmappingiterator_p.h @@ -0,0 +1,190 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef Patternist_ItemMappingIterator_H +#define Patternist_ItemMappingIterator_H + +#include "qabstractxmlforwarditerator_p.h" +#include "qdynamiccontext_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short Proxies another QAbstractXmlForwardIterator, and for each item, returns the + * Item returned from a mapping function. + * + * ItemMappingIterator is practical when the items in an QAbstractXmlForwardIterator needs to + * be translated to another sequence, while still doing it in a pipe-lined + * fashion. + * + * This is achieved by that ItemMappingIterator's constructor takes + * an instance of a class, that must have the following member: + * + * @code + * TResult::Ptr mapToItem(const TSource &item, + * const Context &context) const + * @endcode + * + * For each item in the QAbstractXmlForwardIterator ItemMappingIterator proxies, this function is + * called and its return value becomes the return value of the ItemMappingIterator. If the + * mapping function returns null, ItemMappingIterator maps the next item in the source sequence + * such that a contiguous sequence of items is returned. + * + * Declaring the mapToItem() function as inline, can be a good way to improve performance. + * + * @see SequenceMappingIterator + * @author Frans Englich <fenglich@trolltech.com> + * @ingroup Patternist_iterators + */ + template<typename TResult, typename TSource, typename TMapper, typename Context = DynamicContext::Ptr> + class ItemMappingIterator : public QAbstractXmlForwardIterator<TResult> + { + public: + /** + * Constructs an ItemMappingIterator. + * + * @param mapper the object that has the mapToItem() sequence. + * @param iterator the QAbstractXmlForwardIterator whose items should be mapped. + * @param context the context that will be passed to the map function. + * May be null. + */ + ItemMappingIterator(const TMapper &mapper, + const typename QAbstractXmlForwardIterator<TSource>::Ptr &iterator, + const Context &context) : m_mapper(mapper) + , m_it(iterator) + , m_context(context) + , m_position(0) + { + Q_ASSERT(mapper); + Q_ASSERT(iterator); + } + + /** + * @returns the next item in the sequence, or + * @c null if the end have been reached. + */ + virtual TResult next() + { + const TSource sourceItem(m_it->next()); + + if(qIsForwardIteratorEnd(sourceItem)) + { + m_current = TResult(); + m_position = -1; + return TResult(); + } + else + { + m_current = m_mapper->mapToItem(sourceItem, m_context); + if(qIsForwardIteratorEnd(m_current)) + return next(); /* The mapper returned null, so continue with the next in the source. */ + else + { + ++m_position; + return m_current; + } + } + } + + virtual TResult current() const + { + return m_current; + } + + virtual xsInteger position() const + { + return m_position; + } + + virtual typename QAbstractXmlForwardIterator<TResult>::Ptr copy() const + { + return typename QAbstractXmlForwardIterator<TResult>::Ptr + (new ItemMappingIterator<TResult, TSource, TMapper, Context>(m_mapper, m_it->copy(), m_context)); + } + + private: + const TMapper m_mapper; + const typename QAbstractXmlForwardIterator<TSource>::Ptr m_it; + const Context m_context; + TResult m_current; + xsInteger m_position; + }; + + /** + * @short An object generator for ItemMappingIterator. + * + * makeItemMappingIterator() is a convenience function for avoiding specifying + * the full template instantiation for ItemMappingIterator. Conceptually, it + * is identical to Qt's qMakePair(). + * + * @relates ItemMappingIterator + */ + template<typename TResult, typename TSource, typename TMapper, typename Context> + static inline + typename QAbstractXmlForwardIterator<TResult>::Ptr + makeItemMappingIterator(const TMapper &mapper, + const QExplicitlySharedDataPointer<QAbstractXmlForwardIterator<TSource> > &source, + const Context &context) + { + return typename QAbstractXmlForwardIterator<TResult>::Ptr + (new ItemMappingIterator<TResult, TSource, TMapper, Context>(mapper, source, context)); + } +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/iterators/qrangeiterator.cpp b/src/xmlpatterns/iterators/qrangeiterator.cpp new file mode 100644 index 0000000..7f8f629 --- /dev/null +++ b/src/xmlpatterns/iterators/qrangeiterator.cpp @@ -0,0 +1,126 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qinteger_p.h" + +#include "qrangeiterator_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +RangeIterator::RangeIterator(const xsInteger start, + const Direction direction, + const xsInteger end) + : m_start(start), + m_end(end), + m_position(0), + m_count(start), + m_direction(direction), + m_increment(m_direction == Forward ? 1 : -1) +{ + Q_ASSERT(m_start < m_end); + Q_ASSERT(m_direction == Backward || m_direction == Forward); + + if(m_direction == Backward) + { + qSwap(m_start, m_end); + m_count = m_start; + } +} + +Item RangeIterator::next() +{ + if(m_position == -1) + return Item(); + else if((m_direction == Forward && m_count > m_end) || + (m_direction == Backward && m_count < m_end)) + { + m_position = -1; + m_current.reset(); + return Item(); + } + else + { + m_current = Integer::fromValue(m_count); + m_count += m_increment; + ++m_position; + return m_current; + } +} + +xsInteger RangeIterator::count() +{ + /* This complication is for handling that m_start & m_end may be reversed. */ + xsInteger ret; + + if(m_start < m_end) + ret = m_end - m_start; + else + ret = m_start - m_end; + + return ret + 1; +} + +Item::Iterator::Ptr RangeIterator::toReversed() +{ + return Item::Iterator::Ptr(new RangeIterator(m_start, Backward, m_end)); +} + +Item RangeIterator::current() const +{ + return m_current; +} + +xsInteger RangeIterator::position() const +{ + return m_position; +} + +Item::Iterator::Ptr RangeIterator::copy() const +{ + if(m_direction == Backward) + return Item::Iterator::Ptr(new RangeIterator(m_end, Backward, m_start)); + else + return Item::Iterator::Ptr(new RangeIterator(m_start, Forward, m_end)); +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/iterators/qrangeiterator_p.h b/src/xmlpatterns/iterators/qrangeiterator_p.h new file mode 100644 index 0000000..0a0f294 --- /dev/null +++ b/src/xmlpatterns/iterators/qrangeiterator_p.h @@ -0,0 +1,141 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef Patternist_RangeIterator_H +#define Patternist_RangeIterator_H + +#include "qitem_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short RangeIterator represents a sequence of integers between a + * start and end value. + * + * The RangeIterator contains the evaluation logic for the range expression, <tt>N to M</tt>, + * and its behavior is therefore consistent with the definition of that XPath expression. + * Hence, the detailed behavior of the RangeIterator can be found in the XPath 2.0 + * specification. + * + * @see <a href="http://www.w3.org/TR/xpath20/\#doc-xpath-RangeExpr">XML Path Language + * (XPath) 2.0, 3.3 Sequence Expressions, RangeExpr</a> + * @see RangeExpression + * @author Frans Englich <fenglich@trolltech.com> + * @ingroup Patternist_iterators + * @todo Documentation is missing + */ + class Q_AUTOTEST_EXPORT RangeIterator : public Item::Iterator + { + public: + + /** + * RangeIterator can iterate in both directions. + * This enumerator exist for identifying different directions. + */ + enum Direction + { + /** + * Signifies that the QAbstractXmlForwardIterator operates in a reverse direction, where the + * first item returned by the next() function is from the beginning of the + * source sequence. + */ + Backward = 0, + + /** + * Signifies the forward direction. Iterators do conceptually operate + * in the forward direction by default. + */ + Forward = 1 + }; + + /** + * Creates an QAbstractXmlForwardIterator that returns integer values from consecutive sequence + * of integers between @p start and @p end, where the step taken + * between each integer is 1 with polarity as specified in @p direction. + * + * @note @p start must be smaller than @p end, not larger + * or equal. This is not checked. + */ + RangeIterator(const xsInteger start, + const Direction direction, + const xsInteger end); + + virtual Item next(); + virtual Item current() const; + virtual xsInteger position() const; + virtual xsInteger count(); + virtual Item::Iterator::Ptr toReversed(); + virtual Item::Iterator::Ptr copy() const; + + private: + xsInteger m_start; + xsInteger m_end; + Item m_current; + xsInteger m_position; + xsInteger m_count; + const Direction m_direction; + + /** + * We only need to store -1 or 1, so save memory with a bit field. + */ + const qint8 m_increment : 2; + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/iterators/qremovaliterator.cpp b/src/xmlpatterns/iterators/qremovaliterator.cpp new file mode 100644 index 0000000..eb27ce7 --- /dev/null +++ b/src/xmlpatterns/iterators/qremovaliterator.cpp @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include "qremovaliterator_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +RemovalIterator::RemovalIterator(const Item::Iterator::Ptr &target, + const xsInteger pos) : m_target(target), + m_removalPos(pos), + m_position(0) +{ + Q_ASSERT(target); + Q_ASSERT(pos >= 1); +} + +Item RemovalIterator::next() +{ + if(m_position == -1) + return Item(); + + m_current = m_target->next(); + + if(!m_current) + { + m_position = -1; + m_current.reset(); + return Item(); + } + + ++m_position; + + if(m_position == m_removalPos) + { + next(); /* Recurse, return the next item. */ + --m_position; /* Don't count the one we removed. */ + return m_current; + } + + return m_current; +} + +xsInteger RemovalIterator::count() +{ + const xsInteger itc = m_target->count(); + + if(itc < m_removalPos) + return itc; + else + return itc - 1; +} + +Item RemovalIterator::current() const +{ + return m_current; +} + +xsInteger RemovalIterator::position() const +{ + return m_position; +} + +Item::Iterator::Ptr RemovalIterator::copy() const +{ + return Item::Iterator::Ptr(new RemovalIterator(m_target->copy(), m_removalPos)); +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/iterators/qremovaliterator_p.h b/src/xmlpatterns/iterators/qremovaliterator_p.h new file mode 100644 index 0000000..e5f8ad3 --- /dev/null +++ b/src/xmlpatterns/iterators/qremovaliterator_p.h @@ -0,0 +1,122 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef Patternist_RemovalIterator_H +#define Patternist_RemovalIterator_H + +#include "qitem_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + + /** + * @short Removes one items at a specified position from an input QAbstractXmlForwardIterator. + * + * RemoveIterator removes an item from a sequence at a certain position, + * while retaining the pull-based characteristic of being an QAbstractXmlForwardIterator itself. The + * RemovalIterator's constructor is passed an QAbstractXmlForwardIterator, the QAbstractXmlForwardIterator to + * remove from, and the position of the item to remove. When calling the RemovalIterator's + * functions, it acts as an ordinary QAbstractXmlForwardIterator, taking into account that + * one item is removed from the source QAbstractXmlForwardIterator. + * + * The RemovalIterator class contains the central business logic for implementing the + * <tt>fn:remove()</tt> function, whose definition therefore specifies the detailed behaviors + * of RemovalIterator. + * + * @see <a href="http://www.w3.org/TR/xpath-functions/#func-remove">XQuery 1.0 + * and XPath 2.0 Functions and Operators, 15.1.8 fn:remove</a> + * @author Frans Englich <fenglich@trolltech.com> + * @ingroup Patternist_iterators + */ + class RemovalIterator : public Item::Iterator + { + public: + + /** + * Creates an RemovalIterator. + * + * @param target the QAbstractXmlForwardIterator containing the sequence of items + * which the item at position @p position should be removed from. + * @param position the position of the item to remove. Must be + * 1 or larger. + */ + RemovalIterator(const Item::Iterator::Ptr &target, + const xsInteger position); + + virtual Item next(); + virtual Item current() const; + virtual xsInteger position() const; + + /** + * The QAbstractXmlForwardIterator's count is computed by subtracting one from the source + * QAbstractXmlForwardIterator's count. + */ + virtual xsInteger count(); + + virtual Item::Iterator::Ptr copy() const; + + private: + const Item::Iterator::Ptr m_target; + const xsInteger m_removalPos; + Item m_current; + xsInteger m_position; + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/iterators/qsequencemappingiterator_p.h b/src/xmlpatterns/iterators/qsequencemappingiterator_p.h new file mode 100644 index 0000000..2e203fb --- /dev/null +++ b/src/xmlpatterns/iterators/qsequencemappingiterator_p.h @@ -0,0 +1,237 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef Patternist_SequenceMappingIterator_H +#define Patternist_SequenceMappingIterator_H + +#include "qabstractxmlforwarditerator_p.h" +#include "qdynamiccontext_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short Proxies another QAbstractXmlForwardIterator, and for each item, returns the + * Sequence returned from a mapping function. + * + * ItemMappingIterator is practical when the items in an QAbstractXmlForwardIterator needs to + * be translated to another sequence, while still doing it in a pipe-lined + * fashion. In contrast to ItemMappingIterator, SequenceMappingIterator maps + * each item into another QAbstractXmlForwardIterator, and where the SequenceMappingIterator's own + * result is the concatenation of all those Iterators. Hence, while ItemMappingIterator + * is better tailored for one-to-one or one-to-zero conversion, SequenceMappingIterator + * is more suitable for one-to-many conversion. + * + * This is achieved by that SequenceMappingIterator's constructor takes + * an instance of a class, that must have the following member: + * + * @code + * QAbstractXmlForwardIterator<TResult>::Ptr mapToSequence(const TSource::Ptr &item, + * const DynamicContext::Ptr &context) const; + * @endcode + * + * @author Frans Englich <fenglich@trolltech.com> + * @see ItemMappingIterator + * @ingroup Patternist_iterators + */ + template<typename TResult, typename TSource, typename TMapper> + class SequenceMappingIterator : public QAbstractXmlForwardIterator<TResult> + { + public: + /** + * Constructs a SequenceMappingIterator. + * + * @param mapper the object that has the mapToItem() sequence. + * @param sourceIterator the QAbstractXmlForwardIterator whose items should be mapped. + * @param context the DynamicContext that will be passed to the map function. + * May be null. + */ + SequenceMappingIterator(const TMapper &mapper, + const typename QAbstractXmlForwardIterator<TSource>::Ptr &sourceIterator, + const DynamicContext::Ptr &context); + + virtual TResult next(); + virtual xsInteger count(); + virtual TResult current() const; + virtual xsInteger position() const; + + /** + * The reason the implementation is placed in line here, is due to a bug + * in MSVC-2005 version 14.00.50727.762. Note that it works with version 14.00.50727.42. + */ + virtual typename QAbstractXmlForwardIterator<TResult>::Ptr copy() const + { + return typename QAbstractXmlForwardIterator<TResult>::Ptr + (new SequenceMappingIterator<TResult, TSource, TMapper>(m_mapper, + m_mainIterator->copy(), + m_context)); + } + + private: + xsInteger m_position; + TResult m_current; + typename QAbstractXmlForwardIterator<TSource>::Ptr m_mainIterator; + typename QAbstractXmlForwardIterator<TResult>::Ptr m_currentIterator; + const typename DynamicContext::Ptr m_context; + const TMapper m_mapper; + }; + + template<typename TResult, typename TSource, typename TMapper> + SequenceMappingIterator<TResult, TSource, TMapper>::SequenceMappingIterator( + const TMapper &mapper, + const typename QAbstractXmlForwardIterator<TSource>::Ptr &iterator, + const DynamicContext::Ptr &context) + : m_position(0), + m_mainIterator(iterator), + m_context(context), + m_mapper(mapper) + { + Q_ASSERT(mapper); + Q_ASSERT(iterator); + } + + template<typename TResult, typename TSource, typename TMapper> + TResult SequenceMappingIterator<TResult, TSource, TMapper>::next() + { + /* This was once implemented with a recursive function, but the stack + * got blown for some inputs by that approach. */ + while(true) + { + while(!m_currentIterator) + { + const TSource mainItem(m_mainIterator->next()); + + if(qIsForwardIteratorEnd(mainItem)) /* We've reached the very end. */ + { + m_position = -1; + m_current = TResult(); + return TResult(); + } + else + m_currentIterator = m_mapper->mapToSequence(mainItem, m_context); + } + + m_current = m_currentIterator->next(); + + if(qIsForwardIteratorEnd(m_current)) + { + m_currentIterator.reset(); + continue; + } + else + { + ++m_position; + return m_current; + } + } + } + + template<typename TResult, typename TSource, typename TMapper> + xsInteger SequenceMappingIterator<TResult, TSource, TMapper>::count() + { + TSource unit(m_mainIterator->next()); + xsInteger c = 0; + + while(!qIsForwardIteratorEnd(unit)) + { + const typename QAbstractXmlForwardIterator<TResult>::Ptr sit(m_mapper->mapToSequence(unit, m_context)); + c += sit->count(); + unit = m_mainIterator->next(); + } + + return c; + } + + template<typename TResult, typename TSource, typename TMapper> + TResult SequenceMappingIterator<TResult, TSource, TMapper>::current() const + { + return m_current; + } + + template<typename TResult, typename TSource, typename TMapper> + xsInteger SequenceMappingIterator<TResult, TSource, TMapper>::position() const + { + return m_position; + } + + + /** + * @short An object generator for SequenceMappingIterator. + * + * makeSequenceMappingIterator() is a convenience function for avoiding specifying + * the full template instantiation for SequenceMappingIterator. Conceptually, it + * is identical to Qt's qMakePair(). + * + * @returns a SequenceMappingIterator wrapped in a smart pointer, that has been + * passed the constructor arguments @p mapper, @p source, and @p context. + * @see makeMappingCallbackPtr() + * @relates QAbstractXmlForwardIterator + */ + template<typename TResult, typename TSource, typename TMapper> + static inline + typename QAbstractXmlForwardIterator<TResult>::Ptr + makeSequenceMappingIterator(const TMapper &mapper, + const QExplicitlySharedDataPointer<QAbstractXmlForwardIterator<TSource> > &source, + const DynamicContext::Ptr &context) + { + return typename QAbstractXmlForwardIterator<TResult>::Ptr + (new SequenceMappingIterator<TResult, TSource, TMapper>(mapper, source, context)); + } +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/iterators/qsingletoniterator_p.h b/src/xmlpatterns/iterators/qsingletoniterator_p.h new file mode 100644 index 0000000..d37f057 --- /dev/null +++ b/src/xmlpatterns/iterators/qsingletoniterator_p.h @@ -0,0 +1,177 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef Patternist_SingletonIterator_H +#define Patternist_SingletonIterator_H + +#include <QtXmlPatterns/private/qabstractxmlforwarditerator_p.h> + +#include <QtXmlPatterns/private/qprimitives_p.h> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short An QAbstractXmlForwardIterator over exactly one item. + * + * SingletonIterator's constructor takes one value which is + * the item it forms an QAbstractXmlForwardIterator over. Other QAbstractXmlForwardIterator instances can + * also form an QAbstractXmlForwardIterator with one in length, but by that SingletonIterator + * has this as it only task, it means it is efficient at it. + * + * Having to represent single items in Iterators is relatively common. + * + * @author Frans Englich <fenglich@trolltech.com> + * @ingroup Patternist_iterators + */ + template<typename T> + class SingletonIterator : public QAbstractXmlForwardIterator<T> + { + public: + /** + * Creates an iterator over @p item. + * + * @note item may not be @c null. Use the EmptyIterator for + * the empty sequence + */ + SingletonIterator(const T &item) : m_item(item), + m_position(0) + { + Q_ASSERT(!qIsForwardIteratorEnd(item)); + } + + virtual T next() + { + switch(m_position) + { + case 0: + { + ++m_position; + return m_item; + } + case 1: + { + m_position = -1; + return T(); + } + default: + { + Q_ASSERT(m_position == -1); + return T(); + } + } + } + + virtual T current() const + { + if(m_position == 1) + return m_item; + else + return T(); + } + + virtual xsInteger position() const + { + return m_position; + } + + /** + * @returns a copy of this instance, rewinded to the beginning. + */ + virtual typename QAbstractXmlForwardIterator<T>::Ptr toReversed() + { + return typename QAbstractXmlForwardIterator<T>::Ptr(new SingletonIterator<T>(m_item)); + } + + /** + * @returns always 1 + */ + virtual xsInteger count() + { + return 1; + } + + virtual typename QAbstractXmlForwardIterator<T>::Ptr copy() const + { + return typename QAbstractXmlForwardIterator<T>::Ptr(new SingletonIterator(m_item)); + } + + private: + const T m_item; + qint8 m_position; + }; + + /** + * @short An object generator for SingletonIterator. + * + * makeSingletonIterator() is a convenience function for avoiding specifying + * the full template instantiation for SingletonIterator. Conceptually, it + * is identical to Qt's qMakePair(). + * + * @relates SingletonIterator + */ + template<typename T> + inline + typename SingletonIterator<T>::Ptr + makeSingletonIterator(const T &item) + { + return typename SingletonIterator<T>::Ptr(new SingletonIterator<T>(item)); + } +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/iterators/qsubsequenceiterator.cpp b/src/xmlpatterns/iterators/qsubsequenceiterator.cpp new file mode 100644 index 0000000..4d9ee96 --- /dev/null +++ b/src/xmlpatterns/iterators/qsubsequenceiterator.cpp @@ -0,0 +1,110 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qxpathhelper_p.h" + +#include "qsubsequenceiterator_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +SubsequenceIterator::SubsequenceIterator(const Item::Iterator::Ptr &iterator, + const xsInteger start, + const xsInteger len) + : m_position(0), + m_it(iterator), + m_counter(start), + m_start(start), + m_len(len), + m_stop(m_start + m_len) +{ + Q_ASSERT(iterator); + Q_ASSERT(start >= 1); + Q_ASSERT(len == -1 || len >= 1); + + /* Note, "The first item of a sequence is located at position 1, not position 0." */ + for(xsInteger i = 1; i != m_start; ++i) + m_it->next(); +} + +Item SubsequenceIterator::next() +{ + if(m_position == -1) + return Item(); + + m_current = m_it->next(); + ++m_position; + + if(m_len == -1) + { + if(!m_current) + m_position = -1; + + return m_current; + } + + ++m_counter; + + if(!(m_counter > m_stop) && m_current) + return m_current; + + m_position = -1; + m_current.reset(); + return Item(); +} + +Item SubsequenceIterator::current() const +{ + return m_current; +} + +xsInteger SubsequenceIterator::position() const +{ + return m_position; +} + +Item::Iterator::Ptr SubsequenceIterator::copy() const +{ + return Item::Iterator::Ptr(new SubsequenceIterator(m_it->copy(), m_start, m_len)); +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/iterators/qsubsequenceiterator_p.h b/src/xmlpatterns/iterators/qsubsequenceiterator_p.h new file mode 100644 index 0000000..1217069 --- /dev/null +++ b/src/xmlpatterns/iterators/qsubsequenceiterator_p.h @@ -0,0 +1,118 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef Patternist_SubsequenceIterator_H +#define Patternist_SubsequenceIterator_H + +#include "qitem_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short Picks out a slice from another QAbstractXmlForwardIterator, specified by a start and end position. + * + * SubsequenceIterator allows a "slice", a subsequence, from an QAbstractXmlForwardIterator to + * be extracted. The SubsequenceIterator's constructor takes a source QAbstractXmlForwardIterator, + * a start position, and the length of the subsequence to be extracted. + * + * SubsequenceIterator contains the central business logic to implement + * the <tt>fn:subsequence()</tt> function. The detailed behavior, such as how it behaves + * if the source QAbstractXmlForwardIterator is empty or if the specified subsequence stretches + * beyond the source QAbstractXmlForwardIterator, is therefore consistent with the definition of + * the <tt>fn:subsequence()</tt> function. + * + * @see <a href="http://www.w3.org/TR/xpath-functions/#func-subsequence">XQuery 1.0 + * and XPath 2.0 Functions and Operators, 15.1.10 fn:subsequence</a> + * @author Frans Englich <fenglich@trolltech.com> + * @ingroup Patternist_iterators + */ + class SubsequenceIterator : public Item::Iterator + { + public: + /** + * Creates a SubsequenceIterator that extracts a subsequence from the sequence + * in @p iterator, as specified by the @p start position and @p length parameter. + * + * @param iterator the iterator which the subsequence should + * be extracted from + * @param start the start position of extraction. Must be 1 or larger. + * @param length the length of the subsequence to extract. If it is + * -1, to the end is returned. The value must be -1 or 1 or larger. + */ + SubsequenceIterator(const Item::Iterator::Ptr &iterator, + const xsInteger start, + const xsInteger length); + + virtual Item next(); + virtual Item current() const; + virtual xsInteger position() const; + virtual Item::Iterator::Ptr copy() const; + + private: + xsInteger m_position; + Item m_current; + const Item::Iterator::Ptr m_it; + xsInteger m_counter; + const xsInteger m_start; + const xsInteger m_len; + const xsInteger m_stop; + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/iterators/qtocodepointsiterator.cpp b/src/xmlpatterns/iterators/qtocodepointsiterator.cpp new file mode 100644 index 0000000..63d92ab --- /dev/null +++ b/src/xmlpatterns/iterators/qtocodepointsiterator.cpp @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qinteger_p.h" + +#include "qtocodepointsiterator_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +ToCodepointsIterator::ToCodepointsIterator(const QString &string) + : m_string(string), + m_len(string.length()), + m_position(0) +{ + Q_ASSERT(!string.isEmpty()); +} + +Item ToCodepointsIterator::next() +{ + if(m_position == -1) + return Item(); + + ++m_position; + if(m_position > m_len) + { + m_position = -1; + m_current.reset(); + return m_current; + } + + m_current = Integer::fromValue(m_string.at(m_position - 1).unicode()); + return m_current; +} + +xsInteger ToCodepointsIterator::count() +{ + return m_len; +} + +Item ToCodepointsIterator::current() const +{ + return m_current; +} + +xsInteger ToCodepointsIterator::position() const +{ + return m_position; +} + +Item::Iterator::Ptr ToCodepointsIterator::copy() const +{ + return Item::Iterator::Ptr(new ToCodepointsIterator(m_string)); +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/iterators/qtocodepointsiterator_p.h b/src/xmlpatterns/iterators/qtocodepointsiterator_p.h new file mode 100644 index 0000000..2c9f481 --- /dev/null +++ b/src/xmlpatterns/iterators/qtocodepointsiterator_p.h @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef Patternist_ToCodepointsIterator_H +#define Patternist_ToCodepointsIterator_H + +#include "qitem_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short Represents a stream of Unicode codepoints, which are computed from a string. + * + * ToCodepointsIterator takes in its constructor a string, whose Unicode characters' + * codepoints it forms an QAbstractXmlForwardIterator over. + * + * @see <a href="http://www.w3.org/TR/xpath-functions/#func-string-to-codepoints">XQuery 1.0 + * and XPath 2.0 Functions and Operators, 7.2.2 fn:string-to-codepoints</a> + * @see StringToCodepointsFN + * @author Frans Englich <fenglich@trolltech.com> + * @ingroup Patternist_iterators + */ + class ToCodepointsIterator : public Item::Iterator + { + public: + /** + * Constructs a ToCodepointsIterator. + * + * @param string the string to retrieve Unicode codepoints from. Can not be + * empty. + */ + ToCodepointsIterator(const QString &string); + virtual Item next(); + virtual Item current() const; + virtual xsInteger position() const; + virtual xsInteger count(); + virtual Item::Iterator::Ptr copy() const; + + private: + const QString m_string; + const int m_len; + Item m_current; + xsInteger m_position; + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/xmlpatterns/iterators/qunioniterator.cpp b/src/xmlpatterns/iterators/qunioniterator.cpp new file mode 100644 index 0000000..7a54a0d --- /dev/null +++ b/src/xmlpatterns/iterators/qunioniterator.cpp @@ -0,0 +1,131 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qitem_p.h" + +#include "qunioniterator_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QPatternist; + +UnionIterator::UnionIterator(const Item::Iterator::Ptr &it1, + const Item::Iterator::Ptr &it2) : m_it1(it1), + m_it2(it2), + m_position(0), + m_node1(m_it1->next()), + m_node2(m_it2->next()) +{ + Q_ASSERT(m_it1); + Q_ASSERT(m_it2); +} + +Item UnionIterator::next() +{ + ++m_position; + if(m_node1 && m_node2) + { + if(m_node1.asNode().model() != m_node2.asNode().model()) + { + m_current = m_node1; + m_node1 = m_it1->next(); + return m_current; + } + + switch(m_node1.asNode().compareOrder(m_node2.asNode())) + { + case QXmlNodeModelIndex::Precedes: + { + m_current = m_node1; + m_node1 = m_it1->next(); + return m_current; + } + case QXmlNodeModelIndex::Follows: + { + m_current = m_node2; + m_node2 = m_it2->next(); + return m_current; + } + default: + { + m_current = m_node2; + m_node1 = m_it1->next(); + m_node2 = m_it2->next(); + return m_current; + } + } + } + + if(m_node1) + { + m_current = m_node1; + m_node1 = m_it1->next(); + return m_current; + } + + if(m_node2) + { + m_current = m_node2; + m_node2 = m_it2->next(); + return m_current; + } + + m_current.reset(); + m_position = -1; + return Item(); +} + +Item UnionIterator::current() const +{ + return m_current; +} + +xsInteger UnionIterator::position() const +{ + return m_position; +} + +Item::Iterator::Ptr UnionIterator::copy() const +{ + return Item::Iterator::Ptr(new UnionIterator(m_it1->copy(), m_it2->copy())); +} + +QT_END_NAMESPACE diff --git a/src/xmlpatterns/iterators/qunioniterator_p.h b/src/xmlpatterns/iterators/qunioniterator_p.h new file mode 100644 index 0000000..6db0889 --- /dev/null +++ b/src/xmlpatterns/iterators/qunioniterator_p.h @@ -0,0 +1,107 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef Patternist_UnionIterator_H +#define Patternist_UnionIterator_H + +#include "qitem_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +namespace QPatternist +{ + /** + * @short Implements the @c except operator. That is, the computation + * of the sequence of nodes from one sequence, that doesn't appear in the + * other. + * + * @ingroup Patternist_iterators + */ + class UnionIterator : public Item::Iterator + { + public: + /** + * It is assumed that @p it1 and @p it2 are in document order and + * without duplicates. + */ + UnionIterator(const Item::Iterator::Ptr &it1, + const Item::Iterator::Ptr &it2); + + virtual Item next(); + virtual Item current() const; + virtual xsInteger position() const; + virtual Item::Iterator::Ptr copy() const; + + private: + inline Item nextFromFirstOperand() + { + ++m_position; + m_current = m_node1; + m_node1 = m_it1->next(); + return m_current; + } + + const Item::Iterator::Ptr m_it1; + const Item::Iterator::Ptr m_it2; + Item m_current; + xsInteger m_position; + Item m_node1; + Item m_node2; + }; +} + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif |