| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "qxmlresultitems.h" | 
|---|
| 43 | #include "qxmlresultitems_p.h" | 
|---|
| 44 | #include "qitem_p.h" | 
|---|
| 45 |  | 
|---|
| 46 | QT_BEGIN_NAMESPACE | 
|---|
| 47 |  | 
|---|
| 48 | /*! | 
|---|
| 49 | \class QXmlResultItems | 
|---|
| 50 | \brief The QXmlResultItems class iterates through the results of evaluating an XQuery in QXmlQuery. | 
|---|
| 51 | \reentrant | 
|---|
| 52 | \since 4.4 | 
|---|
| 53 | \ingroup xml-tools | 
|---|
| 54 |  | 
|---|
| 55 | QXmlResultItems presents the evaluation of an associated query as a | 
|---|
| 56 | sequence of \l{QXmlItem}{QXmlItems}. The sequence is traversed by | 
|---|
| 57 | repeatedly calling next(), which actually produces the sequence by | 
|---|
| 58 | lazy evaluation of the query. | 
|---|
| 59 |  | 
|---|
| 60 | \snippet doc/src/snippets/code/src_xmlpatterns_api_qxmlresultitems.cpp 0 | 
|---|
| 61 |  | 
|---|
| 62 | An effect of letting next() produce the sequence by lazy evaluation | 
|---|
| 63 | is that a query error can occur on any call to next(). If an error | 
|---|
| 64 | occurs, both next() and current() will return the null QXmlItem, and | 
|---|
| 65 | hasError() will return true. | 
|---|
| 66 |  | 
|---|
| 67 | QXmlResultItems can be thought of as an "iterator" that traverses | 
|---|
| 68 | the sequence of query results once, in the forward direction. Each | 
|---|
| 69 | call to next() advances the iterator to the next QXmlItem in the | 
|---|
| 70 | sequence and returns it, and current() always returns the QXmlItem | 
|---|
| 71 | that next() returned the last time it was called. | 
|---|
| 72 |  | 
|---|
| 73 | \sa QXmlItem::isNode(), QXmlItem::isAtomicValue(), QXmlNodeModelIndex | 
|---|
| 74 | */ | 
|---|
| 75 |  | 
|---|
| 76 | /*! | 
|---|
| 77 | Constructs an instance of QXmlResultItems. | 
|---|
| 78 | */ | 
|---|
| 79 | QXmlResultItems::QXmlResultItems() : d_ptr(new QXmlResultItemsPrivate()) | 
|---|
| 80 | { | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | /*! | 
|---|
| 84 | Destroys this instance of QXmlResultItems. | 
|---|
| 85 | */ | 
|---|
| 86 | QXmlResultItems::~QXmlResultItems() | 
|---|
| 87 | { | 
|---|
| 88 | delete d_ptr; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | /*! | 
|---|
| 92 | Returns the next result in the sequence produced by lazy evaluation | 
|---|
| 93 | of the associated query. When the returned QXmlItem is null, either | 
|---|
| 94 | the evaluation terminated normally without producing another result, | 
|---|
| 95 | or an error occurred. Call hasError() to determine whether the null | 
|---|
| 96 | item was caused by normal termination or by an error. | 
|---|
| 97 |  | 
|---|
| 98 | Returns a null QXmlItem if there is no associated QXmlQuery. | 
|---|
| 99 | */ | 
|---|
| 100 | QXmlItem QXmlResultItems::next() | 
|---|
| 101 | { | 
|---|
| 102 | Q_D(QXmlResultItems); | 
|---|
| 103 | if(d->hasError) | 
|---|
| 104 | return QXmlItem(); | 
|---|
| 105 |  | 
|---|
| 106 | try | 
|---|
| 107 | { | 
|---|
| 108 | d->current = QPatternist::Item::toPublic(d->iterator->next()); | 
|---|
| 109 | return d->current; | 
|---|
| 110 | } | 
|---|
| 111 | catch(const QPatternist::Exception) | 
|---|
| 112 | { | 
|---|
| 113 | d->current = QXmlItem(); | 
|---|
| 114 | d->hasError = true; | 
|---|
| 115 | return QXmlItem(); | 
|---|
| 116 | } | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | /*! | 
|---|
| 120 | Returns the current item. The current item is the last item | 
|---|
| 121 | that was produced and returned by next(). | 
|---|
| 122 |  | 
|---|
| 123 | Returns a null QXmlItem if there is no associated QXmlQuery. | 
|---|
| 124 | */ | 
|---|
| 125 | QXmlItem QXmlResultItems::current() const | 
|---|
| 126 | { | 
|---|
| 127 | Q_D(const QXmlResultItems); | 
|---|
| 128 |  | 
|---|
| 129 | if(d->hasError) | 
|---|
| 130 | return QXmlItem(); | 
|---|
| 131 | else | 
|---|
| 132 | return d->current; | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | /*! | 
|---|
| 136 |  | 
|---|
| 137 | If an error occurred during evaluation of the query, true is | 
|---|
| 138 | returned. | 
|---|
| 139 |  | 
|---|
| 140 | Returns false if query evaluation has been done. | 
|---|
| 141 | */ | 
|---|
| 142 | bool QXmlResultItems::hasError() const | 
|---|
| 143 | { | 
|---|
| 144 | Q_D(const QXmlResultItems); | 
|---|
| 145 | return d->hasError; | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | QT_END_NAMESPACE | 
|---|
| 149 |  | 
|---|