source: trunk/src/xmlpatterns/parser/qmaintainingreader_p.h@ 317

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

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

File size: 8.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//
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_MaintainingReader_H
53#define Patternist_MaintainingReader_H
54
55#include <QSet>
56#include <QSourceLocation>
57#include <QStack>
58#include <QStringList>
59#include <QXmlStreamReader>
60
61#include "qxpathhelper_p.h"
62
63class QUrl;
64
65QT_BEGIN_HEADER
66QT_BEGIN_NAMESPACE
67
68namespace QPatternist
69{
70 /**
71 * @short A structure that lists the optional and required
72 * attributes of an element. Used with MaintainingReader.
73 *
74 * A constant source to misunderstandings is mixing up the order of
75 * arguments for functions that takes a local name and a namespace. Be wary
76 * of this.
77 *
78 * @author Frans Englich <[email protected]>
79 * @since 4.5
80 */
81 template<typename TokenLookupClass,
82 typename LookupKey = typename TokenLookupClass::NodeName>
83 class ElementDescription
84 {
85 public:
86 typedef QHash<LookupKey, ElementDescription<TokenLookupClass, LookupKey> > Hash;
87 QSet<typename TokenLookupClass::NodeName> requiredAttributes;
88 QSet<typename TokenLookupClass::NodeName> optionalAttributes;
89 };
90
91 /**
92 * @short Base class for tokenizers that reads XML formats. This is
93 * XSLTTokenizer, and the W3C XML Schema parser.
94 *
95 * MaintainingReader is intended for sub-classing.
96 *
97 * @tparam TokenLookupClass The name of the class that is generated by
98 * QTokenAutomaton and which supplies tokenizing tokens. For XSLTTokenizer,
99 * this is XSLTTokenLookup, for instance.
100 *
101 * @tparam LookupKey The type that is passed to validateElement() and is
102 * the key in ElementDescription. For the schema code, where elements have
103 * different interpretations depending on context, the lookup key is hence
104 * not equal element name.
105 *
106 * @author Frans Englich <[email protected]>
107 * @since 4.5
108 */
109 template<typename TokenLookupClass,
110 typename LookupKey = typename TokenLookupClass::NodeName>
111 class MaintainingReader : public QXmlStreamReader
112 , protected TokenLookupClass
113 {
114 protected:
115
116 MaintainingReader(const typename ElementDescription<TokenLookupClass, LookupKey>::Hash &elementDescriptions,
117 const QSet<typename TokenLookupClass::NodeName> &standardAttributes,
118 const ReportContext::Ptr &context,
119 QIODevice *const queryDevice);
120
121 virtual ~MaintainingReader();
122
123 TokenType readNext();
124
125 /**
126 * Returns the name of the current element.
127 */
128 inline typename TokenLookupClass::NodeName currentElementName() const;
129
130 /**
131 * @short Convenience function for calling ReportContext::error().
132 */
133 void error(const QString &message,
134 const ReportContext::ErrorCode code) const;
135
136 /**
137 * @short Convenience function for calling ReportContext::warning().
138 */
139 void warning(const QString &message) const;
140
141 /**
142 * @short Returns the location of the document that MaintainingReader
143 * is parsing. Used for error reporting
144 */
145 virtual QUrl documentURI() const = 0;
146
147 /**
148 * @short Returns @c true, if any attribute is allowed on the
149 * element currently being validated.
150 */
151 virtual bool isAnyAttributeAllowed() const = 0;
152
153 /**
154 * QXmlStreamReader::isWhitespace() returns true for whitespace that is
155 * not expressed as character references, while XSL-T operatates ontop
156 * of the XDM, which means we needs to return true for those too.
157 *
158 * @see <a href="http://www.w3.org/TR/xslt20/#data-model">4 Data Model</a>
159 */
160 bool isWhitespace() const;
161
162 /**
163 * This function is not merged with handleStandardAttributes() because
164 * handleStandardAttributes() needs to be called for all elements,
165 * while validateElement() only applies to XSL-T elements.
166 *
167 * @see handleStandardAttributes()
168 */
169 void validateElement(const LookupKey name) const;
170
171 QXmlStreamAttributes m_currentAttributes;
172
173 bool m_hasHandledStandardAttributes;
174
175 /**
176 * This stack mirrors the depth of elements in the parsed document. If
177 * no @c xml:space is present on the current element, MaintainingReader
178 * simply pushes the current top(). However, it never sets the value
179 * depending on @c xml:space's value.
180 */
181 QStack<bool> m_stripWhitespace;
182
183 /**
184 * @short Returns the value for attribute by name \a name.
185 *
186 * If it doesn't exist, an error is raised.
187 *
188 * It is assumed that m_reader's current state is
189 * QXmlStreamReader::StartElement.
190 */
191 QString readAttribute(const QString &localName,
192 const QString &namespaceURI = QString()) const;
193
194 /**
195 * @short Returns @c true if the current element has an attribute whose
196 * name is @p namespaceURI and local name is @p localName.
197 */
198 bool hasAttribute(const QString &namespaceURI, const QString &localName) const;
199
200 /**
201 * @short Returns @c true if the current element has an attribute whose
202 * local name is @p localName and namespace URI is null.
203 */
204 inline bool hasAttribute(const QString &localName) const;
205
206 private:
207 typename TokenLookupClass::NodeName m_currentElementName;
208
209 /**
210 * This member is private, see the error() and warning() functions in
211 * this class.
212 */
213 const ReportContext::Ptr m_context;
214
215 /**
216 * Returns the current location that QXmlStreamReader has.
217 */
218 inline QSourceLocation currentLocation() const;
219
220 const typename ElementDescription<TokenLookupClass, LookupKey>::Hash m_elementDescriptions;
221 const QSet<typename TokenLookupClass::NodeName> m_standardAttributes;
222 Q_DISABLE_COPY(MaintainingReader)
223 };
224
225#include "qmaintainingreader.cpp"
226
227}
228
229QT_END_NAMESPACE
230QT_END_HEADER
231
232#endif
233
Note: See TracBrowser for help on using the repository browser.