source: trunk/src/corelib/xml/qxmlstream.g@ 440

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

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

File size: 55.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 QtCore 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-- This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
41-- WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
42--
43----------------------------------------------------------------------------
44
45%parser QXmlStreamReader_Table
46
47%merged_output qxmlstream_p.h
48
49%token NOTOKEN
50%token SPACE " "
51%token LANGLE "<"
52%token RANGLE ">"
53%token AMPERSAND "&"
54%token HASH "#"
55%token QUOTE "\'"
56%token DBLQUOTE "\""
57%token LBRACK "["
58%token RBRACK "]"
59%token LPAREN "("
60%token RPAREN ")"
61%token PIPE "|"
62%token EQ "="
63%token PERCENT "%"
64%token SLASH "/"
65%token COLON ":"
66%token SEMICOLON ";"
67%token COMMA ","
68%token DASH "-"
69%token PLUS "+"
70%token STAR "*"
71%token DOT "."
72%token QUESTIONMARK "?"
73%token BANG "!"
74%token LETTER "[a-zA-Z]"
75%token DIGIT "[0-9]"
76
77-- after langle_bang
78%token CDATA_START "[CDATA["
79%token DOCTYPE "DOCTYPE"
80%token ELEMENT "ELEMENT"
81%token ATTLIST "ATTLIST"
82%token ENTITY "ENTITY"
83%token NOTATION "NOTATION"
84
85-- entity decl
86%token SYSTEM "SYSTEM"
87%token PUBLIC "PUBLIC"
88%token NDATA "NDATA"
89
90-- default decl
91%token REQUIRED "REQUIRED"
92%token IMPLIED "IMPLIED"
93%token FIXED "FIXED"
94
95-- conent spec
96%token EMPTY "EMPTY"
97%token ANY "ANY"
98%token PCDATA "PCDATA"
99
100-- error
101%token ERROR
102
103-- entities
104%token PARSE_ENTITY
105%token ENTITY_DONE
106%token UNRESOLVED_ENTITY
107
108-- att type
109%token CDATA "CDATA"
110%token ID "ID"
111%token IDREF "IDREF"
112%token IDREFS "IDREFS"
113%token ENTITY "ENTITY"
114%token ENTITIES "ENTITIES"
115%token NMTOKEN "NMTOKEN"
116%token NMTOKENS "NMTOKENS"
117
118-- xml declaration
119%token XML "<?xml"
120%token VERSION "version"
121
122%nonassoc SHIFT_THERE
123%nonassoc AMPERSAND
124 BANG
125 COLON
126 COMMA
127 DASH
128 DBLQUOTE
129 DIGIT
130 DOT
131 ENTITY_DONE
132 EQ
133 HASH
134 LBRACK
135 LETTER
136 LPAREN
137 PERCENT
138 PIPE
139 PLUS
140 QUESTIONMARK
141 QUOTE
142 RANGLE
143 RBRACK
144 RPAREN
145 SEMICOLON
146 SLASH
147 SPACE
148 STAR
149
150%start document
151
152/.
153template <typename T> class QXmlStreamSimpleStack {
154 T *data;
155 int tos, cap;
156public:
157 inline QXmlStreamSimpleStack():data(0), tos(-1), cap(0){}
158 inline ~QXmlStreamSimpleStack(){ if (data) qFree(data); }
159
160 inline void reserve(int extraCapacity) {
161 if (tos + extraCapacity + 1 > cap) {
162 cap = qMax(tos + extraCapacity + 1, cap << 1 );
163 data = reinterpret_cast<T *>(qRealloc(data, cap * sizeof(T)));
164 }
165 }
166
167 inline T &push() { reserve(1); return data[++tos]; }
168 inline T &rawPush() { return data[++tos]; }
169 inline const T &top() const { return data[tos]; }
170 inline T &top() { return data[tos]; }
171 inline T &pop() { return data[tos--]; }
172 inline T &operator[](int index) { return data[index]; }
173 inline const T &at(int index) const { return data[index]; }
174 inline int size() const { return tos + 1; }
175 inline void resize(int s) { tos = s - 1; }
176 inline bool isEmpty() const { return tos < 0; }
177 inline void clear() { tos = -1; }
178};
179
180
181class QXmlStream
182{
183 Q_DECLARE_TR_FUNCTIONS(QXmlStream)
184};
185
186class QXmlStreamPrivateTagStack {
187public:
188 struct NamespaceDeclaration
189 {
190 QStringRef prefix;
191 QStringRef namespaceUri;
192 };
193
194 struct Tag
195 {
196 QStringRef name;
197 QStringRef qualifiedName;
198 NamespaceDeclaration namespaceDeclaration;
199 int tagStackStringStorageSize;
200 int namespaceDeclarationsSize;
201 };
202
203
204 QXmlStreamPrivateTagStack();
205 QXmlStreamSimpleStack<NamespaceDeclaration> namespaceDeclarations;
206 QString tagStackStringStorage;
207 int tagStackStringStorageSize;
208 bool tagsDone;
209
210 inline QStringRef addToStringStorage(const QStringRef &s) {
211 int pos = tagStackStringStorageSize;
212 int sz = s.size();
213 if (pos != tagStackStringStorage.size())
214 tagStackStringStorage.resize(pos);
215 tagStackStringStorage.insert(pos, s.unicode(), sz);
216 tagStackStringStorageSize += sz;
217 return QStringRef(&tagStackStringStorage, pos, sz);
218 }
219 inline QStringRef addToStringStorage(const QString &s) {
220 int pos = tagStackStringStorageSize;
221 int sz = s.size();
222 if (pos != tagStackStringStorage.size())
223 tagStackStringStorage.resize(pos);
224 tagStackStringStorage.insert(pos, s.unicode(), sz);
225 tagStackStringStorageSize += sz;
226 return QStringRef(&tagStackStringStorage, pos, sz);
227 }
228
229 QXmlStreamSimpleStack<Tag> tagStack;
230
231
232 inline Tag &tagStack_pop() {
233 Tag& tag = tagStack.pop();
234 tagStackStringStorageSize = tag.tagStackStringStorageSize;
235 namespaceDeclarations.resize(tag.namespaceDeclarationsSize);
236 tagsDone = tagStack.isEmpty();
237 return tag;
238 }
239 inline Tag &tagStack_push() {
240 Tag &tag = tagStack.push();
241 tag.tagStackStringStorageSize = tagStackStringStorageSize;
242 tag.namespaceDeclarationsSize = namespaceDeclarations.size();
243 return tag;
244 }
245};
246
247
248class QXmlStreamEntityResolver;
249
250class QXmlStreamReaderPrivate : public QXmlStreamReader_Table, public QXmlStreamPrivateTagStack{
251 QXmlStreamReader *q_ptr;
252 Q_DECLARE_PUBLIC(QXmlStreamReader)
253public:
254 QXmlStreamReaderPrivate(QXmlStreamReader *q);
255 ~QXmlStreamReaderPrivate();
256 void init();
257
258 QByteArray rawReadBuffer;
259 QByteArray dataBuffer;
260 uchar firstByte;
261 qint64 nbytesread;
262 QString readBuffer;
263 int readBufferPos;
264 QXmlStreamSimpleStack<uint> putStack;
265 struct Entity {
266 Entity(const QString& str = QString())
267 :value(str), external(false), unparsed(false), literal(false),
268 hasBeenParsed(false), isCurrentlyReferenced(false){}
269 static inline Entity createLiteral(const QString &entity)
270 { Entity result(entity); result.literal = result.hasBeenParsed = true; return result; }
271 QString value;
272 uint external : 1;
273 uint unparsed : 1;
274 uint literal : 1;
275 uint hasBeenParsed : 1;
276 uint isCurrentlyReferenced : 1;
277 };
278 QHash<QString, Entity> entityHash;
279 QHash<QString, Entity> parameterEntityHash;
280 QXmlStreamSimpleStack<Entity *>entityReferenceStack;
281 inline bool referenceEntity(Entity &entity) {
282 if (entity.isCurrentlyReferenced) {
283 raiseWellFormedError(QXmlStream::tr("Recursive entity detected."));
284 return false;
285 }
286 entity.isCurrentlyReferenced = true;
287 entityReferenceStack.push() = &entity;
288 injectToken(ENTITY_DONE);
289 return true;
290 }
291
292
293 QIODevice *device;
294 bool deleteDevice;
295#ifndef QT_NO_TEXTCODEC
296 QTextCodec *codec;
297 QTextDecoder *decoder;
298#endif
299 bool atEnd;
300
301 /*!
302 \sa setType()
303 */
304 QXmlStreamReader::TokenType type;
305 QXmlStreamReader::Error error;
306 QString errorString;
307 QString unresolvedEntity;
308
309 qint64 lineNumber, lastLineStart, characterOffset;
310
311
312 void write(const QString &);
313 void write(const char *);
314
315
316 QXmlStreamAttributes attributes;
317 QStringRef namespaceForPrefix(const QStringRef &prefix);
318 void resolveTag();
319 void resolvePublicNamespaces();
320 void resolveDtd();
321 uint resolveCharRef(int symbolIndex);
322 bool checkStartDocument();
323 void startDocument();
324 void parseError();
325 void checkPublicLiteral(const QStringRef &publicId);
326
327 bool scanDtd;
328 QStringRef lastAttributeValue;
329 bool lastAttributeIsCData;
330 struct DtdAttribute {
331 QStringRef tagName;
332 QStringRef attributeQualifiedName;
333 QStringRef attributePrefix;
334 QStringRef attributeName;
335 QStringRef defaultValue;
336 bool isCDATA;
337 bool isNamespaceAttribute;
338 };
339 QXmlStreamSimpleStack<DtdAttribute> dtdAttributes;
340 struct NotationDeclaration {
341 QStringRef name;
342 QStringRef publicId;
343 QStringRef systemId;
344 };
345 QXmlStreamSimpleStack<NotationDeclaration> notationDeclarations;
346 QXmlStreamNotationDeclarations publicNotationDeclarations;
347 QXmlStreamNamespaceDeclarations publicNamespaceDeclarations;
348
349 struct EntityDeclaration {
350 QStringRef name;
351 QStringRef notationName;
352 QStringRef publicId;
353 QStringRef systemId;
354 QStringRef value;
355 bool parameter;
356 bool external;
357 inline void clear() {
358 name.clear();
359 notationName.clear();
360 publicId.clear();
361 systemId.clear();
362 value.clear();
363 parameter = external = false;
364 }
365 };
366 QXmlStreamSimpleStack<EntityDeclaration> entityDeclarations;
367 QXmlStreamEntityDeclarations publicEntityDeclarations;
368
369 QStringRef text;
370
371 QStringRef prefix, namespaceUri, qualifiedName, name;
372 QStringRef processingInstructionTarget, processingInstructionData;
373 QStringRef dtdName, dtdPublicId, dtdSystemId;
374 QStringRef documentVersion, documentEncoding;
375 uint isEmptyElement : 1;
376 uint isWhitespace : 1;
377 uint isCDATA : 1;
378 uint standalone : 1;
379 uint hasCheckedStartDocument : 1;
380 uint normalizeLiterals : 1;
381 uint hasSeenTag : 1;
382 uint inParseEntity : 1;
383 uint referenceToUnparsedEntityDetected : 1;
384 uint referenceToParameterEntityDetected : 1;
385 uint hasExternalDtdSubset : 1;
386 uint lockEncoding : 1;