|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
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 ** |
|
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. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 /** |
|
43 * @file |
|
44 * @short This file is included by qacceltreebuilder_p.h. |
|
45 * If you need includes in this file, put them in qacceltreebuilder_p.h, outside of the namespace. |
|
46 */ |
|
47 |
|
48 template <bool FromDocument> |
|
49 AccelTreeBuilder<FromDocument>::AccelTreeBuilder(const QUrl &docURI, |
|
50 const QUrl &baseURI, |
|
51 const NamePool::Ptr &np, |
|
52 ReportContext *const context, |
|
53 Features features) : m_preNumber(-1) |
|
54 , m_isPreviousAtomic(false) |
|
55 , m_hasCharacters(false) |
|
56 , m_isCharactersCompressed(false) |
|
57 , m_namePool(np) |
|
58 , m_document(new AccelTree(docURI, baseURI)) |
|
59 , m_skippedDocumentNodes(0) |
|
60 , m_documentURI(docURI) |
|
61 , m_context(context) |
|
62 , m_features(features) |
|
63 { |
|
64 Q_ASSERT(m_namePool); |
|
65 |
|
66 /* TODO Perhaps we can merge m_ancestors and m_size |
|
67 * into one, and store a struct for the two instead? */ |
|
68 m_ancestors.reserve(DefaultNodeStackSize); |
|
69 m_ancestors.push(-1); |
|
70 |
|
71 m_size.reserve(DefaultNodeStackSize); |
|
72 m_size.push(0); |
|
73 } |
|
74 |
|
75 template <bool FromDocument> |
|
76 void AccelTreeBuilder<FromDocument>::startStructure() |
|
77 { |
|
78 if(m_hasCharacters) |
|
79 { |
|
80 /* We create a node even if m_characters is empty. |
|
81 * Remember that `text {""}' creates one text node |
|
82 * with string value "". */ |
|
83 |
|
84 ++m_preNumber; |
|
85 m_document->basicData.append(AccelTree::BasicNodeData(currentDepth(), |
|
86 currentParent(), |
|
87 QXmlNodeModelIndex::Text, |
|
88 m_isCharactersCompressed ? AccelTree::IsCompressed : 0)); |
|
89 m_document->data.insert(m_preNumber, m_characters); |
|
90 ++m_size.top(); |
|
91 |
|
92 m_characters.clear(); /* We don't want it added twice. */ |
|
93 m_hasCharacters = false; |
|
94 |
|
95 if(m_isCharactersCompressed) |
|
96 m_isCharactersCompressed = false; |
|
97 } |
|
98 } |
|
99 |
|
100 template <bool FromDocument> |
|
101 void AccelTreeBuilder<FromDocument>::item(const Item &it) |
|
102 { |
|
103 Q_ASSERT(it); |
|
104 |
|
105 if(it.isAtomicValue()) |
|
106 { |
|
107 if(m_isPreviousAtomic) |
|
108 { |
|
109 m_characters += QLatin1Char(' '); |
|
110 m_characters += it.stringValue(); |
|
111 } |
|
112 else |
|
113 { |
|
114 m_isPreviousAtomic = true; |
|
115 const QString sv(it.stringValue()); |
|
116 |
|
117 if(!sv.isEmpty()) |
|
118 { |
|
119 m_characters += sv; |
|
120 m_hasCharacters = true; |
|
121 } |
|
122 } |
|
123 } |
|
124 else |
|
125 sendAsNode(it); |
|
126 } |
|
127 |
|
128 template <bool FromDocument> |
|
129 void AccelTreeBuilder<FromDocument>::startElement(const QXmlName &name) |
|
130 { |
|
131 startElement(name, 1, 1); |
|
132 } |
|
133 |
|
134 template <bool FromDocument> |
|
135 void AccelTreeBuilder<FromDocument>::startElement(const QXmlName &name, qint64 line, qint64 column) |
|
136 { |
|
137 startStructure(); |
|
138 |
|
139 AccelTree::BasicNodeData data(currentDepth(), currentParent(), QXmlNodeModelIndex::Element, -1, name); |
|
140 m_document->basicData.append(data); |
|
141 if (m_features & SourceLocationsFeature) |
|
142 m_document->sourcePositions.insert(m_document->maximumPreNumber(), qMakePair(line, column)); |
|
143 |
|
144 ++m_preNumber; |
|
145 m_ancestors.push(m_preNumber); |
|
146 |
|
147 ++m_size.top(); |
|
148 m_size.push(0); |
|
149 |
|
150 /* With node constructors, we can receive names for which we have no namespace |
|
151 * constructors, such as in the query '<xs:space/>'. Since the 'xs' prefix has no |
|
152 * NamespaceConstructor in this case, we synthesize the namespace. |
|
153 * |
|
154 * In case we're constructing from an XML document we avoid the call because |
|
155 * although it's redundant, it's on extra virtual call for each element. */ |
|
156 if(!FromDocument) |
|
157 namespaceBinding(QXmlName(name.namespaceURI(), 0, name.prefix())); |
|
158 |
|
159 m_isPreviousAtomic = false; |
|
160 } |
|
161 |
|
162 template <bool FromDocument> |
|
163 void AccelTreeBuilder<FromDocument>::endElement() |
|
164 { |
|
165 startStructure(); |
|
166 const AccelTree::PreNumber index = m_ancestors.pop(); |
|
167 AccelTree::BasicNodeData &data = m_document->basicData[index]; |
|
168 |
|
169 /* Sub trees needs to be included in upper trees, so we add the count of this element |
|
170 * to our parent. */ |
|
171 m_size[m_size.count() - 2] += m_size.top(); |
|
172 |
|
173 data.setSize(m_size.pop()); |
|
174 m_isPreviousAtomic = false; |
|
175 } |
|
176 |
|
177 template <bool FromDocument> |
|
178 void AccelTreeBuilder<FromDocument>::attribute(const QXmlName &name, const QStringRef &value) |
|
179 { |
|
180 /* Attributes adds a namespace binding, so lets synthesize one. |
|
181 * |
|
182 * We optimize by checking whether we have a namespace for which a binding would |
|
183 * be generated. Happens relatively rarely. */ |
|
184 if(name.hasPrefix()) |
|
185 namespaceBinding(QXmlName(name.namespaceURI(), 0, name.prefix())); |
|
186 |
|
187 m_document->basicData.append(AccelTree::BasicNodeData(currentDepth(), currentParent(), QXmlNodeModelIndex::Attribute, 0, name)); |
|
188 ++m_preNumber; |
|
189 ++m_size.top(); |
|
190 |
|
191 m_isPreviousAtomic = false; |
|
192 |
|
193 if(name.namespaceURI() == StandardNamespaces::xml && name.localName() == StandardLocalNames::id) |
|
194 { |
|
195 const QString normalized(value.toString().simplified()); |
|
196 |
|
197 if(QXmlUtils::isNCName(normalized)) |
|
198 { |
|
199 const QXmlName::LocalNameCode id = m_namePool->allocateLocalName(normalized); |
|
200 |
|
201 const int oldSize = m_document->m_IDs.count(); |
|
202 m_document->m_IDs.insert(id, currentParent()); |
|
203 /* We don't run the value through m_attributeCompress here, because |
|
204 * the likelyhood of it deing identical to another attribute is |
|
205 * very small. */ |
|
206 m_document->data.insert(m_preNumber, normalized); |
|
207 |
|
208 /** |
|
209 * In the case that we're called for doc-available(), m_context is |
|
210 * null, and we need to flag somehow that we failed to load this |
|
211 * document. |
|
212 */ |
|
213 if(oldSize == m_document->m_IDs.count() && m_context) // TODO |
|
214 { |
|
215 Q_ASSERT(m_context); |
|
216 m_context->error(QtXmlPatterns::tr("An %1-attribute with value %2 has already been declared.") |
|
217 .arg(formatKeyword("xml:id"), |
|
218 formatData(normalized)), |
|
219 FromDocument ? ReportContext::FODC0002 : ReportContext::XQDY0091, |
|
220 this); |
|
221 } |
|
222 } |
|
223 else if(m_context) // TODO |
|
224 { |
|
225 Q_ASSERT(m_context); |
|
226 |
|
227 /* If we're building from an XML Document(e.g, we're fed from QXmlStreamReader, we raise FODC0002, |
|
228 * otherwise XQDY0091. */ |
|
229 m_context->error(QtXmlPatterns::tr("An %1-attribute must have a " |
|
230 "valid %2 as value, which %3 isn't.").arg(formatKeyword("xml:id"), |
|
231 formatType(m_namePool, BuiltinTypes::xsNCName), |
|
232 formatData(value.toString())), |
|
233 FromDocument ? ReportContext::FODC0002 : ReportContext::XQDY0091, |
|
234 this); |
|
235 } |
|
236 } |
|
237 else |
|
238 m_document->data.insert(m_preNumber, *m_attributeCompress.insert(value.toString())); |
|
239 } |
|
240 |
|
241 template <bool FromDocument> |
|
242 void AccelTreeBuilder<FromDocument>::characters(const QStringRef &ch) |
|
243 { |
|
244 |
|
245 /* If a text node constructor appears by itself, a node needs to |
|
246 * be created. Therefore, we set m_hasCharacters |
|
247 * if we're the only node. |
|
248 * However, if the text node appears as a child of a document or element |
|
249 * node it is discarded if it's empty. |
|
250 */ |
|
251 if(m_hasCharacters && m_isCharactersCompressed) |
|
252 { |
|
253 m_characters = CompressedWhitespace::decompress(m_characters); |
|
254 m_isCharactersCompressed = false; |
|
255 } |
|
256 |
|
257 m_characters += ch; |
|
258 |
|
259 m_isPreviousAtomic = false; |
|
260 m_hasCharacters = !m_characters.isEmpty() || m_preNumber == -1; /* -1 is our start value. */ |
|
261 } |
|
262 |
|
263 template <bool FromDocument> |
|
264 void AccelTreeBuilder<FromDocument>::whitespaceOnly(const QStringRef &ch) |
|
265 { |
|
266 Q_ASSERT(!ch.isEmpty()); |
|
267 Q_ASSERT(ch.toString().trimmed().isEmpty()); |
|
268 |
|
269 /* This gets problematic due to how QXmlStreamReader works(which |
|
270 * is the only one we get whitespaceOnly() events from). Namely, text intermingled |
|
271 * with CDATA gets reported as individual Characters events, and |
|
272 * QXmlStreamReader::isWhitespace() can return differently for each of those. However, |
|
273 * it will occur very rarely, so this workaround of 1) mistakenly compressing 2) decompressing 3) |
|
274 * appending, will happen infrequently. |
|
275 */ |
|
276 if(m_hasCharacters) |
|
277 { |
|
278 if(m_isCharactersCompressed) |
|
279 { |
|
280 m_characters = CompressedWhitespace::decompress(m_characters); |
|
281 m_isCharactersCompressed = false; |
|
282 } |
|
283 |
|
284 m_characters.append(ch.toString()); |
|
285 } |
|
286 else |
|
287 { |
|
288 /* We haven't received a text node previously. */ |
|
289 m_characters = CompressedWhitespace::compress(ch); |
|
290 m_isCharactersCompressed = true; |
|
291 m_isPreviousAtomic = false; |
|
292 m_hasCharacters = true; |
|
293 } |
|
294 } |
|
295 |
|
296 template <bool FromDocument> |
|
297 void AccelTreeBuilder<FromDocument>::processingInstruction(const QXmlName &target, |
|
298 const QString &data) |
|
299 { |
|
300 startStructure(); |
|
301 ++m_preNumber; |
|
302 m_document->data.insert(m_preNumber, data); |
|
303 |
|
304 m_document->basicData.append(AccelTree::BasicNodeData(currentDepth(), |
|
305 currentParent(), |
|
306 QXmlNodeModelIndex::ProcessingInstruction, |
|
307 0, |
|
308 target)); |
|
309 ++m_size.top(); |
|
310 m_isPreviousAtomic = false; |
|
311 } |
|
312 |
|
313 template <bool FromDocument> |
|
314 void AccelTreeBuilder<FromDocument>::comment(const QString &content) |
|
315 { |
|
316 startStructure(); |
|
317 m_document->basicData.append(AccelTree::BasicNodeData(currentDepth(), currentParent(), QXmlNodeModelIndex::Comment, 0)); |
|
318 ++m_preNumber; |
|
319 m_document->data.insert(m_preNumber, content); |
|
320 ++m_size.top(); |
|
321 } |
|
322 |
|
323 template <bool FromDocument> |
|
324 void AccelTreeBuilder<FromDocument>::namespaceBinding(const QXmlName &nb) |
|
325 { |
|
326 /* Note, because attribute() sometimes generate namespaceBinding() calls, this function |
|
327 * can be called after attributes, in contrast to what the class documentation says. This is ok, |
|
328 * as long as we're not dealing with public API. */ |
|
329 |
|
330 /* If we've received attributes, it means the element's size have changed and m_preNumber have advanced, |
|
331 * so "reverse back" to the actual element. */ |
|
332 const AccelTree::PreNumber pn = m_preNumber - m_size.top(); |
|
333 |
|
334 QVector<QXmlName> &nss = m_document->namespaces[pn]; |
|
335 |
|
336 /* "xml" hasn't been declared for each node, AccelTree::namespaceBindings() adds it, so avoid it |
|
337 * such that we don't get duplicates. */ |
|
338 if(nb.prefix() == StandardPrefixes::xml) |
|
339 return; |
|
340 |
|
341 /* If we already have the binding, skip it. */ |
|
342 const int len = nss.count(); |
|
343 for(int i = 0; i < len; ++i) |
|
344 { |
|
345 if(nss.at(i).prefix() == nb.prefix()) |
|
346 return; |
|
347 } |
|
348 |
|
349 nss.append(nb); |
|
350 } |
|
351 |
|
352 template <bool FromDocument> |
|
353 void AccelTreeBuilder<FromDocument>::startDocument() |
|
354 { |
|
355 /* If we have already received nodes, we can't add a document node. */ |
|
356 if(m_preNumber == -1) /* -1 is our start value. */ |
|
357 { |
|
358 m_size.push(0); |
|
359 m_document->basicData.append(AccelTree::BasicNodeData(0, -1, QXmlNodeModelIndex::Document, -1)); |
|
360 ++m_preNumber; |
|
361 m_ancestors.push(m_preNumber); |
|
362 } |
|
363 else |
|
364 ++m_skippedDocumentNodes; |
|
365 |
|
366 m_isPreviousAtomic = false; |
|
367 } |
|
368 |
|
369 template <bool FromDocument> |
|
370 void AccelTreeBuilder<FromDocument>::endDocument() |
|
371 { |
|
372 if(m_skippedDocumentNodes == 0) |
|
373 { |
|
374 /* Create text nodes, if we've received any. We do this only if we're the |
|
375 * top node because if we're getting this event as being a child of an element, |
|
376 * text nodes or atomic values can appear after us, and which must get |
|
377 * merged with the previous text. |
|
378 * |
|
379 * We call startStructure() before we pop the ancestor, such that the text node becomes |
|
380 * a child of this document node. */ |
|
381 startStructure(); |
|
382 |
|
383 m_document->basicData.first().setSize(m_size.pop()); |
|
384 m_ancestors.pop(); |
|
385 } |
|
386 else |
|
387 --m_skippedDocumentNodes; |
|
388 |
|
389 m_isPreviousAtomic = false; |
|
390 } |
|
391 |
|
392 template <bool FromDocument> |
|
393 void AccelTreeBuilder<FromDocument>::atomicValue(const QVariant &value) |
|
394 { |
|
395 Q_UNUSED(value); |
|
396 // TODO |
|
397 } |
|
398 |
|
399 template <bool FromDocument> |
|
400 QAbstractXmlNodeModel::Ptr AccelTreeBuilder<FromDocument>::builtDocument() |
|
401 { |
|
402 /* Create a text node, if we have received text in some way. */ |
|
403 startStructure(); |
|
404 m_document->printStats(m_namePool); |
|
405 |
|
406 return m_document; |
|
407 } |
|
408 |
|
409 template <bool FromDocument> |
|
410 NodeBuilder::Ptr AccelTreeBuilder<FromDocument>::create(const QUrl &baseURI) const |
|
411 { |
|
412 Q_UNUSED(baseURI); |
|
413 return NodeBuilder::Ptr(new AccelTreeBuilder(QUrl(), baseURI, m_namePool, m_context)); |
|
414 } |
|
415 |
|
416 template <bool FromDocument> |
|
417 void AccelTreeBuilder<FromDocument>::startOfSequence() |
|
418 { |
|
419 } |
|
420 |
|
421 template <bool FromDocument> |
|
422 void AccelTreeBuilder<FromDocument>::endOfSequence() |
|
423 { |
|
424 } |
|
425 |
|
426 template <bool FromDocument> |
|
427 const SourceLocationReflection *AccelTreeBuilder<FromDocument>::actualReflection() const |
|
428 { |
|
429 return this; |
|
430 } |
|
431 |
|
432 template <bool FromDocument> |
|
433 QSourceLocation AccelTreeBuilder<FromDocument>::sourceLocation() const |
|
434 { |
|
435 if(m_documentURI.isEmpty()) |
|
436 return QSourceLocation(QUrl(QLatin1String("AnonymousNodeTree"))); |
|
437 else |
|
438 return QSourceLocation(m_documentURI); |
|
439 } |
|
440 |