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

source code of qtxmlpatterns/src/xmlpatterns/acceltree/qacceltreebuilder_tpl_p.h