1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
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.
48 template <bool FromDocument>
49 AccelTreeBuilder<FromDocument>::AccelTreeBuilder(const QUrl &docURI,
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)
58 , m_document(new AccelTree(docURI, baseURI))
59 , m_skippedDocumentNodes(0)
60 , m_documentURI(docURI)
62 , m_features(features)
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);
71 m_size.reserve(DefaultNodeStackSize);
75 template <bool FromDocument>
76 void AccelTreeBuilder<FromDocument>::startStructure()
80 /* We create a node even if m_characters is empty.
81 * Remember that `text {""}' creates one text node
82 * with string value "". */
85 m_document->basicData.append(AccelTree::BasicNodeData(currentDepth(),
87 QXmlNodeModelIndex::Text,
88 m_isCharactersCompressed ? AccelTree::IsCompressed : 0));
89 m_document->data.insert(m_preNumber, m_characters);
92 m_characters.clear(); /* We don't want it added twice. */
93 m_hasCharacters = false;
95 if(m_isCharactersCompressed)
96 m_isCharactersCompressed = false;
100 template <bool FromDocument>
101 void AccelTreeBuilder<FromDocument>::item(const Item &it)
105 if(it.isAtomicValue())
107 if(m_isPreviousAtomic)
109 m_characters += QLatin1Char(' ');
110 m_characters += it.stringValue();
114 m_isPreviousAtomic = true;
115 const QString sv(it.stringValue());
120 m_hasCharacters = true;
128 template <bool FromDocument>
129 void AccelTreeBuilder<FromDocument>::startElement(const QXmlName &name)
131 startElement(name, 1, 1);
134 template <bool FromDocument>
135 void AccelTreeBuilder<FromDocument>::startElement(const QXmlName &name, qint64 line, qint64 column)
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));
145 m_ancestors.push(m_preNumber);
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.
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. */
157 namespaceBinding(QXmlName(name.namespaceURI(), 0, name.prefix()));
159 m_isPreviousAtomic = false;
162 template <bool FromDocument>
163 void AccelTreeBuilder<FromDocument>::endElement()
166 const AccelTree::PreNumber index = m_ancestors.pop();
167 AccelTree::BasicNodeData &data = m_document->basicData[index];
169 /* Sub trees needs to be included in upper trees, so we add the count of this element
171 m_size[m_size.count() - 2] += m_size.top();
173 data.setSize(m_size.pop());
174 m_isPreviousAtomic = false;
177 template <bool FromDocument>
178 void AccelTreeBuilder<FromDocument>::attribute(const QXmlName &name, const QStringRef &value)
180 /* Attributes adds a namespace binding, so lets synthesize one.
182 * We optimize by checking whether we have a namespace for which a binding would
183 * be generated. Happens relatively rarely. */
185 namespaceBinding(QXmlName(name.namespaceURI(), 0, name.prefix()));
187 m_document->basicData.append(AccelTree::BasicNodeData(currentDepth(), currentParent(), QXmlNodeModelIndex::Attribute, 0, name));
191 m_isPreviousAtomic = false;
193 if(name.namespaceURI() == StandardNamespaces::xml && name.localName() == StandardLocalNames::id)
195 const QString normalized(value.toString().simplified());
197 if(QXmlUtils::isNCName(normalized))
199 const QXmlName::LocalNameCode id = m_namePool->allocateLocalName(normalized);
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
206 m_document->data.insert(m_preNumber, normalized);
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
213 if(oldSize == m_document->m_IDs.count() && m_context) // TODO
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,
223 else if(m_context) // TODO
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,
238 m_document->data.insert(m_preNumber, *m_attributeCompress.insert(value.toString()));
241 template <bool FromDocument>
242 void AccelTreeBuilder<FromDocument>::characters(const QStringRef &ch)
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.
251 if(m_hasCharacters && m_isCharactersCompressed)
253 m_characters = CompressedWhitespace::decompress(m_characters);
254 m_isCharactersCompressed = false;
259 m_isPreviousAtomic = false;
260 m_hasCharacters = !m_characters.isEmpty() || m_preNumber == -1; /* -1 is our start value. */
263 template <bool FromDocument>
264 void AccelTreeBuilder<FromDocument>::whitespaceOnly(const QStringRef &ch)
266 Q_ASSERT(!ch.isEmpty());
267 Q_ASSERT(ch.toString().trimmed().isEmpty());
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.
278 if(m_isCharactersCompressed)
280 m_characters = CompressedWhitespace::decompress(m_characters);
281 m_isCharactersCompressed = false;
284 m_characters.append(ch.toString());
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;
296 template <bool FromDocument>
297 void AccelTreeBuilder<FromDocument>::processingInstruction(const QXmlName &target,
302 m_document->data.insert(m_preNumber, data);
304 m_document->basicData.append(AccelTree::BasicNodeData(currentDepth(),
306 QXmlNodeModelIndex::ProcessingInstruction,
310 m_isPreviousAtomic = false;
313 template <bool FromDocument>
314 void AccelTreeBuilder<FromDocument>::comment(const QString &content)
317 m_document->basicData.append(AccelTree::BasicNodeData(currentDepth(), currentParent(), QXmlNodeModelIndex::Comment, 0));
319 m_document->data.insert(m_preNumber, content);
323 template <bool FromDocument>
324 void AccelTreeBuilder<FromDocument>::namespaceBinding(const QXmlName &nb)
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. */
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();
334 QVector<QXmlName> &nss = m_document->namespaces[pn];
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)
341 /* If we already have the binding, skip it. */
342 const int len = nss.count();
343 for(int i = 0; i < len; ++i)
345 if(nss.at(i).prefix() == nb.prefix())
352 template <bool FromDocument>
353 void AccelTreeBuilder<FromDocument>::startDocument()
355 /* If we have already received nodes, we can't add a document node. */
356 if(m_preNumber == -1) /* -1 is our start value. */
359 m_document->basicData.append(AccelTree::BasicNodeData(0, -1, QXmlNodeModelIndex::Document, -1));
361 m_ancestors.push(m_preNumber);
364 ++m_skippedDocumentNodes;
366 m_isPreviousAtomic = false;
369 template <bool FromDocument>
370 void AccelTreeBuilder<FromDocument>::endDocument()
372 if(m_skippedDocumentNodes == 0)
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.
379 * We call startStructure() before we pop the ancestor, such that the text node becomes
380 * a child of this document node. */
383 m_document->basicData.first().setSize(m_size.pop());
387 --m_skippedDocumentNodes;
389 m_isPreviousAtomic = false;
392 template <bool FromDocument>
393 void AccelTreeBuilder<FromDocument>::atomicValue(const QVariant &value)
399 template <bool FromDocument>
400 QAbstractXmlNodeModel::Ptr AccelTreeBuilder<FromDocument>::builtDocument()
402 /* Create a text node, if we have received text in some way. */
404 m_document->printStats(m_namePool);
409 template <bool FromDocument>
410 NodeBuilder::Ptr AccelTreeBuilder<FromDocument>::create(const QUrl &baseURI) const
413 return NodeBuilder::Ptr(new AccelTreeBuilder(QUrl(), baseURI, m_namePool, m_context));
416 template <bool FromDocument>
417 void AccelTreeBuilder<FromDocument>::startOfSequence()
421 template <bool FromDocument>
422 void AccelTreeBuilder<FromDocument>::endOfSequence()
426 template <bool FromDocument>
427 const SourceLocationReflection *AccelTreeBuilder<FromDocument>::actualReflection() const
432 template <bool FromDocument>
433 QSourceLocation AccelTreeBuilder<FromDocument>::sourceLocation() const
435 if(m_documentURI.isEmpty())
436 return QSourceLocation(QUrl(QLatin1String("AnonymousNodeTree")));
438 return QSourceLocation(m_documentURI);