source: trunk/src/xmlpatterns/functions/qsequencegeneratingfns.cpp@ 219

Last change on this file since 219 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 9.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
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 [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QStack>
43#include <QStringList>
44
45#include "qanyuri_p.h"
46#include "qboolean_p.h"
47#include "qcommonsequencetypes_p.h"
48#include "qcommonvalues_p.h"
49#include "qemptysequence_p.h"
50#include "qitemmappingiterator_p.h"
51#include "qnodesort_p.h"
52#include "qpatternistlocale_p.h"
53#include "private/qxmlutils_p.h"
54
55#include "qsequencegeneratingfns_p.h"
56
57QT_BEGIN_NAMESPACE
58
59using namespace QPatternist;
60
61IdFN::IdFN() : m_hasCreatedSorter(false)
62{
63}
64
65Item IdFN::mapToItem(const QString &id,
66 const IDContext &context) const
67{
68 return context.second->elementById(context.first->namePool()->allocateQName(QString(), id));
69}
70
71/**
72 * @short Helper class for StringSplitter
73 *
74 * Needed by the QAbstractXmlForwardIterator sub-class.
75 *
76 * @relates StringSplitter
77 */
78template<>
79bool qIsForwardIteratorEnd(const QString &unit)
80{
81 return unit.isNull();
82}
83
84/**
85 * @short Helper class for IdFN.
86 *
87 * StringSplitter takes an Iterator which delivers strings of this kind:
88 *
89 * "a", "b c", "%invalidNCName", " ", "d"
90 *
91 * and we deliver instead:
92 *
93 * "a", "b", "c", "d"
94 *
95 * That is, we:
96 * - Remove invalid @c NCName
97 * - Split IDREFs into individual NCNames
98 *
99 * @author Frans Englich
100 */
101class StringSplitter : public QAbstractXmlForwardIterator<QString>
102{
103public:
104 StringSplitter(const Item::Iterator::Ptr &source);
105 virtual QString next();
106 virtual QString current() const;
107 virtual qint64 position() const;
108private:
109 QString loadNext();
110 const Item::Iterator::Ptr m_source;
111 QStack<QString> m_buffer;
112 QString m_current;
113 qint64 m_position;
114 bool m_sourceAtEnd;
115};
116
117StringSplitter::StringSplitter(const Item::Iterator::Ptr &source) : m_source(source)
118 , m_position(0)
119 , m_sourceAtEnd(false)
120{
121 Q_ASSERT(m_source);
122 m_buffer.push(loadNext());
123}
124
125QString StringSplitter::next()
126{
127 /* We also check m_position, we want to load on our first run. */
128 if(!m_buffer.isEmpty())
129 {
130 ++m_position;
131 m_current = m_buffer.pop();
132 return m_current;
133 }
134 else if(m_sourceAtEnd)
135 {
136 m_current.clear();
137 m_position = -1;
138 return QString();
139 }
140
141 return loadNext();
142}
143
144QString StringSplitter::loadNext()
145{
146 const Item sourceNext(m_source->next());
147
148 if(sourceNext.isNull())
149 {
150 m_sourceAtEnd = true;
151 /* We might have strings in m_buffer, let's empty it. */
152 return next();
153 }
154
155 const QStringList candidates(sourceNext.stringValue().simplified().split(QLatin1Char(' ')));
156 const int count = candidates.length();
157
158 for(int i = 0; i < count; ++i)
159 {
160 const QString &at = candidates.at(i);
161
162 if(QXmlUtils::isNCName(at))
163 m_buffer.push(at);
164 }