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 <QString>
|
---|
43 |
|
---|
44 | #include "qitem_p.h"
|
---|
45 |
|
---|
46 | #include "qabstractxmlreceiver_p.h"
|
---|
47 | #include "qabstractxmlreceiver.h"
|
---|
48 |
|
---|
49 | QT_BEGIN_NAMESPACE
|
---|
50 |
|
---|
51 | /*!
|
---|
52 | \class QAbstractXmlReceiver
|
---|
53 | \brief The QAbstractXmlReceiver class provides a callback interface
|
---|
54 | for transforming the output of a QXmlQuery.
|
---|
55 | \reentrant
|
---|
56 | \since 4.4
|
---|
57 | \ingroup xml-tools
|
---|
58 |
|
---|
59 | QAbstractXmlReceiver is an abstract base class that provides
|
---|
60 | a callback interface for receiving an \l {XQuery Sequence}
|
---|
61 | {XQuery sequence}, usually the output of an QXmlQuery, and
|
---|
62 | transforming that sequence into a structure of your choosing,
|
---|
63 | usually XML. Consider the example:
|
---|
64 |
|
---|
65 | \snippet doc/src/snippets/code/src_xmlpatterns_api_qabstractxmlreceiver.cpp 0
|
---|
66 |
|
---|
67 | First it constructs a \l {QXmlQuery} {query} that gets the
|
---|
68 | first paragraph from document \c index.html. Then it constructs
|
---|
69 | an \l {QXmlSerializer} {XML serializer} with the \l {QXmlQuery}
|
---|
70 | {query} and \l {QIODevice} {myOutputDevice} (Note the
|
---|
71 | \l {QXmlSerializer} {serializer} is an \e {XML receiver},
|
---|
72 | ie a subclass of QAbstractXmlReceiver). Finally, it
|
---|
73 | \l {QXmlQuery::evaluateTo()} {evaluates} the
|
---|
74 | \l {QXmlQuery} {query}, producing an ordered sequence of calls
|
---|
75 | to the \l {QXmlSerializer} {serializer's} callback functions.
|
---|
76 | The sequence of callbacks transforms the query output to XML
|
---|
77 | and writes it to \l {QIODevice} {myOutputDevice}.
|
---|
78 |
|
---|
79 | Although the example uses \l {QXmlQuery} to produce the sequence
|
---|
80 | of callbacks to functions in QAbstractXmlReceiver, you can call
|
---|
81 | the callback functions directly as long as your sequence of
|
---|
82 | calls represents a valid \l {XQuery Sequence} {XQuery sequence}.
|
---|
83 |
|
---|
84 | \target XQuery Sequence
|
---|
85 | \section1 XQuery Sequences
|
---|
86 |
|
---|
87 | An XQuery \a sequence is an ordered collection of zero, one,
|
---|
88 | or many \e items. Each \e item is either an \e {atomic value}
|
---|
89 | or a \e {node}. An \e {atomic value} is a simple data value.
|
---|
90 |
|
---|
91 | There are six kinds of \e nodes.
|
---|
92 |
|
---|
93 | \list
|
---|
94 |
|
---|
95 | \o An \e {Element Node} represents an XML element.
|
---|
96 |
|
---|
97 | \o An \e {Attribute Node} represents an XML attribute.
|
---|
98 |
|
---|
99 | \o A \e {Document Node} represents an entire XML document.
|
---|
100 |
|
---|
101 | \o A \e {Text Node} represents character data (element content).
|
---|
102 |
|
---|
103 | \o A \e {Processing Instruction Node} represents an XML
|
---|
104 | processing instruction, which is used in an XML document
|
---|
105 | to tell the application reading the document to perform
|
---|
106 | some action. A typical example is to use a processing
|
---|
107 | instruction to tell the application to use a particular
|
---|
108 | XSLT stylesheet to display the document.
|
---|
109 |
|
---|
110 | \o And a \e {Comment node} represents an XML comment.
|
---|
111 |
|
---|
112 | \endlist
|
---|
113 |
|
---|
114 | The \e sequence of \e nodes and \e {atomic values} obeys
|
---|
115 | the following rules. Note that \e {Namespace Node} refers
|
---|
116 | to a special \e {Attribute Node} with name \e {xmlns}.
|
---|
117 |
|
---|
118 | \list
|
---|
119 |
|
---|
120 | \o Each \e node appears in the \e sequence before its children
|
---|
121 | and their descendants appear.
|
---|
122 |
|
---|
123 | \o A \e node's descendants appear in the \e sequence before
|
---|
124 | any of its siblings appear.
|
---|
125 |
|
---|
126 | \o A \e {Document Node} represents an entire document. Zero or
|
---|
127 | more \e {Document Nodes} can appear in a \e sequence, but they
|
---|
128 | can only be top level items (i.e., a \e {Document Node} can't
|
---|
129 | be a child of another \e node.
|
---|
130 |
|
---|
131 | \o \e {Namespace Nodes} immediately follow the \e {Element Node}
|
---|
132 | with which they are associated.
|
---|
133 |
|
---|
134 | \o \e {Attribute Nodes} immediately follow the \e {Namespace Nodes}
|
---|
135 | of the element with which they are associated, or...
|
---|
136 |
|
---|
137 | \o If there are no \e {Namespace Nodes} following an element, then
|
---|
138 | the \e {Attribute Nodes} immediately follow the element.
|
---|
139 |
|
---|
140 | \o An \e {atomic value} can only appear as a top level \e item,
|
---|
141 | i.e., it can't appear as a child of a \e node.
|
---|
142 |
|
---|
143 | \o \e {Processing Instruction Nodes} do not have children, and
|
---|
144 | their parent is either a \e {Document Node} or an \e {Element
|
---|
145 | Node}.
|
---|
146 |
|
---|
147 | \o \e {Comment Nodes} do not have children, and
|
---|
148 | their parent is either a \e {Document Node} or an \e {Element
|
---|
149 | Node}.
|
---|
150 |
|
---|
151 | \endlist
|
---|
152 |
|
---|
153 | The \e sequence of \e nodes and \e {atomic values} is sent to
|
---|
154 | an QAbstractXmlReceiver (QXmlSerializer in
|
---|
155 | the example above) as a sequence of calls to the receiver's
|
---|
156 | callback functions. The mapping of callback functions to
|
---|
157 | sequence items is as follows.
|
---|
158 |
|
---|
159 | \list
|
---|
160 |
|
---|
161 | \o startDocument() and endDocument() are called for each
|
---|
162 | \e {Document Node} in the \e sequence. endDocument() is not
|
---|
163 | called until all the \e {Document Node's} children have
|
---|
164 | appeared in the \e sequence.
|
---|
165 |
|
---|
166 | \o startElement() and endElement() are called for each
|
---|
167 | \e {Element Node}. endElement() is not called until all the
|
---|
168 | \e {Element Node's} children have appeared in the \e sequence.
|
---|
169 |
|
---|
170 | \o attribute() is called for each \e {Attribute Node}.
|
---|
171 |
|
---|
172 | \o comment() is called for each \e {Comment Node}.
|
---|
173 |
|
---|
174 | \o characters() is called for each \e {Text Node}.
|
---|
175 |
|
---|
176 | \o processingInstruction() is called for each \e {Processing
|
---|
177 | Instruction Node}.
|
---|
178 |
|
---|
179 | \o namespaceBinding() is called for each \e {Namespace Node}.
|
---|
180 |
|
---|
181 | \o atomicValue() is called for each \e {atomic value}.
|
---|
182 |
|
---|
183 | \endlist
|
---|
184 |
|
---|
185 | For a complete explanation of XQuery sequences, visit
|
---|
186 | \l {http://www.w3.org/TR/xpath-datamodel/}{XQuery Data Model}.
|
---|
187 |
|
---|
188 | \sa {http://www.w3.org/TR/xpath-datamodel/}{W3C XQuery 1.0 and XPath 2.0 Data Model (XDM)}
|
---|
189 | \sa QXmlSerializer
|
---|
190 | \sa QXmlResultItems
|
---|
191 | */
|
---|
192 |
|
---|
193 | template<const QXmlNodeModelIndex::Axis axis>
|
---|
194 | void QAbstractXmlReceiver::sendFromAxis(const QXmlNodeModelIndex &node)
|
---|
195 | {
|
---|
196 | Q_ASSERT(!node.isNull());
|
---|
197 | const QXmlNodeModelIndex::Iterator::Ptr it(node.iterate(axis));
|
---|
198 | QXmlNodeModelIndex next(it->next());
|
---|
199 |
|
---|
200 | while(!next.isNull())
|
---|
201 | {
|
---|
202 | sendAsNode(next);
|
---|
203 | next = it->next();
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | /*!
|
---|
208 | \internal
|
---|
209 | */
|
---|
210 | QAbstractXmlReceiver::QAbstractXmlReceiver(QAbstractXmlReceiverPrivate *d)
|
---|
211 | : d_ptr(d)
|
---|
212 | {
|
---|
213 | }
|
---|
214 |
|
---|
215 | /*!
|
---|
216 | Constructs an abstract xml receiver.
|
---|
217 | */
|
---|
218 | QAbstractXmlReceiver::QAbstractXmlReceiver() : d_ptr(0)
|
---|
219 | {
|
---|
220 | }
|
---|
221 |
|
---|
222 | /*!
|
---|
223 | Destroys the xml receiver.
|
---|
224 | */
|
---|
225 | QAbstractXmlReceiver::~QAbstractXmlReceiver()
|
---|
|
---|