[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
[561] | 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
[2] | 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 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
[2] | 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 | *
|
---|
[561] | 78 | * @author Frans Englich <[email protected]>
|
---|
[2] | 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 | *
|
---|
[561] | 110 | * @author Frans Englich <[email protected]>
|
---|
[2] | 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.
|
---|
| |
---|