summaryrefslogtreecommitdiff
path: root/src/xmlpatterns/parser/qmaintainingreader_p.h
blob: a4cdf77decd2c26a8cd582a02591c83da938ec28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtXmlPatterns module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.

#ifndef Patternist_MaintainingReader_H
#define Patternist_MaintainingReader_H

#include <QSet>
#include <QSourceLocation>
#include <QStack>
#include <QStringList>
#include <QXmlStreamReader>

#include <private/qxpathhelper_p.h>
#include <private/qxslttokenlookup_p.h>

QT_BEGIN_HEADER
QT_BEGIN_NAMESPACE

class QUrl;

namespace QPatternist
{
    /**
     * @short A structure that lists the optional and required
     * attributes of an element. Used with MaintainingReader.
     *
     * A constant source to misunderstandings is mixing up the order of
     * arguments for functions that takes a local name and a namespace. Be wary
     * of this.
     *
     * @author Frans Englich <frans.englich@nokia.com>
     * @since 4.5
     */
    template<typename TokenLookupClass,
             typename LookupKey = typename TokenLookupClass::NodeName>
    class ElementDescription
    {
    public:
        typedef QHash<LookupKey, ElementDescription<TokenLookupClass, LookupKey> > Hash;
        QSet<typename TokenLookupClass::NodeName> requiredAttributes;
        QSet<typename TokenLookupClass::NodeName> optionalAttributes;
    };

    /**
     * @short Base class for tokenizers that reads XML formats. This is
     * XSLTTokenizer, and the W3C XML Schema parser.
     *
     * MaintainingReader is intended for sub-classing.
     *
     * @tparam TokenLookupClass The name of the class that is generated by
     * QTokenAutomaton and which supplies tokenizing tokens. For XSLTTokenizer,
     * this is XSLTTokenLookup, for instance.
     *
     * @tparam LookupKey The type that is passed to validateElement() and is
     * the key in ElementDescription. For the schema code, where elements have
     * different interpretations depending on context, the lookup key is hence
     * not equal element name.
     *
     * @author Frans Englich <frans.englich@nokia.com>
     * @since 4.5
     */
    template<typename TokenLookupClass,
             typename LookupKey = typename TokenLookupClass::NodeName>
    class MaintainingReader : public QXmlStreamReader
                            , protected TokenLookupClass
    {
    protected:

        MaintainingReader(const typename ElementDescription<TokenLookupClass, LookupKey>::Hash &elementDescriptions,
                          const QSet<typename TokenLookupClass::NodeName> &standardAttributes,
                          const ReportContext::Ptr &context,
                          QIODevice *const queryDevice);

        virtual ~MaintainingReader();

        TokenType readNext();

        /**
         * Returns the name of the current element.
         */
        inline typename TokenLookupClass::NodeName currentElementName() const;

        /**
         * @short Convenience function for calling ReportContext::error().
         */
        void error(const QString &message,
                   const ReportContext::ErrorCode code) const;

        /**
         * @short Convenience function for calling ReportContext::warning().
         */
        void warning(const QString &message) const;

        /**
         * @short Returns the location of the document that MaintainingReader
         * is parsing. Used for error reporting
         */
        virtual QUrl documentURI() const = 0;

        /**
         * @short Returns @c true, if any attribute is allowed on the
         * element currently being validated.
         */
        virtual bool isAnyAttributeAllowed() const = 0;

        /**
         * QXmlStreamReader::isWhitespace() returns true for whitespace that is
         * not expressed as character references, while XSL-T operatates ontop
         * of the XDM, which means we needs to return true for those too.
         *
         * @see <a href="http://www.w3.org/TR/xslt20/#data-model">4 Data Model</a>
         */
        bool isWhitespace() const;

        /**
         * This function is not merged with handleStandardAttributes() because
         * handleStandardAttributes() needs to be called for all elements,
         * while validateElement() only applies to XSL-T elements.
         *
         * @see handleStandardAttributes()
         */
        void validateElement(const LookupKey name) const;

        QXmlStreamAttributes                                                    m_currentAttributes;

        bool                                                                    m_hasHandledStandardAttributes;

        /**
         * This stack mirrors the depth of elements in the parsed document. If
         * no @c xml:space is present on the current element, MaintainingReader
         * simply pushes the current top(). However, it never sets the value
         * depending on @c xml:space's value.
         */
        QStack<bool>                                                            m_stripWhitespace;

        /**
         * @short Returns the value for attribute by name \a name.
         *
         * If it doesn't exist, an error is raised.
         *
         * It is assumed that m_reader's current state is
         * QXmlStreamReader::StartElement.
         */
        QString readAttribute(const QString &localName,
                              const QString &namespaceURI = QString()) const;

        /**
         * @short Returns @c true if the current element has an attribute whose
         * name is @p namespaceURI and local name is @p localName.
         */
        bool hasAttribute(const QString &namespaceURI, const QString &localName) const;

        /**
         * @short Returns @c true if the current element has an attribute whose
         * local name is @p localName and namespace URI is null.
         */
        inline bool hasAttribute(const QString &localName) const;

    private:
        typename TokenLookupClass::NodeName                                     m_currentElementName;

        /**
         * This member is private, see the error() and warning() functions in
         * this class.
         */
        const ReportContext::Ptr                                                m_context;

        /**
         * Returns the current location that QXmlStreamReader has.
         */
        inline QSourceLocation currentLocation() const;

        const typename ElementDescription<TokenLookupClass, LookupKey>::Hash    m_elementDescriptions;
        const QSet<typename TokenLookupClass::NodeName>                         m_standardAttributes;
        Q_DISABLE_COPY(MaintainingReader)
    };

#include "qmaintainingreader_tpl_p.h"

}

QT_END_NAMESPACE
QT_END_HEADER

#endif