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 | //
|
---|
43 | // W A R N I N G
|
---|
44 | // -------------
|
---|
45 | //
|
---|
46 | // This file is not part of the Qt API. It exists purely as an
|
---|
47 | // implementation detail. This header file may change from version to
|
---|
48 | // version without notice, or even be removed.
|
---|
49 | //
|
---|
50 | // We mean it.
|
---|
51 |
|
---|
52 | #ifndef Patternist_XSLTTokenizer_H
|
---|
53 | #define Patternist_XSLTTokenizer_H
|
---|
54 |
|
---|
55 | #include <QQueue>
|
---|
56 | #include <QStack>
|
---|
57 | #include <QUrl>
|
---|
58 |
|
---|
59 | #include "qmaintainingreader_p.h"
|
---|
60 | #include "qreportcontext_p.h"
|
---|
61 | #include "qtokenizer_p.h"
|
---|
62 | #include "qxslttokenlookup_p.h"
|
---|
63 |
|
---|
64 | QT_BEGIN_HEADER
|
---|
65 |
|
---|
66 | QT_BEGIN_NAMESPACE
|
---|
67 |
|
---|
68 | namespace QPatternist
|
---|
69 | {
|
---|
70 | /**
|
---|
71 | * @short A TokenSource which contains one Tokenizer::Token.
|
---|
72 | *
|
---|
73 | * One possible way to optimize this is to let SingleTokenContainer
|
---|
74 | * actually contain a list of tokens, such that XSLTTokenizer::queueToken()
|
---|
75 | * could append to that, instead of instansiating a SingleTokenContainer
|
---|
76 | * all the time.
|
---|
77 | *
|
---|
78 | * @author Frans Englich <[email protected]>
|
---|
79 | */
|
---|
80 | class SingleTokenContainer : public TokenSource
|
---|
81 | {
|
---|
82 | public:
|
---|
83 | inline SingleTokenContainer(const Tokenizer::Token &token,
|
---|
84 | const YYLTYPE &location);
|
---|
85 |
|
---|
86 | virtual Tokenizer::Token nextToken(YYLTYPE *const sourceLocator);
|
---|
87 | private:
|
---|
88 | const Tokenizer::Token m_token;
|
---|
89 | const YYLTYPE m_location;
|
---|
90 | bool m_hasDelivered;
|
---|
91 | };
|
---|
92 |
|
---|
93 | SingleTokenContainer::SingleTokenContainer(const Tokenizer::Token &token,
|
---|
94 | const YYLTYPE &location) : m_token(token)
|
---|
95 | , m_location(location)
|
---|
96 | , m_hasDelivered(false)
|
---|
97 | {
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * @short Tokenizes XSL-T 2.0 documents.
|
---|
102 | *
|
---|
103 | * XSLTTokenizer takes in its constructor a pointer to a QIODevice which is
|
---|
104 | * supposed to contain an XSL-T document. XSLTTokenizer then rewrites that
|
---|
105 | * document into XQuery tokens delivered via nextToken(), which the regular
|
---|
106 | * XQuery parser then reads. Hence, the XSL-T language is rewritten into
|
---|
107 | * XQuery code, slightly extended to handle the featuress specific to
|
---|
108 | * XSL-T.
|
---|
109 | *
|
---|
110 | * @author Frans Englich <[email protected]>
|
---|
111 | */
|
---|
112 | class XSLTTokenizer : public Tokenizer
|
---|
113 | , private MaintainingReader<XSLTTokenLookup>
|
---|
114 | {
|
---|
115 | public:
|
---|
116 | /**
|
---|
117 | * XSLTTokenizer do not own @p queryDevice.
|
---|
118 | */
|
---|
119 | XSLTTokenizer(QIODevice *const queryDevice,
|
---|
120 | const QUrl &location,
|
---|
121 | const ReportContext::Ptr &context,
|
---|
122 | const NamePool::Ptr &np);
|
---|
123 |
|
---|
124 | virtual Token nextToken(YYLTYPE *const sourceLocator);
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * For XSLT we don't need this mechanism, so we do nothing.
|
---|
128 | */
|
---|
129 | virtual int commenceScanOnly();
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * For XSLT we don't need this mechanism, so we do nothing.
|
---|
133 | */
|
---|
134 | virtual void resumeTokenizationFrom(const int position);
|
---|
135 |
|
---|
136 | virtual void setParserContext(const ParserContext::Ptr &parseInfo);
|
---|
137 |
|
---|
138 | virtual QUrl documentURI() const
|
---|
139 | {
|
---|
140 | return queryURI();
|
---|
141 | }
|
---|
142 |
|
---|
143 | protected:
|
---|
144 | virtual bool isAnyAttributeAllowed() const;
|
---|
145 |
|
---|
146 | private:
|
---|
147 | inline void validateElement() const;
|
---|
148 |
|
---|
149 | YYLTYPE currentSourceLocator() const;
|
---|
150 |
|
---|
151 | enum State
|
---|
152 | {
|
---|
153 | OutsideDocumentElement,
|
---|
154 | InsideStylesheetModule,
|
---|
155 | InsideSequenceConstructor
|
---|
156 | };
|
---|
157 |
|
---|
158 | enum VariableType
|
---|
159 | {
|
---|
160 | FunctionParameter,
|
---|
161 | GlobalParameter,
|
---|
162 | TemplateParameter,
|
---|
163 | VariableDeclaration,
|
---|
164 | VariableInstruction,
|
---|
165 | WithParamVariable
|
---|
166 | };
|
---|
167 |
|
---|
168 | void queueNamespaceDeclarations(TokenSource::Queue *const ts,
|
---|
169 | QStack<Token> *const target,
|
---|
170 | const bool isDeclaration = false);
|
---|
171 |
|
---|
172 | inline void queueToken(const Token &token,
|
---|
173 | TokenSource::Queue *const ts);
|
---|
174 | void queueEmptySequence(TokenSource::Queue *const to);
|
---|
175 | void queueSequenceType(const QString &expr);
|
---|
176 | /**
|
---|
177 | * If @p emptynessAllowed is @c true, the @c select attribute may
|
---|
178 | * be empty while there also is no sequence constructor.
|
---|
179 | */
|
---|
180 | void queueSimpleContentConstructor(const ReportContext::ErrorCode code,
|
---|
181 | const bool emptynessAllowed,
|
---|
182 | TokenSource::Queue *const to,
|
---|
183 | const bool selectOnlyFirst = false);
|
---|
184 | /**
|
---|
185 | * Tokenizes and queues @p expr as if it was an attribute value
|
---|
186 | * template.
|
---|
187 | */
|
---|
188 | void queueAVT(const QString &expr,
|
---|
189 | TokenSource::Queue *const to);
|
---|
190 |
|
---|
191 | void hasWrittenExpression(bool &beacon);
|
---|
192 | void commencingExpression(bool &hasWrittenExpression,
|
---|
193 | TokenSource::Queue *const to);
|
---|
194 |
|
---|
195 | void outsideDocumentElement();
|
---|
196 | void insideChoose(TokenSource::Queue *const to);
|
---|
197 | void insideFunction();
|
---|
198 |
|
---|
199 | bool attributeYesNo(const QString &localName) const;
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Scans/skips @c xsl:fallback elements only. This is the case of the
|
---|
203 | * children of @c xsl:sequence, for instance.
|
---|
204 | */
|
---|
205 | void parseFallbacksOnly();
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Returns true if the current element is either @c stylesheet
|
---|
209 | * or the synonym @c transform.
|
---|
210 | *
|
---|
211 | * This function assumes that m_reader is positioned at an element
|
---|
212 | * and that the namespace is XSL-T.
|
---|
213 | */
|
---|
214 | bool isStylesheetElement() const;
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Returns true if the current element name is @p name.
|
---|
218 | *
|
---|
219 | * It is assumed that the namespace is XSL-T and that the current
|
---|
220 | * state in m_reader is either QXmlStreamReader::StartElement or
|
---|
221 | * QXmlStreamReader::EndElement.
|
---|
222 | */
|
---|
223 | bool isElement(const NodeName &name) const;
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Queues a text constructor for @p chars, if @p chars is
|
---|
227 | * not empty.
|
---|
228 | */
|
---|
229 | void queueTextConstructor(QString &chars,
|
---|
230 | bool &hasWrittenExpression,
|
---|
231 | TokenSource::Queue *const to);
|
---|
232 |
|
---|
233 | /**
|
---|
234 | *
|
---|
235 | * @see <a href="http://www.w3.org/TR/xslt20/#stylesheet-structure">XSL
|
---|
236 | * Transformations (XSLT) Version 2, 3.6 Stylesheet Element</a>
|
---|
237 | */
|
---|
238 | void insideStylesheetModule();
|
---|
239 | void insideTemplate();
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Takes @p expr for an XPath expression, and pushes the necessary
|
---|
243 | * things for having it delivered as a stream of token, appropriate
|
---|
244 | * for Effective Boolean Value parsing.
|
---|
245 | */
|
---|
246 | void queueExpression(const QString &expr,
|
---|
247 | TokenSource::Queue *const to,
|
---|
248 | const bool wrapWithParantheses = true);
|
---|
249 |
|
---|
250 | void skipBodyOfParam(const ReportContext::ErrorCode code);
|
---|
251 |
|
---|
252 | void queueParams(const NodeName parentName,
|
---|
253 | TokenSource::Queue *const to);
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Used for @c xsl:apply-templates and @c xsl:call-templates.
|
---|
257 | */
|
---|
258 | void queueWithParams(const NodeName parentName,
|
---|
259 | TokenSource::Queue *const to,
|
---|
260 | const bool initialAdvance = true);
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Queues an @c xsl:variable declaration. If @p isInstruction is @c
|
---|
264 | * true, it is assumed to be a an instruction, otherwise a top-level
|
---|
265 | * declaration element.
|
---|
266 | */
|
---|
267 | void queueVariableDeclaration(const VariableType variableType,
|
---|
268 | TokenSource::Queue *const to);
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Skips the current sub-tree.
|
---|
272 | *
|
---|
273 | * If text nodes that aren't strippable whitespace, or elements are
|
---|
274 | * encountered, @c true is returned, otherwise @c false.
|
---|
275 | *
|
---|
276 | * If @p exitOnContent is @c true, this function exits immediately
|
---|
277 | * if content is encountered for which it would return @c false.
|
---|
278 | */
|
---|
279 | bool skipSubTree(const bool exitOnContent = false);
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Queues the necessary tokens for the expression that is either
|
---|
283 | * supplied using a @c select attribute or a sequence constructor,
|
---|
284 | * while doing the necessary error handling for ensuring they are
|
---|
285 | * mutually exclusive.
|
---|
286 | *
|
---|
287 | * It is assumed that the current state of m_reader is
|
---|
288 | * QXmlStreamReader::StartElement, or that the attributes for the
|
---|
289 | * element is supplied through @p atts. This function advances m_reader
|
---|
290 | * up until the corresponding QXmlStreamReader::EndElement.
|
---|
291 | *
|
---|
292 | * If @p emptynessAllowed is @c false, the element must either have a
|
---|
293 | * sequence constructor or a @c select attribute. If @c true, both may
|
---|
294 | * be absent.
|
---|
295 | *
|
---|
296 | * Returns @c true if the queued expression was supplied through the
|
---|
297 | * @c select attribute otherwise @c false.
|
---|
298 | */
|
---|
299 | bool queueSelectOrSequenceConstructor(const ReportContext::ErrorCode code,
|
---|
300 | const bool emptynessAllowed,
|
---|
301 | TokenSource::Queue *const to,
|
---|
302 | const QXmlStreamAttributes *const atts = 0,
|
---|
303 | const bool queueEmptyOnEmpty = true);
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * If @p initialAdvance is @c true, insideSequenceConstructor() will
|
---|
307 | * advance m_reader, otherwise it won't. Not doing so is useful
|
---|
308 | * when the caller is already inside a sequence constructor.
|
---|
309 | *
|
---|
310 | * Returns @c true if a sequence constructor was found and queued.
|
---|
311 | * Returns @c false if none was found, and the empty sequence was
|
---|
312 | * synthesized.
|
---|
313 | */
|
---|
314 | bool insideSequenceConstructor(TokenSource::Queue *const to,
|
---|
315 | const bool initialAdvance = true,
|
---|
316 | const bool queueEmptyOnEmpty = true);
|
---|
317 |
|
---|
318 | bool insideSequenceConstructor(TokenSource::Queue *const to,
|
---|
319 | QStack<Token> &queueOnExit,
|
---|
320 | const bool initialAdvance = true,
|
---|
321 | const bool queueEmptyOnEmpty = true);
|
---|
322 |
|
---|
323 | void insideAttributeSet();
|
---|
324 | void pushState(const State nextState);
|
---|
325 | void leaveState();
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * @short Handles @c xml:space and standard attributes.
|
---|
329 | *
|
---|
330 | * If @p isXSLTElement is @c true, the current element is an XSL-T
|
---|
331 | * element, as opposed to a Literal Result Element.
|
---|
332 | *
|
---|
333 | * handleStandardAttributes() must be called before validateElement(),
|
---|
|
---|